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 String16STL_h | |
6 #define String16STL_h | |
7 | |
8 #include <cctype> | |
9 #include <cstdlib> | |
10 #include <cstring> | |
11 #include <stdint.h> | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 using UChar = uint16_t; | |
16 | |
17 namespace blink { | |
18 namespace protocol { | |
19 | |
20 class String16 : public String16Base<String16, UChar> { | |
21 public: | |
22 static const size_t kNotFound = static_cast<size_t>(-1); | |
23 | |
24 String16() { } | |
25 String16(const String16& other) : m_impl(other.m_impl) { } | |
26 String16(const UChar* characters, size_t size) : m_impl(characters, size) {
} | |
27 String16(const UChar* characters) : m_impl(characters) { } | |
28 String16(const char* characters) : String16(characters, std::strlen(characte
rs)) { } | |
29 String16(const char* characters, size_t size) | |
30 { | |
31 m_impl.resize(size); | |
32 for (size_t i = 0; i < size; ++i) | |
33 m_impl[i] = characters[i]; | |
34 } | |
35 | |
36 String16 isolatedCopy() const { return String16(m_impl); } | |
37 const UChar* characters16() const { return m_impl.c_str(); } | |
38 size_t length() const { return m_impl.length(); } | |
39 bool isEmpty() const { return !m_impl.length(); } | |
40 UChar operator[](unsigned index) const { return m_impl[index]; } | |
41 String16 substring(unsigned pos, unsigned len = 0xFFFFFFFF) const { return S
tring16(m_impl.substr(pos, len)); } | |
42 size_t find(const String16& str, unsigned start = 0) const { return m_impl.f
ind(str.m_impl, start); } | |
43 size_t reverseFind(const String16& str, unsigned start = 0xFFFFFFFF) const {
return m_impl.rfind(str.m_impl, start); } | |
44 | |
45 // Convenience methods. | |
46 std::string utf8() const; | |
47 static String16 fromUTF8(const char* stringStart, size_t length); | |
48 | |
49 const std::basic_string<UChar>& impl() const { return m_impl; } | |
50 explicit String16(const std::basic_string<UChar>& impl) : m_impl(impl) { } | |
51 | |
52 std::size_t hash() const | |
53 { | |
54 if (!has_hash) { | |
55 size_t hash = 0; | |
56 for (size_t i = 0; i < length(); ++i) | |
57 hash = 31 * hash + m_impl[i]; | |
58 hash_code = hash; | |
59 has_hash = true; | |
60 } | |
61 return hash_code; | |
62 } | |
63 | |
64 private: | |
65 std::basic_string<UChar> m_impl; | |
66 mutable bool has_hash = false; | |
67 mutable std::size_t hash_code = 0; | |
68 }; | |
69 | |
70 inline bool operator==(const String16& a, const String16& b) { return a.impl() =
= b.impl(); } | |
71 inline bool operator!=(const String16& a, const String16& b) { return a.impl() !
= b.impl(); } | |
72 inline bool operator==(const String16& a, const char* b) { return a.impl() == St
ring16(b).impl(); } | |
73 inline String16 operator+(const String16& a, const char* b) { return String16(a.
impl() + String16(b).impl()); } | |
74 inline String16 operator+(const char* a, const String16& b) { return String16(St
ring16(a).impl() + b.impl()); } | |
75 inline String16 operator+(const String16& a, const String16& b) { return String1
6(a.impl() + b.impl()); } | |
76 | |
77 } // namespace protocol | |
78 } // namespace blink | |
79 | |
80 #if !defined(__APPLE__) || defined(_LIBCPP_VERSION) | |
81 | |
82 namespace std { | |
83 template<> struct hash<blink::protocol::String16> { | |
84 std::size_t operator()(const blink::protocol::String16& string) const | |
85 { | |
86 return string.hash(); | |
87 } | |
88 }; | |
89 | |
90 } // namespace std | |
91 | |
92 #endif // !defined(__APPLE__) || defined(_LIBCPP_VERSION) | |
93 | |
94 class InspectorProtocolConvenienceStringType { | |
95 public: | |
96 // This class should not be ever instantiated, so we don't implement constru
ctors. | |
97 InspectorProtocolConvenienceStringType(); | |
98 InspectorProtocolConvenienceStringType(const blink::protocol::String16& othe
r); | |
99 operator blink::protocol::String16() const { return blink::protocol::String1
6(); }; | |
100 }; | |
101 | |
102 #endif // !defined(String16STL_h) | |
OLD | NEW |