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