mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-05 09:20:29 +00:00
feat: Redbean => add new UuidV4 method
This commit is contained in:
parent
b9d6e6e348
commit
2dfc9bb32d
4 changed files with 31 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.
|
||||
|
|
|
@ -72,6 +72,7 @@
|
|||
#include "third_party/mbedtls/sha512.h"
|
||||
#include "third_party/musl/netdb.h"
|
||||
#include "third_party/zlib/zlib.h"
|
||||
#include <time.h>
|
||||
|
||||
static int Rdpid(void) {
|
||||
#ifdef __x86_64__
|
||||
|
@ -829,6 +830,30 @@ int LuaVisualizeControlCodes(lua_State *L) {
|
|||
return LuaCoder(L, VisualizeControlCodes);
|
||||
}
|
||||
|
||||
int LuaUuidV4(lua_State *L) {
|
||||
char *template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
|
||||
char *uuid = (char *)malloc(strlen(template) + 1);
|
||||
if (uuid == NULL) {
|
||||
return NULL; // Memory allocation failed
|
||||
}
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
for (int i = 0; i < strlen(template); i++) {
|
||||
char c = template[i];
|
||||
if (c == 'x') {
|
||||
uuid[i] = (rand() % 16 < 10) ? (rand() % 10 + '0') : (rand() % 6 + 'a');
|
||||
} else if (c == 'y') {
|
||||
uuid[i] = (rand() % 4 + '8');
|
||||
} else {
|
||||
uuid[i] = c;
|
||||
}
|
||||
}
|
||||
uuid[strlen(template)] = '\0';
|
||||
lua_pushfstring(L, uuid);
|
||||
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