📄 ncbistr.cpp
字号:
"Wrong UTF8 format", s_DiffPtr(srcBuf,c_str())); } } result.reserve( needed+1); for (srcBuf = c_str(); *srcBuf; ++srcBuf) { Uint1 chRes; size_t more; Uint1 ch = *srcBuf; if ((ch & 0x80) == 0) { chRes = ch; more = 0; } else { chRes = (ch & 0x1F); more = 1; } while (more--) { ch = *(++srcBuf); chRes = (chRes << 6) | (ch & 0x3F); } result += chRes; } return result;}#if defined(HAVE_WSTRING)wstring CStringUTF8::AsUnicode(void) const{ wstring result; const char* srcBuf; size_t needed = 0; bool bad = false; for (srcBuf = c_str(); *srcBuf; ++srcBuf) { Uint1 ch = *srcBuf; if ((ch & 0x80) == 0) { ++needed; } else if ((ch & 0xE0) == 0xC0) { ++needed; ch = *(++srcBuf); bad = (ch & 0xC0) != 0x80; } else if ((ch & 0xF0) == 0xE0) { ++needed; ch = *(++srcBuf); bad = (ch & 0xC0) != 0x80; if (!bad) { ch = *(++srcBuf); bad = (ch & 0xC0) != 0x80; } } else { bad = true; } if (bad) { NCBI_THROW2(CStringException, eFormat, "Wrong UTF8 format", s_DiffPtr(srcBuf,c_str())); } } result.reserve( needed+1); for (srcBuf = c_str(); *srcBuf; ++srcBuf) { Uint2 chRes; size_t more; Uint1 ch = *srcBuf; if ((ch & 0x80) == 0) { chRes = ch; more = 0; } else if ((ch & 0xE0) == 0xC0) { chRes = (ch & 0x1F); more = 1; } else { chRes = (ch & 0x0F); more = 2; } while (more--) { ch = *(++srcBuf); chRes = (chRes << 6) | (ch & 0x3F); } result += chRes; } return result;}#endif // HAVE_WSTRINGEND_NCBI_SCOPE/* * =========================================================================== * $Log: ncbistr.cpp,v $ * Revision 1000.6 2004/06/01 19:09:21 gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.109 * * Revision 1.109 2004/05/26 19:21:25 ucko * FindNoCase: avoid looping in eLastMode when there aren't any full * matches but the first character of the string matches the first * character of the pattern. * * Revision 1.108 2004/05/14 13:59:27 gorelenk * Added include of ncbi_pch.hpp * * Revision 1.107 2004/03/11 18:49:48 gorelenk * Removed(condionaly) implementation of class CNcbiEmptyString. * * Revision 1.106 2004/03/05 12:26:43 ivanov * Moved CDirEntry::MatchesMask() to NStr class. * * Revision 1.105 2004/03/04 13:38:57 kuznets * + set of ToString conversion functions taking outout string as a parameter, * not a return value (should give a performance advantage in some cases) * * Revision 1.104 2004/02/19 16:44:55 vasilche * WorkShop compiler doesn't support static templates. * * Revision 1.103 2004/02/18 20:54:47 shomrat * bug fix (pos -> pos2) * * Revision 1.102 2003/12/12 20:06:44 rsmith * Take out un-needed include of stdarg.h (included in ncbistr.hpp). * * Revision 1.101 2003/12/12 20:04:24 rsmith * make sure stdarg.h is included to define va_list. * * Revision 1.100 2003/12/12 17:26:54 ucko * +FormatVarargs * * Revision 1.99 2003/12/01 20:45:47 ucko * Extend Join to handle vectors as well as lists (common code templatized). * Add ParseEscapes (inverse of PrintableString). * * Revision 1.98 2003/10/31 13:15:20 lavr * Fix typos in the log of the previous commit :-) * * Revision 1.97 2003/10/31 12:59:46 lavr * Better diagnostics messages from exceptions; some other cosmetic changes * * Revision 1.96 2003/10/03 15:16:02 ucko * NStr::Join: preallocate as much space as we need for result. * * Revision 1.95 2003/09/17 15:18:29 vasilche * Reduce memory allocations in NStr::PrintableString() * * Revision 1.94 2003/08/19 15:17:20 rsmith * Add NStr::SplitInTwo() function. * * Revision 1.93 2003/06/16 15:19:03 ucko * FindNoCase: always honor both start and end (oops). * * Revision 1.92 2003/05/22 20:09:29 gouriano * added UTF8 strings * * Revision 1.91 2003/05/14 21:52:09 ucko * Move FindNoCase out of line and reimplement it to avoid making * lowercase copies of both strings. * * Revision 1.90 2003/03/25 22:15:40 lavr * NStr::PrintableString():: Print NUL char as \x00 instead of \0 * * Revision 1.89 2003/03/20 13:27:52 dicuccio * Oops. Removed old code wrapped in #if 0...#endif. * * Revision 1.88 2003/03/20 13:27:11 dicuccio * Changed NStr::StringToPtr() - now symmetric with NSrt::PtrToString (there were * too many special cases). * * Revision 1.87 2003/03/17 12:49:26 dicuccio * Fixed indexing error in NStr::PtrToString() - buffer is 0-based index, not * 1-based * * Revision 1.86 2003/03/11 16:57:12 ucko * Process backspaces in NStr::Wrap, allowing in particular the use of * " \b" ("-\b") to indicate mid-word break (hyphenation) points. * * Revision 1.85 2003/03/10 18:57:08 kuznets * iterate->ITERATE * * Revision 1.84 2003/03/04 00:02:21 vakatov * NStr::PtrToString() -- use runtime check for "0x" * NStr::StringToPtr() -- minor polishing * * Revision 1.83 2003/02/27 15:34:01 lavr * Bugfix in converting string to double [spurious dots], some reformatting * * Revision 1.82 2003/02/26 21:07:52 siyan * Remove const for base parameter for StringToUInt8 * * Revision 1.81 2003/02/26 20:34:11 siyan * Added/deleted whitespaces to conform to existing coding style * * Revision 1.80 2003/02/26 16:45:53 siyan * Reimplemented NStr::StringToUInt8 to support additional base parameters * that can take radix values such as 10(default), 16, 8, 2. * Reimplemented StringToPtr to support 64 bit addresses. * * Revision 1.79 2003/02/25 19:14:53 kuznets * NStr::StringToBool changed to understand YES/NO * * Revision 1.78 2003/02/25 15:43:40 dicuccio * Added #ifdef'd hack for MSVC's non-standard sprintf() in PtrToString(), * '%p' lacks a leading '0x' * * Revision 1.77 2003/02/25 14:43:53 dicuccio * Added handling of special NULL pointer encoding in StringToPtr() * * Revision 1.76 2003/02/24 20:25:59 gouriano * use template-based errno and parse exceptions * * Revision 1.75 2003/02/21 21:20:01 vakatov * Fixed some types, did some casts to avoid compilation warnings in 64-bit * * Revision 1.74 2003/02/20 18:41:28 dicuccio * Added NStr::StringToPtr() * * Revision 1.73 2003/02/11 22:11:03 ucko * Make NStr::WrapList a no-op if the input list is empty. * * Revision 1.72 2003/02/06 21:31:35 ucko * Fixed an off-by-one error in NStr::Wrap. * * Revision 1.71 2003/02/04 21:54:12 ucko * NStr::Wrap: when breaking on punctuation, try to position the break * *after* everything but opening delimiters. * * Revision 1.70 2003/01/31 03:39:11 lavr * Heed int->bool performance warning * * Revision 1.69 2003/01/27 20:06:59 ivanov * Get rid of compilation warnings in StringToUInt8() and DoubleToString() * * Revision 1.68 2003/01/24 16:59:27 ucko * Add an optional parameter to Split and Tokenize indicating whether to * merge adjacent delimiters; clean up WrapList slightly. * * Revision 1.67 2003/01/21 23:22:22 vakatov * NStr::Tokenize() to return reference, and not a new "vector<>". * * Revision 1.66 2003/01/21 20:08:01 ivanov * Added function NStr::DoubleToString(value, precision, buf, buf_size) * * Revision 1.65 2003/01/14 22:13:56 kuznets * Overflow check reimplemented for NStr::StringToInt * * Revision 1.64 2003/01/14 21:16:46 kuznets * +Nstr::Tokenize * * Revision 1.63 2003/01/13 14:47:16 kuznets * Implemented overflow checking for StringToInt8 function * * Revision 1.62 2003/01/10 22:17:06 kuznets * Implemented NStr::String2Int8 * * Revision 1.61 2003/01/10 16:49:54 kuznets * Cosmetics * * Revision 1.60 2003/01/10 15:27:12 kuznets * Eliminated int -> bool performance warning * * Revision 1.59 2003/01/10 00:08:17 vakatov * + Int8ToString(), UInt8ToString() * * Revision 1.58 2003/01/06 16:42:45 ivanov * + DoubleToString() with 'precision' * * Revision 1.57 2002/10/18 20:48:56 lavr * +ENewLine_Mode and '\n' translation in NStr::PrintableString() * * Revision 1.56 2002/10/17 14:41:20 ucko * * Make s_EndOf{Tag,Reference} actually static (oops). * * Pull width-determination code from WrapList into a separate function * (s_VisibleWidth) and make Wrap{,List} call it for everything rather * than assuming prefixes and delimiters to be plain text. * * Add a column variable to WrapList, as it may not equal s.size(). * * Revision 1.55 2002/10/16 19:30:36 ucko * Add support for wrapping HTML <PRE> blocks. (Not yet tested, but * behavior without fWrap_HTMLPre should stay the same.) * * Revision 1.54 2002/10/11 19:41:48 ucko * Clean up NStr::Wrap a bit more, doing away with the "limit" variables * for ease of potential extension. * * Revision 1.53 2002/10/03 14:44:35 ucko * Tweak the interfaces to NStr::Wrap* to avoid publicly depending on * kEmptyStr, removing the need for fWrap_UsePrefix1 in the process; also * drop fWrap_FavorPunct, as WrapList should be a better choice for such * situations. * * Revision 1.52 2002/10/02 20:15:09 ucko * Add Join, Wrap, and WrapList functions to NStr::. * * Revision 1.51 2002/09/04 15:16:57 lavr * Backslashed double quote (\") in PrintableString() * * Revision 1.50 2002/07/15 18:17:25 gouriano * renamed CNcbiException and its descendents * * Revision 1.49 2002/07/11 14:18:28 gouriano * exceptions replaced by CNcbiException-type ones * * Revision 1.48 2002/05/02 15:25:37 ivanov * Added new parameter to String-to-X functions for skipping the check * the end of string on zero * * Revision 1.47 2002/04/11 21:08:03 ivanov * CVS log moved to end of the file * * Revision 1.46 2002/02/22 17:50:52 ivanov * Added compatible compare functions strcmp, strncmp, strcasecmp, strncasecmp. * Was speed-up some Compare..() functions. * * Revision 1.45 2001/08/30 00:36:45 vakatov * + NStr::StringToNumeric() * Also, well-groomed the code and get rid of some compilation warnings. * * Revision 1.44 2001/05/30 15:56:25 vakatov * NStr::CompareNocase, NStr::CompareCase -- get rid of the possible * compilation warning (ICC compiler: "return statement missing"). * * Revision 1.43 2001/05/17 15:04:59 lavr * Typos corrected * * Revision 1.42 2001/04/12 21:39:44 vakatov * NStr::Replace() -- check against source and dest. strings being the same * * Revision 1.41 2001/04/11 20:15:29 vakatov * NStr::PrintableString() -- cast "char" to "unsigned char". * * Revision 1.40 2001/03/16 19:38:35 grichenk * Added NStr::Split() * * Revision 1.39 2001/01/03 17:45:35 vakatov * + <ncbi_limits.h> * * Revision 1.38 2000/12/15 15:36:41 vasilche * Added header corelib/ncbistr.hpp for all string utility functions. * Optimized string utility functions. * Added assignment operator to CRef<> and CConstRef<>. * Add Upcase() and Locase() methods for automatic conversion. * * Revision 1.37 2000/12/12 14:20:36 vasilche * Added operator bool to CArgValue. * Various NStr::Compare() methods made faster. * Added class Upcase for printing strings to ostream with automatic conversion * * Revision 1.36 2000/12/11 20:42:50 vakatov * + NStr::PrintableString() * * Revision 1.35 2000/11/16 23:52:41 vakatov * Porting to Mac... * * Revision 1.34 2000/11/07 04:06:08 vakatov * kEmptyCStr (equiv. to NcbiEmptyCStr) * * Revision 1.33 2000/10/11 21:03:49 vakatov * Cleanup to avoid 64-bit to 32-bit values truncation, etc. * (reported by Forte6 Patch 109490-01) * * Revision 1.32 2000/08/03 20:21:29 golikov * Added predicate PCase for AStrEquiv * PNocase, PCase goes through NStr::Compare now * * Revision 1.31 2000/07/19 19:03:55 vakatov * StringToBool() -- short and case-insensitive versions of "true"/"false" * ToUpper/ToLower(string&) -- fixed * * Revision 1.30 2000/06/01 19:05:40 vasilche * NStr::StringToInt now reports errors for tailing symbols in release version * too * * Revision 1.29 2000/05/01 19:02:25 vasilche * Force argument in NStr::StringToInt() etc to be full number. * This check will be in DEBUG version for month. * * Revision 1.28 2000/04/19 18:36:04 vakatov * Fixed for non-zero "pos" in s_Compare() * * Revision 1.27 2000/04/17 04:15:08 vakatov * NStr:: extended Compare(), and allow case-insensitive string comparison * NStr:: added ToLower() and ToUpper() * * Revision 1.26 2000/04/04 22:28:09 vakatov * NStr:: added conversions for "long" * * Revision 1.25 2000/02/01 16:48:09 vakatov * CNcbiEmptyString:: more dancing around the Sun "feature" (see also R1.24) * * Revision 1.24 2000/01/20 16:24:42 vakatov * Kludging around the "NcbiEmptyString" to ensure its initialization when * it is used by the constructor of a statically allocated object * (I believe that it is actually just another Sun WorkShop compiler "feature") * * Revision 1.23 1999/12/28 18:55:43 vasilche * Reduced size of compiled object files: * 1. avoid inline or implicit virtual methods (especially destructors). * 2. avoid std::string's methods usage in inline methods. * 3. avoid string literals ("xxx") in inline methods. * * Revision 1.22 1999/12/17 19:04:09 vasilche * NcbiEmptyString made extern. * * Revision 1.21 1999/11/26 19:29:09 golikov * fix * * Revision 1.20 1999/11/26 18:45:17 golikov * NStr::Replace added * * Revision 1.19 1999/11/17 22:05:04 vakatov * [!HAVE_STRDUP] Emulate "strdup()" -- it's missing on some platforms * * Revision 1.18 1999/10/13 16:30:30 vasilche * Fixed bug in PNocase which appears under GCC implementation of STL. * * Revision 1.17 1999/07/08 16:10:14 vakatov * Fixed a warning in NStr::StringToUInt() * * Revision 1.16 1999/07/06 15:21:06 vakatov * + NStr::TruncateSpaces(const string& str, ETrunc where=eTrunc_Both) * * Revision 1.15 1999/06/15 20:50:05 vakatov * NStr:: +BoolToString, +StringToBool * * Revision 1.14 1999/05/27 15:21:40 vakatov * Fixed all StringToXXX() functions * * Revision 1.13 1999/05/17 20:10:36 vasilche * Fixed bug in NStr::StringToUInt which cause an exception. * * Revision 1.12 1999/05/04 00:03:13 vakatov * Removed the redundant severity arg from macro ERR_POST() * * Revision 1.11 1999/04/22 14:19:04 vasilche * Added _TRACE_THROW() macro, which can be configured to produce coredump * at a point of throwing an exception. * * Revision 1.10 1999/04/14 21:20:33 vakatov * Dont use "snprintf()" as it is not quite portable yet * * Revision 1.9 1999/04/14 19:57:36 vakatov * Use limits from <ncbitype.h> rather than from <limits>. * [MSVC++] fix for "snprintf()" in <ncbistd.hpp>. * * Revision 1.8 1999/04/09 19:51:37 sandomir * minor changes in NStr::StringToXXX - base added * * Revision 1.7 1999/01/21 16:18:04 sandomir * minor changes due to NStr namespace to contain string utility functions * * Revision 1.6 1999/01/11 22:05:50 vasilche * Fixed CHTML_font size. * Added CHTML_image input element. * * Revision 1.5 1998/12/28 17:56:39 vakatov * New CVS and development tree structure for the NCBI C++ projects * * Revision 1.4 1998/12/21 17:19:37 sandomir * VC++ fixes in ncbistd; minor fixes in Resource * * Revision 1.3 1998/12/17 21:50:45 sandomir * CNCBINode fixed in Resource; case insensitive string comparison predicate * added * * Revision 1.2 1998/12/15 17:36:33 vasilche * Fixed "double colon" bug in multithreaded version of headers. * * Revision 1.1 1998/12/15 15:43:22 vasilche * Added utilities to convert string <> int. * =========================================================================== */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -