mirror of
https://github.com/Ed94/metadesk.git
synced 2026-08-08 16:48:15 +00:00
string -> integer implementation
This commit is contained in:
+32
-3
@@ -485,9 +485,38 @@ MD_JoinStringListWithSeparator(MD_String8List list, MD_String8 separator)
|
||||
MD_FUNCTION_IMPL MD_i64
|
||||
MD_I64FromString(MD_String8 string, MD_u32 radix)
|
||||
{
|
||||
char str[64];
|
||||
_MD_WriteStringToBuffer(string, sizeof(str), str);
|
||||
return strtoll(str, 0, radix);
|
||||
MD_Assert(2 <= radix && radix <= 16);
|
||||
|
||||
static MD_u8 char_to_value[] = {
|
||||
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
|
||||
0x08,0x09,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
||||
0xFF,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0xFF,
|
||||
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
||||
};
|
||||
|
||||
// get the sign
|
||||
MD_i64 sign = +1;
|
||||
MD_u64 i = 0;
|
||||
if (string.size > 0){
|
||||
if (string.str[i] == '-'){
|
||||
i = 1;
|
||||
sign = -1;
|
||||
}
|
||||
if (string.str[i] == '+'){
|
||||
i = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// get the value
|
||||
MD_i64 value = 0;
|
||||
for (;i < string.size; i += 1){
|
||||
value *= radix;
|
||||
MD_u8 c = string.str[i];
|
||||
value += char_to_value[(c - 0x30)&0x1F];
|
||||
}
|
||||
value *= sign;
|
||||
|
||||
return(value);
|
||||
}
|
||||
|
||||
MD_FUNCTION_IMPL MD_f64
|
||||
|
||||
Reference in New Issue
Block a user