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 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 base::string16* str, | 444 base::string16* str, |
445 size_t start_offset, | 445 size_t start_offset, |
446 StringPiece16 find_this, | 446 StringPiece16 find_this, |
447 StringPiece16 replace_with); | 447 StringPiece16 replace_with); |
448 BASE_EXPORT void ReplaceSubstringsAfterOffset( | 448 BASE_EXPORT void ReplaceSubstringsAfterOffset( |
449 std::string* str, | 449 std::string* str, |
450 size_t start_offset, | 450 size_t start_offset, |
451 StringPiece find_this, | 451 StringPiece find_this, |
452 StringPiece replace_with); | 452 StringPiece replace_with); |
453 | 453 |
454 } // namespace base | |
455 | |
456 #if defined(OS_WIN) | |
457 #include "base/strings/string_util_win.h" | |
458 #elif defined(OS_POSIX) | |
459 #include "base/strings/string_util_posix.h" | |
460 #else | |
461 #error Define string operations appropriately for your platform | |
462 #endif | |
463 | |
464 // Reserves enough memory in |str| to accommodate |length_with_null| characters, | 454 // Reserves enough memory in |str| to accommodate |length_with_null| characters, |
465 // sets the size of |str| to |length_with_null - 1| characters, and returns a | 455 // sets the size of |str| to |length_with_null - 1| characters, and returns a |
466 // pointer to the underlying contiguous array of characters. This is typically | 456 // pointer to the underlying contiguous array of characters. This is typically |
467 // used when calling a function that writes results into a character array, but | 457 // used when calling a function that writes results into a character array, but |
468 // the caller wants the data to be managed by a string-like object. It is | 458 // the caller wants the data to be managed by a string-like object. It is |
469 // convenient in that is can be used inline in the call, and fast in that it | 459 // convenient in that is can be used inline in the call, and fast in that it |
470 // avoids copying the results of the call from a char* into a string. | 460 // avoids copying the results of the call from a char* into a string. |
471 // | 461 // |
472 // |length_with_null| must be at least 2, since otherwise the underlying string | 462 // |length_with_null| must be at least 2, since otherwise the underlying string |
473 // would have size 0, and trying to access &((*str)[0]) in that case can result | 463 // would have size 0, and trying to access &((*str)[0]) in that case can result |
474 // in a number of problems. | 464 // in a number of problems. |
475 // | 465 // |
476 // Internally, this takes linear time because the resize() call 0-fills the | 466 // Internally, this takes linear time because the resize() call 0-fills the |
477 // underlying array for potentially all | 467 // underlying array for potentially all |
478 // (|length_with_null - 1| * sizeof(string_type::value_type)) bytes. Ideally we | 468 // (|length_with_null - 1| * sizeof(string_type::value_type)) bytes. Ideally we |
479 // could avoid this aspect of the resize() call, as we expect the caller to | 469 // could avoid this aspect of the resize() call, as we expect the caller to |
480 // immediately write over this memory, but there is no other way to set the size | 470 // immediately write over this memory, but there is no other way to set the size |
481 // of the string, and not doing that will mean people who access |str| rather | 471 // of the string, and not doing that will mean people who access |str| rather |
482 // than str.c_str() will get back a string of whatever size |str| had on entry | 472 // than str.c_str() will get back a string of whatever size |str| had on entry |
483 // to this function (probably 0). | 473 // to this function (probably 0). |
484 template <class string_type> | 474 BASE_EXPORT char* WriteInto(std::string* str, size_t length_with_null); |
485 inline typename string_type::value_type* WriteInto(string_type* str, | 475 BASE_EXPORT char16* WriteInto(base::string16* str, size_t length_with_null); |
486 size_t length_with_null) { | 476 #ifndef OS_WIN |
487 DCHECK_GT(length_with_null, 1u); | 477 BASE_EXPORT wchar_t* WriteInto(std::wstring* str, size_t length_with_null); |
488 str->reserve(length_with_null); | 478 #endif |
489 str->resize(length_with_null - 1); | 479 |
490 return &((*str)[0]); | 480 } // namespace base |
491 } | 481 |
| 482 #if defined(OS_WIN) |
| 483 #include "base/strings/string_util_win.h" |
| 484 #elif defined(OS_POSIX) |
| 485 #include "base/strings/string_util_posix.h" |
| 486 #else |
| 487 #error Define string operations appropriately for your platform |
| 488 #endif |
492 | 489 |
493 //----------------------------------------------------------------------------- | 490 //----------------------------------------------------------------------------- |
494 | 491 |
495 // Does the opposite of SplitString(). | 492 // Does the opposite of SplitString(). |
496 BASE_EXPORT base::string16 JoinString(const std::vector<base::string16>& parts, | 493 BASE_EXPORT base::string16 JoinString(const std::vector<base::string16>& parts, |
497 base::char16 s); | 494 base::char16 s); |
498 BASE_EXPORT std::string JoinString( | 495 BASE_EXPORT std::string JoinString( |
499 const std::vector<std::string>& parts, char s); | 496 const std::vector<std::string>& parts, char s); |
500 | 497 |
501 // Join |parts| using |separator|. | 498 // Join |parts| using |separator|. |
(...skipping 18 matching lines...) Expand all Loading... |
520 const std::vector<std::string>& subst, | 517 const std::vector<std::string>& subst, |
521 std::vector<size_t>* offsets); | 518 std::vector<size_t>* offsets); |
522 | 519 |
523 // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL. | 520 // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL. |
524 BASE_EXPORT base::string16 ReplaceStringPlaceholders( | 521 BASE_EXPORT base::string16 ReplaceStringPlaceholders( |
525 const base::string16& format_string, | 522 const base::string16& format_string, |
526 const base::string16& a, | 523 const base::string16& a, |
527 size_t* offset); | 524 size_t* offset); |
528 | 525 |
529 #endif // BASE_STRINGS_STRING_UTIL_H_ | 526 #endif // BASE_STRINGS_STRING_UTIL_H_ |
OLD | NEW |