OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 9 #pragma once |
10 | 10 |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 string16* str, | 450 string16* str, |
451 string16::size_type start_offset, | 451 string16::size_type start_offset, |
452 const string16& find_this, | 452 const string16& find_this, |
453 const string16& replace_with); | 453 const string16& replace_with); |
454 BASE_EXPORT void ReplaceSubstringsAfterOffset( | 454 BASE_EXPORT void ReplaceSubstringsAfterOffset( |
455 std::string* str, | 455 std::string* str, |
456 std::string::size_type start_offset, | 456 std::string::size_type start_offset, |
457 const std::string& find_this, | 457 const std::string& find_this, |
458 const std::string& replace_with); | 458 const std::string& replace_with); |
459 | 459 |
460 // Reserves enough memory in |str| to accommodate |length_with_null| | 460 // Reserves enough memory in |str| to accommodate |length_with_null| characters, |
461 // characters, sets the size of |str| to |length_with_null - 1| characters, | 461 // sets the size of |str| to |length_with_null - 1| characters, and returns a |
462 // and returns a pointer to the underlying contiguous array of characters. | 462 // pointer to the underlying contiguous array of characters. This is typically |
| 463 // used when calling a function that writes results into a character array, but |
| 464 // the caller wants the data to be managed by a string-like object. It is |
| 465 // convenient in that is can be used inline in the call, and fast in that it |
| 466 // avoids copying the results of the call from a char* into a string. |
463 // | 467 // |
464 // This is typically used when calling a function that writes results into a | 468 // |length_with_null| must be at least 2, since otherwise the underlying string |
465 // character array, but the caller wants the data to be managed by a | 469 // would have size 0, and trying to access &((*str)[0]) in that case can result |
466 // string-like object. | 470 // in a number of problems. |
467 // | 471 // |
468 // |length_with_null| must be >= 1. In the |length_with_null| == 1 case, | 472 // Internally, this takes linear time because the resize() call 0-fills the |
469 // NULL is returned rather than a pointer to the array, since there is no way | 473 // underlying array for potentially all |
470 // to provide access to the underlying character array of a 0-length | 474 // (|length_with_null - 1| * sizeof(string_type::value_type)) bytes. Ideally we |
471 // string-like object without breaking guarantees provided by the C++ | 475 // could avoid this aspect of the resize() call, as we expect the caller to |
472 // standards. | 476 // immediately write over this memory, but there is no other way to set the size |
473 // | 477 // of the string, and not doing that will mean people who access |str| rather |
474 // Internally, this takes linear time because the underlying array needs to | 478 // than str.c_str() will get back a string of whatever size |str| had on entry |
475 // be 0-filled for all |length_with_null - 1| * sizeof(character) bytes. | 479 // to this function (probably 0). |
476 template <class string_type> | 480 template <class string_type> |
477 inline typename string_type::value_type* WriteInto(string_type* str, | 481 inline typename string_type::value_type* WriteInto(string_type* str, |
478 size_t length_with_null) { | 482 size_t length_with_null) { |
479 DCHECK_NE(0u, length_with_null); | 483 DCHECK_GT(length_with_null, 1u); |
480 str->reserve(length_with_null); | 484 str->reserve(length_with_null); |
481 str->resize(length_with_null - 1); | 485 str->resize(length_with_null - 1); |
482 | |
483 // If |length_with_null| is 1, calling (*str)[0] is invalid since the | |
484 // size() is 0. In some implementations this triggers an assertion. | |
485 // | |
486 // Trying to access the underlying byte array by casting away const | |
487 // when calling str->data() or str->c_str() is also incorrect. | |
488 // Some implementations of basic_string use a copy-on-write approach and | |
489 // this could end up mutating the data that is shared across multiple string | |
490 // objects. | |
491 if (length_with_null <= 1) | |
492 return NULL; | |
493 | |
494 return &((*str)[0]); | 486 return &((*str)[0]); |
495 } | 487 } |
496 | 488 |
497 //----------------------------------------------------------------------------- | 489 //----------------------------------------------------------------------------- |
498 | 490 |
499 // Splits a string into its fields delimited by any of the characters in | 491 // Splits a string into its fields delimited by any of the characters in |
500 // |delimiters|. Each field is added to the |tokens| vector. Returns the | 492 // |delimiters|. Each field is added to the |tokens| vector. Returns the |
501 // number of tokens found. | 493 // number of tokens found. |
502 BASE_EXPORT size_t Tokenize(const std::wstring& str, | 494 BASE_EXPORT size_t Tokenize(const std::wstring& str, |
503 const std::wstring& delimiters, | 495 const std::wstring& delimiters, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 #elif defined(WCHAR_T_IS_UTF32) | 560 #elif defined(WCHAR_T_IS_UTF32) |
569 typedef uint32 Unsigned; | 561 typedef uint32 Unsigned; |
570 #endif | 562 #endif |
571 }; | 563 }; |
572 template<> | 564 template<> |
573 struct ToUnsigned<short> { | 565 struct ToUnsigned<short> { |
574 typedef unsigned short Unsigned; | 566 typedef unsigned short Unsigned; |
575 }; | 567 }; |
576 | 568 |
577 #endif // BASE_STRING_UTIL_H_ | 569 #endif // BASE_STRING_UTIL_H_ |
OLD | NEW |