fastdo  0.6.8
strings.hpp
浏览该文件的文档.
1 #ifndef __STRINGS_HPP__
2 #define __STRINGS_HPP__
3 //
4 // strings 提供字符串相关的功能
5 //
6 
7 #include <stdarg.h>
8 
9 namespace winux
10 {
11 // 生成不同类型的字符/字符串字面量
12 #define LITERAL_ITEM_SC( ty, name, pref, content ) \
13  static constexpr ty const *name##Str = pref##content; \
14  static constexpr ty const name##Char = name##Str[0];
15 
16 #define LITERAL_ITEM_S( ty, name, pref, content ) \
17  static constexpr ty const *name##Str = pref##content;
18 
19 #define LITERAL_ITEM_LIST( ty, pref ) \
20  LITERAL_ITEM_SC(ty, A, pref, "A") \
21  LITERAL_ITEM_SC(ty, B, pref, "B") \
22  LITERAL_ITEM_SC(ty, C, pref, "C") \
23  LITERAL_ITEM_SC(ty, D, pref, "D") \
24  LITERAL_ITEM_SC(ty, E, pref, "E") \
25  LITERAL_ITEM_SC(ty, F, pref, "F") \
26  LITERAL_ITEM_SC(ty, X, pref, "X") \
27  LITERAL_ITEM_SC(ty, Z, pref, "Z") \
28  LITERAL_ITEM_SC(ty, a, pref, "a") \
29  LITERAL_ITEM_SC(ty, b, pref, "b") \
30  LITERAL_ITEM_SC(ty, c, pref, "c") \
31  LITERAL_ITEM_SC(ty, d, pref, "d") \
32  LITERAL_ITEM_SC(ty, e, pref, "e") \
33  LITERAL_ITEM_SC(ty, f, pref, "f") \
34  LITERAL_ITEM_SC(ty, n, pref, "n") \
35  LITERAL_ITEM_SC(ty, r, pref, "r") \
36  LITERAL_ITEM_SC(ty, t, pref, "t") \
37  LITERAL_ITEM_SC(ty, v, pref, "v") \
38  LITERAL_ITEM_SC(ty, x, pref, "x") \
39  LITERAL_ITEM_SC(ty, z, pref, "z") \
40  LITERAL_ITEM_SC(ty, zero, pref, "0") \
41  LITERAL_ITEM_SC(ty, one, pref, "1") \
42  LITERAL_ITEM_SC(ty, two, pref, "2") \
43  LITERAL_ITEM_SC(ty, three, pref, "3") \
44  LITERAL_ITEM_SC(ty, four, pref, "4") \
45  LITERAL_ITEM_SC(ty, five, pref, "5") \
46  LITERAL_ITEM_SC(ty, six, pref, "6") \
47  LITERAL_ITEM_SC(ty, seven, pref, "7") \
48  LITERAL_ITEM_SC(ty, eight, pref, "8") \
49  LITERAL_ITEM_SC(ty, nine, pref, "9") \
50  LITERAL_ITEM_SC(ty, nul, pref, "\0") \
51  LITERAL_ITEM_SC(ty, bel, pref, "\a") \
52  LITERAL_ITEM_SC(ty, bs, pref, "\b") \
53  LITERAL_ITEM_SC(ty, ht, pref, "\t") \
54  LITERAL_ITEM_SC(ty, lf, pref, "\n") \
55  LITERAL_ITEM_SC(ty, vt, pref, "\v") \
56  LITERAL_ITEM_SC(ty, ff, pref, "\f") \
57  LITERAL_ITEM_SC(ty, cr, pref, "\r") \
58  LITERAL_ITEM_S(ty, empty, pref, "") \
59  LITERAL_ITEM_SC(ty, space, pref, " ") \
60  LITERAL_ITEM_SC(ty, under, pref, "_") \
61  LITERAL_ITEM_SC(ty, dollar, pref, "$") \
62  LITERAL_ITEM_SC(ty, slash, pref, "\\") \
63  LITERAL_ITEM_SC(ty, divide, pref, "/") \
64  LITERAL_ITEM_SC(ty, positive, pref, "+") \
65  LITERAL_ITEM_SC(ty, negative, pref, "-") \
66  LITERAL_ITEM_SC(ty, quote, pref, "\"") \
67  LITERAL_ITEM_SC(ty, apos, pref, "\'") \
68  LITERAL_ITEM_SC(ty, sharp, pref, "#") \
69  LITERAL_ITEM_SC(ty, caret, pref, "^") \
70  LITERAL_ITEM_SC(ty, pipe, pref, "|") \
71  LITERAL_ITEM_SC(ty, amp, pref, "&") \
72  LITERAL_ITEM_SC(ty, lt, pref, "<") \
73  LITERAL_ITEM_SC(ty, gt, pref, ">") \
74  LITERAL_ITEM_SC(ty, dbldivide, pref, "//") \
75  LITERAL_ITEM_S(ty, crlf, pref, "\r\n") \
76  LITERAL_ITEM_S(ty, slash_a, pref, "\\a") \
77  LITERAL_ITEM_S(ty, slash_b, pref, "\\b") \
78  LITERAL_ITEM_S(ty, slash_t, pref, "\\t") \
79  LITERAL_ITEM_S(ty, slash_n, pref, "\\n") \
80  LITERAL_ITEM_S(ty, slash_v, pref, "\\v") \
81  LITERAL_ITEM_S(ty, slash_f, pref, "\\f") \
82  LITERAL_ITEM_S(ty, slash_r, pref, "\\r") \
83  LITERAL_ITEM_S(ty, slash_x, pref, "\\x") \
84  LITERAL_ITEM_S(ty, cslashes, pref, "\n\r\t\v\a\\\'\"")
85 
86 template < typename _ChTy >
87 struct Literal
88 {
90 };
91 
92 template <>
93 struct Literal<char>
94 {
96 };
97 
98 template <>
99 struct Literal<wchar_t>
100 {
101  LITERAL_ITEM_LIST( wchar_t, L );
102 };
103 
104 template <>
105 struct Literal<char16_t>
106 {
107  LITERAL_ITEM_LIST( char16_t, u );
108 };
109 
110 template <>
111 struct Literal<char32_t>
112 {
113  LITERAL_ITEM_LIST( char32_t, U );
114 };
115 
116 
117 WINUX_FUNC_DECL(size_t) StrSplitA( AnsiString const & str, AnsiString const & delimList, AnsiStringArray * arr, bool alwaysRetOneElem = false );
118 WINUX_FUNC_DECL(size_t) StrSplitW( UnicodeString const & str, UnicodeString const & delimList, UnicodeStringArray * arr, bool alwaysRetOneElem = false );
127 #if defined(_UNICODE) || defined(UNICODE)
128 inline size_t StrSplit( UnicodeString const & str, UnicodeString const & delimList, UnicodeStringArray * arr, bool alwaysRetOneElem = false ) { return StrSplitW( str, delimList, arr, alwaysRetOneElem ); }
129 #else
130 inline size_t StrSplit( AnsiString const & str, AnsiString const & delimList, AnsiStringArray * arr, bool alwaysRetOneElem = false ) { return StrSplitA( str, delimList, arr, alwaysRetOneElem ); }
131 #endif
132 
133 
134 WINUX_FUNC_DECL(size_t) StrSplit2A( AnsiString const & str, AnsiString const & delim, AnsiStringArray * arr, bool alwaysRetOneElem = false );
135 WINUX_FUNC_DECL(size_t) StrSplit2W( UnicodeString const & str, UnicodeString const & delim, UnicodeStringArray * arr, bool alwaysRetOneElem = false );
144 #if defined(_UNICODE) || defined(UNICODE)
145 inline size_t StrSplit2( UnicodeString const & str, UnicodeString const & delim, UnicodeStringArray * arr, bool alwaysRetOneElem = false ) { return StrSplit2W( str, delim, arr, alwaysRetOneElem ); }
146 #else
147 inline size_t StrSplit2( AnsiString const & str, AnsiString const & delim, AnsiStringArray * arr, bool alwaysRetOneElem = false ) { return StrSplit2A( str, delim, arr, alwaysRetOneElem ); }
148 #endif
149 
150 
151 WINUX_FUNC_DECL(AnsiString) StrJoinA( AnsiString const & delim, AnsiStringArray const & arr );
154 #if defined(_UNICODE) || defined(UNICODE)
155 inline UnicodeString StrJoin( UnicodeString const & delim, UnicodeStringArray const & arr ) { return StrJoinW( delim, arr ); }
156 #else
157 inline AnsiString StrJoin( AnsiString const & delim, AnsiStringArray const & arr ) { return StrJoinA( delim, arr ); }
158 #endif
159 
160 
161 WINUX_FUNC_DECL(AnsiString) StrJoinExA( AnsiString const & delim, AnsiStringArray const & arr, size_t start = 0, size_t elemCount = -1 );
162 WINUX_FUNC_DECL(UnicodeString) StrJoinExW( UnicodeString const & delim, UnicodeStringArray const & arr, size_t start = 0, size_t elemCount = -1 );
164 #if defined(_UNICODE) || defined(UNICODE)
165 inline UnicodeString StrJoinEx( UnicodeString const & delim, UnicodeStringArray const & arr, size_t start = 0, size_t elemCount = -1 ) { return StrJoinExW( delim, arr, start, elemCount ); }
166 #else
167 inline AnsiString StrJoinEx( AnsiString const & delim, AnsiStringArray const & arr, size_t start = 0, size_t elemCount = -1 ) { return StrJoinExA( delim, arr, start, elemCount ); }
168 #endif
169 
170 
171 WINUX_FUNC_DECL(AnsiString) StrInsertA( AnsiString const & str, size_t start, size_t end, AnsiString const & insert );
172 WINUX_FUNC_DECL(UnicodeString) StrInsertW( UnicodeString const & str, size_t start, size_t end, UnicodeString const & insert );
174 #if defined(_UNICODE) || defined(UNICODE)
175 inline UnicodeString StrInsert( UnicodeString const & str, size_t start, size_t end, UnicodeString const & insert ) { return StrInsertW( str, start, end, insert ); }
176 #else
177 inline AnsiString StrInsert( AnsiString const & str, size_t start, size_t end, AnsiString const & insert ) { return StrInsertA( str, start, end, insert ); }
178 #endif
179 
180 
181 WINUX_FUNC_DECL(AnsiString &) StrMakeReplaceA( AnsiString * str, AnsiString const & findText, AnsiString const & replaceText, size_t offset = 0 );
182 WINUX_FUNC_DECL(UnicodeString &) StrMakeReplaceW( UnicodeString * str, UnicodeString const & findText, UnicodeString const & replaceText, size_t offset = 0 );
184 #if defined(_UNICODE) || defined(UNICODE)
185 inline UnicodeString & StrMakeReplace( UnicodeString * str, UnicodeString const & findText, UnicodeString const & replaceText, size_t offset = 0 ) { return StrMakeReplaceW( str, findText, replaceText, offset ); }
186 #else
187 inline AnsiString & StrMakeReplace( AnsiString * str, AnsiString const & findText, AnsiString const & replaceText, size_t offset = 0 ) { return StrMakeReplaceA( str, findText, replaceText, offset ); }
188 #endif
189 
190 
197 #if defined(_UNICODE) || defined(UNICODE)
198 inline UnicodeString StrTrim( UnicodeString const & str ) { return StrTrimW(str); }
199 #else
200 inline AnsiString StrTrim( AnsiString const & str ) { return StrTrimA(str); }
201 #endif
202 
203 
207 #if defined(_UNICODE) || defined(UNICODE)
208 inline UnicodeString & StrMakeUpper( UnicodeString * str ) { return StrMakeUpperW(str); }
209 #else
210 inline AnsiString & StrMakeUpper( AnsiString * str ) { return StrMakeUpperA(str); }
211 #endif
212 
216 #if defined(_UNICODE) || defined(UNICODE)
217 inline UnicodeString StrUpper( UnicodeString str ) { return StrUpperW(str); }
218 #else
219 inline AnsiString StrUpper( AnsiString str ) { return StrUpperA(str); }
220 #endif
221 
222 
226 #if defined(_UNICODE) || defined(UNICODE)
227 inline UnicodeString & StrMakeLower( UnicodeString * str ) { return StrMakeLowerW(str); }
228 #else
229 inline AnsiString & StrMakeLower( AnsiString * str ) { return StrMakeLowerA(str); }
230 #endif
231 
235 #if defined(_UNICODE) || defined(UNICODE)
236 inline UnicodeString StrLower( UnicodeString str ) { return StrLowerW(str); }
237 #else
238 inline AnsiString StrLower( AnsiString str ) { return StrLowerA(str); }
239 #endif
240 
241 
242 WINUX_FUNC_DECL(AnsiString) StrMultipleA( AnsiString const & str, int multiple );
243 WINUX_FUNC_DECL(UnicodeString) StrMultipleW( UnicodeString const & str, int multiple );
245 #if defined(_UNICODE) || defined(UNICODE)
246 inline UnicodeString StrMultiple( UnicodeString const & str, int multiple ) { return StrMultipleW( str, multiple ); }
247 #else
248 inline AnsiString StrMultiple( AnsiString const & str, int multiple ) { return StrMultipleA( str, multiple ); }
249 #endif
250 
251 
255 #if defined(_UNICODE) || defined(UNICODE)
256 inline UnicodeString StrSubtract( UnicodeString str1, UnicodeString const & str2 ) { return StrSubtractW( str1, str2 ); }
257 #else
258 inline AnsiString StrSubtract( AnsiString str1, AnsiString const & str2 ) { return StrSubtractA( str1, str2 ); }
259 #endif
260 
261 
264 {
269 };
270 
271 WINUX_FUNC_DECL(uint64) StrToXqA( char const * nptr, char const ** endptr, int ibase, int flags );
272 WINUX_FUNC_DECL(uint64) StrToXqW( wchar const * nptr, wchar const ** endptr, int ibase, int flags );
274 #if defined(_UNICODE) || defined(UNICODE)
275 inline uint64 StrToXq( wchar const * nptr, wchar const ** endptr, int ibase, int flags ) { return StrToXqW( nptr, endptr, ibase, flags ); }
276 #else
277 inline uint64 StrToXq( char const * nptr, char const ** endptr, int ibase, int flags ) { return StrToXqA( nptr, endptr, ibase, flags ); }
278 #endif
279 
280 WINUX_FUNC_DECL(uint64) StrToXqU16( char16 const * nptr, char16 const ** endptr, int ibase, int flags );
281 WINUX_FUNC_DECL(uint64) StrToXqU32( char32 const * nptr, char32 const ** endptr, int ibase, int flags );
282 
283 
284 WINUX_FUNC_DECL(int64) StrToInt64A( AnsiString const & numStr, int ibase );
285 WINUX_FUNC_DECL(int64) StrToInt64W( UnicodeString const & numStr, int ibase );
286 #if defined(_UNICODE) || defined(UNICODE)
287 inline int64 StrToInt64( UnicodeString const & numStr, int ibase ) { return StrToInt64W( numStr, ibase ); }
288 #else
289 inline int64 StrToInt64( AnsiString const & numStr, int ibase ) { return StrToInt64A( numStr, ibase ); }
290 #endif
291 
292 
293 WINUX_FUNC_DECL(uint64) StrToUint64A( AnsiString const & numStr, int ibase );
294 WINUX_FUNC_DECL(uint64) StrToUint64W( UnicodeString const & numStr, int ibase );
295 #if defined(_UNICODE) || defined(UNICODE)
296 inline uint64 StrToUint64( UnicodeString const & numStr, int ibase ) { return StrToUint64W( numStr, ibase ); }
297 #else
298 inline uint64 StrToUint64( AnsiString const & numStr, int ibase ) { return StrToUint64A( numStr, ibase ); }
299 #endif
300 
301 
302 WINUX_FUNC_DECL(AnsiString) AddSlashesA( AnsiString const & str, AnsiString const & charlist );
303 WINUX_FUNC_DECL(UnicodeString) AddSlashesW( UnicodeString const & str, UnicodeString const & charlist );
305 #if defined(_UNICODE) || defined(UNICODE)
306 inline UnicodeString AddSlashes( UnicodeString const & str, UnicodeString const & charlist ) { return AddSlashesW( str, charlist ); }
307 #else
308 inline AnsiString AddSlashes( AnsiString const & str, AnsiString const & charlist ) { return AddSlashesA( str, charlist ); }
309 #endif
310 
311 
315 #if defined(_UNICODE) || defined(UNICODE)
316 inline UnicodeString AddCSlashes( UnicodeString const & str ) { return AddCSlashesW(str); }
317 #else
318 inline AnsiString AddCSlashes( AnsiString const & str ) { return AddCSlashesA(str); }
319 #endif
320 
321 
322 WINUX_FUNC_DECL(AnsiString) StripSlashesA( AnsiString const & str, AnsiString const & charlist );
323 WINUX_FUNC_DECL(UnicodeString) StripSlashesW( UnicodeString const & str, UnicodeString const & charlist );
325 #if defined(_UNICODE) || defined(UNICODE)
326 inline UnicodeString StripSlashes( UnicodeString const & str, UnicodeString const & charlist ) { return StripSlashesW( str, charlist ); }
327 #else
328 inline AnsiString StripSlashes( AnsiString const & str, AnsiString const & charlist ) { return StripSlashesA( str, charlist ); }
329 #endif
330 
331 
335 #if defined(_UNICODE) || defined(UNICODE)
336 inline UnicodeString StripCSlashes( UnicodeString const & str ) { return StripCSlashesW(str); }
337 #else
338 inline AnsiString StripCSlashes( AnsiString const & str ) { return StripCSlashesA(str); }
339 #endif
340 
341 
342 WINUX_FUNC_DECL(AnsiString) AddQuotesA( AnsiString const & str, AnsiString::value_type quote = Literal<AnsiString::value_type>::quoteChar );
345 #if defined(_UNICODE) || defined(UNICODE)
346 inline UnicodeString AddQuotes( UnicodeString const & str, UnicodeString::value_type quote = Literal<UnicodeString::value_type>::quoteChar ) { return AddQuotesW( str, quote ); }
347 #else
348 inline AnsiString AddQuotes( AnsiString const & str, AnsiString::value_type quote = Literal<AnsiString::value_type>::quoteChar ) { return AddQuotesA( str, quote ); }
349 #endif
350 
351 
352 WINUX_FUNC_DECL(bool) StrGetLineA( AnsiString * line, AnsiString const & str, size_t * i, AnsiString * nl = nullptr );
353 WINUX_FUNC_DECL(bool) StrGetLineW( UnicodeString * line, UnicodeString const & str, size_t * i, UnicodeString * nl = nullptr );
362 #if defined(_UNICODE) || defined(UNICODE)
363 inline bool StrGetLine( UnicodeString * line, UnicodeString const & str, size_t * i, UnicodeString * nl = nullptr ) { return StrGetLineW( line, str, i, nl ); }
364 #else
365 inline bool StrGetLine( AnsiString * line, AnsiString const & str, size_t * i, AnsiString * nl = nullptr ) { return StrGetLineA( line, str, i, nl ); }
366 #endif
367 
368 
371 {
372  wordRaw = 0x00,
373  wordAllUpper = 0x01,
374  wordAllLower = 0x02,
376 
377  //nameRaw = 0x00,
378  nameSmallHump = 0x10,
379  //nameBigHump = 0x20,
380  //nameShortLine = 0x40,
381  //nameUnderline = 0x80,
382 };
383 
385 WINUX_FUNC_DECL(UnicodeString) CollateIdentifierToStringW( UnicodeString const & identifier, UnicodeString const & sep, winux::uint flags = wordFirstCharUpper );
387 #if defined(_UNICODE) || defined(UNICODE)
388 inline UnicodeString CollateIdentifierToString( UnicodeString const & identifier, UnicodeString const & sep, winux::uint flags = wordFirstCharUpper ) { return CollateIdentifierToStringW( identifier, sep, flags ); }
389 #else
390 inline AnsiString CollateIdentifierToString( AnsiString const & identifier, AnsiString const & sep, winux::uint flags = wordFirstCharUpper ) { return CollateIdentifierToStringA( identifier, sep, flags ); }
391 #endif
392 
393 
394 WINUX_FUNC_DECL(AnsiStringArray) CollateIdentifierToArrayA( AnsiString const & identifier, winux::uint flags = wordFirstCharUpper );
395 WINUX_FUNC_DECL(UnicodeStringArray) CollateIdentifierToArrayW( UnicodeString const & identifier, winux::uint flags = wordFirstCharUpper );
397 #if defined(_UNICODE) || defined(UNICODE)
398 inline UnicodeStringArray CollateIdentifierToArray( UnicodeString const & identifier, winux::uint flags = wordFirstCharUpper ) { return CollateIdentifierToArrayW( identifier, flags ); }
399 #else
400 inline AnsiStringArray CollateIdentifierToArray( AnsiString const & identifier, winux::uint flags = wordFirstCharUpper ) { return CollateIdentifierToArrayA( identifier, flags ); }
401 #endif
402 
403 
405 template < typename _IndexType, typename _ChTy >
406 inline static std::vector<_IndexType> _Templ_KmpCalcNext( _ChTy const * substr, size_t sublen )
407 {
408  std::vector<_IndexType> next( sublen + 1 );
409  ssize_t j = 0, k = -1;
410  next[0] = -1;
411  while ( j < (ssize_t)sublen )
412  {
413  if ( k == -1 || substr[j] == substr[k] )
414  {
415  j++;
416  k++;
417  //if ( substr[j] != substr[k] )
418  next[j] = (_IndexType)k;
419  //else
420  // next[j] = next[k];
421  }
422  else
423  {
424  k = next[k];
425  }
426  }
427  return next;
428 }
429 
431 template < typename _IndexType, typename _ChTy >
432 inline static size_t _Templ_KmpMatchEx( _ChTy const * str, size_t len, _ChTy const * substr, size_t sublen, size_t pos, std::vector<_IndexType> const & next )
433 {
434  ssize_t i, j;
435  i = pos;
436  j = 0;
437  while ( i < (ssize_t)len && j < (ssize_t)sublen )
438  {
439  if ( str[i] == substr[j] )
440  {
441  j++;
442  i++;
443  }
444  else
445  {
446  j = next[j];
447 
448  if ( j == -1 )
449  {
450  j = 0;
451  i++;
452  }
453  }
454  }
455  return j == (ssize_t)sublen ? i - (ssize_t)sublen : -1;
456 }
457 
459 template < typename _IndexType, typename _ChTy >
460 inline static size_t _Templ_KmpMatch( _ChTy const * str, size_t len, _ChTy const * substr, size_t sublen, size_t pos )
461 {
462  return _Templ_KmpMatchEx<_IndexType, _ChTy>( str, len, substr, sublen, pos, _Templ_KmpCalcNext( substr, sublen ) );
463 }
464 
466 WINUX_FUNC_DECL(std::vector<int>) KmpCalcNext( char const * substr, size_t sublen );
468 WINUX_FUNC_DECL(size_t) KmpMatchEx( char const * str, size_t len, char const * substr, size_t sublen, size_t pos, std::vector<int> const & next );
470 WINUX_FUNC_DECL(size_t) KmpMatch( char const * str, size_t len, char const * substr, size_t sublen, size_t pos );
471 
472 
474 template < typename _ChTy >
476 {
477  XString<_ChTy> * _str;
478  std::basic_ostringstream<_ChTy> * _sout;
479  bool _isAppend;
480 public:
485  XStringWriter( XString<_ChTy> * str, bool isAppend = false ) : _str(str), _sout(NULL), _isAppend(isAppend)
486  {
487  _sout = new std::basic_ostringstream<_ChTy>();
488  }
489  XStringWriter( XStringWriter const & other ) : _str(NULL), _sout(NULL)
490  {
491  this->operator = ( const_cast<XStringWriter &>(other) );
492  }
494  {
495  if ( _str )
496  {
497  if ( _isAppend )
498  {
499  *_str += _sout->str();
500  }
501  else
502  {
503  *_str = _sout->str();
504  }
505  }
506  if ( _sout ) delete _sout;
507  }
508  XStringWriter & operator = ( XStringWriter & other )
509  {
510  if ( this != &other )
511  {
512  _str = other._str;
513  _sout = other._sout;
514  _isAppend = other._isAppend;
515 
516  other._str = NULL;
517  other._sout = NULL;
518  other._isAppend = false;
519  }
520  return *this;
521  }
522 
523  template < typename _AnyType >
524  std::ostream & operator << ( _AnyType && t )
525  {
526  assert( _sout != NULL );
527  return *_sout << std::forward<_AnyType>(t);
528  }
529 };
530 
534 
537 {
538 public:
539  struct MatchResult
540  {
543  };
544 
545  struct MatchState
546  {
551  bool matched;
552  };
553 
555  typedef std::vector<short> KmpNextValueArray;
556 
558  typedef std::vector<MatchState> MatchStates;
559 
561  typedef String (* ReplaceFuncType)( MultiMatch const * matchObj, ssize_t item, void * extra );
562 
564  typedef MatchResult ( MultiMatch:: * SearchFuncType )( String const & str, ssize_t offset ) const;
565 
569  MultiMatch();
570 
572  MultiMatch( StringArray const & matches, StringArray const & replaces );
573 
575  MultiMatch( StringArray const & matches, ReplaceFuncType replaceFunc, void * extra = NULL );
576 
578  template < size_t _N >
579  MultiMatch( String (&matches)[_N], String (&replaces)[_N] ) : _replaceFunc(NULL), _extra(NULL)
580  {
581  this->init( StringArray( matches, matches + _N ), StringArray( replaces, replaces + _N ) );
582  }
583 
585  template < size_t _N >
586  MultiMatch( String (&matches)[_N], ReplaceFuncType replaceFunc, void * extra = NULL ) : _replaceFunc(NULL), _extra(NULL)
587  {
588  this->init( StringArray( matches, matches + _N ), replaceFunc, extra );
589  }
590 
592  void init( StringArray const & matches, StringArray const & replaces );
593 
595  void init( StringArray const & matches, ReplaceFuncType replaceFunc, void * extra = NULL );
596 
598  ssize_t addMatchReplacePair( String const & match, String const & replace );
599 
601  ssize_t addMatch( String const & match );
602 
604  String const & getMatchItem( ssize_t item ) const;
605 
607  void setMatchItem( ssize_t item, String const & match );
608 
610  String getReplaceItem( ssize_t item ) const;
611 
613  void setReplaceItem( ssize_t item, String const & replace );
614 
616  void setReplaceItems( StringArray const & replaces );
617 
619  ReplaceFuncType setReplaceFunc( ReplaceFuncType newReplaceFunc, void * extra = NULL );
620 
622  void clear();
623 
629  MatchResult search( String const & str, ssize_t offset = 0 ) const;
630 
636  MatchResult greedSearch( String const & str, ssize_t offset = 0 ) const;
637 
643  MatchResult commonSearch( String const & str, ssize_t offset = 0 ) const;
644 
650  String replace( String const & str, ssize_t offset = 0, SearchFuncType fnSearch = &MultiMatch::search ) const;
651 
652 private:
653  std::vector<KmpNextValueArray> _nextVals;
654  StringArray _matchItems;
655  StringArray _replaceItems;
656  ReplaceFuncType _replaceFunc;
657  void * _extra;
658 };
659 
662 {
663  AnsiString _loc;
664  AnsiString _prevLoc;
665  static AnsiString _clsLc;
666 public:
667  static void Set( char const * lc ) { _clsLc = lc; }
668  static char const * Get() { return _clsLc.c_str(); }
669  Locale( char const * lc = NULL );
670  ~Locale();
672 };
673 
675 WINUX_FUNC_DECL(size_t) LocalCharsCount( AnsiString const & local );
677 WINUX_FUNC_DECL(size_t) UnicodeMinLength( UnicodeString const & unicode );
682 
691 
692 
693 WINUX_DLL AnsiString FormatExVA( size_t cch, char const * fmt, va_list args );
694 WINUX_DLL UnicodeString FormatExVW( size_t cch, wchar const * fmt, va_list args );
695 
696 WINUX_DLL AnsiString FormatExA( size_t cch, char const * fmt, ... );
697 WINUX_DLL UnicodeString FormatExW( size_t cch, wchar const * fmt, ... );
698 
699 WINUX_DLL AnsiString FormatA( char const * fmt, ... );
700 WINUX_DLL UnicodeString FormatW( wchar const * fmt, ... );
701 
702 #if defined(_UNICODE) || defined(UNICODE)
703 
704 inline UnicodeString FormatExVW( size_t cch, wchar const * fmt, va_list args ) { return FormatExVW( cch, fmt, args ); }
706 WINUX_DLL UnicodeString FormatEx( size_t cch, wchar const * fmt, ... );
708 WINUX_DLL UnicodeString Format( wchar const * fmt, ... );
709 #else
710 
711 inline AnsiString FormatExV( size_t cch, char const * fmt, va_list args ) { return FormatExVA( cch, fmt, args ); }
713 WINUX_DLL AnsiString FormatEx( size_t cch, char const * fmt, ... );
715 WINUX_DLL AnsiString Format( char const * fmt, ... );
716 #endif
717 
720 {
721  union
722  {
723  char * _psz;
724  wchar_t * _pwsz;
725  };
726  enum { szCharInput, szWCharInput } _type;
727  size_t _count;
728 public:
729  explicit SZInput( char * psz, size_t count ) : _psz(psz), _type(szCharInput), _count(count) { }
730  explicit SZInput( wchar_t * pwsz, size_t count ) : _pwsz(pwsz), _type(szWCharInput), _count(count) { }
731  SZInput & operator = ( char const * pstr );
732  SZInput & operator = ( wchar_t const * pwstr );
733 
734 };
735 
736 #if defined(__GNUC__) || _MSC_VER >= 1600
737 /* VC2010以上支持模板取数组大小 */
738 template < typename _CHAR, uint _N >
739 SZInput SZ( _CHAR (&sz)[_N] )
740 {
741  return SZInput( sz, _N );
742 }
743 
744 #else
745 /* 否则使用宏定义 */
746 #define SZ(sz) SZInput( sz, sizeof(sz) / sizeof(sz[0]) )
747 
748 #endif
749 /* 如果操作对象是缓冲区指针,则使用SZP宏 */
750 #define SZP SZInput
751 
752 // ----------------------------------------------------------------------------------
753 
756 {
757 public:
758  Conv( char const * fromCode, char const * toCode );
759  ~Conv();
760 
767  size_t convert( char const * srcBuf, size_t srcSize, char * * destBuf );
768 
773  template < typename _RetString, typename _String >
774  _RetString convert( _String const & str )
775  {
776  typename _RetString::pointer buf;
777  size_t outBytes = this->convert( (char *)str.c_str(), ( str.length() + 1 ) * sizeof(typename _String::value_type), (char **)&buf );
778  _RetString s = (typename _RetString::pointer)buf;
779  free(buf);
780  return s;
781  }
782 private:
784 
786 };
787 
789 template < typename _ToString >
790 class ConvTo : public Conv
791 {
792 public:
793  ConvTo( char const * toCode ) : Conv( "", toCode )
794  {
795  }
796 
797  _ToString convert( AnsiString const & str )
798  {
799  return this->Conv::convert<_ToString, AnsiString>(str);
800  }
801 
802  _ToString operator () ( AnsiString const & str )
803  {
804  typename _ToString::value_type * buf;
805  Conv::convert( (char *)str.c_str(), (str.length() + 1) * sizeof(typename AnsiString::value_type), (char **)&buf );
806  _ToString s = buf;
807  free(buf);
808  return s;
809  }
810 };
811 
813 template < typename _FromString >
814 class ConvFrom : public Conv
815 {
816 public:
817  ConvFrom( char const * fromCode ) : Conv( fromCode, "" )
818  {
819  }
820 
821  AnsiString convert( _FromString const & str )
822  {
823  return this->Conv::convert<AnsiString, _FromString>(str);
824  }
825 
826  AnsiString operator () ( _FromString const & str )
827  {
828  AnsiString::value_type * buf;
829  Conv::convert( (char *)str.c_str(), (str.length() + 1) * sizeof(typename _FromString::value_type), (char **)&buf );
830  AnsiString s = buf;
831  free(buf);
832  return s;
833  }
834 
835 };
836 
837 // UTF-8编码转换
842 
843 
844 } // namespace winux
845 
846 #endif // __STRINGS_HPP__
XString< char > AnsiString
Definition: utilities.hpp:212
不处理单词
Definition: strings.hpp:372
__int64 int64
Definition: utilities.hpp:187
uint64 StrToUint64W(UnicodeString const &numStr, int ibase)
static std::vector< _IndexType > _Templ_KmpCalcNext(_ChTy const *substr, size_t sublen)
KMP匹配算法:求子串next值
Definition: strings.hpp:406
StrToXqFlags
字符串转换成数字Flags
Definition: strings.hpp:263
AnsiString StripCSlashesA(AnsiString const &str)
Definition: strings.hpp:332
UnicodeString FormatExW(size_t cch, wchar const *fmt,...)
XString< wchar > UnicodeString
Definition: utilities.hpp:213
ConvTo(char const *toCode)
Definition: strings.hpp:793
AnsiString StrInsertA(AnsiString const &str, size_t start, size_t end, AnsiString const &insert)
ssize_t pos
匹配到的位置
Definition: strings.hpp:541
AnsiString AddCSlashesA(AnsiString const &str)
Definition: strings.hpp:312
intptr_t ssize_t
Definition: utilities.hpp:180
wchar_t * _pwsz
Definition: strings.hpp:724
AnsiString FormatA(char const *fmt,...)
char32_t char32
Definition: utilities.hpp:178
UnicodeString AddSlashesW(UnicodeString const &str, UnicodeString const &charlist)
UnicodeString StrJoinExW(UnicodeString const &delim, UnicodeStringArray const &arr, size_t start=0, size_t elemCount=-1)
AnsiStringArray CollateIdentifierToArray(AnsiString const &identifier, winux::uint flags=wordFirstCharUpper)
整理标识符为单词数组
Definition: strings.hpp:400
MatchResult mr
匹配结果
Definition: strings.hpp:549
int64 StrToInt64W(UnicodeString const &numStr, int ibase)
AnsiString StripSlashesA(AnsiString const &str, AnsiString const &charlist)
AnsiString UnicodeToLocal(UnicodeString const &unicode)
Unicode转换到本地Ansi.
uint64 StrToUint64(AnsiString const &numStr, int ibase)
Definition: strings.hpp:298
UnicodeString StrMultipleW(UnicodeString const &str, int multiple)
AnsiString StrLowerA(AnsiString str)
size_t StrSplit2W(UnicodeString const &str, UnicodeString const &delim, UnicodeStringArray *arr, bool alwaysRetOneElem=false)
AnsiString StringToLocal(String const &str)
兼容字符串与Unicode、Local字符串相互转换
AnsiString & StrMakeLowerA(AnsiString *str)
#define WINUX_DLL
Definition: utilities.hpp:60
wchar_t wchar
Definition: utilities.hpp:184
std::vector< int > KmpCalcNext(char const *substr, size_t sublen)
KMP字符串匹配算法:求子串next值
uint64 StrToUint64A(AnsiString const &numStr, int ibase)
AnsiString AddCSlashes(AnsiString const &str)
加C-Style反斜杠
Definition: strings.hpp:318
AnsiStringArray CollateIdentifierToArrayA(AnsiString const &identifier, winux::uint flags=wordFirstCharUpper)
AnsiString StrMultipleA(AnsiString const &str, int multiple)
SZInput(char *psz, size_t count)
Definition: strings.hpp:729
用来使得String能够用operator<<来赋值
Definition: strings.hpp:475
SZInput(wchar_t *pwsz, size_t count)
Definition: strings.hpp:730
AnsiString & StrMakeReplaceA(AnsiString *str, AnsiString const &findText, AnsiString const &replaceText, size_t offset=0)
bool StrGetLineA(AnsiString *line, AnsiString const &str, size_t *i, AnsiString *nl=nullptr)
int64 StrToInt64A(AnsiString const &numStr, int ibase)
uint64 StrToXqA(char const *nptr, char const **endptr, int ibase, int flags)
int64 StrToInt64(AnsiString const &numStr, int ibase)
Definition: strings.hpp:289
AnsiString StrJoinExA(AnsiString const &delim, AnsiStringArray const &arr, size_t start=0, size_t elemCount=-1)
XString< tchar > String
Definition: utilities.hpp:216
UnicodeString AddQuotesW(UnicodeString const &str, UnicodeString::value_type quote=Literal< UnicodeString::value_type >::quoteChar)
size_t StrSplit2A(AnsiString const &str, AnsiString const &delim, AnsiStringArray *arr, bool alwaysRetOneElem=false)
size_t StrSplitW(UnicodeString const &str, UnicodeString const &delimList, UnicodeStringArray *arr, bool alwaysRetOneElem=false)
AnsiString & StrMakeUpper(AnsiString *str)
使字符串大写
Definition: strings.hpp:210
#define LITERAL_ITEM_LIST(ty, pref)
Definition: strings.hpp:19
AnsiString StrJoin(AnsiString const &delim, AnsiStringArray const &arr)
字符串组合连接
Definition: strings.hpp:157
size_t StrSplit2(AnsiString const &str, AnsiString const &delim, AnsiStringArray *arr, bool alwaysRetOneElem=false)
字符串分割2
Definition: strings.hpp:147
static size_t _Templ_KmpMatch(_ChTy const *str, size_t len, _ChTy const *substr, size_t sublen, size_t pos)
KMP匹配算法:匹配
Definition: strings.hpp:460
单词大写
Definition: strings.hpp:373
单词小写
Definition: strings.hpp:374
size_t KmpMatchEx(char const *str, size_t len, char const *substr, size_t sublen, size_t pos, std::vector< int > const &next)
KMP字符串匹配算法:传入已经求好的next进行匹配
size_t UnicodeMinLength(UnicodeString const &unicode)
返回一个unicode字符串转换为多字节字符串最少需要多少字节(by local CodePage),用于wcstombs ...
UnicodeString StrTrimW(UnicodeString const &str)
AnsiString convert(_FromString const &str)
Definition: strings.hpp:821
CollateIdentifierStringFlag
整理标识符串的标记
Definition: strings.hpp:370
UnicodeString & StrMakeUpperW(UnicodeString *str)
std::ostream & operator<<(std::ostream &o, ConsoleAttrT< _VarType > const &tr)
Definition: console.hpp:137
_ToString convert(AnsiString const &str)
Definition: strings.hpp:797
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:81
AnsiString LocalFromUtf8(AnsiString const &str)
从utf-8转到本地编码
有解析到负号
Definition: strings.hpp:266
MultiMatch(String(&matches)[_N], ReplaceFuncType replaceFunc, void *extra=NULL)
构造函数4,要求匹配项和替换函数,若不进行替换,则replaceFunc可为NULL
Definition: strings.hpp:586
static void Set(char const *lc)
Definition: strings.hpp:667
字符串编码转换
Definition: strings.hpp:755
XStringWriter(XString< _ChTy > *str, bool isAppend=false)
构造函数1
Definition: strings.hpp:485
AnsiString StrTrimA(AnsiString const &str)
SZInput SZ(_CHAR(&sz)[_N])
Definition: strings.hpp:739
UnicodeString StripCSlashesW(UnicodeString const &str)
Definition: strings.hpp:333
AnsiString StrTrim(AnsiString const &str)
祛除字符串首尾出现的空白字符
Definition: strings.hpp:200
AnsiString StrJoinEx(AnsiString const &delim, AnsiStringArray const &arr, size_t start=0, size_t elemCount=-1)
字符串组合连接。start表示开始位置,elemCount表示自开始位置的元素数,默认-1表示自开始位置的全部元素 ...
Definition: strings.hpp:167
UnicodeString FormatExVW(size_t cch, wchar const *fmt, va_list args)
UnicodeString CollateIdentifierToStringW(UnicodeString const &identifier, UnicodeString const &sep, winux::uint flags=wordFirstCharUpper)
uint64 StrToXqU16(char16 const *nptr, char16 const **endptr, int ibase, int flags)
无符号处理
Definition: strings.hpp:265
bool StrGetLine(AnsiString *line, AnsiString const &str, size_t *i, AnsiString *nl=nullptr)
获取字符串中的一行,支持unix,windows,mac平台的行分隔方式 line 不包含换行符,i 指示起始位置,并获得处...
Definition: strings.hpp:365
UnicodeString StrSubtractW(UnicodeString str1, UnicodeString const &str2)
填充based-zero字符串缓冲区包装类
Definition: strings.hpp:719
size_t StrSplit(AnsiString const &str, AnsiString const &delimList, AnsiStringArray *arr, bool alwaysRetOneElem=false)
字符串分割
Definition: strings.hpp:130
AnsiString StripSlashes(AnsiString const &str, AnsiString const &charlist)
去掉反斜杠
Definition: strings.hpp:328
size_t StrSplitA(AnsiString const &str, AnsiString const &delimList, AnsiStringArray *arr, bool alwaysRetOneElem=false)
AnsiString StrUpper(AnsiString str)
使字符串大写
Definition: strings.hpp:219
AnsiString StrSubtract(AnsiString str1, AnsiString const &str2)
字符串相减,str1 - str2,即去掉str1里与str2相同的部分。限制:str1 >= str2。
Definition: strings.hpp:258
AnsiString AddQuotes(AnsiString const &str, AnsiString::value_type quote=Literal< AnsiString::value_type >::quoteChar)
double引号
Definition: strings.hpp:348
XStringArray< char > AnsiStringArray
Definition: utilities.hpp:221
AnsiString StrUpperA(AnsiString str)
std::vector< MatchState > MatchStates
匹配状态数组类型
Definition: strings.hpp:558
ssize_t markpos
标记位置,表示进行到这个位置了,该从这个位置开始
Definition: strings.hpp:548
uint64 StrToXqU32(char32 const *nptr, char32 const **endptr, int ibase, int flags)
UnicodeString LocalToUnicode(AnsiString const &local)
本地Ansi转到Unicode
指定编码转到本地编码
Definition: strings.hpp:814
UnicodeString StrJoinW(UnicodeString const &delim, UnicodeStringArray const &arr)
UnicodeStringArray CollateIdentifierToArrayW(UnicodeString const &identifier, winux::uint flags=wordFirstCharUpper)
AnsiString AddSlashes(AnsiString const &str, AnsiString const &charlist)
加反斜杠
Definition: strings.hpp:308
ConvFrom(char const *fromCode)
Definition: strings.hpp:817
UnicodeString StripSlashesW(UnicodeString const &str, UnicodeString const &charlist)
AnsiString StrInsert(AnsiString const &str, size_t start, size_t end, AnsiString const &insert)
在指定位置插入字符串
Definition: strings.hpp:177
AnsiString AddSlashesA(AnsiString const &str, AnsiString const &charlist)
UnicodeString StrInsertW(UnicodeString const &str, size_t start, size_t end, UnicodeString const &insert)
AnsiString AddQuotesA(AnsiString const &str, AnsiString::value_type quote=Literal< AnsiString::value_type >::quoteChar)
unsigned int uint
Definition: utilities.hpp:170
AnsiString StrJoinA(AnsiString const &delim, AnsiStringArray const &arr)
AnsiString & StrMakeReplace(AnsiString *str, AnsiString const &findText, AnsiString const &replaceText, size_t offset=0)
使字符串全文替换
Definition: strings.hpp:187
本地编码转到指定编码
Definition: strings.hpp:790
static size_t _Templ_KmpMatchEx(_ChTy const *str, size_t len, _ChTy const *substr, size_t sublen, size_t pos, std::vector< _IndexType > const &next)
KMP匹配算法:传入已经求好的next进行匹配
Definition: strings.hpp:432
UnicodeString StrLowerW(UnicodeString str)
AnsiString StrMultiple(AnsiString const &str, int multiple)
字符串倍数的出现
Definition: strings.hpp:248
#define WINUX_FUNC_DECL(ret)
Definition: utilities.hpp:64
size_t convert(char const *srcBuf, size_t srcSize, char **destBuf)
进行编码转换
size_t LocalCharsCount(AnsiString const &local)
返回一个本地字符串里有多少个实际的字符(by local CodePage),用于mbstowcs
UnicodeString AddCSlashesW(UnicodeString const &str)
Definition: strings.hpp:313
AnsiString FormatExV(size_t cch, char const *fmt, va_list args)
格式化字符串0
Definition: strings.hpp:711
读到一个正确的数字字符
Definition: strings.hpp:268
bool StrGetLineW(UnicodeString *line, UnicodeString const &str, size_t *i, UnicodeString *nl=nullptr)
AnsiString Format(char const *fmt,...)
格式化字符串2
String LocalToString(AnsiString const &local)
兼容字符串与Unicode、Local字符串相互转换
首字母大写
Definition: strings.hpp:375
AnsiString CollateIdentifierToStringA(AnsiString const &identifier, AnsiString const &sep, winux::uint flags=wordFirstCharUpper)
AnsiString & StrMakeUpperA(AnsiString *str)
char16_t char16
Definition: utilities.hpp:177
_RetString convert(_String const &str)
进行编码转换
Definition: strings.hpp:774
AnsiString FormatExVA(size_t cch, char const *fmt, va_list args)
XStringArray< wchar > UnicodeStringArray
Definition: utilities.hpp:222
AnsiString StrLower(AnsiString str)
使字符串小写
Definition: strings.hpp:238
std::vector< short > KmpNextValueArray
NEXT值数组类型
Definition: strings.hpp:555
AnsiString & StrMakeLower(AnsiString *str)
使字符串小写
Definition: strings.hpp:229
AnsiString StrSubtractA(AnsiString str1, AnsiString const &str2)
UnicodeString & StrMakeReplaceW(UnicodeString *str, UnicodeString const &findText, UnicodeString const &replaceText, size_t offset=0)
MultiMatch(String(&matches)[_N], String(&replaces)[_N])
构造函数3,要求匹配项和替换项
Definition: strings.hpp:579
bool matched
是否完成匹配,完成匹配就不用再进行
Definition: strings.hpp:551
AnsiString FormatExA(size_t cch, char const *fmt,...)
UnicodeString StringToUnicode(String const &str)
兼容字符串与Unicode、Local字符串相互转换
std::basic_string< _ChTy > XString
Definition: utilities.hpp:210
ssize_t j
下一次从子串开始的位置
Definition: strings.hpp:547
AnsiString CollateIdentifierToString(AnsiString const &identifier, AnsiString const &sep, winux::uint flags=wordFirstCharUpper)
整理标识符为字符串
Definition: strings.hpp:390
bool prevCharMatch
上一个字符是否匹配
Definition: strings.hpp:550
unsigned __int64 uint64
Definition: utilities.hpp:185
static char const * Get()
Definition: strings.hpp:668
UnicodeString FormatW(wchar const *fmt,...)
uint64 StrToXqW(wchar const *nptr, wchar const **endptr, int ibase, int flags)
XStringWriter(XStringWriter const &other)
Definition: strings.hpp:489
String UnicodeToString(UnicodeString const &unicode)
兼容字符串与Unicode、Local字符串相互转换
size_t KmpMatch(char const *str, size_t len, char const *substr, size_t sublen, size_t pos)
KMP字符串匹配算法:匹配
XStringArray< tchar > StringArray
Definition: utilities.hpp:227
AnsiString StripCSlashes(AnsiString const &str)
去掉C-Style反斜杠
Definition: strings.hpp:338
uint64 StrToXq(char const *nptr, char const **endptr, int ibase, int flags)
字符串转换成64位的数字
Definition: strings.hpp:277
设置locale信息
Definition: strings.hpp:661
发生溢出
Definition: strings.hpp:267
MatchResult search(String const &str, ssize_t offset=0) const
搜索任意一项匹配(KMP)
KMP多项匹配/替换
Definition: strings.hpp:536
UnicodeString & StrMakeLowerW(UnicodeString *str)
UnicodeString StrUpperW(UnicodeString str)
AnsiString FormatEx(size_t cch, char const *fmt,...)
格式化字符串1
跨平台基础功能库
Definition: archives.hpp:7
AnsiString LocalToUtf8(AnsiString const &str)
从本地编码转到utf-8
ssize_t item
匹配项的索引
Definition: strings.hpp:542