mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 19:58:30 +00:00
[Redbean] Add UuidV4 method (#1140)
This commit is contained in:
parent
3e16e59f72
commit
39dde41516
6 changed files with 59 additions and 0 deletions
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 *);
|
||||
|
|
|
@ -5287,6 +5287,7 @@ static const luaL_Reg kLuaFuncs[] = {
|
|||
{"StoreAsset", LuaStoreAsset}, //
|
||||
{"Uncompress", LuaUncompress}, //
|
||||
{"Underlong", LuaUnderlong}, //
|
||||
{"UuidV4", LuaUuidV4}, //
|
||||
{"VisualizeControlCodes", LuaVisualizeControlCodes}, //
|
||||
{"Write", LuaWrite}, //
|
||||
{"bin", LuaBin}, //
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue