| Left: | ||
| Right: |
| 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 #include <utility> | |
| 24 | 25 |
| 25 #include "debug.h" | 26 #include "debug.h" |
| 26 #include "library.h" | 27 #include "library.h" |
| 27 | 28 |
| 28 inline void String_assert_writable(bool isWritable); | 29 inline void String_assert_writable(bool isWritable); |
| 29 | 30 |
| 30 class String | 31 class String |
| 31 { | 32 { |
| 32 friend class DependentString; | 33 friend class DependentString; |
| 33 friend class OwnedString; | 34 friend class OwnedString; |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 // This should be more efficient with a lookup table but I couldn't measur e | 185 // This should be more efficient with a lookup table but I couldn't measur e |
| 185 // any performance difference. | 186 // any performance difference. |
| 186 if (currChar >= u'A' && currChar <= u'Z') | 187 if (currChar >= u'A' && currChar <= u'Z') |
| 187 mBuf[i] = currChar + u'a' - u'A'; | 188 mBuf[i] = currChar + u'a' - u'A'; |
| 188 else if (currChar >= 128) | 189 else if (currChar >= 128) |
| 189 { | 190 { |
| 190 mBuf[i] = CharToLower(currChar); | 191 mBuf[i] = CharToLower(currChar); |
| 191 } | 192 } |
| 192 } | 193 } |
| 193 } | 194 } |
| 195 | |
| 196 int64_t toInt64() const | |
| 197 { | |
| 198 size_type len = length(); | |
| 199 if (len == 0) | |
| 200 return 0; | |
|
Wladimir Palant
2017/12/21 10:30:36
What about using StringScanner so that we won't ne
| |
| 201 size_type pos = 0; | |
| 202 bool negative = mBuf[0] == u'-'; | |
| 203 if (negative) | |
| 204 { | |
| 205 ++pos; | |
| 206 } | |
| 207 int64_t result = 0; | |
| 208 for (; pos < len && mBuf[pos] >= u'0' && mBuf[pos] <= u'9'; ++pos) | |
| 209 { | |
| 210 result *= 10; | |
| 211 result += mBuf[pos] - u'0'; | |
| 212 } | |
|
hub
2017/11/22 15:40:12
No overflow checking?
int64_t is quite a lot of s
hub
2017/11/30 21:35:37
std::numeric_limits<int64_t>::digit10 is the value
| |
| 213 return negative ? -result : result; | |
| 214 } | |
| 215 | |
| 216 uint64_t toUInt64() const | |
|
Wladimir Palant
2017/12/21 10:30:36
Nit: This should be called toUint64().
| |
| 217 { | |
| 218 uint64_t result = 0; | |
| 219 size_type len = length(); | |
| 220 for (size_type pos = 0; pos < len && mBuf[pos] >= u'0' && mBuf[pos] <= u'9'; ++pos) | |
| 221 { | |
| 222 result *= 10; | |
| 223 result += mBuf[pos] - u'0'; | |
| 224 } | |
| 225 return result; | |
| 226 } | |
|
Wladimir Palant
2017/12/21 10:30:36
General note: this should be ok as a first iterati
| |
| 194 }; | 227 }; |
| 195 | 228 |
| 196 class DependentString : public String | 229 class DependentString : public String |
| 197 { | 230 { |
| 198 public: | 231 public: |
| 199 explicit DependentString() | 232 explicit DependentString() |
| 200 : String(nullptr, 0, INVALID) | 233 : String(nullptr, 0, INVALID) |
| 201 { | 234 { |
| 202 } | 235 } |
| 203 | 236 |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 if (negative) | 431 if (negative) |
| 399 mBuf[pos++] = '-'; | 432 mBuf[pos++] = '-'; |
| 400 | 433 |
| 401 for (int i = size - 1; i >= 0; i--) | 434 for (int i = size - 1; i >= 0; i--) |
| 402 { | 435 { |
| 403 mBuf[pos + i] = '0' + (num % 10); | 436 mBuf[pos + i] = '0' + (num % 10); |
| 404 num /= 10; | 437 num /= 10; |
| 405 } | 438 } |
| 406 } | 439 } |
| 407 }; | 440 }; |
| 441 | |
| 442 template<typename Type> | |
| 443 inline Type AdaptValue(const String& value); | |
|
hub
2017/11/22 15:40:12
I would have called this "lexical_cast<>" probably
Wladimir Palant
2017/12/21 10:30:36
Can't we have that as an instance method instead?
| |
| 444 | |
| 445 template<> | |
| 446 inline bool AdaptValue<bool>(const String& value) | |
| 447 { | |
| 448 return value == u"true"_str; | |
| 449 } | |
| 450 template<> | |
| 451 inline int AdaptValue<int>(const String& value) | |
| 452 { | |
| 453 return static_cast<int>(value.toInt64()); | |
| 454 } | |
| 455 template<> | |
| 456 inline uint64_t AdaptValue<uint64_t>(const String& value) | |
| 457 { | |
| 458 return value.toUInt64(); | |
| 459 } | |
| 460 template<> | |
| 461 inline OwnedString AdaptValue<OwnedString>(const String& value) | |
| 462 { | |
| 463 return OwnedString{value}; | |
| 464 } | |
| 465 | |
| 466 DependentString TrimSpaces(const String& value); | |
| 467 | |
| 468 inline std::pair<DependentString, DependentString> SplitString(const String& val ue, String::size_type separatorPos) | |
| 469 { | |
| 470 const auto secondBeginPos = separatorPos < String::npos ? separatorPos + 1 : S tring::npos; | |
| 471 return {DependentString{value, 0, separatorPos}, | |
| 472 DependentString{value, secondBeginPos, value.length() - secondBeginPos}}; | |
|
Wladimir Palant
2017/12/21 10:30:36
What is `value.length() - secondBeginPos` going to
sergei
2018/03/01 15:05:25
It's handled by the constructor of DependentString
| |
| 473 } | |
|
Wladimir Palant
2017/12/21 10:30:37
Why aren't these helpers methods of the String cla
sergei
2018/03/01 15:05:25
These functions are rather helper functions than m
| |
| OLD | NEW |