summaryrefslogtreecommitdiff
path: root/core/fxcrt
diff options
context:
space:
mode:
authorRyan Harrison <rharrison@chromium.org>2017-09-13 11:16:32 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-09-13 15:42:13 +0000
commit06c6855258bf25246c46a1f628b8a8a8185029a7 (patch)
tree7db9bd2cae8672a06834105542cef5c70bc9db08 /core/fxcrt
parentf2ca50ffa2d26a6c023add24e92adbe6b28bfcc9 (diff)
downloadpdfium-06c6855258bf25246c46a1f628b8a8a8185029a7.tar.xz
Rewrite IsHyphen using string operations
The existing code did end of range checks by making sure that the value was never less then 0. This isn't correct when using an unsigned type, since 0 - 1 will wrap around to the max possible value, and thus still be less then 0. Additionally the existing code was hard to follow due to the complexity of some of the low level operations being performed. It has been rewritten using higher level string operations to make it clearer and correct. BUG=chromium:763256 Change-Id: Ib8bf5ca0e29e73724c4a1c4781362e8a8fc30149 Reviewed-on: https://pdfium-review.googlesource.com/13690 Commit-Queue: Ryan Harrison <rharrison@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core/fxcrt')
-rw-r--r--core/fxcrt/cfx_binarybuf.cpp4
-rw-r--r--core/fxcrt/cfx_binarybuf.h4
-rw-r--r--core/fxcrt/cfx_bytestring_unittest.cpp8
-rw-r--r--core/fxcrt/cfx_string_c_template.h14
-rw-r--r--core/fxcrt/cfx_widestring_unittest.cpp8
-rw-r--r--core/fxcrt/cfx_widetextbuf.cpp4
-rw-r--r--core/fxcrt/cfx_widetextbuf.h2
7 files changed, 42 insertions, 2 deletions
diff --git a/core/fxcrt/cfx_binarybuf.cpp b/core/fxcrt/cfx_binarybuf.cpp
index 73fe945fd6..b826fdd5b5 100644
--- a/core/fxcrt/cfx_binarybuf.cpp
+++ b/core/fxcrt/cfx_binarybuf.cpp
@@ -28,6 +28,10 @@ void CFX_BinaryBuf::Delete(FX_STRSIZE start_index, FX_STRSIZE count) {
m_DataSize -= count;
}
+FX_STRSIZE CFX_BinaryBuf::GetLength() const {
+ return m_DataSize;
+}
+
void CFX_BinaryBuf::Clear() {
m_DataSize = 0;
}
diff --git a/core/fxcrt/cfx_binarybuf.h b/core/fxcrt/cfx_binarybuf.h
index 3081d02902..4c795eda0b 100644
--- a/core/fxcrt/cfx_binarybuf.h
+++ b/core/fxcrt/cfx_binarybuf.h
@@ -17,10 +17,12 @@ class CFX_BinaryBuf {
public:
CFX_BinaryBuf();
explicit CFX_BinaryBuf(FX_STRSIZE size);
- ~CFX_BinaryBuf();
+ virtual ~CFX_BinaryBuf();
uint8_t* GetBuffer() const { return m_pBuffer.get(); }
FX_STRSIZE GetSize() const { return m_DataSize; }
+ virtual FX_STRSIZE GetLength() const;
+ bool IsEmpty() const { return GetLength() == 0; }
void Clear();
void EstimateSize(FX_STRSIZE size, FX_STRSIZE alloc_step = 0);
diff --git a/core/fxcrt/cfx_bytestring_unittest.cpp b/core/fxcrt/cfx_bytestring_unittest.cpp
index dcb8577b65..a4c5187c11 100644
--- a/core/fxcrt/cfx_bytestring_unittest.cpp
+++ b/core/fxcrt/cfx_bytestring_unittest.cpp
@@ -999,6 +999,14 @@ TEST(fxcrt, ByteStringCMid) {
EXPECT_EQ("", longer_string.Mid(4, 3));
}
+TEST(fxcrt, ByteStringCTrimmedRight) {
+ CFX_ByteStringC fred("FRED");
+ EXPECT_EQ("FRED", fred.TrimmedRight('E'));
+ EXPECT_EQ("FRE", fred.TrimmedRight('D'));
+ CFX_ByteStringC fredd("FREDD");
+ EXPECT_EQ("FRE", fred.TrimmedRight('D'));
+}
+
TEST(fxcrt, ByteStringCElementAccess) {
// CFX_ByteStringC includes the NUL terminator for non-empty strings.
CFX_ByteStringC abc("abc");
diff --git a/core/fxcrt/cfx_string_c_template.h b/core/fxcrt/cfx_string_c_template.h
index 8585d73ae6..b0e17a1222 100644
--- a/core/fxcrt/cfx_string_c_template.h
+++ b/core/fxcrt/cfx_string_c_template.h
@@ -176,6 +176,20 @@ class CFX_StringCTemplate {
return Mid(GetLength() - count, count);
}
+ CFX_StringCTemplate TrimmedRight(CharType ch) const {
+ if (IsEmpty())
+ return CFX_StringCTemplate();
+
+ FX_STRSIZE pos = GetLength();
+ while (pos && CharAt(pos - 1) == ch)
+ pos--;
+
+ if (pos == 0)
+ return CFX_StringCTemplate();
+
+ return CFX_StringCTemplate(m_Ptr.Get(), pos);
+ }
+
bool operator<(const CFX_StringCTemplate& that) const {
int result = FXSYS_cmp(reinterpret_cast<const CharType*>(m_Ptr.Get()),
reinterpret_cast<const CharType*>(that.m_Ptr.Get()),
diff --git a/core/fxcrt/cfx_widestring_unittest.cpp b/core/fxcrt/cfx_widestring_unittest.cpp
index 7b12d50601..1c8aca9aca 100644
--- a/core/fxcrt/cfx_widestring_unittest.cpp
+++ b/core/fxcrt/cfx_widestring_unittest.cpp
@@ -1038,6 +1038,14 @@ TEST(fxcrt, WideStringCAnyAllNoneOf) {
EXPECT_FALSE(pdfium::ContainsValue(str, L'z'));
}
+TEST(fxcrt, WideStringCTrimmedRight) {
+ CFX_WideStringC fred(L"FRED");
+ EXPECT_EQ(L"FRED", fred.TrimmedRight(L'E'));
+ EXPECT_EQ(L"FRE", fred.TrimmedRight(L'D'));
+ CFX_WideStringC fredd(L"FREDD");
+ EXPECT_EQ(L"FRE", fred.TrimmedRight(L'D'));
+}
+
TEST(fxcrt, WideStringFormatWidth) {
{
CFX_WideString str;
diff --git a/core/fxcrt/cfx_widetextbuf.cpp b/core/fxcrt/cfx_widetextbuf.cpp
index 246124b95f..81b5fd4b05 100644
--- a/core/fxcrt/cfx_widetextbuf.cpp
+++ b/core/fxcrt/cfx_widetextbuf.cpp
@@ -6,6 +6,10 @@
#include "core/fxcrt/cfx_widetextbuf.h"
+FX_STRSIZE CFX_WideTextBuf::GetLength() const {
+ return m_DataSize / sizeof(wchar_t);
+}
+
void CFX_WideTextBuf::AppendChar(wchar_t ch) {
ExpandBuf(sizeof(wchar_t));
*(wchar_t*)(m_pBuffer.get() + m_DataSize) = ch;
diff --git a/core/fxcrt/cfx_widetextbuf.h b/core/fxcrt/cfx_widetextbuf.h
index 0c9b6ff99d..389f2e52a3 100644
--- a/core/fxcrt/cfx_widetextbuf.h
+++ b/core/fxcrt/cfx_widetextbuf.h
@@ -14,7 +14,7 @@
class CFX_WideTextBuf : public CFX_BinaryBuf {
public:
void AppendChar(wchar_t wch);
- FX_STRSIZE GetLength() const { return m_DataSize / sizeof(wchar_t); }
+ FX_STRSIZE GetLength() const override;
wchar_t* GetBuffer() const {
return reinterpret_cast<wchar_t*>(m_pBuffer.get());
}