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

Side by Side Diff: third_party/WebKit/Source/wtf/text/StringImpl.cpp

Issue 2226363003: Use StringView for String::reverseFind. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months 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) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller ( mueller@kde.org ) 4 * (C) 2001 Dirk Mueller ( mueller@kde.org )
5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All r ights reserved. 5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All r ights reserved.
6 * Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net) 6 * Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net)
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public 9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 1299 matching lines...) Expand 10 before | Expand all | Expand 10 after
1310 } 1310 }
1311 1311
1312 size_t StringImpl::reverseFind(UChar c, unsigned index) 1312 size_t StringImpl::reverseFind(UChar c, unsigned index)
1313 { 1313 {
1314 if (is8Bit()) 1314 if (is8Bit())
1315 return WTF::reverseFind(characters8(), m_length, c, index); 1315 return WTF::reverseFind(characters8(), m_length, c, index);
1316 return WTF::reverseFind(characters16(), m_length, c, index); 1316 return WTF::reverseFind(characters16(), m_length, c, index);
1317 } 1317 }
1318 1318
1319 template <typename SearchCharacterType, typename MatchCharacterType> 1319 template <typename SearchCharacterType, typename MatchCharacterType>
1320 ALWAYS_INLINE static size_t reverseFindInner(const SearchCharacterType* searchCh aracters, const MatchCharacterType* matchCharacters, unsigned index, unsigned le ngth, unsigned matchLength) 1320 ALWAYS_INLINE static size_t reverseFindInternal(const SearchCharacterType* searc hCharacters, const MatchCharacterType* matchCharacters, unsigned index, unsigned length, unsigned matchLength)
1321 { 1321 {
1322 // Optimization: keep a running hash of the strings, 1322 // Optimization: keep a running hash of the strings,
1323 // only call equal if the hashes match. 1323 // only call equal if the hashes match.
1324 1324
1325 // delta is the number of additional times to test; delta == 0 means test on ly once. 1325 // delta is the number of additional times to test; delta == 0 means test on ly once.
1326 unsigned delta = min(index, length - matchLength); 1326 unsigned delta = min(index, length - matchLength);
1327 1327
1328 unsigned searchHash = 0; 1328 unsigned searchHash = 0;
1329 unsigned matchHash = 0; 1329 unsigned matchHash = 0;
1330 for (unsigned i = 0; i < matchLength; ++i) { 1330 for (unsigned i = 0; i < matchLength; ++i) {
1331 searchHash += searchCharacters[delta + i]; 1331 searchHash += searchCharacters[delta + i];
1332 matchHash += matchCharacters[i]; 1332 matchHash += matchCharacters[i];
1333 } 1333 }
1334 1334
1335 // keep looping until we match 1335 // keep looping until we match
1336 while (searchHash != matchHash || !equal(searchCharacters + delta, matchChar acters, matchLength)) { 1336 while (searchHash != matchHash || !equal(searchCharacters + delta, matchChar acters, matchLength)) {
1337 if (!delta) 1337 if (!delta)
1338 return kNotFound; 1338 return kNotFound;
1339 --delta; 1339 --delta;
1340 searchHash -= searchCharacters[delta + matchLength]; 1340 searchHash -= searchCharacters[delta + matchLength];
1341 searchHash += searchCharacters[delta]; 1341 searchHash += searchCharacters[delta];
1342 } 1342 }
1343 return delta; 1343 return delta;
1344 } 1344 }
1345 1345
1346 size_t StringImpl::reverseFind(StringImpl* matchString, unsigned index) 1346 size_t StringImpl::reverseFind(const StringView& matchString, unsigned index)
1347 { 1347 {
1348 // Check for null or empty string to match against 1348 if (UNLIKELY(matchString.isNull()))
1349 if (!matchString)
1350 return kNotFound; 1349 return kNotFound;
1351 unsigned matchLength = matchString->length(); 1350
1351 unsigned matchLength = matchString.length();
1352 unsigned ourLength = length(); 1352 unsigned ourLength = length();
1353 if (!matchLength) 1353 if (!matchLength)
1354 return min(index, ourLength); 1354 return min(index, ourLength);
1355 1355
1356 // Optimization 1: fast case for strings of length 1. 1356 // Optimization 1: fast case for strings of length 1.
1357 if (matchLength == 1) { 1357 if (matchLength == 1) {
1358 if (is8Bit()) 1358 if (is8Bit())
1359 return WTF::reverseFind(characters8(), ourLength, (*matchString)[0], index); 1359 return WTF::reverseFind(characters8(), ourLength, matchString[0], in dex);
1360 return WTF::reverseFind(characters16(), ourLength, (*matchString)[0], in dex); 1360 return WTF::reverseFind(characters16(), ourLength, matchString[0], index );
1361 } 1361 }
1362 1362
1363 // Check index & matchLength are in range. 1363 // Check index & matchLength are in range.
1364 if (matchLength > ourLength) 1364 if (matchLength > ourLength)
1365 return kNotFound; 1365 return kNotFound;
1366 1366
1367 if (is8Bit()) { 1367 if (is8Bit()) {
1368 if (matchString->is8Bit()) 1368 if (matchString.is8Bit())
1369 return reverseFindInner(characters8(), matchString->characters8(), i ndex, ourLength, matchLength); 1369 return reverseFindInternal(characters8(), matchString.characters8(), index, ourLength, matchLength);
1370 return reverseFindInner(characters8(), matchString->characters16(), inde x, ourLength, matchLength); 1370 return reverseFindInternal(characters8(), matchString.characters16(), in dex, ourLength, matchLength);
1371 } 1371 }
1372 1372 if (matchString.is8Bit())
1373 if (matchString->is8Bit()) 1373 return reverseFindInternal(characters16(), matchString.characters8(), in dex, ourLength, matchLength);
1374 return reverseFindInner(characters16(), matchString->characters8(), inde x, ourLength, matchLength); 1374 return reverseFindInternal(characters16(), matchString.characters16(), index , ourLength, matchLength);
1375
1376 return reverseFindInner(characters16(), matchString->characters16(), index, ourLength, matchLength);
1377 } 1375 }
1378 1376
1379 bool StringImpl::startsWith(UChar character) const 1377 bool StringImpl::startsWith(UChar character) const
1380 { 1378 {
1381 return m_length && (*this)[0] == character; 1379 return m_length && (*this)[0] == character;
1382 } 1380 }
1383 1381
1384 bool StringImpl::startsWith(const StringView& prefix) const 1382 bool StringImpl::startsWith(const StringView& prefix) const
1385 { 1383 {
1386 if (prefix.length() > length()) 1384 if (prefix.length() > length())
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after
2013 } else if (localeIdMatchesLang(localeIdentifier, "lt")) { 2011 } else if (localeIdMatchesLang(localeIdentifier, "lt")) {
2014 // TODO(rob.buis) implement upper-casing rules for lt 2012 // TODO(rob.buis) implement upper-casing rules for lt
2015 // like in StringImpl::upper(locale). 2013 // like in StringImpl::upper(locale).
2016 } 2014 }
2017 } 2015 }
2018 2016
2019 return toUpper(c); 2017 return toUpper(c);
2020 } 2018 }
2021 2019
2022 } // namespace WTF 2020 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/text/StringImpl.h ('k') | third_party/WebKit/Source/wtf/text/WTFString.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698