string_util.h 22.2 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4 5 6
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This file defines utility functions for working with strings.

7 8
#ifndef BUTIL_STRINGS_STRING_UTIL_H_
#define BUTIL_STRINGS_STRING_UTIL_H_
gejun's avatar
gejun committed
9 10 11 12 13 14 15

#include <ctype.h>
#include <stdarg.h>   // va_list

#include <string>
#include <vector>

16 17 18 19 20
#include "butil/base_export.h"
#include "butil/basictypes.h"
#include "butil/compiler_specific.h"
#include "butil/strings/string16.h"
#include "butil/strings/string_piece.h"  // For implicit conversions.
gejun's avatar
gejun committed
21

22
namespace butil {
gejun's avatar
gejun committed
23 24

// C standard-library functions like "strncasecmp" and "snprintf" that aren't
25
// cross-platform are provided as "butil::strncasecmp", and their prototypes
gejun's avatar
gejun committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
// are listed below.  These functions are then implemented as inline calls
// to the platform-specific equivalents in the platform-specific headers.

// Compares the two strings s1 and s2 without regard to case using
// the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if
// s2 > s1 according to a lexicographic comparison.
int strcasecmp(const char* s1, const char* s2);

// Compares up to count characters of s1 and s2 without regard to case using
// the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if
// s2 > s1 according to a lexicographic comparison.
int strncasecmp(const char* s1, const char* s2, size_t count);

// Same as strncmp but for char16 strings.
int strncmp16(const char16* s1, const char16* s2, size_t count);

// Wrapper for vsnprintf that always null-terminates and always returns the
// number of characters that would be in an untruncated formatted
// string, even when truncation occurs.
int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments)
    PRINTF_FORMAT(3, 0);

// Some of these implementations need to be inlined.

// We separate the declaration from the implementation of this inline
// function just so the PRINTF_FORMAT works.
inline int snprintf(char* buffer, size_t size, const char* format, ...)
    PRINTF_FORMAT(3, 4);
inline int snprintf(char* buffer, size_t size, const char* format, ...) {
  va_list arguments;
  va_start(arguments, format);
  int result = vsnprintf(buffer, size, format, arguments);
  va_end(arguments);
  return result;
}

// BSD-style safe and consistent string copy functions.
// Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|.
// Copies at most |dst_size|-1 characters, and always NULL terminates |dst|, as
// long as |dst_size| is not 0.  Returns the length of |src| in characters.
// If the return value is >= dst_size, then the output was truncated.
// NOTE: All sizes are in number of characters, NOT in bytes.
68 69
BUTIL_EXPORT size_t strlcpy(char* dst, const char* src, size_t dst_size);
BUTIL_EXPORT size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size);
gejun's avatar
gejun committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

// Scan a wprintf format string to determine whether it's portable across a
// variety of systems.  This function only checks that the conversion
// specifiers used by the format string are supported and have the same meaning
// on a variety of systems.  It doesn't check for other errors that might occur
// within a format string.
//
// Nonportable conversion specifiers for wprintf are:
//  - 's' and 'c' without an 'l' length modifier.  %s and %c operate on char
//     data on all systems except Windows, which treat them as wchar_t data.
//     Use %ls and %lc for wchar_t data instead.
//  - 'S' and 'C', which operate on wchar_t data on all systems except Windows,
//     which treat them as char data.  Use %ls and %lc for wchar_t data
//     instead.
//  - 'F', which is not identified by Windows wprintf documentation.
//  - 'D', 'O', and 'U', which are deprecated and not available on all systems.
//     Use %ld, %lo, and %lu instead.
//
// Note that there is no portable conversion specifier for char data when
// working with wprintf.
//
91
// This function is intended to be called from butil::vswprintf.
92
BUTIL_EXPORT bool IsWprintfFormatPortable(const wchar_t* format);
gejun's avatar
gejun committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

// ASCII-specific tolower.  The standard library's tolower is locale sensitive,
// so we don't want to use it here.
template <class Char> inline Char ToLowerASCII(Char c) {
  return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
}

// ASCII-specific toupper.  The standard library's toupper is locale sensitive,
// so we don't want to use it here.
template <class Char> inline Char ToUpperASCII(Char c) {
  return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
}

// Function objects to aid in comparing/searching strings.

template<typename Char> struct CaseInsensitiveCompare {
 public:
  bool operator()(Char x, Char y) const {
    // TODO(darin): Do we really want to do locale sensitive comparisons here?
    // See http://crbug.com/24917
    return tolower(x) == tolower(y);
  }
};

template<typename Char> struct CaseInsensitiveCompareASCII {
 public:
  bool operator()(Char x, Char y) const {
    return ToLowerASCII(x) == ToLowerASCII(y);
  }
};

// These threadsafe functions return references to globally unique empty
// strings.
//
// It is likely faster to construct a new empty string object (just a few
// instructions to set the length to 0) than to get the empty string singleton
// returned by these functions (which requires threadsafe singleton access).
//
// Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT
// CONSTRUCTORS. There is only one case where you should use these: functions
// which need to return a string by reference (e.g. as a class member
// accessor), and don't have an empty string to use (e.g. in an error case).
// These should not be used as initializers, function arguments, or return
// values for functions which return by value or outparam.
137 138
BUTIL_EXPORT const std::string& EmptyString();
BUTIL_EXPORT const string16& EmptyString16();
gejun's avatar
gejun committed
139 140 141

// Contains the set of characters representing whitespace in the corresponding
// encoding. Null-terminated.
142 143 144
BUTIL_EXPORT extern const wchar_t kWhitespaceWide[];
BUTIL_EXPORT extern const char16 kWhitespaceUTF16[];
BUTIL_EXPORT extern const char kWhitespaceASCII[];
gejun's avatar
gejun committed
145 146

// Null-terminated string representing the UTF-8 byte order mark.
147
BUTIL_EXPORT extern const char kUtf8ByteOrderMark[];
gejun's avatar
gejun committed
148 149 150 151

// Removes characters in |remove_chars| from anywhere in |input|.  Returns true
// if any characters were removed.  |remove_chars| must be null-terminated.
// NOTE: Safe to use the same variable for both |input| and |output|.
152
BUTIL_EXPORT bool RemoveChars(const string16& input,
153
                             const butil::StringPiece16& remove_chars,
gejun's avatar
gejun committed
154
                             string16* output);
155
BUTIL_EXPORT bool RemoveChars(const std::string& input,
156
                             const butil::StringPiece& remove_chars,
gejun's avatar
gejun committed
157 158 159 160 161 162 163
                             std::string* output);

// Replaces characters in |replace_chars| from anywhere in |input| with
// |replace_with|.  Each character in |replace_chars| will be replaced with
// the |replace_with| string.  Returns true if any characters were replaced.
// |replace_chars| must be null-terminated.
// NOTE: Safe to use the same variable for both |input| and |output|.
164
BUTIL_EXPORT bool ReplaceChars(const string16& input,
165
                              const butil::StringPiece16& replace_chars,
gejun's avatar
gejun committed
166 167
                              const string16& replace_with,
                              string16* output);
168
BUTIL_EXPORT bool ReplaceChars(const std::string& input,
169
                              const butil::StringPiece& replace_chars,
gejun's avatar
gejun committed
170 171 172 173 174 175
                              const std::string& replace_with,
                              std::string* output);

// Removes characters in |trim_chars| from the beginning and end of |input|.
// |trim_chars| must be null-terminated.
// NOTE: Safe to use the same variable for both |input| and |output|.
176
BUTIL_EXPORT bool TrimString(const string16& input,
177
                            const butil::StringPiece16& trim_chars,
gejun's avatar
gejun committed
178
                            string16* output);
179
BUTIL_EXPORT bool TrimString(const std::string& input,
180
                            const butil::StringPiece& trim_chars,
gejun's avatar
gejun committed
181 182 183 184
                            std::string* output);

// Truncates a string to the nearest UTF-8 character that will leave
// the string less than or equal to the specified byte size.
185
BUTIL_EXPORT void TruncateUTF8ToByteSize(const std::string& input,
gejun's avatar
gejun committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
                                        const size_t byte_size,
                                        std::string* output);

// Trims any whitespace from either end of the input string.  Returns where
// whitespace was found.
// The non-wide version has two functions:
// * TrimWhitespaceASCII()
//   This function is for ASCII strings and only looks for ASCII whitespace;
// Please choose the best one according to your usage.
// NOTE: Safe to use the same variable for both input and output.
enum TrimPositions {
  TRIM_NONE     = 0,
  TRIM_LEADING  = 1 << 0,
  TRIM_TRAILING = 1 << 1,
  TRIM_ALL      = TRIM_LEADING | TRIM_TRAILING,
};
202
BUTIL_EXPORT TrimPositions TrimWhitespace(const string16& input,
gejun's avatar
gejun committed
203
                                         TrimPositions positions,
204
                                         butil::string16* output);
205
BUTIL_EXPORT TrimPositions TrimWhitespaceASCII(const std::string& input,
gejun's avatar
gejun committed
206 207 208 209 210
                                              TrimPositions positions,
                                              std::string* output);

// Deprecated. This function is only for backward compatibility and calls
// TrimWhitespaceASCII().
211
BUTIL_EXPORT TrimPositions TrimWhitespace(const std::string& input,
gejun's avatar
gejun committed
212 213 214 215 216 217 218 219 220 221 222
                                         TrimPositions positions,
                                         std::string* output);

// Searches  for CR or LF characters.  Removes all contiguous whitespace
// strings that contain them.  This is useful when trying to deal with text
// copied from terminals.
// Returns |text|, with the following three transformations:
// (1) Leading and trailing whitespace is trimmed.
// (2) If |trim_sequences_with_line_breaks| is true, any other whitespace
//     sequences containing a CR or LF are trimmed.
// (3) All other whitespace sequences are converted to single spaces.
223
BUTIL_EXPORT string16 CollapseWhitespace(
gejun's avatar
gejun committed
224 225
    const string16& text,
    bool trim_sequences_with_line_breaks);
226
BUTIL_EXPORT std::string CollapseWhitespaceASCII(
gejun's avatar
gejun committed
227 228 229 230 231
    const std::string& text,
    bool trim_sequences_with_line_breaks);

// Returns true if |input| is empty or contains only characters found in
// |characters|.
232
BUTIL_EXPORT bool ContainsOnlyChars(const StringPiece& input,
gejun's avatar
gejun committed
233
                                   const StringPiece& characters);
234
BUTIL_EXPORT bool ContainsOnlyChars(const StringPiece16& input,
gejun's avatar
gejun committed
235 236 237 238 239 240 241 242 243 244 245 246 247
                                   const StringPiece16& characters);

// Returns true if the specified string matches the criteria. How can a wide
// string be 8-bit or UTF8? It contains only characters that are < 256 (in the
// first case) or characters that use only 8-bits and whose 8-bit
// representation looks like a UTF-8 string (the second case).
//
// Note that IsStringUTF8 checks not only if the input is structurally
// valid but also if it doesn't contain any non-character codepoint
// (e.g. U+FFFE). It's done on purpose because all the existing callers want
// to have the maximum 'discriminating' power from other encodings. If
// there's a use case for just checking the structural validity, we have to
// add a new function for that.
248 249 250
BUTIL_EXPORT bool IsStringUTF8(const std::string& str);
BUTIL_EXPORT bool IsStringASCII(const StringPiece& str);
BUTIL_EXPORT bool IsStringASCII(const string16& str);
gejun's avatar
gejun committed
251

252
}  // namespace butil
gejun's avatar
gejun committed
253 254

#if defined(OS_WIN)
255
#include "butil/strings/string_util_win.h"
gejun's avatar
gejun committed
256
#elif defined(OS_POSIX)
257
#include "butil/strings/string_util_posix.h"
gejun's avatar
gejun committed
258 259 260 261 262 263 264 265
#else
#error Define string operations appropriately for your platform
#endif

// Converts the elements of the given string.  This version uses a pointer to
// clearly differentiate it from the non-pointer variant.
template <class str> inline void StringToLowerASCII(str* s) {
  for (typename str::iterator i = s->begin(); i != s->end(); ++i)
266
    *i = butil::ToLowerASCII(*i);
gejun's avatar
gejun committed
267 268 269 270 271 272 273 274 275 276 277 278 279
}

template <class str> inline str StringToLowerASCII(const str& s) {
  // for std::string and std::wstring
  str output(s);
  StringToLowerASCII(&output);
  return output;
}

// Converts the elements of the given string.  This version uses a pointer to
// clearly differentiate it from the non-pointer variant.
template <class str> inline void StringToUpperASCII(str* s) {
  for (typename str::iterator i = s->begin(); i != s->end(); ++i)
280
    *i = butil::ToUpperASCII(*i);
gejun's avatar
gejun committed
281 282 283 284 285 286 287 288 289 290 291 292 293
}

template <class str> inline str StringToUpperASCII(const str& s) {
  // for std::string and std::wstring
  str output(s);
  StringToUpperASCII(&output);
  return output;
}

// Compare the lower-case form of the given string against the given ASCII
// string.  This is useful for doing checking if an input string matches some
// token, and it is optimized to avoid intermediate string copies.  This API is
// borrowed from the equivalent APIs in Mozilla.
294 295
BUTIL_EXPORT bool LowerCaseEqualsASCII(const std::string& a, const char* b);
BUTIL_EXPORT bool LowerCaseEqualsASCII(const butil::string16& a, const char* b);
gejun's avatar
gejun committed
296 297

// Same thing, but with string iterators instead.
298
BUTIL_EXPORT bool LowerCaseEqualsASCII(std::string::const_iterator a_begin,
gejun's avatar
gejun committed
299 300
                                      std::string::const_iterator a_end,
                                      const char* b);
301
BUTIL_EXPORT bool LowerCaseEqualsASCII(butil::string16::const_iterator a_begin,
302
                                      butil::string16::const_iterator a_end,
gejun's avatar
gejun committed
303
                                      const char* b);
304
BUTIL_EXPORT bool LowerCaseEqualsASCII(const char* a_begin,
gejun's avatar
gejun committed
305 306
                                      const char* a_end,
                                      const char* b);
307
BUTIL_EXPORT bool LowerCaseEqualsASCII(const butil::char16* a_begin,
308
                                      const butil::char16* a_end,
gejun's avatar
gejun committed
309 310 311 312
                                      const char* b);

// Performs a case-sensitive string compare. The behavior is undefined if both
// strings are not ASCII.
313
BUTIL_EXPORT bool EqualsASCII(const butil::string16& a, const butil::StringPiece& b);
gejun's avatar
gejun committed
314 315

// Returns true if str starts with search, or false otherwise.
316
BUTIL_EXPORT bool StartsWithASCII(const std::string& str,
gejun's avatar
gejun committed
317 318
                                 const std::string& search,
                                 bool case_sensitive);
319
BUTIL_EXPORT bool StartsWith(const butil::string16& str,
320
                            const butil::string16& search,
gejun's avatar
gejun committed
321 322 323
                            bool case_sensitive);

// Returns true if str ends with search, or false otherwise.
324
BUTIL_EXPORT bool EndsWith(const std::string& str,
gejun's avatar
gejun committed
325 326
                          const std::string& search,
                          bool case_sensitive);
327
BUTIL_EXPORT bool EndsWith(const butil::string16& str,
328
                          const butil::string16& search,
gejun's avatar
gejun committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
                          bool case_sensitive);


// Determines the type of ASCII character, independent of locale (the C
// library versions will change based on locale).
template <typename Char>
inline bool IsAsciiWhitespace(Char c) {
  return c == ' ' || c == '\r' || c == '\n' || c == '\t';
}
template <typename Char>
inline bool IsAsciiAlpha(Char c) {
  return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'));
}
template <typename Char>
inline bool IsAsciiDigit(Char c) {
  return c >= '0' && c <= '9';
}

template <typename Char>
inline bool IsHexDigit(Char c) {
  return (c >= '0' && c <= '9') ||
         (c >= 'A' && c <= 'F') ||
         (c >= 'a' && c <= 'f');
}

template <typename Char>
inline Char HexDigitToInt(Char c) {
  DCHECK(IsHexDigit(c));
  if (c >= '0' && c <= '9')
    return c - '0';
  if (c >= 'A' && c <= 'F')
    return c - 'A' + 10;
  if (c >= 'a' && c <= 'f')
    return c - 'a' + 10;
  return 0;
}

// Returns true if it's a whitespace character.
inline bool IsWhitespace(wchar_t c) {
368
  return wcschr(butil::kWhitespaceWide, c) != NULL;
gejun's avatar
gejun committed
369 370 371 372 373 374
}

// Return a byte string in human-readable format with a unit suffix. Not
// appropriate for use in any UI; use of FormatBytes and friends in ui/base is
// highly recommended instead. TODO(avi): Figure out how to get callers to use
// FormatBytes instead; remove this.
375
BUTIL_EXPORT butil::string16 FormatBytesUnlocalized(int64_t bytes);
gejun's avatar
gejun committed
376 377 378

// Starting at |start_offset| (usually 0), replace the first instance of
// |find_this| with |replace_with|.
379
BUTIL_EXPORT void ReplaceFirstSubstringAfterOffset(
380
    butil::string16* str,
gejun's avatar
gejun committed
381
    size_t start_offset,
382 383
    const butil::string16& find_this,
    const butil::string16& replace_with);
384
BUTIL_EXPORT void ReplaceFirstSubstringAfterOffset(
gejun's avatar
gejun committed
385 386 387 388 389 390 391 392 393 394 395
    std::string* str,
    size_t start_offset,
    const std::string& find_this,
    const std::string& replace_with);

// Starting at |start_offset| (usually 0), look through |str| and replace all
// instances of |find_this| with |replace_with|.
//
// This does entire substrings; use std::replace in <algorithm> for single
// characters, for example:
//   std::replace(str.begin(), str.end(), 'a', 'b');
396
BUTIL_EXPORT void ReplaceSubstringsAfterOffset(
397
    butil::string16* str,
gejun's avatar
gejun committed
398
    size_t start_offset,
399 400
    const butil::string16& find_this,
    const butil::string16& replace_with);
401
BUTIL_EXPORT void ReplaceSubstringsAfterOffset(std::string* str,
gejun's avatar
gejun committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
                                              size_t start_offset,
                                              const std::string& find_this,
                                              const std::string& replace_with);

// Reserves enough memory in |str| to accommodate |length_with_null| characters,
// sets the size of |str| to |length_with_null - 1| characters, and returns a
// pointer to the underlying contiguous array of characters.  This is typically
// used when calling a function that writes results into a character array, but
// the caller wants the data to be managed by a string-like object.  It is
// convenient in that is can be used inline in the call, and fast in that it
// avoids copying the results of the call from a char* into a string.
//
// |length_with_null| must be at least 2, since otherwise the underlying string
// would have size 0, and trying to access &((*str)[0]) in that case can result
// in a number of problems.
//
// Internally, this takes linear time because the resize() call 0-fills the
// underlying array for potentially all
// (|length_with_null - 1| * sizeof(string_type::value_type)) bytes.  Ideally we
// could avoid this aspect of the resize() call, as we expect the caller to
// immediately write over this memory, but there is no other way to set the size
// of the string, and not doing that will mean people who access |str| rather
// than str.c_str() will get back a string of whatever size |str| had on entry
// to this function (probably 0).
template <class string_type>
inline typename string_type::value_type* WriteInto(string_type* str,
                                                   size_t length_with_null) {
  DCHECK_GT(length_with_null, 1u);
  str->reserve(length_with_null);
  str->resize(length_with_null - 1);
  return &((*str)[0]);
}

//-----------------------------------------------------------------------------

// Splits a string into its fields delimited by any of the characters in
// |delimiters|.  Each field is added to the |tokens| vector.  Returns the
// number of tokens found.
440
BUTIL_EXPORT size_t Tokenize(const butil::string16& str,
441 442
                            const butil::string16& delimiters,
                            std::vector<butil::string16>* tokens);
443
BUTIL_EXPORT size_t Tokenize(const std::string& str,
gejun's avatar
gejun committed
444 445
                            const std::string& delimiters,
                            std::vector<std::string>* tokens);
446
BUTIL_EXPORT size_t Tokenize(const butil::StringPiece& str,
447 448
                            const butil::StringPiece& delimiters,
                            std::vector<butil::StringPiece>* tokens);
gejun's avatar
gejun committed
449 450

// Does the opposite of SplitString().
451
BUTIL_EXPORT butil::string16 JoinString(const std::vector<butil::string16>& parts,
452
                                      butil::char16 s);
453
BUTIL_EXPORT std::string JoinString(
gejun's avatar
gejun committed
454 455 456
    const std::vector<std::string>& parts, char s);

// Join |parts| using |separator|.
457
BUTIL_EXPORT std::string JoinString(
gejun's avatar
gejun committed
458 459
    const std::vector<std::string>& parts,
    const std::string& separator);
460
BUTIL_EXPORT butil::string16 JoinString(
461 462
    const std::vector<butil::string16>& parts,
    const butil::string16& separator);
gejun's avatar
gejun committed
463 464 465 466 467

// Replace $1-$2-$3..$9 in the format string with |a|-|b|-|c|..|i| respectively.
// Additionally, any number of consecutive '$' characters is replaced by that
// number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be
// NULL. This only allows you to use up to nine replacements.
468
BUTIL_EXPORT butil::string16 ReplaceStringPlaceholders(
469 470
    const butil::string16& format_string,
    const std::vector<butil::string16>& subst,
gejun's avatar
gejun committed
471 472
    std::vector<size_t>* offsets);

473
BUTIL_EXPORT std::string ReplaceStringPlaceholders(
474
    const butil::StringPiece& format_string,
gejun's avatar
gejun committed
475 476 477 478
    const std::vector<std::string>& subst,
    std::vector<size_t>* offsets);

// Single-string shortcut for ReplaceStringHolders. |offset| may be NULL.
479
BUTIL_EXPORT butil::string16 ReplaceStringPlaceholders(
480 481
    const butil::string16& format_string,
    const butil::string16& a,
gejun's avatar
gejun committed
482 483 484 485 486 487 488
    size_t* offset);

// Returns true if the string passed in matches the pattern. The pattern
// string can contain wildcards like * and ?
// The backslash character (\) is an escape character for * and ?
// We limit the patterns to having a max of 16 * or ? characters.
// ? matches 0 or 1 character, while * matches 0 or more characters.
489
BUTIL_EXPORT bool MatchPattern(const butil::StringPiece& string,
490
                              const butil::StringPiece& pattern);
491
BUTIL_EXPORT bool MatchPattern(const butil::string16& string,
492
                              const butil::string16& pattern);
gejun's avatar
gejun committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

// Hack to convert any char-like type to its unsigned counterpart.
// For example, it will convert char, signed char and unsigned char to unsigned
// char.
template<typename T>
struct ToUnsigned {
  typedef T Unsigned;
};

template<>
struct ToUnsigned<char> {
  typedef unsigned char Unsigned;
};
template<>
struct ToUnsigned<signed char> {
  typedef unsigned char Unsigned;
};
template<>
struct ToUnsigned<wchar_t> {
#if defined(WCHAR_T_IS_UTF16)
  typedef unsigned short Unsigned;
#elif defined(WCHAR_T_IS_UTF32)
  typedef uint32_t Unsigned;
#endif
};
template<>
struct ToUnsigned<short> {
  typedef unsigned short Unsigned;
};

523
#endif  // BUTIL_STRINGS_STRING_UTIL_H_