Skip to content

Commit d5021cf

Browse files
JohnCoconutminggo
authored andcommitted
Optimize calls to std::string::find() and friends when the needle passed is a single character string literal. The character literal overload is more efficient. (#19614)
1 parent b1e4a16 commit d5021cf

File tree

30 files changed

+81
-81
lines changed

30 files changed

+81
-81
lines changed

cocos/2d/CCFontAtlas.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,9 @@ std::string FontAtlas::getFontName() const
487487
{
488488
std::string fontName = _fontFreeType ? _fontFreeType->getFontName() : "";
489489
if(fontName.empty()) return fontName;
490-
auto idx = fontName.rfind("/");
490+
auto idx = fontName.rfind('/');
491491
if (idx != std::string::npos) { return fontName.substr(idx + 1); }
492-
idx = fontName.rfind("\\");
492+
idx = fontName.rfind('\\');
493493
if (idx != std::string::npos) { return fontName.substr(idx + 1); }
494494
return fontName;
495495
}

cocos/2d/CCFontCharMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ NS_CC_BEGIN
3535
FontCharMap * FontCharMap::create(const std::string& plistFile)
3636
{
3737
std::string pathStr = FileUtils::getInstance()->fullPathForFilename(plistFile);
38-
std::string relPathStr = pathStr.substr(0, pathStr.find_last_of("/"))+"/";
38+
std::string relPathStr = pathStr.substr(0, pathStr.find_last_of('/'))+"/";
3939

4040
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(pathStr);
4141

cocos/2d/CCLabelAtlas.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ LabelAtlas* LabelAtlas::create(const std::string& string, const std::string& fnt
105105
bool LabelAtlas::initWithString(const std::string& theString, const std::string& fntFile)
106106
{
107107
std::string pathStr = FileUtils::getInstance()->fullPathForFilename(fntFile);
108-
std::string relPathStr = pathStr.substr(0, pathStr.find_last_of("/"))+"/";
108+
std::string relPathStr = pathStr.substr(0, pathStr.find_last_of('/'))+"/";
109109

110110
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(pathStr);
111111

cocos/2d/CCSpriteFrameCache.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ void SpriteFrameCache::addSpriteFramesWithFile(const std::string& plist)
410410
texturePath = plist;
411411

412412
// remove .xxx
413-
size_t startPos = texturePath.find_last_of(".");
413+
size_t startPos = texturePath.find_last_of('.');
414414
texturePath = texturePath.erase(startPos);
415415

416416
// append .png
@@ -712,7 +712,7 @@ bool SpriteFrameCache::reloadTexture(const std::string& plist)
712712
texturePath = plist;
713713

714714
// remove .xxx
715-
size_t startPos = texturePath.find_last_of(".");
715+
size_t startPos = texturePath.find_last_of('.');
716716
texturePath = texturePath.erase(startPos);
717717

718718
// append .png

cocos/2d/CCTMXXMLParser.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char *name, const char **atts
298298
_externalTilesetFilename = externalTilesetFilename;
299299

300300
// Tileset file will be relative to the map file. So we need to convert it to an absolute path
301-
if (_TMXFileName.find_last_of("/") != string::npos)
301+
if (_TMXFileName.find_last_of('/') != string::npos)
302302
{
303-
string dir = _TMXFileName.substr(0, _TMXFileName.find_last_of("/") + 1);
303+
string dir = _TMXFileName.substr(0, _TMXFileName.find_last_of('/') + 1);
304304
externalTilesetFilename = dir + externalTilesetFilename;
305305
}
306306
else
@@ -434,12 +434,12 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char *name, const char **atts
434434

435435
if (!_externalTilesetFullPath.empty())
436436
{
437-
string dir = _externalTilesetFullPath.substr(0, _externalTilesetFullPath.find_last_of("/") + 1);
437+
string dir = _externalTilesetFullPath.substr(0, _externalTilesetFullPath.find_last_of('/') + 1);
438438
tileset->_sourceImage = dir + imagename;
439439
}
440-
else if (_TMXFileName.find_last_of("/") != string::npos)
440+
else if (_TMXFileName.find_last_of('/') != string::npos)
441441
{
442-
string dir = _TMXFileName.substr(0, _TMXFileName.find_last_of("/") + 1);
442+
string dir = _TMXFileName.substr(0, _TMXFileName.find_last_of('/') + 1);
443443
tileset->_sourceImage = dir + imagename;
444444
}
445445
else

cocos/3d/CCBundle3D.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ bool Bundle3D::loadObj(MeshDatas& meshdatas, MaterialDatas& materialdatas, NodeD
225225
int i = 0;
226226
char str[20];
227227
std::string dir = "";
228-
auto last = fullPath.rfind("/");
228+
auto last = fullPath.rfind('/');
229229
if (last != std::string::npos)
230230
dir = fullPath.substr(0, last + 1);
231231
for (auto& material : materials) {

cocos/base/CCConsole.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ void Console::Command::commandGeneric(int fd, const std::string& args)
454454
{
455455
// The first argument (including the empty)
456456
std::string key(args);
457-
auto pos = args.find(" ");
457+
auto pos = args.find(' ');
458458
if ((pos != std::string::npos) && (0 < pos)) {
459459
key = args.substr(0, pos);
460460
}

cocos/base/CCProperties.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,12 +1117,12 @@ void calculateNamespacePath(const std::string& urlString, std::string& fileStrin
11171117
{
11181118
// If the url references a specific namespace within the file,
11191119
// calculate the full namespace path to the final namespace.
1120-
size_t loc = urlString.rfind("#");
1120+
size_t loc = urlString.rfind('#');
11211121
if (loc != std::string::npos)
11221122
{
11231123
fileString = urlString.substr(0, loc);
11241124
std::string namespacePathString = urlString.substr(loc + 1);
1125-
while ((loc = namespacePathString.find("/")) != std::string::npos)
1125+
while ((loc = namespacePathString.find('/')) != std::string::npos)
11261126
{
11271127
namespacePath.push_back(namespacePathString.substr(0, loc));
11281128
namespacePathString = namespacePathString.substr(loc + 1);

cocos/base/ZipUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ std::vector<std::string> ZipFile::listFiles(const std::string &pathname) const
627627
if(filename.substr(0, dirname.length()) == dirname)
628628
{
629629
std::string suffix = filename.substr(dirname.length());
630-
auto pos = suffix.find("/");
630+
auto pos = suffix.find('/');
631631
if (pos == std::string::npos)
632632
{
633633
fileSet.insert(suffix);

cocos/base/ccUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void onCaptureScreen(const std::function<void(bool, const std::string&)>& afterC
124124
}
125125
else
126126
{
127-
CCASSERT(filename.find("/") == std::string::npos, "The existence of a relative path is not guaranteed!");
127+
CCASSERT(filename.find('/') == std::string::npos, "The existence of a relative path is not guaranteed!");
128128
outputFile = FileUtils::getInstance()->getWritablePath() + filename;
129129
}
130130

0 commit comments

Comments
 (0)