OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium 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 String16_h | |
6 #define String16_h | |
7 | |
8 #include "platform/PlatformExport.h" | |
9 | |
10 #include <cctype> | |
11 #include <climits> | |
12 #include <cstring> | |
13 #include <stdint.h> | |
14 #include <string> | |
15 #include <vector> | |
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) : m_impl(other.m_impl) { } | |
27 String16(const UChar* characters, size_t size) : m_impl(characters, size) {
} | |
28 String16(const UChar* characters) : m_impl(characters) { } | |
29 String16(const char* characters) : String16(characters, std::strlen(characte
rs)) { } | |
30 String16(const char* characters, size_t size) | |
31 { | |
32 m_impl.resize(size); | |
33 for (size_t i = 0; i < size; ++i) | |
34 m_impl[i] = characters[i]; | |
35 } | |
36 | |
37 static String16 fromInteger(int); | |
38 static String16 fromDouble(double); | |
39 static String16 fromDoublePrecision3(double); | |
40 static String16 fromDoublePrecision6(double); | |
41 | |
42 int toInteger(bool* ok = nullptr) const; | |
43 String16 stripWhiteSpace() const; | |
44 const UChar* characters16() const { return m_impl.c_str(); } | |
45 size_t length() const { return m_impl.length(); } | |
46 bool isEmpty() const { return !m_impl.length(); } | |
47 UChar operator[](unsigned index) const { return m_impl[index]; } | |
48 String16 substring(unsigned pos, unsigned len = UINT_MAX) const { return Str
ing16(m_impl.substr(pos, len)); } | |
49 size_t find(const String16& str, unsigned start = 0) const { return m_impl.f
ind(str.m_impl, start); } | |
50 size_t reverseFind(const String16& str, unsigned start = UINT_MAX) const { r
eturn m_impl.rfind(str.m_impl, start); } | |
51 void swap(String16& other) { m_impl.swap(other.m_impl); } | |
52 | |
53 // Convenience methods. | |
54 std::string utf8() const; | |
55 static String16 fromUTF8(const char* stringStart, size_t length); | |
56 | |
57 const std::basic_string<UChar>& impl() const { return m_impl; } | |
58 explicit String16(const std::basic_string<UChar>& impl) : m_impl(impl) { } | |
59 | |
60 std::size_t hash() const | |
61 { | |
62 if (!has_hash) { | |
63 size_t hash = 0; | |
64 for (size_t i = 0; i < length(); ++i) | |
65 hash = 31 * hash + m_impl[i]; | |
66 hash_code = hash; | |
67 has_hash = true; | |
68 } | |
69 return hash_code; | |
70 } | |
71 | |
72 private: | |
73 std::basic_string<UChar> m_impl; | |
74 mutable bool has_hash = false; | |
75 mutable std::size_t hash_code = 0; | |
76 }; | |
77 | |
78 inline bool operator==(const String16& a, const String16& b) { return a.impl() =
= b.impl(); } | |
79 inline bool operator<(const String16& a, const String16& b) { return a.impl() <
b.impl(); } | |
80 inline bool operator!=(const String16& a, const String16& b) { return a.impl() !
= b.impl(); } | |
81 inline bool operator==(const String16& a, const char* b) { return a.impl() == St
ring16(b).impl(); } | |
82 inline String16 operator+(const String16& a, const char* b) { return String16(a.
impl() + String16(b).impl()); } | |
83 inline String16 operator+(const char* a, const String16& b) { return String16(St
ring16(a).impl() + b.impl()); } | |
84 inline String16 operator+(const String16& a, const String16& b) { return String1
6(a.impl() + b.impl()); } | |
85 | |
86 class String16Builder { | |
87 public: | |
88 String16Builder(); | |
89 void append(const String16&); | |
90 void append(UChar); | |
91 void append(char); | |
92 void append(const UChar*, size_t); | |
93 void append(const char*, size_t); | |
94 String16 toString(); | |
95 void reserveCapacity(size_t); | |
96 | |
97 private: | |
98 std::vector<UChar> m_buffer; | |
99 }; | |
100 | |
101 } // namespace v8_inspector | |
102 | |
103 #if !defined(__APPLE__) || defined(_LIBCPP_VERSION) | |
104 | |
105 namespace std { | |
106 template<> struct hash<v8_inspector::String16> { | |
107 std::size_t operator()(const v8_inspector::String16& string) const | |
108 { | |
109 return string.hash(); | |
110 } | |
111 }; | |
112 | |
113 } // namespace std | |
114 | |
115 #endif // !defined(__APPLE__) || defined(_LIBCPP_VERSION) | |
116 | |
117 #endif // !defined(String16_h) | |
OLD | NEW |