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_STRING_16_H_
6 #define V8_INSPECTOR_STRING_16_H_
7
8 #include <stdint.h>
9
10 #include <cctype>
11 #include <climits>
12 #include <cstring>
13 #include <string>
14 #include <vector>
15
16 #include "src/base/compiler-specific.h"
17
18 namespace v8_inspector {
19
20 using UChar = uint16_t;
21
22 class String16 {
23 public:
24 static const size_t kNotFound = static_cast<size_t>(-1);
25
26 String16() = default;
27 String16(const String16&) V8_NOEXCEPT = default;
28 String16(String16&&) V8_NOEXCEPT = default;
29 String16(const UChar* characters, size_t size);
30 V8_EXPORT String16(const UChar* characters);
31 V8_EXPORT String16(const char* characters);
32 String16(const char* characters, size_t size);
33 explicit String16(const std::basic_string<UChar>& impl);
34 explicit String16(std::basic_string<UChar>&& impl);
35
36 String16& operator=(const String16&) V8_NOEXCEPT = default;
37 String16& operator=(String16&&) V8_NOEXCEPT = default;
38
39 static String16 fromInteger(int);
40 static String16 fromInteger(size_t);
41 static String16 fromInteger64(int64_t);
42 static String16 fromUInt64(uint64_t);
43 static String16 fromDouble(double);
44 static String16 fromDouble(double, int precision);
45
46 int64_t toInteger64(bool* ok = nullptr) const;
47 uint64_t toUInt64(bool* ok = nullptr) const;
48 int toInteger(bool* ok = nullptr) const;
49 String16 stripWhiteSpace() const;
characters16()50 const UChar* characters16() const { return m_impl.c_str(); }
length()51 size_t length() const { return m_impl.length(); }
isEmpty()52 bool isEmpty() const { return !m_impl.length(); }
53 UChar operator[](size_t index) const { return m_impl[index]; }
54 String16 substring(size_t pos, size_t len = UINT_MAX) const {
55 return String16(m_impl.substr(pos, len));
56 }
57 size_t find(const String16& str, size_t start = 0) const {
58 return m_impl.find(str.m_impl, start);
59 }
60 size_t reverseFind(const String16& str, size_t start = UINT_MAX) const {
61 return m_impl.rfind(str.m_impl, start);
62 }
63 size_t find(UChar c, size_t start = 0) const { return m_impl.find(c, start); }
64 size_t reverseFind(UChar c, size_t start = UINT_MAX) const {
65 return m_impl.rfind(c, start);
66 }
swap(String16 & other)67 void swap(String16& other) {
68 m_impl.swap(other.m_impl);
69 std::swap(hash_code, other.hash_code);
70 }
71
72 // Convenience methods.
73 V8_EXPORT std::string utf8() const;
74 V8_EXPORT static String16 fromUTF8(const char* stringStart, size_t length);
75
76 // Instantiates a String16 in native endianness from UTF16 LE.
77 // On Big endian architectures, byte order needs to be flipped.
78 V8_EXPORT static String16 fromUTF16LE(const UChar* stringStart,
79 size_t length);
80
hash()81 std::size_t hash() const {
82 if (!hash_code) {
83 for (char c : m_impl) hash_code = 31 * hash_code + c;
84 // Map hash code 0 to 1. This double the number of hash collisions for 1,
85 // but avoids recomputing the hash code.
86 if (!hash_code) ++hash_code;
87 }
88 return hash_code;
89 }
90
91 inline bool operator==(const String16& other) const {
92 return m_impl == other.m_impl;
93 }
94 inline bool operator<(const String16& other) const {
95 return m_impl < other.m_impl;
96 }
97 inline bool operator!=(const String16& other) const {
98 return m_impl != other.m_impl;
99 }
100 inline String16 operator+(const String16& other) const {
101 return String16(m_impl + other.m_impl);
102 }
103 inline String16& operator+=(const String16& other) {
104 m_impl += other.m_impl;
105 return *this;
106 }
107
108 // Defined later, since it uses the String16Builder.
109 template <typename... T>
110 static String16 concat(T... args);
111
112 private:
113 std::basic_string<UChar> m_impl;
114 mutable std::size_t hash_code = 0;
115 };
116
117 inline String16 operator+(const char* a, const String16& b) {
118 return String16(a) + b;
119 }
120
121 class String16Builder {
122 public:
123 String16Builder();
124 void append(const String16&);
125 void append(UChar);
126 void append(char);
127 void append(const UChar*, size_t);
128 void append(const char*, size_t);
129 void appendNumber(int);
130 void appendNumber(size_t);
131 void appendUnsignedAsHex(uint64_t);
132 void appendUnsignedAsHex(uint32_t);
133 void appendUnsignedAsHex(uint8_t);
134 String16 toString();
135 void reserveCapacity(size_t);
136
137 template <typename T, typename... R>
appendAll(T first,R...rest)138 void appendAll(T first, R... rest) {
139 append(first);
140 appendAll(rest...);
141 }
appendAll()142 void appendAll() {}
143
144 private:
145 std::vector<UChar> m_buffer;
146 };
147
148 template <typename... T>
concat(T...args)149 String16 String16::concat(T... args) {
150 String16Builder builder;
151 builder.appendAll(args...);
152 return builder.toString();
153 }
154
155 } // namespace v8_inspector
156
157 #if !defined(__APPLE__) || defined(_LIBCPP_VERSION)
158
159 namespace std {
160 template <>
161 struct hash<v8_inspector::String16> {
162 std::size_t operator()(const v8_inspector::String16& string) const {
163 return string.hash();
164 }
165 };
166
167 } // namespace std
168
169 #endif // !defined(__APPLE__) || defined(_LIBCPP_VERSION)
170
171 #endif // V8_INSPECTOR_STRING_16_H_
172