| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 | 74 |
| 75 protected: | 75 protected: |
| 76 value_type* mBuf; | 76 value_type* mBuf; |
| 77 size_type mLen; | 77 size_type mLen; |
| 78 | 78 |
| 79 constexpr explicit String(value_type* buf, size_type len, size_type flags) | 79 constexpr explicit String(value_type* buf, size_type len, size_type flags) |
| 80 : mBuf(buf), mLen((len & LENGTH_MASK) | flags) | 80 : mBuf(buf), mLen((len & LENGTH_MASK) | flags) |
| 81 { | 81 { |
| 82 } | 82 } |
| 83 | 83 |
| 84 ~String() = default; | 84 ~String() = default; |
|
Eric
2018/03/15 17:05:00
We're not defining copy or move constructors, so t
René Jeschke
2018/03/15 17:41:23
What was the reason to make the dtor protected in
Eric
2018/03/15 18:33:54
Don't know. If there's a good reason, it deserves
sergei
2018/03/16 13:43:26
Such class hierarchy caused questions from the beg
| |
| 85 | 85 |
| 86 void reset(value_type* buf, size_type len, size_type flags) | 86 void reset(value_type* buf, size_type len, size_type flags) |
| 87 { | 87 { |
| 88 mBuf = buf; | 88 mBuf = buf; |
| 89 mLen = (len & LENGTH_MASK) | flags; | 89 mLen = (len & LENGTH_MASK) | flags; |
| 90 } | 90 } |
| 91 | 91 |
| 92 public: | 92 public: |
| 93 constexpr size_type length() const | 93 constexpr size_type length() const |
|
Eric
2018/03/15 17:05:00
constexpr implies const, so the trailing declarati
René Jeschke
2018/03/15 17:41:23
No, it does not. 'constexpr' on an object declarat
Eric
2018/03/15 18:33:54
Bah. Misread the spec. Never mind.
| |
| 94 { | 94 { |
| 95 return mLen & LENGTH_MASK; | 95 return mLen & LENGTH_MASK; |
| 96 } | 96 } |
| 97 | 97 |
| 98 constexpr bool empty() const | 98 constexpr bool empty() const |
| 99 { | 99 { |
| 100 return !(mLen & LENGTH_MASK); | 100 return !(mLen & LENGTH_MASK); |
| 101 } | 101 } |
| 102 | 102 |
| 103 constexpr const value_type* data() const | 103 constexpr const value_type* data() const |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 120 { | 120 { |
| 121 String_assert_writable(is_writable()); | 121 String_assert_writable(is_writable()); |
| 122 return mBuf[pos]; | 122 return mBuf[pos]; |
| 123 } | 123 } |
| 124 | 124 |
| 125 constexpr bool is_writable() const | 125 constexpr bool is_writable() const |
| 126 { | 126 { |
| 127 return (mLen & FLAGS_MASK) == READ_WRITE; | 127 return (mLen & FLAGS_MASK) == READ_WRITE; |
| 128 } | 128 } |
| 129 | 129 |
| 130 bool equals(const String& other) const | 130 constexpr bool equals(const String& other) const |
|
Eric
2018/03/15 17:05:00
I'd make this private, since its behavior is expos
René Jeschke
2018/03/15 17:41:22
'equals' is already used in 'compiled/filter/RegEx
Eric
2018/03/15 18:33:54
OK. It would be better to change it there, but tha
sergei
2018/03/16 13:43:26
Yes, it's outside the scope of this issue and ther
| |
| 131 { | 131 { |
| 132 if (length() != other.length()) | 132 if (length() != other.length()) |
| 133 return false; | 133 return false; |
| 134 | 134 |
| 135 return std::memcmp(mBuf, other.mBuf, sizeof(value_type) * length()) == 0; | 135 return std::memcmp(mBuf, other.mBuf, sizeof(value_type) * length()) == 0; |
| 136 } | 136 } |
| 137 | 137 |
| 138 bool operator==(const String& other) const | 138 constexpr bool operator==(const String& other) const |
|
Eric
2018/03/15 17:05:01
This can be constexpr, no?
René Jeschke
2018/03/15 17:41:23
Yep, 'equals' and 'operator==' can both be 'conste
| |
| 139 { | 139 { |
| 140 return equals(other); | 140 return equals(other); |
| 141 } | 141 } |
| 142 | 142 |
| 143 bool operator!=(const String& other) const | 143 constexpr bool operator!=(const String& other) const |
| 144 { | 144 { |
| 145 return !equals(other); | 145 return !equals(other); |
| 146 } | 146 } |
| 147 | 147 |
| 148 size_type find(value_type c, size_type pos = 0) const | 148 constexpr size_type find(value_type c, size_type pos = 0) const |
| 149 { | 149 { |
| 150 for (size_type i = pos; i < length(); ++i) | 150 for (size_type i = pos; i < length(); ++i) |
| 151 if (mBuf[i] == c) | 151 if (mBuf[i] == c) |
| 152 return i; | 152 return i; |
| 153 return npos; | 153 return npos; |
| 154 } | 154 } |
| 155 | 155 |
| 156 size_type find(const String& str, size_type pos = 0) const | 156 constexpr size_type find(const String& str, size_type pos = 0) const |
| 157 { | 157 { |
| 158 return find(str.mBuf, pos, str.length()); | 158 return find(str.mBuf, pos, str.length()); |
| 159 } | 159 } |
| 160 | 160 |
| 161 size_type find(const value_type* str, size_type pos, size_type count) const | 161 constexpr size_type find(const value_type* str, size_type pos, size_type count ) const |
| 162 { | 162 { |
| 163 if (pos > LENGTH_MASK || pos + count > length()) | 163 if (pos > LENGTH_MASK || pos + count > length()) |
| 164 return npos; | 164 return npos; |
| 165 | 165 |
| 166 if (!count) | 166 if (!count) |
| 167 return pos; | 167 return pos; |
| 168 | 168 |
| 169 for (; pos + count <= length(); ++pos) | 169 for (; pos + count <= length(); ++pos) |
| 170 { | 170 { |
| 171 if (mBuf[pos] == str[0] && | 171 if (mBuf[pos] == str[0] && |
| 172 std::memcmp(mBuf + pos, str, sizeof(value_type) * count) == 0) | 172 std::memcmp(mBuf + pos, str, sizeof(value_type) * count) == 0) |
| 173 { | 173 { |
| 174 return pos; | 174 return pos; |
| 175 } | 175 } |
| 176 } | 176 } |
| 177 | 177 |
| 178 return npos; | 178 return npos; |
| 179 } | 179 } |
| 180 | 180 |
| 181 size_type rfind(value_type c, size_type pos = npos) const | 181 constexpr size_type rfind(value_type c, size_type pos = npos) const |
| 182 { | 182 { |
| 183 if (length() == 0) | 183 if (length() == 0) |
| 184 return npos; | 184 return npos; |
| 185 | 185 |
| 186 if (pos >= length()) | 186 if (pos >= length()) |
| 187 pos = length() - 1; | 187 pos = length() - 1; |
| 188 | 188 |
| 189 for (int i = pos; i >= 0; --i) | 189 for (int i = pos; i >= 0; --i) |
| 190 if (mBuf[i] == c) | 190 if (mBuf[i] == c) |
| 191 return i; | 191 return i; |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 } | 310 } |
| 311 }; | 311 }; |
| 312 | 312 |
| 313 #ifdef INSIDE_TESTS | 313 #ifdef INSIDE_TESTS |
| 314 inline std::ostream& operator<<(std::ostream& os, const DependentString& str) | 314 inline std::ostream& operator<<(std::ostream& os, const DependentString& str) |
| 315 { | 315 { |
| 316 return os << static_cast<const String&>(str); | 316 return os << static_cast<const String&>(str); |
| 317 } | 317 } |
| 318 #endif | 318 #endif |
| 319 | 319 |
| 320 inline constexpr DependentString operator "" _str(const String::value_type* str, | 320 constexpr DependentString operator "" _str(const String::value_type* str, |
|
Eric
2018/03/15 17:05:01
constexpr on functions implies inline, so it could
René Jeschke
2018/03/15 17:41:23
Right. Done.
| |
| 321 String::size_type len) | 321 String::size_type len) |
| 322 { | 322 { |
| 323 return DependentString(str, len); | 323 return DependentString(str, len); |
| 324 } | 324 } |
| 325 | 325 |
| 326 inline void String_assert_writable(bool isWritable) | 326 inline void String_assert_writable(bool isWritable) |
| 327 { | 327 { |
| 328 assert2(isWritable, ABP_TEXT("Writing access to a read-only string"_str)); | 328 assert2(isWritable, ABP_TEXT("Writing access to a read-only string"_str)); |
| 329 } | 329 } |
| 330 | 330 |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 562 return OwnedString{value}; | 562 return OwnedString{value}; |
| 563 } | 563 } |
| 564 | 564 |
| 565 DependentString TrimSpaces(const String& value); | 565 DependentString TrimSpaces(const String& value); |
| 566 | 566 |
| 567 // Splits the `value` string into two `DependentString`s excluding the character staying at `separatorPos`. | 567 // Splits the `value` string into two `DependentString`s excluding the character staying at `separatorPos`. |
| 568 // Useful for parsing. | 568 // Useful for parsing. |
| 569 std::pair<DependentString, DependentString> SplitString(const String& value, Str ing::size_type separatorPos); | 569 std::pair<DependentString, DependentString> SplitString(const String& value, Str ing::size_type separatorPos); |
| 570 | 570 |
| 571 ABP_NS_END | 571 ABP_NS_END |
| LEFT | RIGHT |