From 8e9e3d8975eeea3429c3b3ea703f04ac34e20e28 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 21 Sep 2017 16:49:32 -0400 Subject: Move CFX_StringPoolTemplate to StringPoolTemplate This CL moves CFX_StringPoolTemplate to StringPoolTemplate and places in the fxcrt namespace. Bug: pdfium:898 Change-Id: I41c8f23d6ad814d65cd154092143efaa630a96d1 Reviewed-on: https://pdfium-review.googlesource.com/14619 Reviewed-by: Tom Sepez Commit-Queue: dsinclair --- BUILD.gn | 4 +- core/fpdfapi/font/cpdf_fontencoding.h | 2 +- core/fpdfapi/page/cpdf_streamparser.h | 2 +- core/fpdfapi/parser/cpdf_dictionary.h | 2 +- core/fpdfapi/parser/cpdf_indirect_object_holder.h | 2 +- core/fpdfapi/parser/cpdf_name.h | 2 +- core/fpdfapi/parser/cpdf_string.h | 2 +- core/fpdfapi/parser/cpdf_syntax_parser.h | 2 +- core/fpdfdoc/cpdf_filespec.h | 2 +- core/fxcrt/bytestring.cpp | 4 +- core/fxcrt/bytestring.h | 7 +- core/fxcrt/bytestring_unittest.cpp | 4 + core/fxcrt/cfx_string_pool_template.h | 30 ------- core/fxcrt/cfx_string_pool_template_unittest.cpp | 93 ---------------------- core/fxcrt/maybe_owned_unittest.cpp | 1 - core/fxcrt/observable_unittest.cpp | 3 + core/fxcrt/retain_ptr_unittest.cpp | 3 + core/fxcrt/shared_copy_on_write_unittest.cpp | 3 + core/fxcrt/string_pool_template.h | 36 +++++++++ core/fxcrt/string_pool_template_unittest.cpp | 97 +++++++++++++++++++++++ core/fxcrt/widestring.cpp | 4 +- core/fxcrt/widestring.h | 6 +- core/fxcrt/widestring_unittest.cpp | 4 + 23 files changed, 170 insertions(+), 145 deletions(-) delete mode 100644 core/fxcrt/cfx_string_pool_template.h delete mode 100644 core/fxcrt/cfx_string_pool_template_unittest.cpp create mode 100644 core/fxcrt/string_pool_template.h create mode 100644 core/fxcrt/string_pool_template_unittest.cpp diff --git a/BUILD.gn b/BUILD.gn index a823fe41f6..7b9664c7db 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -821,7 +821,6 @@ static_library("fxcrt") { "core/fxcrt/cfx_fixedbufgrow.h", "core/fxcrt/cfx_memorystream.cpp", "core/fxcrt/cfx_memorystream.h", - "core/fxcrt/cfx_string_pool_template.h", "core/fxcrt/cfx_unowned_ptr.h", "core/fxcrt/cfx_utf8decoder.cpp", "core/fxcrt/cfx_utf8decoder.h", @@ -857,6 +856,7 @@ static_library("fxcrt") { "core/fxcrt/retain_ptr.h", "core/fxcrt/shared_copy_on_write.h", "core/fxcrt/string_data_template.h", + "core/fxcrt/string_pool_template.h", "core/fxcrt/string_view_template.h", "core/fxcrt/widestring.cpp", "core/fxcrt/widestring.h", @@ -1920,7 +1920,6 @@ test("pdfium_unittests") { "core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp", "core/fxcodec/jbig2/JBig2_Image_unittest.cpp", "core/fxcrt/bytestring_unittest.cpp", - "core/fxcrt/cfx_string_pool_template_unittest.cpp", "core/fxcrt/cfx_unowned_ptr_unittest.cpp", "core/fxcrt/cfx_weak_ptr_unittest.cpp", "core/fxcrt/fx_bidi_unittest.cpp", @@ -1934,6 +1933,7 @@ test("pdfium_unittests") { "core/fxcrt/observable_unittest.cpp", "core/fxcrt/retain_ptr_unittest.cpp", "core/fxcrt/shared_copy_on_write_unittest.cpp", + "core/fxcrt/string_pool_template_unittest.cpp", "core/fxcrt/widestring_unittest.cpp", "core/fxge/dib/cstretchengine_unittest.cpp", "fpdfsdk/fpdfdoc_unittest.cpp", diff --git a/core/fpdfapi/font/cpdf_fontencoding.h b/core/fpdfapi/font/cpdf_fontencoding.h index 075cc04c7c..d96f787da1 100644 --- a/core/fpdfapi/font/cpdf_fontencoding.h +++ b/core/fpdfapi/font/cpdf_fontencoding.h @@ -9,9 +9,9 @@ #include -#include "core/fxcrt/cfx_string_pool_template.h" #include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/fx_string.h" +#include "core/fxcrt/string_pool_template.h" #define PDFFONT_ENCODING_BUILTIN 0 #define PDFFONT_ENCODING_WINANSI 1 diff --git a/core/fpdfapi/page/cpdf_streamparser.h b/core/fpdfapi/page/cpdf_streamparser.h index 32001a8b6e..53d5dfd135 100644 --- a/core/fpdfapi/page/cpdf_streamparser.h +++ b/core/fpdfapi/page/cpdf_streamparser.h @@ -14,8 +14,8 @@ #include "core/fpdfapi/parser/cpdf_document.h" #include "core/fpdfapi/parser/cpdf_object.h" #include "core/fpdfapi/parser/cpdf_stream.h" -#include "core/fxcrt/cfx_string_pool_template.h" #include "core/fxcrt/cfx_weak_ptr.h" +#include "core/fxcrt/string_pool_template.h" class CPDF_StreamParser { public: diff --git a/core/fpdfapi/parser/cpdf_dictionary.h b/core/fpdfapi/parser/cpdf_dictionary.h index 93c00c8c10..a9ff353fbe 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.h +++ b/core/fpdfapi/parser/cpdf_dictionary.h @@ -13,10 +13,10 @@ #include #include "core/fpdfapi/parser/cpdf_object.h" -#include "core/fxcrt/cfx_string_pool_template.h" #include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/fx_coordinates.h" #include "core/fxcrt/fx_string.h" +#include "core/fxcrt/string_pool_template.h" #include "third_party/base/ptr_util.h" class CPDF_IndirectObjectHolder; diff --git a/core/fpdfapi/parser/cpdf_indirect_object_holder.h b/core/fpdfapi/parser/cpdf_indirect_object_holder.h index ab99dd5fcc..ea70c4a351 100644 --- a/core/fpdfapi/parser/cpdf_indirect_object_holder.h +++ b/core/fpdfapi/parser/cpdf_indirect_object_holder.h @@ -14,9 +14,9 @@ #include #include "core/fpdfapi/parser/cpdf_object.h" -#include "core/fxcrt/cfx_string_pool_template.h" #include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/fx_system.h" +#include "core/fxcrt/string_pool_template.h" #include "third_party/base/ptr_util.h" class CPDF_IndirectObjectHolder { diff --git a/core/fpdfapi/parser/cpdf_name.h b/core/fpdfapi/parser/cpdf_name.h index 72a72a3a66..8a1cf2b63b 100644 --- a/core/fpdfapi/parser/cpdf_name.h +++ b/core/fpdfapi/parser/cpdf_name.h @@ -10,8 +10,8 @@ #include #include "core/fpdfapi/parser/cpdf_object.h" -#include "core/fxcrt/cfx_string_pool_template.h" #include "core/fxcrt/cfx_weak_ptr.h" +#include "core/fxcrt/string_pool_template.h" class CPDF_Name : public CPDF_Object { public: diff --git a/core/fpdfapi/parser/cpdf_string.h b/core/fpdfapi/parser/cpdf_string.h index 9fd3e6ae8a..6f762ae27b 100644 --- a/core/fpdfapi/parser/cpdf_string.h +++ b/core/fpdfapi/parser/cpdf_string.h @@ -10,10 +10,10 @@ #include #include "core/fpdfapi/parser/cpdf_object.h" -#include "core/fxcrt/cfx_string_pool_template.h" #include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/fx_system.h" +#include "core/fxcrt/string_pool_template.h" class CPDF_String : public CPDF_Object { public: diff --git a/core/fpdfapi/parser/cpdf_syntax_parser.h b/core/fpdfapi/parser/cpdf_syntax_parser.h index 78d5345001..cb266837dc 100644 --- a/core/fpdfapi/parser/cpdf_syntax_parser.h +++ b/core/fpdfapi/parser/cpdf_syntax_parser.h @@ -10,8 +10,8 @@ #include #include -#include "core/fxcrt/cfx_string_pool_template.h" #include "core/fxcrt/cfx_weak_ptr.h" +#include "core/fxcrt/string_pool_template.h" class CPDF_CryptoHandler; class CPDF_Dictionary; diff --git a/core/fpdfdoc/cpdf_filespec.h b/core/fpdfdoc/cpdf_filespec.h index 2cbbc15977..131dfbe7ca 100644 --- a/core/fpdfdoc/cpdf_filespec.h +++ b/core/fpdfdoc/cpdf_filespec.h @@ -7,10 +7,10 @@ #ifndef CORE_FPDFDOC_CPDF_FILESPEC_H_ #define CORE_FPDFDOC_CPDF_FILESPEC_H_ -#include "core/fxcrt/cfx_string_pool_template.h" #include "core/fxcrt/cfx_unowned_ptr.h" #include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/fx_string.h" +#include "core/fxcrt/string_pool_template.h" class CPDF_Dictionary; class CPDF_Object; diff --git a/core/fxcrt/bytestring.cpp b/core/fxcrt/bytestring.cpp index a78020bae4..aa1fb57775 100644 --- a/core/fxcrt/bytestring.cpp +++ b/core/fxcrt/bytestring.cpp @@ -12,17 +12,17 @@ #include #include -#include "core/fxcrt/cfx_string_pool_template.h" #include "core/fxcrt/cfx_utf8decoder.h" #include "core/fxcrt/fx_codepage.h" #include "core/fxcrt/fx_extension.h" #include "core/fxcrt/fx_safe_types.h" +#include "core/fxcrt/string_pool_template.h" #include "third_party/base/numerics/safe_math.h" #include "third_party/base/stl_util.h" template class fxcrt::StringDataTemplate; template class fxcrt::StringViewTemplate; -template class CFX_StringPoolTemplate; +template class fxcrt::StringPoolTemplate; template struct std::hash; namespace { diff --git a/core/fxcrt/bytestring.h b/core/fxcrt/bytestring.h index c2c41e8c47..eba9744402 100644 --- a/core/fxcrt/bytestring.h +++ b/core/fxcrt/bytestring.h @@ -18,11 +18,10 @@ #include "core/fxcrt/string_view_template.h" #include "third_party/base/optional.h" -class ByteString_Concat_Test; -class fxcrt_ByteStringPool_Test; - namespace fxcrt { +class ByteString_Concat_Test; +class StringPool_ByteString_Test; class WideString; // A mutable string with shared buffers using copy-on-write semantics that @@ -198,7 +197,7 @@ class ByteString { RetainPtr m_pData; friend ByteString_Concat_Test; - friend fxcrt_ByteStringPool_Test; + friend class StringPool_ByteString_Test; }; inline bool operator==(const char* lhs, const ByteString& rhs) { diff --git a/core/fxcrt/bytestring_unittest.cpp b/core/fxcrt/bytestring_unittest.cpp index 06ba1f42fd..e1c832e114 100644 --- a/core/fxcrt/bytestring_unittest.cpp +++ b/core/fxcrt/bytestring_unittest.cpp @@ -11,6 +11,8 @@ #include "testing/gtest/include/gtest/gtest.h" #include "third_party/base/stl_util.h" +namespace fxcrt { + TEST(ByteString, ElementAccess) { const ByteString abc("abc"); EXPECT_EQ('a', abc[0]); @@ -1640,3 +1642,5 @@ TEST(ByteString, FormatInteger) { EXPECT_EQ("2147483647", ByteString::FormatInteger(INT_MAX)); EXPECT_EQ("-2147483648", ByteString::FormatInteger(INT_MIN)); } + +} // namespace fxcrt diff --git a/core/fxcrt/cfx_string_pool_template.h b/core/fxcrt/cfx_string_pool_template.h deleted file mode 100644 index b170a84127..0000000000 --- a/core/fxcrt/cfx_string_pool_template.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2016 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_FXCRT_CFX_STRING_POOL_TEMPLATE_H_ -#define CORE_FXCRT_CFX_STRING_POOL_TEMPLATE_H_ - -#include - -#include "core/fxcrt/fx_string.h" - -template -class CFX_StringPoolTemplate { - public: - StringType Intern(const StringType& str) { return *m_Pool.insert(str).first; } - void Clear() { m_Pool.clear(); } - - private: - std::unordered_set m_Pool; -}; - -using ByteStringPool = CFX_StringPoolTemplate; -using WideStringPool = CFX_StringPoolTemplate; - -extern template class CFX_StringPoolTemplate; -extern template class CFX_StringPoolTemplate; - -#endif // CORE_FXCRT_CFX_STRING_POOL_TEMPLATE_H_ diff --git a/core/fxcrt/cfx_string_pool_template_unittest.cpp b/core/fxcrt/cfx_string_pool_template_unittest.cpp deleted file mode 100644 index 9a5ac5d3ed..0000000000 --- a/core/fxcrt/cfx_string_pool_template_unittest.cpp +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2016 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "core/fxcrt/cfx_string_pool_template.h" -#include "core/fxcrt/fx_string.h" -#include "testing/gtest/include/gtest/gtest.h" - -TEST(fxcrt, ByteStringPool) { - ByteStringPool pool; - - ByteString null1; - ByteString null2; - ByteString goats1("goats"); - ByteString goats2("goats"); - - // Underlying storage, if non-null, is not shared. - EXPECT_EQ(nullptr, null1.m_pData.Get()); - EXPECT_EQ(nullptr, null2.m_pData.Get()); - EXPECT_NE(goats1.m_pData, goats2.m_pData); - - ByteString interned_null1 = pool.Intern(null1); - ByteString interned_null2 = pool.Intern(null2); - ByteString interned_goats1 = pool.Intern(goats1); - ByteString interned_goats2 = pool.Intern(goats2); - - // Strings are logically equal after being interned. - EXPECT_EQ(null1, interned_null1); - EXPECT_EQ(null2, interned_null2); - EXPECT_EQ(goats1, interned_goats1); - EXPECT_EQ(goats2, interned_goats2); - - // Interned underlying storage, if non-null, belongs to first seen. - EXPECT_EQ(nullptr, interned_null1.m_pData.Get()); - EXPECT_EQ(nullptr, interned_null2.m_pData.Get()); - EXPECT_EQ(goats1.m_pData, interned_goats1.m_pData); - EXPECT_EQ(goats1.m_pData, interned_goats2.m_pData); - - pool.Clear(); - ByteString reinterned_null2 = pool.Intern(null2); - ByteString reinterned_null1 = pool.Intern(null2); - ByteString reinterned_goats2 = pool.Intern(goats2); - ByteString reinterned_goats1 = pool.Intern(goats2); - - // After clearing pool, storage was re-interned using second strings. - EXPECT_EQ(nullptr, interned_null1.m_pData.Get()); - EXPECT_EQ(nullptr, interned_null2.m_pData.Get()); - EXPECT_EQ(goats2.m_pData, reinterned_goats1.m_pData); - EXPECT_EQ(goats2.m_pData, reinterned_goats2.m_pData); -} - -TEST(fxcrt, WideStringPool) { - WideStringPool pool; - - WideString null1; - WideString null2; - WideString goats1(L"goats"); - WideString goats2(L"goats"); - - // Underlying storage, if non-null, is not shared. - EXPECT_EQ(nullptr, null1.m_pData.Get()); - EXPECT_EQ(nullptr, null2.m_pData.Get()); - EXPECT_NE(goats1.m_pData, goats2.m_pData); - - WideString interned_null1 = pool.Intern(null1); - WideString interned_null2 = pool.Intern(null2); - WideString interned_goats1 = pool.Intern(goats1); - WideString interned_goats2 = pool.Intern(goats2); - - // Strings are logically equal after being interned. - EXPECT_EQ(null1, interned_null1); - EXPECT_EQ(null2, interned_null2); - EXPECT_EQ(goats1, interned_goats1); - EXPECT_EQ(goats2, interned_goats2); - - // Interned underlying storage, if non-null, belongs to first seen. - EXPECT_EQ(nullptr, interned_null1.m_pData.Get()); - EXPECT_EQ(nullptr, interned_null2.m_pData.Get()); - EXPECT_EQ(goats1.m_pData, interned_goats1.m_pData); - EXPECT_EQ(goats1.m_pData, interned_goats2.m_pData); - - pool.Clear(); - WideString reinterned_null2 = pool.Intern(null2); - WideString reinterned_null1 = pool.Intern(null2); - WideString reinterned_goats2 = pool.Intern(goats2); - WideString reinterned_goats1 = pool.Intern(goats2); - - // After clearing pool, storage was re-interned using second strings. - EXPECT_EQ(nullptr, interned_null1.m_pData.Get()); - EXPECT_EQ(nullptr, interned_null2.m_pData.Get()); - EXPECT_EQ(goats2.m_pData, reinterned_goats1.m_pData); - EXPECT_EQ(goats2.m_pData, reinterned_goats2.m_pData); -} diff --git a/core/fxcrt/maybe_owned_unittest.cpp b/core/fxcrt/maybe_owned_unittest.cpp index 686fa6ffd2..7182647207 100644 --- a/core/fxcrt/maybe_owned_unittest.cpp +++ b/core/fxcrt/maybe_owned_unittest.cpp @@ -12,7 +12,6 @@ #include "third_party/base/ptr_util.h" namespace fxcrt { - namespace { class PseudoDeletable { diff --git a/core/fxcrt/observable_unittest.cpp b/core/fxcrt/observable_unittest.cpp index 8edc58f613..5e64743d86 100644 --- a/core/fxcrt/observable_unittest.cpp +++ b/core/fxcrt/observable_unittest.cpp @@ -9,6 +9,7 @@ #include "testing/gtest/include/gtest/gtest.h" +namespace fxcrt { namespace { class PseudoObservable : public Observable { @@ -183,3 +184,5 @@ TEST(ObservePtr, Bool) { EXPECT_FALSE(null_bool); EXPECT_TRUE(obj1_bool); } + +} // namespace fxcrt diff --git a/core/fxcrt/retain_ptr_unittest.cpp b/core/fxcrt/retain_ptr_unittest.cpp index 3e14478dbf..f4b2994eae 100644 --- a/core/fxcrt/retain_ptr_unittest.cpp +++ b/core/fxcrt/retain_ptr_unittest.cpp @@ -9,6 +9,7 @@ #include "testing/gtest/include/gtest/gtest.h" +namespace fxcrt { namespace { class PseudoRetainable { @@ -298,3 +299,5 @@ TEST(RetainPtr, VectorMove) { EXPECT_EQ(1, obj.retain_count()); EXPECT_EQ(1, obj.release_count()); } + +} // namespace fxcrt diff --git a/core/fxcrt/shared_copy_on_write_unittest.cpp b/core/fxcrt/shared_copy_on_write_unittest.cpp index a683767054..01cc09468c 100644 --- a/core/fxcrt/shared_copy_on_write_unittest.cpp +++ b/core/fxcrt/shared_copy_on_write_unittest.cpp @@ -9,6 +9,7 @@ #include "testing/gtest/include/gtest/gtest.h" +namespace fxcrt { namespace { class Observer { @@ -126,3 +127,5 @@ TEST(SharedCopyOnWrite, GetModify) { } EXPECT_EQ(2, observer.GetDestructionCount("one")); } + +} // namespace fxcrt diff --git a/core/fxcrt/string_pool_template.h b/core/fxcrt/string_pool_template.h new file mode 100644 index 0000000000..2f9fb09a9f --- /dev/null +++ b/core/fxcrt/string_pool_template.h @@ -0,0 +1,36 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXCRT_STRING_POOL_TEMPLATE_H_ +#define CORE_FXCRT_STRING_POOL_TEMPLATE_H_ + +#include + +#include "core/fxcrt/fx_string.h" + +namespace fxcrt { + +template +class StringPoolTemplate { + public: + StringType Intern(const StringType& str) { return *m_Pool.insert(str).first; } + void Clear() { m_Pool.clear(); } + + private: + std::unordered_set m_Pool; +}; + +extern template class StringPoolTemplate; +extern template class StringPoolTemplate; + +} // namespace fxcrt + +using fxcrt::StringPoolTemplate; + +using ByteStringPool = StringPoolTemplate; +using WideStringPool = StringPoolTemplate; + +#endif // CORE_FXCRT_STRING_POOL_TEMPLATE_H_ diff --git a/core/fxcrt/string_pool_template_unittest.cpp b/core/fxcrt/string_pool_template_unittest.cpp new file mode 100644 index 0000000000..717e33d919 --- /dev/null +++ b/core/fxcrt/string_pool_template_unittest.cpp @@ -0,0 +1,97 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "core/fxcrt/string_pool_template.h" +#include "core/fxcrt/fx_string.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace fxcrt { + +TEST(StringPool, ByteString) { + ByteStringPool pool; + + ByteString null1; + ByteString null2; + ByteString goats1("goats"); + ByteString goats2("goats"); + + // Underlying storage, if non-null, is not shared. + EXPECT_EQ(nullptr, null1.m_pData.Get()); + EXPECT_EQ(nullptr, null2.m_pData.Get()); + EXPECT_NE(goats1.m_pData, goats2.m_pData); + + ByteString interned_null1 = pool.Intern(null1); + ByteString interned_null2 = pool.Intern(null2); + ByteString interned_goats1 = pool.Intern(goats1); + ByteString interned_goats2 = pool.Intern(goats2); + + // Strings are logically equal after being interned. + EXPECT_EQ(null1, interned_null1); + EXPECT_EQ(null2, interned_null2); + EXPECT_EQ(goats1, interned_goats1); + EXPECT_EQ(goats2, interned_goats2); + + // Interned underlying storage, if non-null, belongs to first seen. + EXPECT_EQ(nullptr, interned_null1.m_pData.Get()); + EXPECT_EQ(nullptr, interned_null2.m_pData.Get()); + EXPECT_EQ(goats1.m_pData, interned_goats1.m_pData); + EXPECT_EQ(goats1.m_pData, interned_goats2.m_pData); + + pool.Clear(); + ByteString reinterned_null2 = pool.Intern(null2); + ByteString reinterned_null1 = pool.Intern(null2); + ByteString reinterned_goats2 = pool.Intern(goats2); + ByteString reinterned_goats1 = pool.Intern(goats2); + + // After clearing pool, storage was re-interned using second strings. + EXPECT_EQ(nullptr, interned_null1.m_pData.Get()); + EXPECT_EQ(nullptr, interned_null2.m_pData.Get()); + EXPECT_EQ(goats2.m_pData, reinterned_goats1.m_pData); + EXPECT_EQ(goats2.m_pData, reinterned_goats2.m_pData); +} + +TEST(StringPool, WideString) { + WideStringPool pool; + + WideString null1; + WideString null2; + WideString goats1(L"goats"); + WideString goats2(L"goats"); + + // Underlying storage, if non-null, is not shared. + EXPECT_EQ(nullptr, null1.m_pData.Get()); + EXPECT_EQ(nullptr, null2.m_pData.Get()); + EXPECT_NE(goats1.m_pData, goats2.m_pData); + + WideString interned_null1 = pool.Intern(null1); + WideString interned_null2 = pool.Intern(null2); + WideString interned_goats1 = pool.Intern(goats1); + WideString interned_goats2 = pool.Intern(goats2); + + // Strings are logically equal after being interned. + EXPECT_EQ(null1, interned_null1); + EXPECT_EQ(null2, interned_null2); + EXPECT_EQ(goats1, interned_goats1); + EXPECT_EQ(goats2, interned_goats2); + + // Interned underlying storage, if non-null, belongs to first seen. + EXPECT_EQ(nullptr, interned_null1.m_pData.Get()); + EXPECT_EQ(nullptr, interned_null2.m_pData.Get()); + EXPECT_EQ(goats1.m_pData, interned_goats1.m_pData); + EXPECT_EQ(goats1.m_pData, interned_goats2.m_pData); + + pool.Clear(); + WideString reinterned_null2 = pool.Intern(null2); + WideString reinterned_null1 = pool.Intern(null2); + WideString reinterned_goats2 = pool.Intern(goats2); + WideString reinterned_goats1 = pool.Intern(goats2); + + // After clearing pool, storage was re-interned using second strings. + EXPECT_EQ(nullptr, interned_null1.m_pData.Get()); + EXPECT_EQ(nullptr, interned_null2.m_pData.Get()); + EXPECT_EQ(goats2.m_pData, reinterned_goats1.m_pData); + EXPECT_EQ(goats2.m_pData, reinterned_goats2.m_pData); +} + +} // namespace fxcrt diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp index e5925845be..5fe7ee4777 100644 --- a/core/fxcrt/widestring.cpp +++ b/core/fxcrt/widestring.cpp @@ -12,17 +12,17 @@ #include #include -#include "core/fxcrt/cfx_string_pool_template.h" #include "core/fxcrt/cfx_utf8decoder.h" #include "core/fxcrt/fx_codepage.h" #include "core/fxcrt/fx_extension.h" #include "core/fxcrt/fx_safe_types.h" +#include "core/fxcrt/string_pool_template.h" #include "third_party/base/numerics/safe_math.h" #include "third_party/base/stl_util.h" template class fxcrt::StringDataTemplate; template class fxcrt::StringViewTemplate; -template class CFX_StringPoolTemplate; +template class fxcrt::StringPoolTemplate; template struct std::hash; #define FORCE_ANSI 0x10000 diff --git a/core/fxcrt/widestring.h b/core/fxcrt/widestring.h index df7d95fc6f..f56703f1ab 100644 --- a/core/fxcrt/widestring.h +++ b/core/fxcrt/widestring.h @@ -18,12 +18,12 @@ #include "core/fxcrt/string_view_template.h" #include "third_party/base/optional.h" -class WideString_ConcatInPlace_Test; -class fxcrt_WideStringPool_Test; namespace fxcrt { class ByteString; +class StringPool_WideString_Test; +class WideString_ConcatInPlace_Test; // A mutable string with shared buffers using copy-on-write semantics that // avoids the cost of std::string's iterator stability guarantees. @@ -195,7 +195,7 @@ class WideString { RetainPtr m_pData; friend WideString_ConcatInPlace_Test; - friend fxcrt_WideStringPool_Test; + friend StringPool_WideString_Test; }; inline WideString operator+(const WideStringView& str1, diff --git a/core/fxcrt/widestring_unittest.cpp b/core/fxcrt/widestring_unittest.cpp index 949827f6f1..5eba72eea7 100644 --- a/core/fxcrt/widestring_unittest.cpp +++ b/core/fxcrt/widestring_unittest.cpp @@ -10,6 +10,8 @@ #include "core/fxcrt/fx_string.h" #include "testing/gtest/include/gtest/gtest.h" +namespace fxcrt { + TEST(WideString, ElementAccess) { const WideString abc(L"abc"); EXPECT_EQ(L'a', abc[0]); @@ -1603,3 +1605,5 @@ TEST(WideStringView, WideOStreamOverload) { EXPECT_EQ(L"abcdef", stream.str()); } } + +} // namespace fxcrt -- cgit v1.2.3