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 { |
21 public: | 21 public: |
22 String16() { } | 22 String16() { } |
23 String16(const String16& other); | 23 String16(const String16& other); |
24 String16(const UChar*, unsigned); | 24 String16(const UChar*, unsigned); |
25 String16(const char*); | 25 String16(const char*); |
26 String16(const char*, size_t); | 26 String16(const char*, size_t); |
27 static String16 createUninitialized(unsigned length, UChar*& data); | 27 ~String16() { } |
| 28 |
| 29 String16 isolatedCopy() const { return String16(m_impl.isolatedCopy()); } |
| 30 const UChar* characters16() const { return m_impl.isEmpty() ? nullptr : m_im
pl.characters16(); } |
| 31 size_t length() const { return m_impl.length(); } |
| 32 bool isEmpty() const { return m_impl.isEmpty(); } |
| 33 UChar operator[](unsigned index) const { return m_impl[index]; } |
| 34 String16 substring(unsigned pos, unsigned len = UINT_MAX) const { return m_i
mpl.substring(pos, len); } |
| 35 size_t find(const String16& str, unsigned start = 0) const { return m_impl.f
ind(str.impl(), start); } |
| 36 size_t reverseFind(const String16& str, unsigned start = UINT_MAX) const { r
eturn m_impl.reverseFind(str.impl(), start); } |
28 | 37 |
29 // WTF convenience constructors and helper methods. | 38 // WTF convenience constructors and helper methods. |
30 String16(const WebString& other) : String16(String(other)) { } | 39 String16(const WebString& other) : String16(String(other)) { } |
31 template<typename StringType1, typename StringType2> | 40 template<typename StringType1, typename StringType2> |
32 String16(const WTF::StringAppend<StringType1, StringType2>& impl) : String16
(String(impl)) { } | 41 String16(const WTF::StringAppend<StringType1, StringType2>& impl) : String16
(String(impl)) { } |
33 String16(const WTF::AtomicString& impl) : String16(String(impl)) { } | 42 String16(const WTF::AtomicString& impl) : String16(String(impl)) { } |
34 String16(const WTF::String& impl); | 43 String16(const WTF::String& impl); |
35 String16(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue
) { } | 44 String16(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue
) { } |
36 bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue
(); } | 45 bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue
(); } |
37 operator WTF::String() const { return m_impl; } | 46 operator WTF::String() const { return m_impl; } |
38 operator WTF::StringView() const { return StringView(m_impl); } | 47 operator WTF::StringView() const { return StringView(m_impl); } |
39 operator WebString() { return m_impl; } | 48 operator WebString() { return m_impl; } |
40 const WTF::String& impl() const { return m_impl; } | 49 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 unsigned sizeInBytes() const { return m_impl.sizeInBytes(); } | |
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 | 50 |
73 private: | 51 private: |
74 WTF::String m_impl; | 52 WTF::String m_impl; |
75 }; | 53 }; |
76 | 54 |
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(); } | 55 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(); } | 56 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;
} | 57 inline bool operator==(const String16& a, const char* b) { return a.impl() == b;
} |
98 | 58 |
99 inline String16 operator+(const String16& a, const char* b) | 59 inline String16 operator+(const String16& a, const char* b) |
100 { | 60 { |
101 return String(a.impl() + b); | 61 return String(a.impl() + b); |
102 } | 62 } |
103 | 63 |
104 inline String16 operator+(const char* a, const String16& b) | 64 inline String16 operator+(const char* a, const String16& b) |
105 { | 65 { |
106 return String(a + b.impl()); | 66 return String(a + b.impl()); |
107 } | 67 } |
108 | 68 |
109 inline String16 operator+(const String16& a, const String16& b) | 69 inline String16 operator+(const String16& a, const String16& b) |
110 { | 70 { |
111 return String(a.impl() + b.impl()); | 71 return String(a.impl() + b.impl()); |
112 } | 72 } |
113 | 73 |
114 } // namespace protocol | 74 } // namespace protocol |
115 } // namespace blink | 75 } // namespace blink |
116 | 76 |
117 using String16 = blink::protocol::String16; | 77 namespace std { |
118 using String16Builder = blink::protocol::String16Builder; | 78 template<> struct hash<blink::protocol::String16> { |
| 79 std::size_t operator()(const blink::protocol::String16& string) const |
| 80 { |
| 81 return StringHash::hash(string.impl()); |
| 82 } |
| 83 }; |
| 84 } // namespace std |
| 85 |
| 86 using InspectorProtocolConvenienceStringType = WTF::String; |
| 87 |
| 88 // WTF helpers below this line. |
119 | 89 |
120 namespace WTF { | 90 namespace WTF { |
121 | 91 |
122 struct String16Hash { | 92 struct String16Hash { |
123 static unsigned hash(const String16& key) { return StringHash::hash(key.impl
()); } | 93 static unsigned hash(const blink::protocol::String16& key) { return StringHa
sh::hash(key.impl()); } |
124 static bool equal(const String16& a, const String16& b) | 94 static bool equal(const blink::protocol::String16& a, const blink::protocol:
:String16& b) |
125 { | 95 { |
126 return StringHash::equal(a.impl(), b.impl()); | 96 return StringHash::equal(a.impl(), b.impl()); |
127 } | 97 } |
128 static const bool safeToCompareToEmptyOrDeleted = false; | 98 static const bool safeToCompareToEmptyOrDeleted = false; |
129 }; | 99 }; |
130 | 100 |
131 template<typename T> struct DefaultHash; | 101 template<typename T> struct DefaultHash; |
132 template<> struct DefaultHash<String16> { | 102 template<> struct DefaultHash<blink::protocol::String16> { |
133 typedef String16Hash Hash; | 103 typedef String16Hash Hash; |
134 }; | 104 }; |
135 | 105 |
136 template<> | 106 template<> |
137 struct HashTraits<String16> : SimpleClassHashTraits<String16> { | 107 struct HashTraits<blink::protocol::String16> : SimpleClassHashTraits<blink::prot
ocol::String16> { |
138 static const bool hasIsEmptyValueFunction = true; | 108 static const bool hasIsEmptyValueFunction = true; |
139 static bool isEmptyValue(const String16& a) { return a.impl().isNull(); } | 109 static bool isEmptyValue(const blink::protocol::String16& a) { return a.impl
().isNull(); } |
140 }; | 110 }; |
141 | 111 |
142 } // namespace WTF | 112 } // namespace WTF |
143 | 113 |
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) | 114 #endif // !defined(String16WTF_h) |
OLD | NEW |