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 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 const std::string& search, | 370 const std::string& search, |
371 bool case_sensitive) { | 371 bool case_sensitive) { |
372 return EndsWith(StringPiece(str), StringPiece(search), | 372 return EndsWith(StringPiece(str), StringPiece(search), |
373 case_sensitive ? CompareCase::SENSITIVE | 373 case_sensitive ? CompareCase::SENSITIVE |
374 : CompareCase::INSENSITIVE_ASCII); | 374 : CompareCase::INSENSITIVE_ASCII); |
375 } | 375 } |
376 BASE_EXPORT bool EndsWith(const string16& str, | 376 BASE_EXPORT bool EndsWith(const string16& str, |
377 const string16& search, | 377 const string16& search, |
378 bool case_sensitive); | 378 bool case_sensitive); |
379 | 379 |
380 } // namespace base | |
381 | |
382 #if defined(OS_WIN) | |
383 #include "base/strings/string_util_win.h" | |
384 #elif defined(OS_POSIX) | |
385 #include "base/strings/string_util_posix.h" | |
386 #else | |
387 #error Define string operations appropriately for your platform | |
388 #endif | |
389 | |
390 // Determines the type of ASCII character, independent of locale (the C | 380 // Determines the type of ASCII character, independent of locale (the C |
391 // library versions will change based on locale). | 381 // library versions will change based on locale). |
392 template <typename Char> | 382 template <typename Char> |
393 inline bool IsAsciiWhitespace(Char c) { | 383 inline bool IsAsciiWhitespace(Char c) { |
394 return c == ' ' || c == '\r' || c == '\n' || c == '\t'; | 384 return c == ' ' || c == '\r' || c == '\n' || c == '\t'; |
395 } | 385 } |
396 template <typename Char> | 386 template <typename Char> |
397 inline bool IsAsciiAlpha(Char c) { | 387 inline bool IsAsciiAlpha(Char c) { |
398 return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')); | 388 return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')); |
399 } | 389 } |
400 template <typename Char> | 390 template <typename Char> |
401 inline bool IsAsciiDigit(Char c) { | 391 inline bool IsAsciiDigit(Char c) { |
402 return c >= '0' && c <= '9'; | 392 return c >= '0' && c <= '9'; |
403 } | 393 } |
404 | 394 |
405 template <typename Char> | 395 template <typename Char> |
406 inline bool IsHexDigit(Char c) { | 396 inline bool IsHexDigit(Char c) { |
407 return (c >= '0' && c <= '9') || | 397 return (c >= '0' && c <= '9') || |
408 (c >= 'A' && c <= 'F') || | 398 (c >= 'A' && c <= 'F') || |
409 (c >= 'a' && c <= 'f'); | 399 (c >= 'a' && c <= 'f'); |
410 } | 400 } |
411 | 401 |
412 template <typename Char> | 402 // Returns the integer corresponding to the given hex character. For example: |
413 inline char HexDigitToInt(Char c) { | 403 // '4' -> 4 |
414 DCHECK(IsHexDigit(c)); | 404 // 'a' -> 10 |
415 if (c >= '0' && c <= '9') | 405 // 'B' -> 11 |
416 return static_cast<char>(c - '0'); | 406 // Assumes the input is a valid hex character. DCHECKs in debug builds if not. |
417 if (c >= 'A' && c <= 'F') | 407 BASE_EXPORT char HexDigitToInt(wchar_t c); |
418 return static_cast<char>(c - 'A' + 10); | |
419 if (c >= 'a' && c <= 'f') | |
420 return static_cast<char>(c - 'a' + 10); | |
421 return 0; | |
422 } | |
423 | 408 |
424 // Returns true if it's a whitespace character. | 409 // Returns true if it's a Unicode whitespace character. |
425 inline bool IsWhitespace(wchar_t c) { | 410 inline bool IsUnicodeWhitespace(wchar_t c) { |
426 return wcschr(base::kWhitespaceWide, c) != NULL; | 411 return wcschr(base::kWhitespaceWide, c) != NULL; |
427 } | 412 } |
428 | 413 |
429 // Return a byte string in human-readable format with a unit suffix. Not | 414 // Return a byte string in human-readable format with a unit suffix. Not |
430 // appropriate for use in any UI; use of FormatBytes and friends in ui/base is | 415 // appropriate for use in any UI; use of FormatBytes and friends in ui/base is |
431 // highly recommended instead. TODO(avi): Figure out how to get callers to use | 416 // highly recommended instead. TODO(avi): Figure out how to get callers to use |
432 // FormatBytes instead; remove this. | 417 // FormatBytes instead; remove this. |
433 BASE_EXPORT base::string16 FormatBytesUnlocalized(int64 bytes); | 418 BASE_EXPORT string16 FormatBytesUnlocalized(int64 bytes); |
| 419 |
| 420 } // namespace base |
| 421 |
| 422 #if defined(OS_WIN) |
| 423 #include "base/strings/string_util_win.h" |
| 424 #elif defined(OS_POSIX) |
| 425 #include "base/strings/string_util_posix.h" |
| 426 #else |
| 427 #error Define string operations appropriately for your platform |
| 428 #endif |
434 | 429 |
435 // Starting at |start_offset| (usually 0), replace the first instance of | 430 // Starting at |start_offset| (usually 0), replace the first instance of |
436 // |find_this| with |replace_with|. | 431 // |find_this| with |replace_with|. |
437 BASE_EXPORT void ReplaceFirstSubstringAfterOffset( | 432 BASE_EXPORT void ReplaceFirstSubstringAfterOffset( |
438 base::string16* str, | 433 base::string16* str, |
439 size_t start_offset, | 434 size_t start_offset, |
440 const base::string16& find_this, | 435 const base::string16& find_this, |
441 const base::string16& replace_with); | 436 const base::string16& replace_with); |
442 BASE_EXPORT void ReplaceFirstSubstringAfterOffset( | 437 BASE_EXPORT void ReplaceFirstSubstringAfterOffset( |
443 std::string* str, | 438 std::string* str, |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 // string can contain wildcards like * and ? | 541 // string can contain wildcards like * and ? |
547 // The backslash character (\) is an escape character for * and ? | 542 // The backslash character (\) is an escape character for * and ? |
548 // We limit the patterns to having a max of 16 * or ? characters. | 543 // We limit the patterns to having a max of 16 * or ? characters. |
549 // ? matches 0 or 1 character, while * matches 0 or more characters. | 544 // ? matches 0 or 1 character, while * matches 0 or more characters. |
550 BASE_EXPORT bool MatchPattern(const base::StringPiece& string, | 545 BASE_EXPORT bool MatchPattern(const base::StringPiece& string, |
551 const base::StringPiece& pattern); | 546 const base::StringPiece& pattern); |
552 BASE_EXPORT bool MatchPattern(const base::string16& string, | 547 BASE_EXPORT bool MatchPattern(const base::string16& string, |
553 const base::string16& pattern); | 548 const base::string16& pattern); |
554 | 549 |
555 #endif // BASE_STRINGS_STRING_UTIL_H_ | 550 #endif // BASE_STRINGS_STRING_UTIL_H_ |
OLD | NEW |