OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #pragma once | 18 #pragma once |
19 | 19 |
20 #include <algorithm> | 20 #include <algorithm> |
21 #include <cstddef> | 21 #include <cstddef> |
22 #include <cstring> | 22 #include <cstring> |
23 #include <type_traits> | 23 #include <type_traits> |
24 | 24 |
25 #include "debug.h" | 25 #include "debug.h" |
26 #include "library.h" | 26 #include "library.h" |
27 | 27 |
28 inline void String_assert_readonly(bool readOnly); | 28 inline void String_assert_writable(bool isWritable); |
29 | 29 |
30 class String | 30 class String |
31 { | 31 { |
32 friend class DependentString; | 32 friend class DependentString; |
33 friend class OwnedString; | 33 friend class OwnedString; |
34 | 34 |
35 public: | 35 public: |
36 typedef char16_t value_type; | 36 typedef char16_t value_type; |
37 typedef size_t size_type; | 37 typedef size_t size_type; |
38 | 38 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 return !(mLen & LENGTH_MASK); | 77 return !(mLen & LENGTH_MASK); |
78 } | 78 } |
79 | 79 |
80 const value_type* data() const | 80 const value_type* data() const |
81 { | 81 { |
82 return mBuf; | 82 return mBuf; |
83 } | 83 } |
84 | 84 |
85 value_type* data() | 85 value_type* data() |
86 { | 86 { |
87 String_assert_readonly(is_readOnly()); | 87 String_assert_writable(is_writable()); |
88 return mBuf; | 88 return mBuf; |
89 } | 89 } |
90 | 90 |
91 const value_type& operator[](size_type pos) const | 91 const value_type& operator[](size_type pos) const |
92 { | 92 { |
93 return mBuf[pos]; | 93 return mBuf[pos]; |
94 } | 94 } |
95 | 95 |
96 value_type& operator[](size_type pos) | 96 value_type& operator[](size_type pos) |
97 { | 97 { |
98 String_assert_readonly(is_readOnly()); | 98 String_assert_writable(is_writable()); |
99 return mBuf[pos]; | 99 return mBuf[pos]; |
100 } | 100 } |
101 | 101 |
102 bool is_readOnly() const | 102 bool is_writable() const |
103 { | 103 { |
104 return (mLen & FLAGS_MASK) != READ_WRITE; | 104 return (mLen & FLAGS_MASK) == READ_WRITE; |
105 } | 105 } |
106 | 106 |
107 bool equals(const String& other) const | 107 bool equals(const String& other) const |
108 { | 108 { |
109 if (length() != other.length()) | 109 if (length() != other.length()) |
110 return false; | 110 return false; |
111 | 111 |
112 return std::memcmp(mBuf, other.mBuf, sizeof(value_type) * length()) == 0; | 112 return std::memcmp(mBuf, other.mBuf, sizeof(value_type) * length()) == 0; |
113 } | 113 } |
114 | 114 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 208 |
209 explicit DependentString(const value_type* buf, size_type len) | 209 explicit DependentString(const value_type* buf, size_type len) |
210 : String(const_cast<value_type*>(buf), len, READ_ONLY) | 210 : String(const_cast<value_type*>(buf), len, READ_ONLY) |
211 { | 211 { |
212 } | 212 } |
213 | 213 |
214 explicit DependentString(String& str, size_type pos = 0, size_type len = npos) | 214 explicit DependentString(String& str, size_type pos = 0, size_type len = npos) |
215 : String( | 215 : String( |
216 str.mBuf + std::min(pos, str.length()), | 216 str.mBuf + std::min(pos, str.length()), |
217 std::min(len, str.length() - std::min(pos, str.length())), | 217 std::min(len, str.length() - std::min(pos, str.length())), |
218 str.is_readOnly() ? READ_ONLY : READ_WRITE | 218 str.is_writable() ? READ_WRITE: READ_ONLY |
219 ) | 219 ) |
220 { | 220 { |
221 } | 221 } |
222 | 222 |
223 explicit DependentString(const String& str, size_type pos = 0, | 223 explicit DependentString(const String& str, size_type pos = 0, |
224 size_type len = npos) | 224 size_type len = npos) |
225 : String( | 225 : String( |
226 str.mBuf + std::min(pos, str.length()), | 226 str.mBuf + std::min(pos, str.length()), |
227 std::min(len, str.length() - std::min(pos, str.length())), | 227 std::min(len, str.length() - std::min(pos, str.length())), |
228 READ_ONLY | 228 READ_ONLY |
(...skipping 27 matching lines...) Expand all Loading... |
256 mLen = DELETED; | 256 mLen = DELETED; |
257 } | 257 } |
258 }; | 258 }; |
259 | 259 |
260 inline DependentString operator "" _str(const String::value_type* str, | 260 inline DependentString operator "" _str(const String::value_type* str, |
261 String::size_type len) | 261 String::size_type len) |
262 { | 262 { |
263 return DependentString(str, len); | 263 return DependentString(str, len); |
264 } | 264 } |
265 | 265 |
266 inline void String_assert_readonly(bool readOnly) | 266 inline void String_assert_writable(bool isWritable) |
267 { | 267 { |
268 assert(!readOnly, u"Writing access to a read-only string"_str); | 268 assert(isWritable, u"Writing access to a read-only string"_str); |
269 } | 269 } |
270 | 270 |
271 class OwnedString : public String | 271 class OwnedString : public String |
272 { | 272 { |
273 private: | 273 private: |
274 void grow(size_type additionalSize) | 274 void grow(size_type additionalSize) |
275 { | 275 { |
276 OwnedString newValue(length() + additionalSize); | 276 OwnedString newValue(length() + additionalSize); |
277 if (length() > 0) | 277 if (length() > 0) |
278 std::memcpy(newValue.mBuf, mBuf, sizeof(value_type) * length()); | 278 std::memcpy(newValue.mBuf, mBuf, sizeof(value_type) * length()); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 if (negative) | 398 if (negative) |
399 mBuf[pos++] = '-'; | 399 mBuf[pos++] = '-'; |
400 | 400 |
401 for (int i = size - 1; i >= 0; i--) | 401 for (int i = size - 1; i >= 0; i--) |
402 { | 402 { |
403 mBuf[pos + i] = '0' + (num % 10); | 403 mBuf[pos + i] = '0' + (num % 10); |
404 num /= 10; | 404 num /= 10; |
405 } | 405 } |
406 } | 406 } |
407 }; | 407 }; |
OLD | NEW |