OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef String16WTF_h | 5 #ifndef String16WTF_h |
6 #define String16WTF_h | 6 #define String16WTF_h |
7 | 7 |
8 #include "platform/Decimal.h" | 8 #include "platform/Decimal.h" |
9 #include "public/platform/WebString.h" | 9 #include "public/platform/WebString.h" |
10 #include "wtf/text/StringBuilder.h" | 10 #include "wtf/text/StringBuilder.h" |
11 #include "wtf/text/StringConcatenate.h" | 11 #include "wtf/text/StringConcatenate.h" |
12 #include "wtf/text/StringHash.h" | 12 #include "wtf/text/StringHash.h" |
13 #include "wtf/text/StringToNumber.h" | 13 #include "wtf/text/StringToNumber.h" |
14 #include "wtf/text/StringView.h" | 14 #include "wtf/text/StringView.h" |
15 #include "wtf/text/WTFString.h" | 15 #include "wtf/text/WTFString.h" |
16 | 16 |
17 namespace blink { | 17 namespace blink { |
18 namespace protocol { | 18 namespace protocol { |
19 | 19 |
20 class PLATFORM_EXPORT String16 { | 20 class PLATFORM_EXPORT String16 : public String16Base<String16, UChar> { |
21 public: | 21 public: |
| 22 static const size_t kNotFound = WTF::kNotFound; |
| 23 |
22 String16() { } | 24 String16() { } |
23 String16(const String16& other); | 25 String16(const String16& other) : m_impl(other.m_impl) { } |
24 String16(const UChar*, unsigned); | 26 String16(const UChar* characters, unsigned length) : m_impl(characters, leng
th) { } |
25 String16(const char*); | 27 String16(const char* characters) : String16(characters, strlen(characters))
{ } |
26 String16(const char*, size_t); | 28 String16(const char* characters, size_t length) |
27 static String16 createUninitialized(unsigned length, UChar*& data); | 29 { |
| 30 UChar* data; |
| 31 m_impl = WTF::String::createUninitialized(length, data); |
| 32 for (size_t i = 0; i < length; ++i) |
| 33 data[i] = characters[i]; |
| 34 } |
| 35 |
| 36 ~String16() { } |
| 37 |
| 38 String16 isolatedCopy() const { return String16(m_impl.isolatedCopy()); } |
| 39 const UChar* characters16() const { return m_impl.isEmpty() ? nullptr : m_im
pl.characters16(); } |
| 40 size_t length() const { return m_impl.length(); } |
| 41 bool isEmpty() const { return m_impl.isEmpty(); } |
| 42 UChar operator[](unsigned index) const { return m_impl[index]; } |
| 43 String16 substring(unsigned pos, unsigned len = UINT_MAX) const { return m_i
mpl.substring(pos, len); } |
| 44 size_t find(const String16& str, unsigned start = 0) const { return m_impl.f
ind(str.impl(), start); } |
| 45 size_t reverseFind(const String16& str, unsigned start = UINT_MAX) const { r
eturn m_impl.reverseFind(str.impl(), start); } |
28 | 46 |
29 // WTF convenience constructors and helper methods. | 47 // WTF convenience constructors and helper methods. |
30 String16(const WebString& other) : String16(String(other)) { } | 48 String16(const WebString& other) : String16(String(other)) { } |
31 template<typename StringType1, typename StringType2> | 49 template<typename StringType1, typename StringType2> |
32 String16(const WTF::StringAppend<StringType1, StringType2>& impl) : String16
(String(impl)) { } | 50 String16(const WTF::StringAppend<StringType1, StringType2>& impl) : String16
(String(impl)) { } |
33 String16(const WTF::AtomicString& impl) : String16(String(impl)) { } | 51 String16(const WTF::AtomicString& impl) : String16(String(impl)) { } |
34 String16(const WTF::String& impl); | 52 String16(const WTF::String& impl) : m_impl(impl) { m_impl.ensure16Bit(); } |
35 String16(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue
) { } | 53 String16(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue
) { } |
36 bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue
(); } | 54 bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue
(); } |
37 operator WTF::String() const { return m_impl; } | 55 operator WTF::String() const { return m_impl; } |
38 operator WTF::StringView() const { return StringView(m_impl); } | 56 operator WTF::StringView() const { return StringView(m_impl); } |
39 operator WebString() { return m_impl; } | 57 operator WebString() { return m_impl; } |
40 const WTF::String& impl() const { return m_impl; } | 58 const WTF::String& impl() const { return m_impl; } |
41 String16 isolatedCopy() const { return String16(m_impl.isolatedCopy()); } | |
42 | |
43 ~String16() { } | |
44 | |
45 static String16 fromInteger(int i) { return String::number(i); } | |
46 static String16 fromDouble(double number) { return Decimal::fromDouble(numbe
r).toString(); } | |
47 static String16 fromDoubleFixedPrecision(double number, int precision) { ret
urn String::numberToStringFixedWidth(number, precision); } | |
48 | |
49 size_t length() const { return m_impl.length(); } | |
50 bool isEmpty() const { return m_impl.isEmpty(); } | |
51 UChar operator[](unsigned index) const { return m_impl[index]; } | |
52 | |
53 size_t charactersSizeInBytes() const { return m_impl.charactersSizeInBytes()
; } | |
54 const UChar* characters16() const { return m_impl.isEmpty() ? nullptr : m_im
pl.characters16(); } | |
55 | |
56 static double charactersToDouble(const LChar* characters, size_t length, boo
l* ok = 0) { return ::charactersToDouble(characters, length, ok); } | |
57 static double charactersToDouble(const UChar* characters, size_t length, boo
l* ok = 0) { return ::charactersToDouble(characters, length, ok); } | |
58 | |
59 String16 substring(unsigned pos, unsigned len = UINT_MAX) const { return m_i
mpl.substring(pos, len); } | |
60 String16 stripWhiteSpace() const { return m_impl.stripWhiteSpace(); } | |
61 | |
62 int toInt(bool* ok = 0) const { return m_impl.toInt(ok); } | |
63 | |
64 size_t find(UChar c, unsigned start = 0) const { return m_impl.find(c, start
); } | |
65 size_t find(const String16& str, unsigned start = 0) const { return m_impl.f
ind(str.impl(), start); } | |
66 size_t reverseFind(const String16& str, unsigned start = UINT_MAX) const { r
eturn m_impl.reverseFind(str.impl(), start); } | |
67 | |
68 bool startWith(const String16& s) const { return m_impl.startsWith(s); } | |
69 bool startWith(UChar character) const { return m_impl.startsWith(character);
} | |
70 bool endsWith(const String16& s) const { return m_impl.endsWith(s); } | |
71 bool endsWith(UChar character) const { return m_impl.endsWith(character); } | |
72 | 59 |
73 private: | 60 private: |
74 WTF::String m_impl; | 61 WTF::String m_impl; |
75 }; | 62 }; |
76 | 63 |
77 class String16Builder { | |
78 public: | |
79 String16Builder() { } | |
80 void append(const String16& str) { m_impl.append(str); }; | |
81 void append(UChar c) { m_impl.append(c); }; | |
82 void append(LChar c) { m_impl.append(c); }; | |
83 void append(char c) { m_impl.append(c); }; | |
84 void append(const UChar* c, size_t size) { m_impl.append(c, size); }; | |
85 void append(const char* characters, unsigned length) { m_impl.append(charact
ers, length); } | |
86 void appendNumber(int number) { m_impl.appendNumber(number); } | |
87 void appendNumber(double number) { m_impl.appendNumber(number); } | |
88 String16 toString() { return m_impl.toString(); } | |
89 void reserveCapacity(unsigned newCapacity) { m_impl.reserveCapacity(newCapac
ity); } | |
90 | |
91 private: | |
92 WTF::StringBuilder m_impl; | |
93 }; | |
94 | |
95 inline bool operator==(const String16& a, const String16& b) { return a.impl() =
= b.impl(); } | 64 inline bool operator==(const String16& a, const String16& b) { return a.impl() =
= b.impl(); } |
96 inline bool operator!=(const String16& a, const String16& b) { return a.impl() !
= b.impl(); } | 65 inline bool operator!=(const String16& a, const String16& b) { return a.impl() !
= b.impl(); } |
97 inline bool operator==(const String16& a, const char* b) { return a.impl() == b;
} | 66 inline bool operator==(const String16& a, const char* b) { return a.impl() == b;
} |
98 | 67 inline String16 operator+(const String16& a, const char* b) { return String16(a.
impl() + b); } |
99 inline String16 operator+(const String16& a, const char* b) | 68 inline String16 operator+(const char* a, const String16& b) { return String16(a
+ b.impl()); } |
100 { | 69 inline String16 operator+(const String16& a, const String16& b) { return String1
6(a.impl() + b.impl()); } |
101 return String(a.impl() + b); | |
102 } | |
103 | |
104 inline String16 operator+(const char* a, const String16& b) | |
105 { | |
106 return String(a + b.impl()); | |
107 } | |
108 | |
109 inline String16 operator+(const String16& a, const String16& b) | |
110 { | |
111 return String(a.impl() + b.impl()); | |
112 } | |
113 | 70 |
114 } // namespace protocol | 71 } // namespace protocol |
115 } // namespace blink | 72 } // namespace blink |
116 | 73 |
117 using String16 = blink::protocol::String16; | 74 namespace std { |
118 using String16Builder = blink::protocol::String16Builder; | 75 template<> struct hash<blink::protocol::String16> { |
| 76 std::size_t operator()(const blink::protocol::String16& string) const |
| 77 { |
| 78 return StringHash::hash(string.impl()); |
| 79 } |
| 80 }; |
| 81 } // namespace std |
| 82 |
| 83 using InspectorProtocolConvenienceStringType = WTF::String; |
| 84 |
| 85 // WTF helpers below this line. |
119 | 86 |
120 namespace WTF { | 87 namespace WTF { |
121 | 88 |
122 struct String16Hash { | 89 struct String16Hash { |
123 static unsigned hash(const String16& key) { return StringHash::hash(key.impl
()); } | 90 static unsigned hash(const blink::protocol::String16& key) { return StringHa
sh::hash(key.impl()); } |
124 static bool equal(const String16& a, const String16& b) | 91 static bool equal(const blink::protocol::String16& a, const blink::protocol:
:String16& b) |
125 { | 92 { |
126 return StringHash::equal(a.impl(), b.impl()); | 93 return StringHash::equal(a.impl(), b.impl()); |
127 } | 94 } |
128 static const bool safeToCompareToEmptyOrDeleted = false; | 95 static const bool safeToCompareToEmptyOrDeleted = false; |
129 }; | 96 }; |
130 | 97 |
131 template<typename T> struct DefaultHash; | 98 template<typename T> struct DefaultHash; |
132 template<> struct DefaultHash<String16> { | 99 template<> struct DefaultHash<blink::protocol::String16> { |
133 typedef String16Hash Hash; | 100 typedef String16Hash Hash; |
134 }; | 101 }; |
135 | 102 |
136 template<> | 103 template<> |
137 struct HashTraits<String16> : SimpleClassHashTraits<String16> { | 104 struct HashTraits<blink::protocol::String16> : SimpleClassHashTraits<blink::prot
ocol::String16> { |
138 static const bool hasIsEmptyValueFunction = true; | 105 static const bool hasIsEmptyValueFunction = true; |
139 static bool isEmptyValue(const String16& a) { return a.impl().isNull(); } | 106 static bool isEmptyValue(const blink::protocol::String16& a) { return a.impl
().isNull(); } |
140 }; | 107 }; |
141 | 108 |
142 } // namespace WTF | 109 } // namespace WTF |
143 | 110 |
144 namespace std { | |
145 template<> struct hash<String16> { | |
146 std::size_t operator()(const String16& string) const | |
147 { | |
148 return StringHash::hash(string.impl()); | |
149 } | |
150 }; | |
151 | |
152 } // namespace std | |
153 | |
154 #endif // !defined(String16WTF_h) | 111 #endif // !defined(String16WTF_h) |
OLD | NEW |