blob: cc91745bfb699659cc14644dd8e46dad297cd5e2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#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 = "";
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 {
if (flags & PXStringFlagOwned)
free((void *)_bytes);
[super dealloc];
}
/* getters */
- (const char *)UTF8String {
return _bytes;
}
- (uint32_t)length {
return len;
}
- (BOOL)isEqual:(PXString *)string {
if (self == string)
return YES;
if (string == nil || len != string->len)
return NO;
return (memcmp(_bytes, string->_bytes, size) == 0);
}
@end
|