OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_STRINGS_STRING_UTIL_H_ | 7 #ifndef BASE_STRINGS_STRING_UTIL_H_ |
8 #define BASE_STRINGS_STRING_UTIL_H_ | 8 #define BASE_STRINGS_STRING_UTIL_H_ |
9 | 9 |
10 #include <ctype.h> | 10 #include <ctype.h> |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 | 141 |
142 // Contains the set of characters representing whitespace in the corresponding | 142 // Contains the set of characters representing whitespace in the corresponding |
143 // encoding. Null-terminated. | 143 // encoding. Null-terminated. |
144 BASE_EXPORT extern const wchar_t kWhitespaceWide[]; | 144 BASE_EXPORT extern const wchar_t kWhitespaceWide[]; |
145 BASE_EXPORT extern const char16 kWhitespaceUTF16[]; | 145 BASE_EXPORT extern const char16 kWhitespaceUTF16[]; |
146 BASE_EXPORT extern const char kWhitespaceASCII[]; | 146 BASE_EXPORT extern const char kWhitespaceASCII[]; |
147 | 147 |
148 // Null-terminated string representing the UTF-8 byte order mark. | 148 // Null-terminated string representing the UTF-8 byte order mark. |
149 BASE_EXPORT extern const char kUtf8ByteOrderMark[]; | 149 BASE_EXPORT extern const char kUtf8ByteOrderMark[]; |
150 | 150 |
151 } // namespace base | |
152 | |
153 #if defined(OS_WIN) | |
154 #include "base/strings/string_util_win.h" | |
155 #elif defined(OS_POSIX) | |
156 #include "base/strings/string_util_posix.h" | |
157 #else | |
158 #error Define string operations appropriately for your platform | |
159 #endif | |
160 | |
161 // Removes characters in |remove_chars| from anywhere in |input|. Returns true | 151 // Removes characters in |remove_chars| from anywhere in |input|. Returns true |
162 // if any characters were removed. |remove_chars| must be null-terminated. | 152 // if any characters were removed. |remove_chars| must be null-terminated. |
163 // NOTE: Safe to use the same variable for both |input| and |output|. | 153 // NOTE: Safe to use the same variable for both |input| and |output|. |
164 BASE_EXPORT bool RemoveChars(const base::string16& input, | 154 BASE_EXPORT bool RemoveChars(const string16& input, |
165 const base::char16 remove_chars[], | 155 const char16 remove_chars[], |
166 base::string16* output); | 156 string16* output); |
167 BASE_EXPORT bool RemoveChars(const std::string& input, | 157 BASE_EXPORT bool RemoveChars(const std::string& input, |
168 const char remove_chars[], | 158 const char remove_chars[], |
169 std::string* output); | 159 std::string* output); |
170 | 160 |
171 // Replaces characters in |replace_chars| from anywhere in |input| with | 161 // Replaces characters in |replace_chars| from anywhere in |input| with |
172 // |replace_with|. Each character in |replace_chars| will be replaced with | 162 // |replace_with|. Each character in |replace_chars| will be replaced with |
173 // the |replace_with| string. Returns true if any characters were replaced. | 163 // the |replace_with| string. Returns true if any characters were replaced. |
174 // |replace_chars| must be null-terminated. | 164 // |replace_chars| must be null-terminated. |
175 // NOTE: Safe to use the same variable for both |input| and |output|. | 165 // NOTE: Safe to use the same variable for both |input| and |output|. |
176 BASE_EXPORT bool ReplaceChars(const base::string16& input, | 166 BASE_EXPORT bool ReplaceChars(const string16& input, |
177 const base::char16 replace_chars[], | 167 const char16 replace_chars[], |
178 const base::string16& replace_with, | 168 const string16& replace_with, |
179 base::string16* output); | 169 string16* output); |
180 BASE_EXPORT bool ReplaceChars(const std::string& input, | 170 BASE_EXPORT bool ReplaceChars(const std::string& input, |
181 const char replace_chars[], | 171 const char replace_chars[], |
182 const std::string& replace_with, | 172 const std::string& replace_with, |
183 std::string* output); | 173 std::string* output); |
184 | 174 |
185 // Removes characters in |trim_chars| from the beginning and end of |input|. | 175 // Removes characters in |trim_chars| from the beginning and end of |input|. |
186 // |trim_chars| must be null-terminated. | 176 // |trim_chars| must be null-terminated. |
187 // NOTE: Safe to use the same variable for both |input| and |output|. | 177 // NOTE: Safe to use the same variable for both |input| and |output|. |
188 BASE_EXPORT bool TrimString(const base::string16& input, | 178 BASE_EXPORT bool TrimString(const string16& input, |
189 const base::char16 trim_chars[], | 179 const char16 trim_chars[], |
190 base::string16* output); | 180 string16* output); |
191 BASE_EXPORT bool TrimString(const std::string& input, | 181 BASE_EXPORT bool TrimString(const std::string& input, |
192 const char trim_chars[], | 182 const char trim_chars[], |
193 std::string* output); | 183 std::string* output); |
194 | 184 |
195 // Truncates a string to the nearest UTF-8 character that will leave | 185 // Truncates a string to the nearest UTF-8 character that will leave |
196 // the string less than or equal to the specified byte size. | 186 // the string less than or equal to the specified byte size. |
197 BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input, | 187 BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input, |
198 const size_t byte_size, | 188 const size_t byte_size, |
199 std::string* output); | 189 std::string* output); |
200 | 190 |
| 191 } // namespace base |
| 192 |
| 193 #if defined(OS_WIN) |
| 194 #include "base/strings/string_util_win.h" |
| 195 #elif defined(OS_POSIX) |
| 196 #include "base/strings/string_util_posix.h" |
| 197 #else |
| 198 #error Define string operations appropriately for your platform |
| 199 #endif |
| 200 |
201 // Trims any whitespace from either end of the input string. Returns where | 201 // Trims any whitespace from either end of the input string. Returns where |
202 // whitespace was found. | 202 // whitespace was found. |
203 // The non-wide version has two functions: | 203 // The non-wide version has two functions: |
204 // * TrimWhitespaceASCII() | 204 // * TrimWhitespaceASCII() |
205 // This function is for ASCII strings and only looks for ASCII whitespace; | 205 // This function is for ASCII strings and only looks for ASCII whitespace; |
206 // Please choose the best one according to your usage. | 206 // Please choose the best one according to your usage. |
207 // NOTE: Safe to use the same variable for both input and output. | 207 // NOTE: Safe to use the same variable for both input and output. |
208 enum TrimPositions { | 208 enum TrimPositions { |
209 TRIM_NONE = 0, | 209 TRIM_NONE = 0, |
210 TRIM_LEADING = 1 << 0, | 210 TRIM_LEADING = 1 << 0, |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 #elif defined(WCHAR_T_IS_UTF32) | 527 #elif defined(WCHAR_T_IS_UTF32) |
528 typedef uint32 Unsigned; | 528 typedef uint32 Unsigned; |
529 #endif | 529 #endif |
530 }; | 530 }; |
531 template<> | 531 template<> |
532 struct ToUnsigned<short> { | 532 struct ToUnsigned<short> { |
533 typedef unsigned short Unsigned; | 533 typedef unsigned short Unsigned; |
534 }; | 534 }; |
535 | 535 |
536 #endif // BASE_STRINGS_STRING_UTIL_H_ | 536 #endif // BASE_STRINGS_STRING_UTIL_H_ |
OLD | NEW |