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