Skip to content

Commit 37de41f

Browse files
committed
refactor: Even further optimisation
1 parent 6ed698b commit 37de41f

File tree

2 files changed

+38
-86
lines changed
  • GeneralsMD/Code/GameEngine/Source/Common/INI
  • Generals/Code/GameEngine/Source/Common/INI

2 files changed

+38
-86
lines changed

Generals/Code/GameEngine/Source/Common/INI/INI.cpp

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,65 +1868,41 @@ void INI::parseDeathTypeFlags(INI* ini, void* /*instance*/, void* store, const v
18681868
// both blockType and blockName are case insensitive
18691869
Bool INI::isDeclarationOfType( AsciiString blockType, AsciiString blockName, char *bufferToCheck )
18701870
{
1871-
Bool retVal = true;
1872-
if (!bufferToCheck || blockType.isEmpty() || blockName.isEmpty()) {
1871+
if (!bufferToCheck || blockType.isEmpty() || blockName.isEmpty())
18731872
return false;
1874-
}
1875-
// DO NOT RETURN EARLY FROM THIS FUNCTION. (beyond this point)
1876-
// we have to restore the bufferToCheck to its previous state before returning, so
1877-
// it is important to get through all the checks.
18781873

1879-
char restoreChar;
1880-
char *tempBuff = bufferToCheck;
1881-
int blockTypeLength = blockType.getLength();
1882-
int blockNameLength = blockName.getLength();
1874+
const char* tempBuff = bufferToCheck;
18831875

1884-
while (isspace(*tempBuff)) {
1876+
while (isspace(*tempBuff))
18851877
++tempBuff;
1886-
}
18871878

1888-
size_t tempBuffLen = strlen(tempBuff);
1889-
char* tempBuffEnd = tempBuff + tempBuffLen;
1879+
const int blockTypeLength = blockType.getLength();
1880+
if (strnicmp(tempBuff, blockType.str(), blockTypeLength) != 0)
1881+
return false;
18901882

1891-
if (tempBuffLen > blockTypeLength) {
1892-
restoreChar = tempBuff[blockTypeLength];
1893-
tempBuff[blockTypeLength] = 0;
1883+
tempBuff += blockTypeLength;
18941884

1895-
if (stricmp(blockType.str(), tempBuff) != 0) {
1896-
retVal = false;
1897-
}
1898-
1899-
tempBuff[blockTypeLength] = restoreChar;
1900-
tempBuff = tempBuff + blockTypeLength;
1901-
} else {
1902-
retVal = false;
1903-
}
1885+
if (!isspace(*tempBuff))
1886+
return false;
19041887

1905-
while (isspace(*tempBuff)) {
1888+
while (isspace(*tempBuff))
19061889
++tempBuff;
1907-
}
19081890

1909-
tempBuffLen = tempBuffEnd - tempBuff;
1910-
if (tempBuffLen > blockNameLength) {
1911-
restoreChar = tempBuff[blockNameLength];
1912-
tempBuff[blockNameLength] = 0;
1891+
const int blockNameLength = blockName.getLength();
1892+
if (strnicmp(tempBuff, blockName.str(), blockNameLength) != 0)
1893+
return false;
19131894

1914-
if (stricmp(blockName.str(), tempBuff) != 0) {
1915-
retVal = false;
1916-
}
1895+
tempBuff += blockNameLength;
19171896

1918-
tempBuff[blockNameLength] = restoreChar;
1919-
tempBuff = tempBuff + blockNameLength;
1920-
} else {
1921-
retVal = false;
1922-
}
1897+
while (*tempBuff)
1898+
{
1899+
if (!isspace(*tempBuff))
1900+
return false;
19231901

1924-
while (*tempBuff && retVal) {
1925-
retVal = isspace(*tempBuff);
19261902
++tempBuff;
19271903
}
19281904

1929-
return retVal;
1905+
return true;
19301906
}
19311907

19321908
//-------------------------------------------------------------------------------------------------

GeneralsMD/Code/GameEngine/Source/Common/INI/INI.cpp

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,65 +1873,41 @@ void INI::parseDeathTypeFlags(INI* ini, void* /*instance*/, void* store, const v
18731873
// both blockType and blockName are case insensitive
18741874
Bool INI::isDeclarationOfType( AsciiString blockType, AsciiString blockName, char *bufferToCheck )
18751875
{
1876-
Bool retVal = true;
1877-
if (!bufferToCheck || blockType.isEmpty() || blockName.isEmpty()) {
1876+
if (!bufferToCheck || blockType.isEmpty() || blockName.isEmpty())
18781877
return false;
1879-
}
1880-
// DO NOT RETURN EARLY FROM THIS FUNCTION. (beyond this point)
1881-
// we have to restore the bufferToCheck to its previous state before returning, so
1882-
// it is important to get through all the checks.
18831878

1884-
char restoreChar;
1885-
char *tempBuff = bufferToCheck;
1886-
int blockTypeLength = blockType.getLength();
1887-
int blockNameLength = blockName.getLength();
1879+
const char* tempBuff = bufferToCheck;
18881880

1889-
while (isspace(*tempBuff)) {
1881+
while (isspace(*tempBuff))
18901882
++tempBuff;
1891-
}
18921883

1893-
size_t tempBuffLen = strlen(tempBuff);
1894-
char* tempBuffEnd = tempBuff + tempBuffLen;
1884+
const int blockTypeLength = blockType.getLength();
1885+
if (strnicmp(tempBuff, blockType.str(), blockTypeLength) != 0)
1886+
return false;
18951887

1896-
if (tempBuffLen > blockTypeLength) {
1897-
restoreChar = tempBuff[blockTypeLength];
1898-
tempBuff[blockTypeLength] = 0;
1888+
tempBuff += blockTypeLength;
18991889

1900-
if (stricmp(blockType.str(), tempBuff) != 0) {
1901-
retVal = false;
1902-
}
1903-
1904-
tempBuff[blockTypeLength] = restoreChar;
1905-
tempBuff = tempBuff + blockTypeLength;
1906-
} else {
1907-
retVal = false;
1908-
}
1890+
if (!isspace(*tempBuff))
1891+
return false;
19091892

1910-
while (isspace(*tempBuff)) {
1893+
while (isspace(*tempBuff))
19111894
++tempBuff;
1912-
}
19131895

1914-
tempBuffLen = tempBuffEnd - tempBuff;
1915-
if (tempBuffLen > blockNameLength) {
1916-
restoreChar = tempBuff[blockNameLength];
1917-
tempBuff[blockNameLength] = 0;
1896+
const int blockNameLength = blockName.getLength();
1897+
if (strnicmp(tempBuff, blockName.str(), blockNameLength) != 0)
1898+
return false;
19181899

1919-
if (stricmp(blockName.str(), tempBuff) != 0) {
1920-
retVal = false;
1921-
}
1900+
tempBuff += blockNameLength;
19221901

1923-
tempBuff[blockNameLength] = restoreChar;
1924-
tempBuff = tempBuff + blockNameLength;
1925-
} else {
1926-
retVal = false;
1927-
}
1902+
while (*tempBuff)
1903+
{
1904+
if (!isspace(*tempBuff))
1905+
return false;
19281906

1929-
while (*tempBuff && retVal) {
1930-
retVal = isspace(*tempBuff);
19311907
++tempBuff;
19321908
}
19331909

1934-
return retVal;
1910+
return true;
19351911
}
19361912

19371913
//-------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)