sapphire/core/string.sp
2022-06-02 18:25:01 +02:00

35 lines
861 B
SourcePawn

import core/allocate;
namespace string {
@len (u64) (str string) {
i8* ptr = (i8*)string;
u64 length = 0;
while(ptr[length] != 0)
{
length += 1;
}
return length;
}
@concat (str) (str a, str b) {
u64 len_a = len(a);
u64 len_b = len(b);
u64 final_size = len_a + len_b;
i8* chars = (i8*)allocate.memalloc(final_size + 1);
clone(a,chars,len_a);
clone(b,chars + len_a,len_b);
chars[final_size] = 0;
return (str)chars;
}
@clone (str string, i8* buffer, u64 max_copy_size) {
u64 chars_cloned = 0;
i8* ptr = (i8*)string;
while(chars_cloned <= max_copy_size and ptr[chars_cloned] != 0)
{
buffer[chars_cloned] = ptr[chars_cloned];
chars_cloned += 1;
}
}
}