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 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 | 54 |
55 static constexpr size_type FLAGS_MASK = 0xC0000000; | 55 static constexpr size_type FLAGS_MASK = 0xC0000000; |
56 static constexpr size_type LENGTH_MASK = 0x3FFFFFFF; | 56 static constexpr size_type LENGTH_MASK = 0x3FFFFFFF; |
57 | 57 |
58 static constexpr size_type npos = -1; | 58 static constexpr size_type npos = -1; |
59 | 59 |
60 protected: | 60 protected: |
61 value_type* mBuf; | 61 value_type* mBuf; |
62 size_type mLen; | 62 size_type mLen; |
63 | 63 |
64 explicit String(value_type* buf, size_type len, size_type flags) | 64 constexpr explicit String(value_type* buf, size_type len, size_type flags) |
65 : mBuf(buf), mLen((len & LENGTH_MASK) | flags) | 65 : mBuf(buf), mLen((len & LENGTH_MASK) | flags) |
66 { | 66 { |
67 } | 67 } |
68 | 68 |
69 ~String() | 69 ~String() = default; |
70 { | |
71 } | |
72 | 70 |
73 void reset(value_type* buf, size_type len, size_type flags) | 71 void reset(value_type* buf, size_type len, size_type flags) |
74 { | 72 { |
75 mBuf = buf; | 73 mBuf = buf; |
76 mLen = (len & LENGTH_MASK) | flags; | 74 mLen = (len & LENGTH_MASK) | flags; |
77 } | 75 } |
78 | 76 |
79 public: | 77 public: |
80 size_type length() const | 78 constexpr size_type length() const |
81 { | 79 { |
82 return mLen & LENGTH_MASK; | 80 return mLen & LENGTH_MASK; |
83 } | 81 } |
84 | 82 |
85 bool empty() const | 83 constexpr bool empty() const |
86 { | 84 { |
87 return !(mLen & LENGTH_MASK); | 85 return !(mLen & LENGTH_MASK); |
88 } | 86 } |
89 | 87 |
90 const value_type* data() const | 88 constexpr const value_type* data() const |
91 { | 89 { |
92 return mBuf; | 90 return mBuf; |
93 } | 91 } |
94 | 92 |
95 value_type* data() | 93 value_type* data() |
96 { | 94 { |
97 String_assert_writable(is_writable()); | 95 String_assert_writable(is_writable()); |
98 return mBuf; | 96 return mBuf; |
99 } | 97 } |
100 | 98 |
101 const value_type& operator[](size_type pos) const | 99 const value_type& operator[](size_type pos) const |
102 { | 100 { |
103 return mBuf[pos]; | 101 return mBuf[pos]; |
104 } | 102 } |
105 | 103 |
106 value_type& operator[](size_type pos) | 104 value_type& operator[](size_type pos) |
107 { | 105 { |
108 String_assert_writable(is_writable()); | 106 String_assert_writable(is_writable()); |
109 return mBuf[pos]; | 107 return mBuf[pos]; |
110 } | 108 } |
111 | 109 |
112 bool is_writable() const | 110 constexpr bool is_writable() const |
113 { | 111 { |
114 return (mLen & FLAGS_MASK) == READ_WRITE; | 112 return (mLen & FLAGS_MASK) == READ_WRITE; |
115 } | 113 } |
116 | 114 |
117 bool equals(const String& other) const | 115 bool equals(const String& other) const |
118 { | 116 { |
119 if (length() != other.length()) | 117 if (length() != other.length()) |
120 return false; | 118 return false; |
121 | 119 |
122 return std::memcmp(mBuf, other.mBuf, sizeof(value_type) * length()) == 0; | 120 return std::memcmp(mBuf, other.mBuf, sizeof(value_type) * length()) == 0; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 | 170 |
173 if (pos >= length()) | 171 if (pos >= length()) |
174 pos = length() - 1; | 172 pos = length() - 1; |
175 | 173 |
176 for (int i = pos; i >= 0; --i) | 174 for (int i = pos; i >= 0; --i) |
177 if (mBuf[i] == c) | 175 if (mBuf[i] == c) |
178 return i; | 176 return i; |
179 return npos; | 177 return npos; |
180 } | 178 } |
181 | 179 |
182 bool is_invalid() const | 180 constexpr bool is_invalid() const |
183 { | 181 { |
184 return (mLen & FLAGS_MASK) == INVALID; | 182 return (mLen & FLAGS_MASK) == INVALID; |
185 } | 183 } |
186 | 184 |
187 bool is_deleted() const | 185 constexpr bool is_deleted() const |
188 { | 186 { |
189 return (mLen & FLAGS_MASK) == DELETED; | 187 return (mLen & FLAGS_MASK) == DELETED; |
190 } | 188 } |
191 | 189 |
192 void toLower() | 190 void toLower() |
193 { | 191 { |
194 size_type len = length(); | 192 size_type len = length(); |
195 for (size_type i = 0; i < len; ++i) | 193 for (size_type i = 0; i < len; ++i) |
196 { | 194 { |
197 value_type currChar = mBuf[i]; | 195 value_type currChar = mBuf[i]; |
(...skipping 21 matching lines...) Expand all Loading... | |
219 std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter; | 217 std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter; |
220 os << converter.to_bytes(str.data(), str.data() + str.length()); | 218 os << converter.to_bytes(str.data(), str.data() + str.length()); |
221 #endif | 219 #endif |
222 return os; | 220 return os; |
223 } | 221 } |
224 #endif | 222 #endif |
225 | 223 |
226 class DependentString : public String | 224 class DependentString : public String |
227 { | 225 { |
228 public: | 226 public: |
229 explicit DependentString() | 227 explicit DependentString() |
sergei
2018/03/14 16:22:53
what about this one too?
René Jeschke
2018/03/14 16:27:07
Is there a reason why we would want invalid nullpt
sergei
2018/03/14 17:01:46
It's difficult to say right now, and it's also up
| |
230 : String(nullptr, 0, INVALID) | 228 : String(nullptr, 0, INVALID) |
231 { | 229 { |
232 } | 230 } |
233 | 231 |
232 template <int N1> | |
233 constexpr explicit DependentString(const value_type (&buf)[N1]) | |
234 : String(const_cast<value_type*>(buf), N1 - 1, READ_ONLY) | |
235 { | |
236 } | |
237 | |
234 explicit DependentString(value_type* buf, size_type len) | 238 explicit DependentString(value_type* buf, size_type len) |
235 : String(buf, len, READ_WRITE) | 239 : String(buf, len, READ_WRITE) |
236 { | 240 { |
237 } | 241 } |
238 | 242 |
239 explicit DependentString(const value_type* buf, size_type len) | 243 explicit DependentString(const value_type* buf, size_type len) |
240 : String(const_cast<value_type*>(buf), len, READ_ONLY) | 244 : String(const_cast<value_type*>(buf), len, READ_ONLY) |
241 { | 245 { |
242 } | 246 } |
243 | 247 |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
537 return OwnedString{value}; | 541 return OwnedString{value}; |
538 } | 542 } |
539 | 543 |
540 DependentString TrimSpaces(const String& value); | 544 DependentString TrimSpaces(const String& value); |
541 | 545 |
542 // Splits the `value` string into two `DependentString`s excluding the character staying at `separatorPos`. | 546 // Splits the `value` string into two `DependentString`s excluding the character staying at `separatorPos`. |
543 // Useful for parsing. | 547 // Useful for parsing. |
544 std::pair<DependentString, DependentString> SplitString(const String& value, Str ing::size_type separatorPos); | 548 std::pair<DependentString, DependentString> SplitString(const String& value, Str ing::size_type separatorPos); |
545 | 549 |
546 ABP_NS_END | 550 ABP_NS_END |
OLD | NEW |