Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: src/inspector/String16.h

Issue 2292573002: [inspector] Initial import of v8_inspector. (Closed)
Patch Set: format the code, disable cpplint Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/inspector/SearchUtil.cpp ('k') | src/inspector/String16.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 class String16 {
20 public:
21 static const size_t kNotFound = static_cast<size_t>(-1);
22
23 String16() {}
24 String16(const String16& other) : m_impl(other.m_impl) {}
25 String16(const UChar* characters, size_t size) : m_impl(characters, size) {}
26 String16(const UChar* characters) : m_impl(characters) {}
27 String16(const char* characters)
28 : String16(characters, std::strlen(characters)) {}
29 String16(const char* characters, size_t size) {
30 m_impl.resize(size);
31 for (size_t i = 0; i < size; ++i) m_impl[i] = characters[i];
32 }
33
34 static String16 fromInteger(int);
35 static String16 fromDouble(double);
36 static String16 fromDoublePrecision3(double);
37 static String16 fromDoublePrecision6(double);
38
39 int toInteger(bool* ok = nullptr) const;
40 String16 stripWhiteSpace() const;
41 const UChar* characters16() const { return m_impl.c_str(); }
42 size_t length() const { return m_impl.length(); }
43 bool isEmpty() const { return !m_impl.length(); }
44 UChar operator[](unsigned index) const { return m_impl[index]; }
45 String16 substring(unsigned pos, unsigned len = UINT_MAX) const {
46 return String16(m_impl.substr(pos, len));
47 }
48 size_t find(const String16& str, unsigned start = 0) const {
49 return m_impl.find(str.m_impl, start);
50 }
51 size_t reverseFind(const String16& str, unsigned start = UINT_MAX) const {
52 return m_impl.rfind(str.m_impl, start);
53 }
54 void swap(String16& other) { m_impl.swap(other.m_impl); }
55
56 // Convenience methods.
57 std::string utf8() const;
58 static String16 fromUTF8(const char* stringStart, size_t length);
59
60 const std::basic_string<UChar>& impl() const { return m_impl; }
61 explicit String16(const std::basic_string<UChar>& impl) : m_impl(impl) {}
62
63 std::size_t hash() const {
64 if (!has_hash) {
65 size_t hash = 0;
66 for (size_t i = 0; i < length(); ++i) hash = 31 * hash + m_impl[i];
67 hash_code = hash;
68 has_hash = true;
69 }
70 return hash_code;
71 }
72
73 private:
74 std::basic_string<UChar> m_impl;
75 mutable bool has_hash = false;
76 mutable std::size_t hash_code = 0;
77 };
78
79 inline bool operator==(const String16& a, const String16& b) {
80 return a.impl() == b.impl();
81 }
82 inline bool operator<(const String16& a, const String16& b) {
83 return a.impl() < b.impl();
84 }
85 inline bool operator!=(const String16& a, const String16& b) {
86 return a.impl() != b.impl();
87 }
88 inline bool operator==(const String16& a, const char* b) {
89 return a.impl() == String16(b).impl();
90 }
91 inline String16 operator+(const String16& a, const char* b) {
92 return String16(a.impl() + String16(b).impl());
93 }
94 inline String16 operator+(const char* a, const String16& b) {
95 return String16(String16(a).impl() + b.impl());
96 }
97 inline String16 operator+(const String16& a, const String16& b) {
98 return String16(a.impl() + b.impl());
99 }
100
101 class String16Builder {
102 public:
103 String16Builder();
104 void append(const String16&);
105 void append(UChar);
106 void append(char);
107 void append(const UChar*, size_t);
108 void append(const char*, size_t);
109 String16 toString();
110 void reserveCapacity(size_t);
111
112 private:
113 std::vector<UChar> m_buffer;
114 };
115
116 } // namespace v8_inspector
117
118 #if !defined(__APPLE__) || defined(_LIBCPP_VERSION)
119
120 namespace std {
121 template <>
122 struct hash<v8_inspector::String16> {
123 std::size_t operator()(const v8_inspector::String16& string) const {
124 return string.hash();
125 }
126 };
127
128 } // namespace std
129
130 #endif // !defined(__APPLE__) || defined(_LIBCPP_VERSION)
131
132 #endif // V8_INSPECTOR_STRING16_H_
OLDNEW
« no previous file with comments | « src/inspector/SearchUtil.cpp ('k') | src/inspector/String16.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698