LEFT | RIGHT |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2017 eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
1 #pragma once | 18 #pragma once |
2 | 19 |
| 20 #include <algorithm> |
3 #include <cstddef> | 21 #include <cstddef> |
4 #include <cstring> | 22 #include <cstring> |
5 #include <algorithm> | 23 #include <type_traits> |
6 | 24 |
7 #include <emscripten.h> | 25 #include <emscripten.h> |
8 | 26 |
9 #include "debug.h" | 27 #include "debug.h" |
10 | 28 |
11 inline void String_assert_readonly(bool readOnly); | 29 inline void String_assert_readonly(bool readOnly); |
12 | 30 |
13 class String | 31 class String |
14 { | 32 { |
15 friend class DependentString; | 33 friend class DependentString; |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 explicit DependentString(value_type* buf, size_type len) | 199 explicit DependentString(value_type* buf, size_type len) |
182 : String(buf, len, READ_WRITE) | 200 : String(buf, len, READ_WRITE) |
183 { | 201 { |
184 } | 202 } |
185 | 203 |
186 explicit DependentString(const value_type* buf, size_type len) | 204 explicit DependentString(const value_type* buf, size_type len) |
187 : String(const_cast<value_type*>(buf), len, READ_ONLY) | 205 : String(const_cast<value_type*>(buf), len, READ_ONLY) |
188 { | 206 { |
189 } | 207 } |
190 | 208 |
191 DependentString(String& str, size_type pos = 0, size_type len = npos) | 209 explicit DependentString(String& str, size_type pos = 0, size_type len = npos) |
192 : String( | 210 : String( |
193 str.mBuf + std::min(pos, str.length()), | 211 str.mBuf + std::min(pos, str.length()), |
194 std::min(len, str.length() - std::min(pos, str.length())), | 212 std::min(len, str.length() - std::min(pos, str.length())), |
195 str.is_readOnly() ? READ_ONLY : READ_WRITE | 213 str.is_readOnly() ? READ_ONLY : READ_WRITE |
196 ) | 214 ) |
197 { | 215 { |
198 } | 216 } |
199 | 217 |
200 DependentString(const String& str, size_type pos = 0, | 218 explicit DependentString(const String& str, size_type pos = 0, |
201 size_type len = npos) | 219 size_type len = npos) |
202 : String( | 220 : String( |
203 str.mBuf + std::min(pos, str.length()), | 221 str.mBuf + std::min(pos, str.length()), |
204 std::min(len, str.length() - std::min(pos, str.length())), | 222 std::min(len, str.length() - std::min(pos, str.length())), |
205 READ_ONLY | 223 READ_ONLY |
206 ) | 224 ) |
207 { | 225 { |
208 } | 226 } |
209 | 227 |
210 void reset(value_type* buf, size_type len) | 228 void reset(value_type* buf, size_type len) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 inline void String_assert_readonly(bool readOnly) | 261 inline void String_assert_readonly(bool readOnly) |
244 { | 262 { |
245 assert(!readOnly, u"Writing access to a read-only string"_str); | 263 assert(!readOnly, u"Writing access to a read-only string"_str); |
246 } | 264 } |
247 | 265 |
248 class OwnedString : public String | 266 class OwnedString : public String |
249 { | 267 { |
250 private: | 268 private: |
251 void grow(size_type additionalSize) | 269 void grow(size_type additionalSize) |
252 { | 270 { |
253 auto newValue = OwnedString(length() + additionalSize); | 271 OwnedString newValue(length() + additionalSize); |
254 if (length() > 0) | 272 if (length() > 0) |
255 std::memcpy(newValue.mBuf, mBuf, sizeof(value_type) * length()); | 273 std::memcpy(newValue.mBuf, mBuf, sizeof(value_type) * length()); |
256 *this = std::move(newValue); | 274 *this = std::move(newValue); |
257 } | 275 } |
258 | 276 |
259 public: | 277 public: |
260 explicit OwnedString(size_type len = 0) | 278 explicit OwnedString(size_type len = 0) |
261 : String(nullptr, len, READ_WRITE) | 279 : String(nullptr, len, READ_WRITE) |
262 { | 280 { |
263 if (len) | 281 if (len) |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 | 363 |
346 void append(const String& str) | 364 void append(const String& str) |
347 { | 365 { |
348 append(str.mBuf, str.length()); | 366 append(str.mBuf, str.length()); |
349 } | 367 } |
350 | 368 |
351 void append(value_type c) | 369 void append(value_type c) |
352 { | 370 { |
353 append(&c, 1); | 371 append(&c, 1); |
354 } | 372 } |
| 373 |
| 374 template<typename T, |
| 375 typename std::enable_if<std::is_integral<T>::value>::type* = nullptr> |
| 376 void append(T num) |
| 377 { |
| 378 bool negative = false; |
| 379 if (num < 0) |
| 380 { |
| 381 negative = true; |
| 382 num = -num; |
| 383 } |
| 384 |
| 385 size_type size = 0; |
| 386 for (T i = num; i; i /= 10) |
| 387 size++; |
| 388 size = (size ? size : 1); |
| 389 |
| 390 size_type pos = length(); |
| 391 grow((negative ? 1 : 0) + size); |
| 392 |
| 393 if (negative) |
| 394 mBuf[pos++] = '-'; |
| 395 |
| 396 for (int i = size - 1; i >= 0; i--) |
| 397 { |
| 398 mBuf[pos + i] = '0' + (num % 10); |
| 399 num /= 10; |
| 400 } |
| 401 } |
355 }; | 402 }; |
LEFT | RIGHT |