aboutsummaryrefslogtreecommitdiff
path: root/src/String.m
blob: 7d5d4d86a000202e349630897f238c2b68c22cf1 (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
#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