OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CompressibleString_h | |
6 #define CompressibleString_h | |
7 | |
8 #include "platform/PlatformExport.h" | |
9 #include "wtf/RefCounted.h" | |
10 #include "wtf/text/Unicode.h" | |
11 #include "wtf/text/WTFString.h" | |
12 | |
13 namespace blink { | |
14 | |
15 // TODO(hajimehoshi): Now these classes are in platform/text to use UMA. Move | |
16 // them to wtf/text. | |
17 | |
18 class PLATFORM_EXPORT CompressibleStringImpl final : public RefCounted<Compressi
bleStringImpl> { | |
19 WTF_MAKE_NONCOPYABLE(CompressibleStringImpl); | |
20 public: | |
21 static void compressAll(); | |
22 | |
23 CompressibleStringImpl() | |
24 : m_string() | |
25 , m_isCompressed(false) | |
26 { | |
27 } | |
28 | |
29 explicit CompressibleStringImpl(PassRefPtr<StringImpl>); | |
30 ~CompressibleStringImpl(); | |
31 | |
32 bool isEmpty() const { return originalLength() == 0; } | |
33 | |
34 bool isCompressed() const { return m_isCompressed; } | |
35 unsigned originalLength() const { return m_string.length(); } | |
36 bool is8Bit() const { return m_string.is8Bit(); } | |
37 | |
38 unsigned originalContentSizeInBytes() const | |
39 { | |
40 if (is8Bit()) | |
41 return originalLength() * sizeof(LChar); | |
42 return originalLength() * sizeof(UChar); | |
43 } | |
44 | |
45 // TODO(hajimehoshi): Update this once we implement compression. | |
46 unsigned currentSizeInBytes() const | |
47 { | |
48 return originalContentSizeInBytes(); | |
49 } | |
50 | |
51 const String& toString() | |
52 { | |
53 if (UNLIKELY(isCompressed())) | |
54 decompressString(); | |
55 return m_string; | |
56 } | |
57 | |
58 const LChar* characters8() | |
59 { | |
60 return toString().characters8(); | |
61 } | |
62 | |
63 const UChar* characters16() | |
64 { | |
65 return toString().characters16(); | |
66 } | |
67 | |
68 void compressString(); | |
69 void decompressString(); | |
70 | |
71 private: | |
72 String m_string; | |
73 bool m_isCompressed; | |
74 }; | |
75 | |
76 class PLATFORM_EXPORT CompressibleString final { | |
77 public: | |
78 CompressibleString() | |
79 : m_impl(nullptr) | |
80 { | |
81 } | |
82 | |
83 CompressibleString(const CompressibleString& rhs) | |
84 : m_impl(rhs.m_impl) | |
85 { | |
86 } | |
87 | |
88 explicit CompressibleString(PassRefPtr<StringImpl> impl) | |
89 : m_impl(impl ? adoptRef(new CompressibleStringImpl(impl)) : nullptr) | |
90 { | |
91 } | |
92 | |
93 bool isNull() const { return !m_impl; } | |
94 bool isEmpty() const { return isNull() || m_impl->isEmpty(); } | |
95 unsigned length() const { return m_impl ? m_impl->originalLength() : 0; } | |
96 | |
97 unsigned currentSizeInBytes() const { return m_impl ? m_impl->currentSizeInB
ytes() : 0; } | |
98 | |
99 bool isCompressed() const { return m_impl ? m_impl->isCompressed() : false;
} | |
100 bool is8Bit() const { return m_impl ? m_impl->is8Bit() : false; } | |
101 | |
102 const String& toString() const { return m_impl ? m_impl->toString() : emptyS
tring(); } | |
103 const LChar* characters8() const { return m_impl ? m_impl->characters8() : n
ullptr; } | |
104 const UChar* characters16() const { return m_impl ? m_impl->characters16() :
nullptr; } | |
105 | |
106 CompressibleStringImpl* impl() const { return m_impl.get(); } | |
107 | |
108 private: | |
109 void compressString() const { m_impl->compressString(); } | |
110 void decompressString() const { m_impl->decompressString(); } | |
111 | |
112 mutable RefPtr<CompressibleStringImpl> m_impl; | |
113 }; | |
114 | |
115 } // namespace blink | |
116 | |
117 using blink::CompressibleString; | |
118 using blink::CompressibleStringImpl; | |
119 | |
120 #endif | |
OLD | NEW |