52 lines
1.1 KiB
C
Raw Normal View History

2025-11-22 22:22:43 +01:00
#include <string.h>
#include "types.h"
#include "string_utils.h"
#include "interpreter.h"
int is_string_ptr(long val) {
return val > MEM_SIZE * 8;
}
long concat_strings(long ptr1, long ptr2) {
char *s1 = (char*)ptr1;
char *s2 = (char*)ptr2;
char *result = &str_pool[str_pool_idx];
int len1 = strlen(s1);
int len2 = strlen(s2);
if (str_pool_idx + len1 + len2 + 1 >= STR_POOL_SIZE) {
error("String pool overflow");
}
strcpy(result, s1);
strcat(result, s2);
str_pool_idx += len1 + len2 + 1;
return (long)result;
}
long slice_string(long str_ptr, int start, int end) {
char *str = (char*)str_ptr;
char *result = &str_pool[str_pool_idx];
int str_len = strlen(str);
if (start < 0) start = 0;
if (end < 0) end = str_len;
if (end > str_len) end = str_len;
if (start > end) start = end;
int length = end - start;
if (str_pool_idx + length + 1 >= STR_POOL_SIZE) {
error("String pool overflow");
}
strncpy(result, str + start, length);
result[length] = 0;
str_pool_idx += length + 1;
return (long)result;
}