Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: third_party/WebKit/Source/wtf/text/StringBuilder.h

Issue 2508953003: WTF: Add comments and tests for Unicode aware case-insensitive string operations. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009, 2010, 2012, 2013 Apple Inc. All rights reserved. 2 * Copyright (C) 2009, 2010, 2012, 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Google Inc. All rights reserved. 3 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 unsigned length) { 240 unsigned length) {
241 if (s.length() != length) 241 if (s.length() != length)
242 return false; 242 return false;
243 243
244 if (s.is8Bit()) 244 if (s.is8Bit())
245 return equalIgnoringCase(s.characters8(), buffer, length); 245 return equalIgnoringCase(s.characters8(), buffer, length);
246 246
247 return equalIgnoringCase(s.characters16(), buffer, length); 247 return equalIgnoringCase(s.characters16(), buffer, length);
248 } 248 }
249 249
250 // Unicode aware case insensitive string matching. Non-ASCII characters might
251 // match to ASCII characters. This function is rarely used to implement web
252 // platform features.
250 inline bool equalIgnoringCase(const StringBuilder& s, const char* string) { 253 inline bool equalIgnoringCase(const StringBuilder& s, const char* string) {
251 return equalIgnoringCase(s, reinterpret_cast<const LChar*>(string), 254 return equalIgnoringCase(s, reinterpret_cast<const LChar*>(string),
252 strlen(string)); 255 strlen(string));
253 } 256 }
254 257
255 template <typename StringType> 258 template <typename StringType>
256 bool equal(const StringBuilder& a, const StringType& b) { 259 bool equal(const StringBuilder& a, const StringType& b) {
257 if (a.length() != b.length()) 260 if (a.length() != b.length())
258 return false; 261 return false;
259 262
260 if (!a.length()) 263 if (!a.length())
261 return true; 264 return true;
262 265
263 if (a.is8Bit()) { 266 if (a.is8Bit()) {
264 if (b.is8Bit()) 267 if (b.is8Bit())
265 return equal(a.characters8(), b.characters8(), a.length()); 268 return equal(a.characters8(), b.characters8(), a.length());
266 return equal(a.characters8(), b.characters16(), a.length()); 269 return equal(a.characters8(), b.characters16(), a.length());
267 } 270 }
268 271
269 if (b.is8Bit()) 272 if (b.is8Bit())
270 return equal(a.characters16(), b.characters8(), a.length()); 273 return equal(a.characters16(), b.characters8(), a.length());
271 return equal(a.characters16(), b.characters16(), a.length()); 274 return equal(a.characters16(), b.characters16(), a.length());
272 } 275 }
273 276
274 template <typename StringType>
275 bool equalIgnoringCase(const StringBuilder& a, const StringType& b) {
276 if (a.length() != b.length())
277 return false;
278
279 if (!a.length())
280 return true;
281
282 if (a.is8Bit()) {
283 if (b.is8Bit())
284 return equalIgnoringCase(a.characters8(), b.characters8(), a.length());
285 return equalIgnoringCase(a.characters8(), b.characters16(), a.length());
286 }
287
288 if (b.is8Bit())
289 return equalIgnoringCase(a.characters16(), b.characters8(), a.length());
290 return equalIgnoringCase(a.characters16(), b.characters16(), a.length());
291 }
292
293 inline bool operator==(const StringBuilder& a, const StringBuilder& b) { 277 inline bool operator==(const StringBuilder& a, const StringBuilder& b) {
294 return equal(a, b); 278 return equal(a, b);
295 } 279 }
296 inline bool operator!=(const StringBuilder& a, const StringBuilder& b) { 280 inline bool operator!=(const StringBuilder& a, const StringBuilder& b) {
297 return !equal(a, b); 281 return !equal(a, b);
298 } 282 }
299 inline bool operator==(const StringBuilder& a, const String& b) { 283 inline bool operator==(const StringBuilder& a, const String& b) {
300 return equal(a, b); 284 return equal(a, b);
301 } 285 }
302 inline bool operator!=(const StringBuilder& a, const String& b) { 286 inline bool operator!=(const StringBuilder& a, const String& b) {
303 return !equal(a, b); 287 return !equal(a, b);
304 } 288 }
305 inline bool operator==(const String& a, const StringBuilder& b) { 289 inline bool operator==(const String& a, const StringBuilder& b) {
306 return equal(b, a); 290 return equal(b, a);
307 } 291 }
308 inline bool operator!=(const String& a, const StringBuilder& b) { 292 inline bool operator!=(const String& a, const StringBuilder& b) {
309 return !equal(b, a); 293 return !equal(b, a);
310 } 294 }
311 295
312 } // namespace WTF 296 } // namespace WTF
313 297
314 using WTF::StringBuilder; 298 using WTF::StringBuilder;
315 299
316 #endif // StringBuilder_h 300 #endif // StringBuilder_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/text/AtomicString.h ('k') | third_party/WebKit/Source/wtf/text/StringImpl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698