undar-lang/libc.c

97 lines
1.5 KiB
C

#include "libc.h"
void
mcpy(void *to, void *from, u32 length)
{
u8 *src, *dest;
if(to == nil || from == nil) return;
src = (u8 *)from;
dest = (u8 *)to;
while(length-- > 0) *(dest++) = *(src++);
return;
}
i32
scpy(char *to, const char *from, u32 length)
{
u32 i;
if(to == nil || from == nil) return -1;
if(length == 0) return 0;
for(i = 0; i < length - 1 && from[i] != '\0'; i++) to[i] = from[i];
to[i] = '\0';
return 0;
}
bool
seq(const char *s1, const char *s2)
{
if(s1 == nil && s2 == nil) return true;
if(s1 == nil || s2 == nil) return false;
while(*s1 && *s2) {
if(*s1 != *s2) return false;
s1++;
s2++;
}
return (*s1 == '\0' && *s2 == '\0');
}
bool
sleq(const char *s1, const char *s2, u32 length)
{
u32 i;
if(s1 == nil && s2 == nil) return true;
if(s1 == nil || s2 == nil) return false;
i = 0;
while(i < length && *s1 && *s2) {
if(*s1 != *s2) return false;
s1++;
s2++;
i++;
}
if(i == length) return true;
return (*s1 == '\0' && *s2 == '\0');
}
u32
slen(const char *str)
{
u32 i;
if(str == nil) return 0;
for(i = 0; str[i] != '\0'; i++);
return i;
}
u32
snlen(const char *str, u32 max_len)
{
u32 i;
if(str == nil) return 0;
for(i = 0; i < max_len && str[i] != '\0'; i++);
return i;
}
void *
aaloc(Arena *arena, u32 size)
{
u32 pos;
if(arena == nil) return nil;
if(arena->count + size > arena->capacity) return nil;
pos = arena->count;
arena->count += size;
return &arena->tape[pos];
}
u32
afree(Arena *arena)
{
u32 freed = arena->count;
arena->count = 0;
return freed;
}