string_util.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. //
  5. // This file defines utility functions for working with strings.
  6. #ifndef BUTIL_STRINGS_STRING_UTIL_H_
  7. #define BUTIL_STRINGS_STRING_UTIL_H_
  8. #include <ctype.h>
  9. #include <stdarg.h> // va_list
  10. #include <string>
  11. #include <vector>
  12. #include "butil/base_export.h"
  13. #include "butil/basictypes.h"
  14. #include "butil/compiler_specific.h"
  15. #include "butil/strings/string16.h"
  16. #include "butil/strings/string_piece.h" // For implicit conversions.
  17. namespace butil {
  18. // C standard-library functions like "strncasecmp" and "snprintf" that aren't
  19. // cross-platform are provided as "butil::strncasecmp", and their prototypes
  20. // are listed below. These functions are then implemented as inline calls
  21. // to the platform-specific equivalents in the platform-specific headers.
  22. // Compares the two strings s1 and s2 without regard to case using
  23. // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if
  24. // s2 > s1 according to a lexicographic comparison.
  25. int strcasecmp(const char* s1, const char* s2);
  26. // Compares up to count characters of s1 and s2 without regard to case using
  27. // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if
  28. // s2 > s1 according to a lexicographic comparison.
  29. int strncasecmp(const char* s1, const char* s2, size_t count);
  30. // Same as strncmp but for char16 strings.
  31. int strncmp16(const char16* s1, const char16* s2, size_t count);
  32. // Wrapper for vsnprintf that always null-terminates and always returns the
  33. // number of characters that would be in an untruncated formatted
  34. // string, even when truncation occurs.
  35. int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments)
  36. PRINTF_FORMAT(3, 0);
  37. // Some of these implementations need to be inlined.
  38. // We separate the declaration from the implementation of this inline
  39. // function just so the PRINTF_FORMAT works.
  40. inline int snprintf(char* buffer, size_t size, const char* format, ...)
  41. PRINTF_FORMAT(3, 4);
  42. inline int snprintf(char* buffer, size_t size, const char* format, ...) {
  43. va_list arguments;
  44. va_start(arguments, format);
  45. int result = vsnprintf(buffer, size, format, arguments);
  46. va_end(arguments);
  47. return result;
  48. }
  49. // BSD-style safe and consistent string copy functions.
  50. // Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|.
  51. // Copies at most |dst_size|-1 characters, and always NULL terminates |dst|, as
  52. // long as |dst_size| is not 0. Returns the length of |src| in characters.
  53. // If the return value is >= dst_size, then the output was truncated.
  54. // NOTE: All sizes are in number of characters, NOT in bytes.
  55. BUTIL_EXPORT size_t strlcpy(char* dst, const char* src, size_t dst_size);
  56. BUTIL_EXPORT size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size);
  57. // Scan a wprintf format string to determine whether it's portable across a
  58. // variety of systems. This function only checks that the conversion
  59. // specifiers used by the format string are supported and have the same meaning
  60. // on a variety of systems. It doesn't check for other errors that might occur
  61. // within a format string.
  62. //
  63. // Nonportable conversion specifiers for wprintf are:
  64. // - 's' and 'c' without an 'l' length modifier. %s and %c operate on char
  65. // data on all systems except Windows, which treat them as wchar_t data.
  66. // Use %ls and %lc for wchar_t data instead.
  67. // - 'S' and 'C', which operate on wchar_t data on all systems except Windows,
  68. // which treat them as char data. Use %ls and %lc for wchar_t data
  69. // instead.
  70. // - 'F', which is not identified by Windows wprintf documentation.
  71. // - 'D', 'O', and 'U', which are deprecated and not available on all systems.
  72. // Use %ld, %lo, and %lu instead.
  73. //
  74. // Note that there is no portable conversion specifier for char data when
  75. // working with wprintf.
  76. //
  77. // This function is intended to be called from butil::vswprintf.
  78. BUTIL_EXPORT bool IsWprintfFormatPortable(const wchar_t* format);
  79. // ASCII-specific tolower. The standard library's tolower is locale sensitive,
  80. // so we don't want to use it here.
  81. template <class Char> inline Char ToLowerASCII(Char c) {
  82. return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
  83. }
  84. // ASCII-specific toupper. The standard library's toupper is locale sensitive,
  85. // so we don't want to use it here.
  86. template <class Char> inline Char ToUpperASCII(Char c) {
  87. return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
  88. }
  89. // Function objects to aid in comparing/searching strings.
  90. template<typename Char> struct CaseInsensitiveCompare {
  91. public:
  92. bool operator()(Char x, Char y) const {
  93. // TODO(darin): Do we really want to do locale sensitive comparisons here?
  94. // See http://crbug.com/24917
  95. return tolower(x) == tolower(y);
  96. }
  97. };
  98. template<typename Char> struct CaseInsensitiveCompareASCII {
  99. public:
  100. bool operator()(Char x, Char y) const {
  101. return ToLowerASCII(x) == ToLowerASCII(y);
  102. }
  103. };
  104. // These threadsafe functions return references to globally unique empty
  105. // strings.
  106. //
  107. // It is likely faster to construct a new empty string object (just a few
  108. // instructions to set the length to 0) than to get the empty string singleton
  109. // returned by these functions (which requires threadsafe singleton access).
  110. //
  111. // Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT
  112. // CONSTRUCTORS. There is only one case where you should use these: functions
  113. // which need to return a string by reference (e.g. as a class member
  114. // accessor), and don't have an empty string to use (e.g. in an error case).
  115. // These should not be used as initializers, function arguments, or return
  116. // values for functions which return by value or outparam.
  117. BUTIL_EXPORT const std::string& EmptyString();
  118. BUTIL_EXPORT const string16& EmptyString16();
  119. // Contains the set of characters representing whitespace in the corresponding
  120. // encoding. Null-terminated.
  121. BUTIL_EXPORT extern const wchar_t kWhitespaceWide[];
  122. BUTIL_EXPORT extern const char16 kWhitespaceUTF16[];
  123. BUTIL_EXPORT extern const char kWhitespaceASCII[];
  124. // Null-terminated string representing the UTF-8 byte order mark.
  125. BUTIL_EXPORT extern const char kUtf8ByteOrderMark[];
  126. // Removes characters in |remove_chars| from anywhere in |input|. Returns true
  127. // if any characters were removed. |remove_chars| must be null-terminated.
  128. // NOTE: Safe to use the same variable for both |input| and |output|.
  129. BUTIL_EXPORT bool RemoveChars(const string16& input,
  130. const butil::StringPiece16& remove_chars,
  131. string16* output);
  132. BUTIL_EXPORT bool RemoveChars(const std::string& input,
  133. const butil::StringPiece& remove_chars,
  134. std::string* output);
  135. // Replaces characters in |replace_chars| from anywhere in |input| with
  136. // |replace_with|. Each character in |replace_chars| will be replaced with
  137. // the |replace_with| string. Returns true if any characters were replaced.
  138. // |replace_chars| must be null-terminated.
  139. // NOTE: Safe to use the same variable for both |input| and |output|.
  140. BUTIL_EXPORT bool ReplaceChars(const string16& input,
  141. const butil::StringPiece16& replace_chars,
  142. const string16& replace_with,
  143. string16* output);
  144. BUTIL_EXPORT bool ReplaceChars(const std::string& input,
  145. const butil::StringPiece& replace_chars,
  146. const std::string& replace_with,
  147. std::string* output);
  148. // Removes characters in |trim_chars| from the beginning and end of |input|.
  149. // |trim_chars| must be null-terminated.
  150. // NOTE: Safe to use the same variable for both |input| and |output|.
  151. BUTIL_EXPORT bool TrimString(const string16& input,
  152. const butil::StringPiece16& trim_chars,
  153. string16* output);
  154. BUTIL_EXPORT bool TrimString(const std::string& input,
  155. const butil::StringPiece& trim_chars,
  156. std::string* output);
  157. // Truncates a string to the nearest UTF-8 character that will leave
  158. // the string less than or equal to the specified byte size.
  159. BUTIL_EXPORT void TruncateUTF8ToByteSize(const std::string& input,
  160. const size_t byte_size,
  161. std::string* output);
  162. // Trims any whitespace from either end of the input string. Returns where
  163. // whitespace was found.
  164. // The non-wide version has two functions:
  165. // * TrimWhitespaceASCII()
  166. // This function is for ASCII strings and only looks for ASCII whitespace;
  167. // Please choose the best one according to your usage.
  168. // NOTE: Safe to use the same variable for both input and output.
  169. enum TrimPositions {
  170. TRIM_NONE = 0,
  171. TRIM_LEADING = 1 << 0,
  172. TRIM_TRAILING = 1 << 1,
  173. TRIM_ALL = TRIM_LEADING | TRIM_TRAILING,
  174. };
  175. BUTIL_EXPORT TrimPositions TrimWhitespace(const string16& input,
  176. TrimPositions positions,
  177. butil::string16* output);
  178. BUTIL_EXPORT TrimPositions TrimWhitespace(const butil::StringPiece16& input,
  179. TrimPositions positions,
  180. butil::StringPiece16* output);
  181. BUTIL_EXPORT TrimPositions TrimWhitespaceASCII(const std::string& input,
  182. TrimPositions positions,
  183. std::string* output);
  184. BUTIL_EXPORT TrimPositions TrimWhitespaceASCII(const butil::StringPiece& input,
  185. TrimPositions positions,
  186. butil::StringPiece* output);
  187. // Deprecated. This function is only for backward compatibility and calls
  188. // TrimWhitespaceASCII().
  189. BUTIL_EXPORT TrimPositions TrimWhitespace(const std::string& input,
  190. TrimPositions positions,
  191. std::string* output);
  192. BUTIL_EXPORT TrimPositions TrimWhitespace(const butil::StringPiece& input,
  193. TrimPositions positions,
  194. butil::StringPiece* output);
  195. // Searches for CR or LF characters. Removes all contiguous whitespace
  196. // strings that contain them. This is useful when trying to deal with text
  197. // copied from terminals.
  198. // Returns |text|, with the following three transformations:
  199. // (1) Leading and trailing whitespace is trimmed.
  200. // (2) If |trim_sequences_with_line_breaks| is true, any other whitespace
  201. // sequences containing a CR or LF are trimmed.
  202. // (3) All other whitespace sequences are converted to single spaces.
  203. BUTIL_EXPORT string16 CollapseWhitespace(
  204. const string16& text,
  205. bool trim_sequences_with_line_breaks);
  206. BUTIL_EXPORT std::string CollapseWhitespaceASCII(
  207. const std::string& text,
  208. bool trim_sequences_with_line_breaks);
  209. // Returns true if |input| is empty or contains only characters found in
  210. // |characters|.
  211. BUTIL_EXPORT bool ContainsOnlyChars(const StringPiece& input,
  212. const StringPiece& characters);
  213. BUTIL_EXPORT bool ContainsOnlyChars(const StringPiece16& input,
  214. const StringPiece16& characters);
  215. // Returns true if the specified string matches the criteria. How can a wide
  216. // string be 8-bit or UTF8? It contains only characters that are < 256 (in the
  217. // first case) or characters that use only 8-bits and whose 8-bit
  218. // representation looks like a UTF-8 string (the second case).
  219. //
  220. // Note that IsStringUTF8 checks not only if the input is structurally
  221. // valid but also if it doesn't contain any non-character codepoint
  222. // (e.g. U+FFFE). It's done on purpose because all the existing callers want
  223. // to have the maximum 'discriminating' power from other encodings. If
  224. // there's a use case for just checking the structural validity, we have to
  225. // add a new function for that.
  226. BUTIL_EXPORT bool IsStringUTF8(const std::string& str);
  227. BUTIL_EXPORT bool IsStringUTF8(const StringPiece& str);
  228. BUTIL_EXPORT bool IsStringASCII(const StringPiece& str);
  229. BUTIL_EXPORT bool IsStringASCII(const string16& str);
  230. } // namespace butil
  231. #if defined(OS_WIN)
  232. #include "butil/strings/string_util_win.h"
  233. #elif defined(OS_POSIX)
  234. #include "butil/strings/string_util_posix.h"
  235. #else
  236. #error Define string operations appropriately for your platform
  237. #endif
  238. // Converts the elements of the given string. This version uses a pointer to
  239. // clearly differentiate it from the non-pointer variant.
  240. template <class str> inline void StringToLowerASCII(str* s) {
  241. for (typename str::iterator i = s->begin(); i != s->end(); ++i)
  242. *i = butil::ToLowerASCII(*i);
  243. }
  244. template <class str> inline str StringToLowerASCII(const str& s) {
  245. // for std::string and std::wstring
  246. str output(s);
  247. StringToLowerASCII(&output);
  248. return output;
  249. }
  250. // Converts the elements of the given string. This version uses a pointer to
  251. // clearly differentiate it from the non-pointer variant.
  252. template <class str> inline void StringToUpperASCII(str* s) {
  253. for (typename str::iterator i = s->begin(); i != s->end(); ++i)
  254. *i = butil::ToUpperASCII(*i);
  255. }
  256. template <class str> inline str StringToUpperASCII(const str& s) {
  257. // for std::string and std::wstring
  258. str output(s);
  259. StringToUpperASCII(&output);
  260. return output;
  261. }
  262. // Compare the lower-case form of the given string against the given ASCII
  263. // string. This is useful for doing checking if an input string matches some
  264. // token, and it is optimized to avoid intermediate string copies. This API is
  265. // borrowed from the equivalent APIs in Mozilla.
  266. BUTIL_EXPORT bool LowerCaseEqualsASCII(const std::string& a, const char* b);
  267. BUTIL_EXPORT bool LowerCaseEqualsASCII(const butil::string16& a, const char* b);
  268. // Same thing, but with string iterators instead.
  269. BUTIL_EXPORT bool LowerCaseEqualsASCII(std::string::const_iterator a_begin,
  270. std::string::const_iterator a_end,
  271. const char* b);
  272. BUTIL_EXPORT bool LowerCaseEqualsASCII(butil::string16::const_iterator a_begin,
  273. butil::string16::const_iterator a_end,
  274. const char* b);
  275. BUTIL_EXPORT bool LowerCaseEqualsASCII(const char* a_begin,
  276. const char* a_end,
  277. const char* b);
  278. BUTIL_EXPORT bool LowerCaseEqualsASCII(const butil::char16* a_begin,
  279. const butil::char16* a_end,
  280. const char* b);
  281. // Performs a case-sensitive string compare. The behavior is undefined if both
  282. // strings are not ASCII.
  283. BUTIL_EXPORT bool EqualsASCII(const butil::string16& a, const butil::StringPiece& b);
  284. // Returns true if str starts with search, or false otherwise.
  285. BUTIL_EXPORT bool StartsWithASCII(const std::string& str,
  286. const std::string& search,
  287. bool case_sensitive);
  288. BUTIL_EXPORT bool StartsWith(const butil::string16& str,
  289. const butil::string16& search,
  290. bool case_sensitive);
  291. // Returns true if str ends with search, or false otherwise.
  292. BUTIL_EXPORT bool EndsWith(const std::string& str,
  293. const std::string& search,
  294. bool case_sensitive);
  295. BUTIL_EXPORT bool EndsWith(const butil::string16& str,
  296. const butil::string16& search,
  297. bool case_sensitive);
  298. // Determines the type of ASCII character, independent of locale (the C
  299. // library versions will change based on locale).
  300. template <typename Char>
  301. inline bool IsAsciiWhitespace(Char c) {
  302. return c == ' ' || c == '\r' || c == '\n' || c == '\t';
  303. }
  304. template <typename Char>
  305. inline bool IsAsciiAlpha(Char c) {
  306. return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'));
  307. }
  308. template <typename Char>
  309. inline bool IsAsciiDigit(Char c) {
  310. return c >= '0' && c <= '9';
  311. }
  312. template <typename Char>
  313. inline bool IsHexDigit(Char c) {
  314. return (c >= '0' && c <= '9') ||
  315. (c >= 'A' && c <= 'F') ||
  316. (c >= 'a' && c <= 'f');
  317. }
  318. template <typename Char>
  319. inline Char HexDigitToInt(Char c) {
  320. DCHECK(IsHexDigit(c));
  321. if (c >= '0' && c <= '9')
  322. return c - '0';
  323. if (c >= 'A' && c <= 'F')
  324. return c - 'A' + 10;
  325. if (c >= 'a' && c <= 'f')
  326. return c - 'a' + 10;
  327. return 0;
  328. }
  329. // Returns true if it's a whitespace character.
  330. inline bool IsWhitespace(wchar_t c) {
  331. return wcschr(butil::kWhitespaceWide, c) != NULL;
  332. }
  333. // Return a byte string in human-readable format with a unit suffix. Not
  334. // appropriate for use in any UI; use of FormatBytes and friends in ui/base is
  335. // highly recommended instead. TODO(avi): Figure out how to get callers to use
  336. // FormatBytes instead; remove this.
  337. BUTIL_EXPORT butil::string16 FormatBytesUnlocalized(int64_t bytes);
  338. // Starting at |start_offset| (usually 0), replace the first instance of
  339. // |find_this| with |replace_with|.
  340. BUTIL_EXPORT void ReplaceFirstSubstringAfterOffset(
  341. butil::string16* str,
  342. size_t start_offset,
  343. const butil::string16& find_this,
  344. const butil::string16& replace_with);
  345. BUTIL_EXPORT void ReplaceFirstSubstringAfterOffset(
  346. std::string* str,
  347. size_t start_offset,
  348. const std::string& find_this,
  349. const std::string& replace_with);
  350. // Starting at |start_offset| (usually 0), look through |str| and replace all
  351. // instances of |find_this| with |replace_with|.
  352. //
  353. // This does entire substrings; use std::replace in <algorithm> for single
  354. // characters, for example:
  355. // std::replace(str.begin(), str.end(), 'a', 'b');
  356. BUTIL_EXPORT void ReplaceSubstringsAfterOffset(
  357. butil::string16* str,
  358. size_t start_offset,
  359. const butil::string16& find_this,
  360. const butil::string16& replace_with);
  361. BUTIL_EXPORT void ReplaceSubstringsAfterOffset(std::string* str,
  362. size_t start_offset,
  363. const std::string& find_this,
  364. const std::string& replace_with);
  365. // Reserves enough memory in |str| to accommodate |length_with_null| characters,
  366. // sets the size of |str| to |length_with_null - 1| characters, and returns a
  367. // pointer to the underlying contiguous array of characters. This is typically
  368. // used when calling a function that writes results into a character array, but
  369. // the caller wants the data to be managed by a string-like object. It is
  370. // convenient in that is can be used inline in the call, and fast in that it
  371. // avoids copying the results of the call from a char* into a string.
  372. //
  373. // |length_with_null| must be at least 2, since otherwise the underlying string
  374. // would have size 0, and trying to access &((*str)[0]) in that case can result
  375. // in a number of problems.
  376. //
  377. // Internally, this takes linear time because the resize() call 0-fills the
  378. // underlying array for potentially all
  379. // (|length_with_null - 1| * sizeof(string_type::value_type)) bytes. Ideally we
  380. // could avoid this aspect of the resize() call, as we expect the caller to
  381. // immediately write over this memory, but there is no other way to set the size
  382. // of the string, and not doing that will mean people who access |str| rather
  383. // than str.c_str() will get back a string of whatever size |str| had on entry
  384. // to this function (probably 0).
  385. template <class string_type>
  386. inline typename string_type::value_type* WriteInto(string_type* str,
  387. size_t length_with_null) {
  388. DCHECK_GT(length_with_null, 1u);
  389. str->reserve(length_with_null);
  390. str->resize(length_with_null - 1);
  391. return &((*str)[0]);
  392. }
  393. //-----------------------------------------------------------------------------
  394. // Splits a string into its fields delimited by any of the characters in
  395. // |delimiters|. Each field is added to the |tokens| vector. Returns the
  396. // number of tokens found.
  397. BUTIL_EXPORT size_t Tokenize(const butil::string16& str,
  398. const butil::string16& delimiters,
  399. std::vector<butil::string16>* tokens);
  400. BUTIL_EXPORT size_t Tokenize(const std::string& str,
  401. const std::string& delimiters,
  402. std::vector<std::string>* tokens);
  403. BUTIL_EXPORT size_t Tokenize(const butil::StringPiece& str,
  404. const butil::StringPiece& delimiters,
  405. std::vector<butil::StringPiece>* tokens);
  406. // Does the opposite of SplitString().
  407. BUTIL_EXPORT butil::string16 JoinString(const std::vector<butil::string16>& parts,
  408. butil::char16 s);
  409. BUTIL_EXPORT std::string JoinString(
  410. const std::vector<std::string>& parts, char s);
  411. // Join |parts| using |separator|.
  412. BUTIL_EXPORT std::string JoinString(
  413. const std::vector<std::string>& parts,
  414. const std::string& separator);
  415. BUTIL_EXPORT butil::string16 JoinString(
  416. const std::vector<butil::string16>& parts,
  417. const butil::string16& separator);
  418. // Replace $1-$2-$3..$9 in the format string with |a|-|b|-|c|..|i| respectively.
  419. // Additionally, any number of consecutive '$' characters is replaced by that
  420. // number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be
  421. // NULL. This only allows you to use up to nine replacements.
  422. BUTIL_EXPORT butil::string16 ReplaceStringPlaceholders(
  423. const butil::string16& format_string,
  424. const std::vector<butil::string16>& subst,
  425. std::vector<size_t>* offsets);
  426. BUTIL_EXPORT std::string ReplaceStringPlaceholders(
  427. const butil::StringPiece& format_string,
  428. const std::vector<std::string>& subst,
  429. std::vector<size_t>* offsets);
  430. // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL.
  431. BUTIL_EXPORT butil::string16 ReplaceStringPlaceholders(
  432. const butil::string16& format_string,
  433. const butil::string16& a,
  434. size_t* offset);
  435. // Returns true if the string passed in matches the pattern. The pattern
  436. // string can contain wildcards like * and ?
  437. // The backslash character (\) is an escape character for * and ?
  438. // We limit the patterns to having a max of 16 * or ? characters.
  439. // ? matches 0 or 1 character, while * matches 0 or more characters.
  440. BUTIL_EXPORT bool MatchPattern(const butil::StringPiece& string,
  441. const butil::StringPiece& pattern);
  442. BUTIL_EXPORT bool MatchPattern(const butil::string16& string,
  443. const butil::string16& pattern);
  444. // Hack to convert any char-like type to its unsigned counterpart.
  445. // For example, it will convert char, signed char and unsigned char to unsigned
  446. // char.
  447. template<typename T>
  448. struct ToUnsigned {
  449. typedef T Unsigned;
  450. };
  451. template<>
  452. struct ToUnsigned<char> {
  453. typedef unsigned char Unsigned;
  454. };
  455. template<>
  456. struct ToUnsigned<signed char> {
  457. typedef unsigned char Unsigned;
  458. };
  459. template<>
  460. struct ToUnsigned<wchar_t> {
  461. #if defined(WCHAR_T_IS_UTF16)
  462. typedef unsigned short Unsigned;
  463. #elif defined(WCHAR_T_IS_UTF32)
  464. typedef uint32_t Unsigned;
  465. #endif
  466. };
  467. template<>
  468. struct ToUnsigned<short> {
  469. typedef unsigned short Unsigned;
  470. };
  471. #endif // BUTIL_STRINGS_STRING_UTIL_H_