diff options
| -rw-r--r-- | include/Paradox/String.h | 22 | ||||
| -rw-r--r-- | src/String.m | 25 | ||||
| -rw-r--r-- | tests/String.m | 7 |
3 files changed, 42 insertions, 12 deletions
diff --git a/include/Paradox/String.h b/include/Paradox/String.h index ddb2261..1cbc53f 100644 --- a/include/Paradox/String.h +++ b/include/Paradox/String.h @@ -5,9 +5,25 @@ #include <objc/runtime.h> #include <Paradox/Object.h> +#define PXStringFlagOwned 0x00010000 + @interface PXString : PXObject { - char *_bytes; - size_t _length; + /* old declaration did not conform to the API or some shit */ + /*char *_bytes; + size_t _length;*/ + + /* flags. currently only low 2 bits are used for encoding + * values: + * 0. ASCII (utf-8 but 7bit chars) + * 1. UTF-8 + * 2. UTF-16 + * 3. UTF-32 + */ + uint32_t flags; + uint32_t len; // num of UTF-16 code units + uint32_t size; // num of bytes + uint32_t hash; + char *_bytes; // as per ABI it's `const char * const _bytes;` when compiler-generated but too bad } /* init/lifecycle */ @@ -16,7 +32,7 @@ /* getters */ - (const char *)UTF8String; -- (size_t)length; +- (uint32_t)length; - (BOOL)isEqual:(PXString *)string; @end diff --git a/src/String.m b/src/String.m index 7d5d4d8..cc91745 100644 --- a/src/String.m +++ b/src/String.m @@ -25,15 +25,24 @@ if (cstring == NULL) cstring = ""; - _length = strlen(cstring); - _bytes = malloc(_length + 1); - memcpy(_bytes, cstring, _length + 1); + size_t length = strlen(cstring); + + flags = 0; // ASCII + flags |= PXStringFlagOwned; // OUR string (not compiler's) + len = (uint32_t)length; + size = (uint32_t)length; + hash = 0; // implement hashes later + + _bytes = malloc(length + 1); + memcpy((char *)_bytes, cstring, length + 1); } return self; } - (void)dealloc { - free(_bytes); + if (flags & PXStringFlagOwned) + free((void *)_bytes); + [super dealloc]; } @@ -42,18 +51,18 @@ return _bytes; } -- (size_t)length { - return _length; +- (uint32_t)length { + return len; } - (BOOL)isEqual:(PXString *)string { if (self == string) return YES; - if (string == nil || _length != string->_length) + if (string == nil || len != string->len) return NO; - return (memcmp(_bytes, string->_bytes, _length) == 0); + return (memcmp(_bytes, string->_bytes, size) == 0); } @end diff --git a/tests/String.m b/tests/String.m index 18a3825..1726f0d 100644 --- a/tests/String.m +++ b/tests/String.m @@ -5,5 +5,10 @@ int main(void) { printf("PXString test\n"); PXString *string = @"Low taper fade"; printf("string UTF8String = %s\n", [string UTF8String]); - [string release]; + printf("string length = %d\n", [string length]); + PXString *string2 = @"Locked In Alien"; + PXString *string3 = @"Low taper fade"; + printf("string isEqual:string2 = %s\n", [string isEqual:string2] ? "YES" : "NO"); + printf("string isEqual:string3 = %s\n", [string isEqual:string3] ? "YES" : "NO"); + printf("test complete\n"); } |
