Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: compiled/String.h

Issue 29722755: Issue 6378 - [emscripten] Make DependentString constexpr
Patch Set: Made _str constexpr. Created March 15, 2018, 4:09 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/compiled/String.cpp » ('j') | test/compiled/String.cpp » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 69
70 static constexpr size_type FLAGS_MASK = 0xC0000000; 70 static constexpr size_type FLAGS_MASK = 0xC0000000;
71 static constexpr size_type LENGTH_MASK = 0x3FFFFFFF; 71 static constexpr size_type LENGTH_MASK = 0x3FFFFFFF;
72 72
73 static constexpr size_type npos = -1; 73 static constexpr size_type npos = -1;
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 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() 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 {
86 }
87 85
88 void reset(value_type* buf, size_type len, size_type flags) 86 void reset(value_type* buf, size_type len, size_type flags)
89 { 87 {
90 mBuf = buf; 88 mBuf = buf;
91 mLen = (len & LENGTH_MASK) | flags; 89 mLen = (len & LENGTH_MASK) | flags;
92 } 90 }
93 91
94 public: 92 public:
95 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.
96 { 94 {
97 return mLen & LENGTH_MASK; 95 return mLen & LENGTH_MASK;
98 } 96 }
99 97
100 bool empty() const 98 constexpr bool empty() const
101 { 99 {
102 return !(mLen & LENGTH_MASK); 100 return !(mLen & LENGTH_MASK);
103 } 101 }
104 102
105 const value_type* data() const 103 constexpr const value_type* data() const
106 { 104 {
107 return mBuf; 105 return mBuf;
108 } 106 }
109 107
110 value_type* data() 108 value_type* data()
111 { 109 {
112 String_assert_writable(is_writable()); 110 String_assert_writable(is_writable());
113 return mBuf; 111 return mBuf;
114 } 112 }
115 113
116 const value_type& operator[](size_type pos) const 114 constexpr const value_type& operator[](size_type pos) const
117 { 115 {
118 return mBuf[pos]; 116 return mBuf[pos];
119 } 117 }
120 118
121 value_type& operator[](size_type pos) 119 value_type& operator[](size_type pos)
122 { 120 {
123 String_assert_writable(is_writable()); 121 String_assert_writable(is_writable());
124 return mBuf[pos]; 122 return mBuf[pos];
125 } 123 }
126 124
127 bool is_writable() const 125 constexpr bool is_writable() const
128 { 126 {
129 return (mLen & FLAGS_MASK) == READ_WRITE; 127 return (mLen & FLAGS_MASK) == READ_WRITE;
130 } 128 }
131 129
132 bool equals(const String& other) const 130 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
133 { 131 {
134 if (length() != other.length()) 132 if (length() != other.length())
135 return false; 133 return false;
136 134
137 return std::memcmp(mBuf, other.mBuf, sizeof(value_type) * length()) == 0; 135 return std::memcmp(mBuf, other.mBuf, sizeof(value_type) * length()) == 0;
138 } 136 }
139 137
140 bool operator==(const String& other) const 138 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
141 { 139 {
142 return equals(other); 140 return equals(other);
143 } 141 }
144 142
145 bool operator!=(const String& other) const 143 bool operator!=(const String& other) const
146 { 144 {
147 return !equals(other); 145 return !equals(other);
148 } 146 }
149 147
150 size_type find(value_type c, size_type pos = 0) const 148 size_type find(value_type c, size_type pos = 0) const
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 185
188 if (pos >= length()) 186 if (pos >= length())
189 pos = length() - 1; 187 pos = length() - 1;
190 188
191 for (int i = pos; i >= 0; --i) 189 for (int i = pos; i >= 0; --i)
192 if (mBuf[i] == c) 190 if (mBuf[i] == c)
193 return i; 191 return i;
194 return npos; 192 return npos;
195 } 193 }
196 194
197 bool is_invalid() const 195 constexpr bool is_invalid() const
198 { 196 {
199 return (mLen & FLAGS_MASK) == INVALID; 197 return (mLen & FLAGS_MASK) == INVALID;
200 } 198 }
201 199
202 bool is_deleted() const 200 constexpr bool is_deleted() const
203 { 201 {
204 return (mLen & FLAGS_MASK) == DELETED; 202 return (mLen & FLAGS_MASK) == DELETED;
205 } 203 }
206 204
207 void toLower() 205 void toLower()
208 { 206 {
209 size_type len = length(); 207 size_type len = length();
210 for (size_type i = 0; i < len; ++i) 208 for (size_type i = 0; i < len; ++i)
211 { 209 {
212 value_type currChar = mBuf[i]; 210 value_type currChar = mBuf[i];
(...skipping 25 matching lines...) Expand all
238 os << converter.to_bytes(str.data(), str.data() + str.length()); 236 os << converter.to_bytes(str.data(), str.data() + str.length());
239 #endif // _MSC_VER >= 1900 237 #endif // _MSC_VER >= 1900
240 #endif // ABP_UTF8_STRING 238 #endif // ABP_UTF8_STRING
241 return os; 239 return os;
242 } 240 }
243 #endif // INSIDE_TESTS 241 #endif // INSIDE_TESTS
244 242
245 class DependentString : public String 243 class DependentString : public String
246 { 244 {
247 public: 245 public:
248 explicit DependentString() 246 constexpr explicit DependentString()
249 : String(nullptr, 0, INVALID) 247 : String(nullptr, 0, INVALID)
250 { 248 {
251 } 249 }
252 250
251 template <int N1>
252 constexpr explicit DependentString(const value_type (&buf)[N1])
253 : String(const_cast<value_type*>(buf), N1 - 1, READ_ONLY)
254 {
255 }
256
253 explicit DependentString(value_type* buf, size_type len) 257 explicit DependentString(value_type* buf, size_type len)
254 : String(buf, len, READ_WRITE) 258 : String(buf, len, READ_WRITE)
255 { 259 {
256 } 260 }
257 261
258 explicit DependentString(const value_type* buf, size_type len) 262 constexpr explicit DependentString(const value_type* buf, size_type len)
259 : String(const_cast<value_type*>(buf), len, READ_ONLY) 263 : String(const_cast<value_type*>(buf), len, READ_ONLY)
260 { 264 {
261 } 265 }
262 266
263 explicit DependentString(String& str, size_type pos = 0, size_type len = npos) 267 explicit DependentString(String& str, size_type pos = 0, size_type len = npos)
264 : String( 268 : String(
265 str.mBuf + std::min(pos, str.length()), 269 str.mBuf + std::min(pos, str.length()),
266 std::min(len, str.length() - std::min(pos, str.length())), 270 std::min(len, str.length() - std::min(pos, str.length())),
267 str.is_writable() ? READ_WRITE: READ_ONLY 271 str.is_writable() ? READ_WRITE: READ_ONLY
268 ) 272 )
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 } 310 }
307 }; 311 };
308 312
309 #ifdef INSIDE_TESTS 313 #ifdef INSIDE_TESTS
310 inline std::ostream& operator<<(std::ostream& os, const DependentString& str) 314 inline std::ostream& operator<<(std::ostream& os, const DependentString& str)
311 { 315 {
312 return os << static_cast<const String&>(str); 316 return os << static_cast<const String&>(str);
313 } 317 }
314 #endif 318 #endif
315 319
316 inline DependentString operator "" _str(const String::value_type* str, 320 inline 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.
317 String::size_type len) 321 String::size_type len)
318 { 322 {
319 return DependentString(str, len); 323 return DependentString(str, len);
320 } 324 }
321 325
322 inline void String_assert_writable(bool isWritable) 326 inline void String_assert_writable(bool isWritable)
323 { 327 {
324 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));
325 } 329 }
326 330
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 return OwnedString{value}; 562 return OwnedString{value};
559 } 563 }
560 564
561 DependentString TrimSpaces(const String& value); 565 DependentString TrimSpaces(const String& value);
562 566
563 // 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`.
564 // Useful for parsing. 568 // Useful for parsing.
565 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);
566 570
567 ABP_NS_END 571 ABP_NS_END
OLDNEW
« no previous file with comments | « no previous file | test/compiled/String.cpp » ('j') | test/compiled/String.cpp » ('J')

Powered by Google App Engine
This is Rietveld