Skip to content

Commit 39dde41

Browse files
authored
[Redbean] Add UuidV4 method (#1140)
1 parent 3e16e59 commit 39dde41

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

test/tool/net/uuidv4_test.lua

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

tool/net/definitions.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,10 @@ function VisualizeControlCodes(str) end
19771977
---@nodiscard
19781978
function Underlong(str) end
19791979

1980+
--- Generate a uuid_v4
1981+
--- @return string
1982+
function UuidV4() end
1983+
19801984
---@param x integer
19811985
---@return integer # position of first bit set.
19821986
--- Passing `0` will raise an error. Same as the Intel x86 instruction BSF.

tool/net/help.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,9 @@ FUNCTIONS
10621062
Server Name Indicator (SNI) when performing Fetch() requests.
10631063
This function is not available in unsecure mode.
10641064

1065+
UuidV4() -> str
1066+
Returns an uuid v4 string.
1067+
10651068
Fetch(url:str[,body:str|{method=value:str,body=value:str,headers=table,...}])
10661069
├─→ status:int, {header:str=value:str,...}, body:str
10671070
└─→ nil, error:str

tool/net/lfuncs.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,31 @@ int LuaVisualizeControlCodes(lua_State *L) {
829829
return LuaCoder(L, VisualizeControlCodes);
830830
}
831831

832+
int LuaUuidV4(lua_State *L) {
833+
static const char v[] = {'0', '1', '2', '3', '4', '5', '6', '7',
834+
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
835+
char uuid_str[37] = {0};
836+
uint64_t r = _rand64();
837+
int j = 0;
838+
for (int i = 0; i < 36; ++i, ++j) {
839+
if (j == 16) {
840+
r = _rand64();
841+
j = 0;
842+
}
843+
uuid_str[i] = v[(r & (0xfull << (j * 4ull))) >> (j * 4ull)];
844+
}
845+
846+
uuid_str[8] = '-';
847+
uuid_str[13] = '-';
848+
uuid_str[14] = '4';
849+
uuid_str[18] = '-';
850+
uuid_str[19] = v[8 | (r & (0x3ull << (j * 4ull))) >> (j * 4ull)];
851+
uuid_str[23] = '-';
852+
uuid_str[36] = '\0';
853+
lua_pushfstring(L, uuid_str);
854+
return 1;
855+
}
856+
832857
static dontinline int LuaHasherImpl(lua_State *L, size_t k,
833858
int H(const void *, size_t, uint8_t *)) {
834859
size_t n;

tool/net/lfuncs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ int LuaSleep(lua_State *);
9090
int LuaSlurp(lua_State *);
9191
int LuaUncompress(lua_State *);
9292
int LuaUnderlong(lua_State *);
93+
int LuaUuidV4(lua_State *);
9394
int LuaVisualizeControlCodes(lua_State *);
9495

9596
void LuaPushUrlView(lua_State *, struct UrlView *);

tool/net/redbean.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5287,6 +5287,7 @@ static const luaL_Reg kLuaFuncs[] = {
52875287
{"StoreAsset", LuaStoreAsset}, //
52885288
{"Uncompress", LuaUncompress}, //
52895289
{"Underlong", LuaUnderlong}, //
5290+
{"UuidV4", LuaUuidV4}, //
52905291
{"VisualizeControlCodes", LuaVisualizeControlCodes}, //
52915292
{"Write", LuaWrite}, //
52925293
{"bin", LuaBin}, //

0 commit comments

Comments
 (0)