00001 #ifndef CLASSLIB_OPS_STRING_OPS_H
00002 # define CLASSLIB_OPS_STRING_OPS_H
00003
00004
00005
00006 # include "classlib/utils/StringList.h"
00007 # include <string>
00008 # include <vector>
00009
00010 namespace lat {
00011
00012
00013
00014
00015 class Regexp;
00016 class RegexpMatch;
00017
00018
00019
00020
00021
00022 class StringOps
00023 {
00024 public:
00025 typedef std::vector<int> FieldList;
00026
00034 enum FieldOption {
00036 TrimEmpty = 1,
00037
00040 KeepLeadingEmpty = 2,
00041
00044 KeepTrailingEmpty = 4,
00045
00049 CaseInsensitiveSep = 8
00050 };
00051
00053 static std::string join (const StringList &items,
00054 const std::string &sep);
00055
00057 static StringList grep (const StringList &items,
00058 const std::string &str,
00059 bool caseless = false);
00060 static StringList grep (const StringList &items,
00061 const Regexp &rx);
00062
00064
00065 static int contains (const char *s,
00066 char what,
00067 bool caseless = false);
00068
00069 static int contains (const std::string &s,
00070 char what,
00071 bool caseless = false);
00072
00073 static int contains (const char *s,
00074 const char *what,
00075 bool caseless = false);
00076
00077 static int contains (const std::string &s,
00078 const std::string &what,
00079 bool caseless = false);
00080
00081 static int contains (const char *s,
00082 const Regexp &rx);
00083
00084 static int contains (const std::string &s,
00085 const Regexp &rx);
00086
00088
00089
00090 static int find (const char *s,
00091 char what,
00092 int offset = 0,
00093 bool caseless = false);
00094
00095 static int find (const std::string &s,
00096 char what,
00097 int offset = 0,
00098 bool caseless = false);
00099
00100 static int find (const char *s,
00101 const char *what,
00102 int offset = 0,
00103 bool caseless = false);
00104
00105 static int find (const std::string &s,
00106 const std::string &what,
00107 int offset = 0,
00108 bool caseless = false);
00109
00110 static int find (const char *s,
00111 const Regexp &r,
00112 int offset = 0);
00113
00114 static int find (const std::string &s,
00115 const Regexp &r,
00116 int offset = 0);
00117
00119
00120
00121
00122
00123 static int rfind (const char *s,
00124 char what,
00125 int offset = -1,
00126 bool caseless = false);
00127
00128 static int rfind (const std::string &s,
00129 char what,
00130 int offset = -1,
00131 bool caseless = false);
00132
00133 static int rfind (const char *s,
00134 const char *what,
00135 int offset = -1,
00136 bool caseless = false);
00137
00138 static int rfind (const std::string &s,
00139 const std::string &what,
00140 int offset = -1,
00141 bool caseless = false);
00142
00143 static int rfind (const char *s,
00144 const Regexp &r,
00145 int offset = -1);
00146
00147 static int rfind (const std::string &s,
00148 const Regexp &r,
00149 int offset = -1);
00150
00152 static StringList split (const char *s,
00153 char sep,
00154 int flags = 0,
00155 int nmax = 0,
00156 int first = 0,
00157 int last = 0);
00158
00159 static StringList split (const std::string &s,
00160 char sep,
00161 int flags = 0,
00162 int nmax = 0,
00163 int first = 0,
00164 int last = 0);
00165
00166 static StringList split (const char *s,
00167 const char *sep,
00168 int flags = 0,
00169 int nmax = 0,
00170 int first = 0,
00171 int last = 0);
00172
00173 static StringList split (const std::string &s,
00174 const std::string &sep,
00175 int flags = 0,
00176 int nmax = 0,
00177 int first = 0,
00178 int last = 0);
00179
00180 static StringList split (const char *s,
00181 const Regexp &sep,
00182 int flags = 0,
00183 int nmax = 0,
00184 int first = 0,
00185 int last = 0);
00186
00187 static StringList split (const std::string &s,
00188 const Regexp &sep,
00189 int flags = 0,
00190 int nmax = 0,
00191 int first = 0,
00192 int last = 0);
00193
00195
00196
00197
00198
00199
00200
00201
00202 static std::string section (const char *s,
00203 char sep,
00204 int first,
00205 int last = -1,
00206 int flags = 0);
00207
00208 static std::string section (const std::string &s,
00209 char sep,
00210 int first,
00211 int last = -1,
00212 int flags = 0);
00213
00214 static std::string section (const char *s,
00215 const char *sep,
00216 int first,
00217 int last = -1,
00218 int flags = 0);
00219
00220 static std::string section (const std::string &s,
00221 const std::string &sep,
00222 int first,
00223 int last = -1,
00224 int flags = 0);
00225
00226 static std::string section (const char *s,
00227 const Regexp &sep,
00228 int first,
00229 int last = -1,
00230 int flags = 0);
00231
00232 static std::string section (const std::string &s,
00233 const Regexp &sep,
00234 int first,
00235 int last = -1,
00236 int flags = 0);
00237
00239
00240 static std::string remove (const char *s,
00241 char what,
00242 int offset = 0);
00243
00244 static std::string remove (const std::string &s,
00245 char what,
00246 int offset = 0);
00247
00248 static std::string remove (const char *s,
00249 const char *what,
00250 int offset = 0);
00251
00252 static std::string remove (const std::string &s,
00253 const std::string &what,
00254 int offset = 0);
00255
00256 static std::string remove (const char *s,
00257 const Regexp &what,
00258 int offset = 0);
00259
00260 static std::string remove (const std::string &s,
00261 const Regexp &what,
00262 int offset = 0);
00263
00265
00266
00267 static std::string replace (const char *s,
00268 char what,
00269 const char *with);
00270
00271 static std::string replace (const std::string &s,
00272 char what,
00273 const std::string &with);
00274
00275 static std::string replace (const char *s,
00276 int offset,
00277 char what,
00278 const char *with);
00279
00280 static std::string replace (const std::string &s,
00281 int offset,
00282 char what,
00283 const std::string &with);
00284
00285 static std::string replace (const char *s,
00286 const char *what,
00287 const char *with);
00288
00289 static std::string replace (const std::string &s,
00290 const std::string &what,
00291 const std::string &with);
00292
00293 static std::string replace (const char *s,
00294 int offset,
00295 const char *what,
00296 const char *with);
00297
00298 static std::string replace (const std::string &s,
00299 int offset,
00300 const std::string &what,
00301 const std::string &with);
00302
00303 static std::string replace (const char *s,
00304 const Regexp &what,
00305 const char *with);
00306
00307 static std::string replace (const std::string &s,
00308 const Regexp &what,
00309 const std::string &with);
00310
00311 static std::string replace (const char *s,
00312 const Regexp &what,
00313 unsigned options,
00314 const char *with);
00315
00316 static std::string replace (const std::string &s,
00317 const Regexp &what,
00318 unsigned options,
00319 const std::string &with);
00320
00321 static std::string replace (const char *s,
00322 int offset,
00323 const Regexp &what,
00324 const char *with);
00325
00326 static std::string replace (const std::string &s,
00327 int offset,
00328 const Regexp &what,
00329 const std::string &with);
00330
00331 static std::string replace (const char *s,
00332 int offset,
00333 const Regexp &what,
00334 unsigned options,
00335 const char *with);
00336
00337 static std::string replace (const std::string &s,
00338 int offset,
00339 const Regexp &what,
00340 unsigned options,
00341 const std::string &with);
00342
00343 static std::string replace (const char *s,
00344 const RegexpMatch &matches,
00345 const char *pattern);
00346
00347 static std::string replace (const std::string &s,
00348 const RegexpMatch &matches,
00349 const std::string &pattern);
00350
00351 private:
00352 static int imemcmp (const char *x, const char *y, size_t len);
00353 static int rawfind (const char *s,
00354 size_t slen,
00355 char what,
00356 int offset = 0,
00357 bool caseless = false);
00358
00359 static int rawfind (const char *s,
00360 size_t slen,
00361 const char *what,
00362 size_t len,
00363 int offset = 0,
00364 bool caseless = false);
00365
00366 static int rawrfind (const char *s,
00367 size_t slen,
00368 char what,
00369 int offset,
00370 bool caseless = false);
00371
00372 static int rawrfind (const char *s,
00373 size_t slen,
00374 const char *what,
00375 size_t len,
00376 int offset,
00377 bool caseless = false);
00378
00379 static size_t splitmax (int nmax, int first, int last);
00380 static FieldList splitpos (const char *s,
00381 size_t slen,
00382 const char *sep,
00383 size_t seplen,
00384 int flags,
00385 size_t nmax);
00386
00387 static FieldList splitpos (const char *s,
00388 size_t slen,
00389 char sep,
00390 int flags,
00391 size_t nmax);
00392
00393 static FieldList splitpos (const char *s,
00394 size_t slen,
00395 const Regexp &rx,
00396 int flags,
00397 size_t nmax);
00398
00399 static StringList split (const char *s,
00400 size_t slen,
00401 const FieldList &fields,
00402 int nmax,
00403 int first,
00404 int last);
00405
00406 static std::string section (const char *s,
00407 size_t slen,
00408 const FieldList &fields,
00409 int first,
00410 int last);
00411 };
00412
00413
00414
00415
00416 }
00417 #endif // CLASSLIB_OPS_STRING_OPS_H