summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2015-04-22 10:09:35 -0700
committerTom Sepez <tsepez@chromium.org>2015-04-22 10:09:35 -0700
commitca19b6e6b4e81ba0fce1bc9d2e145cb39cf0537d (patch)
tree17668e19a426c0cb737f35d5267124901463da2a
parent97d10aff654e42c1b7c3d2abf33fbcf8d341799e (diff)
downloadpdfium-ca19b6e6b4e81ba0fce1bc9d2e145cb39cf0537d.tar.xz
Add missing operators for CFX_ByteString.
Part 3 of 4. BUG=pdfium:142 R=brucedawson@chromium.org, thestig@chromium.org Review URL: https://codereview.chromium.org/1099213002
-rw-r--r--core/include/fxcrt/fx_string.h8
-rw-r--r--core/src/fxcrt/fx_basic_bstring_unittest.cpp39
2 files changed, 47 insertions, 0 deletions
diff --git a/core/include/fxcrt/fx_string.h b/core/include/fxcrt/fx_string.h
index 70f64932c8..4db83e3914 100644
--- a/core/include/fxcrt/fx_string.h
+++ b/core/include/fxcrt/fx_string.h
@@ -7,6 +7,8 @@
#ifndef _FX_STRING_H_
#define _FX_STRING_H_
+#include <algorithm>
+
#include "fx_memory.h"
class CFX_ByteStringC;
@@ -261,6 +263,12 @@ public:
return !operator==(str);
}
+ bool operator< (const CFX_ByteString& str) const
+ {
+ int result = FXSYS_memcmp32(c_str(), str.c_str(), std::min(GetLength(), str.GetLength()));
+ return result < 0 || (result == 0 && GetLength() < str.GetLength());
+ }
+
void Empty();
const CFX_ByteString& operator = (FX_LPCSTR str);
diff --git a/core/src/fxcrt/fx_basic_bstring_unittest.cpp b/core/src/fxcrt/fx_basic_bstring_unittest.cpp
index 53427e3650..1ddaad420a 100644
--- a/core/src/fxcrt/fx_basic_bstring_unittest.cpp
+++ b/core/src/fxcrt/fx_basic_bstring_unittest.cpp
@@ -6,6 +6,45 @@
#include "../../../testing/fx_string_testhelpers.h"
#include "../../include/fxcrt/fx_basic.h"
+TEST(fxcrt, ByteStringOperatorSubscript) {
+ // CFX_ByteString includes the NUL terminator for non-empty strings.
+ CFX_ByteString abc("abc");
+ EXPECT_EQ('a', abc[0]);
+ EXPECT_EQ('b', abc[1]);
+ EXPECT_EQ('c', abc[2]);
+ EXPECT_EQ(0, abc[3]);
+}
+
+TEST(fxcrt, ByteStringOperatorLT) {
+ CFX_ByteString empty;
+ CFX_ByteString a("a");
+ CFX_ByteString abc("abc");
+ CFX_ByteString def("def");
+
+ EXPECT_FALSE(empty < empty);
+ EXPECT_FALSE(a < a);
+ EXPECT_FALSE(abc < abc);
+ EXPECT_FALSE(def < def);
+
+ EXPECT_TRUE(empty < a);
+ EXPECT_FALSE(a < empty);
+
+ EXPECT_TRUE(empty < abc);
+ EXPECT_FALSE(abc < empty);
+
+ EXPECT_TRUE(empty < def);
+ EXPECT_FALSE(def < empty);
+
+ EXPECT_TRUE(a < abc);
+ EXPECT_FALSE(abc < a);
+
+ EXPECT_TRUE(a < def);
+ EXPECT_FALSE(def < a);
+
+ EXPECT_TRUE(abc < def);
+ EXPECT_FALSE(def < abc);
+}
+
TEST(fxcrt, ByteStringCNull) {
CFX_ByteStringC null_string;
EXPECT_EQ(null_string.GetPtr(), nullptr);