1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://www.wtfpl.net/ for more details. |
---|
9 | // |
---|
10 | |
---|
11 | // |
---|
12 | // The String class |
---|
13 | // ---------------- |
---|
14 | // A very simple String class, based on Array. |
---|
15 | // |
---|
16 | |
---|
17 | #if !defined __LOL_BASE_STRING_H__ |
---|
18 | #define __LOL_BASE_STRING_H__ |
---|
19 | |
---|
20 | #include <lol/base/assert.h> |
---|
21 | #include <lol/base/array.h> |
---|
22 | |
---|
23 | #include <cstring> |
---|
24 | #include <cstdarg> |
---|
25 | |
---|
26 | namespace lol |
---|
27 | { |
---|
28 | |
---|
29 | class String : protected Array<char> |
---|
30 | { |
---|
31 | private: |
---|
32 | typedef Array<char> Super; |
---|
33 | |
---|
34 | public: |
---|
35 | inline String() |
---|
36 | : Super() |
---|
37 | { |
---|
38 | Push('\0'); |
---|
39 | } |
---|
40 | |
---|
41 | inline String(char const *str) |
---|
42 | : Super() |
---|
43 | { |
---|
44 | using namespace std; |
---|
45 | ASSERT(str); |
---|
46 | Resize((int)strlen(str)); |
---|
47 | memcpy(&(*this)[0], str, Count() + 1); |
---|
48 | } |
---|
49 | |
---|
50 | inline String(char const *str, int count) |
---|
51 | : Super() |
---|
52 | { |
---|
53 | using namespace std; |
---|
54 | ASSERT(str); |
---|
55 | Resize(count); |
---|
56 | memcpy(&(*this)[0], str, count); |
---|
57 | ((Super &)*this)[count] = '\0'; |
---|
58 | } |
---|
59 | |
---|
60 | inline String(String const &s) |
---|
61 | : Super((Super const &)s) |
---|
62 | { |
---|
63 | } |
---|
64 | |
---|
65 | inline char &operator [](int n) |
---|
66 | { |
---|
67 | /* Allow n == Count() because we might have reasonable reasons |
---|
68 | * to access that hidden null character. */ |
---|
69 | ASSERT(n >= 0); |
---|
70 | ASSERT((unsigned)n <= (unsigned)Count()); |
---|
71 | return ((Super &)*this)[n]; |
---|
72 | } |
---|
73 | |
---|
74 | inline char const &operator [](int n) const |
---|
75 | { |
---|
76 | ASSERT(n >= 0); |
---|
77 | ASSERT(n <= Count()); |
---|
78 | return ((Super const &)*this)[n]; |
---|
79 | } |
---|
80 | |
---|
81 | inline char &Last() |
---|
82 | { |
---|
83 | ASSERT(Count() > 0); |
---|
84 | return (*this)[Count() - 1]; |
---|
85 | } |
---|
86 | |
---|
87 | inline char const &Last() const |
---|
88 | { |
---|
89 | ASSERT(Count() > 0); |
---|
90 | return (*this)[Count() - 1]; |
---|
91 | } |
---|
92 | |
---|
93 | inline int Count() const |
---|
94 | { |
---|
95 | return ((Super const &)*this).Count() - 1; |
---|
96 | } |
---|
97 | |
---|
98 | /* Return a C string */ |
---|
99 | inline char const *C() const |
---|
100 | { |
---|
101 | return &(*this)[0]; |
---|
102 | } |
---|
103 | |
---|
104 | /* Does not initialise the newly allocated characters */ |
---|
105 | void Resize(int count) |
---|
106 | { |
---|
107 | ASSERT(count >= 0); |
---|
108 | ((Super &)*this).Resize(count + 1); |
---|
109 | ((Super &)*this).Last() = '\0'; |
---|
110 | } |
---|
111 | |
---|
112 | String Sub(int start, int count) const |
---|
113 | { |
---|
114 | ASSERT(start >= 0); |
---|
115 | ASSERT(count >= 0); |
---|
116 | ASSERT(start + count <= Count()); |
---|
117 | return String(&(*this)[start], count); |
---|
118 | } |
---|
119 | |
---|
120 | int IndexOf(char token) const |
---|
121 | { |
---|
122 | using namespace std; |
---|
123 | |
---|
124 | char const *tmp = strchr(C(), token); |
---|
125 | return tmp ? (int)(intptr_t)(tmp - C()) : -1; |
---|
126 | } |
---|
127 | |
---|
128 | int IndexOf(char const* token) const |
---|
129 | { |
---|
130 | using namespace std; |
---|
131 | |
---|
132 | char const *tmp = strstr(C(), token); |
---|
133 | return tmp ? (int)(intptr_t)(tmp - C()) : -1; |
---|
134 | } |
---|
135 | |
---|
136 | int LastIndexOf(char token) const |
---|
137 | { |
---|
138 | using namespace std; |
---|
139 | |
---|
140 | char const *tmp = strrchr(C(), token); |
---|
141 | return tmp ? (int)(intptr_t)(tmp - C()) : -1; |
---|
142 | } |
---|
143 | |
---|
144 | int LastIndexOf(char const* token) const |
---|
145 | { |
---|
146 | using namespace std; |
---|
147 | |
---|
148 | int token_len = (int)strlen(token); |
---|
149 | for (int i = Count() - token_len; i >= 0; --i) |
---|
150 | if (strstr(C() + i, token)) |
---|
151 | return i; |
---|
152 | return -1; |
---|
153 | } |
---|
154 | |
---|
155 | inline String operator +(String const &s) const |
---|
156 | { |
---|
157 | String ret(*this); |
---|
158 | return ret += s; |
---|
159 | } |
---|
160 | |
---|
161 | inline String& operator +=(String const &s) |
---|
162 | { |
---|
163 | using namespace std; |
---|
164 | int old_count = Count(); |
---|
165 | Resize(Count() + s.Count()); |
---|
166 | memcpy(&(*this)[old_count], &s[0], Count() - old_count); |
---|
167 | return *this; |
---|
168 | } |
---|
169 | |
---|
170 | inline String operator +(char c) const |
---|
171 | { |
---|
172 | String ret(*this); |
---|
173 | return ret += c; |
---|
174 | } |
---|
175 | |
---|
176 | inline String& operator +=(char c) |
---|
177 | { |
---|
178 | ((Super &)*this).Last() = c; |
---|
179 | ((Super &)*this).Push('\0'); |
---|
180 | return *this; |
---|
181 | } |
---|
182 | |
---|
183 | inline bool operator ==(String const &s) const |
---|
184 | { |
---|
185 | if (this->m_count != s.m_count) |
---|
186 | return false; |
---|
187 | |
---|
188 | for (int i = 0; i < this->m_count; ++i) |
---|
189 | if ((*this)[i] != s[i]) |
---|
190 | return false; |
---|
191 | |
---|
192 | return true; |
---|
193 | } |
---|
194 | |
---|
195 | inline bool operator !=(String const &s) const |
---|
196 | { |
---|
197 | return !(*this == s); |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | inline bool operator ==(char const* sz) const |
---|
202 | { |
---|
203 | int i; |
---|
204 | for (i = 0; i < this->m_count; ++i) |
---|
205 | { |
---|
206 | if (i < this->m_count - 1 && sz[i] == '\0') |
---|
207 | return false; |
---|
208 | |
---|
209 | if ((*this)[i] != sz[i]) |
---|
210 | return false; |
---|
211 | } |
---|
212 | |
---|
213 | return true; |
---|
214 | } |
---|
215 | |
---|
216 | inline bool operator !=(char const* sz) const |
---|
217 | { |
---|
218 | return !(*this == sz); |
---|
219 | } |
---|
220 | |
---|
221 | #ifdef __GNUC__ |
---|
222 | # define LOL_FMT_ATTR(n, p) __attribute__((format(printf, n, p))) |
---|
223 | #else |
---|
224 | # define LOL_FMT_ATTR(n, p) |
---|
225 | #endif |
---|
226 | static String Printf(char const *format, ...) LOL_FMT_ATTR(1, 2); |
---|
227 | #undef LOL_FMT_ATTR |
---|
228 | static String Printf(char const *format, va_list ap); |
---|
229 | }; |
---|
230 | |
---|
231 | inline bool operator ==(char const* sz, String const &s) |
---|
232 | { |
---|
233 | return s == sz; |
---|
234 | } |
---|
235 | |
---|
236 | inline bool operator !=(char const* sz, String const &s) |
---|
237 | { |
---|
238 | return s != sz; |
---|
239 | } |
---|
240 | |
---|
241 | } /* namespace lol */ |
---|
242 | |
---|
243 | #endif // __LOL_BASE_STRING_H__ |
---|
244 | |
---|