|
#include <string.h>
|
|
#include "types.h"
|
|
#include "string_utils.h"
|
|
#include "interpreter.h"
|
|
|
|
int is_string_ptr(long val) {
|
|
if (val < 0) {
|
|
return 0;
|
|
}
|
|
if (val > MEM_SIZE * 8) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
long concat_strings(long ptr1, long ptr2) {
|
|
static char empty_str[] = "";
|
|
char *s1 = (char*)ptr1;
|
|
char *s2 = (char*)ptr2;
|
|
|
|
if (!s1 || !s2) {
|
|
return (long)empty_str;
|
|
}
|
|
|
|
if (str_pool_idx >= STR_POOL_SIZE) {
|
|
error("String pool overflow");
|
|
return (long)empty_str;
|
|
}
|
|
|
|
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");
|
|
return (long)empty_str;
|
|
}
|
|
|
|
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) {
|
|
static char empty_str[] = "";
|
|
char *str = (char*)str_ptr;
|
|
|
|
if (!str) {
|
|
return (long)empty_str;
|
|
}
|
|
|
|
if (str_pool_idx >= STR_POOL_SIZE) {
|
|
error("String pool overflow");
|
|
return (long)empty_str;
|
|
}
|
|
|
|
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;
|
|
if (start > str_len) start = str_len;
|
|
|
|
int length = end - start;
|
|
if (length < 0) length = 0;
|
|
|
|
if (str_pool_idx + length + 1 >= STR_POOL_SIZE) {
|
|
error("String pool overflow");
|
|
return (long)empty_str;
|
|
}
|
|
|
|
if (length > 0 && start < str_len) {
|
|
strncpy(result, str + start, length);
|
|
}
|
|
result[length] = 0;
|
|
|
|
str_pool_idx += length + 1;
|
|
return (long)result;
|
|
}
|