From e61e2b17851818c993d4fb509b4076616ea8292b Mon Sep 17 00:00:00 2001 From: eroman Date: Tue, 13 Oct 2015 16:39:22 -0700 Subject: [PATCH] Make base::IsUnicodeWhitespace('\0') return false. BUG=542759 Review URL: https://codereview.chromium.org/1399253004 Cr-Commit-Position: refs/heads/master@{#353905} --- base/strings/string_util.cc | 9 +++++++++ base/strings/string_util.h | 4 +--- base/strings/string_util_unittest.cc | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/base/strings/string_util.cc b/base/strings/string_util.cc index d7a1d54c72210a..a978891d57e291 100644 --- a/base/strings/string_util.cc +++ b/base/strings/string_util.cc @@ -674,6 +674,15 @@ char HexDigitToInt(wchar_t c) { return 0; } +bool IsUnicodeWhitespace(wchar_t c) { + // kWhitespaceWide is a NULL-terminated string + for (const wchar_t* cur = kWhitespaceWide; *cur; ++cur) { + if (*cur == c) + return true; + } + return false; +} + static const char* const kByteStringsUnlocalized[] = { " B", " kB", diff --git a/base/strings/string_util.h b/base/strings/string_util.h index 169726bca5a128..2578569ce014a4 100644 --- a/base/strings/string_util.h +++ b/base/strings/string_util.h @@ -358,9 +358,7 @@ inline bool IsHexDigit(Char c) { BASE_EXPORT char HexDigitToInt(wchar_t c); // Returns true if it's a Unicode whitespace character. -inline bool IsUnicodeWhitespace(wchar_t c) { - return wcschr(base::kWhitespaceWide, c) != NULL; -} +BASE_EXPORT bool IsUnicodeWhitespace(wchar_t c); // Return a byte string in human-readable format with a unit suffix. Not // appropriate for use in any UI; use of FormatBytes and friends in ui/base is diff --git a/base/strings/string_util_unittest.cc b/base/strings/string_util_unittest.cc index 187e49e6698160..765ba83508c6a1 100644 --- a/base/strings/string_util_unittest.cc +++ b/base/strings/string_util_unittest.cc @@ -1080,6 +1080,26 @@ TEST(StringUtilTest, EqualsCaseInsensitiveASCII) { EXPECT_FALSE(EqualsCaseInsensitiveASCII("Asdf", "aSDFz")); } +TEST(StringUtilTest, IsUnicodeWhitespace) { + // NOT unicode white space. + EXPECT_FALSE(IsUnicodeWhitespace(L'\0')); + EXPECT_FALSE(IsUnicodeWhitespace(L'A')); + EXPECT_FALSE(IsUnicodeWhitespace(L'0')); + EXPECT_FALSE(IsUnicodeWhitespace(L'.')); + EXPECT_FALSE(IsUnicodeWhitespace(L';')); + EXPECT_FALSE(IsUnicodeWhitespace(L'\x4100')); + + // Actual unicode whitespace. + EXPECT_TRUE(IsUnicodeWhitespace(L' ')); + EXPECT_TRUE(IsUnicodeWhitespace(L'\xa0')); + EXPECT_TRUE(IsUnicodeWhitespace(L'\x3000')); + EXPECT_TRUE(IsUnicodeWhitespace(L'\t')); + EXPECT_TRUE(IsUnicodeWhitespace(L'\r')); + EXPECT_TRUE(IsUnicodeWhitespace(L'\v')); + EXPECT_TRUE(IsUnicodeWhitespace(L'\f')); + EXPECT_TRUE(IsUnicodeWhitespace(L'\n')); +} + class WriteIntoTest : public testing::Test { protected: static void WritesCorrectly(size_t num_chars) {