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 <string> | 10 #include <string> |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 // strings that contain them. This is useful when trying to deal with text | 149 // strings that contain them. This is useful when trying to deal with text |
150 // copied from terminals. | 150 // copied from terminals. |
151 // Returns |text, with the following three transformations: | 151 // Returns |text, with the following three transformations: |
152 // (1) Leading and trailing whitespace is trimmed. | 152 // (1) Leading and trailing whitespace is trimmed. |
153 // (2) If |trim_sequences_with_line_breaks| is true, any other whitespace | 153 // (2) If |trim_sequences_with_line_breaks| is true, any other whitespace |
154 // sequences containing a CR or LF are trimmed. | 154 // sequences containing a CR or LF are trimmed. |
155 // (3) All other whitespace sequences are converted to single spaces. | 155 // (3) All other whitespace sequences are converted to single spaces. |
156 std::wstring CollapseWhitespace(const std::wstring& text, | 156 std::wstring CollapseWhitespace(const std::wstring& text, |
157 bool trim_sequences_with_line_breaks); | 157 bool trim_sequences_with_line_breaks); |
158 | 158 |
159 // These convert between ASCII (7-bit) and Wide/UTF16 strings. | 159 // These convert between ASCII (7-bit) and UTF16 strings. |
160 std::string WideToASCII(const std::wstring& wide); | 160 std::string WideToASCII(const std::wstring& wide); |
161 std::wstring ASCIIToWide(const std::string& ascii); | 161 std::wstring ASCIIToWide(const std::string& ascii); |
162 std::string UTF16ToASCII(const string16& utf16); | |
163 string16 ASCIIToUTF16(const std::string& ascii); | |
164 | 162 |
165 // These convert between UTF-8, -16, and -32 strings. They are potentially slow, | 163 // These convert between UTF-8, -16, and -32 strings. They are potentially slow, |
166 // so avoid unnecessary conversions. The low-level versions return a boolean | 164 // so avoid unnecessary conversions. The low-level versions return a boolean |
167 // indicating whether the conversion was 100% valid. In this case, it will still | 165 // indicating whether the conversion was 100% valid. In this case, it will still |
168 // do the best it can and put the result in the output buffer. The versions that | 166 // do the best it can and put the result in the output buffer. The versions that |
169 // return strings ignore this error and just return the best conversion | 167 // return strings ignore this error and just return the best conversion |
170 // possible. | 168 // possible. |
171 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); | 169 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); |
172 std::string WideToUTF8(const std::wstring& wide); | 170 std::string WideToUTF8(const std::wstring& wide); |
173 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output); | 171 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 | 228 |
231 // Returns true if the specified string matches the criteria. How can a wide | 229 // Returns true if the specified string matches the criteria. How can a wide |
232 // string be 8-bit or UTF8? It contains only characters that are < 256 (in the | 230 // string be 8-bit or UTF8? It contains only characters that are < 256 (in the |
233 // first case) or characters that use only 8-bits and whose 8-bit | 231 // first case) or characters that use only 8-bits and whose 8-bit |
234 // representation looks like a UTF-8 string (the second case). | 232 // representation looks like a UTF-8 string (the second case). |
235 bool IsString8Bit(const std::wstring& str); | 233 bool IsString8Bit(const std::wstring& str); |
236 bool IsStringUTF8(const std::string& str); | 234 bool IsStringUTF8(const std::string& str); |
237 bool IsStringWideUTF8(const std::wstring& str); | 235 bool IsStringWideUTF8(const std::wstring& str); |
238 bool IsStringASCII(const std::wstring& str); | 236 bool IsStringASCII(const std::wstring& str); |
239 bool IsStringASCII(const std::string& str); | 237 bool IsStringASCII(const std::string& str); |
240 bool IsStringASCII(const string16& str); | |
241 | 238 |
242 // ASCII-specific tolower. The standard library's tolower is locale sensitive, | 239 // ASCII-specific tolower. The standard library's tolower is locale sensitive, |
243 // so we don't want to use it here. | 240 // so we don't want to use it here. |
244 template <class Char> inline Char ToLowerASCII(Char c) { | 241 template <class Char> inline Char ToLowerASCII(Char c) { |
245 return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; | 242 return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; |
246 } | 243 } |
247 | 244 |
248 // Converts the elements of the given string. This version uses a pointer to | 245 // Converts the elements of the given string. This version uses a pointer to |
249 // clearly differentiate it from the non-pointer variant. | 246 // clearly differentiate it from the non-pointer variant. |
250 template <class str> inline void StringToLowerASCII(str* s) { | 247 template <class str> inline void StringToLowerASCII(str* s) { |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 // Returns a hex string representation of a binary buffer. | 566 // Returns a hex string representation of a binary buffer. |
570 // The returned hex string will be in upper case. | 567 // The returned hex string will be in upper case. |
571 // This function does not check if |size| is within reasonable limits since | 568 // This function does not check if |size| is within reasonable limits since |
572 // it's written with trusted data in mind. | 569 // it's written with trusted data in mind. |
573 // If you suspect that the data you want to format might be large, | 570 // If you suspect that the data you want to format might be large, |
574 // the absolute max size for |size| should be is | 571 // the absolute max size for |size| should be is |
575 // std::numeric_limits<size_t>::max() / 2 | 572 // std::numeric_limits<size_t>::max() / 2 |
576 std::string HexEncode(const void* bytes, size_t size); | 573 std::string HexEncode(const void* bytes, size_t size); |
577 | 574 |
578 #endif // BASE_STRING_UTIL_H_ | 575 #endif // BASE_STRING_UTIL_H_ |
OLD | NEW |