Line data Source code
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 <stdint.h>
9 : #include <cctype>
10 : #include <climits>
11 : #include <cstring>
12 : #include <string>
13 : #include <vector>
14 :
15 : namespace v8_inspector {
16 :
17 : using UChar = uint16_t;
18 :
19 80 : class String16 {
20 : public:
21 : static const size_t kNotFound = static_cast<size_t>(-1);
22 :
23 : String16();
24 : String16(const String16& other);
25 : String16(String16&& other);
26 : String16(const UChar* characters, size_t size);
27 : String16(const UChar* characters); // NOLINT(runtime/explicit)
28 : String16(const char* characters); // NOLINT(runtime/explicit)
29 : String16(const char* characters, size_t size);
30 : explicit String16(const std::basic_string<UChar>& impl);
31 :
32 : String16& operator=(const String16& other);
33 : String16& operator=(String16&& other);
34 :
35 : static String16 fromInteger(int);
36 : static String16 fromInteger(size_t);
37 : static String16 fromDouble(double);
38 : static String16 fromDouble(double, int precision);
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 2255 : UChar operator[](size_t index) const { return m_impl[index]; }
46 144957 : String16 substring(size_t pos, size_t len = UINT_MAX) const {
47 289914 : return String16(m_impl.substr(pos, len));
48 : }
49 : size_t find(const String16& str, size_t start = 0) const {
50 140348 : return m_impl.find(str.m_impl, start);
51 : }
52 : size_t reverseFind(const String16& str, size_t start = UINT_MAX) const {
53 264 : return m_impl.rfind(str.m_impl, start);
54 : }
55 9372 : size_t find(UChar c, size_t start = 0) const { return m_impl.find(c, start); }
56 : size_t reverseFind(UChar c, size_t start = UINT_MAX) const {
57 : return m_impl.rfind(c, start);
58 : }
59 : void swap(String16& other) {
60 : m_impl.swap(other.m_impl);
61 : std::swap(hash_code, other.hash_code);
62 : }
63 :
64 : // Convenience methods.
65 : std::string utf8() const;
66 : static String16 fromUTF8(const char* stringStart, size_t length);
67 :
68 194665182 : std::size_t hash() const {
69 194665182 : if (!hash_code) {
70 1077755536 : for (char c : m_impl) hash_code = 31 * hash_code + c;
71 : // Map hash code 0 to 1. This double the number of hash collisions for 1,
72 : // but avoids recomputing the hash code.
73 109294482 : if (!hash_code) ++hash_code;
74 : }
75 194665182 : return hash_code;
76 : }
77 :
78 : inline bool operator==(const String16& other) const {
79 58582798 : return m_impl == other.m_impl;
80 : }
81 : inline bool operator<(const String16& other) const {
82 290 : return m_impl < other.m_impl;
83 : }
84 : inline bool operator!=(const String16& other) const {
85 60 : return m_impl != other.m_impl;
86 : }
87 503122 : inline String16 operator+(const String16& other) const {
88 1509366 : return String16(m_impl + other.m_impl);
89 : }
90 :
91 : // Defined later, since it uses the String16Builder.
92 : template <typename... T>
93 : static String16 concat(T... args);
94 :
95 : private:
96 : std::basic_string<UChar> m_impl;
97 : mutable std::size_t hash_code = 0;
98 : };
99 :
100 126003 : inline String16 operator+(const char* a, const String16& b) {
101 252006 : return String16(a) + b;
102 : }
103 :
104 : class String16Builder {
105 : public:
106 : String16Builder();
107 : void append(const String16&);
108 : void append(UChar);
109 : void append(char);
110 : void append(const UChar*, size_t);
111 : void append(const char*, size_t);
112 : void appendNumber(int);
113 : void appendNumber(size_t);
114 : String16 toString();
115 : void reserveCapacity(size_t);
116 :
117 : template <typename T, typename... R>
118 1775 : void appendAll(T first, R... rest) {
119 2870 : append(first);
120 3550 : appendAll(rest...);
121 1775 : }
122 : void appendAll() {}
123 :
124 : private:
125 : std::vector<UChar> m_buffer;
126 : };
127 :
128 : template <typename... T>
129 846 : String16 String16::concat(T... args) {
130 846 : String16Builder builder;
131 2538 : builder.appendAll(args...);
132 1692 : return builder.toString();
133 : }
134 :
135 : } // namespace v8_inspector
136 :
137 : #if !defined(__APPLE__) || defined(_LIBCPP_VERSION)
138 :
139 : namespace std {
140 : template <>
141 : struct hash<v8_inspector::String16> {
142 : std::size_t operator()(const v8_inspector::String16& string) const {
143 194665182 : return string.hash();
144 : }
145 : };
146 :
147 : } // namespace std
148 :
149 : #endif // !defined(__APPLE__) || defined(_LIBCPP_VERSION)
150 :
151 : #endif // V8_INSPECTOR_STRING16_H_
|