Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[redbean] Add UuidV4 method #1140

Merged
merged 18 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions test/tool/net/uuidv4_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Copyright 2022 Justine Alexandra Roberts Tunney
mrdomino marked this conversation as resolved.
Show resolved Hide resolved
--
-- 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
mrdomino marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 4 additions & 0 deletions tool/net/definitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions tool/net/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions tool/net/lfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,52 @@ int LuaVisualizeControlCodes(lua_State *L) {
return LuaCoder(L, VisualizeControlCodes);
}

int LuaUuidV4(lua_State *L) {
mrdomino marked this conversation as resolved.
Show resolved Hide resolved
char v[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
static char uuid_str[37] = {0};
mrdomino marked this conversation as resolved.
Show resolved Hide resolved

uuid_str[0] = v[_rand64() % 16];
uuid_str[1] = v[_rand64() % 16];
uuid_str[2] = v[_rand64() % 16];
uuid_str[3] = v[_rand64() % 16];
uuid_str[4] = v[_rand64() % 16];
uuid_str[5] = v[_rand64() % 16];
uuid_str[6] = v[_rand64() % 16];
uuid_str[7] = v[_rand64() % 16];
uuid_str[8] = '-';
uuid_str[9] = v[_rand64() % 16];
uuid_str[10] = v[_rand64() % 16];
uuid_str[11] = v[_rand64() % 16];
uuid_str[12] = v[_rand64() % 16];
uuid_str[13] = '-';
uuid_str[14] = '4';
uuid_str[15] = v[_rand64() % 16];
uuid_str[16] = v[_rand64() % 16];
uuid_str[17] = v[_rand64() % 16];
uuid_str[18] = '-';
uuid_str[19] = v[8 + _rand64() % 4];
uuid_str[20] = v[_rand64() % 16];
uuid_str[21] = v[_rand64() % 16];
uuid_str[22] = v[_rand64() % 16];
uuid_str[23] = '-';
uuid_str[24] = v[_rand64() % 16];
uuid_str[25] = v[_rand64() % 16];
uuid_str[26] = v[_rand64() % 16];
uuid_str[27] = v[_rand64() % 16];
uuid_str[28] = v[_rand64() % 16];
uuid_str[29] = v[_rand64() % 16];
uuid_str[30] = v[_rand64() % 16];
uuid_str[31] = v[_rand64() % 16];
uuid_str[32] = v[_rand64() % 16];
uuid_str[33] = v[_rand64() % 16];
uuid_str[34] = v[_rand64() % 16];
uuid_str[35] = v[_rand64() % 16];
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;
Expand Down
1 change: 1 addition & 0 deletions tool/net/lfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
1 change: 1 addition & 0 deletions tool/net/redbean.c
Original file line number Diff line number Diff line change
Expand Up @@ -5287,6 +5287,7 @@ static const luaL_Reg kLuaFuncs[] = {
{"StoreAsset", LuaStoreAsset}, //
{"Uncompress", LuaUncompress}, //
{"Underlong", LuaUnderlong}, //
{"UuidV4", LuaUuidV4}, //
{"VisualizeControlCodes", LuaVisualizeControlCodes}, //
{"Write", LuaWrite}, //
{"bin", LuaBin}, //
Expand Down
Loading