From 521e2b7f0f97653745ee74172759fc96948c57a6 Mon Sep 17 00:00:00 2001 From: BONNAURE Olivier Date: Fri, 12 Apr 2024 16:07:18 +0200 Subject: [PATCH] optimize the 19th caracter --- tool/net/lfuncs.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tool/net/lfuncs.c b/tool/net/lfuncs.c index 047d017c5..79f0d1ca0 100644 --- a/tool/net/lfuncs.c +++ b/tool/net/lfuncs.c @@ -834,22 +834,20 @@ int LuaUuidV4(lua_State *L) { '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; char uuid_str[37] = {0}; uint64_t r = _rand64(); - for (int i = 0, j = 0; i < 36; ++i, ++j) { + int j = 0; + for (int i = 0; i < 36; ++i, ++j) { if (j == 16) { r = _rand64(); j = 0; } - if (i == 19) { - uuid_str[i] = v[8 + r % 4]; - } else { - uuid_str[i] = v[(r & (0xfull << (j * 4ull))) >> (j * 4ull)]; - } + uuid_str[i] = v[(r & (0xfull << (j * 4ull))) >> (j * 4ull)]; } uuid_str[8] = '-'; uuid_str[13] = '-'; uuid_str[14] = '4'; uuid_str[18] = '-'; + uuid_str[19] = v[8 | (r & (0x3ull << (j * 4ull))) >> (j * 4ull)]; uuid_str[23] = '-'; uuid_str[36] = '\0'; lua_pushfstring(L, uuid_str);