OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 // WideStringPiece is similar to StringPiece (in base/string_piece.h) but for | |
6 // base::string16 instead of std::string. We do not define as large of a subset | |
7 // of the STL functions from basic_string as in StringPiece, but this can be | |
8 // changed if these functions (find, find_first_of, etc.) are found to be | |
9 // useful in this context. | |
10 // | |
11 | |
12 #ifndef BASE_WIDE_STRING_PIECE_H_ | |
13 #define BASE_WIDE_STRING_PIECE_H_ | |
14 #pragma once | |
15 | |
16 #include "base/base_api.h" | |
17 #include "base/hash_tables.h" | |
18 #include "base/string16.h" | |
19 | |
20 namespace base { | |
21 | |
22 class BASE_API WideStringPiece { | |
brettw
2011/08/03 20:47:38
This should be called StringPiece16. "Wide" is wha
Garrett Casto
2011/08/04 00:03:51
Done.
| |
23 public: | |
24 // standard STL container boilerplate | |
25 typedef size_t size_type; | |
26 typedef char16 value_type; | |
27 typedef const char16* pointer; | |
28 typedef const char16& reference; | |
29 typedef const char16& const_reference; | |
30 typedef ptrdiff_t difference_type; | |
31 typedef const char16* const_iterator; | |
32 typedef const char16* iterator; | |
33 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | |
34 typedef std::reverse_iterator<iterator> reverse_iterator; | |
35 | |
36 public: | |
37 // We provide non-explicit singleton constructors so users can pass | |
38 // in a "const char16*" or a "string16" wherever a "WideStringPiece" is | |
39 // expected. | |
40 WideStringPiece() : ptr_(NULL), length_(0) { } | |
41 WideStringPiece(const char16* str) | |
42 : ptr_(str), | |
43 length_((str == NULL) ? 0 : string16::traits_type::length(str)) { } | |
44 WideStringPiece(const string16& str) | |
45 : ptr_(str.data()), length_(str.size()) { } | |
46 WideStringPiece(const char16* offset, size_type len) | |
47 : ptr_(offset), length_(len) { } | |
48 | |
49 // data() may return a pointer to a buffer with embedded NULs, and the | |
50 // returned buffer may or may not be null terminated. Therefore it is | |
51 // typically a mistake to pass data() to a routine that expects a NUL | |
52 // terminated string. | |
53 const char16* data() const { return ptr_; } | |
54 size_type size() const { return length_; } | |
55 size_type length() const { return length_; } | |
56 bool empty() const { return length_ == 0; } | |
57 | |
58 void clear() { | |
59 ptr_ = NULL; | |
60 length_ = 0; | |
61 } | |
62 void set(const char16* data, size_type len) { | |
63 ptr_ = data; | |
64 length_ = len; | |
65 } | |
66 void set(const char16* str) { | |
67 ptr_ = str; | |
68 length_ = str ? string16::traits_type::length(str) : 0; | |
69 } | |
70 void set(const void* data, size_type len) { | |
71 ptr_ = reinterpret_cast<const char16*>(data); | |
72 length_ = len; | |
73 } | |
74 | |
75 char16 operator[](size_type i) const { return ptr_[i]; } | |
76 | |
77 string16 as_string16() const { | |
78 // StringPiece claims that this is bad when data() is NULL, but unittesting | |
79 // seems to say otherwise. | |
80 return string16(data(), size()); | |
81 } | |
82 | |
83 iterator begin() const { return ptr_; } | |
84 iterator end() const { return ptr_ + length_; } | |
85 const_reverse_iterator rbegin() const { | |
86 return const_reverse_iterator(ptr_ + length_); | |
87 } | |
88 const_reverse_iterator rend() const { | |
89 return const_reverse_iterator(ptr_); | |
90 } | |
91 | |
92 size_type max_size() const { return length_; } | |
93 size_type capacity() const { return length_; } | |
94 | |
95 static int wordmemcmp(const char16* p, const char16* p2, size_type N) { | |
96 return string16::traits_type::compare(p, p2, N); | |
97 } | |
98 | |
99 private: | |
100 const char16* ptr_; | |
101 size_type length_; | |
102 }; | |
103 | |
104 inline bool operator==(const WideStringPiece& x, const WideStringPiece& y) { | |
105 if (x.size() != y.size()) | |
106 return false; | |
107 | |
108 return WideStringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0; | |
109 } | |
110 | |
111 inline bool operator!=(const WideStringPiece& x, const WideStringPiece& y) { | |
112 return !(x == y); | |
113 } | |
114 | |
115 inline bool operator<(const WideStringPiece& x, const WideStringPiece& y) { | |
116 const int r = WideStringPiece::wordmemcmp( | |
117 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); | |
118 return ((r < 0) || ((r == 0) && (x.size() < y.size()))); | |
119 } | |
120 | |
121 inline bool operator>(const WideStringPiece& x, const WideStringPiece& y) { | |
122 return y < x; | |
123 } | |
124 | |
125 inline bool operator<=(const WideStringPiece& x, const WideStringPiece& y) { | |
126 return !(x > y); | |
127 } | |
128 | |
129 inline bool operator>=(const WideStringPiece& x, const WideStringPiece& y) { | |
130 return !(x < y); | |
131 } | |
132 | |
133 } // namespace base | |
134 | |
135 // Provide appropriate hash functions so WideStringPiece can be used as a key in | |
136 // hash sets and maps. | |
137 | |
138 // This hash function is copied from base/hash_tables.h. We don't use the | |
139 // already defined string16 directly because it would require the string16 | |
140 // constructor to be called, which we don't want. | |
141 #define HASH_WIDE_STRING_PIECE(wsp) \ | |
142 std::size_t result = 0; \ | |
143 for (base::WideStringPiece::const_iterator i = wsp.begin(); \ | |
144 i != wsp.end(); ++i) \ | |
145 result = (result * 131) + *i; \ | |
146 return result; \ | |
147 | |
148 namespace BASE_HASH_NAMESPACE { | |
149 #if defined(COMPILER_GCC) | |
150 | |
151 template<> | |
152 struct hash<base::WideStringPiece> { | |
153 std::size_t operator()(const base::WideStringPiece& wsp) const { | |
154 HASH_WIDE_STRING_PIECE(wsp); | |
155 } | |
156 }; | |
157 | |
158 #elif defined(COMPILER_MSVC) | |
159 | |
160 inline size_t hash_value(const base::WideStringPiece& wsp) { | |
161 HASH_WIDE_STRING_PIECE(wsp); | |
162 } | |
163 | |
164 #endif // COMPILER | |
165 | |
166 } // namespace BASE_HASH_NAMESPACE | |
167 | |
168 #undef HASH_WIDE_STRING_PIECE | |
169 | |
170 #endif // BASE_STRING_PIECE_H_ | |
OLD | NEW |