ipc/chromium/src/base/string_util.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // Copyright (c) 2006-2008 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.
     7 #ifndef BASE_STRING_UTIL_H_
     8 #define BASE_STRING_UTIL_H_
    10 #include <stdarg.h>   // va_list
    11 #include <ctype.h>
    13 #include <string>
    14 #include <vector>
    16 #include "base/basictypes.h"
    17 #include "base/string16.h"
    18 #include "base/string_piece.h"  // For implicit conversions.
    20 // Safe standard library wrappers for all platforms.
    22 namespace base {
    24 // C standard-library functions like "strncasecmp" and "snprintf" that aren't
    25 // cross-platform are provided as "base::strncasecmp", and their prototypes
    26 // are listed below.  These functions are then implemented as inline calls
    27 // to the platform-specific equivalents in the platform-specific headers.
    29 // Compare the two strings s1 and s2 without regard to case using
    30 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if
    31 // s2 > s1 according to a lexicographic comparison.
    32 int strcasecmp(const char* s1, const char* s2);
    34 // Compare up to count characters of s1 and s2 without regard to case using
    35 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if
    36 // s2 > s1 according to a lexicographic comparison.
    37 int strncasecmp(const char* s1, const char* s2, size_t count);
    39 // Wrapper for vsnprintf that always null-terminates and always returns the
    40 // number of characters that would be in an untruncated formatted
    41 // string, even when truncation occurs.
    42 int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments);
    44 // vswprintf always null-terminates, but when truncation occurs, it will either
    45 // return -1 or the number of characters that would be in an untruncated
    46 // formatted string.  The actual return value depends on the underlying
    47 // C library's vswprintf implementation.
    48 int vswprintf(wchar_t* buffer, size_t size,
    49               const wchar_t* format, va_list arguments);
    51 // Some of these implementations need to be inlined.
    53 inline int snprintf(char* buffer, size_t size, const char* format, ...) {
    54   va_list arguments;
    55   va_start(arguments, format);
    56   int result = vsnprintf(buffer, size, format, arguments);
    57   va_end(arguments);
    58   return result;
    59 }
    61 inline int swprintf(wchar_t* buffer, size_t size, const wchar_t* format, ...) {
    62   va_list arguments;
    63   va_start(arguments, format);
    64   int result = vswprintf(buffer, size, format, arguments);
    65   va_end(arguments);
    66   return result;
    67 }
    69 // BSD-style safe and consistent string copy functions.
    70 // Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|.
    71 // Copies at most |dst_size|-1 characters, and always NULL terminates |dst|, as
    72 // long as |dst_size| is not 0.  Returns the length of |src| in characters.
    73 // If the return value is >= dst_size, then the output was truncated.
    74 // NOTE: All sizes are in number of characters, NOT in bytes.
    75 size_t strlcpy(char* dst, const char* src, size_t dst_size);
    76 size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size);
    78 // Scan a wprintf format string to determine whether it's portable across a
    79 // variety of systems.  This function only checks that the conversion
    80 // specifiers used by the format string are supported and have the same meaning
    81 // on a variety of systems.  It doesn't check for other errors that might occur
    82 // within a format string.
    83 //
    84 // Nonportable conversion specifiers for wprintf are:
    85 //  - 's' and 'c' without an 'l' length modifier.  %s and %c operate on char
    86 //     data on all systems except Windows, which treat them as wchar_t data.
    87 //     Use %ls and %lc for wchar_t data instead.
    88 //  - 'S' and 'C', which operate on wchar_t data on all systems except Windows,
    89 //     which treat them as char data.  Use %ls and %lc for wchar_t data
    90 //     instead.
    91 //  - 'F', which is not identified by Windows wprintf documentation.
    92 //  - 'D', 'O', and 'U', which are deprecated and not available on all systems.
    93 //     Use %ld, %lo, and %lu instead.
    94 //
    95 // Note that there is no portable conversion specifier for char data when
    96 // working with wprintf.
    97 //
    98 // This function is intended to be called from base::vswprintf.
    99 bool IsWprintfFormatPortable(const wchar_t* format);
   101 }  // namespace base
   103 #if defined(OS_WIN)
   104 #include "base/string_util_win.h"
   105 #elif defined(OS_POSIX)
   106 #include "base/string_util_posix.h"
   107 #else
   108 #error Define string operations appropriately for your platform
   109 #endif
   111 // Trims any whitespace from either end of the input string.  Returns where
   112 // whitespace was found.
   113 // The non-wide version has two functions:
   114 // * TrimWhitespaceASCII()
   115 //   This function is for ASCII strings and only looks for ASCII whitespace;
   116 // * TrimWhitespaceUTF8()
   117 //   This function is for UTF-8 strings and looks for Unicode whitespace.
   118 // Please choose the best one according to your usage.
   119 // NOTE: Safe to use the same variable for both input and output.
   120 enum TrimPositions {
   121   TRIM_NONE     = 0,
   122   TRIM_LEADING  = 1 << 0,
   123   TRIM_TRAILING = 1 << 1,
   124   TRIM_ALL      = TRIM_LEADING | TRIM_TRAILING
   125 };
   126 TrimPositions TrimWhitespace(const std::wstring& input,
   127                              TrimPositions positions,
   128                              std::wstring* output);
   129 TrimPositions TrimWhitespaceASCII(const std::string& input,
   130                                   TrimPositions positions,
   131                                   std::string* output);
   133 // Deprecated. This function is only for backward compatibility and calls
   134 // TrimWhitespaceASCII().
   135 TrimPositions TrimWhitespace(const std::string& input,
   136                              TrimPositions positions,
   137                              std::string* output);
   139 // Searches  for CR or LF characters.  Removes all contiguous whitespace
   140 // strings that contain them.  This is useful when trying to deal with text
   141 // copied from terminals.
   142 // Returns |text, with the following three transformations:
   143 // (1) Leading and trailing whitespace is trimmed.
   144 // (2) If |trim_sequences_with_line_breaks| is true, any other whitespace
   145 //     sequences containing a CR or LF are trimmed.
   146 // (3) All other whitespace sequences are converted to single spaces.
   147 std::wstring CollapseWhitespace(const std::wstring& text,
   148                                 bool trim_sequences_with_line_breaks);
   150 // These convert between ASCII (7-bit) and Wide/UTF16 strings.
   151 std::string WideToASCII(const std::wstring& wide);
   152 std::wstring ASCIIToWide(const std::string& ascii);
   153 std::string UTF16ToASCII(const string16& utf16);
   154 string16 ASCIIToUTF16(const std::string& ascii);
   156 // These convert between UTF-8, -16, and -32 strings. They are potentially slow,
   157 // so avoid unnecessary conversions. The low-level versions return a boolean
   158 // indicating whether the conversion was 100% valid. In this case, it will still
   159 // do the best it can and put the result in the output buffer. The versions that
   160 // return strings ignore this error and just return the best conversion
   161 // possible.
   162 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output);
   163 std::string WideToUTF8(const std::wstring& wide);
   164 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output);
   165 std::wstring UTF8ToWide(const ::StringPiece& utf8);
   167 bool IsStringASCII(const std::wstring& str);
   168 bool IsStringASCII(const std::string& str);
   169 bool IsStringASCII(const string16& str);
   171 // Specialized string-conversion functions.
   172 std::string IntToString(int value);
   173 std::wstring IntToWString(int value);
   174 std::string UintToString(unsigned int value);
   175 std::wstring UintToWString(unsigned int value);
   176 std::string Int64ToString(int64_t value);
   177 std::wstring Int64ToWString(int64_t value);
   178 std::string Uint64ToString(uint64_t value);
   179 std::wstring Uint64ToWString(uint64_t value);
   180 // The DoubleToString methods convert the double to a string format that
   181 // ignores the locale.  If you want to use locale specific formatting, use ICU.
   182 std::string DoubleToString(double value);
   183 std::wstring DoubleToWString(double value);
   185 // Perform a best-effort conversion of the input string to a numeric type,
   186 // setting |*output| to the result of the conversion.  Returns true for
   187 // "perfect" conversions; returns false in the following cases:
   188 //  - Overflow/underflow.  |*output| will be set to the maximum value supported
   189 //    by the data type.
   190 //  - Trailing characters in the string after parsing the number.  |*output|
   191 //    will be set to the value of the number that was parsed.
   192 //  - No characters parseable as a number at the beginning of the string.
   193 //    |*output| will be set to 0.
   194 //  - Empty string.  |*output| will be set to 0.
   195 bool StringToInt(const std::string& input, int* output);
   196 bool StringToInt(const string16& input, int* output);
   197 bool StringToInt64(const std::string& input, int64_t* output);
   198 bool StringToInt64(const string16& input, int64_t* output);
   200 // Convenience forms of the above, when the caller is uninterested in the
   201 // boolean return value.  These return only the |*output| value from the
   202 // above conversions: a best-effort conversion when possible, otherwise, 0.
   203 int StringToInt(const std::string& value);
   204 int StringToInt(const string16& value);
   205 int64_t StringToInt64(const std::string& value);
   206 int64_t StringToInt64(const string16& value);
   208 // Return a C++ string given printf-like input.
   209 std::string StringPrintf(const char* format, ...);
   210 std::wstring StringPrintf(const wchar_t* format, ...);
   212 // Store result into a supplied string and return it
   213 const std::string& SStringPrintf(std::string* dst, const char* format, ...);
   214 const std::wstring& SStringPrintf(std::wstring* dst,
   215                                   const wchar_t* format, ...);
   217 // Append result to a supplied string
   218 void StringAppendF(std::string* dst, const char* format, ...);
   219 void StringAppendF(std::wstring* dst, const wchar_t* format, ...);
   221 //-----------------------------------------------------------------------------
   223 // Splits |str| into a vector of strings delimited by |s|. Append the results
   224 // into |r| as they appear. If several instances of |s| are contiguous, or if
   225 // |str| begins with or ends with |s|, then an empty string is inserted.
   226 //
   227 // Every substring is trimmed of any leading or trailing white space.
   228 void SplitString(const std::wstring& str,
   229                  wchar_t s,
   230                  std::vector<std::wstring>* r);
   231 void SplitString(const std::string& str,
   232                  char s,
   233                  std::vector<std::string>* r);
   235 #endif  // BASE_STRING_UTIL_H_

mercurial