OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This file defines utility functions for working with strings. | 5 // This file defines utility functions for working with strings. |
6 | 6 |
7 #ifndef BASE_STRING_UTIL_H_ | 7 #ifndef BASE_STRING_UTIL_H_ |
8 #define BASE_STRING_UTIL_H_ | 8 #define BASE_STRING_UTIL_H_ |
9 | 9 |
10 #include <stdarg.h> // va_list | 10 #include <stdarg.h> // va_list |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 // Returns |text, with the following three transformations: | 167 // Returns |text, with the following three transformations: |
168 // (1) Leading and trailing whitespace is trimmed. | 168 // (1) Leading and trailing whitespace is trimmed. |
169 // (2) If |trim_sequences_with_line_breaks| is true, any other whitespace | 169 // (2) If |trim_sequences_with_line_breaks| is true, any other whitespace |
170 // sequences containing a CR or LF are trimmed. | 170 // sequences containing a CR or LF are trimmed. |
171 // (3) All other whitespace sequences are converted to single spaces. | 171 // (3) All other whitespace sequences are converted to single spaces. |
172 std::wstring CollapseWhitespace(const std::wstring& text, | 172 std::wstring CollapseWhitespace(const std::wstring& text, |
173 bool trim_sequences_with_line_breaks); | 173 bool trim_sequences_with_line_breaks); |
174 | 174 |
175 // These convert between ASCII (7-bit) and Wide/UTF16 strings. | 175 // These convert between ASCII (7-bit) and Wide/UTF16 strings. |
176 std::string WideToASCII(const std::wstring& wide); | 176 std::string WideToASCII(const std::wstring& wide); |
177 std::wstring ASCIIToWide(const std::string& ascii); | 177 std::wstring ASCIIToWide(const StringPiece& ascii); |
178 std::string UTF16ToASCII(const string16& utf16); | 178 std::string UTF16ToASCII(const string16& utf16); |
179 string16 ASCIIToUTF16(const std::string& ascii); | 179 string16 ASCIIToUTF16(const StringPiece& ascii); |
180 | 180 |
181 // These convert between UTF-8, -16, and -32 strings. They are potentially slow, | 181 // These convert between UTF-8, -16, and -32 strings. They are potentially slow, |
182 // so avoid unnecessary conversions. The low-level versions return a boolean | 182 // so avoid unnecessary conversions. The low-level versions return a boolean |
183 // indicating whether the conversion was 100% valid. In this case, it will still | 183 // indicating whether the conversion was 100% valid. In this case, it will still |
184 // do the best it can and put the result in the output buffer. The versions that | 184 // do the best it can and put the result in the output buffer. The versions that |
185 // return strings ignore this error and just return the best conversion | 185 // return strings ignore this error and just return the best conversion |
186 // possible. | 186 // possible. |
187 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); | 187 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); |
188 std::string WideToUTF8(const std::wstring& wide); | 188 std::string WideToUTF8(const std::wstring& wide); |
189 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output); | 189 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 bool WideToLatin1(const std::wstring& wide, std::string* latin1); | 245 bool WideToLatin1(const std::wstring& wide, std::string* latin1); |
246 | 246 |
247 // Returns true if the specified string matches the criteria. How can a wide | 247 // Returns true if the specified string matches the criteria. How can a wide |
248 // string be 8-bit or UTF8? It contains only characters that are < 256 (in the | 248 // string be 8-bit or UTF8? It contains only characters that are < 256 (in the |
249 // first case) or characters that use only 8-bits and whose 8-bit | 249 // first case) or characters that use only 8-bits and whose 8-bit |
250 // representation looks like a UTF-8 string (the second case). | 250 // representation looks like a UTF-8 string (the second case). |
251 bool IsString8Bit(const std::wstring& str); | 251 bool IsString8Bit(const std::wstring& str); |
252 bool IsStringUTF8(const std::string& str); | 252 bool IsStringUTF8(const std::string& str); |
253 bool IsStringWideUTF8(const std::wstring& str); | 253 bool IsStringWideUTF8(const std::wstring& str); |
254 bool IsStringASCII(const std::wstring& str); | 254 bool IsStringASCII(const std::wstring& str); |
255 bool IsStringASCII(const std::string& str); | 255 bool IsStringASCII(const StringPiece& str); |
256 bool IsStringASCII(const string16& str); | 256 bool IsStringASCII(const string16& str); |
257 | 257 |
258 // ASCII-specific tolower. The standard library's tolower is locale sensitive, | 258 // ASCII-specific tolower. The standard library's tolower is locale sensitive, |
259 // so we don't want to use it here. | 259 // so we don't want to use it here. |
260 template <class Char> inline Char ToLowerASCII(Char c) { | 260 template <class Char> inline Char ToLowerASCII(Char c) { |
261 return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; | 261 return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; |
262 } | 262 } |
263 | 263 |
264 // Converts the elements of the given string. This version uses a pointer to | 264 // Converts the elements of the given string. This version uses a pointer to |
265 // clearly differentiate it from the non-pointer variant. | 265 // clearly differentiate it from the non-pointer variant. |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 #elif defined(WCHAR_T_IS_UTF32) | 618 #elif defined(WCHAR_T_IS_UTF32) |
619 typedef uint32 Unsigned; | 619 typedef uint32 Unsigned; |
620 #endif | 620 #endif |
621 }; | 621 }; |
622 template<> | 622 template<> |
623 struct ToUnsigned<short> { | 623 struct ToUnsigned<short> { |
624 typedef unsigned short Unsigned; | 624 typedef unsigned short Unsigned; |
625 }; | 625 }; |
626 | 626 |
627 #endif // BASE_STRING_UTIL_H_ | 627 #endif // BASE_STRING_UTIL_H_ |
OLD | NEW |