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_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 389700409 : class String16 {
22 : public:
23 : static const size_t kNotFound = static_cast<size_t>(-1);
24 :
25 45555078 : String16() = default;
26 215489567 : String16(const String16&) V8_NOEXCEPT = default;
27 42381128 : String16(String16&&) V8_NOEXCEPT = default;
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&) V8_NOEXCEPT = default;
35 : String16& operator=(String16&&) V8_NOEXCEPT = default;
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;
45 : const UChar* characters16() const { return m_impl.c_str(); }
46 : size_t length() const { return m_impl.length(); }
47 : bool isEmpty() const { return !m_impl.length(); }
48 3340 : UChar operator[](size_t index) const { return m_impl[index]; }
49 160411 : String16 substring(size_t pos, size_t len = UINT_MAX) const {
50 320822 : return String16(m_impl.substr(pos, len));
51 : }
52 : size_t find(const String16& str, size_t start = 0) const {
53 154955 : return m_impl.find(str.m_impl, start);
54 : }
55 : size_t reverseFind(const String16& str, size_t start = UINT_MAX) const {
56 372 : 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 : }
62 : void swap(String16& other) {
63 330125 : 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 :
71 : std::size_t hash() const {
72 106832410 : if (!hash_code) {
73 332486847 : 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 36882417 : if (!hash_code) ++hash_code;
77 : }
78 106832410 : return hash_code;
79 : }
80 :
81 : inline bool operator==(const String16& other) const {
82 37680676 : return m_impl == other.m_impl;
83 : }
84 : inline bool operator<(const String16& other) const {
85 605 : return m_impl < other.m_impl;
86 : }
87 : inline bool operator!=(const String16& other) const {
88 0 : return m_impl != other.m_impl;
89 : }
90 592868 : inline String16 operator+(const String16& other) const {
91 1185736 : 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 148709 : inline String16 operator+(const char* a, const String16& b) {
104 297418 : return String16(a) + b;
105 : }
106 :
107 14054457 : 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>
123 11244449 : void appendAll(T first, R... rest) {
124 22482770 : append(first);
125 19622387 : appendAll(rest...);
126 11244449 : }
127 : void appendAll() {}
128 :
129 : private:
130 : std::vector<UChar> m_buffer;
131 : };
132 :
133 : template <typename... T>
134 2871982 : String16 String16::concat(T... args) {
135 2871982 : String16Builder builder;
136 8685504 : builder.appendAll(args...);
137 5743964 : 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_
|