summaryrefslogtreecommitdiff
path: root/Source/Library/Common/BasicString.class.cpp
blob: ddb4e2c29882d94723b3ce6219a238a306d9fae4 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <Vector.class.h>

#define BS_FREE if (m_string != 0) delete m_string;
#define BS_ALLOC m_string = new T[m_length];
#define BS_VRFY if (m_length == 0) { m_string = NULL; return; }

using namespace CMem;

template <typename T>
BasicString<T>::BasicString() {
	m_string = NULL;
	m_length = 0;
}

template <typename T>
BasicString<T>::BasicString(const T* string, u32int length) {
	m_string = NULL;
	affect(string, length);
}

template <typename T>
BasicString<T>::BasicString(const BasicString<T> &other) {
	m_string = NULL;
	affect(other);
}

template <typename T>
BasicString<T>::BasicString(const T value, u32int count) {
	m_string = NULL;
	affect(value, count);
}

template <typename T>
BasicString<T>::~BasicString() {
	BS_FREE;
}

template <typename T>
void BasicString<T>::affect(const BasicString<T> &other) {
	BS_FREE;
	m_length = other.m_length;
	BS_VRFY;
	BS_ALLOC;
	memcpy((u8int*)m_string, (u8int*)(other.m_string), m_length * sizeof(T));
}

template <typename T>
void BasicString<T>::affect(const T* string, u32int length) {
	BS_FREE;
	m_length = length;
	BS_VRFY;
	BS_ALLOC;
	memcpy((u8int*)string, (u8int*)string, m_length * sizeof(T));
}

template <typename T>
void BasicString<T>::affect(const T value, u32int count) {
	BS_FREE;
	m_length = count;
	BS_VRFY;
	BS_ALLOC;
	for (u32int i = 0; i < count; i++) {
		m_string[i] = value;
	}
}

template <typename T>
bool BasicString<T>::compare(const BasicString<T> &other) const {
	if (m_length != other.m_length) return false;
	for (u32int i = 0; i < m_length; i++) {
		if (m_string[i] != other.m_string[i]) return false;
	}
	return true;
}

template <typename T>
bool BasicString<T>::compare(const T* string, u32int length) const {
	if (m_length != length) return false;
	for (u32int i = 0; i < m_length; i++) {
		if (m_string[i] != string[i]) return false;
	}
	return true;
}

template <typename T>
BasicString<T> &BasicString<T>::append(const BasicString<T> &other) {
	T* newdata = new T[m_length + other.m_length];
	for (u32int i = 0; i < m_length; i++) {
		newdata[i] = m_string[i];
	}
	for (u32int i = 0; i < other.m_length; i++) {
		newdata[i + m_length] = other.m_string[i];
	}
	BS_FREE;
	m_string = newdata;
	m_length += other.m_length;
	return *this;
}

template <typename T>
BasicString<T> &BasicString<T>::append(const T* string, u32int length) {
	T* newdata = new T[m_length + length];
	for (u32int i = 0; i < m_length; i++) {
		newdata[i] = m_string[i];
	}
	for (u32int i = 0; i < length; i++) {
		newdata[i + m_length] = string[i];
	}
	BS_FREE;
	m_string = newdata;
	m_length += length;
	return *this;
}

template <typename T>
BasicString<T> &BasicString<T>::append(const T other) {
	T* newdata = new T[m_length + 1];
	for (u32int i = 0; i < m_length; i++) {
		newdata[i] = m_string[i];
	}
	BS_FREE;
	m_string = newdata;
	m_string[m_length] = other;
	m_length++;
	return *this;
}

template <typename T>
BasicString<T> BasicString<T>::concat(const BasicString<T> &other) const {
	BasicString<T> ret(*this);
	return (ret.append(other));
}

template <typename T>
BasicString<T> BasicString<T>::concat(const T* string, u32int length) const {
	BasicString<T> ret(*this);
	return (ret.append(string, length));
}

template <typename T>
BasicString<T> BasicString<T>::concat(const T other) const {
	BasicString<T> ret(*this);
	return (ret.append(other));
}

template <typename T>
void BasicString<T>::clear() {
	BS_FREE;
	m_string = 0;
	m_length = 0;
}

template <typename T>
bool BasicString<T>::contains(const T& chr) const {
	for (u32int i = 0; i < m_length; i++) {
		if (m_string[i] == chr) return true;
	}
	return false;
}

template <typename T>
Vector< BasicString<T> > BasicString<T>::split(T sep) const {
	Vector< BasicString<T> > ret;
	ret.push(BasicString<T>());
	for (u32int i = 0; i < m_length; i++) {
		if (m_string[i] == sep) {
			ret.push(BasicString<T>());
		} else {
			ret.back().append(m_string[i]);
		}
	}
	return ret;
}

template <typename T>
BasicString<T> BasicString<T>::substr(s32int start, s32int size) {
	if (size == 0) return BasicString<T>();
	if (start < 0) start = m_length - start;
	if (size == -1) size = m_length - start;
	BasicString<T> ret;
	ret.m_string = new T[size + 1];
	ret.m_length = size;
	memcpy((u8int*)ret.m_string, (u8int*)(&m_string[start]), size * sizeof(T));
	return ret;
}