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