00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "text/strings.h"
00026
00027 using namespace std;
00028
00029 #ifndef __GNUC__
00030 char* strndup(char const* s, int len)
00031 {
00032 char* res = (char*)malloc(len + 1);
00033 ::strncpy(res, s, len);
00034 res[len] = '\0';
00035 return res;
00036 }
00037 #endif
00038
00039 namespace Tanl {
00040 namespace Text {
00041
00042 bool strStartsWith(const char* s1, const char* init)
00043 {
00044 for (; *s1 && *init && to_lower(*s1) == to_lower(*init); s1++, init++)
00045 ;
00046 return *init == '\0';
00047 }
00048
00049
00057
00058
00059 void itoa(register long n, register char* s)
00060 {
00061 char* s0 = s;
00062 bool const is_neg = n < 0;
00063
00064 if (is_neg)
00065 n = -n;
00066 do
00067 *s++ = n % 10 + '0';
00068 while (n /= 10);
00069 if (is_neg)
00070 *s++ = '-';
00071 *s = '\0';
00072
00073
00074 for (register char *t = s0; t < s; ++t)
00075 {
00076 char const tmp = *--s;
00077 *s = *t;
00078 *t = tmp;
00079 }
00080 }
00081
00082
00089
00090 void to_lower(register char* d, register char const* s)
00091 {
00092 while ((*d++ = to_lower(*s++)))
00093 ;
00094 }
00095
00096
00103
00104
00105 char* to_lower(register char *s)
00106 {
00107 for (register char *p = s; (*p = to_lower(*p)); p++) ;
00108 return s;
00109 }
00110
00111
00119
00120
00121 string& to_lower(string &s)
00122 {
00123 string::iterator p = s.begin();
00124 int len = s.length();
00125
00126 while (len-- > 0) {
00127 *p = tolower(*p);
00128 p++;
00129 }
00130 return s;
00131 }
00132
00133
00140
00141 void to_upper(register char* d, register char const* s)
00142 {
00143 while ((*d++ = to_upper(*s++)))
00144 ;
00145 }
00146
00147
00154
00155
00156 char* to_upper(register char *s)
00157 {
00158 for (register char *p = s; (*p = to_upper(*p)); p++) ;
00159 return s;
00160 }
00161
00162
00170
00171
00172 string& to_upper(string &s)
00173 {
00174 string::iterator p = s.begin();
00175 int len = s.length();
00176
00177 while (len-- > 0) {
00178 *p = toupper(*p);
00179 p++;
00180 }
00181 return s;
00182 }
00183
00194 char const* next_token(char const*& start, char const* end, const char* sep)
00195 {
00196
00197 while (start < end && strchr(sep, *start))
00198 ++start;
00199
00200 if (start == end)
00201 return 0;
00202
00203 char const* token = start;
00204
00205
00206
00207 while (++start < end && !strchr(sep, *start))
00208 ;
00209
00210 return token;
00211 }
00212
00223 char const* next_token(char const*& ptr, const char* sep, char esc)
00224 {
00225
00226
00227 while (*ptr) {
00228 if (*ptr == esc) {
00229 ++ptr;
00230 if (*ptr)
00231 ++ptr;
00232 } else if (strchr(sep, *ptr))
00233 ++ptr;
00234 else
00235 break;
00236 }
00237
00238 if (*ptr) {
00239
00240 char const* start = ptr++;
00241
00242
00243
00244 while (*ptr) {
00245 if (*ptr == esc) {
00246 ++ptr;
00247 if (*ptr)
00248 ++ptr;
00249 } else if (strchr(sep, *ptr))
00250 break;
00251 else
00252 ++ptr;
00253 }
00254
00255 return start;
00256 }
00257
00258
00259 return 0;
00260 }
00261
00262 char* strstr(const char* haystack, const char* needle, size_t count)
00263 {
00264 if (!*needle)
00265 return((char*)haystack);
00266
00267 char const* end = haystack + count;
00268 for (char* cp = (char*)haystack; cp < end && *cp; cp++) {
00269 char* s2 = (char*)needle;
00270
00271 for (char* s1 = cp; s1 < end && *s1 && *s2 && !(*s1-*s2); s1++, s2++)
00272 ;
00273
00274 if (!*s2)
00275 return(cp);
00276 }
00277 return(NULL);
00278 }
00279
00280
00281
00282 std::string operator+(const std::string s, const int i)
00283 {
00284 char is[32];
00285 std::string str(s);
00286 itoa(i, is);
00287 str.append(is);
00288 return str;
00289 }
00290
00291 std::string operator+(const int i, const std::string s)
00292 {
00293 char is[32];
00294 itoa(i, is);
00295 std::string str(is);
00296 str.append(s);
00297 return str;
00298 }
00299
00300 std::string operator+(const std::string s, const unsigned i)
00301 {
00302 char is[32];
00303 std::string str(s);
00304 itoa(i, is);
00305 str.append(is);
00306 return str;
00307 }
00308
00309 std::string operator+(const unsigned i, const std::string s)
00310 {
00311 char is[32];
00312 itoa(i, is);
00313 std::string str(is);
00314 str.append(s);
00315 return str;
00316 }
00317
00318 }
00319 }