mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Added 'name' to string types
This commit is contained in:
@@ -53,6 +53,20 @@ Toy_String* Toy_createStringLength(Toy_Bucket** bucketHandle, const char* cstrin
|
||||
return ret;
|
||||
}
|
||||
|
||||
TOY_API Toy_String* Toy_createNameString(Toy_Bucket** bucketHandle, const char* cname) {
|
||||
int length = strlen(cname);
|
||||
|
||||
Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String) + length + 1); //TODO: compensate for partitioning more space than bucket capacity
|
||||
|
||||
ret->type = TOY_STRING_NAME;
|
||||
ret->length = length;
|
||||
ret->refCount = 1;
|
||||
memcpy(ret->as.name.data, cname, length);
|
||||
ret->as.name.data[length] = '\0';
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Toy_String* Toy_copyString(Toy_Bucket** bucketHandle, Toy_String* str) {
|
||||
if (str->refCount == 0) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't copy a string with refcount of zero\n" TOY_CC_RESET);
|
||||
@@ -69,17 +83,30 @@ Toy_String* Toy_deepCopyString(Toy_Bucket** bucketHandle, Toy_String* str) {
|
||||
}
|
||||
Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String) + str->length + 1); //TODO: compensate for partitioning more space than bucket capacity
|
||||
|
||||
//
|
||||
ret->type = TOY_STRING_LEAF;
|
||||
ret->length = str->length;
|
||||
ret->refCount = 1;
|
||||
deepCopyUtil(ret->as.leaf.data, str); //copy each leaf into the buffer
|
||||
ret->as.leaf.data[ret->length] = '\0';
|
||||
if (str->type == TOY_STRING_NODE || str->type == TOY_STRING_LEAF) {
|
||||
ret->type = TOY_STRING_LEAF;
|
||||
ret->length = str->length;
|
||||
ret->refCount = 1;
|
||||
deepCopyUtil(ret->as.leaf.data, str); //copy each leaf into the buffer
|
||||
ret->as.leaf.data[ret->length] = '\0';
|
||||
}
|
||||
else {
|
||||
ret->type = TOY_STRING_NAME;
|
||||
ret->length = str->length;
|
||||
ret->refCount = 1;
|
||||
memcpy(ret->as.name.data, str->as.name.data, str->length);
|
||||
ret->as.name.data[ret->length] = '\0';
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Toy_String* Toy_concatStrings(Toy_Bucket** bucketHandle, Toy_String* left, Toy_String* right) {
|
||||
if (left->type == TOY_STRING_NAME || right->type == TOY_STRING_NAME) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't concatenate a name string\n" TOY_CC_RESET);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (left->refCount == 0 || right->refCount == 0) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't concatenate a string with refcount of zero\n" TOY_CC_RESET);
|
||||
exit(-1);
|
||||
@@ -112,6 +139,11 @@ int Toy_getStringRefCount(Toy_String* str) {
|
||||
}
|
||||
|
||||
char* Toy_getStringRawBuffer(Toy_String* str) {
|
||||
if (str->type == TOY_STRING_NAME) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't get raw string buffer of a name string\n" TOY_CC_RESET);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (str->refCount == 0) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't get raw string buffer of a string with refcount of zero\n" TOY_CC_RESET);
|
||||
exit(-1);
|
||||
@@ -204,6 +236,15 @@ int Toy_compareStrings(Toy_String* left, Toy_String* right) {
|
||||
return left->length - right->length;
|
||||
}
|
||||
|
||||
if (left->type == TOY_STRING_NAME || right->type == TOY_STRING_NAME) {
|
||||
if (left->type != right->type) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't compare a name string to a non-name string\n" TOY_CC_RESET);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
return strcmp(left->as.name.data, right->as.name.data);
|
||||
}
|
||||
|
||||
//util pointers
|
||||
const char* leftHead = NULL;
|
||||
const char* rightHead = NULL;
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
#include "toy_common.h"
|
||||
|
||||
#include "toy_bucket.h"
|
||||
#include "toy_value.h"
|
||||
|
||||
//rope pattern
|
||||
typedef struct Toy_String { //32 | 64 BITNESS
|
||||
enum Toy_StringType {
|
||||
TOY_STRING_NODE,
|
||||
TOY_STRING_LEAF,
|
||||
TOY_STRING_NAME,
|
||||
} type; //4 | 4
|
||||
|
||||
unsigned int length; //4 | 4
|
||||
@@ -24,12 +26,19 @@ typedef struct Toy_String { //32 | 64 BITNESS
|
||||
int _dummy; //4 | 4
|
||||
char data[]; //- | -
|
||||
} leaf; //4 | 4
|
||||
|
||||
struct {
|
||||
Toy_ValueType type; //4 | 4
|
||||
char data[]; //- | -
|
||||
} name; //4 | 4
|
||||
} as; //8 | 16
|
||||
} Toy_String; //24 | 32
|
||||
|
||||
TOY_API Toy_String* Toy_createString(Toy_Bucket** bucketHandle, const char* cstring);
|
||||
TOY_API Toy_String* Toy_createStringLength(Toy_Bucket** bucketHandle, const char* cstring, int length);
|
||||
|
||||
TOY_API Toy_String* Toy_createNameString(Toy_Bucket** bucketHandle, const char* cname); //for variable names
|
||||
|
||||
TOY_API Toy_String* Toy_copyString(Toy_Bucket** bucketHandle, Toy_String* str);
|
||||
TOY_API Toy_String* Toy_deepCopyString(Toy_Bucket** bucketHandle, Toy_String* str);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user