[Redbean] Add UuidV4 method (#1140)

This commit is contained in:
BONNAURE Olivier 2024-04-12 17:10:27 +02:00 committed by GitHub
parent 3e16e59f72
commit 39dde41516
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1,25 @@
-- Copyright 2024 Justine Alexandra Roberts Tunney
--
-- Permission to use, copy, modify, and/or distribute this software for
-- any purpose with or without fee is hereby granted, provided that the
-- above copyright notice and this permission notice appear in all copies.
--
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
-- DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
-- PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
-- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-- PERFORMANCE OF THIS SOFTWARE.
for i = 1, 1000000 do
local uuid = UuidV4()
assert(#uuid == 36)
assert(string.sub(uuid, 9, 9) == "-")
assert(string.sub(uuid, 14, 14) == "-")
assert(string.sub(uuid, 15, 15) == "4")
assert(string.sub(uuid, 19, 19) == "-")
y = string.sub(uuid, 20, 20)
assert(y == "8" or y == "9" or y == "a" or y == "b")
assert(string.sub(uuid, 24, 24) == "-")
end

View file

@ -1977,6 +1977,10 @@ function VisualizeControlCodes(str) end
---@nodiscard
function Underlong(str) end
--- Generate a uuid_v4
--- @return string
function UuidV4() end
---@param x integer
---@return integer # position of first bit set.
--- Passing `0` will raise an error. Same as the Intel x86 instruction BSF.

View file

@ -1062,6 +1062,9 @@ FUNCTIONS
Server Name Indicator (SNI) when performing Fetch() requests.
This function is not available in unsecure mode.
UuidV4() -> str
Returns an uuid v4 string.
Fetch(url:str[,body:str|{method=value:str,body=value:str,headers=table,...}])
├─→ status:int, {header:str=value:str,...}, body:str
└─→ nil, error:str

View file

@ -829,6 +829,31 @@ int LuaVisualizeControlCodes(lua_State *L) {
return LuaCoder(L, VisualizeControlCodes);
}
int LuaUuidV4(lua_State *L) {
static const char v[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
char uuid_str[37] = {0};
uint64_t r = _rand64();
int j = 0;
for (int i = 0; i < 36; ++i, ++j) {
if (j == 16) {
r = _rand64();
j = 0;
}
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);
return 1;
}
static dontinline int LuaHasherImpl(lua_State *L, size_t k,
int H(const void *, size_t, uint8_t *)) {
size_t n;

View file

@ -90,6 +90,7 @@ int LuaSleep(lua_State *);
int LuaSlurp(lua_State *);
int LuaUncompress(lua_State *);
int LuaUnderlong(lua_State *);
int LuaUuidV4(lua_State *);
int LuaVisualizeControlCodes(lua_State *);
void LuaPushUrlView(lua_State *, struct UrlView *);

View file

@ -5287,6 +5287,7 @@ static const luaL_Reg kLuaFuncs[] = {
{"StoreAsset", LuaStoreAsset}, //
{"Uncompress", LuaUncompress}, //
{"Underlong", LuaUnderlong}, //
{"UuidV4", LuaUuidV4}, //
{"VisualizeControlCodes", LuaVisualizeControlCodes}, //
{"Write", LuaWrite}, //
{"bin", LuaBin}, //