Compare commits

..

2 Commits

Author SHA1 Message Date
3a4af168b1 fix alloc bug 2025-12-06 01:04:14 +01:00
6fc645c9d2 parse s8 to int 2025-12-03 17:29:26 +01:00
2 changed files with 104 additions and 2 deletions

View File

@ -215,6 +215,14 @@ RTC_API split_result S8Split(s8 s, u8 c);
/* Creates a clone of string s on arena a */ /* Creates a clone of string s on arena a */
RTC_API s8 S8Clone(s8 s, arena *a); RTC_API s8 S8Clone(s8 s, arena *a);
typedef struct { i64 i; b32 ok; } s8_parse_i64_result;
typedef struct { i32 i; b32 ok; } s8_parse_i32_result;
/* Parses a integer from string s */
RTC_API s8_parse_i64_result S8ParseI64(s8 s, int base);
/* Parses an integer from string s */
RTC_API s8_parse_i32_result S8ParseI32(s8 s, int base);
/* Basic file io */ /* Basic file io */
typedef struct file_buffer typedef struct file_buffer
{ {
@ -285,7 +293,7 @@ RTC_API void *
ArenaAlloc(arena *a, isize size, isize align, isize n, int flags) ArenaAlloc(arena *a, isize size, isize align, isize n, int flags)
{ {
isize padding = -(usize)a->begin & (align - 1); isize padding = -(usize)a->begin & (align - 1);
isize available = a->end - a->begin - padding; isize available = (isize)(a->end - a->begin) - padding;
if (available < 0 || n > available / size) if (available < 0 || n > available / size)
{ {
if (!(flags & ALLOC_SOFTFAIL)) if (!(flags & ALLOC_SOFTFAIL))
@ -293,7 +301,7 @@ ArenaAlloc(arena *a, isize size, isize align, isize n, int flags)
return NULL; return NULL;
} }
void *p = a->begin + padding; void *p = a->begin + padding;
a->begin += padding - n * size; a->begin += padding + n * size;
return (flags & ALLOC_NOZERO) ? p : memset(p, 0, n * size); return (flags & ALLOC_NOZERO) ? p : memset(p, 0, n * size);
} }
@ -392,6 +400,84 @@ S8Clone(s8 s, arena *a)
memcpy(c.data, s.data, s.length); memcpy(c.data, s.data, s.length);
return s; return s;
} }
RTC_API s8_parse_i64_result
S8ParseI64(s8 s, int base)
{
isize at = s.length - 1;
i64 exp = 1;
i64 val = 0;
while (at >= 0)
{
u8 c = s.data[at];
int digit = 0;
if (c >= '0' && c <= '9')
digit = c - '0';
else if (c >= 'A' && c <= 'Z')
digit = c - 'A';
else if (c >= 'a' && c <= 'z')
digit = c - 'a';
else if (c == '-')
{
val *= -1;
break;
}
else if (c == '+')
{
break;
}
else
return (s8_parse_i64_result){0};
if (digit >= base)
return (s8_parse_i64_result){0};
val += digit * exp;
exp *= base;
--at;
}
return (s8_parse_i64_result){
.i = val,
.ok = 1,
};
}
RTC_API s8_parse_i32_result
S8ParseI32(s8 s, int base)
{
isize at = s.length - 1;
i32 exp = 1;
i32 val = 0;
while (at >= 0)
{
u8 c = s.data[at];
int digit = 0;
if (c >= '0' && c <= '9')
digit = c - '0';
else if (c >= 'A' && c <= 'Z')
digit = c - 'A';
else if (c >= 'a' && c <= 'z')
digit = c - 'a';
else if (c == '-')
{
val *= -1;
break;
}
else if (c == '+')
{
break;
}
else
return (s8_parse_i32_result){0};
if (digit >= base)
return (s8_parse_i32_result){0};
val += digit * exp;
exp *= base;
--at;
}
return (s8_parse_i32_result){
.i = val,
.ok = 1,
};
}
/* Basic file io */ /* Basic file io */
RTC_API file_buffer RTC_API file_buffer

16
test.c
View File

@ -47,6 +47,20 @@ ThreadTest(void)
return AtomicLoad(&p) == 42; return AtomicLoad(&p) == 42;
} }
internal int
ParseIntTest(void)
{
if (S8ParseI32(S8("123"), 10).i != 123)
return 0;
if (S8ParseI32(S8("-124"), 10).i != -124)
return 0;
if (S8ParseI64(S8("9223372036854775807"), 10).i != 9223372036854775807)
return 0;
if (S8ParseI32(S8("Not a number"), 10).ok)
return 0;
return 1;
}
int int
main() main()
{ {
@ -56,5 +70,7 @@ main()
return 2; return 2;
if (!ThreadTest()) if (!ThreadTest())
return 3; return 3;
if (!ParseIntTest())
return 4;
return 0; return 0;
} }