diff options
Diffstat (limited to 'src/String.m')
| -rw-r--r-- | src/String.m | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/String.m b/src/String.m new file mode 100644 index 0000000..7d5d4d8 --- /dev/null +++ b/src/String.m @@ -0,0 +1,59 @@ +#include <objc/objc.h> +#include <objc/runtime.h> +#include <Paradox/Object.h> +#include <Paradox/String.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +@implementation PXString + +/* init/lifecycle */ + ++ (instancetype)stringWithCString:(const char *)cstring { + PXString *string = [PXString alloc]; + return [string initWithCString:cstring]; +} + +- (instancetype)init { + return [self initWithCString:""]; +} + +- (instancetype)initWithCString:(const char *)cstring { + self = [super init]; + if (self) { + if (cstring == NULL) + cstring = ""; + + _length = strlen(cstring); + _bytes = malloc(_length + 1); + memcpy(_bytes, cstring, _length + 1); + } + return self; +} + +- (void)dealloc { + free(_bytes); + [super dealloc]; +} + +/* getters */ +- (const char *)UTF8String { + return _bytes; +} + +- (size_t)length { + return _length; +} + +- (BOOL)isEqual:(PXString *)string { + if (self == string) + return YES; + + if (string == nil || _length != string->_length) + return NO; + + return (memcmp(_bytes, string->_bytes, _length) == 0); +} + +@end |
