From 12db7515f17228798d1aa38fce0fee3e7d2d36b6 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Wed, 23 Aug 2017 10:39:35 -0400 Subject: Convert string Find methods to return an Optional The Find and ReverseFind methods for WideString, WideStringC, ByteString, and ByteStringC have been converted from returning a raw FX_STRSIZE, to returning Optional, so that success/failure can be indicated without using FX_STRNPOS. This allows for removing FX_STRNPOS and by association makes the conversion of FX_STRSIZE to size_t easier, since it forces checking the return value of Find to be explictly done as well as taking the error value out of the range of FX_STRSIZE. New Contains methods have been added for cases where the success or failure is all the call site to Find cared about, and the actual position was ignored. BUG=pdfium:828 Change-Id: Id827e508c8660affa68cc08a13d96121369364b7 Reviewed-on: https://pdfium-review.googlesource.com/11350 Commit-Queue: Ryan Harrison Reviewed-by: dsinclair --- core/fxcrt/cfx_bytestring_unittest.cpp | 117 +++++++++++++++++++++++++++++---- 1 file changed, 105 insertions(+), 12 deletions(-) (limited to 'core/fxcrt/cfx_bytestring_unittest.cpp') diff --git a/core/fxcrt/cfx_bytestring_unittest.cpp b/core/fxcrt/cfx_bytestring_unittest.cpp index 35f407f6ec..e7281546fb 100644 --- a/core/fxcrt/cfx_bytestring_unittest.cpp +++ b/core/fxcrt/cfx_bytestring_unittest.cpp @@ -317,7 +317,7 @@ TEST(fxcrt, ByteStringCNull) { EXPECT_EQ(null_string, assigned_null_string); CFX_ByteStringC assigned_nullptr_string("initially not nullptr"); - assigned_nullptr_string = (const char*)nullptr; + assigned_nullptr_string = nullptr; EXPECT_FALSE(assigned_nullptr_string.raw_str()); EXPECT_EQ(assigned_nullptr_string.GetLength(), 0); EXPECT_TRUE(assigned_nullptr_string.IsEmpty()); @@ -568,6 +568,89 @@ TEST(fxcrt, ByteStringRight) { EXPECT_EQ("", empty.Right(-1)); } +TEST(fxcrt, ByteStringFind) { + CFX_ByteString null_string; + EXPECT_FALSE(null_string.Find('a').has_value()); + EXPECT_FALSE(null_string.Find('\0').has_value()); + + CFX_ByteString empty_string(""); + EXPECT_FALSE(empty_string.Find('a').has_value()); + EXPECT_FALSE(empty_string.Find('\0').has_value()); + + pdfium::Optional result; + CFX_ByteString single_string("a"); + result = single_string.Find('a'); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(0, result.value()); + EXPECT_FALSE(single_string.Find('b').has_value()); + EXPECT_FALSE(single_string.Find('\0').has_value()); + + CFX_ByteString longer_string("abccc"); + result = longer_string.Find('a'); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(0, result.value()); + result = longer_string.Find('c'); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(2, result.value()); + result = longer_string.Find('c', 3); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(3, result.value()); + EXPECT_FALSE(longer_string.Find('d').has_value()); + EXPECT_FALSE(longer_string.Find('\0').has_value()); + + result = longer_string.Find("ab"); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(0, result.value()); + result = longer_string.Find("ccc"); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(2, result.value()); + result = longer_string.Find("cc", 3); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(3, result.value()); + EXPECT_FALSE(longer_string.Find("d").has_value()); + + CFX_ByteString hibyte_string( + "ab\x8c" + "def"); + result = hibyte_string.Find('\x8c'); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(2, result.value()); +} + +TEST(fxcrt, ByteStringReverseFind) { + CFX_ByteString null_string; + EXPECT_FALSE(null_string.ReverseFind('a').has_value()); + EXPECT_FALSE(null_string.ReverseFind('\0').has_value()); + + CFX_ByteString empty_string(""); + EXPECT_FALSE(empty_string.ReverseFind('a').has_value()); + EXPECT_FALSE(empty_string.ReverseFind('\0').has_value()); + + pdfium::Optional result; + CFX_ByteString single_string("a"); + result = single_string.ReverseFind('a'); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(0, result.value()); + EXPECT_FALSE(single_string.ReverseFind('b').has_value()); + EXPECT_FALSE(single_string.ReverseFind('\0').has_value()); + + CFX_ByteString longer_string("abccc"); + result = longer_string.ReverseFind('a'); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(0, result.value()); + result = longer_string.ReverseFind('c'); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(4, result.value()); + EXPECT_FALSE(longer_string.ReverseFind('\0').has_value()); + + CFX_ByteString hibyte_string( + "ab\x8c" + "def"); + result = hibyte_string.ReverseFind('\x8c'); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(2, result.value()); +} + TEST(fxcrt, ByteStringUpperLower) { CFX_ByteString fred("F-Re.42D"); fred.MakeLower(); @@ -882,27 +965,37 @@ TEST(fxcrt, ByteStringCGetID) { TEST(fxcrt, ByteStringCFind) { CFX_ByteStringC null_string; - EXPECT_EQ(FX_STRNPOS, null_string.Find('a')); - EXPECT_EQ(FX_STRNPOS, null_string.Find(0)); + EXPECT_FALSE(null_string.Find('a').has_value()); + EXPECT_FALSE(null_string.Find('\0').has_value()); CFX_ByteStringC empty_string(""); - EXPECT_EQ(FX_STRNPOS, empty_string.Find('a')); - EXPECT_EQ(FX_STRNPOS, empty_string.Find(0)); + EXPECT_FALSE(empty_string.Find('a').has_value()); + EXPECT_FALSE(empty_string.Find('\0').has_value()); + pdfium::Optional result; CFX_ByteStringC single_string("a"); - EXPECT_EQ(0, single_string.Find('a')); - EXPECT_EQ(FX_STRNPOS, single_string.Find('b')); - EXPECT_EQ(FX_STRNPOS, single_string.Find(0)); + result = single_string.Find('a'); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(0, result.value()); + EXPECT_FALSE(single_string.Find('b').has_value()); + EXPECT_FALSE(single_string.Find('\0').has_value()); CFX_ByteStringC longer_string("abccc"); - EXPECT_EQ(0, longer_string.Find('a')); - EXPECT_EQ(2, longer_string.Find('c')); - EXPECT_EQ(FX_STRNPOS, longer_string.Find(0)); + result = longer_string.Find('a'); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(0, result.value()); + result = longer_string.Find('c'); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(2, result.value()); + EXPECT_FALSE(longer_string.Find('d').has_value()); + EXPECT_FALSE(longer_string.Find('\0').has_value()); CFX_ByteStringC hibyte_string( "ab\x8c" "def"); - EXPECT_EQ(2, hibyte_string.Find('\x8c')); + result = hibyte_string.Find('\x8c'); + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(2, result.value()); } TEST(fxcrt, ByteStringCMid) { -- cgit v1.2.3