diff options
author | jerrica@google.com <jerrica@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-17 16:20:33 +0000 |
---|---|---|
committer | jerrica@google.com <jerrica@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-17 16:20:33 +0000 |
commit | a89dc1ed4fa02fbf69591e6f8010658a50ec67cf (patch) | |
tree | bf209b8f1ea38b5d3a8fe8971bb6b217882aa739 | |
parent | 8f3a19de23042068b07f10d8b867c8fb2c0df019 (diff) | |
download | chromium_src-a89dc1ed4fa02fbf69591e6f8010658a50ec67cf.zip chromium_src-a89dc1ed4fa02fbf69591e6f8010658a50ec67cf.tar.gz chromium_src-a89dc1ed4fa02fbf69591e6f8010658a50ec67cf.tar.bz2 |
Made changes to TruncateUtF8ToByteSize to cleanup the code.
I made some corrections to my previously submitted function
TruncateUTF8ToByteSize, in order to clean up the code,
and use it to truncate sync node titles above 255 bytes.
BUG=43675
TEST=base/string_util_unittest.cc
Review URL: http://codereview.chromium.org/2632005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50112 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/string_util.cc | 9 | ||||
-rw-r--r-- | base/string_util_unittest.cc | 2 |
2 files changed, 6 insertions, 5 deletions
diff --git a/base/string_util.cc b/base/string_util.cc index 216b047..2488b0b 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -486,14 +486,16 @@ bool TrimString(const std::string& input, void TruncateUTF8ToByteSize(const std::string& input, const size_t byte_size, std::string* output) { + DCHECK(output); if (byte_size > input.length()) { *output = input; return; } - + DCHECK_LE(byte_size, static_cast<uint32>(kint32max)); + // Note: This cast is necessary because CBU8_NEXT uses int32s. int32 truncation_length = static_cast<int32>(byte_size); int32 char_index = truncation_length - 1; - const char* cstr = input.c_str(); + const char* data = input.data(); // Using CBU8, we will move backwards from the truncation point // to the beginning of the string looking for a valid UTF8 @@ -502,7 +504,7 @@ void TruncateUTF8ToByteSize(const std::string& input, while (char_index >= 0) { int32 prev = char_index; uint32 code_point = 0; - CBU8_NEXT(cstr, char_index, truncation_length, code_point); + CBU8_NEXT(data, char_index, truncation_length, code_point); if (!base::IsValidCharacter(code_point) || !base::IsValidCodepoint(code_point)) { char_index = prev - 1; @@ -511,7 +513,6 @@ void TruncateUTF8ToByteSize(const std::string& input, } } - DCHECK(output != NULL); if (char_index >= 0 ) *output = input.substr(0, char_index); else diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc index 2fdd62e..7afc0bd 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -85,7 +85,7 @@ bool Truncated(const std::string& input, const size_t byte_size, std::string* output) { size_t prev = input.length(); TruncateUTF8ToByteSize(input, byte_size, output); - return prev != (*output).length(); + return prev != output->length(); } } // namespace |