string -> integer implementation

This commit is contained in:
Allen Webster
2021-03-14 17:35:12 -07:00
parent 084d55d55d
commit 30b8466a12
+35 -6
View File
@@ -485,9 +485,38 @@ MD_JoinStringListWithSeparator(MD_String8List list, MD_String8 separator)
MD_FUNCTION_IMPL MD_i64 MD_FUNCTION_IMPL MD_i64
MD_I64FromString(MD_String8 string, MD_u32 radix) MD_I64FromString(MD_String8 string, MD_u32 radix)
{ {
char str[64]; MD_Assert(2 <= radix && radix <= 16);
_MD_WriteStringToBuffer(string, sizeof(str), str);
return strtoll(str, 0, radix); 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 MD_FUNCTION_IMPL MD_f64
@@ -1625,9 +1654,9 @@ _MD_ParseOneNode(MD_ParseCtx *ctx)
// NOTE(rjf): Unnamed Sets // NOTE(rjf): Unnamed Sets
if((MD_Parse_TokenMatch(next_token, MD_S8Lit("("), 0) || if((MD_Parse_TokenMatch(next_token, MD_S8Lit("("), 0) ||
MD_Parse_TokenMatch(next_token, MD_S8Lit("{"), 0) || MD_Parse_TokenMatch(next_token, MD_S8Lit("{"), 0) ||
MD_Parse_TokenMatch(next_token, MD_S8Lit("["), 0)) && MD_Parse_TokenMatch(next_token, MD_S8Lit("["), 0)) &&
next_token.kind == MD_TokenKind_Symbol ) next_token.kind == MD_TokenKind_Symbol )
{ {
result.node = _MD_MakeNodeFromString_Ctx(ctx, MD_NodeKind_Label, MD_S8Lit("")); result.node = _MD_MakeNodeFromString_Ctx(ctx, MD_NodeKind_Label, MD_S8Lit(""));
_MD_ParseSet(ctx, result.node, _MD_ParseSet(ctx, result.node,