NCBI C++ ToolKit
ncbistr.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CORELIB___NCBISTR__HPP
2 #define CORELIB___NCBISTR__HPP
3 
4 /* $Id: ncbistr.hpp 102091 2024-03-29 13:22:37Z ivanov $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Eugene Vasilchenko, Denis Vakatov
30  *
31  *
32  */
33 
34 /// @file ncbistr.hpp
35 /// The NCBI C++ standard methods for dealing with std::string
36 
37 
38 #include <corelib/ncbi_limits.hpp>
39 #include <corelib/tempstr.hpp>
40 #include <corelib/ncbierror.hpp>
41 #ifdef NCBI_OS_OSF1
42 # include <strings.h>
43 #endif
44 #include <stdarg.h>
45 #include <time.h>
46 #include <set>
47 #include <functional>
48 
49 
51 
52 /** @addtogroup String
53  *
54  * @{
55  */
56 
57 /// Empty "C" string (points to a '\0').
58 NCBI_XNCBI_EXPORT extern const char *const kEmptyCStr;
59 #define NcbiEmptyCStr NCBI_NS_NCBI::kEmptyCStr
60 
61 #if defined(HAVE_WSTRING)
62 NCBI_XNCBI_EXPORT extern const wchar_t *const kEmptyWCStr;
63 #define NcbiEmptyWCStr NCBI_NS_NCBI::kEmptyWCStr
64 #endif
65 
66 /// Empty "C++" string.
67 #if defined(NCBI_OS_MSWIN) \
68  || (defined(NCBI_OS_LINUX) \
69  && (defined(NCBI_COMPILER_GCC) || defined(NCBI_COMPILER_ANY_CLANG)))
71 {
72 public:
73  /// Get string.
74  inline
75  static const string& Get(void)
76  {
77  static string empty_str;
78  return empty_str;
79  }
80 };
81 #if defined(HAVE_WSTRING)
83 {
84 public:
85  /// Get string.
86  static const wstring& Get(void)
87  {
88  static wstring empty_str;
89  return empty_str;
90  }
91 };
92 #endif
93 #else
95 {
96 public:
97  /// Get string.
98  static const string& Get(void);
99 private:
100  /// Helper method to initialize private data member and return
101  /// null string.
102  static const string& FirstGet(void);
103  static const string* m_Str; ///< Null string pointer.
104 };
105 
106 # if defined(HAVE_WSTRING)
108 {
109 public:
110  /// Get string.
111  static const wstring& Get(void);
112 private:
113  /// Helper method to initialize private data member and return
114  /// null string.
115  static const wstring& FirstGet(void);
116  static const wstring* m_Str; ///< Null string pointer.
117 };
118 # endif
119 #endif // NCBI_OS_MSWIN....
120 
121 
122 #define NcbiEmptyString NCBI_NS_NCBI::CNcbiEmptyString::Get()
123 #define kEmptyStr NcbiEmptyString
124 
125 #if defined(HAVE_WSTRING)
126 # define NcbiEmptyWString NCBI_NS_NCBI::CNcbiEmptyWString::Get()
127 # define kEmptyWStr NcbiEmptyWString
128 #endif
129 
130 // SIZE_TYPE and NPOS
131 
132 typedef NCBI_NS_STD::string::size_type SIZE_TYPE;
133 #define NPOS NCBI_NS_STD::string::npos
134 
135 
136 
137 /////////////////////////////////////////////////////////////////////////////
138 // Unicode-related definitions and conversions
139 
140 /// Unicode character
142 /// Unicode string
143 typedef basic_string<TUnicodeSymbol> TStringUnicode;
144 
145 #if defined(NCBI_OS_MSWIN) && defined(_UNICODE)
146 
147 typedef wchar_t TXChar;
148 typedef wstring TXString;
149 
150 # if !defined(_TX)
151 # define _TX(x) L ## x
152 # endif
153 
154 # if defined(_DEBUG)
155 # define _T_XSTRING(x) \
156  ncbi::CUtf8::AsBasicString<ncbi::TXChar>(x, NULL, ncbi::CUtf8::eValidate)
157 # else
158 # define _T_XSTRING(x) \
159  ncbi::CUtf8::AsBasicString<ncbi::TXChar>(x, NULL, ncbi::CUtf8::eNoValidate)
160 # endif
161 # define _T_STDSTRING(x) ncbi::CUtf8::AsUTF8(x)
162 # define _T_XCSTRING(x) _T_XSTRING(x).c_str()
163 # define _T_CSTRING(x) _T_STDSTRING(x).c_str()
164 
165 # define NcbiEmptyXCStr NcbiEmptyWCStr
166 # define NcbiEmptyXString NcbiEmptyWString
167 # define kEmptyXStr kEmptyWStr
168 # define kEmptyXCStr kEmptyWCStr
169 
170 #else
171 
172 typedef char TXChar;
173 typedef string TXString;
174 
175 # if !defined(_TX)
176 # define _TX(x) x
177 # endif
178 
179 # define _T_XSTRING(x) (x)
180 # define _T_STDSTRING(x) (x)
181 # define _T_XCSTRING(x) ncbi::impl_ToCString(x)
182 # define _T_CSTRING(x) (x)
183 
184 # define NcbiEmptyXCStr NcbiEmptyCStr
185 # define NcbiEmptyXString NcbiEmptyString
186 # define kEmptyXStr kEmptyStr
187 # define kEmptyXCStr kEmptyCStr
188 
189 inline const char* impl_ToCString(const char* s) { return s; }
190 inline const char* impl_ToCString(const string& s) { return s.c_str(); }
191 
192 #endif
193 
194 
195 /////////////////////////////////////////////////////////////////////////////
196 ///
197 
198 #if defined(NCBI_CUTF8_ENCODING_CLASSIC) || !defined(HAVE_ENUM_CLASS)
199 enum EEncoding {
203  eEncoding_ISO8859_1, ///< Note: From the point of view of the C++
204  ///< Toolkit, the ISO 8859-1 character set includes
205  ///< symbols 0x00 through 0xFF except 0x80 through
206  ///< 0x9F.
209 };
210 #else
211 // Temporary safeguard to protect against implicit conversion of EEncoding
212 // to size_t, etc
213 // @attention Do not use "EEncoding::Xxx" values directly, as they will go
214 // away eventually! Use the "eEncoding_Xxx" values instead.
215 enum class EEncoding {
216  Unknown, ///< Do not use this directly! It will go away eventually!
217  UTF8, ///< Do not use this directly! It will go away eventually!
218  Ascii, ///< Do not use this directly! It will go away eventually!
219  ISO8859_1, ///< Do not use this directly! It will go away eventually!
220  Windows_1252, ///< Do not use this directly! It will go away eventually!
221  // CESU-8 spec https://www.unicode.org/reports/tr26/tr26-4.html
222  // It is not intended nor recommended for open information exchange.
223  // but since it may appear in incoming data, we want to be able to detect it
224  // and convert into 'normal' UTF-8
225  CESU8
226 };
227 #define eEncoding_Unknown EEncoding::Unknown
228 #define eEncoding_UTF8 EEncoding::UTF8
229 #define eEncoding_Ascii EEncoding::Ascii
230 #define eEncoding_ISO8859_1 EEncoding::ISO8859_1
231 #define eEncoding_Windows_1252 EEncoding::Windows_1252
232 #define eEncoding_CESU8 EEncoding::CESU8
233 #endif
234 
235 
236 /////////////////////////////////////////////////////////////////////////////
237 ///
238 /// NStr --
239 ///
240 /// Encapsulates class-wide string processing functions.
241 
243 {
244 public:
245  /// Number to string conversion flags.
246  ///
247  /// NOTE:
248  /// If specified base in the *ToString() methods is not default 10,
249  /// that some flags like fWithSign and fWithCommas will be ignored.
251  fUseLowercase = (1 << 4), ///< Use lowercase letters for string representation for bases above 10
252  fWithRadix = (1 << 5), ///< Prefix the output value with radix for "well-known" bases like 8 ("0") and 16 ("0x")
253  fWithSign = (1 << 6), ///< Prefix the output value with a sign ('+'/'-')
254  fWithCommas = (1 << 7), ///< Use commas as thousands separator
255  fDoubleFixed = (1 << 8), ///< DoubleToString*(): Use n.nnnn format for double conversions
256  fDoubleScientific = (1 << 9), ///< DoubleToString*(): Use scientific format for double conversions
257  fDoublePosix = (1 << 10), ///< DoubleToString*(): Use C locale for double conversions
258  fDoubleGeneral = fDoubleFixed | fDoubleScientific,
259  // Additional flags to convert "software" qualifiers (see UInt8ToString_DataSize)
260  fDS_Binary = (1 << 11), ///< UInt8ToString_DataSize(): Use 1024 as a kilobyte factor, not 1000.
261  fDS_NoDecimalPoint = (1 << 12), ///< UInt8ToString_DataSize(): Do not add a decimal point ("10KB" vs "10.0KB")
262  fDS_PutSpaceBeforeSuffix = (1 << 13), ///< UInt8ToString_DataSize(): Add space between value and qualifiers, like "10.0 KB"
263  fDS_ShortSuffix = (1 << 14), ///< UInt8ToString_DataSize(): Use short suffix, like "10.0K"
264  fDS_PutBSuffixToo = (1 << 15) ///< UInt8ToString_DataSize(): Use "B" suffix for small bytes values.
265  };
266  typedef int TNumToStringFlags; ///< Bitwise OR of "ENumToStringFlags"
267 
268  /// String to number conversion flags.
270  /// Do not throw an exception on error.
271  /// Could be used with methods throwing an exception by default, ignored otherwise.
272  /// Just return zero and set errno to non-zero instead of throwing an exception.
273  /// We recommend the following technique to check against errors
274  /// with minimum overhead when this flag is used:
275  /// @code
276  /// if (!retval && errno != 0)
277  /// ERROR;
278  /// @endcode
279  /// And for StringToDouble*() variants:
280  /// @code
281  /// if (retval == HUGE_VAL || retval == -HUGE_VAL ||
282  /// !retval && errno != 0)
283  /// ERROR;
284  /// @endcode
285  fConvErr_NoThrow = (1 << 0),
286  /*
287  fConvErr_NoErrno = (1 << 1), ///< Do not set errno at all.
288  ///< If used together with fConvErr_NoThrow flag
289  ///< returns 0 on error (-1 for StringToNonNegativeInt).
290  */
291  fConvErr_NoErrMessage = (1 << 2), ///< Set errno, but do not set CNcbiError message on error
292  fMandatorySign = (1 << 17), ///< Check on mandatory sign. See 'ENumToStringFlags::fWithSign'.
293  fAllowCommas = (1 << 18), ///< Allow commas. See 'ENumToStringFlags::fWithCommas'.
294  fAllowLeadingSpaces = (1 << 19), ///< Ignore leading spaces in converted string.
295  fAllowLeadingSymbols = (1 << 20) | fAllowLeadingSpaces,
296  ///< Ignore leading non-numeric characters.
297  fAllowTrailingSpaces = (1 << 21), ///< Ignore trailing space characters.
298  fAllowTrailingSymbols = (1 << 22) | fAllowTrailingSpaces,
299  ///< Ignore trailing non-numerics characters.
300  fDecimalPosix = (1 << 23), ///< StringToDouble*(): For decimal point, use C locale.
301  fDecimalPosixOrLocal = (1 << 24), ///< StringToDouble*(): For decimal point, try both C and current locale.
302  fDecimalPosixFinite = (1 << 25), ///< StringToDouble*(): Keep result finite and normalized:
303  ///< if DBL_MAX < result < INF, result becomes DBL_MAX
304  ///< if 0 < result < DBL_MIN, result becomes DBL_MIN
305  // Additional flags to convert "software" qualifiers (see StringToUInt8_DataSize)
306  fDS_ForceBinary = (1 << 26), ///< StringToUInt8_DataSize(): Use 1024 as a kilobyte factor regardless of suffix, like "KB" or "KiB".
307  fDS_ProhibitFractions = (1 << 27), ///< StringToUInt8_DataSize(): Ignore any fraction part of a value, "1.2K" ~ "1K"
308  fDS_ProhibitSpaceBeforeSuffix = (1 << 28) ///< StringToUInt8_DataSize(): Do not allow spaces between value and suffix, like "10 K".
309  };
310  typedef EStringToNumFlags EConvErrFlags; ///< Formerly split out
311  typedef int TStringToNumFlags; ///< Bitwise OR of "EStringToNumFlags"
312  typedef TStringToNumFlags TConvErrFlags; ///< Formerly split out
313 
314  /// Convert string to a numeric value.
315  ///
316  /// @param str
317  /// String to be converted.
318  /// @param flags
319  /// Optional flags to tune up how the string is converted to value.
320  /// @param base
321  /// Radix base. Allowed values are 0, 2..36. Zero means to use the
322  /// first characters to determine the base - a leading "0x" or "0X"
323  /// means base 16; otherwise a leading 0 means base 8; otherwise base 10.
324  /// @return
325  /// - If conversion succeeds, set errno to zero and return the
326  /// converted value.
327  /// - Otherwise, if fConvErr_NoThrow is not set, throw an exception.
328  /// - Otherwise, set errno to non-zero and return zero.
329  template <typename TNumeric>
330  static TNumeric StringToNumeric(const CTempString str,
332  int base = 10)
333  {
334  return x_StringToNumeric<TNumeric>(str, flags, base);
335  }
336 
337  /// Convert string to a numeric value.
338  ///
339  /// @param str [in]
340  /// String to be converted.
341  /// @param value [out]
342  /// The numeric value represented by "str". Zero on any error.
343  /// @param flags [in]
344  /// Optional flags to tune up how the string is converted to value.
345  /// @param base [in]
346  /// Radix base. Allowed values are 0, 2..36. Zero means to use the
347  /// first characters to determine the base - a leading "0x" or "0X"
348  /// means base 16; otherwise a leading 0 means base 8; otherwise base 10.
349  /// @return
350  /// - If conversion succeeds, set errno to zero, set the value, and
351  /// return true.
352  /// - Otherwise, if fConvErr_NoThrow is not set, throw an exception.
353  /// - Otherwise, set errno to non-zero, set the value to zero, and
354  /// return false.
355  template <typename TNumeric>
356  static bool StringToNumeric(const CTempString str,
357  TNumeric* value, /*[out]*/
359  int base = 10)
360  {
361  return x_StringToNumeric(str, value, flags, base);
362  }
363 
364  /// Convert string to non-negative integer value.
365  ///
366  /// @param str
367  /// String containing only digits, representing non-negative
368  /// decimal value in the int range: [0..kMax_Int].
369  /// @param flags
370  /// How to convert string to value.
371  /// Only fConvErr_NoErrMessage flag is supported here.
372  /// @return
373  /// - If conversion succeeds, set errno to zero and return the converted value.
374  /// - Otherwise, set errno to non-zero and return -1.
375  static int StringToNonNegativeInt(const CTempString str, TStringToNumFlags flags = 0);
376 
377  /// @deprecated
378  /// Use template-based StringToNumeric<> or StringToNonNegativeInt() instead.
380  static int StringToNumeric(const string& str)
381  {
382  return StringToNonNegativeInt(str);
383  }
384 
385  /// Convert string to int.
386  ///
387  /// @param str
388  /// String to be converted.
389  /// @param flags
390  /// How to convert string to value.
391  /// @param base
392  /// Radix base. Allowed values are 0, 2..36. Zero means to use the
393  /// first characters to determine the base - a leading "0x" or "0X"
394  /// means base 16; otherwise a leading 0 means base 8; otherwise base 10.
395  /// @return
396  /// - If conversion succeeds, set errno to zero and return the
397  /// converted value.
398  /// - Otherwise, if fConvErr_NoThrow is not set, throw an exception.
399  /// - Otherwise, set errno to non-zero and return zero.
400  static int StringToInt(const CTempString str,
401  TStringToNumFlags flags = 0,
402  int base = 10);
403 
404  /// Convert string to unsigned int.
405  ///
406  /// @param str
407  /// String to be converted.
408  /// @param flags
409  /// How to convert string to value.
410  /// @param base
411  /// Radix base. Allowed values are 0, 2..36. Zero means to use the
412  /// first characters to determine the base - a leading "0x" or "0X"
413  /// means base 16; otherwise a leading 0 means base 8; otherwise base 10.
414  /// @return
415  /// - If conversion succeeds, set errno to zero and return the
416  /// converted value.
417  /// - Otherwise, if fConvErr_NoThrow is not set, throw an exception.
418  /// - Otherwise, set errno to non-zero and return zero.
419  static unsigned int StringToUInt(const CTempString str,
420  TStringToNumFlags flags = 0,
421  int base = 10);
422 
423  /// Convert string to long.
424  ///
425  /// @param str
426  /// String to be converted.
427  /// @param flags
428  /// How to convert string to value.
429  /// @param base
430  /// Radix base. Allowed values are 0, 2..36. Zero means to use the
431  /// first characters to determine the base - a leading "0x" or "0X"
432  /// means base 16; otherwise a leading 0 means base 8; otherwise base 10.
433  /// @return
434  /// - If conversion succeeds, set errno to zero and return the
435  /// converted value.
436  /// - Otherwise, if fConvErr_NoThrow is not set, throw an exception.
437  /// - Otherwise, set errno to non-zero and return zero.
438  static long StringToLong(const CTempString str,
439  TStringToNumFlags flags = 0,
440  int base = 10);
441 
442  /// Convert string to unsigned long.
443  ///
444  /// @param str
445  /// String to be converted.
446  /// @param flags
447  /// How to convert string to value.
448  /// @param base
449  /// Radix base. Allowed values are 0, 2..36. Zero means to use the
450  /// first characters to determine the base - a leading "0x" or "0X"
451  /// means base 16; otherwise a leading 0 means base 8; otherwise base 10.
452  /// @return
453  /// - If conversion succeeds, set errno to zero and return the
454  /// converted value.
455  /// - Otherwise, if fConvErr_NoThrow is not set, throw an exception.
456  /// - Otherwise, set errno to non-zero and return zero.
457  static unsigned long StringToULong(const CTempString str,
458  TStringToNumFlags flags = 0,
459  int base = 10);
460 
461  /// Convert string to double-precision value (analog of strtod function)
462  ///
463  /// @param str
464  /// String to be converted.
465  /// @param endptr
466  /// Pointer to character that stops scan.
467  /// @return
468  /// Double-precision value.
469  /// This function always uses dot as decimal separator.
470  /// - on overflow, it returns HUGE_VAL and sets errno to ERANGE;
471  /// - on underflow, it returns 0 and sets errno to ERANGE;
472  /// - if conversion was impossible, it returns 0 and sets errno.
473  /// Also, when input string equals (case-insensitive) to
474  /// - "NAN", the function returns NaN;
475  /// - "INF" or "INFINITY", the function returns HUGE_VAL;
476  /// - "-INF" or "-INFINITY", the function returns -HUGE_VAL;
477  /// @note
478  /// - If conversion succeeds, set errno to zero and return the
479  /// converted value.
480  /// - Otherwise, set errno to non-zero and return zero.
481  /// - Denormal or infinite results are considered successful conversion.
482  /// - To enforce finite and normalized result, use fDecimalPosixFinite flag.
483  /// - This function is meant to be more "low-level" than other
484  /// StringToXxx functions - for example, it allows trailing characters
485  /// (and doesn't include a flags parameter for tweaking such behavior).
486  /// This could result in strings like "nanosecond" being converted to
487  /// NaN, "-inf=input_file" being converted to -INF, or other unexpected
488  /// behavior. Therefore, please consider using StringToDouble unless
489  /// you specifically need this functionality.
490  static double StringToDoublePosix(const char* str, char** endptr=0,
491  TStringToNumFlags flags=0);
492 
493 
494  /// Convert string to double.
495  ///
496  /// @param str
497  /// String to be converted.
498  /// @param flags
499  /// How to convert string to value.
500  /// Do not support fAllowCommas flag.
501  /// @return
502  /// - If invalid flags are passed, throw an exception.
503  /// - If conversion succeeds, set errno to zero and return the
504  /// converted value.
505  /// - Otherwise, if fConvErr_NoThrow is not set, throw an exception.
506  /// - Otherwise, set errno to non-zero and return zero.
507  /// @note
508  /// - Denormal or infinite results are considered successful conversion.
509  /// - To enforce finite and normalized result, use fDecimalPosixFinite flag.
510  static double StringToDouble(const CTempStringEx str,
511  TStringToNumFlags flags = 0);
512 
513  /// This version accepts zero-terminated string
514  /// @deprecated
515  /// It is unsafe to use this method directly, please use StringToDouble()
516  /// instead.
518  static double StringToDoubleEx(const char* str, size_t size,
519  TStringToNumFlags flags = 0);
520 
521  /// Convert string to Int8.
522  ///
523  /// @param str
524  /// String to be converted.
525  /// @param flags
526  /// How to convert string to value.
527  /// @param base
528  /// Radix base. Allowed values are 0, 2..36. Zero means to use the
529  /// first characters to determine the base - a leading "0x" or "0X"
530  /// means base 16; otherwise a leading 0 means base 8; otherwise base 10.
531  /// @return
532  /// - If conversion succeeds, set errno to zero and return the
533  /// converted value.
534  /// - Otherwise, if fConvErr_NoThrow is not set, throw an exception.
535  /// - Otherwise, set errno to non-zero and return zero.
536  static Int8 StringToInt8(const CTempString str,
537  TStringToNumFlags flags = 0,
538  int base = 10);
539 
540  /// Convert string to Uint8.
541  ///
542  /// @param str
543  /// String to be converted.
544  /// @param flags
545  /// How to convert string to value.
546  /// @param base
547  /// Radix base. Allowed values are 0, 2..36. Zero means to use the
548  /// first characters to determine the base - a leading "0x" or "0X"
549  /// means base 16; otherwise a leading 0 means base 8; otherwise base 10.
550  /// @return
551  /// - If conversion succeeds, set errno to zero and return the
552  /// converted value.
553  /// - Otherwise, if fConvErr_NoThrow is not set, throw an exception.
554  /// - Otherwise, set errno to non-zero and return zero.
555  static Uint8 StringToUInt8(const CTempString str,
556  TStringToNumFlags flags = 0,
557  int base = 10);
558 
559  /// Convert string that can contain "software" qualifiers to Uint8.
560  ///
561  /// String can contain "software" qualifiers: G(giga-), MB(mega-),
562  /// KiB (kibi-) etc.
563  /// Example: 100MB, 1024KiB, 5.7G.
564  /// Meaning of qualifiers depends on flags and by default is 1000-based
565  /// (i.e. K=1000, M=10^6 etc.) except in cases when qualifiers with "iB"
566  /// are used, i.e. KiB=1024, MiB=1024^2 etc. When flags parameter contains
567  /// fDS_ForceBinary then qualifiers without "iB" (i.e. "K" or "MB") will
568  /// also be 1024-based.
569  /// String can contain a decimal fraction (except when fDS_ProhibitFractions
570  /// flag is used), in this case the resultant Uint8 number will be rounded
571  /// to fit into integer value.
572  ///
573  /// @param str
574  /// String to be converted.
575  /// @param flags
576  /// How to convert string to value.
577  /// @return
578  /// - If invalid flags are passed, throw an exception.
579  /// - If conversion succeeds, return the converted value.
580  /// - Otherwise, if fConvErr_NoThrow is not set, throw an exception.
581  /// - Otherwise, set errno to non-zero and return zero.
582  static Uint8 StringToUInt8_DataSize(const CTempString str,
583  TStringToNumFlags flags = 0);
584 
585  /// Convert string to number of bytes.
586  ///
587  /// String can contain "software" qualifiers: MB(megabyte), KB (kilobyte).
588  /// Example: 100MB, 1024KB
589  /// Note the qualifiers are power-of-2 based, aka kibi-, mebi- etc, so that
590  /// 1KB = 1024B (not 1000B), 1MB = 1024KB = 1048576B, etc.
591  ///
592  /// @param str
593  /// String to be converted.
594  /// @param flags
595  /// How to convert string to value.
596  /// @param base
597  /// Numeric base of the number (before the qualifier). Allowed values
598  /// are 0, 2..20. Zero means to use the first characters to determine
599  /// the base - a leading "0x" or "0X" means base 16; otherwise a
600  /// leading 0 means base 8; otherwise base 10.
601  /// The base is limited to 20 to prevent 'K' from being interpreted as
602  /// a digit in the number.
603  /// @return
604  /// - If conversion succeeds, set errno to zero and return the
605  /// converted value.
606  /// - Otherwise, if fConvErr_NoThrow is not set, throw an exception.
607  /// - Otherwise, set errno to non-zero and return zero.
608  /// @deprecated Use StringToUInt8_DataSize(str, flags) instead.
610  static Uint8 StringToUInt8_DataSize(const CTempString str,
611  TStringToNumFlags flags,
612  int base);
613 
614  /// Convert string to size_t.
615  ///
616  /// @param str
617  /// String to be converted.
618  /// @param flags
619  /// How to convert string to value.
620  /// @param base
621  /// Radix base. Allowed values are 0, 2..36. Zero means to use the
622  /// first characters to determine the base - a leading "0x" or "0X"
623  /// means base 16; otherwise a leading 0 means base 8; otherwise base 10.
624  /// @return
625  /// - If conversion succeeds, set errno to zero and return the
626  /// converted value.
627  /// - Otherwise, if fConvErr_NoThrow is not set, throw an exception.
628  /// - Otherwise, set errno to non-zero and return zero.
629  static size_t StringToSizet(const CTempString str,
630  TStringToNumFlags flags = 0,
631  int base = 10);
632 
633  /// Convert string to pointer.
634  ///
635  /// @param str
636  /// String to be converted.
637  /// @param flags
638  /// How to convert string to value.
639  /// Only fConvErr_NoErrMessage flag is supported here.
640  /// @return
641  /// Pointer value corresponding to its string representation.
642  /// - If conversion succeeds, set errno to zero and return the
643  /// converted value.
644  /// - Otherwise, set errno to non-zero and return NULL.
645  static const void* StringToPtr(const CTempStringEx str, TStringToNumFlags flags = 0);
646 
647  /// Convert character to integer.
648  ///
649  /// @param ch
650  /// Character to be converted.
651  /// @return
652  /// Integer (0..15) corresponding to the "ch" as a hex digit.
653  /// Return -1 on error.
654  static int HexChar(char ch);
655 
656  /// Convert numeric value to string.
657  ///
658  /// @param value
659  /// Numeric value to be converted.
660  /// @param flags
661  /// How to convert value to string.
662  /// @param base
663  /// Radix base. Default is 10. Allowed values are 2..36.
664  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
665  /// If necessary you should add it yourself.
666  /// If value is float or double type, the parameter is ignored.
667  /// @return
668  /// - If conversion succeeds, set errno to zero and return the
669  /// converted string value.
670  /// - Otherwise, set errno to non-zero and return empty string.
671  template<typename TNumeric>
673  NumericToString(TNumeric value, TNumToStringFlags flags = 0, int base = 10)
674  {
675  string ret;
676  x_NumericToString(ret, value, flags, base);
677  return ret;
678  }
679  template <typename TStrictId>
680  static typename enable_if< is_integral<typename TStrictId::TId>::value && is_member_function_pointer<decltype(&TStrictId::Get)>::value, string>::type
681  NumericToString(TStrictId value, TNumToStringFlags flags = 0, int base = 10)
682  {
683  return NumericToString(value.Get(), flags, base);
684  }
685 
686  /// Convert numeric value to string.
687  ///
688  /// @param out_str
689  /// Output string variable.
690  /// @param value
691  /// Numeric value to be converted.
692  /// @param flags
693  /// How to convert value to string.
694  /// @param base
695  /// Radix base. Default is 10. Allowed values are 2..36.
696  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
697  /// If necessary you should add it yourself.
698  /// If value is float or double type, the parameter is ignored.
699  /// @note
700  /// - If conversion succeeds, set errno to zero and return the
701  /// converted string value in 'out_str'.
702  /// - Otherwise, set errno to non-zero, value of 'out_str' is undefined.
703  template<typename TNumeric>
704  static void NumericToString(string& out_str, TNumeric value,
705  TNumToStringFlags flags = 0, int base = 10)
706  {
707  x_NumericToString(out_str, value, flags, base);
708  }
709 
710  /// Convert int to string.
711  ///
712  /// @param value
713  /// Integer value to be converted.
714  /// @param flags
715  /// How to convert value to string.
716  /// @param base
717  /// Radix base. Default is 10. Allowed values are 2..36.
718  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
719  /// If necessary you should add it yourself.
720  /// @return
721  /// - If conversion succeeds, set errno to zero and return the
722  /// converted string value.
723  /// - Otherwise, set errno to non-zero and return empty string.
724  static string IntToString(int value, TNumToStringFlags flags = 0,
725  int base = 10);
726 
727  static string IntToString(unsigned int value, TNumToStringFlags flags = 0,
728  int base = 10);
729 
730  /// Convert int to string.
731  ///
732  /// @param out_str
733  /// Output string variable.
734  /// @param value
735  /// Integer value to be converted.
736  /// @param flags
737  /// How to convert value to string.
738  /// @param base
739  /// Radix base. Default is 10. Allowed values are 2..36.
740  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
741  /// If necessary you should add it yourself.
742  /// @note
743  /// - If conversion succeeds, set errno to zero and return the
744  /// converted string value in 'out_str'.
745  /// - Otherwise, set errno to non-zero, value of 'out_str' is undefined.
746  static void IntToString(string& out_str, int value,
747  TNumToStringFlags flags = 0,
748  int base = 10);
749 
750  static void IntToString(string& out_str, unsigned int value,
751  TNumToStringFlags flags = 0,
752  int base = 10);
753 
754  /// Convert UInt to string.
755  ///
756  /// @param value
757  /// Integer value (unsigned long) to be converted.
758  /// @param flags
759  /// How to convert value to string.
760  /// @param base
761  /// Radix base. Default is 10. Allowed values are 2..36.
762  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
763  /// If necessary you should add it yourself.
764  /// @return
765  /// - If conversion succeeds, set errno to zero and return the
766  /// converted string value.
767  /// - Otherwise, set errno to non-zero and return empty string.
768  static string UIntToString(unsigned int value,
769  TNumToStringFlags flags = 0,
770  int base = 10);
771 
772  static string UIntToString(int value,
773  TNumToStringFlags flags = 0,
774  int base = 10);
775 
776  /// Convert UInt to string.
777  ///
778  /// @param out_str
779  /// Output string variable
780  /// @param value
781  /// Integer value (unsigned long) to be converted.
782  /// @param flags
783  /// How to convert value to string.
784  /// @param base
785  /// Radix base. Default is 10. Allowed values are 2..36.
786  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
787  /// If necessary you should add it yourself.
788  /// @note
789  /// - If conversion succeeds, set errno to zero and return the
790  /// converted string value in 'out_str'.
791  /// - Otherwise, set errno to non-zero, value of 'out_str' is undefined.
792  static void UIntToString(string& out_str, unsigned int value,
793  TNumToStringFlags flags = 0,
794  int base = 10);
795 
796  static void UIntToString(string& out_str, int value,
797  TNumToStringFlags flags = 0,
798  int base = 10);
799 
800  /// Convert Int to string.
801  ///
802  /// @param value
803  /// Integer value (long) to be converted.
804  /// @param flags
805  /// How to convert value to string.
806  /// @param base
807  /// Radix base. Default is 10. Allowed values are 2..36.
808  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
809  /// If necessary you should add it yourself.
810  /// @return
811  /// - If conversion succeeds, set errno to zero and return the
812  /// converted string value.
813  /// - Otherwise, set errno to non-zero and return empty string.
814  static string LongToString(long value, TNumToStringFlags flags = 0,
815  int base = 10);
816 
817  /// Convert Int to string.
818  ///
819  /// @param out_str
820  /// Output string variable.
821  /// @param value
822  /// Integer value (long) to be converted.
823  /// @param flags
824  /// How to convert value to string.
825  /// @param base
826  /// Radix base. Default is 10. Allowed values are 2..36.
827  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
828  /// If necessary you should add it yourself.
829  /// @note
830  /// - If conversion succeeds, set errno to zero and return the
831  /// converted string value in 'out_str'.
832  /// - Otherwise, set errno to non-zero, value of 'out_str' is undefined.
833  static void LongToString(string& out_str, long value,
834  TNumToStringFlags flags = 0,
835  int base = 10);
836 
837  /// Convert unsigned long to string.
838  ///
839  /// @param value
840  /// Integer value (unsigned long) to be converted.
841  /// @param flags
842  /// How to convert value to string.
843  /// @param base
844  /// Radix base. Default is 10. Allowed values are 2..36.
845  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
846  /// If necessary you should add it yourself.
847  /// @return
848  /// - If conversion succeeds, set errno to zero and return the
849  /// converted string value.
850  /// - Otherwise, set errno to non-zero and return empty string.
851  static string ULongToString(unsigned long value,
852  TNumToStringFlags flags = 0,
853  int base = 10);
854 
855  /// Convert unsigned long to string.
856  ///
857  /// @param out_str
858  /// Output string variable
859  /// @param value
860  /// Integer value (unsigned long) to be converted.
861  /// @param flags
862  /// How to convert value to string.
863  /// @param base
864  /// Radix base. Default is 10. Allowed values are 2..36.
865  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
866  /// If necessary you should add it yourself.
867  /// @note
868  /// - If conversion succeeds, set errno to zero and return the
869  /// converted string value in 'out_str'.
870  /// - Otherwise, set errno to non-zero, value of 'out_str' is undefined.
871  static void ULongToString(string& out_str, unsigned long value,
872  TNumToStringFlags flags = 0,
873  int base = 10);
874 
875  /// Convert Int8 to string.
876  ///
877  /// @param value
878  /// Integer value (Int8) to be converted.
879  /// @param flags
880  /// How to convert value to string.
881  /// @param base
882  /// Radix base. Default is 10. Allowed values are 2..36.
883  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
884  /// If necessary you should add it yourself.
885  /// @return
886  /// - If conversion succeeds, set errno to zero and return the
887  /// converted string value.
888  /// - Otherwise, set errno to non-zero and return empty string.
889  static string Int8ToString(Int8 value,
890  TNumToStringFlags flags = 0,
891  int base = 10);
892 
893  /// Convert Int8 to string.
894  ///
895  /// @param out_str
896  /// Output string variable
897  /// @param value
898  /// Integer value (Int8) to be converted.
899  /// @param flags
900  /// How to convert value to string.
901  /// @param base
902  /// Radix base. Default is 10. Allowed values are 2..36.
903  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
904  /// If necessary you should add it yourself.
905  /// @note
906  /// - If conversion succeeds, set errno to zero and return the
907  /// converted string value in 'out_str'.
908  /// - Otherwise, set errno to non-zero, value of 'out_str' is undefined.
909  static void Int8ToString(string& out_str, Int8 value,
910  TNumToStringFlags flags = 0,
911  int base = 10);
912 
913  /// Convert UInt8 to string.
914  ///
915  /// @param value
916  /// Integer value (UInt8) to be converted.
917  /// @param flags
918  /// How to convert value to string.
919  /// @param base
920  /// Radix base. Default is 10. Allowed values are 2..36.
921  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
922  /// If necessary you should add it yourself.
923  /// @return
924  /// - If conversion succeeds, set errno to zero and return the
925  /// converted string value.
926  /// - Otherwise, set errno to non-zero and return empty string.
927  static string UInt8ToString(Uint8 value,
928  TNumToStringFlags flags = 0,
929  int base = 10);
930 
931  /// Convert UInt8 to string.
932  ///
933  /// @param out_str
934  /// Output string variable
935  /// @param value
936  /// Integer value (UInt8) to be converted.
937  /// @param flags
938  /// How to convert value to string.
939  /// @param base
940  /// Radix base. Default is 10. Allowed values are 2..36.
941  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
942  /// If necessary you should add it yourself.
943  /// @note
944  /// - If conversion succeeds, set errno to zero and return the
945  /// converted string value in 'out_str'.
946  /// - Otherwise, set errno to non-zero, value of 'out_str' is undefined.
947  static void UInt8ToString(string& out_str, Uint8 value,
948  TNumToStringFlags flags = 0,
949  int base = 10);
950 
951  /// Convert UInt8 to string using "software" qualifiers.
952  ///
953  /// Result of conversion will be limited to max_digits digits so that e.g.
954  /// 1024 will be converted to 1.02KB. Conversion will be made using
955  /// rounding so that 1025 will be converted to 1.03KB. By default function
956  /// uses 1000-based qualifiers (as in examples above) but with fDS_Binary
957  /// flag it will use 1024-based qualifiers, e.g. 1100 will be converted to
958  /// 1.07KiB. With fDS_ShortSuffix flag function will omit "B" in 1000-based
959  /// and "iB" in 1024-based qualifiers. When the result of conversion doesn't
960  /// need any qualifiers then the result of this function will be equivalent
961  /// to result of UInt8ToString() above except if fDS_PutBSuffixToo flag
962  /// is passed. In the latter case "B" will be added to the number.
963  ///
964  /// Function will always try to use a maximum possible qualifier and
965  /// a number with decimal point except if fDS_NoDecimalPoint flag is passed.
966  /// In that case function will return only whole number and try to use a
967  /// minimum possible qualifier (which makes difference only if
968  /// max_digits > 3).
969  ///
970  /// @param value
971  /// Integer value (UInt8) to be converted.
972  /// @param flags
973  /// How to convert value to string.
974  /// @param max_digits
975  /// Maximum number of digits to use (cannot be less than 3)
976  /// @return
977  /// - If invalid flags are passed, throw an exception.
978  /// - If conversion succeeds, return the converted value.
979  static string UInt8ToString_DataSize(Uint8 value,
980  TNumToStringFlags flags = 0,
981  unsigned int max_digits = 3);
982 
983  /// Convert UInt8 to string using "software" qualifiers.
984  ///
985  /// See notes and details of how function works in the comments to
986  /// UInt8ToString_DataSize() above.
987  ///
988  /// @param out_str
989  /// Output string variable
990  /// @param value
991  /// Integer value (UInt8) to be converted.
992  /// @param flags
993  /// How to convert value to string.
994  /// @param max_digits
995  /// Maximum number of digits to use (cannot be less than 3)
996  static void UInt8ToString_DataSize(string& out_str,
997  Uint8 value,
998  TNumToStringFlags flags = 0,
999  unsigned int max_digits = 3);
1000  /// Convert double to string.
1001  ///
1002  /// @param value
1003  /// Double value to be converted.
1004  /// @param precision
1005  /// Precision value for conversion. If precision is more that maximum
1006  /// for current platform, then it will be truncated to this maximum.
1007  /// If it is negative, that double will be converted to number in
1008  /// scientific notation.
1009  /// @param flags
1010  /// How to convert value to string.
1011  /// If double format flags are not specified, that next output format
1012  /// will be used by default:
1013  /// - fDoubleFixed, if 'precision' >= 0.
1014  /// - fDoubleGeneral, if 'precision' < 0.
1015  /// @return
1016  /// - If conversion succeeds, set errno to zero and return the
1017  /// converted string value.
1018  /// - Otherwise, set errno to non-zero and return empty string.
1019  static string DoubleToString(double value, int precision = -1,
1020  TNumToStringFlags flags = 0);
1021 
1022  /// Convert double to string.
1023  ///
1024  /// @param out_str
1025  /// Output string variable
1026  /// @param value
1027  /// Double value to be converted.
1028  /// @param precision
1029  /// Precision value for conversion. If precision is more that maximum
1030  /// for current platform, then it will be truncated to this maximum.
1031  /// If it is negative, that double will be converted to number in
1032  /// scientific notation.
1033  /// @param flags
1034  /// How to convert value to string.
1035  /// If double format flags are not specified, that next output format
1036  /// will be used by default:
1037  /// - fDoubleFixed, if 'precision' >= 0.
1038  /// - fDoubleGeneral, if 'precision' < 0.
1039  /// @note
1040  /// - If conversion succeeds, set errno to zero and return the
1041  /// converted string value in 'out_str'.
1042  /// - Otherwise, set errno to non-zero, value of 'out_str' is undefined.
1043  static void DoubleToString(string& out_str, double value,
1044  int precision = -1,
1045  TNumToStringFlags flags = 0);
1046 
1047  /// Convert double to string with specified precision and place the result
1048  /// in the specified buffer.
1049  ///
1050  /// @param value
1051  /// Double value to be converted.
1052  /// @param precision
1053  /// Precision value for conversion. If precision is more that maximum
1054  /// for current platform, then it will be truncated to this maximum.
1055  /// @param buf
1056  /// Put result of the conversion into this buffer.
1057  /// @param buf_size
1058  /// Size of buffer, "buf".
1059  /// @param flags
1060  /// How to convert value to string.
1061  /// Default output format is fDoubleFixed.
1062  /// @return
1063  /// - If conversion succeeds, set errno to zero and return the
1064  /// number of bytes stored in "buf", not counting the
1065  /// terminating '\0'.
1066  /// - Otherwise, set errno to non-zero, value of 'out_str' is undefined.
1067  static SIZE_TYPE DoubleToString(double value, unsigned int precision,
1068  char* buf, SIZE_TYPE buf_size,
1069  TNumToStringFlags flags = 0);
1070 
1071  /// Convert double to string with specified precision and put the result
1072  /// into a character buffer, in scientific format.
1073  ///
1074  /// NOTE:
1075  /// The output character buffer is NOT zero-terminated.
1076  /// The decimal separator is dot, always.
1077  /// This function DOES NOT check 'value' for being finite or not-a-number;
1078  /// if it is, the result is unpredictable.
1079  /// This function is less precise for a small fraction of values
1080  /// (the difference is in the last significant digit) than its
1081  /// 'DoubleToString' siblings, but it is much faster.
1082  ///
1083  /// @param value
1084  /// Double value to be converted.
1085  /// @param precision
1086  /// Maximum number of significant digits to preserve. If precision is greater than
1087  /// maximum for the current platform, it will be truncated to this maximum.
1088  /// @param buf
1089  /// Put result of the conversion into this buffer.
1090  /// @param buf_size
1091  /// Size of buffer, "buf".
1092  /// @return
1093  /// The number of bytes written into "buf".
1094  static SIZE_TYPE DoubleToStringPosix(double value, unsigned int precision,
1095  char* buf, SIZE_TYPE buf_size);
1096 
1097 
1098  /// Convert double to string with specified precision.
1099  ///
1100  /// The result consists of three parts: significant digits, exponent and sign.
1101  /// For example, input value -12345.67 will produce
1102  /// buf = "1234567", *dec = 4, and *sign = -1.
1103  /// NOTE:
1104  /// The output character buffer is NOT zero-terminated.
1105  /// The buffer is NOT padded with zeros.
1106  /// This function DOES NOT check 'value' for being finite or not-a-number;
1107  /// if it is, the result is unpredictable.
1108  /// This function is less precise for a small fraction of values
1109  /// (the difference is in the last significant digit) than its
1110  /// 'DoubleToString' siblings, but it is much faster.
1111  ///
1112  /// @param value
1113  /// Double value to be converted.
1114  /// @param precision
1115  /// Maximum number of significant digits to preserve. If precision is greater than
1116  /// maximum for the current platform, it will be truncated to this maximum.
1117  /// @param buf
1118  /// Put result of the conversion into this buffer.
1119  /// @param buf_size
1120  /// Size of buffer, "buf".
1121  /// @param dec
1122  /// Exponent
1123  /// @param sign
1124  /// Sign of the value
1125  /// @return
1126  /// The number of bytes written into "buf".
1127  static SIZE_TYPE DoubleToString_Ecvt(double value, unsigned int precision,
1128  char* buf, SIZE_TYPE buf_size,
1129  int* dec, int* sign);
1130 
1131  /// Convert size_t to string.
1132  ///
1133  /// @param value
1134  /// Value to be converted.
1135  /// @param flags
1136  /// How to convert value to string.
1137  /// @param base
1138  /// Radix base. Default is 10. Allowed values are 2..36.
1139  /// Bases 8 and 16 do not add leading '0' and '0x' accordingly.
1140  /// If necessary you should add it yourself.
1141  /// @return
1142  /// - If conversion succeeds, set errno to zero and return the
1143  /// converted string value.
1144  /// - Otherwise, set errno to non-zero and return empty string.
1145  static string SizetToString(size_t value,
1146  TNumToStringFlags flags = 0,
1147  int base = 10);
1148 
1149  /// Convert pointer to string.
1150  ///
1151  /// @param out_str
1152  /// Output string variable.
1153  /// @param str
1154  /// Pointer to be converted.
1155  /// @note
1156  /// - If conversion succeeds, set errno to zero and return the
1157  /// converted string value in 'out_str'.
1158  /// - Otherwise, set errno to non-zero, value of 'out_str' is undefined.
1159  static void PtrToString(string& out_str, const void* ptr);
1160 
1161  /// Convert pointer to string.
1162  ///
1163  /// @param str
1164  /// Pointer to be converted.
1165  /// @return
1166  /// - If conversion succeeds, set errno to zero and return the
1167  /// converted string value representing the pointer.
1168  /// - Otherwise, set errno to non-zero and return empty string.
1169  static string PtrToString(const void* ptr);
1170 
1171  /// Convert bool to string.
1172  ///
1173  /// @param value
1174  /// Boolean value to be converted.
1175  /// @return
1176  /// One of: 'true, 'false'
1177  /// @note
1178  /// Don't change errno.
1179  static const string BoolToString(bool value);
1180 
1181  /// Convert string to bool.
1182  ///
1183  /// @param str
1184  /// Boolean string value to be converted. Can recognize
1185  /// case-insensitive version as one of:
1186  /// TRUE - 'true, 't', 'yes', 'y', '1', 'on';
1187  /// FALSE - 'false', 'f', 'no', 'n', '0', 'off'.
1188  /// @return
1189  /// - If conversion succeeds, set errno to zero and return TRUE or FALSE.
1190  /// - Otherwise, set errno to non-zero and throw an exception.
1191  static bool StringToBool(const CTempString str);
1192 
1193 
1194  /// Handle an arbitrary printf-style format string.
1195  ///
1196  /// This method exists only to support third-party code that insists on
1197  /// representing messages in this format; please stick to type-checked
1198  /// means of formatting such as the above ToString methods and I/O
1199  /// streams whenever possible.
1200  static string FormatVarargs(const char* format, va_list args);
1201 
1202 
1203  /// Which type of string comparison.
1204  enum ECase {
1205  eCase, ///< Case sensitive compare
1206  eNocase ///< Case insensitive compare
1207  };
1208 
1209  // ATTENTION. Be aware that:
1210  //
1211  // 1) "Compare***(..., SIZE_TYPE pos, SIZE_TYPE n, ...)" functions
1212  // follow the ANSI C++ comparison rules a la "basic_string::compare()":
1213  // s1[pos:pos+n) == s2 --> return 0
1214  // s1[pos:pos+n) < s2 --> return negative value
1215  // s1[pos:pos+n) > s2 --> return positive value
1216  //
1217  // 2) "strn[case]cmp()" functions follow the ANSI C comparison rules:
1218  // s1[0:n) == s2[0:n) --> return 0
1219  // s1[0:n) < s2[0:n) --> return negative value
1220  // s1[0:n) > s2[0:n) --> return positive value
1221 
1222 
1223  /// Case-sensitive compare of a substring with another string.
1224  ///
1225  /// @param s1
1226  /// String containing the substring to be compared.
1227  /// @param pos
1228  /// Start position of substring to be compared.
1229  /// @param n
1230  /// Number of characters in substring to be compared.
1231  /// @param s2
1232  /// String (char*) to be compared with substring.
1233  /// @return
1234  /// - 0, if s1[pos:pos+n) == s2;
1235  /// - Negative integer, if s1[pos:pos+n) < s2;
1236  /// - Positive integer, if s1[pos:pos+n) > s2.
1237  /// @sa
1238  /// Other forms of overloaded CompareCase() with differences in argument
1239  /// types: char* vs. CTempString[Ex]
1240  static int CompareCase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n,
1241  const char* s2);
1242 
1243  /// Case-sensitive compare of a substring with another string.
1244  ///
1245  /// @param s1
1246  /// String containing the substring to be compared.
1247  /// @param pos
1248  /// Start position of substring to be compared.
1249  /// @param n
1250  /// Number of characters in substring to be compared.
1251  /// @param s2
1252  /// String to be compared with substring.
1253  /// @return
1254  /// - 0, if s1[pos:pos+n) == s2;
1255  /// - Negative integer, if s1[pos:pos+n) < s2;
1256  /// - Positive integer, if s1[pos:pos+n) > s2.
1257  /// @sa
1258  /// Other forms of overloaded CompareCase() with differences in argument
1259  /// types: char* vs. CTempString[Ex]
1260  static int CompareCase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n,
1261  const CTempString s2);
1262 
1263  /// Case-sensitive compare of two strings -- char* version.
1264  ///
1265  /// @param s1
1266  /// String to be compared -- operand 1.
1267  /// @param s2
1268  /// String to be compared -- operand 2.
1269  /// @return
1270  /// - 0, if s1 == s2;
1271  /// - Negative integer, if s1 < s2;
1272  /// - Positive integer, if s1 > s2.
1273  /// @sa
1274  /// CompareNocase(), Compare() versions with same argument types.
1275  static int CompareCase(const char* s1, const char* s2);
1276 
1277  /// Case-sensitive compare of two strings -- CTempStringEx version.
1278  ///
1279  /// @param s1
1280  /// String to be compared -- operand 1.
1281  /// @param s2
1282  /// String to be compared -- operand 2.
1283  /// @return
1284  /// - 0, if s1 == s2;
1285  /// - Negative integer, if s1 < s2;
1286  /// - Positive integer, if s1 > s2.
1287  /// @sa
1288  /// CompareNocase(), Compare() versions with same argument types.
1289  static int CompareCase(const CTempStringEx s1, const CTempStringEx s2);
1290 
1291  /// Case-insensitive compare of a substring with another string.
1292  ///
1293  /// @param s1
1294  /// String containing the substring to be compared.
1295  /// @param pos
1296  /// Start position of substring to be compared.
1297  /// @param n
1298  /// Number of characters in substring to be compared.
1299  /// @param s2
1300  /// String (char*) to be compared with substring.
1301  /// @return
1302  /// - 0, if s1[pos:pos+n) == s2 (case-insensitive compare);
1303  /// - Negative integer, if s1[pos:pos+n) < s2 (case-insensitive compare);
1304  /// - Positive integer, if s1[pos:pos+n) > s2 (case-insensitive compare).
1305  /// @sa
1306  /// Other forms of overloaded CompareNocase() with differences in
1307  /// argument types: char* vs. CTempString[Ex]
1308  static int CompareNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n,
1309  const char* s2);
1310 
1311  /// Case-insensitive compare of a substring with another string.
1312  ///
1313  /// @param s1
1314  /// String containing the substring to be compared.
1315  /// @param pos
1316  /// Start position of substring to be compared.
1317  /// @param n
1318  /// Number of characters in substring to be compared.
1319  /// @param s2
1320  /// String to be compared with substring.
1321  /// @return
1322  /// - 0, if s1[pos:pos+n) == s2 (case-insensitive compare);
1323  /// - Negative integer, if s1[pos:pos+n) < s2 (case-insensitive compare);
1324  /// - Positive integer, if s1[pos:pos+n) > s2 (case-insensitive compare).
1325  /// @sa
1326  /// Other forms of overloaded CompareNocase() with differences in
1327  /// argument types: char* vs. CTempString[Ex]
1328  static int CompareNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n,
1329  const CTempString s2);
1330 
1331  /// Case-insensitive compare of two strings -- char* version.
1332  ///
1333  /// @param s1
1334  /// String to be compared -- operand 1.
1335  /// @param s2
1336  /// String to be compared -- operand 2.
1337  /// @return
1338  /// - 0, if s1 == s2 (case-insensitive compare);
1339  /// - Negative integer, if s1 < s2 (case-insensitive compare);
1340  /// - Positive integer, if s1 > s2 (case-insensitive compare).
1341  /// @sa
1342  /// CompareCase(), Compare() versions with same argument types.
1343  static int CompareNocase(const char* s1, const char* s2);
1344 
1345  /// Case-insensitive compare of two strings -- CTempStringEx version.
1346  ///
1347  /// @param s1
1348  /// String to be compared -- operand 1.
1349  /// @param s2
1350  /// String to be compared -- operand 2.
1351  /// @return
1352  /// - 0, if s1 == s2 (case-insensitive compare);
1353  /// - Negative integer, if s1 < s2 (case-insensitive compare);
1354  /// - Positive integer, if s1 > s2 (case-insensitive compare).
1355  /// @sa
1356  /// CompareCase(), Compare() versions with same argument types.
1357  static int CompareNocase(const CTempStringEx s1, const CTempStringEx s2);
1358 
1359  /// Compare of a substring with another string.
1360  ///
1361  /// @param s1
1362  /// String containing the substring to be compared.
1363  /// @param pos
1364  /// Start position of substring to be compared.
1365  /// @param n
1366  /// Number of characters in substring to be compared.
1367  /// @param s2
1368  /// String (char*) to be compared with substring.
1369  /// @param use_case
1370  /// Whether to do a case sensitive compare(eCase -- default), or a
1371  /// case-insensitive compare (eNocase).
1372  /// @return
1373  /// - 0, if s1[pos:pos+n) == s2;
1374  /// - Negative integer, if s1[pos:pos+n) < s2;
1375  /// - Positive integer, if s1[pos:pos+n) > s2.
1376  /// @sa
1377  /// Other forms of overloaded Compare() with differences in argument
1378  /// types: char* vs. CTempString[Ex]
1379  static int Compare(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n,
1380  const char* s2, ECase use_case = eCase);
1381 
1382  /// Compare of a substring with another string.
1383  ///
1384  /// @param s1
1385  /// String containing the substring to be compared.
1386  /// @param pos
1387  /// Start position of substring to be compared.
1388  /// @param n
1389  /// Number of characters in substring to be compared.
1390  /// @param s2
1391  /// String to be compared with substring.
1392  /// @param use_case
1393  /// Whether to do a case sensitive compare(default is eCase), or a
1394  /// case-insensitive compare (eNocase).
1395  /// @return
1396  /// - 0, if s1[pos:pos+n) == s2;
1397  /// - Negative integer, if s1pos:pos+n) < s2;
1398  /// - Positive integer, if s1[pos:pos+n) > s2.
1399  /// @sa
1400  /// Other forms of overloaded Compare() with differences in argument
1401  /// types: char* vs. CTempString[Ex]
1402  static int Compare(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n,
1403  const CTempString s2, ECase use_case = eCase);
1404 
1405  /// Compare two strings -- char* version.
1406  ///
1407  /// @param s1
1408  /// String to be compared -- operand 1.
1409  /// @param s2
1410  /// String to be compared -- operand 2.
1411  /// @param use_case
1412  /// Whether to do a case sensitive compare(default is eCase), or a
1413  /// case-insensitive compare (eNocase).
1414  /// @return
1415  /// - 0, if s1 == s2.
1416  /// - Negative integer, if s1 < s2.
1417  /// - Positive integer, if s1 > s2.
1418  /// @sa
1419  /// Other forms of overloaded Compare() with differences in argument
1420  /// types: char* vs. CTempString[Ex]
1421  static int Compare(const char* s1, const char* s2,
1422  ECase use_case = eCase);
1423 
1424  /// Compare two strings -- CTempStringEx version.
1425  ///
1426  /// @param s1
1427  /// String to be compared -- operand 1.
1428  /// @param s2
1429  /// String to be compared -- operand 2.
1430  /// @param use_case
1431  /// Whether to do a case sensitive compare(default is eCase), or a
1432  /// case-insensitive compare (eNocase).
1433  /// @return
1434  /// - 0, if s1 == s2;
1435  /// - Negative integer, if s1 < s2;
1436  /// - Positive integer, if s1 > s2.
1437  /// @sa
1438  /// Other forms of overloaded Compare() with differences in argument
1439  /// types: char* vs. CTempString[Ex]
1440  static int Compare(const CTempStringEx s1, const CTempStringEx s2,
1441  ECase use_case = eCase);
1442 
1443  /// Case-sensitive equality of a substring with another string.
1444  ///
1445  /// @param s1
1446  /// String containing the substring to be compared.
1447  /// @param pos
1448  /// Start position of substring to be compared.
1449  /// @param n
1450  /// Number of characters in substring to be compared.
1451  /// @param s2
1452  /// String (char*) to be compared with substring.
1453  /// @return
1454  /// - true, if s1[pos:pos+n) equals s2;
1455  /// - false, otherwise
1456  /// @sa
1457  /// Other forms of overloaded EqualCase() with differences in argument
1458  /// types: char* vs. CTempString[Ex]
1459  static bool EqualCase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n,
1460  const char* s2);
1461 
1462  /// Case-sensitive equality of a substring with another string.
1463  ///
1464  /// @param s1
1465  /// String containing the substring to be compared.
1466  /// @param pos
1467  /// Start position of substring to be compared.
1468  /// @param n
1469  /// Number of characters in substring to be compared.
1470  /// @param s2
1471  /// String to be compared with substring.
1472  /// @return
1473  /// - true, if s1[pos:pos+n) equals s2;
1474  /// - false, otherwise
1475  /// @sa
1476  /// Other forms of overloaded EqualCase() with differences in argument
1477  /// types: char* vs. CTempString[Ex]
1478  static bool EqualCase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n,
1479  const CTempString s2);
1480 
1481  /// Case-sensitive equality of two strings -- char* version.
1482  ///
1483  /// @param s1
1484  /// String to be compared -- operand 1.
1485  /// @param s2
1486  /// String to be compared -- operand 2.
1487  /// @return
1488  /// - true, if s1 equals s2
1489  /// - false, otherwise
1490  /// @sa
1491  /// EqualCase(), Equal() versions with same argument types.
1492  static bool EqualCase(const char* s1, const char* s2);
1493 
1494  /// Case-sensitive equality of two strings.
1495  ///
1496  /// @param s1
1497  /// String to be compared -- operand 1.
1498  /// @param s2
1499  /// String to be compared -- operand 2.
1500  /// @return
1501  /// - true, if s1 equals s2
1502  /// - false, otherwise
1503  /// @sa
1504  /// EqualCase(), Equal() versions with same argument types.
1505  static bool EqualCase(const CTempStringEx s1, const CTempStringEx s2);
1506 
1507  /// Case-insensitive equality of a substring with another string.
1508  ///
1509  /// @param s1
1510  /// String containing the substring to be compared.
1511  /// @param pos
1512  /// Start position of substring to be compared.
1513  /// @param n
1514  /// Number of characters in substring to be compared.
1515  /// @param s2
1516  /// String (char*) to be compared with substring.
1517  /// @return
1518  /// - true, if s1[pos:pos+n) equals s2 (case-insensitive compare);
1519  /// - false, otherwise.
1520  /// @sa
1521  /// Other forms of overloaded EqualNocase() with differences in
1522  /// argument types: char* vs. CTempString[Ex]
1523  static bool EqualNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n,
1524  const char* s2);
1525 
1526  /// Case-insensitive equality of a substring with another string.
1527  ///
1528  /// @param s1
1529  /// String containing the substring to be compared.
1530  /// @param pos
1531  /// Start position of substring to be compared.
1532  /// @param n
1533  /// Number of characters in substring to be compared.
1534  /// @param s2
1535  /// String to be compared with substring.
1536  /// @return
1537  /// - true, if s1[pos:pos+n) equals s2 (case-insensitive compare);
1538  /// - false, otherwise.
1539  /// @sa
1540  /// Other forms of overloaded EqualNocase() with differences in
1541  /// argument types: char* vs. CTempString[Ex]
1542  static bool EqualNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n,
1543  const CTempString s2);
1544 
1545  /// Case-insensitive equality of two strings -- char* version.
1546  ///
1547  /// @param s1
1548  /// String to be compared -- operand 1.
1549  /// @param s2
1550  /// String to be compared -- operand 2.
1551  /// @return
1552  /// - true, if s1 equals s2 (case-insensitive compare);
1553  /// - false, otherwise.
1554  /// @sa
1555  /// EqualCase(), Equal() versions with same argument types.
1556  static bool EqualNocase(const char* s1, const char* s2);
1557 
1558  /// Case-insensitive equality of two strings.
1559  ///
1560  /// @param s1
1561  /// String to be compared -- operand 1.
1562  /// @param s2
1563  /// String to be compared -- operand 2.
1564  /// @return
1565  /// - true, if s1 equals s2 (case-insensitive compare);
1566  /// - false, otherwise.
1567  /// @sa
1568  /// EqualCase(), Equal() versions with same argument types.
1569  static bool EqualNocase(const CTempStringEx s1, const CTempStringEx s2);
1570 
1571  /// Test for equality of a substring with another string.
1572  ///
1573  /// @param s1
1574  /// String containing the substring to be compared.
1575  /// @param pos
1576  /// Start position of substring to be compared.
1577  /// @param n
1578  /// Number of characters in substring to be compared.
1579  /// @param s2
1580  /// String (char*) to be compared with substring.
1581  /// @param use_case
1582  /// Whether to do a case sensitive compare(eCase -- default), or a
1583  /// case-insensitive compare (eNocase).
1584  /// @return
1585  /// - true, if s1[pos:pos+n) equals s2;
1586  /// - false, otherwise.
1587  /// @sa
1588  /// Other forms of overloaded Equal() with differences in argument
1589  /// types: char* vs. CTempString[Ex]
1590  static bool Equal(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n,
1591  const char* s2, ECase use_case = eCase);
1592 
1593  /// Test for equality of a substring with another string.
1594  ///
1595  /// @param s1
1596  /// String containing the substring to be compared.
1597  /// @param pos
1598  /// Start position of substring to be compared.
1599  /// @param n
1600  /// Number of characters in substring to be compared.
1601  /// @param s2
1602  /// String to be compared with substring.
1603  /// @param use_case
1604  /// Whether to do a case sensitive compare (default is eCase), or a
1605  /// case-insensitive compare (eNocase).
1606  /// @return
1607  /// - 0, if s1[pos:pos+n) == s2;
1608  /// - Negative integer, if s1[pos:pos+n) < s2;
1609  /// - Positive integer, if s1[pos:pos+n) > s2.
1610  /// @sa
1611  /// Other forms of overloaded Equal() with differences in argument
1612  /// types: char* vs. CTempString[Ex]
1613  static bool Equal(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n,
1614  const CTempString s2, ECase use_case = eCase);
1615 
1616  /// Test for equality of two strings -- char* version.
1617  ///
1618  /// @param s1
1619  /// String to be compared -- operand 1.
1620  /// @param s2
1621  /// String to be compared -- operand 2.
1622  /// @param use_case
1623  /// Whether to do a case sensitive compare (default is eCase), or a
1624  /// case-insensitive compare (eNocase).
1625  /// @return
1626  /// - 0, if s1 == s2;
1627  /// - Negative integer, if s1 < s2;
1628  /// - Positive integer, if s1 > s2.
1629  /// @sa
1630  /// EqualNocase(), Equal() versions with similar argument types.
1631  static bool Equal(const char* s1, const char* s2,
1632  ECase use_case = eCase);
1633 
1634  /// Test for equality of two strings.
1635  ///
1636  /// @param s1
1637  /// String to be compared -- operand 1.
1638  /// @param s2
1639  /// String to be compared -- operand 2.
1640  /// @param use_case
1641  /// Whether to do a case sensitive compare (default is eCase), or a
1642  /// case-insensitive compare (eNocase).
1643  /// @return
1644  /// - true, if s1 equals s2;
1645  /// - false, otherwise.
1646  /// @sa
1647  /// EqualNocase(), Equal() versions with similar argument types.
1648  static bool Equal(const CTempStringEx s1, const CTempStringEx s2,
1649  ECase use_case = eCase);
1650 
1651  // NOTE. On some platforms, "strn[case]cmp()" can work faster than their
1652  // "Compare***()" counterparts.
1653 
1654  /// String compare.
1655  ///
1656  /// @param s1
1657  /// String to be compared -- operand 1.
1658  /// @param s2
1659  /// String to be compared -- operand 2.
1660  /// @return
1661  /// - 0, if s1 == s2;
1662  /// - Negative integer, if s1 < s2;
1663  /// - Positive integer, if s1 > s2.
1664  /// @sa
1665  /// strncmp(), strcasecmp(), strncasecmp()
1666  static int strcmp(const char* s1, const char* s2);
1667 
1668  /// String compare up to specified number of characters.
1669  ///
1670  /// @param s1
1671  /// String to be compared -- operand 1.
1672  /// @param s2
1673  /// String to be compared -- operand 2.
1674  /// @param n
1675  /// Number of characters in string
1676  /// @return
1677  /// - 0, if s1 == s2;
1678  /// - Negative integer, if s1 < s2;
1679  /// - Positive integer, if s1 > s2.
1680  /// @sa
1681  /// strcmp(), strcasecmp(), strncasecmp()
1682  static int strncmp(const char* s1, const char* s2, size_t n);
1683 
1684  /// Case-insensitive comparison of two zero-terminated strings.
1685  ///
1686  /// @param s1
1687  /// String to be compared -- operand 1.
1688  /// @param s2
1689  /// String to be compared -- operand 2.
1690  /// @return
1691  /// - 0, if s1 == s2;
1692  /// - Negative integer, if s1 < s2;
1693  /// - Positive integer, if s1 > s2.
1694  /// @sa
1695  /// strcmp(), strncmp(), strncasecmp()
1696  static int strcasecmp(const char* s1, const char* s2);
1697 
1698  /// Case-insensitive comparison of two zero-terminated strings,
1699  /// narrowed to the specified number of characters.
1700  ///
1701  /// @param s1
1702  /// String to be compared -- operand 1.
1703  /// @param s2
1704  /// String to be compared -- operand 2.
1705  /// @return
1706  /// - 0, if s1 == s2;
1707  /// - Negative integer, if s1 < s2.
1708  /// - Positive integer, if s1 > s2.
1709  /// @sa
1710  /// strcmp(), strcasecmp(), strcasecmp()
1711  static int strncasecmp(const char* s1, const char* s2, size_t n);
1712 
1713  /// Wrapper for the function strftime() that corrects handling %D and %T
1714  /// time formats on MS Windows.
1715  static size_t strftime(char* s, size_t maxsize, const char* format,
1716  const struct tm* timeptr);
1717 
1718  /// Match "str" against the "mask".
1719  ///
1720  /// This function does not use regular expressions.
1721  /// Very similar to fnmatch(3), but there are differences (see also glob(7)).
1722  /// There's no special treatment for a slash character '/' in this call.
1723  ///
1724  /// @param str
1725  /// String to match.
1726  /// @param mask
1727  /// Mask used to match string "str".
1728  /// This is a text pattern, which, along ordinary characters that must match
1729  /// literally corresponding symbols in the string "str", can contains also
1730  /// mext wildcard characters: \n
1731  /// ? - matches to any single character in the string. \n
1732  /// * - matches to any number of characters in the string (including none). \n
1733  ///
1734  /// Mask also support POSIX character classes in the forms of "[...]" or "[!...]"
1735  /// that must MATCH or NOT MATCH, respectively, a single character in "str".
1736  /// To cancel the special meaning of '*', '?' or '[', they can be prepended with
1737  /// a backslash '\\' (the backslash in front of other characters does not change
1738  /// their meaning, so "\\\\" matches one graphical backslash in the "str").
1739  /// Within a character class, to have its literal meaning a closing square bracket ']'
1740  /// must be used at the first position, whereas '?', '*', '[, and '\\' stand
1741  /// just for themselves. Two characters separated by a minus sign '-' denote
1742  /// a range that can be used for contraction to include all characters in
1743  /// between: "[A-F]" is equivalent to "[ABCDEF]".
1744  /// For its literal meaning, the minus sign '-' can be used either at the very
1745  /// first position, or the last position before the closing bracket ']'.
1746  /// To have a range that begins with an exclamation point, one has to use
1747  /// a dummy empty range followed by that range with '!'.
1748  ///
1749  /// Examples:
1750  /// "!" matches a single '!' (note that just "[!]" is invalid);
1751  /// "[!!]" matches any character, which is not an exclamation point '!';
1752  /// "[][!]" matches ']', '[', and '!';
1753  /// "[!][-]" matches any character except for ']', '[', and '-';
1754  /// "[-]" matches a minus sign '-' (same as '-' just by itself);
1755  /// "[?*\\]" matches either '?', or '*', or a backslash '\\';
1756  /// "[]-\\]" matches nothing as it defines an empty range (from ']' to '\\');
1757  /// "\\[a]\\*" matches a literal substring "[a]*";
1758  /// "[![a-]" matches any char but '[', 'a' or '-' (same as "[!-[a]"; but not
1759  /// "[![-a]", which defines an empty range, thus matches any char!);
1760  /// "[]A]" matches either ']' or 'A' (NB: "[A]]" matches a substring "A]");
1761  /// "[0-9-]" matches any decimal digit or a minus sign '-' (same: "[-0-9]");
1762  /// "[9-0!-$]" matches '!', '"', '#', and '$' (as first range matches nothing).
1763  ///
1764  /// @note
1765  /// In the above, each double backslash denotes a single graphical backslash
1766  /// character (C string notation is used).
1767  /// @note
1768  /// Unlike shell globbing, "[--0]" *does* match the slash character '/'
1769  /// (along with '-', '.', and '0' that all fall within the range).
1770  /// @param use_case
1771  /// Whether to do a case sensitive compare for letters (eCase -- default),
1772  /// or a case-insensitive compare (eNocase).
1773  /// @return
1774  /// Return TRUE if "str" matches "mask", and FALSE otherwise
1775  /// (including patter errors).
1776  /// @sa
1777  /// CRegexp, CRegexpUtil
1778  ///
1779  static bool MatchesMask(CTempString str, CTempString mask, ECase use_case = eCase);
1780 
1781  /// Check if a string is blank (has no text).
1782  ///
1783  /// @param str
1784  /// String to check.
1785  /// @param pos
1786  /// starting position (default 0)
1787  static bool IsBlank(const CTempString str, SIZE_TYPE pos = 0);
1788 
1789  /// Checks if all letters in the given string have a lower case.
1790  ///
1791  /// @param str
1792  /// String to be checked.
1793  /// @return
1794  /// TRUE if all letter characters in the string are lowercase
1795  /// according to the current C locale (std::islower()).
1796  /// All non-letter characters will be ignored.
1797  /// TRUE if empty or no letters.
1798  static bool IsLower(const CTempString str);
1799 
1800  /// Checks if all letters in the given string have a upper case.
1801  ///
1802  /// @param str
1803  /// String to be checked.
1804  /// @return
1805  /// TRUE if all letter characters in the string are uppercase
1806  /// according to the current C locale (std::isupper()).
1807  /// All non-letter characters will be skipped.
1808  /// TRUE if empty or no letters.
1809  static bool IsUpper(const CTempString str);
1810 
1811 
1812  // The following 4 methods change the passed string, then return it
1813 
1814  /// Convert string to lower case -- string& version.
1815  ///
1816  /// @param str
1817  /// String to be converted.
1818  /// @return
1819  /// Lower cased string.
1820  static string& ToLower(string& str);
1821 
1822  /// Convert string to lower case -- char* version.
1823  ///
1824  /// @param str
1825  /// String to be converted.
1826  /// @return
1827  /// Lower cased string.
1828  static char* ToLower(char* str);
1829 
1830  /// Convert string to upper case -- string& version.
1831  ///
1832  /// @param str
1833  /// String to be converted.
1834  /// @return
1835  /// Upper cased string.
1836  static string& ToUpper(string& str);
1837 
1838  /// Convert string to upper case -- char* version.
1839  ///
1840  /// @param str
1841  /// String to be converted.
1842  /// @return
1843  /// Upper cased string.
1844  static char* ToUpper(char* str);
1845 
1846 private:
1847  /// Privatized ToLower() with const char* parameter to prevent passing of
1848  /// constant strings.
1849  static void/*dummy*/ ToLower(const char* /*dummy*/);
1850 
1851  /// Privatized ToUpper() with const char* parameter to prevent passing of
1852  /// constant strings.
1853  static void/*dummy*/ ToUpper(const char* /*dummy*/);
1854 
1855 public:
1856 
1857  /// Check if a string starts with a specified prefix value.
1858  ///
1859  /// @param str
1860  /// String to check.
1861  /// @param start
1862  /// Prefix value to check for.
1863  /// @param use_case
1864  /// Whether to do a case sensitive compare(default is eCase), or a
1865  /// case-insensitive compare (eNocase) while checking.
1866  static bool StartsWith(const CTempString str, const CTempString start,
1867  ECase use_case = eCase);
1868 
1869  /// Check if a string starts with a specified character value.
1870  ///
1871  /// @param str
1872  /// String to check.
1873  /// @param start
1874  /// Character value to check for.
1875  /// @param use_case
1876  /// Whether to do a case sensitive compare(default is eCase), or a
1877  /// case-insensitive compare (eNocase) while checking.
1878  static bool StartsWith(const CTempString str, char start,
1879  ECase use_case = eCase);
1880 
1881  /// Check if a string ends with a specified suffix value.
1882  ///
1883  /// @param str
1884  /// String to check.
1885  /// @param end
1886  /// Suffix value to check for.
1887  /// @param use_case
1888  /// Whether to do a case sensitive compare(default is eCase), or a
1889  /// case-insensitive compare (eNocase) while checking.
1890  static bool EndsWith(const CTempString str, const CTempString end,
1891  ECase use_case = eCase);
1892 
1893  /// Check if a string ends with a specified character value.
1894  ///
1895  /// @param str
1896  /// String to check.
1897  /// @param end
1898  /// Character value to check for.
1899  /// @param use_case
1900  /// Whether to do a case sensitive compare(default is eCase), or a
1901  /// case-insensitive compare (eNocase) while checking.
1902  static bool EndsWith(const CTempString str, char end,
1903  ECase use_case = eCase);
1904 
1905  /// Determine the common prefix of two strings.
1906  ///
1907  /// @param s1
1908  /// String to be compared -- operand 1.
1909  /// @param s2
1910  /// String to be compared -- operand 2.
1911  /// @return
1912  /// The number of characters common to the start of each string.
1913  static SIZE_TYPE CommonPrefixSize(const CTempString s1, const CTempString s2);
1914 
1915  /// Determine the common suffix of two strings.
1916  ///
1917  /// @param s1
1918  /// String to be compared -- operand 1.
1919  /// @param s2
1920  /// String to be compared -- operand 2.
1921  /// @return
1922  /// The number of characters common to the end of each string.
1923  static SIZE_TYPE CommonSuffixSize(const CTempString s1, const CTempString s2);
1924 
1925  /// Determine if the suffix of one string is the prefix of another.
1926  ///
1927  /// @param s1
1928  /// String to be compared -- operand 1.
1929  /// @param s2
1930  /// String to be compared -- operand 2.
1931  /// @return
1932  /// The number of characters common to the end of the first string
1933  /// and the start of the second string.
1934  static SIZE_TYPE CommonOverlapSize(const CTempString s1, const CTempString s2);
1935 
1936 
1937  /// Whether it is the first or last occurrence.
1938  /// @deprecated
1940  eFirst, ///< First occurrence
1941  eLast ///< Last occurrence
1942  };
1943 
1944  /// Search direction for Find() methods.
1945  enum EDirection {
1946  eForwardSearch = 0, ///< Search in a forward direction
1947  eReverseSearch ///< Search in a backward direction
1948  };
1949 
1950 
1951  /// Find the pattern in the string.
1952  ///
1953  /// @param str
1954  /// String to search.
1955  /// @param pattern
1956  /// Pattern to search for in "str".
1957  /// @param use_case
1958  /// Whether to do a case sensitive compare (default is eCase), or a
1959  /// case-insensitive compare (eNocase) while searching for the pattern.
1960  /// @param direction
1961  /// Define a search direction of the requested "occurrence"
1962  /// of "pattern" in "str".
1963  /// @param occurrence
1964  /// Which occurrence of the pattern in the string to use (zero-based).
1965  /// NOTE: When an occurrence is found the next occurrence will be
1966  /// searched for starting right *after* the found pattern.
1967  /// @return
1968  /// Start of the found pattern in the string.
1969  /// Or NPOS if there is no occurrence of the pattern in the string.
1970  static SIZE_TYPE Find(const CTempString str,
1971  const CTempString pattern,
1972  ECase use_case = eCase,
1973  EDirection direction = eForwardSearch,
1974  SIZE_TYPE occurrence = 0);
1975 
1976  /// Find the pattern in the specified range of a string.
1977  ///
1978  /// @param str
1979  /// String to search.
1980  /// @param pattern
1981  /// Pattern to search for in "str".
1982  /// @param start
1983  /// Position in "str" to start search from.
1984  /// 0 means start the search from the beginning of the string.
1985  /// @param end
1986  /// Position in "str" to perform search up to.
1987  /// NPOS means to search to the end of the string.
1988  /// @param which
1989  /// When set to eFirst, this means to find the first occurrence of
1990  /// "pattern" in "str". When set to eLast, this means to find the last
1991  /// occurrence of "pattern" in "str".
1992  /// @param use_case
1993  /// Whether to do a case sensitive compare (default is eCase), or a
1994  /// case-insensitive compare (eNocase) while searching for the pattern.
1995  /// @return
1996  /// - The start of the first or last (depending on "which" parameter)
1997  /// occurrence of "pattern" in "str", within the string interval
1998  /// ["start", "end"], or
1999  /// - NPOS if there is no occurrence of the pattern.
2000  /// @sa FindCase, FindNoCase, FindWord
2001  ///
2002  /// @deprecated
2003  /// Use
2004  /// @code
2005  /// Find(str, pattern, [use_case], [direction], [occurrence])
2006  /// @endcode
2007  /// method instead.
2008  /// For example:
2009  /// @code
2010  /// Find(str, pattern, 0, NPOS, eLast, eCase)
2011  /// @endcode
2012  /// can be replaced by
2013  /// @code
2014  /// Find(str, pattern, eCase, eReverseSearch, /* 0 */)
2015  /// @endcode
2016  /// If you doing a search on a substring of the 'str' and ["start", "end"] search
2017  /// interval is not a default [0, NPOS], that mean a whole 'str' string, you may
2018  /// need to pass a substring instead of 'str', like
2019  /// @code
2020  /// Find(CTempString(str, start, len), pattern, ....)
2021  /// @endcode
2022  /// and after checking search result on NPOS, adjust it by 'start' yourself.
2024  static SIZE_TYPE Find(const CTempString str,
2025  const CTempString pattern,
2026  SIZE_TYPE start, SIZE_TYPE end,
2027  EOccurrence which = eFirst,
2028  ECase use_case = eCase);
2029 
2030  /// Wrapper for backward-compatibility
2031  inline
2032  static SIZE_TYPE Find(const CTempString str, const CTempString pattern, SIZE_TYPE start)
2033  { return FindCase(str, pattern, start); }
2034 
2035 
2036  /// Find the pattern in the specified range of a string using a case
2037  /// sensitive search.
2038  ///
2039  /// @param str
2040  /// String to search.
2041  /// @param pattern
2042  /// Pattern to search for in "str".
2043  /// @param start
2044  /// Position in "str" to start search from -- default of 0 means start
2045  /// the search from the beginning of the string.
2046  /// @param end
2047  /// Position in "str" to perform search up to -- default of NPOS means
2048  /// to search to the end of the string.
2049  /// @param which
2050  /// When set to eFirst, this means to find the first occurrence of
2051  /// "pattern" in "str". When set to eLast, this means to find the last
2052  /// occurrence of "pattern" in "str".
2053  /// @return
2054  /// - The start of the first or last (depending on "which" parameter)
2055  /// occurrence of "pattern" in "str", within the string interval
2056  /// ["start", "end"], or
2057  /// - NPOS if there is no occurrence of the pattern.
2058  /// @sa Find
2059  ///
2060  /// @deprecated
2061  /// Use Find() method without [start:end] range.
2062  /// @deprecated
2063  /// Use one of the next methods instead:
2064  /// @code
2065  /// Find(str, pattern, [use_case], [direction], [occurrence])
2066  /// FindCase(str, pattern, [start])
2067  /// @endcode
2068  /// For example:
2069  /// @code
2070  /// FindCase(str, pattern, 0, NPOS, eLast)
2071  /// @endcode
2072  /// can be replaced by
2073  /// @code
2074  /// Find(str, pattern, eCase, eReverseSearch, /* 0 */)
2075  /// @endcode
2076  /// For simpler cases without range, or with default [0, NPOS] please use
2077  /// @code
2078  /// FindCase(str, pattern, [start])
2079  /// @endcode
2080  /// But if you doing a search on a substring of the 'str' and ["start", "end"] search
2081  /// interval is not a default [0, NPOS], that mean a whole 'str' string, you may
2082  /// need to pass a substring instead of 'str', like
2083  /// @code
2084  /// FindCase(CTempString(str, start, len), pattern, ....)
2085  /// @endcode
2086  /// and after checking search result on NPOS, adjust it by 'start' yourself.
2088  static SIZE_TYPE FindCase(const CTempString str,
2089  const CTempString pattern,
2090  SIZE_TYPE start, SIZE_TYPE end,
2091  EOccurrence which = eFirst);
2092 
2093  /// Wrappers for backward-compatibility
2094  static SIZE_TYPE FindCase(const CTempString str, const CTempString pattern);
2095  static SIZE_TYPE FindCase(const CTempString str, const CTempString pattern, SIZE_TYPE start);
2096 
2097  /// Find the pattern in the specified range of a string using a case
2098  /// insensitive search.
2099  ///
2100  /// @param str
2101  /// String to search.
2102  /// @param pattern
2103  /// Pattern to search for in "str".
2104  /// @param start
2105  /// Position in "str" to start search from -- default of 0 means start
2106  /// the search from the beginning of the string.
2107  /// @param end
2108  /// Position in "str" to perform search up to -- default of NPOS means
2109  /// to search to the end of the string.
2110  /// @param which
2111  /// When set to eFirst, this means to find the first occurrence of
2112  /// "pattern" in "str". When set to eLast, this means to find the last
2113  /// occurrence of "pattern" in "str".
2114  /// @return
2115  /// - The start of the first or last (depending on "which" parameter)
2116  /// occurrence of "pattern" in "str", within the string interval
2117  /// ["start", "end"], or
2118  /// - NPOS if there is no occurrence of the pattern.
2119  /// @sa Find
2120  ///
2121  /// @deprecated
2122  /// Use one of the next methods instead:
2123  /// @code
2124  /// Find(str, pattern, [use_case], [direction], [occurrence])
2125  /// FindNoCase(str, pattern, [start])
2126  /// @endcode
2127  /// For example:
2128  /// @code
2129  /// FindNoCase(str, pattern, 0, NPOS, eLast)
2130  /// @endcode
2131  /// can be replaced by
2132  /// @code
2133  /// Find(str, pattern, eNocase, eReverseSearch, /* 0 */)
2134  /// @endcode
2135  /// For simpler cases without range, or with default [0, NPOS] please use
2136  /// @code
2137  /// FindNoCase(str, pattern, [start])
2138  /// @endcode
2139  /// But if you doing a search on a substring of the 'str' and ["start", "end"] search
2140  /// interval is not a default [0, NPOS], that mean a whole 'str' string, you may
2141  /// need to pass a substring instead of 'str', like
2142  /// @code
2143  /// FindNoCase(CTempString(str, start, len), pattern, ....)
2144  /// @endcode
2145  /// and after checking search result on NPOS, adjust it by 'start' yourself.
2147  static SIZE_TYPE FindNoCase(const CTempString str,
2148  const CTempString pattern,
2149  SIZE_TYPE start, SIZE_TYPE end,
2150  EOccurrence which = eFirst);
2151 
2152  /// Wrapper for backward-compatibility
2153  static SIZE_TYPE FindNoCase(const CTempString str, const CTempString pattern);
2154  static SIZE_TYPE FindNoCase(const CTempString str, const CTempString pattern, SIZE_TYPE start);
2155 
2156  /// Test for presence of a given string in a list or vector of strings
2157 
2158  static const string* Find (const list<string>& lst,
2159  const CTempString val,
2160  ECase use_case = eCase);
2161 
2162  static const string* FindCase (const list<string>& lst,
2163  const CTempString val);
2164 
2165  static const string* FindNoCase(const list<string>& lst,
2166  const CTempString val);
2167 
2168  static const string* Find (const vector<string>& vec,
2169  const CTempString val,
2170  ECase use_case = eCase);
2171 
2172  static const string* FindCase (const vector<string>& vec,
2173  const CTempString val);
2174 
2175  static const string* FindNoCase(const vector<string>& vec,
2176  const CTempString val);
2177 
2178  /// Find given word in the string.
2179  ///
2180  /// @param str
2181  /// String to search.
2182  /// @param word
2183  /// Word to search for in "str". The "word" can have any symbols,
2184  /// not letters only. Function treat it as a pattern, even it have
2185  /// any non-word characters.
2186  /// @param use_case
2187  /// Whether to do a case sensitive compare (default is eCase), or a
2188  /// case-insensitive compare (eNocase) while searching for the word.
2189  /// @param direction
2190  /// Define a search direction of the occurrence of "word" in "str".
2191  /// @return
2192  /// - Start of the found word in the string.
2193  /// - NPOS if there is no occurrence of the word in the string.
2194  static SIZE_TYPE FindWord(const CTempString str,
2195  const CTempString word,
2196  ECase use_case = eCase,
2197  EDirection direction = eForwardSearch);
2198 
2199  /// Find given word in the string.
2200  ///
2201  /// This function honors word boundaries:
2202  /// - starting or ending of the string,
2203  /// - any non-word character, all except [a-zA-Z0-9_].
2204  ///
2205  /// @param str
2206  /// String to search.
2207  /// @param word
2208  /// Word to search for in "str". The "word" can have any symbols,
2209  /// not letters only. Function treat it as a pattern, even it have
2210  /// any non-word characters.
2211  /// @param which
2212  /// When set to eFirst, this means to find the first occurrence of
2213  /// "word" in "str". When set to eLast, this means to find the last
2214  /// occurrence of "word" in "str".
2215  /// @param use_case
2216  /// Whether to do a case sensitive compare (default is eCase), or a
2217  /// case-insensitive compare (eNocase) while searching for the word.
2218  /// @return
2219  /// - The start of the first or last (depending on "which" parameter)
2220  /// occurrence of "word" in "str", or
2221  /// - NPOS if there is no occurrence of the word.
2222  /// @sa Find
2223  /// @deprecated
2224  /// Use FindWord() variant with EDirection parameter:
2225  /// @code
2226  /// FindWord(str, word, [use_case], [direction])
2227  /// @endcode
2228  inline
2231  const CTempString word,
2232  EOccurrence which,
2233  ECase use_case = eCase) {
2234  return FindWord(str, word, use_case, which == eFirst ? eForwardSearch : eReverseSearch);
2235  }
2236 
2237 
2238  /// Which end to truncate a string.
2239  enum ETrunc {
2240  eTrunc_Begin, ///< Truncate leading spaces only
2241  eTrunc_End, ///< Truncate trailing spaces only
2242  eTrunc_Both ///< Truncate spaces at both begin and end of string
2243  };
2244 
2245  /// Truncate spaces in a string.
2246  ///
2247  /// @param str
2248  /// String to truncate spaces from.
2249  /// @param where
2250  /// Which end of the string to truncate space from. Default is to
2251  /// truncate space from both ends (eTrunc_Both).
2252  /// @sa
2253  /// TruncateSpaces_Unsafe
2254  static string TruncateSpaces(const string& str,
2255  ETrunc where = eTrunc_Both);
2256 
2257  /// Truncate spaces in a string.
2258  /// It can be faster but it is also more dangerous than TruncateSpaces()
2259  ///
2260  /// @param str
2261  /// String to truncate spaces from.
2262  /// @param where
2263  /// Which end of the string to truncate space from. Default is to
2264  /// truncate space from both ends (eTrunc_Both).
2265  /// @attention
2266  /// The lifespan of the result string is the same as one of the source.
2267  /// So, for example, if the source is temporary string, or it changes somehow,
2268  /// then the result will be invalid right away (will point to already released
2269  /// or wrong range in the memory).
2270  /// @sa
2271  /// TruncateSpaces
2272  static CTempString TruncateSpaces_Unsafe(const CTempString str,
2273  ETrunc where = eTrunc_Both);
2274 
2275  /// @deprecated Use TruncateSpaces_Unsafe() instead -- AND, do make sure
2276  /// that you indeed use that in a safe manner!
2277  inline
2280  ETrunc where = eTrunc_Both) {
2281  return TruncateSpaces_Unsafe(str, where);
2282  }
2283 
2284  /// @deprecated Use TruncateSpaces_Unsafe() instead -- AND, do make sure
2285  /// that you indeed use that in a safe manner!
2286  inline
2288  static CTempString TruncateSpaces(const char* str,
2289  ETrunc where = eTrunc_Both) {
2290  return TruncateSpaces_Unsafe(str, where);
2291  }
2292 
2293  /// Truncate spaces in a string (in-place)
2294  ///
2295  /// @param str
2296  /// String to truncate spaces from.
2297  /// @param where
2298  /// Which end of the string to truncate space from. Default is to
2299  /// truncate space from both ends (eTrunc_Both).
2300  static void TruncateSpacesInPlace(string& str, ETrunc where = eTrunc_Both);
2301  static void TruncateSpacesInPlace(CTempString&, ETrunc where = eTrunc_Both);
2302 
2303 
2304  /// Trim prefix from a string (in-place)
2305  ///
2306  /// @param str
2307  /// String to trim from.
2308  /// @param prefix
2309  /// Prefix to remove.
2310  /// If string doesn't have specified prefix, it doesn't changes.
2311  /// @param use_case
2312  /// Whether to do a case sensitive compare (default is eCase), or a
2313  /// case-insensitive compare (eNocase) while checking for a prefix.
2314  static void TrimPrefixInPlace(string& str, const CTempString prefix,
2315  ECase use_case = eCase);
2316  static void TrimPrefixInPlace(CTempString& str, const CTempString prefix,
2317  ECase use_case = eCase);
2318 
2319  /// Trim prefix from a string.
2320  ///
2321  /// "Unsafe" counterpart to TrimPrefixInPlace().
2322  /// @param str
2323  /// String to trim from.
2324  /// @param prefix
2325  /// Prefix to remove.
2326  /// If string doesn't have specified prefix, it doesn't changes.
2327  /// @param use_case
2328  /// Whether to do a case sensitive compare (default is eCase), or a
2329  /// case-insensitive compare (eNocase) while checking for a prefix.
2330  /// @attention
2331  /// The lifespan of the result string is the same as one of the source.
2332  /// So, for example, if the source is temporary string, or it changes somehow,
2333  /// then the result will be invalid right away (will point to already released
2334  /// or wrong range in the memory).
2335  /// @sa
2336  /// TrimPrefixInPlace
2337  static CTempString TrimPrefix_Unsafe(const CTempString str,
2338  const CTempString prefix,
2339  ECase use_case = eCase);
2340 
2341  /// Trim suffix from a string (in-place)
2342  ///
2343  /// @param str
2344  /// String to trim from.
2345  /// @param suffix
2346  /// Suffix to remove.
2347  /// If string doesn't have specified suffix, it doesn't changes.
2348  /// @param use_case
2349  /// Whether to do a case sensitive compare (default is eCase), or a
2350  /// case-insensitive compare (eNocase) while checking for a suffix.
2351  static void TrimSuffixInPlace(string& str, const CTempString suffix,
2352  ECase use_case = eCase);
2353  static void TrimSuffixInPlace(CTempString& str, const CTempString suffix,
2354  ECase use_case = eCase);
2355 
2356  /// Trim suffix from a string.
2357  ///
2358  /// "Unsafe" counterpart to TrimSuffixInPlace().
2359  /// @param str
2360  /// String to trim from.
2361  /// @param suffix
2362  /// Suffix to remove.
2363  /// If string doesn't have specified suffix, it doesn't changes.
2364  /// @param use_case
2365  /// Whether to do a case sensitive compare (default is eCase), or a
2366  /// case-insensitive compare (eNocase) while checking for a suffix.
2367  /// @attention
2368  /// The lifespan of the result string is the same as one of the source.
2369  /// So, for example, if the source is temporary string, or it changes somehow,
2370  /// then the result will be invalid right away (will point to already released
2371  /// or wrong range in the memory).
2372  /// @sa
2373  /// TrimSuffixInPlace
2374  static CTempString TrimSuffix_Unsafe(const CTempString str,
2375  const CTempString suffix,
2376  ECase use_case = eCase);
2377 
2378  /// Replace occurrences of a substring within a string.
2379  ///
2380  /// @param src
2381  /// Source string from which specified substring occurrences are replaced.
2382  /// @param search
2383  /// Substring value in "src" that is replaced.
2384  /// @param replace
2385  /// Replace "search" substring with this value.
2386  /// @param dst
2387  /// Result of replacing the "search" string with "replace" in "src".
2388  /// This value is also returned by the function.
2389  /// @param start_pos
2390  /// Position to start search from.
2391  /// @param max_replace
2392  /// Replace no more than "max_replace" occurrences of substring "search"
2393  /// If "max_replace" is zero(default), then replace all occurrences with
2394  /// "replace".
2395  /// @param num_replace
2396  /// Optional pointer to a value which receives number of replacements occurred.
2397  /// @return
2398  /// Result of replacing the "search" string with "replace" in "src". This
2399  /// value is placed in "dst" as well.
2400  /// @note
2401  /// After replacing each occurence of the "search" string with "replace"
2402  /// this function move current search position behind a new replacement
2403  /// in the resulting string, so it doesn't search over any part of the
2404  /// inserted "replace".
2405  /// @sa
2406  /// Version of Replace() that returns a new string.
2407  static string& Replace(const string& src,
2408  const string& search,
2409  const string& replace,
2410  string& dst,
2411  SIZE_TYPE start_pos = 0,
2412  SIZE_TYPE max_replace = 0,
2413  SIZE_TYPE* num_replace = 0);
2414 
2415  /// Replace occurrences of a substring within a string and returns the
2416  /// result as a new string.
2417  ///
2418  /// @param src
2419  /// Source string from which specified substring occurrences are
2420  /// replaced.
2421  /// @param search
2422  /// Substring value in "src" that is replaced.
2423  /// @param replace
2424  /// Replace "search" substring with this value.
2425  /// @param start_pos
2426  /// Position to start search from.
2427  /// @param max_replace
2428  /// Replace no more than "max_replace" occurrences of substring "search"
2429  /// If "max_replace" is zero(default), then replace all occurrences with
2430  /// "replace".
2431  /// @param num_replace
2432  /// Optional pointer to a value which receives number of replacements occurred.
2433  /// @return
2434  /// A new string containing the result of replacing the "search" string
2435  /// with "replace" in "src"
2436  /// @note
2437  /// After replacing each occurence of the "search" string with "replace"
2438  /// this function move current search position behind a new replacement
2439  /// in the resulting string, so it doesn't search over any part of the
2440  /// inserted "replace".
2441  /// @sa
2442  /// Version of Replace() that has a destination parameter to accept
2443  /// result.
2444  static string Replace(const string& src,
2445  const string& search,
2446  const string& replace,
2447  SIZE_TYPE start_pos = 0,
2448  SIZE_TYPE max_replace = 0,
2449  SIZE_TYPE* num_replace = 0);
2450 
2451  /// Replace occurrences of a substring within a string.
2452  ///
2453  /// On some platforms this function is much faster than Replace()
2454  /// if sizes of "search" and "replace" strings are equal.
2455  /// Otherwise, the performance is mainly the same.
2456  /// @param src
2457  /// String where the specified substring occurrences are replaced.
2458  /// This value is also returned by the function.
2459  /// @param search
2460  /// Substring value in "src" that is replaced.
2461  /// @param replace
2462  /// Replace "search" substring with this value.
2463  /// @param start_pos
2464  /// Position to start search from.
2465  /// @param max_replace
2466  /// Replace no more than "max_replace" occurrences of substring "search"
2467  /// If "max_replace" is zero(default), then replace all occurrences with
2468  /// "replace".
2469  /// @note
2470  /// After replacing each occurence of the "search" string with "replace"
2471  /// this function move current search position behind a new replacement
2472  /// in the resulting string, so it doesn't search over any part of the
2473  /// inserted "replace".
2474  /// @param num_replace
2475  /// Optional pointer to a value which receives number of replacements occurred.
2476  /// @return
2477  /// Result of replacing the "search" string with "replace" in "src".
2478  /// @sa
2479  /// Replace
2480  static string& ReplaceInPlace(string& src,
2481  const string& search,
2482  const string& replace,
2483  SIZE_TYPE start_pos = 0,
2484  SIZE_TYPE max_replace = 0,
2485  SIZE_TYPE* num_replace = 0);
2486 
2487  /// Flags for Split*() methods.
2488  ///
2489  /// @note
2490  /// With quote support enabled, doubling a quote character suppresses
2491  /// its special meaning, as does escaping it if that's enabled too;
2492  /// unescaped trailing backslashes and unbalanced quotes result in
2493  /// exceptions.
2494  /// @note
2495  /// All escape symbols, single or double quotes became removed
2496  /// if a corresponding fSplit_Can* flag is used.
2498  fSplit_MergeDelimiters = 1 << 0, ///< Merge adjacent delimiters
2499  fSplit_Truncate_Begin = 1 << 1, ///< Truncate leading delimiters
2500  fSplit_Truncate_End = 1 << 2, ///< Truncate trailing delimiters
2501  fSplit_Truncate = fSplit_Truncate_Begin | fSplit_Truncate_End,
2502  fSplit_ByPattern = 1 << 3, ///< Require full delimiter strings
2503  fSplit_CanEscape = 1 << 4, ///< Allow \\... escaping
2504  fSplit_CanSingleQuote = 1 << 5, ///< Allow '...' quoting
2505  fSplit_CanDoubleQuote = 1 << 6, ///< Allow "..." quoting
2506  fSplit_CanQuote = fSplit_CanSingleQuote | fSplit_CanDoubleQuote,
2507  /// All delimiters are merged and trimmed, to get non-empty tokens only
2508  fSplit_Tokenize = fSplit_MergeDelimiters | fSplit_Truncate
2509  };
2510  typedef int TSplitFlags; ///< Bitwise OR of ESplitFlags
2511 
2512  /// Whether to merge adjacent delimiters.
2513  /// Used by some methods that don't need full functionality of ESplitFlags.
2515  eMergeDelims = fSplit_MergeDelimiters | fSplit_Truncate,
2516  eNoMergeDelims = 0
2517  };
2518 
2519  /// Split a string using specified delimiters.
2520  ///
2521  /// @param str
2522  /// String to be split.
2523  /// @param delim
2524  /// Delimiter(s) used to split string "str". The interpretation of
2525  /// multi-character values depends on flags: by default, any of those
2526  /// characters marks a split point (when unquoted), but with
2527  /// fSplit_ByPattern, the entire string must occur. (Meanwhile,
2528  /// an empty value disables splitting.)
2529  /// @param arr
2530  /// The split tokens are added to the list "arr" and also returned
2531  /// by the function.
2532  /// @param flags
2533  /// Flags directing splitting, characterized under ESplitFlags.
2534  /// @param token_pos
2535  /// Optional array for the tokens' positions in "str".
2536  /// @attention
2537  /// Modifying source CTempString object or destroying it,
2538  /// will invalidate results.
2539  /// @return
2540  /// The list "arr" is also returned.
2541  /// @sa
2542  /// ESplitFlags, SplitInTwo, SplitByPattern
2543  static list<string>& Split( const CTempString str,
2544  const CTempString delim,
2545  list<string>& arr,
2546  TSplitFlags flags = 0,
2547  vector<SIZE_TYPE>* token_pos = NULL);
2548 
2549  static vector<string>& Split(
2550  const CTempString str,
2551  const CTempString delim,
2552  vector<string>& arr,
2553  TSplitFlags flags = 0,
2554  vector<SIZE_TYPE>* token_pos = NULL);
2555 
2556  static list<CTempString>& Split(
2557  const CTempString str,
2558  const CTempString delim,
2559  list<CTempString>& arr,
2560  TSplitFlags flags = 0,
2561  vector<SIZE_TYPE>* token_pos = NULL,
2562  CTempString_Storage* storage = NULL);
2563 
2564  static vector<CTempString>& Split(
2565  const CTempString str,
2566  const CTempString delim,
2567  vector<CTempString>& arr,
2568  TSplitFlags flags = 0,
2569  vector<SIZE_TYPE>* token_pos = NULL,
2570  CTempString_Storage* storage = NULL);
2571 
2572  static list<CTempStringEx>& Split(
2573  const CTempString str,
2574  const CTempString delim,
2575  list<CTempStringEx>& arr,
2576  TSplitFlags flags = 0,
2577  vector<SIZE_TYPE>* token_pos = NULL,
2578  CTempString_Storage* storage = NULL);
2579 
2580  static vector<CTempStringEx>& Split(
2581  const CTempString str,
2582  const CTempString delim,
2583  vector<CTempStringEx>& arr,
2584  TSplitFlags flags = 0,
2585  vector<SIZE_TYPE>* token_pos = NULL,
2586  CTempString_Storage* storage = NULL);
2587 
2588  /// Split a string into two pieces using the specified delimiters
2589  ///
2590  /// @param str
2591  /// String to be split.
2592  /// @param delim
2593  /// Delimiters used to split string "str".
2594  /// @param str1
2595  /// The sub-string of "str" before the first character of "delim".
2596  /// It will not contain any characters in "delim".
2597  /// Will be empty if "str" begin with a delimiter.
2598  /// @param str2
2599  /// The sub-string of "str" after the first character of "delim" found.
2600  /// May contain "delim" characters.
2601  /// Will be empty if "str" had no "delim" characters or ended
2602  /// with the "delim" character.
2603  /// @param flags
2604  /// Flags directing splitting, characterized under ESplitFlags.
2605  /// Note, that fSplit_Truncate_End don't have any effect due nature
2606  /// of this method.
2607  /// @attention
2608  /// Modifying source CTempString object or destroying it,
2609  /// will invalidate results.
2610  /// @return
2611  /// true if a symbol from "delim" was found in "str", false if not.
2612  /// This lets you distinguish when there were no delimiters and when
2613  /// the very last character was the first delimiter.
2614  /// @sa
2615  /// ESplitFlags, Split
2616  static bool SplitInTwo(const CTempString str,
2617  const CTempString delim,
2618  string& str1,
2619  string& str2,
2620  TSplitFlags flags = 0);
2621 
2622  static bool SplitInTwo(const CTempString str,
2623  const CTempString delim,
2624  CTempString& str1,
2625  CTempString& str2,
2626  TSplitFlags flags = 0,
2627  CTempString_Storage* storage = NULL);
2628 
2629  static bool SplitInTwo(const CTempString str,
2630  const CTempString delim,
2631  CTempStringEx& str1,
2632  CTempStringEx& str2,
2633  TSplitFlags flags = 0,
2634  CTempString_Storage* storage = NULL);
2635 
2636 
2637  /// Variation of Split() with fSplit_ByPattern flag applied by default
2638 
2639  static list<string>& SplitByPattern(
2640  const CTempString str,
2641  const CTempString delim,
2642  list<string>& arr,
2643  TSplitFlags flags = 0,
2644  vector<SIZE_TYPE>* token_pos = NULL);
2645 
2646  static vector<string>& SplitByPattern(
2647  const CTempString str,
2648  const CTempString delim,
2649  vector<string>& arr,
2650  TSplitFlags flags = 0,
2651  vector<SIZE_TYPE>* token_pos = NULL);
2652 
2653  static list<CTempString>& SplitByPattern(
2654  const CTempString str,
2655  const CTempString delim,
2656  list<CTempString>& arr,
2657  TSplitFlags flags = 0,
2658  vector<SIZE_TYPE>* token_pos = NULL,
2659  CTempString_Storage* storage = NULL);
2660 
2661  static vector<CTempString>& SplitByPattern(
2662  const CTempString str,
2663  const CTempString delim,
2664  vector<CTempString>& arr,
2665  TSplitFlags flags = 0,
2666  vector<SIZE_TYPE>* token_pos = NULL,
2667  CTempString_Storage* storage = NULL);
2668 
2669  static list<CTempStringEx>& SplitByPattern(
2670  const CTempString str,
2671  const CTempString delim,
2672  list<CTempStringEx>& arr,
2673  TSplitFlags flags = 0,
2674  vector<SIZE_TYPE>* token_pos = NULL,
2675  CTempString_Storage* storage = NULL);
2676 
2677  static vector<CTempStringEx>& SplitByPattern(
2678  const CTempString str,
2679  const CTempString delim,
2680  vector<CTempStringEx>& arr,
2681  TSplitFlags flags = 0,
2682  vector<SIZE_TYPE>* token_pos = NULL,
2683  CTempString_Storage* storage = NULL);
2684 
2685  /// Join strings using the specified delimiter.
2686  ///
2687  /// @param arr
2688  /// Array of strings to be joined.
2689  /// @param delim
2690  /// Delimiter used to join the string.
2691  /// @return
2692  /// The strings in "arr" are joined into a single string, separated
2693  /// with "delim".
2694  /// @sa Split
2695  template<typename TContainer>
2696  static string
2697  Join(const TContainer& arr, const CTempString& delim)
2698  {
2699  return x_Join(begin(arr), end(arr), delim);
2700  }
2701  template<typename TValue>
2702  static string
2703  Join(const initializer_list<TValue>& arr, const CTempString& delim)
2704  {
2705  return x_Join(begin(arr), end(arr), delim);
2706  }
2707  template<typename TInputIterator>
2708  static string
2709  Join( TInputIterator from, TInputIterator to, const CTempString& delim)
2710  {
2711  return x_Join(from, to, delim);
2712  }
2713  template<typename TInputIterator>
2714  static string
2715  JoinNumeric( TInputIterator from, TInputIterator to, const CTempString& delim)
2716  {
2717  return x_Join( from, to, delim);
2718  }
2719  template<typename TIterator, typename FTransform>
2720  static string
2721  TransformJoin( TIterator from, TIterator to, const CTempString& delim, FTransform fnTransform);
2722 
2723 
2724  /// How to display printable strings.
2725  ///
2726  /// Assists in making a printable version of "str".
2728  fNewLine_Quote = 0, ///< Display "\n" instead of actual linebreak
2729  eNewLine_Quote = fNewLine_Quote,
2730  fNewLine_Passthru = 1, ///< Break the line at every "\n" occurrence
2731  eNewLine_Passthru = fNewLine_Passthru,
2732  fNonAscii_Passthru = 0, ///< Allow non-ASCII but printable characters
2733  fNonAscii_Quote = 2, ///< Octal for all non-ASCII characters
2734  fPrintable_Full = 64 ///< Show all octal digits at all times
2735  };
2736  typedef int TPrintableMode; ///< Bitwise OR of EPrintableMode flags
2737 
2738  /// Get a printable version of the specified string.
2739  ///
2740  /// All non-printable characters will be represented as "\a", "\b", "\f",
2741  /// "\n", "\r", "\t", "\v", "\'", "\"", "\\\\", etc. or "\\ooo" where 'ooo'
2742  /// is an octal code of the character. The resultant string is a well-
2743  /// formed C string literal, which, without alterations, can be compiled by
2744  /// a C/C++ compiler. Potential tri-graphs are taken care of, too.
2745  /// In many instances, octal representations of non-printable characters
2746  /// can be reduced to take less than all 3 digits, if there is no ambiguity
2747  /// in the interpretation. fPrintable_Full cancels the reduction, and
2748  /// forces to produce the full 3-digit octal codes throughout.
2749  ///
2750  /// @param str
2751  /// The string whose printable version is wanted.
2752  /// @param mode
2753  /// How to display the string. The default setting of fNewLine_Quote
2754  /// displays the new lines as "\n", and uses the octal code reduction.
2755  /// When set to fNewLine_Passthru, the line breaks are actually produced
2756  /// after each "\n" but preceded with trailing backslashes.
2757  /// @return
2758  /// Return a printable version of "str".
2759  /// @sa
2760  /// ParseEscapes, Escape, CEncode, CParse, Sanitize
2761  static string PrintableString(const CTempString str,
2762  TPrintableMode mode = fNewLine_Quote | fNonAscii_Passthru);
2763 
2764  /// Escape string (generic version).
2765  ///
2766  /// Prefix any occurrences of the metacharacters with the escape character.
2767  /// @param str
2768  /// The string to be escaped.
2769  /// @metacharacters
2770  /// List of characters that need to be escaped.
2771  /// Use NStr::Join() if you have metacharacters in list<>, vector<> or set<>.
2772  /// @param escape_char
2773  /// Character used for escaping metacharacters.
2774  /// Each metacharacter will be replaced with pair "escape_char + metacharacter".
2775  /// Each escape character will be replaced with pair "escape_char + escape_char".
2776  /// @return
2777  /// Escaped string.
2778  /// @sa
2779  /// Unescape, PrintableString, Join
2780  static string Escape(const CTempString str, const CTempString metacharacters,
2781  char escape_char = '\\');
2782 
2783  /// Unescape string (generic version).
2784  ///
2785  /// Remove escape characters added by Escape().
2786  /// @param str
2787  /// The string to be processed.
2788  /// @param escape_char
2789  /// Character used for escaping.
2790  /// @return
2791  /// Unescaped string.
2792  /// @sa
2793  /// Escape
2794  static string Unescape(const CTempString str, char escape_char = '\\');
2795 
2796 
2797  /// Quote string (generic version).
2798  ///
2799  /// Prepend and append a specified quote character, but escaping any occurrence
2800  /// of the quote character using either a specified escape character (default '\')
2801  /// or as option, by doubling the quoting character if escape character is the same
2802  /// (e.g. like the single quote in SQL, double-quote in CSV).
2803  ///
2804  /// @param str
2805  /// The string to be quoted.
2806  /// @param quote_char
2807  /// Character used for quoting, default to double quote '"'.
2808  /// @param escape_char
2809  /// Character used for escaping other quote characters inside string (default '\').
2810  /// Each <quote_char> in the string will be replaced with pair "escape_char + quote_char".
2811  /// Each <escape_char> in the string will be replaced with pair "escape_char + escape_char".
2812  /// @return
2813  /// Quoted string.
2814  /// @sa
2815  /// Unquote, ParseQuoted, CEncode
2816  static string Quote(const CTempString str, char quote_char = '"', char escape_char = '\\');
2817 
2818  /// Unquote string (generic version).
2819  ///
2820  /// Remove quotation added by Quote(). Uses first character as quoting character.
2821  /// @param str
2822  /// The string to be processed.
2823  /// @param escape_char
2824  /// Character used for escaping.
2825  /// @return
2826  /// Unquoted string.
2827  /// @sa
2828  /// Quote, ParseQuoted, CEncode
2829  static string Unquote(const CTempString str, char escape_char = '\\');
2830 
2831 
2832  /// Flags for Sanitize().
2833  enum ESS_Flags {
2834  // Character filters
2835  fSS_alpha = 1 << 0, ///< Check on ::isalpha()
2836  fSS_digit = 1 << 1, ///< Check on ::isdigit()
2837  fSS_alnum = 1 << 2, ///< Check on ::isalnum()
2838  fSS_print = 1 << 3, ///< Check on ::isprint()
2839  fSS_cntrl = 1 << 4, ///< Check on ::iscntrl()
2840  fSS_punct = 1 << 5, ///< Check on ::ispunct()
2841 
2842  // Filter: in or out?
2843  fSS_Reject = 1 << 11, ///< Reject specified characters, allow all other.
2844  ///< Revert default behavior, that allow specified
2845  ///< characters and reject all other.
2846  // Utility flags
2847  fSS_Remove = 1 << 12, ///< Remove (rather than replace) rejected chars
2848  fSS_NoMerge = 1 << 13, ///< Do not merge adjacent spaces (rejected chars)
2849  fSS_NoTruncate_Begin = 1 << 14, ///< Do not truncate leading spaces
2850  fSS_NoTruncate_End = 1 << 15, ///< Do not truncate trailing spaces
2851  fSS_NoTruncate = fSS_NoTruncate_Begin | fSS_NoTruncate_End
2852  };
2853  typedef int TSS_Flags; ///< Bitwise OR of ESS_Flags
2854 
2855  /// Sanitize a string, allowing only specified classes of characters.
2856  ///
2857  /// By default:
2858  /// - replace all non-printable characters with spaces;
2859  /// - merge coalescent spaces;
2860  /// - truncate leading and trailing spaces.
2861  /// @note
2862  /// - All coalescent leading/trailing spaces also will be merged
2863  /// by default if fSS_NoMerge has not specified.
2864  /// - The truncation of leading/trailing spaces is doing after
2865  /// allowing/rejecting characters. Depending on the specified flags,
2866  /// all rejected characters adjacent to it can be treat as part
2867  /// of leading/trailing spaces.
2868  /// @param str
2869  /// String to sanitize
2870  /// @param flags
2871  /// Alternative sanitation options
2872  /// @return
2873  /// Sanitized string
2874  /// @sa
2875  /// PrintableString
2876  static string Sanitize(CTempString str, TSS_Flags flags = fSS_print)
2877  {
2878  return Sanitize(str, CTempString(), CTempString(), ' ', flags);
2879  }
2880 
2881 
2882  /// Sanitize a string, allowing only specified characters or character classes.
2883  ///
2884  /// More customizable version of Sanitize():
2885  /// - allow to specify custom sets of allowed and rejected characters,
2886  /// in addition to predefined classes if specified, see TSS_Flags;
2887  /// - allow to specify replacement character for rejected symbols;
2888  /// By default:
2889  /// - replace all rejected characters with <reject_replacement>;
2890  /// - merge coalescent spaces and <reject_replacement>s (separately if differ);
2891  /// - truncate leading and trailing spaces.
2892  /// Filters check order:
2893  /// - character classes via flags.
2894  /// Note, that if no character classes are set, and no custom <allow_chars>
2895  /// or <reject_chars>, fSS_print will be used;
2896  /// - <allow_chars> if not empty, have priority over flags.
2897  /// - <reject_chars> if not empty, have priority over flags and <allow_chars> if have intersections.
2898  /// @note
2899  /// - All coalescent leading/trailing spaces also will be merged
2900  /// by default if fSS_NoMerge has not specified.
2901  /// - The truncation of leading/trailing spaces is doing after
2902  /// allowing/rejecting characters.
2903  /// @note
2904  /// Spaces processes after checks on allowance, so if it isn't allowed
2905  /// it will be threatened as regular rejected character.
2906  /// @param str
2907  /// String to sanitize.
2908  /// @param allow_chars
2909  /// Additional list of allowed characters, in addition to character classes in <flags>.
2910  /// Have priority over character classes.
2911  /// Use NStr::Join() if you have it in list<>, vector<> or set<>.
2912  /// @param reject_chars
2913  /// Additional list of rejected characters, in addition to character classes in <flags>.
2914  /// Have priority over character classes and <allow_chars>.
2915  /// Use NStr::Join() if you have it in list<>, vector<> or set<>.
2916  /// @param reject_replacement
2917  /// Replacement character for all rejected characters.
2918  /// @param flags
2919  /// Alternative sanitation options.
2920  /// If no custom <allow_chars> or <reject_chars>, and no character classes are set, then use fSS_print by default.
2921  /// If <reject_chars>, no class, and no fSS_Reject flag, then all characters allowed except <reject_chars>.
2922  /// If <allow_chars>, no class, and fSS_Reject flag, then no any character allowed except <allow_chars>.
2923  /// @return
2924  /// Sanitized string
2925  /// @sa
2926  /// PrintableString, Join
2927  static string Sanitize(CTempString str,
2928  CTempString allow_chars,
2929  CTempString reject_chars,
2930  char reject_replacement = ' ',
2931  TSS_Flags flags = 0);
2932 
2933  /// C-style escape sequences parsing mode.
2934  /// For escape sequences with a value outside the range of [0-255]
2935  /// the behavior of ParseEscapes() depends from this mode.
2936  /// By default all escape sequences within a out or range
2937  /// will be converted to the least significant byte, with no warning.
2939  eEscSeqRange_Standard, ///< Set char to the last (least significant
2940  ///< byte) of the escape sequence (default).
2941  eEscSeqRange_FirstByte, ///< Set char to the first byte of the escape
2942  ///< sequence.
2943  eEscSeqRange_Throw, ///< Throw an exception.
2944  eEscSeqRange_Errno, ///< Set errno to ERANGE, return empty string.
2945  eEscSeqRange_User ///< Set char to the user value
2946  ///< passed in another parameter.
2947  };
2948 
2949  /// Parse C-style escape sequences in the specified string.
2950  ///
2951  /// Parse escape sequences including all those produced by PrintableString.
2952  /// @param str
2953  /// The string to be parsed.
2954  /// @param mode
2955  /// Parsing mode.
2956  /// By default all escape sequences with a value outside the range of [0-255]
2957  /// will be converted to the least significant byte, with no warning.
2958  /// @param user_char
2959  /// If 'mode' have eEscSeqRange_User, replace all out of range
2960  /// escape sequences with this char.
2961  /// @return
2962  /// String with parsed C-style escape sequences.
2963  /// - If string have wrong format throw an CStringException exception.
2964  /// - If parsing succeeds, return the converted value.
2965  /// Set errno to zero only if eEscSeqRange_Errno is set.
2966  /// - Otherwise, if escape sequence is out of range [0-255],
2967  /// see eEscSeqRange* modes for behavior.
2968  /// @sa
2969  /// EEscSeqFlags, PrintableString, CEncode, CParse
2970  static string ParseEscapes(const CTempString str,
2971  EEscSeqRange mode = eEscSeqRange_Standard,
2972  char user_char = '?');
2973 
2974  /// Discard C-style backslash escapes and extract a quoted string.
2975  ///
2976  /// @param[in] str
2977  /// The original string to extract a quoted string from.
2978  /// It must start with a double quote.
2979  /// @param[out] n_read
2980  /// How many symbols the quoted string occupied in the original string.
2981  /// @return
2982  /// The extracted string, un-escaped and with the quotes removed.
2983  /// Throw an exception on format error.
2984  static string ParseQuoted(const CTempString str, size_t* n_read = NULL);
2985 
2986  /// Define that string is quoted or not.
2987  enum EQuoted {
2988  eQuoted, ///< String is quoted
2989  eNotQuoted ///< String is not quoted
2990  };
2991 
2992  /// Encode a string for C/C++.
2993  ///
2994  /// @param str
2995  /// The string to be parsed.
2996  /// @param quoted
2997  /// Define, to
2998  /// @sa
2999  /// CParse, PrintableString
3000  static string CEncode(const CTempString str, EQuoted quoted = eQuoted);
3001 
3002  /// Discard C-style backslash escapes.
3003  ///
3004  /// @param str
3005  /// The original string to parse.
3006  /// @param quoted
3007  /// Define that parsing string is quoted or not.
3008  /// If parameter "quoted" equal eQuoted and string is not started and
3009  /// finished with a double-quote, the exception will be thrown,
3010  /// otherwise quotes will be removed in result.
3011  /// @return
3012  /// String with parsed C-style escape sequences.
3013  /// @sa
3014  /// CEncode
3015  static string CParse(const CTempString str, EQuoted quoted = eQuoted);
3016 
3017  /// Encode a string for JavaScript.
3018  ///
3019  /// Replace relevant characters by predefined entities.
3020  /// Like to PrintableString(), but process some symbols in different way.
3021  /// @sa PrintableString
3022  static string JavaScriptEncode(const CTempString str);
3023 
3024  /// XML-encode flags
3025  enum EXmlEncode {
3026  /// Encode predefined entities only
3027  eXmlEnc_Contents = 0,
3028  /// Encode double hyphen and ending hyphen,
3029  /// making the result safe to put into XML comments.
3030  eXmlEnc_CommentSafe = 1 << 0,
3031  /// Check each character to conform XML 1.1 standards,
3032  /// skip any not allowed character or throw an CStringException.
3033  /// https://www.w3.org/TR/xml11/#NT-Char
3034  eXmlEnc_Unsafe_Skip = 1 << 1,
3035  eXmlEnc_Unsafe_Throw = 1 << 2
3036  };
3037  typedef int TXmlEncode; //< bitwise OR of "EXmlEncode"
3038 
3039  /// Encode a string for XML.
3040  ///
3041  /// Replace relevant characters by predefined entities.
3042  static string XmlEncode(const CTempString str,
3043  TXmlEncode flags = eXmlEnc_Contents);
3044 
3045 
3046  /// HTML-decode flags
3048  fHtmlEnc_EncodeAll = 0, ///< Encode all symbols
3049  fHtmlEnc_SkipLiteralEntities = 1 << 1, ///< Skip "&entity;"
3050  fHtmlEnc_SkipNumericEntities = 1 << 2, ///< Skip "&#NNNN;"
3051  fHtmlEnc_SkipEntities = fHtmlEnc_SkipLiteralEntities | fHtmlEnc_SkipNumericEntities,
3052  fHtmlEnc_CheckPreencoded = 1 << 3 ///< Print warning if some pre-encoded
3053  ///< entity found in the string
3054  };
3055  typedef int THtmlEncode; //< bitwise OR of "EHtmlEncode"
3056 
3057  /// Encode a string for HTML.
3058  ///
3059  /// Replace relevant characters by predefined entities.
3060  /// @param str
3061  /// Original string in UTF8 encoding.
3062  static string HtmlEncode(const CTempString str,
3063  THtmlEncode flags = fHtmlEnc_EncodeAll);
3064 
3065  /// HTML-decode flags
3067  fHtmlDec_CharRef_Entity = 1, ///< Character entity reference(s) was found
3068  fHtmlDec_CharRef_Numeric = 1 << 1, ///< Numeric character reference(s) was found
3069  fHtmlDec_Encoding_Changed = 1 << 2 ///< Character encoding changed
3070  };
3071  typedef int THtmlDecode; //< bitwise OR of "EHtmlDecode"
3072 
3073  /// Decode HTML entities and character references.
3074  ///
3075  /// @param str
3076  /// String to be decoded, which contains characters or numeric HTML entities
3077  /// @param encoding
3078  /// Encoding of the input string
3079  /// @return
3080  /// UTF8 encoded string
3081  static string HtmlDecode(const CTempString str,
3082  EEncoding encoding = eEncoding_Unknown,
3083  THtmlDecode* result_flags = NULL);
3084 
3085  /// Returns HTML entity name for this symbol if one exists
3086  /// (without leading ampersand and trailing semicolon);
3087  /// or empty string if suitable HTML entity was not found
3088  static string HtmlEntity(TUnicodeSymbol uch);
3089 
3090  /// Json-encode flags
3092  eJsonEnc_UTF8, ///< Encode all characters above 0x80 to \uXXXX form.
3093  ///< https://tools.ietf.org/html/rfc7159#section-8.1
3094  eJsonEnc_Quoted ///< Quote resulting string. Keep all Unicode symbols as is.
3095  ///< https://tools.ietf.org/html/rfc7159#section-7
3096  };
3097  /// Encode a string for JSON.
3098  ///
3099  /// @param str
3100  /// The string to encode.
3101  /// @param encoding
3102  /// Specifies how to encode string.
3103  /// There are 2 approaches, with representing whole string as UTF-8 encoded string,
3104  /// or leave all Unicode symbols "as is", but the resulting string will be put in double quotes.
3105  /// @warning
3106  /// This method is not intended to work with strings that already have UTF-8 encoding,
3107  /// except simple eJsonEnc_Quoted mode, that just quote a string, without any real encoding.
3108  /// Passed string have no information on encoding and JsonEncode() cannot detect it.
3109  /// So, with default eJsonEnc_UTF8 mode such strings will be re-encoded again, so you will
3110  /// have double UTF-8 encoded string as result, that is not what you may expect. Be aware.
3111  /// @return
3112  /// JSON encoded string
3113  static string JsonEncode(const CTempString str, EJsonEncode encoding = eJsonEnc_UTF8);
3114 
3115  /// Decode a string encoded by JsonEncode.
3116  ///
3117  /// @param str
3118  /// The string to encode.
3119  /// It must be in double quotes.
3120  /// @param[out] n_read
3121  /// How many symbols the quoted string occupied in the original string.
3122  /// @sa
3123  /// JsonEncode
3124  /// @warning
3125  /// This method only supports strings encoded by JsonEncode-specific encodings.
3126  static string JsonDecode(const CTempString str, size_t* n_read = NULL);
3127 
3128  /// Quotes a string in Bourne Again Shell (BASH) syntax, in a way
3129  /// that disallows non-printable characters in the result.
3130  /// This function does NOT implement aesthetically optimal quoting,
3131  /// but does try to avoid redundant quoting in simpler cases.
3132  /// Also, since it implements BASH syntax, the result may be
3133  /// incompatible with Bourne syntax, and may be non-obvious to
3134  /// people who are not familiar with the extended quoting syntax.
3135  /// @note The BASH shell has extensions beyond Bourne Shell quoting.
3136  /// Also, this is very different from C Shell quoting, and
3137  /// MS Windows Command Prompt quoting rules.
3138  static string ShellEncode(const string& str);
3139 
3140  /// URL-encode flags
3141  enum EUrlEncode {
3142  eUrlEnc_SkipMarkChars, ///< Do not convert chars like '!', '(' etc.
3143  eUrlEnc_ProcessMarkChars, ///< Convert all non-alphanumeric chars, spaces are converted to '+'
3144  eUrlEnc_PercentOnly, ///< Convert all non-alphanumeric chars including space and '%' to %## format
3145  eUrlEnc_Path, ///< Same as ProcessMarkChars but preserves valid path characters ('/', '.')
3146  eUrlEnc_URIScheme, ///< Encode scheme part of an URI.
3147  eUrlEnc_URIUserinfo, ///< Encode userinfo part of an URI.
3148  eUrlEnc_URIHost, ///< Encode host part of an URI.
3149  eUrlEnc_URIPath, ///< Encode path part of an URI.
3150  eUrlEnc_URIQueryName, ///< Encode query part of an URI, arg name.
3151  eUrlEnc_URIQueryValue, ///< Encode query part of an URI, arg value.
3152  eUrlEnc_URIFragment, ///< Encode fragment part of an URI.
3153  eUrlEnc_Cookie, ///< Same as SkipMarkChars with encoded ','
3154  eUrlEnc_None ///< Do not encode
3155  };
3156  /// URL decode flags
3157  enum EUrlDecode {
3158  eUrlDec_All, ///< Decode '+' to space
3159  eUrlDec_Percent ///< Decode only %XX
3160  };
3161  /// URL-encode string
3162  static string URLEncode(const CTempString str,
3163  EUrlEncode flag = eUrlEnc_SkipMarkChars);
3164 
3165  /// SQL encode flags
3166  enum ESqlEncode {
3167  eSqlEnc_Plain, ///< Always produce '...', with no tag.
3168  eSqlEnc_TagNonASCII ///< Produce N'...' when input's not pure ASCII.
3169  };
3170  /// SQL-encode string
3171  ///
3172  /// There are some assumptions/notes about the function:
3173  /// 1. Only for MS SQL and Sybase.
3174  /// 2. Only for string values in WHERE and LIKE clauses.
3175  /// 3. The ' symbol must not be used as an escape symbol in LIKE clause.
3176  /// 4. It must not be used for non-string values.
3177  /// 5. It expects a string without any outer quotes, and
3178  /// it adds single quotes to the returned string.
3179  /// 6. It expects UTF-8 (including its subsets, ASCII and Latin1) or
3180  /// Win1252 string, and the input encoding is preserved.
3181  /// @param str
3182  /// The string to encode
3183  /// @param flag
3184  /// Whether to tag the result with an N prefix if it contains any
3185  /// non-ASCII characters. Such tagging is generally advisable,
3186  /// but off by default per historical practice, since there are
3187  /// corner cases in which it may be inappropriate.
3188  /// @return
3189  /// Encoded string with added outer single quotes
3190  static CStringUTF8 SQLEncode(const CStringUTF8& str, ESqlEncode flag);
3191 
3193  { return SQLEncode(str, eSqlEnc_Plain); }
3194 
3195  /// URL-decode string
3196  static string URLDecode(const CTempString str, EUrlDecode flag = eUrlDec_All);
3197  /// URL-decode string to itself
3198  static void URLDecodeInPlace(string& str, EUrlDecode flag = eUrlDec_All);
3199  /// Check if the string needs the requested URL-encoding
3200  static bool NeedsURLEncoding(const CTempString str, EUrlEncode flag = eUrlEnc_SkipMarkChars);
3201 
3202  /// Base64-encode string.
3203  ///
3204  /// @param str
3205  /// The string to encode.
3206  /// @param line_len
3207  /// Specify a length for Base64-encoded lines. Default 0 mean no line breaks at all.
3208  /// @return
3209  /// Encoded string.
3210  /// @sa Base64Decode, BASE64_Encode, BASE64_Deccode
3211  static string Base64Encode(const CTempString str, size_t line_len = 0);
3212 
3213  /// Base64-decode string
3214  ///
3215  /// @param str
3216  /// The string to decode.
3217  /// @return
3218  /// Encoded string, or empty line on encoding error.
3219  /// @sa Base64Encode, BASE64_Encode, BASE64_Deccode
3220  static string Base64Decode(const CTempString str);
3221 
3222  /// Check if the string contains a valid IP address
3223  static bool IsIPAddress(const CTempStringEx str);
3224 
3225 
3226  /// How to wrap the words in a string to a new line.
3227  enum EWrapFlags {
3228  fWrap_Hyphenate = 0x1, ///< Add a hyphen when breaking words?
3229  fWrap_HTMLPre = 0x2, ///< Wrap as pre-formatted HTML?
3230  fWrap_FlatFile = 0x4 ///< Wrap for flat file use.
3231  };
3232  typedef int TWrapFlags; ///< Bitwise OR of "EWrapFlags"
3233 
3234  /// Wrap the specified string into lines of a specified width.
3235  ///
3236  /// Split string "str" into lines of width "width" and add the
3237  /// resulting lines to the list "arr". Normally, all
3238  /// lines will begin with "prefix" (counted against "width"),
3239  /// but the first line will instead begin with "prefix1" if
3240  /// you supply it.
3241  ///
3242  /// @param str
3243  /// String to be split into wrapped lines.
3244  /// @param width
3245  /// Width of each wrapped line.
3246  /// @param arr
3247  /// List of strings containing wrapped lines.
3248  /// @param flags
3249  /// How to wrap the words to a new line. See EWrapFlags documentation.
3250  /// @param prefix
3251  /// The prefix string added to each wrapped line, except the first line,
3252  /// unless "prefix1" is set.
3253  /// If "prefix" is set to 0(default), do not add a prefix string to the
3254  /// wrapped lines.
3255  /// @param prefix1
3256  /// The prefix string for the first line. Use this for the first line
3257  /// instead of "prefix".
3258  /// If "prefix1" is set to 0(default), do not add a prefix string to the
3259  /// first line.
3260  /// @return
3261  /// Return "arr", the list of wrapped lines.
3262  template<typename _D>
3263  static void WrapIt(const string& str, SIZE_TYPE width,
3264  _D& dest, TWrapFlags flags = 0,
3265  const string* prefix = 0,
3266  const string* prefix1 = 0);
3267 
3269  {
3270  public:
3271  virtual ~IWrapDest() {}
3272  virtual void Append(const string& s) = 0;
3273  virtual void Append(const CTempString& s) = 0;
3274  };
3275 
3277  {
3278  protected:
3279  list<string>& m_list;
3280  public:
3281  CWrapDestStringList(list<string>& l) : m_list(l) {};
3282  virtual void Append(const string& s)
3283  {
3284  m_list.push_back(s);
3285  }
3286  virtual void Append(const CTempString& s)
3287  {
3288  m_list.push_back(NcbiEmptyString);
3289  m_list.back().assign(s.data(), s.length());
3290  }
3291  };
3292 
3293  static void Wrap(const string& str, SIZE_TYPE width,
3294  IWrapDest& dest, TWrapFlags flags,
3295  const string* prefix,
3296  const string* prefix1);
3297 
3298  static list<string>& Wrap(const string& str, SIZE_TYPE width,
3299  list<string>& arr, TWrapFlags flags = 0,
3300  const string* prefix = 0,
3301  const string* prefix1 = 0);
3302 
3303  static list<string>& Wrap(const string& str, SIZE_TYPE width,
3304  list<string>& arr, TWrapFlags flags,
3305  const string& prefix,
3306  const string* prefix1 = 0);
3307 
3308  static list<string>& Wrap(const string& str, SIZE_TYPE width,
3309  list<string>& arr, TWrapFlags flags,
3310  const string& prefix,
3311  const string& prefix1);
3312 
3313 
3314  /// Wrap the list using the specified criteria.
3315  ///
3316  /// WrapList() is similar to Wrap(), but tries to avoid splitting any
3317  /// elements of the list to be wrapped. Also, the "delim" only applies
3318  /// between elements on the same line; if you want everything to end with
3319  /// commas or such, you should add them first.
3320  ///
3321  /// @param l
3322  /// The list to be wrapped.
3323  /// @param width
3324  /// Width of each wrapped line.
3325  /// @param delim
3326  /// Delimiters used to split elements on the same line.
3327  /// @param arr
3328  /// List containing the wrapped list result.
3329  /// @param flags
3330  /// How to wrap the words to a new line. See EWrapFlags documentation.
3331  /// @param prefix
3332  /// The prefix string added to each wrapped line, except the first line,
3333  /// unless "prefix1" is set.
3334  /// If "prefix" is set to 0(default), do not add a prefix string to the
3335  /// wrapped lines.
3336  /// @param prefix1
3337  /// The prefix string for the first line. Use this for the first line
3338  /// instead of "prefix".
3339  /// If "prefix1" is set to 0(default), do not add a prefix string to the
3340  /// first line.
3341  /// @return
3342  /// Return "arr", the wrapped list.
3343  static list<string>& WrapList(const list<string>& l, SIZE_TYPE width,
3344  const string& delim, list<string>& arr,
3345  TWrapFlags flags = 0,
3346  const string* prefix = 0,
3347  const string* prefix1 = 0);
3348 
3349  static list<string>& WrapList(const list<string>& l, SIZE_TYPE width,
3350  const string& delim, list<string>& arr,
3351  TWrapFlags flags,
3352  const string& prefix,
3353  const string* prefix1 = 0);
3354 
3355  static list<string>& WrapList(const list<string>& l, SIZE_TYPE width,
3356  const string& delim, list<string>& arr,
3357  TWrapFlags flags,
3358  const string& prefix,
3359  const string& prefix1);
3360 
3361 
3362  /// Justify the specified string into a series of lines of the same width.
3363  ///
3364  /// Split string "str" into a series of lines, all of which (except for the
3365  /// final one) are to be exactly "width" characters wide (by adding extra
3366  /// inner spaces between words when necessary), and store the resultant
3367  /// lines in the list "par". Normally, all lines in "par" will begin with
3368  /// "pfx" (counted against "width"), but the first line can instead begin
3369  /// with "pfx1" if provided.
3370  ///
3371  /// @note Words exceeding the specified "width" will not be split between
3372  /// lines but occupy individual lines (which will be wider than "width").
3373  ///
3374  /// @param str
3375  /// String to be split into justified lines.
3376  /// @param width
3377  /// Width of every line (except for the last one).
3378  /// @param par
3379  /// Resultant list of justified lines.
3380  /// @param pfx
3381  /// The prefix string added to each line, except for the first line
3382  /// if non-NULL "pfx1" is also set. Empty(or NULL) "pfx" causes no
3383  /// additions.
3384  /// @param pfx1
3385  /// The prefix string for the first line, if non-NULL.
3386  /// @return
3387  /// Return "par", the list of justified lines (a paragraph).
3388  static list<string>& Justify(const CTempString str,
3389  SIZE_TYPE width,
3390  list<string>& par,
3391  const CTempString* pfx = 0,
3392  const CTempString* pfx1 = 0);
3393 
3394  static list<string>& Justify(const CTempString str,
3395  SIZE_TYPE width,
3396  list<string>& par,
3397  const CTempString pfx,
3398  const CTempString* pfx1 = 0);
3399 
3400  static list<string>& Justify(const CTempString str,
3401  SIZE_TYPE width,
3402  list<string>& par,
3403  const CTempString pfx,
3404  const CTempString pfx1);
3405 
3406  /// Flags for Dedent() method
3408  fDedent_NormalizeEmptyLines = 1 << 0, ///< Each line containing only whitespaces will be normalized
3409  ///< to a single newline character in the output. Such lines
3410  ///< are excluded from detecting common whitespace prefix.
3411  // Next flags can be useful for processing RAW multi-line literals "R(...)"
3412  fDedent_SkipFirstLine = 1 << 1, ///< Ignore first line and skip it from the result.
3413  fDedent_SkipEmptyFirstLine = 1 << 2, ///< Ignore first line and skip it from the result, if it is empty only.
3414  };
3415  typedef int TDedentFlags; ///< Bitwise OR of EDedentFlags
3416 
3417  /// Dedent multi-line string, removing common whitespace prefix for each line.
3418  ///
3419  /// @param str
3420  /// String to be dedented.
3421  /// @param flags
3422  /// Optional flags to tune up how to dedent string.
3423  /// @return
3424  /// String with removed common whitespace indentation.
3425  /// @note
3426  /// Empty lines, and lines containing whitespaces only with fDedent_NormalizeEmptyLines flag,
3427  /// are not used in computing common whitespaces prefix.
3428  /// @note
3429  /// Assumes that whitespace prefixes are the same on each line, in other words,
3430  /// if a common prefix have a mix of spaces and tabulation characters, it should
3431  /// be the same for each line. Or this method can works incorrectly.
3432  /// Also, you can use Replace() first to replace tabulations and make whitespaces
3433  /// consistent across lines.
3434  /// @sa
3435  /// Replace, DedentR
3436  static string Dedent(const CTempString str, TDedentFlags flags = 0);
3437 
3438  /// Dedent multi-line string, removing common whitespace prefix for each line.
3439  ///
3440  /// Version for RAW multi-line literals "R(...)", embedded into the C++ code.
3441  /// @example
3442  ///
3443  /// make_request(NStr::DedentR(R"(
3444  /// {
3445  /// "param1": some_val,
3446  /// "param2": another_val,
3447  /// "param3": "These lines are easy-to-read ",
3448  /// "param4": "and don't interrupt the flow of indentation."
3449  /// }
3450  /// )");
3451  /// @sa
3452  /// Dedent
3453  static string DedentR(const CTempString str);
3454 
3455  /// Search for a field.
3456  ///
3457  /// @param str
3458  /// C or C++ string to search in.
3459  /// @param field_no
3460  /// Zero-based field number.
3461  /// @param delimiters
3462  /// A set of single-character delimiters.
3463  /// @param merge
3464  /// Whether to merge or not adjacent delimiters. Default: not to merge.
3465  /// @return
3466  /// Found field; or empty string if the required field is not found.
3467  /// @note
3468  /// Field 0 spans up to the first-found delimiter or the end-of-string.
3469  static string GetField(const CTempString str,
3470  size_t field_no,
3471  const CTempString delimiters,
3472  EMergeDelims merge = eNoMergeDelims);
3473 
3474  /// Search for a field.
3475  ///
3476  /// @param str
3477  /// C or C++ string to search in.
3478  /// @param field_no
3479  /// Zero-based field number.
3480  /// @param delimiter
3481  /// A single-character delimiter.
3482  /// @param merge
3483  /// Whether to merge or not adjacent delimiters. Default: not to merge.
3484  /// @return
3485  /// Found field; or empty string if the required field is not found.
3486  /// @note
3487  /// Field 0 spans up to the delimiter or the end-of-string.
3488  static string GetField(const CTempString str,
3489  size_t field_no,
3490  char delimiter,
3491  EMergeDelims merge = eNoMergeDelims);
3492 
3493  /// Search for a field.
3494  /// Avoid memory allocation at the expense of some usage safety.
3495  ///
3496  /// @param str
3497  /// C or C++ string to search in.
3498  /// @param field_no
3499  /// Zero-based field number.
3500  /// @param delimiters
3501  /// A set of single-character delimiters.
3502  /// @param merge
3503  /// Whether to merge or not adjacent delimiters. Default: not to merge.
3504  /// @return
3505  /// Found field; or empty string if the required field is not found.
3506  /// @note
3507  /// Field 0 spans up to the first-found delimiter or the end-of-string.
3508  /// @warning
3509  /// The return value stores a pointer to the input string 'str' so
3510  /// the return object validity time matches lifetime of the input 'str'.
3511  static
3512  CTempString GetField_Unsafe(const CTempString str,
3513  size_t field_no,
3514  const CTempString delimiters,
3515  EMergeDelims merge = eNoMergeDelims);
3516 
3517  /// Search for a field.
3518  /// Avoid memory allocation at the expense of some usage safety.
3519  ///
3520  /// @param str
3521  /// C or C++ string to search in.
3522  /// @param field_no
3523  /// Zero-based field number.
3524  /// @param delimiter
3525  /// A single-character delimiter.
3526  /// @param merge
3527  /// Whether to merge or not adjacent delimiters. Default: not to merge.
3528  /// @return
3529  /// Found field; or empty string if the required field is not found.
3530  /// @note
3531  /// Field 0 spans up to the delimiter or the end-of-string.
3532  /// @warning
3533  /// The return value stores a pointer to the input string 'str' so
3534  /// the return object validity time matches lifetime of the input 'str'.
3535  static
3536  CTempString GetField_Unsafe(const CTempString str,
3537  size_t field_no,
3538  char delimiter,
3539  EMergeDelims merge = eNoMergeDelims);
3540 
3541 private:
3542 // implementations
3543 
3544 // StringToNumeric
3545  static bool x_ReportLimitsError(const CTempString str, TStringToNumFlags flags);
3546 
3547  template< typename TNumeric, typename TSource>
3549  {
3551  return x_ReportLimitsError(str, flags);
3552  }
3553  return true;
3554  }
3555  template< typename TNumeric, typename TSource>
3557  {
3558  // dont use ::min() for float types, it returns positive value
3560  return x_ReportLimitsError(str, flags);
3561  }
3562  return true;
3563  }
3564 
3565  template <typename TNumeric>
3566  static typename enable_if< is_integral<TNumeric>::value && is_signed<TNumeric>::value && (sizeof(TNumeric) < sizeof(int)), TNumeric>::type
3568  {
3569  int n = StringToInt(str, flags, base);
3570  return x_VerifyIntLimits<TNumeric>(n, str, flags) ? (TNumeric)n : 0;
3571  }
3572  template <typename TNumeric>
3573  static typename enable_if< is_integral<TNumeric>::value && is_unsigned<TNumeric>::value && (sizeof(TNumeric) < sizeof(unsigned int)), TNumeric>::type
3575  {
3576  unsigned int n = StringToUInt(str, flags, base);
3577  return x_VerifyIntLimits<TNumeric>(n, str, flags) ? (TNumeric)n : 0;
3578  }
3579 
3580  template <typename TNumeric>
3581  static typename enable_if< is_integral<TNumeric>::value && is_signed<TNumeric>::value && (sizeof(TNumeric) == sizeof(int) && !is_same<TNumeric, long>::value), TNumeric>::type
3583  {
3584  return StringToInt(str, flags, base);
3585  }
3586  template <typename TNumeric>
3587  static typename enable_if< is_integral<TNumeric>::value && is_unsigned<TNumeric>::value && (sizeof(TNumeric) == sizeof(unsigned int) && !is_same<TNumeric, unsigned long>::value), TNumeric>::type
3589  {
3590  return StringToUInt(str, flags, base);
3591  }
3592  template <typename TNumeric>
3593  static typename enable_if< is_same<TNumeric, long>::value, TNumeric>::type
3595  {
3596  return StringToLong(str, flags, base);
3597  }
3598  template <typename TNumeric>
3601  {
3602  return StringToULong(str, flags, base);
3603  }
3604  template <typename TNumeric>
3605  static typename enable_if< is_integral<TNumeric>::value && is_signed<TNumeric>::value && (sizeof(TNumeric) == sizeof(Int8) && !is_same<TNumeric, long>::value), TNumeric>::type
3607  {
3608  return StringToInt8(str, flags, base);
3609  }
3610  template <typename TNumeric>
3611  static typename enable_if< is_integral<TNumeric>::value && is_unsigned<TNumeric>::value && (sizeof(TNumeric) == sizeof(Uint8) && !is_same<TNumeric, unsigned long>::value), TNumeric>::type
3613  {
3614  return StringToUInt8(str, flags, base);
3615  }
3616  template <typename TStrictId>
3617  static typename enable_if< is_integral<typename TStrictId::TId>::value && is_member_function_pointer<decltype(&TStrictId::Get)>::value, TStrictId>::type
3619  {
3620  return TStrictId(StringToNumeric<typename TStrictId::TId>(str, flags, base));
3621  }
3622 
3623  template <typename TNumeric>
3624  static typename enable_if< is_same<TNumeric, float>::value, TNumeric>::type
3626  {
3627  double n = StringToDouble(str, flags);
3628  return x_VerifyFloatLimits<TNumeric>(n, str, flags) ? (TNumeric)n : 0;
3629  }
3630  template <typename TNumeric>
3631  static typename enable_if< is_same<TNumeric, double>::value, TNumeric>::type
3633  {
3634  return StringToDouble(str, flags);
3635  }
3636 
3637  template <typename TNumeric>
3638  static typename enable_if< is_integral<TNumeric>::value && is_signed<TNumeric>::value && (sizeof(TNumeric) < sizeof(int)), bool>::type
3640  {
3641  int n = StringToInt(str, flags, base);
3642  *value = 0;
3643  if (( !n && errno ) || !x_VerifyIntLimits<TNumeric>(n, str, flags)) {
3644  return false;
3645  }
3646  *value = (TNumeric) n;
3647  return true;
3648  }
3649  template <typename TNumeric>
3650  static typename enable_if< is_integral<TNumeric>::value && is_unsigned<TNumeric>::value && (sizeof(TNumeric) < sizeof(unsigned int)), bool>::type
3652  {
3653  unsigned int n = StringToUInt(str, flags, base);
3654  *value = 0;
3655  if (( !n && errno ) || !x_VerifyIntLimits<TNumeric>(n, str, flags)) {
3656  return false;
3657  }
3658  *value = (TNumeric) n;
3659  return true;
3660  }
3661  template <typename TNumeric>
3662  static typename enable_if< is_integral<TNumeric>::value && is_signed<TNumeric>::value && (sizeof(TNumeric) == sizeof(int) && !is_same<TNumeric, long>::value), bool>::type
3664  {
3665  *value = StringToInt(str, flags, base);
3666  return (*value || !errno);
3667  }
3668  template <typename TNumeric>
3669  static typename enable_if< is_integral<TNumeric>::value && is_unsigned<TNumeric>::value && (sizeof(TNumeric) == sizeof(unsigned int) && !is_same<TNumeric, unsigned long>::value), bool>::type
3671  {
3672  *value = StringToUInt(str, flags, base);
3673  return (*value || !errno);
3674  }
3675  static bool
3677  {
3678  *value = StringToLong(str, flags, base);
3679  return (*value || !errno);
3680  }
3681  static bool
3682  x_StringToNumeric(const CTempString str, unsigned long* value, TStringToNumFlags flags, int base)
3683  {
3684  *value = StringToULong(str, flags, base);
3685  return (*value || !errno);
3686  }
3687  template <typename TNumeric>
3688  static typename enable_if< is_integral<TNumeric>::value && is_signed<TNumeric>::value && (sizeof(TNumeric) == sizeof(Int8) && !is_same<TNumeric, long>::value), bool>::type
3690  {
3691  *value = StringToInt8(str, flags, base);
3692  return (*value || !errno);
3693  }
3694  template <typename TNumeric>
3697  {
3698  *value = StringToUInt8(str, flags, base);
3699  return (*value || !errno);
3700  }
3701  static bool
3703  {
3704  double n = StringToDouble(str, flags);
3705  *value = 0;
3706  if (( !n && errno ) || !x_VerifyFloatLimits<float>(n, str, flags)) {
3707  return false;
3708  }
3709  *value = (float) n;
3710  return true;
3711  }
3712  static bool
3714  {
3715  *value = StringToDouble(str, flags);
3716  return (*value || !errno);
3717  }
3718  template <typename TStrictId>
3719  static typename enable_if< is_integral<typename TStrictId::TId>::value && is_member_function_pointer<decltype(&TStrictId::Get)>::value, bool>::type
3721  {
3722  return x_StringToNumeric(str, &value->Set(), flags, base);
3723  }
3724 
3725 // NumericToString
3726  template<typename TNumeric>
3727  static typename enable_if< is_integral<TNumeric>::value && is_signed<TNumeric>::value && (sizeof(TNumeric) <= sizeof(int) && !is_same<TNumeric, long>::value), void>::type
3728  x_NumericToString(string& out_str, TNumeric value, TNumToStringFlags flags, int base)
3729  {
3730  IntToString(out_str, value, flags, base);
3731  }
3732  template<typename TNumeric>
3733  static typename enable_if< is_integral<TNumeric>::value && is_unsigned<TNumeric>::value && (sizeof(TNumeric) <= sizeof(unsigned int) && !is_same<TNumeric, unsigned long>::value), void>::type
3734  x_NumericToString(string& out_str, TNumeric value, TNumToStringFlags flags, int base)
3735  {
3736  UIntToString(out_str, value, flags, base);
3737  }
3738  static void
3739  x_NumericToString(string& out_str, long value, TNumToStringFlags flags, int base)
3740  {
3741  LongToString(out_str, value, flags, base);
3742  }
3743  static void
3744  x_NumericToString(string& out_str, unsigned long value, TNumToStringFlags flags, int base)
3745  {
3746  ULongToString(out_str, value, flags, base);
3747  }
3748 #if NCBI_COMPILER_MSVC && (_MSC_VER < 1900)
3749  static void
3750  x_NumericToString(string& out_str, Int8 value, TNumToStringFlags flags, int base)
3751  {
3752  Int8ToString(out_str, value, flags, base);
3753  }
3754  static void
3755  x_NumericToString(string& out_str, Uint8 value, TNumToStringFlags flags, int base)
3756  {
3757  UInt8ToString(out_str, value, flags, base);
3758  }
3759 #endif
3760  template<typename TNumeric>
3761  static typename enable_if< is_integral<TNumeric>::value && is_signed<TNumeric>::value && (sizeof(TNumeric) == sizeof(Int8) && !is_same<TNumeric, long>::value), void>::type
3762  x_NumericToString(string& out_str, TNumeric value, TNumToStringFlags flags, int base)
3763  {
3764  Int8ToString(out_str, value, flags, base);
3765  }
3766  template<typename TNumeric>
3768  x_NumericToString(string& out_str, TNumeric value, TNumToStringFlags flags, int base)
3769  {
3770  UInt8ToString(out_str, value, flags, base);
3771  }
3772  template<typename TNumeric>
3774  x_NumericToString(string& out_str, TNumeric value, TNumToStringFlags flags, int /*base*/)
3775  {
3776  DoubleToString(out_str, value, -1, flags);
3777  }
3778  template <typename TStrictId>
3779  static typename enable_if< is_integral<typename TStrictId::TId>::value && is_member_function_pointer<decltype(&TStrictId::Get)>::value, void>::type
3780  x_NumericToString(string& out_str, TStrictId value, TNumToStringFlags flags, int base)
3781  {
3782  return x_NumericToString(out_str, value.Get(), flags, base);
3783  }
3784 
3785 
3786 // Join
3787  template<typename TIterator>
3788  static string xx_Join( TIterator from, TIterator to, const CTempString& delim);
3789 
3790  template<typename TIterator>
3793  x_Join( TIterator from, TIterator to, const CTempString& delim)
3794  {
3795  return TransformJoin(from, to, delim, [](const typename TIterator::value_type& i){ return i;});
3796  }
3797 
3798  template<typename TIterator>
3801  x_Join( TIterator from, TIterator to, const CTempString& delim)
3802  {
3803  return xx_Join(from, to, delim);
3804  }
3805 
3806  template<typename TValue>
3808  x_Join( TValue* from, TValue* to, const CTempString& delim)
3809  {
3810  return xx_Join(from, to, delim);
3811  }
3812 
3813  template<typename TIterator>
3816  x_Join( TIterator from, TIterator to, const CTempString& delim)
3817  {
3818  return TransformJoin( from, to, delim, [](const typename TIterator::value_type& i){ return NumericToString(i);});
3819  }
3820 
3821  template<typename TValue>
3822  static typename enable_if<is_arithmetic<TValue>::value, string>::type
3823  x_Join( TValue* from, TValue* to, const CTempString& delim)
3824  {
3825  return TransformJoin( from, to, delim, [](const TValue& i){ return NumericToString(i);});
3826  }
3827 }; // class NStr
3828 
3829 
3830 
3831 /////////////////////////////////////////////////////////////////////////////
3832 ///
3833 
3834 
3835 #define NCBITOOLKIT_USE_LONG_UCS4 (SIZEOF_LONG == 4)
3836 #if NCBITOOLKIT_USE_LONG_UCS4
3837 /// UCS-4 character
3838 typedef unsigned long TCharUCS4;
3839 /// UCS-4 string
3840 typedef basic_string<TCharUCS4> TStringUCS4;
3841 #else
3844 #endif
3845 
3846 /// Type for character in UCS-2 encoding
3848 /// Type for string in UCS-2 encoding
3849 typedef basic_string<TCharUCS2> TStringUCS2;
3850 
3851 
3852 /// Operator for writing TStringUCS2 to stream.
3853 /// Operator is needed for using in SDBAPI.
3855 {
3856  os.write((const char*)str.data(), str.size() * sizeof(TCharUCS2));
3857  return os;
3858 }
3859 
3860 
3861 
3862 /////////////////////////////////////////////////////////////////////////////
3863 ///
3864 /// CUtf8 --
3865 ///
3866 /// Utility class to handle strings in UTF8 encoding.
3867 /// Can convert data to and from the following encodings:
3868 /// ISO 8859-1 (Latin1)
3869 /// Microsoft Windows code page 1252
3870 /// UCS-2, UCS-4
3871 
3873 {
3874 public:
3875  /// How to verify character encoding of the source data
3876  enum EValidate {
3878  eValidate
3879  };
3880 
3881  /// Convert into UTF8 from a C/C++ string
3882  ///
3883  /// @param src
3884  /// Source string
3885  /// @param encoding
3886  /// Character encoding of the source string
3887  /// @param validate
3888  /// Verify the character encoding of the source
3889  static CStringUTF8 AsUTF8(const CTempString& src,
3890  EEncoding encoding,
3891  EValidate validate = eNoValidate)
3892  {
3893  CStringUTF8 u8;
3894  return x_Append(u8,src,encoding,validate);
3895  }
3896 
3897 #if defined(HAVE_WSTRING)
3898  /// Convert into UTF8 from a C/C++ string
3899  ///
3900  /// @param src
3901  /// Source string
3902  /// @param lcl
3903  /// String locale
3904  static CStringUTF8 AsUTF8(const CTempString& src, const locale& lcl)
3905  {
3906  CStringUTF8 u8;
3907  return x_Append(u8,src,lcl);
3908  }
3909 #endif
3910 
3911  /// Convert into UTF8 from a Unicode C++ string
3912  ///
3913  /// @param src
3914  /// Source string
3915  /// @attention
3916  /// Only for TStringUnicode, TStringUCS4, TStringUCS2, wstring types
3917  template <typename TChar>
3918  static typename enable_if< is_integral<TChar>::value && (1 < sizeof(TChar)), CStringUTF8>::type
3919  AsUTF8(const basic_string<TChar>& src)
3920  {
3921  CStringUTF8 u8;
3922  return x_Append(u8, src.data(), src.size());
3923  }
3924 
3925  /// Convert into UTF8 from a Unicode character buffer
3926  ///
3927  /// @param src
3928  /// Source character buffer
3929  /// @param tchar_count
3930  /// Number of characters in the buffer;
3931  /// If it equals to NPOS, buffer is assumed to be zero-terminated
3932  template <typename TChar>
3933  static typename enable_if< is_integral<TChar>::value && (1 < sizeof(TChar)), CStringUTF8>::type
3934  AsUTF8(const TChar* src, SIZE_TYPE tchar_count = NPOS)
3935  {
3936  CStringUTF8 u8;
3937  return x_Append(u8, src, tchar_count);
3938  }
3939 
3940  /// Convert Unicode C++ string into UTF8 and append it to existing string
3941  ///
3942  /// @param dest
3943  /// Existing UTF8 string
3944  /// @param src
3945  /// Source Unicode string
3946  /// return
3947  /// reference to modified dest string
3948  template <typename TChar>
3949  static typename enable_if< is_integral<TChar>::value && (1 < sizeof(TChar)), CStringUTF8& >::type
3950  AppendAsUTF8(CStringUTF8& dest, const basic_string<TChar>& src)
3951  {
3952  return x_Append(dest, src.data(), src.size());
3953  }
3954 
3955  /// Convert Unicode character buffer into UTF8 and append it to existing string
3956  ///
3957  /// @param dest
3958  /// Existing UTF8 string
3959  /// @param src
3960  /// Source Unicode character buffer
3961  /// @param tchar_count
3962  /// Number of characters in the buffer;
3963  /// If it equals to NPOS, buffer is assumed to be zero-terminated
3964  /// return
3965  /// reference to modified dest string
3966  template <typename TChar>
3967  static typename enable_if< is_integral<TChar>::value && (1 < sizeof(TChar)), CStringUTF8& >::type
3968  AppendAsUTF8(CStringUTF8& dest, const TChar* src, SIZE_TYPE tchar_count = NPOS)
3969  {
3970  return x_Append(dest, src, tchar_count);
3971  }
3972 
3973  /// Convert Unicode symbol into UTF8 and append it to existing string
3974  ///
3975  /// @param dest
3976  /// Existing UTF8 string
3977  /// @param ch
3978  /// Unicode symbol
3979  /// return
3980  /// reference to modified dest string
3981  template <typename TChar>
3982  static typename enable_if< is_integral<TChar>::value && (1 < sizeof(TChar)), CStringUTF8& >::type
3984  {
3985  return x_Append(dest, &ch, 1);
3986  }
3987 
3988  /// Convert non-Unicode C++ string into UTF8 and append it to existing string
3989  ///
3990  /// @param dest
3991  /// Existing UTF8 string
3992  /// @param src
3993  /// Source string
3994  /// @param encoding
3995  /// Character encoding of the source string
3996  /// @param validate
3997  /// Verify the character encoding of the source
3998  /// return
3999  /// reference to modified dest string
4001  const CTempString& src,
4002  EEncoding encoding,
4003  EValidate validate = eNoValidate)
4004  {
4005  return x_Append(dest,src,encoding,validate);
4006  }
4007 
4008 #if defined(HAVE_WSTRING)
4009  /// Convert non-Unicode C++ string into UTF8 and append it to existing string
4010  ///
4011  /// @param dest
4012  /// Existing UTF8 string
4013  /// @param src
4014  /// Source string
4015  /// @param lcl
4016  /// Source string locale
4017  /// return
4018  /// reference to modified dest string
4020  const CTempString& src,
4021  const locale& lcl)
4022  {
4023  return x_Append(dest,src,lcl);
4024  }
4025 #endif
4026 
4027  /// Convert non-Unicode character into UTF8 and append it to existing string
4028  ///
4029  /// @param dest
4030  /// Existing UTF8 string
4031  /// @param ch
4032  /// Character
4033  /// @param encoding
4034  /// Character encoding
4035  /// @param validate
4036  /// Verify the character encoding of the source
4037  /// return
4038  /// reference to modified dest string
4040  char ch,
4041  EEncoding encoding,
4042  EValidate validate = eNoValidate)
4043  {
4044  return x_Append(dest,CTempString(&ch,1),encoding,validate);
4045  }
4046 
4047 #if defined(HAVE_WSTRING)
4048  /// Convert non-Unicode character into UTF8 and append it to existing string
4049  ///
4050  /// @param dest
4051  /// Existing UTF8 string
4052  /// @param ch
4053  /// Character
4054  /// @param lcl
4055  /// Character locale
4056  /// return
4057  /// reference to modified dest string
4059  char ch,
4060  const locale& lcl)
4061  {
4062  return x_Append(dest,CTempString(&ch,1),lcl);
4063  }
4064 #endif
4065 
4066  /// Convert UTF8 string into a single-byte character representation
4067  ///
4068  /// Can throw a CStringException if the conversion is impossible
4069  /// or the string has invalid UTF-8 encoding.
4070  ///
4071  /// @param src
4072  /// Source UTF8 string
4073  /// @param encoding
4074  /// Encoding of the result
4075  /// @param substitute_on_error
4076  /// If the conversion is impossible, append the provided string
4077  /// or, if substitute_on_error equals 0, throw an exception
4078  /// @param validate
4079  /// Verify UTF8 character encoding of the source
4080  /// @return
4081  /// C++ string
4082  static string AsSingleByteString
4083  (const CTempString& src, EEncoding encoding,
4084  const char* substitute_on_error = 0, EValidate validate = eNoValidate);
4085 
4086 #if defined(HAVE_WSTRING)
4087  static string AsSingleByteString
4088  (const CTempString& src, const locale& lcl,
4089  const char* substitute_on_error = 0, EValidate validate = eNoValidate);
4090 #endif
4091 
4092  /// Convert UTF8 string into Unicode
4093  ///
4094  /// Can throw a CStringException if the conversion is impossible
4095  /// or the string has invalid UTF-8 encoding.
4096  ///
4097  /// @param src
4098  /// Source UTF8 string
4099  /// @param substitute_on_error
4100  /// If the conversion is impossible, append the provided string
4101  /// or, if substitute_on_error equals 0, throw an exception
4102  /// @param validate
4103  /// Verify UTF8 character encoding of the source
4104  /// @attention
4105  /// Only for TStringUnicode, TStringUCS4, TStringUCS2, wstring types
4106  template <typename TChar>
4107  static typename enable_if< is_integral<TChar>::value && (1 < sizeof(TChar)), basic_string<TChar> >::type
4108  AsBasicString(const CTempString& src, const TChar* substitute_on_error, EValidate validate = eNoValidate)
4109  {
4110  return x_AsBasicString(src,substitute_on_error,validate);
4111  }
4112 
4113  template <typename TChar>
4114  static typename enable_if< is_integral<TChar>::value && (1 < sizeof(TChar)), basic_string<TChar> >::type
4116  {
4117  return x_AsBasicString<TChar>(src,nullptr,eNoValidate);
4118  }
4119 
4120  /// Get the number of symbols (code points) in UTF8 string
4121  ///
4122  /// @param src
4123  /// Source UTF8 string
4124  /// @return
4125  /// Number of symbols (code points)
4126  static SIZE_TYPE GetSymbolCount(const CTempString& src);
4127 
4128  /// Get the number of valid UTF-8 symbols (code points) in buffer
4129  ///
4130  /// @param src
4131  /// Character buffer
4132  /// @return
4133  /// Number of valid symbols (no exception thrown)
4134  static SIZE_TYPE GetValidSymbolCount(const CTempString& src);
4135 
4136  /// Get the number of valid UTF-8 bytes (code units) in buffer
4137  ///
4138  /// @param src
4139  /// Character buffer
4140  /// @return
4141  /// Number of valid bytes (no exception thrown)
4142  static SIZE_TYPE GetValidBytesCount(const CTempString& src);
4143 
4144  /// Check buffer for presence of UTF-8 byte sequence and return length of first symbol
4145  ///
4146  /// @param src
4147  /// Character buffer
4148  /// @return
4149  /// Number of bytes
4150  static SIZE_TYPE EvaluateSymbolLength(const CTempString& src);
4151 
4152  /// Check that the character is valid first byte of an UTF8 byte sequence
4153  ///
4154  /// @param ch
4155  /// Character
4156  /// @param more
4157  /// Number of additional bytes to expect
4158  /// @return
4159  /// true, if this is a valid first byte
4160  static bool EvaluateFirst(char ch, SIZE_TYPE& more) {
4161  return x_EvalFirst(ch, more);
4162  }
4163 
4164  /// Check that the character is valid continuation byte of an UTF8 byte sequence
4165  ///
4166  /// @param ch
4167  /// Character
4168  /// @return
4169  /// true, if this is a valid byte
4170  static bool EvaluateNext(char ch) {
4171  return x_EvalNext(ch);
4172  }
4173 
4174  /// Check the encoding of the C/C++ string
4175  ///
4176  /// Check that the encoding of the source is the same, or
4177  /// is compatible with the specified one
4178  /// @param src
4179  /// Source string
4180  /// @param encoding
4181  /// Character encoding form to check against
4182  /// @return
4183  /// Boolean result: encoding is same or compatible
4184  static bool MatchEncoding(const CTempString& src, EEncoding encoding);
4185 
4186  /// Guess the encoding of the C/C++ string
4187  ///
4188  /// It can distinguish between UTF-8, Latin1, and Win1252 only
4189  /// @param src
4190  /// Character buffer
4191  /// @return
4192  /// Encoding as guessed; eEncoding_Unknown if cannot guess
4193  static EEncoding GuessEncoding(const CTempString& src);
4194 
4195  /// Give Encoding name as string
4196  ///
4197  /// @param encoding
4198  /// EEncoding enum. (Throw CStringException if passed eEncoding_Unknown.)
4199  /// @return
4200  /// Encoding name
4201  static string EncodingToString(EEncoding encoding);
4202 
4203  /// Convert encoding name into EEncoding enum, taking into account synonyms
4204  /// as per http://www.iana.org/assignments/character-sets
4205  ///
4206  /// @param encoding_name
4207  /// Name of the encoding
4208  /// @return
4209  /// EEncoding enum; eEncoding_Unknown for unsupported encodings
4210  static EEncoding StringToEncoding(const CTempString& encoding_name);
4211 
4212  /// Convert encoded character into Unicode
4213  ///
4214  /// @param ch
4215  /// Encoded character
4216  /// @param encoding
4217  /// Character encoding
4218  /// @return
4219  /// Unicode code point (symbol)
4220  static TUnicodeSymbol CharToSymbol(char ch, EEncoding encoding);
4221 
4222 #if defined(HAVE_WSTRING)
4223  /// Convert encoded character into Unicode
4224  ///
4225  /// @param ch
4226  /// Encoded character
4227  /// @param lcl
4228  /// Character locale
4229  /// @return
4230  /// Unicode code point (symbol)
4231  static TUnicodeSymbol CharToSymbol(char ch, const locale& lcl);
4232 #endif
4233 
4234  /// Convert Unicode code point into encoded character
4235  ///
4236  /// @param sym
4237  /// Unicode code point (symbol)
4238  /// @param encoding
4239  /// Character encoding
4240  /// @return
4241  /// Encoded character
4242  static char SymbolToChar(TUnicodeSymbol sym, EEncoding encoding);
4243 
4244 #if defined(HAVE_WSTRING)
4245  /// Convert Unicode code point into encoded character
4246  ///
4247  /// @param sym
4248  /// Unicode code point (symbol)
4249  /// @param lcl
4250  /// Character locale
4251  /// @return
4252  /// Encoded character
4253  static char SymbolToChar(TUnicodeSymbol sym, const locale& lcl);
4254 #endif
4255 
4256  /// Determines if a symbol is whitespace
4257  /// per http://unicode.org/charts/uca/chart_Whitespace.html
4258  ///
4259  /// @param sym
4260  /// Unicode code point (symbol)
4261  /// @sa
4262  /// TruncateSpacesInPlace, TruncateSpaces_Unsafe, TruncateSpaces
4263  static bool IsWhiteSpace(TUnicodeSymbol sym);
4264 
4265  /// Truncate spaces in the string (in-place)
4266  ///
4267  /// @param src
4268  /// UTF8 string
4269  /// @param side
4270  /// Which end of the string to truncate spaces from. Default is to
4271  /// truncate spaces from both ends.
4272  /// @return
4273  /// Reference to src
4274  /// @sa
4275  /// IsWhiteSpace, TruncateSpaces_Unsafe, TruncateSpaces
4276  static CStringUTF8& TruncateSpacesInPlace
4278 
4279  /// Truncate spaces in the string
4280  ///
4281  /// @param str
4282  /// Source string, in UTF8 encoding
4283  /// @param side
4284  /// Which end of the string to truncate spaces from. Default is to
4285  /// truncate spaces from both ends.
4286  /// @sa
4287  /// IsWhiteSpace, TruncateSpacesInPlace, TruncateSpaces_Unsafe
4288  static CStringUTF8 TruncateSpaces
4289  (const CTempString& str, NStr::ETrunc side = NStr::eTrunc_Both);
4290 
4291  /// Truncate spaces in the string
4292  ///
4293  /// @param str
4294  /// Source string, in UTF8 encoding
4295  /// @param side
4296  /// Which end of the string to truncate spaces from. Default is to
4297  /// truncate spaces from both ends.
4298  /// @attention
4299  /// The lifespan of the result string is the same as one of the source.
4300  /// So, for example, if the source is temporary string, then the result
4301  /// will be invalid right away (will point to already released memory).
4302  /// @sa
4303  /// IsWhiteSpace, TruncateSpacesInPlace, TruncateSpaces
4304  static CTempString TruncateSpaces_Unsafe
4305  (const CTempString& str, NStr::ETrunc side = NStr::eTrunc_Both);
4306 
4307  /// Convert sequence of UTF8 code units into Unicode code point
4308  ///
4309  /// @param src
4310  /// Zero-terminated buffer, in UTF8 encoding
4311  /// @return
4312  /// Unicode code point
4313  static TUnicodeSymbol Decode(const char*& src);
4314 
4315 #ifndef NCBI_COMPILER_WORKSHOP
4316  /// Convert sequence of UTF8 code units into Unicode code point
4317  ///
4318  /// @param src
4319  /// C++ string iterator
4320  /// @return
4321  /// Unicode code point
4322  static TUnicodeSymbol Decode(string::const_iterator& src);
4323 #endif
4324 
4325  /// Begin converting first character of UTF8 sequence into Unicode
4326  ///
4327  /// @param ch
4328  /// Character
4329  /// @param more
4330  /// If the character is valid, - how many more characters to expect
4331  /// @return
4332  /// Part of Unicode code point. Zero if the character is invalid.
4333  static TUnicodeSymbol DecodeFirst(char ch, SIZE_TYPE& more);
4334 
4335  /// Convert next character of UTF8 sequence into Unicode
4336  ///
4337  /// @param ch
4338  /// Character
4339  /// @param chU
4340  /// Incomplete Unicode code point
4341  /// @return
4342  /// Accumulated Unicode code point. Zero if the character is invalid.
4343  static TUnicodeSymbol DecodeNext(TUnicodeSymbol chU, char ch);
4344 
4345 private:
4346  static void x_Validate(const CTempString& str);
4347 
4348  static SIZE_TYPE x_GetValidSymbolCount
4349  (const CTempString& src, CTempString::const_iterator& err);
4350 
4351  static CStringUTF8& x_AppendChar(CStringUTF8& u8str, TUnicodeSymbol ch);
4352 
4353  static CStringUTF8& x_Append(CStringUTF8& u8str, const CTempString& src,
4354  EEncoding encoding, EValidate validate);
4355 #if defined(HAVE_WSTRING)
4356  static CStringUTF8& x_Append(CStringUTF8& u8str, const CTempString& src, const locale& lcl);
4357 #endif
4358  template <typename TChar>
4359  static bool x_TCharToUnicodeSymbol(TUnicodeSymbol& u, const TChar* src);
4360  template <typename TChar>
4361  static CStringUTF8& x_Append(CStringUTF8& u8str, const TChar* src, SIZE_TYPE tchar_count);
4362 
4363  template <typename TChar>
4364  static basic_string<TChar> x_AsBasicString
4365  (const CTempString& src,
4366  const TChar* substitute_on_error, EValidate validate);
4367 
4368  template <typename TIterator>
4369  static TUnicodeSymbol x_Decode(TIterator& src);
4370 
4371  static SIZE_TYPE x_BytesNeeded(TUnicodeSymbol ch);
4372  static bool x_EvalFirst(char ch, SIZE_TYPE& more);
4373  static bool x_EvalNext(char ch);
4374 
4375  // returns part of the string around an error in Utf8 encoding
4376  static CTempString x_GetErrorFragment(const CTempString& src);
4377 
4379 };
4380 
4381 // deprecated CStringUTF8 is there
4383 
4384 
4385 
4386 /////////////////////////////////////////////////////////////////////////////
4387 ///
4388 /// CParseTemplException --
4389 ///
4390 /// Define template class for parsing exception. This class is used to define
4391 /// exceptions for complex parsing tasks and includes an additional m_Pos
4392 /// data member. The constructor requires that an additional positional
4393 /// parameter be supplied along with the description message.
4394 
4395 template <class TBase>
4397 {
4398 public:
4399  /// Error types that for exception class.
4400  enum EErrCode {
4401  eErr ///< Generic error
4402  };
4403 
4404  /// Translate from the error code value to its string representation.
4405  virtual const char* GetErrCodeString(void) const override
4406  {
4407  switch (GetErrCode()) {
4408  case eErr: return "eErr";
4409  default: return CException::GetErrCodeString();
4410  }
4411  }
4412 
4413  /// Constructor.
4414  ///
4415  /// Report "pos" along with "what".
4417  const CException* prev_exception,
4418  EErrCode err_code,const string& message,
4419  string::size_type pos, EDiagSev severity = eDiag_Error)
4420  : TBase(info, prev_exception, message, severity, 0), m_Pos(pos)
4421  {
4422  this->x_Init(info,
4423  string("{") + NStr::SizetToString(m_Pos) +
4424  "} " + message,
4425  prev_exception,
4426  severity);
4427  this->x_InitErrCode((CException::EErrCode) err_code);
4428  }
4429 
4430  /// Constructor.
4432  : TBase(other)
4433  {
4434  m_Pos = other.m_Pos;
4435  this->x_Assign(other);
4436  }
4437 
4438  /// Destructor.
4439  virtual ~CParseTemplException(void) noexcept {}
4440 
4441  /// Report error position.
4442  virtual void ReportExtra(ostream& out) const override
4443  {
4444  out << "m_Pos = " << (unsigned long)m_Pos;
4445  }
4446 
4447  // Attributes.
4448 
4449  /// Get exception class type.
4450  virtual const char* GetType(void) const override
4451  { return "CParseTemplException"; }
4452 
4453  typedef int TErrCode;
4454  /// Get error code.
4455  TErrCode GetErrCode(void) const
4456  {
4457  return typeid(*this) == typeid(CParseTemplException<TBase>) ?
4458  (TErrCode) this->x_GetErrCode() :
4460  }
4461 
4462  /// Get error position.
4463  string::size_type GetPos(void) const noexcept { return m_Pos; }
4464 
4465 protected:
4467  const CException* prev_exception,
4468  const string& message,
4469  string::size_type pos, EDiagSev severity, CException::TFlags flags)
4470  : TBase(info, prev_exception, message, severity, flags), m_Pos(pos)
4471  {
4472  this->x_Init(info,
4473  string("{") + NStr::SizetToString(m_Pos) +
4474  "} " + message,
4475  prev_exception,
4476  severity);
4477  }
4478  /// Constructor.
4480  {
4481  m_Pos = 0;
4482  }
4483 
4484  /// Helper clone method.
4485  virtual const CException* x_Clone(void) const override
4486  {
4487  return new CParseTemplException<TBase>(*this);
4488  }
4489 
4490 private:
4491  string::size_type m_Pos; ///< Error position
4492 };
4493 
4494 
4495 /////////////////////////////////////////////////////////////////////////////
4496 ///
4497 /// CStringException --
4498 ///
4499 /// Define exceptions generated by string classes.
4500 ///
4501 /// CStringException inherits its basic functionality from
4502 /// CParseTemplException<CCoreException> and defines additional error codes
4503 /// for string parsing.
4504 
4506 {
4507 public:
4508  /// Error types that string classes can generate.
4509  enum EErrCode {
4510  eConvert, ///< Failure to convert string
4511  eBadArgs, ///< Bad arguments to string methods
4512  eFormat ///< Wrong format for any input to string methods
4513  };
4514 
4515  /// Translate from the error code value to its string representation.
4516  virtual const char* GetErrCodeString(void) const override;
4517 
4518  // Standard exception boilerplate code.
4520  CParseTemplException<CCoreException>, std::string::size_type);
4521 };
4522 
4523 
4524 
4525 /////////////////////////////////////////////////////////////////////////////
4526 ///
4527 /// CStringPairsParser --
4528 ///
4529 /// Base class for parsing a string to a set of name-value pairs.
4530 
4531 
4532 /// Decoder interface. Names and values can be decoded with different rules.
4534 {
4535 public:
4536  /// Type of string to be decoded
4539  eValue
4540  };
4541  /// Decode the string. Must throw CStringException if the source string
4542  /// is not valid.
4543  virtual string Decode(const CTempString src, EStringType stype) const = 0;
4544  virtual ~IStringDecoder(void) {}
4545 };
4546 
4547 
4548 /// Encoder interface. Names and values can be encoded with different rules.
4550 {
4551 public:
4552  /// Type of string to be decoded
4555  eValue
4556  };
4557  /// Encode the string.
4558  virtual string Encode(const CTempString src, EStringType stype) const = 0;
4559  virtual ~IStringEncoder(void) {}
4560 };
4561 
4562 
4563 /// URL-decoder for string pairs parser
4565 {
4566 public:
4568 
4569  virtual string Decode(const CTempString src, EStringType stype) const;
4570 
4571 private:
4573 };
4574 
4575 
4576 /// URL-encoder for string pairs parser
4578 {
4579 public:
4581 
4582  virtual string Encode(const CTempString src, EStringType stype) const;
4583 
4584 private:
4586 };
4587 
4588 
4589 /// Template for parsing string into pairs of name and value or merging
4590 /// them back into a single string.
4591 /// The container class must hold pairs of strings (pair<string, string>).
4592 template<class TContainer>
4594 {
4595 public:
4596  typedef TContainer TStrPairs;
4597  /// The container's value type must be pair<string, string>
4598  /// or a compatible type.
4600 
4601  /// Create parser with the specified decoder/encoder and default separators.
4602  ///
4603  /// @param decoder
4604  /// String decoder (Url, Xml etc.)
4605  /// @param own_decoder
4606  /// Decoder ownership flag
4607  /// @param decoder
4608  /// String encoder (Url, Xml etc.), optional
4609  /// @param own_encoder
4610  /// Encoder ownership flag, optional
4612  EOwnership own_decoder = eTakeOwnership,
4613  IStringEncoder* encoder = NULL,
4614  EOwnership own_encoder = eTakeOwnership)
4615  : m_ArgSep("&"),
4616  m_ValSep("="),
4617  m_Decoder(decoder, own_decoder),
4618  m_Encoder(encoder, own_encoder)
4619  {
4620  }
4621 
4622  /// Create parser with the specified parameters.
4623  ///
4624  /// @param arg_sep
4625  /// Separator between name+value pairs
4626  /// @param val_sep
4627  /// Separator between name and value
4628  /// @param decoder
4629  /// String decoder (Url, Xml etc.)
4630  /// @param own_decoder
4631  /// Decoder ownership flag
4632  /// @param encoder
4633  /// String encoder (Url, Xml etc.)
4634  /// @param own_encoder
4635  /// Encoder ownership flag
4636  CStringPairs(const CTempString arg_sep,
4637  const CTempString val_sep,
4638  IStringDecoder* decoder = NULL,
4639  EOwnership own_decoder = eTakeOwnership,
4640  IStringEncoder* encoder = NULL,
4641  EOwnership own_encoder = eTakeOwnership)
4642  : m_ArgSep(arg_sep),
4643  m_ValSep(val_sep),
4644  m_Decoder(decoder, own_decoder),
4645  m_Encoder(encoder, own_encoder)
4646  {
4647  }
4648 
4649  /// Create parser with the selected URL-encoding/decoding options
4650  /// and default separators.
4651  ///
4652  /// @param decode_flag
4653  /// URL-decoding flag
4654  /// @param encode_flag
4655  /// URL-encoding flag
4657  NStr::EUrlEncode encode_flag)
4658  : m_ArgSep("&"),
4659  m_ValSep("="),
4660  m_Decoder(new CStringDecoder_Url(decode_flag), eTakeOwnership),
4661  m_Encoder(new CStringEncoder_Url(encode_flag), eTakeOwnership)
4662  {
4663  }
4664 
4665  virtual ~CStringPairs(void) {}
4666 
4667  /// Set string decoder.
4668  ///
4669  /// @param decoder
4670  /// String decoder (Url, Xml etc.)
4671  /// @param own
4672  /// Decoder ownership flag
4674  { m_Decoder.reset(decoder, own); }
4675  /// Get decoder or NULL. Does not affect decoder ownership.
4677 
4678  /// Set string encoder.
4679  ///
4680  /// @param encoder
4681  /// String encoder (Url, Xml etc.)
4682  /// @param own
4683  /// Encoder ownership flag
4685  { m_Encoder.reset(encoder, own); }
4686  /// Get encoder or NULL. Does not affect encoder ownership.
4688 
4689  /// Parse the string.
4690  ///
4691  /// @param str
4692  /// String to parse. The parser assumes the string is formatted like
4693  /// "name1<valsep>value1<argsep>name2<valsep>value2...". Each name and
4694  /// value is passed to the decoder (if not NULL) before storing the pair.
4695  /// @param merge_argsep
4696  /// Flag for merging separators between pairs. By default the separators
4697  /// are merged to prevent pairs where both name and value are empty.
4698  void Parse(const CTempString str,
4699  NStr::EMergeDelims merge_argsep = NStr::eMergeDelims)
4700  {
4702  m_Decoder.get(), eNoOwnership, merge_argsep);
4703  }
4704 
4705  /// Parse the string using the provided decoder, put data into the
4706  /// container.
4707  ///
4708  /// @param pairs
4709  /// Container to be filled with the parsed name/value pairs
4710  /// @param str
4711  /// String to parse. The parser assumes the string is formatted like
4712  /// "name1<valsep>value1<argsep>name2<valsep>value2...". Each name and
4713  /// value is passed to the decoder (if not NULL) before storing the pair.
4714  /// @param decoder
4715  /// String decoder (Url, Xml etc.)
4716  /// @param own
4717  /// Flag indicating if the decoder must be deleted by the function.
4718  /// @param merge_argsep
4719  /// Flag for merging separators between pairs. By default the separators
4720  /// are merged to prevent pairs where both name and value are empty.
4721  static void Parse(TStrPairs& pairs,
4722  const CTempString str,
4723  const CTempString arg_sep,
4724  const CTempString val_sep,
4725  IStringDecoder* decoder = NULL,
4726  EOwnership own = eTakeOwnership,
4727  NStr::EMergeDelims merge_argsep = NStr::eMergeDelims)
4728  {
4729  AutoPtr<IStringDecoder> decoder_guard(decoder, own);
4730  list<string> lst;
4731  NStr::Split(str, arg_sep, lst, (NStr::TSplitFlags)merge_argsep);
4732  pairs.clear();
4733  ITERATE(list<string>, it, lst) {
4734  string name, val;
4735  NStr::SplitInTwo(*it, val_sep, name, val);
4736  if ( decoder ) {
4737  try {
4738  name = decoder->Decode(name, IStringDecoder::eName);
4739  val = decoder->Decode(val, IStringDecoder::eValue);
4740  }
4741  catch (const CStringException&) {
4742  // Discard all data
4743  pairs.clear();
4744  throw;
4745  }
4746  }
4747  pairs.insert(pairs.end(), TStrPair(name, val));
4748  }
4749  }
4750 
4751  /// Merge name-value pairs into a single string using the currently set
4752  /// separators and the provided encoder if any.
4753  string Merge(void) const
4754  {
4755  return Merge(m_Data, m_ArgSep, m_ValSep,
4757  }
4758 
4759  /// Merge name-value pairs from the provided container, separators
4760  /// and encoder. Delete the encoder if the ownership flag allows.
4761  ///
4762  /// @param pairs
4763  /// Container with the name/value pairs to be merged.
4764  /// @param arg_sep
4765  /// Separator to be inserted between pairs.
4766  /// @param val_sep
4767  /// Separator to be inserted between name and value.
4768  /// @param encoder
4769  /// String encoder (Url, Xml etc.)
4770  /// @param own
4771  /// Flag indicating if the encoder must be deleted by the function.
4772  static string Merge(const TStrPairs& pairs,
4773  const string& arg_sep,
4774  const string& val_sep,
4775  IStringEncoder* encoder = NULL,
4776  EOwnership own = eTakeOwnership)
4777  {
4778  AutoPtr<IStringEncoder> encoder_guard(encoder, own);
4779  string ret;
4780  ITERATE(typename TStrPairs, it, pairs) {
4781  if ( !ret.empty() ) {
4782  ret += arg_sep;
4783  }
4784  if ( encoder ) {
4785  ret += encoder->Encode(it->first, IStringEncoder::eName) +
4786  val_sep +
4787  encoder->Encode(it->second, IStringEncoder::eValue);
4788  }
4789  else {
4790  ret += it->first + val_sep + it->second;
4791  }
4792  }
4793  return ret;
4794  }
4795 
4796  /// Read data
4797  const TStrPairs& GetPairs(void) const { return m_Data; }
4798  /// Get non-const data
4799  TStrPairs& GetPairs(void) { return m_Data; }
4800 
4801 private:
4802  string m_ArgSep; // Separator between name+value pairs ("&")
4803  string m_ValSep; // Separator between name and value ("=")
4804  AutoPtr<IStringDecoder> m_Decoder; // String decoder (Url, Xml etc.)
4805  AutoPtr<IStringEncoder> m_Encoder; // String encoder (Url, Xml etc.)
4806  TStrPairs m_Data; // Parsed data
4807 };
4808 
4809 
4810 typedef vector<pair<string, string> > TStringPairsVector;
4812 
4813 
4814 /////////////////////////////////////////////////////////////////////////////
4815 ///
4816 /// CEncodedString --
4817 ///
4818 /// Class to detect if a string needs to be URL-encoded and hold both
4819 /// encoded and original versions.
4820 ///
4821 
4823 {
4824 public:
4826  CEncodedString(const CTempString s,
4828 
4829  /// Set new original string
4830  void SetString(const CTempString s,
4832 
4833  /// Check if the original string was encoded.
4834  bool IsEncoded(void) const { return m_Encoded.get() != 0; }
4835  /// Get the original unencoded string
4836  const string& GetOriginalString(void) const { return m_Original; }
4837  /// Get encoded string
4838  const string& GetEncodedString(void) const
4839  { return IsEncoded() ? *m_Encoded : m_Original; }
4840 
4841  /// Check if the string is empty
4842  bool IsEmpty(void) const { return m_Original.empty(); }
4843 
4844 private:
4845  string m_Original;
4846  unique_ptr<string> m_Encoded;
4847 };
4848 
4849 
4850 /////////////////////////////////////////////////////////////////////////////
4851 // Predicates
4852 //
4853 
4854 
4855 /////////////////////////////////////////////////////////////////////////////
4856 ///
4857 /// Define Case-sensitive string comparison methods.
4858 ///
4859 /// Used as arguments to template functions for specifying the type of
4860 /// comparison.
4861 
4862 template <typename T>
4864 {
4865  /// Return difference between "s1" and "s2".
4866  int Compare(const T& s1, const T& s2) const;
4867 
4868  /// Return TRUE if s1 < s2.
4869  bool Less(const T& s1, const T& s2) const;
4870 
4871  /// Return TRUE if s1 == s2.
4872  bool Equals(const T& s1, const T& s2) const;
4873 
4874  /// Return TRUE if s1 < s2.
4875  bool operator()(const T& s1, const T& s2) const;
4876 };
4877 
4880 
4881 
4882 
4883 /////////////////////////////////////////////////////////////////////////////
4884 ///
4885 /// Define Case-insensitive string comparison methods.
4886 ///
4887 /// Used as arguments to template functions for specifying the type of
4888 /// comparison.
4889 ///
4890 /// @sa PNocase_Conditional_Generic
4891 
4892 template <typename T>
4894 {
4895  /// Return difference between "s1" and "s2".
4896  int Compare(const T& s1, const T& s2) const;
4897 
4898  /// Return TRUE if s1 < s2.
4899  bool Less(const T& s1, const T& s2) const;
4900 
4901  /// Return TRUE if s1 == s2.
4902  bool Equals(const T& s1, const T& s2) const;
4903 
4904  /// Return TRUE if s1 < s2 ignoring case.
4905  bool operator()(const T& s1, const T& s2) const;
4906 };
4907 
4910 
4911 
4912 /////////////////////////////////////////////////////////////////////////////
4913 ///
4914 /// Define Case-insensitive string comparison methods.
4915 /// Case sensitivity can be turned on and off at runtime.
4916 ///
4917 /// Used as arguments to template functions for specifying the type of
4918 /// comparison.
4919 ///
4920 /// @sa PNocase_Generic
4921 
4922 template <typename T>
4924 {
4925 public:
4926  /// Construction
4928 
4929  /// Get comparison type
4931 
4932  /// Set comparison type
4933  void SetCase(NStr::ECase case_sens) { m_CaseSensitive = case_sens; }
4934 
4935  /// Return difference between "s1" and "s2".
4936  int Compare(const T& s1, const T& s2) const;
4937 
4938  /// Return TRUE if s1 < s2.
4939  bool Less(const T& s1, const T& s2) const;
4940 
4941  /// Return TRUE if s1 == s2.
4942  bool Equals(const T& s1, const T& s2) const;
4943 
4944  /// Return TRUE if s1 < s2 ignoring case.
4945  bool operator()(const T& s1, const T& s2) const;
4946 private:
4947  NStr::ECase m_CaseSensitive; ///< case sensitive when TRUE
4948 };
4949 
4952 
4953 
4954 /////////////////////////////////////////////////////////////////////////////
4955 ///
4956 /// Define Case-insensitive string equality (not less-than) comparison method
4957 ///
4958 /// Used as arguments to template functions for specifying the type of
4959 /// equality comparison
4960 ///
4961 /// @sa PEqualNocase_Conditional_Generic
4962 
4963 template <typename T>
4965  : public PNocase_Generic<T>
4966 {
4967 public:
4968  /// Return TRUE if s1 < s2 ignoring case.
4969  bool operator()(const T& s1, const T& s2) const
4970  {
4971  return this->Equals(s1, s2);
4972  }
4973 };
4974 
4977 
4978 
4979 /////////////////////////////////////////////////////////////////////////////
4980 ///
4981 /// Define Case-insensitive string equality (not less-than) comparison method
4982 /// Case sensitivity can be turned on and off at runtime.
4983 ///
4984 /// Used as arguments to template functions for specifying the type of
4985 /// comparison.
4986 ///
4987 /// @sa PEqualNocase_Generic
4988 
4989 template <typename T>
4991  : public PNocase_Conditional_Generic<T>
4992 {
4993 public:
4994  /// Construction
4996  : PNocase_Conditional_Generic<T>(case_sens)
4997  {
4998  }
4999 
5000  /// Return TRUE if s1 < s2 ignoring case.
5001  bool operator()(const T& s1, const T& s2) const
5002  {
5003  return this->Equals(s1, s2);
5004  }
5005 };
5006 
5009 
5010 /////////////////////////////////////////////////////////////////////////////
5011 ///
5012 /// PQuickStringLess implements an ordering of strings,
5013 /// that is more efficient than usual lexicographical order.
5014 /// It can be used in cases when no specific order is required,
5015 /// e.g. only simple key lookup is needed.
5016 /// Current implementation first compares lengths of strings,
5017 /// and will compare string data only when lengths are the same.
5018 ///
5020 {
5021  bool operator()(const CTempString s1, const CTempString s2) const {
5022  size_t len1 = s1.size(), len2 = s2.size();
5023  return len1 < len2 ||
5024  (len1 == len2 && ::memcmp(s1.data(), s2.data(), len1) < 0);
5025  }
5026 };
5027 
5028 
5029 /////////////////////////////////////////////////////////////////////////////
5030 // Algorithms
5031 //
5032 
5033 
5034 /// Check equivalence of arguments using predicate.
5035 template<class Arg1, class Arg2, class Pred>
5036 inline
5037 bool AStrEquiv(const Arg1& x, const Arg2& y, Pred pr)
5038 {
5039  return pr.Equals(x, y);
5040 }
5041 
5042 
5043 /* @} */
5044 
5045 
5046 
5047 /////////////////////////////////////////////////////////////////////////////
5048 //
5049 // IMPLEMENTATION of INLINE functions
5050 //
5051 /////////////////////////////////////////////////////////////////////////////
5052 
5053 
5054 /////////////////////////////////////////////////////////////////////////////
5055 // CNcbiEmptyString::
5056 //
5057 #if !defined(NCBI_OS_MSWIN) && \
5058  !(defined(NCBI_OS_LINUX) && \
5059  (defined(NCBI_COMPILER_GCC) || defined(NCBI_COMPILER_ANY_CLANG)))
5060 inline
5061 const string& CNcbiEmptyString::Get(void)
5062 {
5063  const string* str = m_Str;
5064  return str ? *str: FirstGet();
5065 }
5066 
5067 # ifdef HAVE_WSTRING
5068 inline
5069 const wstring& CNcbiEmptyWString::Get(void)
5070 {
5071  const wstring* str = m_Str;
5072  return str ? *str: FirstGet();
5073 }
5074 # endif
5075 #endif
5076 
5077 
5078 
5079 /////////////////////////////////////////////////////////////////////////////
5080 // NStr::
5081 //
5082 
5083 inline
5085  TNumToStringFlags flags, int base)
5086 {
5087  string ret;
5088  IntToString(ret, value, flags, base);
5089  return ret;
5090 }
5091 
5092 inline
5093 string NStr::IntToString(unsigned int value,
5094  TNumToStringFlags flags, int base)
5095 {
5096  string ret;
5097  IntToString(ret, (int)value, flags, base);
5098  return ret;
5099 }
5100 
5101 inline
5102 void NStr::IntToString(string& out_str, unsigned int value,
5103  TNumToStringFlags flags, int base)
5104 {
5105  IntToString(out_str, (int)value, flags, base);
5106 }
5107 
5108 inline
5109 string NStr::UIntToString(unsigned int value,
5110  TNumToStringFlags flags, int base)
5111 {
5112  string ret;
5113  ULongToString(ret, value, flags, base);
5114  return ret;
5115 }
5116 
5117 inline
5119  TNumToStringFlags flags, int base)
5120 {
5121  string ret;
5122  UIntToString(ret, (unsigned int)value, flags, base);
5123  return ret;
5124 }
5125 
5126 inline
5127 void NStr::UIntToString(string& out_str, unsigned int value,
5128  TNumToStringFlags flags, int base)
5129 {
5130  ULongToString(out_str, value, flags, base);
5131 }
5132 
5133 inline
5134 void NStr::UIntToString(string& out_str, int value,
5135  TNumToStringFlags flags, int base)
5136 {
5137  UIntToString(out_str, (unsigned int)value, flags, base);
5138 }
5139 
5140 inline
5142  TNumToStringFlags flags, int base)
5143 {
5144  string ret;
5145  LongToString(ret, value, flags, base);
5146  return ret;
5147 }
5148 
5149 inline
5150 string NStr::ULongToString(unsigned long value,
5151  TNumToStringFlags flags, int base)
5152 {
5153  string ret;
5154  ULongToString(ret, value, flags, base);
5155  return ret;
5156 }
5157 
5158 inline
5160  TNumToStringFlags flags, int base)
5161 {
5162  string ret;
5163  NStr::Int8ToString(ret, value, flags, base);
5164  return ret;
5165 }
5166 
5167 inline
5169  TNumToStringFlags flags, int base)
5170 {
5171  string ret;
5172  NStr::UInt8ToString(ret, value, flags, base);
5173  return ret;
5174 }
5175 
5176 inline
5178  TNumToStringFlags flags /* = 0 */,
5179  unsigned int max_digits /* = 3 */)
5180 {
5181  string ret;
5182  NStr::UInt8ToString_DataSize(ret, value, flags, max_digits);
5183  return ret;
5184 }
5185 
5186 inline
5189 {
5190  string str;
5192  return str;
5193 }
5194 
5195 inline
5196 int NStr::HexChar(char ch)
5197 {
5198  unsigned int rc = ch - '0';
5199  if (rc <= 9) {
5200  return rc;
5201  } else {
5202  rc = (ch | ' ') - 'a';
5203  return rc <= 5 ? int(rc + 10) : -1;
5204  }
5205 }
5206 
5207 inline
5208 int NStr::strcmp(const char* s1, const char* s2)
5209 {
5210  return ::strcmp(s1, s2);
5211 }
5212 
5213 inline
5214 int NStr::strncmp(const char* s1, const char* s2, size_t n)
5215 {
5216  return ::strncmp(s1, s2, n);
5217 }
5218 
5219 inline
5220 int NStr::strcasecmp(const char* s1, const char* s2)
5221 {
5222 #if defined(HAVE_STRICMP)
5223 #if NCBI_COMPILER_MSVC && (_MSC_VER >= 1400)
5224  return ::_stricmp(s1, s2);
5225 #else
5226  return ::stricmp(s1, s2);
5227 #endif
5228 
5229 #elif defined(HAVE_STRCASECMP_LC)
5230  return ::strcasecmp(s1, s2);
5231 
5232 #else
5233  int diff = 0;
5234  for ( ;; ++s1, ++s2) {
5235  char c1 = *s1;
5236  // calculate difference
5237  diff = tolower((unsigned char) c1) - tolower((unsigned char)(*s2));
5238  // if end of string or different
5239  if (!c1 || diff)
5240  break; // return difference
5241  }
5242  return diff;
5243 #endif
5244 }
5245 
5246 inline
5247 int NStr::strncasecmp(const char* s1, const char* s2, size_t n)
5248 {
5249 #if defined(HAVE_STRICMP)
5250 #if NCBI_COMPILER_MSVC && (_MSC_VER >= 1400)
5251  return ::_strnicmp(s1, s2, n);
5252 #else
5253  return ::strnicmp(s1, s2, n);
5254 #endif
5255 
5256 #elif defined(HAVE_STRCASECMP_LC)
5257  return ::strncasecmp(s1, s2, n);
5258 
5259 #else
5260  int diff = 0;
5261  for ( ; ; ++s1, ++s2, --n) {
5262  if (n == 0)
5263  return 0;
5264  char c1 = *s1;
5265  // calculate difference
5266  diff = tolower((unsigned char) c1) - tolower((unsigned char)(*s2));
5267  // if end of string or different
5268  if (!c1 || diff)
5269  break; // return difference
5270  }
5271  return diff;
5272 #endif
5273 }
5274 
5275 inline
5276 size_t NStr::strftime(char* s, size_t maxsize, const char* format,
5277  const struct tm* timeptr)
5278 {
5279  string x_format = Replace(format, "%T", "%H:%M:%S");
5280  ReplaceInPlace(x_format, "%D", "%m/%d/%y");
5281  return ::strftime(s, maxsize, x_format.c_str(), timeptr);
5282 }
5283 
5284 inline
5285 int NStr::CompareCase(const char* s1, const char* s2)
5286 {
5287  return NStr::strcmp(s1, s2);
5288 }
5289 
5290 inline
5291 int NStr::CompareNocase(const char* s1, const char* s2)
5292 {
5293  return NStr::strcasecmp(s1, s2);
5294 }
5295 
5296 inline
5298  const char* s2, ECase use_case)
5299 {
5300  return use_case == eCase ? CompareCase(s1.substr(pos, n), s2)
5301  : CompareNocase(s1.substr(pos, n), s2);
5302 }
5303 
5304 inline
5306  const CTempString s2, ECase use_case)
5307 {
5308  return use_case == eCase ? CompareCase(s1.substr(pos, n), s2)
5309  : CompareNocase(s1.substr(pos, n), s2);
5310 }
5311 
5312 inline
5313 int NStr::Compare(const char* s1, const char* s2, ECase use_case)
5314 {
5315  return use_case == eCase ? CompareCase(s1, s2) : CompareNocase(s1, s2);
5316 }
5317 
5318 inline
5319 int NStr::Compare(const CTempStringEx s1, const CTempStringEx s2, ECase use_case)
5320 {
5321  return use_case == eCase ? CompareCase(s1, s2) : CompareNocase(s1, s2);
5322 }
5323 
5324 inline
5325 bool NStr::EqualCase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char* s2)
5326 {
5327  return s1.substr(pos, n) == s2;
5328 }
5329 
5330 inline
5332 {
5333  return s1.substr(pos, n) == s2;
5334 }
5335 
5336 inline
5337 bool NStr::EqualCase(const char* s1, const char* s2)
5338 {
5339  size_t n = strlen(s1);
5340  if (n != strlen(s2)) {
5341  return false;
5342  }
5343  return NStr::strncmp(s1, s2, n) == 0;
5344 }
5345 
5346 inline
5348 {
5349  return s1 == s2;
5350 }
5351 
5352 inline
5353 bool NStr::EqualNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char* s2)
5354 {
5355  return CompareNocase(s1.substr(pos, n), s2) == 0;
5356 }
5357 
5358 inline
5360 {
5361  return CompareNocase(s1.substr(pos, n), s2) == 0;
5362 }
5363 
5364 inline
5365 bool NStr::EqualNocase(const char* s1, const char* s2)
5366 {
5367  size_t n = strlen(s1);
5368  if (n != strlen(s2)) {
5369  return false;
5370  }
5371  return NStr::strncasecmp(s1, s2, n) == 0;
5372 }
5373 
5374 inline
5376 {
5377  if (s1.length() != s2.length()) {
5378  return false;
5379  }
5380  return CompareNocase(s1, s2) == 0;
5381 }
5382 
5383 inline
5385  const char* s2, ECase use_case)
5386 {
5387  return use_case == eCase ? EqualCase(s1.substr(pos, n), s2)
5388  : EqualNocase(s1.substr(pos, n), s2);
5389 }
5390 
5391 inline
5393  const CTempString s2, ECase use_case)
5394 {
5395  return use_case == eCase ? EqualCase(s1.substr(pos, n), s2)
5396  : EqualNocase(s1.substr(pos, n), s2);
5397 }
5398 
5399 inline
5400 bool NStr::Equal(const char* s1, const char* s2, ECase use_case)
5401 {
5402  return use_case == eCase ? EqualCase(s1, s2) : EqualNocase(s1, s2);
5403 }
5404 
5405 inline
5406 bool NStr::Equal(const CTempStringEx s1, const CTempStringEx s2, ECase use_case)
5407 {
5408  return use_case == eCase ? EqualCase(s1, s2) : EqualNocase(s1, s2);
5409 }
5410 
5411 inline
5412 bool NStr::StartsWith(const CTempString str, const CTempString start, ECase use_case)
5413 {
5414  return str.size() >= start.size() &&
5415  Equal(str.substr(0, start.size()), start, use_case);
5416 }
5417 
5418 inline
5419 bool NStr::StartsWith(const CTempString str, char start, ECase use_case)
5420 {
5421  return !str.empty() &&
5422  (use_case == eCase ? (str[0] == start)
5423  : (str[0] == start ||
5424  toupper((unsigned char) str[0]) == start ||
5425  tolower((unsigned char) str[0]))
5426  );
5427 }
5428 
5429 inline
5430 bool NStr::EndsWith(const CTempString str, const CTempString end, ECase use_case)
5431 {
5432  return str.size() >= end.size() &&
5433  Equal(str.substr(str.size() - end.size(), end.size()), end, use_case);
5434 }
5435 
5436 inline
5437 bool NStr::EndsWith(const CTempString str, char end, ECase use_case)
5438 {
5439  if (!str.empty()) {
5440  char last = str[str.length() - 1];
5441  return use_case == eCase ? (last == end)
5442  : (last == end ||
5443  toupper((unsigned char) last) == end ||
5444  tolower((unsigned char) last) == end);
5445  }
5446  return false;
5447 }
5448 
5449 inline
5451 {
5452  const SIZE_TYPE n = min(s1.length(), s2.length());
5453  for (SIZE_TYPE i = 0; i < n; i++) {
5454  if (s1[i] != s2[i]) {
5455  return i;
5456  }
5457  }
5458  return n;
5459 }
5460 
5461 inline
5463 {
5464  const SIZE_TYPE len1 = s1.length();
5465  const SIZE_TYPE len2 = s2.length();
5466  const SIZE_TYPE n = min(len1, len2);
5467  for (SIZE_TYPE i = 1; i <= n; i++) {
5468  if (s1[len1 - i] != s2[len2 - i]) {
5469  return i - 1;
5470  }
5471  }
5472  return n;
5473 }
5474 
5475 inline
5477  SIZE_TYPE start, SIZE_TYPE end, EOccurrence where,
5478  ECase use_case)
5479 {
5480  SIZE_TYPE pos = Find(CTempString(str, start, end - start), pattern, use_case,
5481  where == eFirst ? eForwardSearch : eReverseSearch, 0);
5482  if (pos == NPOS) {
5483  return NPOS;
5484  }
5485  return pos + start;
5486 }
5487 
5488 // @deprecated
5489 inline
5491  SIZE_TYPE start, SIZE_TYPE end, EOccurrence where)
5492 {
5493  if (where == eFirst) {
5494  SIZE_TYPE pos = str.find(pattern, start);
5495  return (pos == NPOS || (pos + pattern.length()) > end) ? NPOS : pos;
5496  } else {
5497  SIZE_TYPE pos = str.rfind(pattern, end);
5498  return (pos == NPOS || pos < start) ? NPOS : pos;
5499  }
5500 }
5501 
5502 inline
5504 {
5505  return Find(str, pattern, eCase);
5506 }
5507 
5508 inline
5510 {
5511  SIZE_TYPE pos = Find(CTempString(str, start), pattern, eCase);
5512  if (pos == NPOS) {
5513  return NPOS;
5514  }
5515  return pos + start;
5516 }
5517 
5518 inline
5520 {
5521  return Find(str, pattern, eNocase);
5522 }
5523 
5524 inline
5526 {
5527  SIZE_TYPE pos = Find(CTempString(str, start), pattern, eNocase);
5528  if (pos == NPOS) {
5529  return NPOS;
5530  }
5531  return pos + start;
5532 }
5533 
5534 inline
5535 const string* NStr::FindCase(const list<string>& lst, const CTempString val)
5536 {
5537  return Find(lst, val, eCase);
5538 }
5539 
5540 inline
5541 const string* NStr::FindNoCase(const list <string>& lst, const CTempString val)
5542 {
5543  return Find(lst, val, eNocase);
5544 }
5545 
5546 inline
5547 const string* NStr::FindCase(const vector <string>& vec, const CTempString val)
5548 {
5549  return Find(vec, val, eCase);
5550 }
5551 
5552 inline
5553 const string* NStr::FindNoCase(const vector <string>& vec, const CTempString val)
5554 {
5555  return Find(vec, val, eNocase);
5556 }
5557 
5558 template<typename TIterator, typename FTransform>
5559 string
5560 NStr::TransformJoin( TIterator from, TIterator to, const CTempString& delim, FTransform fnTransform)
5561 {
5562  if (from == to) {
5563  return kEmptyStr;
5564  }
5565  string result(fnTransform(*from++));
5566  for ( ; from != to; ++from) {
5567  result.append(delim).append(fnTransform(*from));
5568  }
5569  return result;
5570 }
5571 
5572 template<typename TIterator>
5573 string
5575 {
5576  if (from == to) {
5577  return kEmptyStr;
5578  }
5579  string result(*from++);
5580  size_t sz_all = 0, sz_delim = delim.size();
5581  for ( TIterator f = from; f != to; ++f) {
5582  sz_all += string(*f).size() + sz_delim;
5583  }
5584  result.reserve(result.size() + sz_all);
5585  for ( ; from != to; ++from) {
5586  result.append(delim).append(string(*from));
5587  }
5588  return result;
5589 }
5590 
5591 inline
5592 list<string>& NStr::Wrap(const string& str, SIZE_TYPE width, list<string>& arr,
5593  NStr::TWrapFlags flags, const string& prefix,
5594  const string* prefix1)
5595 {
5596  return Wrap(str, width, arr, flags, &prefix, prefix1);
5597 }
5598 
5599 inline
5600 list<string>& NStr::Wrap(const string& str, SIZE_TYPE width, list<string>& arr,
5601  NStr::TWrapFlags flags, const string& prefix,
5602  const string& prefix1)
5603 {
5604  return Wrap(str, width, arr, flags, &prefix, &prefix1);
5605 }
5606 
5607 inline
5608 list<string>& NStr::WrapList(const list<string>& l, SIZE_TYPE width,
5609  const string& delim, list<string>& arr,
5610  NStr::TWrapFlags flags, const string& prefix,
5611  const string* prefix1)
5612 {
5613  return WrapList(l, width, delim, arr, flags, &prefix, prefix1);
5614 }
5615 
5616 inline
5617 list<string>& NStr::WrapList(const list<string>& l, SIZE_TYPE width,
5618  const string& delim, list<string>& arr,
5619  NStr::TWrapFlags flags, const string& prefix,
5620  const string& prefix1)
5621 {
5622  return WrapList(l, width, delim, arr, flags, &prefix, &prefix1);
5623 }
5624 
5625 inline
5626 list<string>& NStr::Justify(const CTempString str, SIZE_TYPE width,
5627  list<string>& par, const CTempString pfx,
5628  const CTempString* pfx1)
5629 {
5630  return Justify(str, width, par, &pfx, pfx1);
5631 }
5632 
5633 inline
5634 list<string>& NStr::Justify(const CTempString str, SIZE_TYPE width,
5635  list<string>& par, const CTempString pfx,
5636  const CTempString pfx1)
5637 {
5638  return Justify(str, width, par, &pfx, &pfx1);
5639 }
5640 
5641 inline
5643 {
5645 }
5646 
5647 
5648 /////////////////////////////////////////////////////////////////////////////
5649 // CUtf8::
5650 //
5651 
5654  return x_GetValidSymbolCount(src, err);
5655 }
5656 
5659  x_GetValidSymbolCount(src,err);
5660  return (err-src.begin());
5661 }
5662 inline TUnicodeSymbol CUtf8::Decode(const char*& src) {
5663  return x_Decode(src);
5664 }
5665 #ifndef NCBI_COMPILER_WORKSHOP
5666 inline TUnicodeSymbol CUtf8::Decode(string::const_iterator& src) {
5667  return x_Decode(src);
5668 }
5669 #endif
5670 
5671 template <typename TIterator> inline TUnicodeSymbol
5673 {
5674  SIZE_TYPE more=0;
5675  TUnicodeSymbol sym = DecodeFirst(*src,more);
5676  while (more--) {
5677  sym = DecodeNext(sym, *(++src));
5678  }
5679  return sym;
5680 }
5681 
5682 template <typename TChar> basic_string<TChar>
5684  const TChar* substitute_on_error, EValidate validate)
5685 {
5686  if (validate == eValidate) {
5687  x_Validate(str);
5688  }
5690  basic_string<TChar> result;
5691  result.reserve(CUtf8::GetSymbolCount(str) + 1);
5692  CTempString::const_iterator src = str.begin();
5693  CTempString::const_iterator to = str.end();
5694  for (; src != to; ++src) {
5695  TUnicodeSymbol ch = Decode(src);
5696  if (sizeof(TChar) > 2 && ch >= 0xD800 && ch <= 0xDBFF) {
5697  TUnicodeSymbol ch2 = Decode(++src);
5698  ch = (ch - 0xD800) * 0x400 + (ch2 - 0xDC00) + 0x10000;
5699  }
5700  if (ch > max_char) {
5701  if (sizeof(TChar) == 2) {
5702  ch -= 0x10000;
5703  TUnicodeSymbol hi = ch / 0x400 + 0xD800;
5704  TUnicodeSymbol lo = ch % 0x400 + 0xDC00;
5705  result.append(1, (TChar)hi);
5706  result.append(1, (TChar)lo);
5707  continue;
5708  }
5709  if (substitute_on_error) {
5710  result.append(substitute_on_error);
5711  continue;
5712  } else {
5713  NCBI_THROW2(CStringException, eConvert,
5714  "Failed to convert symbol to wide character",
5715  (src - str.begin()));
5716  }
5717  }
5718  result.append(1, (TChar)ch);
5719  }
5720  return result;
5721 }
5722 
5723 template <typename TChar> bool
5725 {
5726  if ( *src >= 0xD800 && *src <= 0xDBFF &&
5727  *(src+1) >= 0xDC00 && *(src+1) <= 0xDFFF && sizeof(TChar) == 2) {
5728  ch = (*(src) - 0xD800) * 0x400 + (*(src+1) - 0xDC00) + 0x10000;
5729  return true;
5730  }
5731  ch = *(src);
5732  return false;
5733 }
5734 
5735 template <typename TChar> CStringUTF8&
5737 {
5738  const TChar* srcBuf;
5739  SIZE_TYPE needed = 0;
5740  SIZE_TYPE pos=0;
5741 
5742  for (pos=0, srcBuf=src;
5743  (to == NPOS) ? (*srcBuf != 0) : (pos<to); ++pos, ++srcBuf) {
5744  TUnicodeSymbol ch;
5745  if (x_TCharToUnicodeSymbol(ch, srcBuf)) {
5746  ++pos;
5747  ++srcBuf;
5748  }
5749  needed += x_BytesNeeded( ch );
5750  }
5751  if ( !needed ) {
5752  return u8str;
5753  }
5754  u8str.reserve(max(u8str.capacity(),u8str.length()+needed+1));
5755  for (pos=0, srcBuf=src;
5756  (to == NPOS) ? (*srcBuf != 0) : (pos<to); ++pos, ++srcBuf) {
5757  TUnicodeSymbol ch;
5758  if (x_TCharToUnicodeSymbol(ch, srcBuf)) {
5759  ++pos;
5760  ++srcBuf;
5761  }
5762  x_AppendChar( u8str, ch );
5763  }
5764  return u8str;
5765 }
5766 
5767 inline CStringUTF8
5769  CStringUTF8 u8;
5771 }
5772 
5773 // deprecated CStringUTF8 is there
5775 
5776 
5777 
5778 /////////////////////////////////////////////////////////////////////////////
5779 // PCase_Generic::
5780 //
5781 
5782 template <typename T>
5783 inline
5784 int PCase_Generic<T>::Compare(const T& s1, const T& s2) const
5785 {
5786  return NStr::Compare(s1, s2, NStr::eCase);
5787 }
5788 
5789 template <typename T>
5790 inline
5791 bool PCase_Generic<T>::Less(const T& s1, const T& s2) const
5792 {
5793  return Compare(s1, s2) < 0;
5794 }
5795 
5796 template <typename T>
5797 inline
5798 bool PCase_Generic<T>::Equals(const T& s1, const T& s2) const
5799 {
5800  return Compare(s1, s2) == 0;
5801 }
5802 
5803 template <typename T>
5804 inline
5805 bool PCase_Generic<T>::operator()(const T& s1, const T& s2) const
5806 {
5807  return Less(s1, s2);
5808 }
5809 
5810 
5811 
5812 ////////////////////////////////////////////////////////////////////////////
5813 // PNocase_Generic<T>::
5814 //
5815 
5816 
5817 template <typename T>
5818 inline
5819 int PNocase_Generic<T>::Compare(const T& s1, const T& s2) const
5820 {
5821  return NStr::Compare(s1, s2, NStr::eNocase);
5822 }
5823 
5824 template <typename T>
5825 inline
5826 bool PNocase_Generic<T>::Less(const T& s1, const T& s2) const
5827 {
5828  return Compare(s1, s2) < 0;
5829 }
5830 
5831 template <typename T>
5832 inline
5833 bool PNocase_Generic<T>::Equals(const T& s1, const T& s2) const
5834 {
5835  return Compare(s1, s2) == 0;
5836 }
5837 
5838 template <typename T>
5839 inline
5840 bool PNocase_Generic<T>::operator()(const T& s1, const T& s2) const
5841 {
5842  return Less(s1, s2);
5843 }
5844 
5845 ////////////////////////////////////////////////////////////////////////////
5846 // PNocase_Conditional_Generic<T>::
5847 //
5848 
5849 template <typename T>
5850 inline
5852  : m_CaseSensitive(cs)
5853 {}
5854 
5855 template <typename T>
5856 inline
5857 int PNocase_Conditional_Generic<T>::Compare(const T& s1, const T& s2) const
5858 {
5859  return NStr::Compare(s1, s2, m_CaseSensitive);
5860 }
5861 
5862 template <typename T>
5863 inline
5864 bool PNocase_Conditional_Generic<T>::Less(const T& s1, const T& s2) const
5865 {
5866  return Compare(s1, s2) < 0;
5867 }
5868 
5869 template <typename T>
5870 inline
5871 bool PNocase_Conditional_Generic<T>::Equals(const T& s1, const T& s2) const
5872 {
5873  return Compare(s1, s2) == 0;
5874 }
5875 
5876 template <typename T>
5877 inline
5878 bool PNocase_Conditional_Generic<T>::operator()(const T& s1, const T& s2) const
5879 {
5880  return Less(s1, s2);
5881 }
5882 
5883 
5885 
5886 #endif /* CORELIB___NCBISTR__HPP */
void x_Assign(CObject_id &dst, const CObject_id &src)
Definition: Seq_id.cpp:203
ncbi::TMaskedQueryRegions mask
Incapsulate compile time information such as __FILE__, __LINE__, NCBI_MODULE, current function.
Definition: ncbidiag.hpp:65
CEncodedString –.
Definition: ncbistr.hpp:4823
Empty "C++" string.
Definition: ncbistr.hpp:71
CParseTemplException –.
Definition: ncbistr.hpp:4397
Definition: parse.hpp:44
URL-decoder for string pairs parser.
Definition: ncbistr.hpp:4565
URL-encoder for string pairs parser.
Definition: ncbistr.hpp:4578
CStringException –.
Definition: ncbistr.hpp:4506
Template for parsing string into pairs of name and value or merging them back into a single string.
Definition: ncbistr.hpp:4594
Helper class to allocate memory for CTempString[Ex] on demand in the functions which need to modify t...
Definition: tempstr.hpp:1051
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
CUtf8 –.
Definition: ncbistr.hpp:3873
CStringPairsParser –.
Definition: ncbistr.hpp:4534
Encoder interface. Names and values can be encoded with different rules.
Definition: ncbistr.hpp:4550
NStr –.
Definition: ncbistr.hpp:243
static enable_if< is_integral< TNumeric >::value &&is_unsigned< TNumeric >::value &&(sizeof(TNumeric)< sizeof(unsigned int)), TNumeric >::type x_StringToNumeric(const CTempString str, TStringToNumFlags flags, int base)
Definition: ncbistr.hpp:3574
static enable_if< is_integral< TNumeric >::value &&is_unsigned< TNumeric >::value &&(sizeof(TNumeric)<=sizeof(unsigned int) &&!is_same< TNumeric, unsigned long >::value), void >::type x_NumericToString(string &out_str, TNumeric value, TNumToStringFlags flags, int base)
Definition: ncbistr.hpp:3734
static enable_if< is_arithmetic< TValue >::value, string >::type x_Join(TValue *from, TValue *to, const CTempString &delim)
Definition: ncbistr.hpp:3823
static enable_if< is_convertible< typename TIterator::iterator_category, forward_iterator_tag >::value &&is_convertible< typename TIterator::value_type, string >::value, string >::type x_Join(TIterator from, TIterator to, const CTempString &delim)
Definition: ncbistr.hpp:3801
static enable_if< is_integral< TNumeric >::value &&is_unsigned< TNumeric >::value &&(sizeof(TNumeric)< sizeof(unsigned int)), bool >::type x_StringToNumeric(const CTempString str, TNumeric *value, TStringToNumFlags flags, int base)
Definition: ncbistr.hpp:3651
Define Case-insensitive string equality (not less-than) comparison method Case sensitivity can be tur...
Definition: ncbistr.hpp:4992
Define Case-insensitive string equality (not less-than) comparison method.
Definition: ncbistr.hpp:4966
Define Case-insensitive string comparison methods.
Definition: ncbistr.hpp:4924
BEGIN_NCBI_NAMESPACE
Definition: ncbistr.hpp:50
static uch flags
#define T(s)
Definition: common.h:230
char TChar
Definition: interfaces.hpp:78
std::ofstream out("events_result.xml")
main entry point for tests
static DLIST_TYPE *DLIST_NAME() last(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:51
static char precision
Definition: genparams.c:28
static const char * str(char *buf, int n)
Definition: stats.c:84
static const char * validate(DSNINFO *di)
Go looking for trouble.
Definition: winsetup.c:179
void reset(element_type *p=0, EOwnership ownership=eTakeOwnership)
Reset will delete the old pointer (if owned), set content to the new value, and assume the ownership ...
Definition: ncbimisc.hpp:480
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
element_type * get(void) const
Get pointer.
Definition: ncbimisc.hpp:469
@ eTakeOwnership
An object can take ownership of another.
Definition: ncbi_types.h:136
@ eNoOwnership
No ownership is assumed.
Definition: ncbi_types.h:135
string
Definition: cgiapp.hpp:687
EUrlEncode
Definition: cgi_util.hpp:56
EUrlDecode
Definition: cgi_util.hpp:65
#define NULL
Definition: ncbistd.hpp:225
Nlm_Int8 LIBCALL StringToInt8(const char *str, const char **endptr)
Definition: ncbistr.hpp:408
char *LIBCALL Int8ToString(Nlm_Int8 value, char *str, size_t str_size)
Definition: ncbistr.hpp:420
EDiagSev
Severity level for the posted diagnostics.
Definition: ncbidiag.hpp:650
@ eDiag_Error
Error message.
Definition: ncbidiag.hpp:653
#define NCBI_THROW2(exception_class, err_code, message, extra)
Throw exception with extra parameter.
Definition: ncbiexpt.hpp:1754
EErrCode
Error types that an application can generate.
Definition: ncbiexpt.hpp:884
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
#define EXCEPTION_VIRTUAL_BASE
Do not use virtual base classes in exception declaration at all, because in this case derived class s...
Definition: ncbiexpt.hpp:1388
@ eInvalid
To be used ONLY as a return value; please, NEVER throw an exception with this code.
Definition: ncbiexpt.hpp:885
EStringType
String type.
Definition: serialdef.hpp:185
sequence::ECompare Compare(const CSeq_loc &loc1, const CSeq_loc &loc2, CScope *scope)
Returns the sequence::ECompare containment relationship between CSeq_locs.
#define NCBI_DEPRECATED
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
uint16_t Uint2
2-byte (16-bit) unsigned integer
Definition: ncbitype.h:101
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
std::string CStringUTF8
Definition: ncbistl.hpp:254
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
EStringToNumFlags EConvErrFlags
Formerly split out.
Definition: ncbistr.hpp:310
EJsonEncode
Json-encode flags.
Definition: ncbistr.hpp:3091
CParseTemplException(const CDiagCompileInfo &info, const CException *prev_exception, const string &message, string::size_type pos, EDiagSev severity, CException::TFlags flags)
Definition: ncbistr.hpp:4466
bool operator()(const CTempString s1, const CTempString s2) const
Definition: ncbistr.hpp:5021
static void NumericToString(string &out_str, TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:704
static bool x_StringToNumeric(const CTempString str, double *value, TStringToNumFlags flags, int)
Definition: ncbistr.hpp:3713
static enable_if< is_integral< TNumeric >::value &&is_signed< TNumeric >::value &&(sizeof(TNumeric)< sizeof(int)), TNumeric >::type x_StringToNumeric(const CTempString str, TStringToNumFlags flags, int base)
Definition: ncbistr.hpp:3567
static bool x_TCharToUnicodeSymbol(TUnicodeSymbol &u, const TChar *src)
Definition: ncbistr.hpp:5724
NStr::EUrlDecode m_Flag
Definition: ncbistr.hpp:4572
static bool x_StringToNumeric(const CTempString str, unsigned long *value, TStringToNumFlags flags, int base)
Definition: ncbistr.hpp:3682
static void x_Validate(const CTempString &str)
Definition: ncbistr.cpp:7010
NCBI_NS_STD::string::size_type SIZE_TYPE
Definition: ncbistr.hpp:132
EEncoding
Definition: ncbistr.hpp:199
virtual void Append(const string &s)=0
static string SizetToString(size_t value, TNumToStringFlags flags=0, int base=10)
Convert size_t to string.
Definition: ncbistr.cpp:2751
CParseTemplException(void)
Constructor.
Definition: ncbistr.hpp:4479
PEqualNocase_Generic< const char * > PEqualNocase_CStr
Definition: ncbistr.hpp:4976
vector< pair< string, string > > TStringPairsVector
Definition: ncbistr.hpp:4810
static TUnicodeSymbol x_Decode(TIterator &src)
Definition: ncbistr.hpp:5672
static enable_if< is_integral< TChar >::value &&(1< sizeof(TChar)), basic_string< TChar > >::type AsBasicString(const CTempString &src, const TChar *substitute_on_error, EValidate validate=eNoValidate)
Convert UTF8 string into Unicode.
Definition: ncbistr.hpp:4108
static bool x_VerifyIntLimits(TSource v, const CTempString str, TStringToNumFlags flags)
Definition: ncbistr.hpp:3548
static CStringUTF8 & AppendAsUTF8(CStringUTF8 &dest, const CTempString &src, EEncoding encoding, EValidate validate=eNoValidate)
Convert non-Unicode C++ string into UTF8 and append it to existing string.
Definition: ncbistr.hpp:4000
EPrintableMode
How to display printable strings.
Definition: ncbistr.hpp:2727
static void x_NumericToString(string &out_str, unsigned long value, TNumToStringFlags flags, int base)
Definition: ncbistr.hpp:3744
static string Int8ToString(Int8 value, TNumToStringFlags flags=0, int base=10)
Convert Int8 to string.
Definition: ncbistr.hpp:5159
int TXmlEncode
Definition: ncbistr.hpp:3037
EDedentFlags
Flags for Dedent() method.
Definition: ncbistr.hpp:3407
int Compare(const T &s1, const T &s2) const
Return difference between "s1" and "s2".
Definition: ncbistr.hpp:5819
PCase_Generic< string > PCase
Definition: ncbistr.hpp:4878
PNocase_Generic< const char * > PNocase_CStr
Definition: ncbistr.hpp:4909
ESS_Flags
Flags for Sanitize().
Definition: ncbistr.hpp:2833
int TDedentFlags
Bitwise OR of EDedentFlags.
Definition: ncbistr.hpp:3415
EStringToNumFlags
String to number conversion flags.
Definition: ncbistr.hpp:269
string::size_type m_Pos
Error position.
Definition: ncbistr.hpp:4491
EErrCode
Error types that for exception class.
Definition: ncbistr.hpp:4400
void Parse(const CTempString str, NStr::EMergeDelims merge_argsep=NStr::eMergeDelims)
Parse the string.
Definition: ncbistr.hpp:4698
static CStringUTF8 & x_Append(CStringUTF8 &u8str, const CTempString &src, EEncoding encoding, EValidate validate)
Definition: ncbistr.cpp:7042
CStringPairs(IStringDecoder *decoder=NULL, EOwnership own_decoder=eTakeOwnership, IStringEncoder *encoder=NULL, EOwnership own_encoder=eTakeOwnership)
Create parser with the specified decoder/encoder and default separators.
Definition: ncbistr.hpp:4611
static string DoubleToString(double value, int precision=-1, TNumToStringFlags flags=0)
Convert double to string.
Definition: ncbistr.hpp:5187
string Merge(void) const
Merge name-value pairs into a single string using the currently set separators and the provided encod...
Definition: ncbistr.hpp:4753
CParseTemplException(const CDiagCompileInfo &info, const CException *prev_exception, EErrCode err_code, const string &message, string::size_type pos, EDiagSev severity=eDiag_Error)
Constructor.
Definition: ncbistr.hpp:4416
static bool StringToNumeric(const CTempString str, TNumeric *value, TStringToNumFlags flags=0, int base=10)
Convert string to a numeric value.
Definition: ncbistr.hpp:356
static enable_if< is_integral< typename TStrictId::TId >::value &&is_member_function_pointer< decltype(&TStrictId::Get)>::value, void >::type x_NumericToString(string &out_str, TStrictId value, TNumToStringFlags flags, int base)
Definition: ncbistr.hpp:3780
#define kEmptyStr
Definition: ncbistr.hpp:123
ESqlEncode
SQL encode flags.
Definition: ncbistr.hpp:3166
ESplitFlags
Flags for Split*() methods.
Definition: ncbistr.hpp:2497
EWrapFlags
How to wrap the words in a string to a new line.
Definition: ncbistr.hpp:3227
static int CompareNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-insensitive compare of a substring with another string.
Definition: ncbistr.cpp:219
CNcbiOstream & operator<<(CNcbiOstream &os, const TStringUCS2 &str)
Operator for writing TStringUCS2 to stream.
Definition: ncbistr.hpp:3854
EUrlEncode
URL-encode flags.
Definition: ncbistr.hpp:3141
EMergeDelims
Whether to merge adjacent delimiters.
Definition: ncbistr.hpp:2514
AutoPtr< IStringDecoder > m_Decoder
Definition: ncbistr.hpp:4804
CWrapDestStringList(list< string > &l)
Definition: ncbistr.hpp:3281
EOccurrence
Whether it is the first or last occurrence.
Definition: ncbistr.hpp:1939
static string Merge(const TStrPairs &pairs, const string &arg_sep, const string &val_sep, IStringEncoder *encoder=NULL, EOwnership own=eTakeOwnership)
Merge name-value pairs from the provided container, separators and encoder.
Definition: ncbistr.hpp:4772
static void x_NumericToString(string &out_str, long value, TNumToStringFlags flags, int base)
Definition: ncbistr.hpp:3739
static enable_if< is_integral< TChar >::value &&(1< sizeof(TChar)), CStringUTF8 >::type AsUTF8(const TChar *src, SIZE_TYPE tchar_count=NCBI_NS_STD::string::npos)
Convert into UTF8 from a Unicode character buffer.
Definition: ncbistr.hpp:3934
const string & GetEncodedString(void) const
Get encoded string.
Definition: ncbistr.hpp:4838
int TPrintableMode
Bitwise OR of EPrintableMode flags.
Definition: ncbistr.hpp:2736
TStrPairs & GetPairs(void)
Get non-const data.
Definition: ncbistr.hpp:4799
int TNumToStringFlags
Bitwise OR of "ENumToStringFlags".
Definition: ncbistr.hpp:266
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3461
virtual string Encode(const CTempString src, EStringType stype) const =0
Encode the string.
NStr::EUrlEncode m_Flag
Definition: ncbistr.hpp:4585
void SetDecoder(IStringDecoder *decoder, EOwnership own=eTakeOwnership)
Set string decoder.
Definition: ncbistr.hpp:4673
CParseTemplException(const CParseTemplException< TBase > &other)
Constructor.
Definition: ncbistr.hpp:4431
const char * impl_ToCString(const char *s)
Definition: ncbistr.hpp:189
CStringPairs(const CTempString arg_sep, const CTempString val_sep, IStringDecoder *decoder=NULL, EOwnership own_decoder=eTakeOwnership, IStringEncoder *encoder=NULL, EOwnership own_encoder=eTakeOwnership)
Create parser with the specified parameters.
Definition: ncbistr.hpp:4636
EUrlDecode
URL decode flags.
Definition: ncbistr.hpp:3157
static SIZE_TYPE FindNoCase(const CTempString str, const CTempString pattern, SIZE_TYPE start, SIZE_TYPE end, EOccurrence which=eFirst)
Find the pattern in the specified range of a string using a case insensitive search.
Definition: ncbistr.cpp:2993
virtual ~IWrapDest()
Definition: ncbistr.hpp:3271
CEncodedString(void)
Definition: ncbistr.hpp:4825
bool Less(const T &s1, const T &s2) const
Return TRUE if s1 < s2.
Definition: ncbistr.hpp:5826
static CTempString TruncateSpaces_Unsafe(const CTempString &str, NStr::ETrunc side=NStr::eTrunc_Both)
Truncate spaces in the string.
Definition: ncbistr.cpp:7219
static bool EndsWith(const CTempString str, const CTempString end, ECase use_case=eCase)
Check if a string ends with a specified suffix value.
Definition: ncbistr.hpp:5430
static SIZE_TYPE x_BytesNeeded(TUnicodeSymbol ch)
Definition: ncbistr.cpp:7098
static string LongToString(long value, TNumToStringFlags flags=0, int base=10)
Convert Int to string.
Definition: ncbistr.hpp:5141
char TXChar
Definition: ncbistr.hpp:172
unique_ptr< string > m_Encoded
Definition: ncbistr.hpp:4846
static int strcmp(const char *s1, const char *s2)
String compare.
Definition: ncbistr.hpp:5208
EDirection
Search direction for Find() methods.
Definition: ncbistr.hpp:1945
EXmlEncode
XML-encode flags.
Definition: ncbistr.hpp:3025
NStr::ECase m_CaseSensitive
case sensitive when TRUE
Definition: ncbistr.hpp:4947
static bool EvaluateNext(char ch)
Check that the character is valid continuation byte of an UTF8 byte sequence.
Definition: ncbistr.hpp:4170
PEqualNocase_Conditional_Generic< string > PEqualNocase_Conditional
Definition: ncbistr.hpp:5007
#define NPOS
Definition: ncbistr.hpp:133
bool IsEmpty(void) const
Check if the string is empty.
Definition: ncbistr.hpp:4842
virtual void Append(const CTempString &s)
Definition: ncbistr.hpp:3286
static string TransformJoin(TIterator from, TIterator to, const CTempString &delim, FTransform fnTransform)
Definition: ncbistr.hpp:5560
static SIZE_TYPE GetSymbolCount(const CTempString &src)
Get the number of symbols (code points) in UTF8 string.
Definition: ncbistr.cpp:6678
static int strncasecmp(const char *s1, const char *s2, size_t n)
Case-insensitive comparison of two zero-terminated strings, narrowed to the specified number of chara...
Definition: ncbistr.hpp:5247
Uint4 TUnicodeSymbol
Unicode character.
Definition: ncbistr.hpp:141
static CTempString TruncateSpaces(const CTempString str, ETrunc where=eTrunc_Both)
Definition: ncbistr.hpp:2279
IStringEncoder * GetEncoder(void)
Get encoder or NULL. Does not affect encoder ownership.
Definition: ncbistr.hpp:4687
static string Join(const initializer_list< TValue > &arr, const CTempString &delim)
Definition: ncbistr.hpp:2703
string TXString
Definition: ncbistr.hpp:173
PEqualNocase_Generic< string > PEqualNocase
Definition: ncbistr.hpp:4975
static TNumeric StringToNumeric(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to a numeric value.
Definition: ncbistr.hpp:330
static string Join(TInputIterator from, TInputIterator to, const CTempString &delim)
Definition: ncbistr.hpp:2709
static string JoinNumeric(TInputIterator from, TInputIterator to, const CTempString &delim)
Definition: ncbistr.hpp:2715
ENumToStringFlags
Number to string conversion flags.
Definition: ncbistr.hpp:250
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
static CStringUTF8 TruncateSpaces(const CTempString &str, NStr::ETrunc side=NStr::eTrunc_Both)
Truncate spaces in the string.
Definition: ncbistr.hpp:5768
static SIZE_TYPE CommonSuffixSize(const CTempString s1, const CTempString s2)
Determine the common suffix of two strings.
Definition: ncbistr.hpp:5462
virtual const char * GetType(void) const override
Get exception class type.
Definition: ncbistr.hpp:4450
string::size_type GetPos(void) const noexcept
Get error position.
Definition: ncbistr.hpp:4463
int Compare(const T &s1, const T &s2) const
Return difference between "s1" and "s2".
Definition: ncbistr.hpp:5857
virtual void ReportExtra(ostream &out) const override
Report error position.
Definition: ncbistr.hpp:4442
void SetEncoder(IStringEncoder *encoder, EOwnership own=eTakeOwnership)
Set string encoder.
Definition: ncbistr.hpp:4684
static string DedentR(const CTempString str)
Definition: ncbistr.hpp:5642
const string & GetOriginalString(void) const
Get the original unencoded string.
Definition: ncbistr.hpp:4836
NCBI_EXCEPTION_DEFAULT2(CStringException, CParseTemplException< CCoreException >, std::string::size_type)
CStringPairs(NStr::EUrlDecode decode_flag, NStr::EUrlEncode encode_flag)
Create parser with the selected URL-encoding/decoding options and default separators.
Definition: ncbistr.hpp:4656
static string Dedent(const CTempString str, TDedentFlags flags=0)
Dedent multi-line string, removing common whitespace prefix for each line.
Definition: ncbistr.cpp:5503
EErrCode
Error types that string classes can generate.
Definition: ncbistr.hpp:4509
static SIZE_TYPE Find(const CTempString str, const CTempString pattern, ECase use_case=eCase, EDirection direction=eForwardSearch, SIZE_TYPE occurrence=0)
Find the pattern in the string.
Definition: ncbistr.cpp:2891
static CStringUTF8 AsUTF8(const CTempString &src, const locale &lcl)
Convert into UTF8 from a C/C++ string.
Definition: ncbistr.hpp:3904
PNocase_Generic< string > PNocase
Definition: ncbistr.hpp:4908
const char * const_iterator
Definition: tempstr.hpp:71
static SIZE_TYPE GetValidSymbolCount(const CTempString &src)
Get the number of valid UTF-8 symbols (code points) in buffer.
Definition: ncbistr.hpp:5652
TUnicodeSymbol TCharUCS4
Definition: ncbistr.hpp:3842
static int HexChar(char ch)
Convert character to integer.
Definition: ncbistr.hpp:5196
static void ToUpper(const char *)
Privatized ToUpper() with const char* parameter to prevent passing of constant strings.
static string Join(const TContainer &arr, const CTempString &delim)
Join strings using the specified delimiter.
Definition: ncbistr.hpp:2697
static void Parse(TStrPairs &pairs, const CTempString str, const CTempString arg_sep, const CTempString val_sep, IStringDecoder *decoder=NULL, EOwnership own=eTakeOwnership, NStr::EMergeDelims merge_argsep=NStr::eMergeDelims)
Parse the string using the provided decoder, put data into the container.
Definition: ncbistr.hpp:4721
virtual ~IStringEncoder(void)
Definition: ncbistr.hpp:4559
virtual ~IStringDecoder(void)
Definition: ncbistr.hpp:4544
int TSplitFlags
Bitwise OR of ESplitFlags.
Definition: ncbistr.hpp:2510
static enable_if< is_integral< typename TStrictId::TId >::value &&is_member_function_pointer< decltype(&TStrictId::Get)>::value, string >::type NumericToString(TStrictId value, TNumToStringFlags flags=0, int base=10)
Definition: ncbistr.hpp:681
ETrunc
Which end to truncate a string.
Definition: ncbistr.hpp:2239
static bool EqualCase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-sensitive equality of a substring with another string.
Definition: ncbistr.hpp:5325
static basic_string< TChar > x_AsBasicString(const CTempString &src, const TChar *substitute_on_error, EValidate validate)
Definition: ncbistr.hpp:5683
const char * data(void) const
Return a pointer to the array represented.
Definition: tempstr.hpp:313
static string UInt8ToString_DataSize(Uint8 value, TNumToStringFlags flags=0, unsigned int max_digits=3)
Convert UInt8 to string using "software" qualifiers.
Definition: ncbistr.hpp:5177
virtual ~CParseTemplException(void) noexcept
Destructor.
Definition: ncbistr.hpp:4439
AutoPtr< IStringEncoder > m_Encoder
Definition: ncbistr.hpp:4805
basic_string< TUnicodeSymbol > TStringUnicode
Unicode string.
Definition: ncbistr.hpp:143
const wchar_t *const kEmptyWCStr
Definition: ncbistr.cpp:71
bool operator()(const T &s1, const T &s2) const
Return TRUE if s1 < s2 ignoring case.
Definition: ncbistr.hpp:4969
static TUnicodeSymbol DecodeFirst(char ch, SIZE_TYPE &more)
Begin converting first character of UTF8 sequence into Unicode.
Definition: ncbistr.cpp:7154
static int strcasecmp(const char *s1, const char *s2)
Case-insensitive comparison of two zero-terminated strings.
Definition: ncbistr.hpp:5220
EStringType
Type of string to be decoded.
Definition: ncbistr.hpp:4553
TStringUnicode TStringUCS4
Definition: ncbistr.hpp:3843
static enable_if< is_integral< TNumeric >::value &&is_signed< TNumeric >::value &&(sizeof(TNumeric)<=sizeof(int) &&!is_same< TNumeric, long >::value), void >::type x_NumericToString(string &out_str, TNumeric value, TNumToStringFlags flags, int base)
Definition: ncbistr.hpp:3728
static bool x_VerifyFloatLimits(TSource v, const CTempString str, TStringToNumFlags flags)
Definition: ncbistr.hpp:3556
bool AStrEquiv(const Arg1 &x, const Arg2 &y, Pred pr)
Check equivalence of arguments using predicate.
Definition: ncbistr.hpp:5037
static string & Replace(const string &src, const string &search, const string &replace, string &dst, SIZE_TYPE start_pos=0, SIZE_TYPE max_replace=0, SIZE_TYPE *num_replace=0)
Replace occurrences of a substring within a string.
Definition: ncbistr.cpp:3314
virtual ~CStringPairs(void)
Definition: ncbistr.hpp:4665
static SIZE_TYPE FindCase(const CTempString str, const CTempString pattern, SIZE_TYPE start, SIZE_TYPE end, EOccurrence which=eFirst)
Find the pattern in the specified range of a string using a case sensitive search.
Definition: ncbistr.hpp:5490
CStringPairs< TStringPairsVector > CStringPairsParser
Definition: ncbistr.hpp:4811
static int Compare(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2, ECase use_case=eCase)
Compare of a substring with another string.
Definition: ncbistr.hpp:5297
static size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
Wrapper for the function strftime() that corrects handling D and T time formats on MS Windows.
Definition: ncbistr.hpp:5276
static CStringUTF8 SQLEncode(const CStringUTF8 &str)
Definition: ncbistr.hpp:3192
string m_ArgSep
Definition: ncbistr.hpp:4802
Uint2 TCharUCS2
Type for character in UCS-2 encoding.
Definition: ncbistr.hpp:3847
virtual const CException * x_Clone(void) const override
Helper clone method.
Definition: ncbistr.hpp:4485
static CStringUTF8 & x_AppendChar(CStringUTF8 &u8str, TUnicodeSymbol ch)
Definition: ncbistr.cpp:7020
virtual void Append(const string &s)
Definition: ncbistr.hpp:3282
static enable_if< is_integral< TChar >::value &&(1< sizeof(TChar)), CStringUTF8 & >::type AppendAsUTF8(CStringUTF8 &dest, TChar ch)
Convert Unicode symbol into UTF8 and append it to existing string.
Definition: ncbistr.hpp:3983
static CStringUTF8 AsUTF8(const CTempString &src, EEncoding encoding, EValidate validate=eNoValidate)
Convert into UTF8 from a C/C++ string.
Definition: ncbistr.hpp:3889
bool Equals(const T &s1, const T &s2) const
Return TRUE if s1 == s2.
Definition: ncbistr.hpp:5798
basic_string< TCharUCS2 > TStringUCS2
Type for string in UCS-2 encoding.
Definition: ncbistr.hpp:3849
EQuoted
Define that string is quoted or not.
Definition: ncbistr.hpp:2987
IStringDecoder * GetDecoder(void)
Get decoder or NULL. Does not affect decoder ownership.
Definition: ncbistr.hpp:4676
static list< string > & WrapList(const list< string > &l, SIZE_TYPE width, const string &delim, list< string > &arr, TWrapFlags flags=0, const string *prefix=0, const string *prefix1=0)
Wrap the list using the specified criteria.
Definition: ncbistr.cpp:5366
static enable_if< is_integral< TChar >::value &&(1< sizeof(TChar)), CStringUTF8 & >::type AppendAsUTF8(CStringUTF8 &dest, const TChar *src, SIZE_TYPE tchar_count=NCBI_NS_STD::string::npos)
Convert Unicode character buffer into UTF8 and append it to existing string.
Definition: ncbistr.hpp:3968
static string xx_Join(TIterator from, TIterator to, const CTempString &delim)
Definition: ncbistr.hpp:5574
int THtmlDecode
Definition: ncbistr.hpp:3071
virtual void Append(const CTempString &s)=0
EStringType
Type of string to be decoded.
Definition: ncbistr.hpp:4537
virtual string Decode(const CTempString src, EStringType stype) const =0
Decode the string.
TStrPairs m_Data
Definition: ncbistr.hpp:4806
#define NcbiEmptyString
Definition: ncbistr.hpp:122
static SIZE_TYPE x_GetValidSymbolCount(const CTempString &src, CTempString::const_iterator &err)
Definition: ncbistr.cpp:6647
static string UIntToString(unsigned int value, TNumToStringFlags flags=0, int base=10)
Convert UInt to string.
Definition: ncbistr.hpp:5109
static bool StartsWith(const CTempString str, const CTempString start, ECase use_case=eCase)
Check if a string starts with a specified prefix value.
Definition: ncbistr.hpp:5412
PNocase_Conditional_Generic(NStr::ECase case_sens=NStr::eCase)
Construction.
Definition: ncbistr.hpp:5851
const TStrPairs & GetPairs(void) const
Read data.
Definition: ncbistr.hpp:4797
TContainer TStrPairs
Definition: ncbistr.hpp:4596
static enable_if< is_integral< TChar >::value &&(1< sizeof(TChar)), basic_string< TChar > >::type AsBasicString(const CTempString &src)
Definition: ncbistr.hpp:4115
bool IsEncoded(void) const
Check if the original string was encoded.
Definition: ncbistr.hpp:4834
END_NCBI_NAMESPACE
Definition: ncbistr.hpp:5884
static enable_if< is_integral< typename TStrictId::TId >::value &&is_member_function_pointer< decltype(&TStrictId::Get)>::value, bool >::type x_StringToNumeric(const CTempString str, TStrictId *value, TStringToNumFlags flags, int base)
Definition: ncbistr.hpp:3720
EHtmlEncode
HTML-decode flags.
Definition: ncbistr.hpp:3047
bool Equals(const T &s1, const T &s2) const
Return TRUE if s1 == s2.
Definition: ncbistr.hpp:5871
static enable_if< is_integral< TChar >::value &&(1< sizeof(TChar)), CStringUTF8 >::type AsUTF8(const basic_string< TChar > &src)
Convert into UTF8 from a Unicode C++ string.
Definition: ncbistr.hpp:3919
bool operator()(const T &s1, const T &s2) const
Return TRUE if s1 < s2.
Definition: ncbistr.hpp:5805
static TUnicodeSymbol DecodeNext(TUnicodeSymbol chU, char ch)
Convert next character of UTF8 sequence into Unicode.
Definition: ncbistr.cpp:7177
static SIZE_TYPE FindWord(const CTempString str, const CTempString word, EOccurrence which, ECase use_case=eCase)
Find given word in the string.
Definition: ncbistr.hpp:2230
static enable_if< is_convertible< TValue, string >::value, string >::type x_Join(TValue *from, TValue *to, const CTempString &delim)
Definition: ncbistr.hpp:3808
static CStringUTF8 & AppendAsUTF8(CStringUTF8 &dest, char ch, const locale &lcl)
Convert non-Unicode character into UTF8 and append it to existing string.
Definition: ncbistr.hpp:4058
string m_Original
Definition: ncbistr.hpp:4845
static bool x_StringToNumeric(const CTempString str, float *value, TStringToNumFlags flags, int)
Definition: ncbistr.hpp:3702
static bool SplitInTwo(const CTempString str, const CTempString delim, string &str1, string &str2, TSplitFlags flags=0)
Split a string into two pieces using the specified delimiters.
Definition: ncbistr.cpp:3554
bool Less(const T &s1, const T &s2) const
Return TRUE if s1 < s2.
Definition: ncbistr.hpp:5791
size_type length(void) const
Return the length of the represented array.
Definition: tempstr.hpp:320
bool Equals(const T &s1, const T &s2) const
Return TRUE if s1 == s2.
Definition: ncbistr.hpp:5833
static string ULongToString(unsigned long value, TNumToStringFlags flags=0, int base=10)
Convert unsigned long to string.
Definition: ncbistr.hpp:5150
static bool x_StringToNumeric(const CTempString str, long *value, TStringToNumFlags flags, int base)
Definition: ncbistr.hpp:3676
TContainer::value_type TStrPair
The container's value type must be pair<string, string> or a compatible type.
Definition: ncbistr.hpp:4599
int TSS_Flags
Bitwise OR of ESS_Flags.
Definition: ncbistr.hpp:2853
static list< string > & Justify(const CTempString str, SIZE_TYPE width, list< string > &par, const CTempString *pfx=0, const CTempString *pfx1=0)
Justify the specified string into a series of lines of the same width.
Definition: ncbistr.cpp:5418
static const wstring & Get(void)
Get string.
Definition: ncbistr.hpp:86
list< string > & m_list
Definition: ncbistr.hpp:3279
static string Sanitize(CTempString str, TSS_Flags flags=fSS_print)
Sanitize a string, allowing only specified classes of characters.
Definition: ncbistr.hpp:2876
PNocase_Conditional_Generic< string > PNocase_Conditional
Definition: ncbistr.hpp:4950
CTempString substr(size_type pos) const
Obtain a substring from this string, beginning at a given offset.
Definition: tempstr.hpp:776
static CStringUTF8 & AppendAsUTF8(CStringUTF8 &dest, char ch, EEncoding encoding, EValidate validate=eNoValidate)
Convert non-Unicode character into UTF8 and append it to existing string.
Definition: ncbistr.hpp:4039
bool operator()(const T &s1, const T &s2) const
Return TRUE if s1 < s2 ignoring case.
Definition: ncbistr.hpp:5840
static CTempString TruncateSpaces(const char *str, ETrunc where=eTrunc_Both)
Definition: ncbistr.hpp:2288
static void ToLower(const char *)
Privatized ToLower() with const char* parameter to prevent passing of constant strings.
int THtmlEncode
Definition: ncbistr.hpp:3055
static TUnicodeSymbol Decode(const char *&src)
Convert sequence of UTF8 code units into Unicode code point.
Definition: ncbistr.hpp:5662
static CStringUTF8 & AppendAsUTF8(CStringUTF8 &dest, const CTempString &src, const locale &lcl)
Convert non-Unicode C++ string into UTF8 and append it to existing string.
Definition: ncbistr.hpp:4019
static bool EqualNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-insensitive equality of a substring with another string.
Definition: ncbistr.hpp:5353
EHtmlDecode
HTML-decode flags.
Definition: ncbistr.hpp:3066
void SetCase(NStr::ECase case_sens)
Set comparison type.
Definition: ncbistr.hpp:4933
ECase
Which type of string comparison.
Definition: ncbistr.hpp:1204
static int strncmp(const char *s1, const char *s2, size_t n)
String compare up to specified number of characters.
Definition: ncbistr.hpp:5214
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbistr.hpp:4455
static enable_if< is_same< typename TIterator::iterator_category, input_iterator_tag >::value &&is_convertible< typename TIterator::value_type, string >::value, string >::type x_Join(TIterator from, TIterator to, const CTempString &delim)
Definition: ncbistr.hpp:3793
int TWrapFlags
Bitwise OR of "EWrapFlags".
Definition: ncbistr.hpp:3232
int TStringToNumFlags
Bitwise OR of "EStringToNumFlags".
Definition: ncbistr.hpp:311
bool Less(const T &s1, const T &s2) const
Return TRUE if s1 < s2.
Definition: ncbistr.hpp:5864
NStr::ECase GetCase() const
Get comparison type.
Definition: ncbistr.hpp:4930
static SIZE_TYPE GetValidBytesCount(const CTempString &src)
Get the number of valid UTF-8 bytes (code units) in buffer.
Definition: ncbistr.hpp:5657
static void Wrap(const string &str, SIZE_TYPE width, IWrapDest &dest, TWrapFlags flags, const string *prefix, const string *prefix1)
Definition: ncbistr.cpp:5347
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
int Compare(const T &s1, const T &s2) const
Return difference between "s1" and "s2".
Definition: ncbistr.hpp:5784
static bool Equal(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2, ECase use_case=eCase)
Test for equality of a substring with another string.
Definition: ncbistr.hpp:5384
EValidate
How to verify character encoding of the source data.
Definition: ncbistr.hpp:3876
static const string & Get(void)
Get string.
Definition: ncbistr.hpp:75
static string & ReplaceInPlace(string &src, const string &search, const string &replace, SIZE_TYPE start_pos=0, SIZE_TYPE max_replace=0, SIZE_TYPE *num_replace=0)
Replace occurrences of a substring within a string.
Definition: ncbistr.cpp:3405
bool operator()(const T &s1, const T &s2) const
Return TRUE if s1 < s2 ignoring case.
Definition: ncbistr.hpp:5001
static enable_if< is_integral< TChar >::value &&(1< sizeof(TChar)), CStringUTF8 & >::type AppendAsUTF8(CStringUTF8 &dest, const basic_string< TChar > &src)
Convert Unicode C++ string into UTF8 and append it to existing string.
Definition: ncbistr.hpp:3950
string m_ValSep
Definition: ncbistr.hpp:4803
const char *const kEmptyCStr
Empty "C" string (points to a '\0').
Definition: ncbistr.cpp:68
PNocase_Conditional_Generic< const char * > PNocase_Conditional_CStr
Definition: ncbistr.hpp:4951
bool operator()(const T &s1, const T &s2) const
Return TRUE if s1 < s2 ignoring case.
Definition: ncbistr.hpp:5878
static SIZE_TYPE Find(const CTempString str, const CTempString pattern, SIZE_TYPE start)
Wrapper for backward-compatibility.
Definition: ncbistr.hpp:2032
size_type size(void) const
Return the length of the represented array.
Definition: tempstr.hpp:327
static enable_if< is_integral< TNumeric >::value &&is_signed< TNumeric >::value &&(sizeof(TNumeric)< sizeof(int)), bool >::type x_StringToNumeric(const CTempString str, TNumeric *value, TStringToNumFlags flags, int base)
Definition: ncbistr.hpp:3639
PCase_Generic< const char * > PCase_CStr
Definition: ncbistr.hpp:4879
static int CompareCase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-sensitive compare of a substring with another string.
Definition: ncbistr.cpp:135
static string UInt8ToString(Uint8 value, TNumToStringFlags flags=0, int base=10)
Convert UInt8 to string.
Definition: ncbistr.hpp:5168
TStringToNumFlags TConvErrFlags
Formerly split out.
Definition: ncbistr.hpp:312
PEqualNocase_Conditional_Generic< const char * > PEqualNocase_Conditional_CStr
Definition: ncbistr.hpp:5008
PEqualNocase_Conditional_Generic(NStr::ECase case_sens=NStr::eCase)
Construction.
Definition: ncbistr.hpp:4995
EEscSeqRange
C-style escape sequences parsing mode.
Definition: ncbistr.hpp:2938
static bool EvaluateFirst(char ch, SIZE_TYPE &more)
Check that the character is valid first byte of an UTF8 byte sequence.
Definition: ncbistr.hpp:4160
static SIZE_TYPE CommonPrefixSize(const CTempString s1, const CTempString s2)
Determine the common prefix of two strings.
Definition: ncbistr.hpp:5450
const_iterator begin() const
Return an iterator to the string's starting position.
Definition: tempstr.hpp:299
virtual const char * GetErrCodeString(void) const override
Translate from the error code value to its string representation.
Definition: ncbistr.hpp:4405
static int StringToNumeric(const string &str)
Definition: ncbistr.hpp:380
@ eEncoding_Windows_1252
Definition: ncbistr.hpp:207
@ eEncoding_Ascii
Definition: ncbistr.hpp:202
@ eEncoding_ISO8859_1
Note: From the point of view of the C++.
Definition: ncbistr.hpp:203
@ eEncoding_CESU8
Definition: ncbistr.hpp:208
@ eEncoding_UTF8
Definition: ncbistr.hpp:201
@ eEncoding_Unknown
Definition: ncbistr.hpp:200
@ fDedent_SkipEmptyFirstLine
Ignore first line and skip it from the result, if it is empty only.
Definition: ncbistr.hpp:3413
@ eErr
Generic error.
Definition: ncbistr.hpp:4401
@ eSqlEnc_Plain
Always produce '...', with no tag.
Definition: ncbistr.hpp:3167
@ eUrlEnc_ProcessMarkChars
Convert all non-alphanumeric chars, spaces are converted to '+'.
Definition: ncbistr.hpp:3143
@ eUrlEnc_URIQueryValue
Encode query part of an URI, arg value.
Definition: ncbistr.hpp:3151
@ eUrlEnc_PercentOnly
Convert all non-alphanumeric chars including space and '' to %## format.
Definition: ncbistr.hpp:3144
@ eUrlEnc_URIHost
Encode host part of an URI.
Definition: ncbistr.hpp:3148
@ eUrlEnc_URIQueryName
Encode query part of an URI, arg name.
Definition: ncbistr.hpp:3150
@ eUrlEnc_URIPath
Encode path part of an URI.
Definition: ncbistr.hpp:3149
@ eUrlEnc_Path
Same as ProcessMarkChars but preserves valid path characters ('/', '.')
Definition: ncbistr.hpp:3145
@ eUrlEnc_URIScheme
Encode scheme part of an URI.
Definition: ncbistr.hpp:3146
@ eUrlEnc_URIUserinfo
Encode userinfo part of an URI.
Definition: ncbistr.hpp:3147
@ eUrlEnc_SkipMarkChars
Do not convert chars like '!', '(' etc.
Definition: ncbistr.hpp:3142
@ eUrlEnc_Cookie
Same as SkipMarkChars with encoded ','.
Definition: ncbistr.hpp:3153
@ eUrlEnc_URIFragment
Encode fragment part of an URI.
Definition: ncbistr.hpp:3152
@ eMergeDelims
Definition: ncbistr.hpp:2515
@ eFirst
First occurrence.
Definition: ncbistr.hpp:1940
@ eUrlDec_All
Decode '+' to space.
Definition: ncbistr.hpp:3158
@ eReverseSearch
Search in a backward direction.
Definition: ncbistr.hpp:1947
@ eForwardSearch
Search in a forward direction.
Definition: ncbistr.hpp:1946
@ eConvert
Failure to convert string.
Definition: ncbistr.hpp:4510
@ eBadArgs
Bad arguments to string methods.
Definition: ncbistr.hpp:4511
@ eTrunc_Both
Truncate spaces at both begin and end of string.
Definition: ncbistr.hpp:2242
@ eTrunc_End
Truncate trailing spaces only.
Definition: ncbistr.hpp:2241
@ eTrunc_Begin
Truncate leading spaces only.
Definition: ncbistr.hpp:2240
@ eQuoted
String is quoted.
Definition: ncbistr.hpp:2988
@ eNocase
Case insensitive compare.
Definition: ncbistr.hpp:1206
@ eCase
Case sensitive compare.
Definition: ncbistr.hpp:1205
@ eNoValidate
Definition: ncbistr.hpp:3877
@ eValidate
Definition: ncbistr.hpp:3878
@ eEscSeqRange_Throw
Throw an exception.
Definition: ncbistr.hpp:2943
@ eEscSeqRange_Errno
Set errno to ERANGE, return empty string.
Definition: ncbistr.hpp:2944
enum ENcbiOwnership EOwnership
Ownership relations between objects.
#define NCBI_XNCBI_EXPORT
Definition: ncbi_export.h:1283
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
char * buf
int i
yy_size_t n
static MDB_envinfo info
Definition: mdb_load.c:37
vector< CSeq_align const * >::const_iterator TIterator
const TYPE & Get(const CNamedParameterList *param)
constexpr bool Equal(std::integral_constant< ncbi::NStr::ECase, case_sensitive > tag, const ct_basic_string< _CharType > &l, const ct_basic_string< _CharType > &r)
constexpr int CompareNocase(const std::string_view &l, const std::string_view &r)
mdb_mode_t mode
Definition: lmdb++.h:38
double value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:228
const struct ncbi::grid::netcache::search::fields::SIZE size
static RE_Options UTF8()
Definition: pcrecpp.h:465
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
int strncmp(const char *str1, const char *str2, size_t count)
Definition: odbc_utils.hpp:133
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
static const BitmapCharRec ch2
Definition: ncbi_10x20.c:1819
#define strncasecmp
#define strcasecmp
int tolower(Uchar c)
Definition: ncbictype.hpp:72
int toupper(Uchar c)
Definition: ncbictype.hpp:73
Defines NCBI C++ Toolkit portable error codes.
T max(T x_, T y_)
T min(T x_, T y_)
static Format format
Definition: njn_ioutil.cpp:53
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
static bool IsWhiteSpace(char c)
Definition: objistrxml.cpp:212
static const char * suffix[]
Definition: pcregrep.c:408
static const char * prefix[]
Definition: pcregrep.c:405
static char * locale
Definition: pcregrep.c:149
static const char delimiter[]
Define Case-sensitive string comparison methods.
Definition: ncbistr.hpp:4864
Define Case-insensitive string comparison methods.
Definition: ncbistr.hpp:4894
PQuickStringLess implements an ordering of strings, that is more efficient than usual lexicographical...
Definition: ncbistr.hpp:5020
Definition: type.c:6
else result
Definition: token2.c:20
#define Unknown
Definition: utilfun.h:40
void Encode(const CRawScoreVector< Key, Score > &, vector< char > &)
void Decode(const vector< char > &, CRawScoreVector< Key, Score > &)
unsigned char uch
Definition: zutil.h:39
Modified on Wed Apr 17 13:09:12 2024 by modify_doxy.py rev. 669887