OLD | NEW |
| (Empty) |
1 // Copyright 2016 the V8 project 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 V8_INSPECTOR_STRING16_H_ | |
6 #define V8_INSPECTOR_STRING16_H_ | |
7 | |
8 #include <cctype> | |
9 #include <climits> | |
10 #include <cstring> | |
11 #include <stdint.h> | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 namespace v8_inspector { | |
16 | |
17 using UChar = uint16_t; | |
18 | |
19 class String16 { | |
20 public: | |
21 static const size_t kNotFound = static_cast<size_t>(-1); | |
22 | |
23 String16() { } | |
24 String16(const String16& other) : m_impl(other.m_impl) { } | |
25 String16(const UChar* characters, size_t size) : m_impl(characters, size) {
} | |
26 String16(const UChar* characters) : m_impl(characters) { } | |
27 String16(const char* characters) : String16(characters, std::strlen(characte
rs)) { } | |
28 String16(const char* characters, size_t size) | |
29 { | |
30 m_impl.resize(size); | |
31 for (size_t i = 0; i < size; ++i) | |
32 m_impl[i] = characters[i]; | |
33 } | |
34 | |
35 static String16 fromInteger(int); | |
36 static String16 fromDouble(double); | |
37 static String16 fromDoublePrecision3(double); | |
38 static String16 fromDoublePrecision6(double); | |
39 | |
40 int toInteger(bool* ok = nullptr) const; | |
41 String16 stripWhiteSpace() const; | |
42 const UChar* characters16() const { return m_impl.c_str(); } | |
43 size_t length() const { return m_impl.length(); } | |
44 bool isEmpty() const { return !m_impl.length(); } | |
45 UChar operator[](unsigned index) const { return m_impl[index]; } | |
46 String16 substring(unsigned pos, unsigned len = UINT_MAX) const { return Str
ing16(m_impl.substr(pos, len)); } | |
47 size_t find(const String16& str, unsigned start = 0) const { return m_impl.f
ind(str.m_impl, start); } | |
48 size_t reverseFind(const String16& str, unsigned start = UINT_MAX) const { r
eturn m_impl.rfind(str.m_impl, start); } | |
49 void swap(String16& other) { m_impl.swap(other.m_impl); } | |
50 | |
51 // Convenience methods. | |
52 std::string utf8() const; | |
53 static String16 fromUTF8(const char* stringStart, size_t length); | |
54 | |
55 const std::basic_string<UChar>& impl() const { return m_impl; } | |
56 explicit String16(const std::basic_string<UChar>& impl) : m_impl(impl) { } | |
57 | |
58 std::size_t hash() const | |
59 { | |
60 if (!has_hash) { | |
61 size_t hash = 0; | |
62 for (size_t i = 0; i < length(); ++i) | |
63 hash = 31 * hash + m_impl[i]; | |
64 hash_code = hash; | |
65 has_hash = true; | |
66 } | |
67 return hash_code; | |
68 } | |
69 | |
70 private: | |
71 std::basic_string<UChar> m_impl; | |
72 mutable bool has_hash = false; | |
73 mutable std::size_t hash_code = 0; | |
74 }; | |
75 | |
76 inline bool operator==(const String16& a, const String16& b) { return a.impl() =
= b.impl(); } | |
77 inline bool operator<(const String16& a, const String16& b) { return a.impl() <
b.impl(); } | |
78 inline bool operator!=(const String16& a, const String16& b) { return a.impl() !
= b.impl(); } | |
79 inline bool operator==(const String16& a, const char* b) { return a.impl() == St
ring16(b).impl(); } | |
80 inline String16 operator+(const String16& a, const char* b) { return String16(a.
impl() + String16(b).impl()); } | |
81 inline String16 operator+(const char* a, const String16& b) { return String16(St
ring16(a).impl() + b.impl()); } | |
82 inline String16 operator+(const String16& a, const String16& b) { return String1
6(a.impl() + b.impl()); } | |
83 | |
84 class String16Builder { | |
85 public: | |
86 String16Builder(); | |
87 void append(const String16&); | |
88 void append(UChar); | |
89 void append(char); | |
90 void append(const UChar*, size_t); | |
91 void append(const char*, size_t); | |
92 String16 toString(); | |
93 void reserveCapacity(size_t); | |
94 | |
95 private: | |
96 std::vector<UChar> m_buffer; | |
97 }; | |
98 | |
99 } // namespace v8_inspector | |
100 | |
101 #if !defined(__APPLE__) || defined(_LIBCPP_VERSION) | |
102 | |
103 namespace std { | |
104 template<> struct hash<v8_inspector::String16> { | |
105 std::size_t operator()(const v8_inspector::String16& string) const | |
106 { | |
107 return string.hash(); | |
108 } | |
109 }; | |
110 | |
111 } // namespace std | |
112 | |
113 #endif // !defined(__APPLE__) || defined(_LIBCPP_VERSION) | |
114 | |
115 #endif // V8_INSPECTOR_STRING16_H_ | |
OLD | NEW |