summaryrefslogtreecommitdiff
path: root/core/fxcrt
diff options
context:
space:
mode:
Diffstat (limited to 'core/fxcrt')
-rw-r--r--core/fxcrt/cfx_bytestring_unittest.cpp13
-rw-r--r--core/fxcrt/cfx_string_c_template.h8
-rw-r--r--core/fxcrt/cfx_widestring_unittest.cpp14
3 files changed, 35 insertions, 0 deletions
diff --git a/core/fxcrt/cfx_bytestring_unittest.cpp b/core/fxcrt/cfx_bytestring_unittest.cpp
index 3b679a086c..9cfc773fad 100644
--- a/core/fxcrt/cfx_bytestring_unittest.cpp
+++ b/core/fxcrt/cfx_bytestring_unittest.cpp
@@ -4,6 +4,8 @@
#include "core/fxcrt/cfx_bytestring.h"
+#include <vector>
+
#include "testing/fx_string_testhelpers.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -773,6 +775,17 @@ TEST(fxcrt, ByteStringCFromChar) {
EXPECT_NE(longer_string, lower_a_string_from_char);
}
+TEST(fxcrt, ByteStringCFromVector) {
+ std::vector<uint8_t> null_vec;
+ CFX_ByteStringC null_string(null_vec);
+ EXPECT_EQ(0, null_string.GetLength());
+
+ std::vector<uint8_t> lower_a_vec(10, static_cast<uint8_t>('a'));
+ CFX_ByteStringC lower_a_string(lower_a_vec);
+ EXPECT_EQ(10, lower_a_string.GetLength());
+ EXPECT_EQ("aaaaaaaaaa", lower_a_string);
+}
+
TEST(fxcrt, ByteStringCGetID) {
CFX_ByteStringC null_string;
EXPECT_EQ(0u, null_string.GetID());
diff --git a/core/fxcrt/cfx_string_c_template.h b/core/fxcrt/cfx_string_c_template.h
index b52817d790..4aba2cd4b9 100644
--- a/core/fxcrt/cfx_string_c_template.h
+++ b/core/fxcrt/cfx_string_c_template.h
@@ -9,8 +9,10 @@
#include <algorithm>
#include <type_traits>
+#include <vector>
#include "core/fxcrt/fx_system.h"
+#include "third_party/base/stl_util.h"
// An immutable string with caller-provided storage which must outlive the
// string itself. These are not necessarily nul-terminated, so that substring
@@ -53,6 +55,12 @@ class CFX_StringCTemplate {
m_Length = src.m_Length;
}
+ // Any changes to |vec| invalidate the string.
+ explicit CFX_StringCTemplate(const std::vector<UnsignedType>& vec) {
+ m_Ptr = vec.data();
+ m_Length = pdfium::CollectionSize<FX_STRSIZE>(vec);
+ }
+
CFX_StringCTemplate& operator=(const CharType* src) {
m_Ptr = reinterpret_cast<const UnsignedType*>(src);
m_Length = src ? FXSYS_len(src) : 0;
diff --git a/core/fxcrt/cfx_widestring_unittest.cpp b/core/fxcrt/cfx_widestring_unittest.cpp
index dd44cb70af..498b4f0b6a 100644
--- a/core/fxcrt/cfx_widestring_unittest.cpp
+++ b/core/fxcrt/cfx_widestring_unittest.cpp
@@ -4,6 +4,8 @@
#include "core/fxcrt/cfx_widestring.h"
+#include <vector>
+
#include "testing/fx_string_testhelpers.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -689,6 +691,18 @@ TEST(fxcrt, WideStringUTF16LE_Encode) {
}
}
+TEST(fxcrt, WideStringCFromVector) {
+ std::vector<CFX_WideStringC::UnsignedType> null_vec;
+ CFX_WideStringC null_string(null_vec);
+ EXPECT_EQ(0, null_string.GetLength());
+
+ std::vector<CFX_WideStringC::UnsignedType> lower_a_vec(
+ 10, static_cast<CFX_WideStringC::UnsignedType>(L'a'));
+ CFX_WideStringC lower_a_string(lower_a_vec);
+ EXPECT_EQ(10, lower_a_string.GetLength());
+ EXPECT_EQ(L"aaaaaaaaaa", lower_a_string);
+}
+
TEST(fxcrt, WideStringCOperatorSubscript) {
// CFX_WideStringC includes the NUL terminator for non-empty strings.
CFX_WideStringC abc(L"abc");