Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4973 | right-hear | 1 | #ifndef _STRING_INCLUDED |
2 | #define _STRING_INCLUDED |
||
3 | #include |
||
4 | #include |
||
5 | namespace std |
||
6 | { |
||
7 | class string |
||
8 | { |
||
9 | char* data; |
||
10 | public: |
||
11 | string() {data=(char*)malloc(1);data[0]=0;} |
||
12 | string(const char* a) |
||
13 | { |
||
14 | int len=strlen(a)+1; |
||
15 | data=(char*)malloc(len); |
||
16 | memcpy(data,a,len); |
||
17 | } |
||
18 | string(char c) |
||
19 | { |
||
20 | data=(char*)malloc(2); |
||
21 | data[0]=c;data[1]=0; |
||
22 | } |
||
23 | string(const string& s) |
||
24 | { |
||
25 | unsigned sz = s.size()+1; |
||
26 | data=(char*)malloc(sz); |
||
27 | memcpy(data,s.c_str(),sz); |
||
28 | } |
||
29 | ~string() {free(data);} |
||
30 | string& operator=(const char* s) |
||
31 | {free(data);unsigned len=strlen(s)+1;data=(char*)malloc(len); |
||
32 | memcpy(data,s,len);return *this;} |
||
33 | string& operator=(const string& s) |
||
34 | {free(data);unsigned len=s.size()+1;data=(char*)malloc(len); |
||
35 | memcpy(data,s.c_str(),len);return *this;} |
||
36 | const char* c_str() const {return data;} |
||
37 | char& operator[](unsigned pos) {return data[pos];} |
||
38 | char operator[](unsigned pos) const {return data[pos];} |
||
39 | unsigned size() const {return strlen(data);} |
||
40 | unsigned length() const {return size();} |
||
41 | bool operator==(const char* str2) const {return !strcmp(data,str2);} |
||
42 | bool operator==(const string& str2) const {return !strcmp(data,str2.data);} |
||
43 | int compare(const char* str2) {return strcmp(data,str2);} |
||
44 | bool operator!=(const char* str2) const {return (bool)strcmp(data,str2);} |
||
45 | string& replace(unsigned p0,unsigned n0,const string& str) |
||
46 | { |
||
47 | unsigned sz = str.size(); |
||
48 | char* newdata=(char*)malloc(size()+1+sz-n0); |
||
49 | memcpy(newdata,data,p0); |
||
50 | memcpy(newdata+p0,str.c_str(),sz); |
||
51 | memcpy(newdata+p0+sz,data+p0+n0,size()+1-p0-n0); |
||
52 | free(data); |
||
53 | data=newdata; |
||
54 | return *this; |
||
55 | } |
||
56 | string& insert(size_t posl,const string& str) |
||
57 | { return replace(posl,0,str); } |
||
58 | string& operator+=(char c) |
||
59 | { |
||
60 | unsigned sz=size(); |
||
61 | data=(char*)realloc(data,sz+2); |
||
62 | data[sz]=c;data[sz+1]=0; |
||
63 | return *this; |
||
64 | } |
||
65 | string& operator+=(const string& s) |
||
66 | { |
||
67 | unsigned mysz=size(); |
||
68 | unsigned ssz=s.size(); |
||
69 | data=(char*)realloc(data,mysz+1+ssz); |
||
70 | memcpy(data+mysz,s.c_str(),ssz+1); |
||
71 | return *this; |
||
72 | } |
||
73 | // friend string operator+(const string& s1, const char* s2); |
||
74 | // friend string operator+(const char* s1, const string& s2); |
||
75 | string& assign(const string& str, unsigned pos, unsigned n) |
||
76 | { |
||
77 | unsigned len=strlen(str.data); |
||
78 | if (pos>len) |
||
79 | pos=len; |
||
80 | if (n>len||pos+n>len) |
||
81 | n=len-pos; |
||
82 | char* d=(char*)malloc(n+1); |
||
83 | memcpy(d,str.c_str()+pos,n); |
||
84 | d[n]=0; |
||
85 | free(data); |
||
86 | data=d; |
||
87 | return *this; |
||
88 | } |
||
89 | string& assign(const char* s) |
||
90 | { |
||
91 | free(data); |
||
92 | unsigned sz=strlen(s)+1; |
||
93 | data=(char*)malloc(sz); |
||
94 | memcpy(data,s,sz); |
||
95 | return *this; |
||
96 | } |
||
97 | string& assign(const string& str) |
||
98 | { return assign(str.data); } |
||
99 | size_t find_first_of(const char* str, size_t pos=0) const |
||
100 | { |
||
101 | const char* p = data; |
||
102 | while (pos) |
||
103 | { |
||
104 | if (!*p) |
||
105 | return npos; |
||
106 | p++; |
||
107 | pos--; |
||
108 | } |
||
109 | while (*p) |
||
110 | { |
||
111 | if (strchr(str,*p)) |
||
112 | return p-data; |
||
113 | p++; |
||
114 | } |
||
115 | return npos; |
||
116 | } |
||
117 | size_t find_first_of(const string& str, size_t pos=0) const |
||
118 | { return find_first_of(str.data, pos); } |
||
119 | size_t find_first_not_of(const char* str, size_t pos=0) const |
||
120 | { |
||
121 | const char* p = data; |
||
122 | while (pos) |
||
123 | { |
||
124 | if (!*p) |
||
125 | return npos; |
||
126 | p++; |
||
127 | pos--; |
||
128 | } |
||
129 | while (*p) |
||
130 | { |
||
131 | if (!strchr(str,*p)) |
||
132 | return p-data; |
||
133 | p++; |
||
134 | } |
||
135 | return npos; |
||
136 | } |
||
137 | size_t find_first_not_of(const string& str, size_t pos=0) const |
||
138 | { return find_first_not_of(str.data, pos); } |
||
139 | size_t find_last_not_of(const char* str, size_t pos=npos) const |
||
140 | { |
||
141 | const char* p = data; |
||
142 | while (pos) |
||
143 | { |
||
144 | if (!*p) |
||
145 | { |
||
146 | p--; |
||
147 | break; |
||
148 | } |
||
149 | p++; |
||
150 | pos--; |
||
151 | } |
||
152 | for (;;) |
||
153 | { |
||
154 | if (p |
||
155 | return npos; |
||
156 | if (!strchr(str,*p)) |
||
157 | return p-data; |
||
158 | p--; |
||
159 | } |
||
160 | } |
||
161 | size_t find_last_not_of(const string& str, size_t pos=npos) const |
||
162 | { return find_last_not_of(str.data, pos); } |
||
163 | class iterator |
||
164 | { |
||
165 | char* cur; |
||
166 | public: |
||
167 | iterator(char* c):cur(c) {} |
||
168 | iterator(const iterator& i):cur(i.cur) {} |
||
169 | iterator& operator++() {++cur;return *this;} |
||
170 | iterator operator++(int) {iterator tmp(*this);++cur;return tmp;} |
||
171 | bool operator==(iterator it) {return cur==it.cur;} |
||
172 | bool operator!=(iterator it) {return cur!=it.cur;} |
||
173 | char& operator*() {return *cur;} |
||
174 | }; |
||
175 | iterator begin() {return iterator(data);} |
||
176 | iterator end() {return iterator(data+strlen(data));} |
||
177 | // void erase() {free(data);data=(char*)malloc(1);data[0]=0;} |
||
178 | string& erase(unsigned p0=0, unsigned n=(unsigned)-1) |
||
179 | { |
||
180 | unsigned sz=size(); |
||
181 | if (n>sz-p0) n=sz-p0; |
||
182 | char* d=(char*)malloc(sz+1-n); |
||
183 | memcpy(d,data,p0); |
||
184 | memcpy(d+p0,data+p0+n,sz+1-n-p0); |
||
185 | free(data); |
||
186 | data=d; |
||
187 | return *this; |
||
188 | } |
||
189 | typedef unsigned size_type; |
||
190 | static const size_type npos=(size_type)-1; |
||
191 | size_type find(const char* s, size_type pos=0) const |
||
192 | { |
||
193 | char* p=strstr(data+pos,s); |
||
194 | if (!p) return npos; |
||
195 | return p-data; |
||
196 | } |
||
197 | size_type find(char c, size_type pos=0) const |
||
198 | { |
||
199 | char* p=strchr(data+pos,c); |
||
200 | if (!p) return npos; |
||
201 | return p-data; |
||
202 | } |
||
203 | size_type rfind(char c, size_type pos=npos) const |
||
204 | { |
||
205 | size_type len=strlen(data); |
||
206 | if (pos>len) |
||
207 | pos=len; |
||
208 | while (pos--) |
||
209 | if (data[pos]==c) |
||
210 | return pos; |
||
211 | return npos; |
||
212 | } |
||
213 | string substr(unsigned pos=0,unsigned n=npos) |
||
214 | { |
||
215 | string res(data+pos); |
||
216 | if (n |
||
217 | return res; |
||
218 | } |
||
219 | void clear(void) |
||
220 | { |
||
221 | data[0]=0; |
||
222 | } |
||
223 | bool empty(void) const |
||
224 | { |
||
225 | return (data[0]==0); |
||
226 | } |
||
227 | void _toupper(void) |
||
228 | { strupr(data); } |
||
229 | void _tolower(void) |
||
230 | { strlwr(data); } |
||
231 | }; |
||
232 | } |
||
233 | |||
234 | inline std::string operator+(const std::string& s1, const char* s2) |
||
235 | { |
||
236 | std::string res(s1); |
||
237 | res += s2; |
||
238 | return res; |
||
239 | } |
||
240 | inline std::string operator+(const char* s1, const std::string& s2) |
||
241 | { |
||
242 | std::string res(s1); |
||
243 | res += s2; |
||
244 | return res; |
||
245 | } |
||
246 | inline std::string operator+(const std::string& s1, const std::string& s2) |
||
247 | { |
||
248 | std::string res(s1); |
||
249 | res += s2; |
||
250 | return res; |
||
251 | } |
||
252 | |||
253 | #endif |