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 void swap(String16& other) { m_impl.swap(other.m_impl); } | |
46 | |
47 // Convenience methods. | |
48 std::string utf8() const; | |
49 static String16 fromUTF8(const char* stringStart, size_t length); | |
50 | |
51 const std::basic_string<UChar>& impl() const { return m_impl; } | |
52 explicit String16(const std::basic_string<UChar>& impl) : m_impl(impl) { } | |
53 | |
54 std::size_t hash() const | |
55 { | |
56 if (!has_hash) { | |
57 size_t hash = 0; | |
58 for (size_t i = 0; i < length(); ++i) | |
59 hash = 31 * hash + m_impl[i]; | |
60 hash_code = hash; | |
61 has_hash = true; | |
62 } | |
63 return hash_code; | |
64 } | |
65 | |
66 private: | |
67 std::basic_string<UChar> m_impl; | |
68 mutable bool has_hash = false; | |
69 mutable std::size_t hash_code = 0; | |
70 }; | |
71 | |
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 String16& b) { return a.impl() !
= b.impl(); } | |
75 inline bool operator==(const String16& a, const char* b) { return a.impl() == St
ring16(b).impl(); } | |
76 inline String16 operator+(const String16& a, const char* b) { return String16(a.
impl() + String16(b).impl()); } | |
77 inline String16 operator+(const char* a, const String16& b) { return String16(St
ring16(a).impl() + b.impl()); } | |
78 inline String16 operator+(const String16& a, const String16& b) { return String1
6(a.impl() + b.impl()); } | |
79 | |
80 } // namespace protocol | |
81 } // namespace blink | |
82 | |
83 #if !defined(__APPLE__) || defined(_LIBCPP_VERSION) | |
84 | |
85 namespace std { | |
86 template<> struct hash<blink::protocol::String16> { | |
87 std::size_t operator()(const blink::protocol::String16& string) const | |
88 { | |
89 return string.hash(); | |
90 } | |
91 }; | |
92 | |
93 } // namespace std | |
94 | |
95 #endif // !defined(__APPLE__) || defined(_LIBCPP_VERSION) | |
96 | |
97 class InspectorProtocolConvenienceStringType { | |
98 public: | |
99 // This class should not be ever instantiated, so we don't implement constru
ctors. | |
100 InspectorProtocolConvenienceStringType(); | |
101 InspectorProtocolConvenienceStringType(const blink::protocol::String16& othe
r); | |
102 operator blink::protocol::String16() const { return blink::protocol::String1
6(); }; | |
103 }; | |
104 | |
105 #endif // !defined(String16STL_h) | |
OLD | NEW |