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