Skip to content

Commit

Permalink
Christoph Mallon: Replace strlen(x) == 0 (O(n)) by x[0] == '\0' (O(1)).
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Aug 29, 2013
1 parent 3e2930d commit e07d7e6
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 43 deletions.
12 changes: 6 additions & 6 deletions src/test/SDL_test_harness.c
Expand Up @@ -109,17 +109,17 @@ SDLTest_GenerateExecKey(char *runSeed, char *suiteName, char *testName, int iter
Uint32 entireStringLength;
char *buffer;

if (runSeed == NULL || SDL_strlen(runSeed)==0) {
if (runSeed == NULL || runSeed[0] == '\0') {
SDLTest_LogError("Invalid runSeed string.");
return -1;
}

if (suiteName == NULL || SDL_strlen(suiteName)==0) {
if (suiteName == NULL || suiteName[0] == '\0') {
SDLTest_LogError("Invalid suiteName string.");
return -1;
}

if (testName == NULL || SDL_strlen(testName)==0) {
if (testName == NULL || testName[0] == '\0') {
SDLTest_LogError("Invalid testName string.");
return -1;
}
Expand Down Expand Up @@ -399,7 +399,7 @@ int SDLTest_RunSuites(SDLTest_TestSuiteReference *testSuites[], const char *user
}

/* Generate run see if we don't have one already */
if (userRunSeed == NULL || SDL_strlen(userRunSeed) == 0) {
if (userRunSeed == NULL || userRunSeed[0] == '\0') {
runSeed = SDLTest_GenerateRunSeed(16);
if (runSeed == NULL) {
SDLTest_LogError("Generating a random seed failed");
Expand All @@ -422,7 +422,7 @@ int SDLTest_RunSuites(SDLTest_TestSuiteReference *testSuites[], const char *user
SDLTest_Log("::::: Test Run /w seed '%s' started\n", runSeed);

/* Initialize filtering */
if (filter != NULL && SDL_strlen(filter) > 0) {
if (filter != NULL && filter[0] != '\0') {
/* Loop over all suites to check if we have a filter match */
suiteCounter = 0;
while (testSuites[suiteCounter] && suiteFilter == 0) {
Expand Down Expand Up @@ -521,7 +521,7 @@ int SDLTest_RunSuites(SDLTest_TestSuiteReference *testSuites[], const char *user
suiteCounter,
testCounter,
currentTestName);
if (testCase->description != NULL && SDL_strlen(testCase->description)>0) {
if (testCase->description != NULL && testCase->description[0] != '\0') {
SDLTest_Log("Test Description: '%s'",
(testCase->description) ? testCase->description : SDLTest_InvalidNameFormat);
}
Expand Down
2 changes: 1 addition & 1 deletion src/video/SDL_clipboard.c
Expand Up @@ -65,7 +65,7 @@ SDL_HasClipboardText(void)
if (_this->HasClipboardText) {
return _this->HasClipboardText(_this);
} else {
if ((_this->clipboard_text) && (SDL_strlen(_this->clipboard_text)>0)) {
if (_this->clipboard_text && _this->clipboard_text[0] != '\0') {
return SDL_TRUE;
} else {
return SDL_FALSE;
Expand Down
4 changes: 2 additions & 2 deletions src/video/SDL_video.c
Expand Up @@ -1453,7 +1453,7 @@ SDL_SetWindowData(SDL_Window * window, const char *name, void *userdata)
CHECK_WINDOW_MAGIC(window, NULL);

/* Input validation */
if (name == NULL || SDL_strlen(name) == 0) {
if (name == NULL || name[0] == '\0') {
SDL_InvalidParamError("name");
return NULL;
}
Expand Down Expand Up @@ -1500,7 +1500,7 @@ SDL_GetWindowData(SDL_Window * window, const char *name)
CHECK_WINDOW_MAGIC(window, NULL);

/* Input validation */
if (name == NULL || SDL_strlen(name) == 0) {
if (name == NULL || name[0] == '\0') {
SDL_InvalidParamError("name");
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/video/bwindow/SDL_bclipboard.cc
Expand Up @@ -82,7 +82,7 @@ SDL_bool BE_HasClipboardText(_THIS) {
SDL_bool result = SDL_FALSE;
char *text = BE_GetClipboardText(_this);
if (text) {
result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
SDL_free(text);
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/video/cocoa/SDL_cocoaclipboard.m
Expand Up @@ -95,7 +95,7 @@
SDL_bool result = SDL_FALSE;
char *text = Cocoa_GetClipboardText(_this);
if (text) {
result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
SDL_free(text);
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/video/windows/SDL_windowsclipboard.c
Expand Up @@ -137,7 +137,7 @@ WIN_HasClipboardText(_THIS)
SDL_bool result = SDL_FALSE;
char *text = WIN_GetClipboardText(_this);
if (text) {
result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
SDL_free(text);
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/video/x11/SDL_x11clipboard.c
Expand Up @@ -165,7 +165,7 @@ X11_HasClipboardText(_THIS)
SDL_bool result = SDL_FALSE;
char *text = X11_GetClipboardText(_this);
if (text) {
result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
SDL_free(text);
}
return result;
Expand Down
14 changes: 7 additions & 7 deletions test/testautomation_audio.c
Expand Up @@ -77,7 +77,7 @@ int audio_initQuitAudio()
audioDriver = SDL_GetAudioDriver(i);
SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
SDLTest_AssertCheck(SDL_strlen(audioDriver) > 0, "Audio driver name is not empty; got: %s", audioDriver);
SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);

/* Call Init */
result = SDL_AudioInit(audioDriver);
Expand Down Expand Up @@ -134,7 +134,7 @@ int audio_initOpenCloseQuitAudio()
audioDriver = SDL_GetAudioDriver(i);
SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
SDLTest_AssertCheck(SDL_strlen(audioDriver) > 0, "Audio driver name is not empty; got: %s", audioDriver);
SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);

/* Change specs */
for (j = 0; j < 2; j++) {
Expand Down Expand Up @@ -226,14 +226,14 @@ int audio_enumerateAndNameAudioDevices()
SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
SDLTest_AssertCheck(name != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, t);
if (name != NULL) {
SDLTest_AssertCheck(SDL_strlen(name)>0, "verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, t, name);
SDLTest_AssertCheck(name[0] != '\0', "verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, t, name);
if (t==1) {
/* Also try non-zero type */
tt = t + SDLTest_RandomIntegerInRange(1,10);
nameAgain = SDL_GetAudioDeviceName(i, tt);
SDLTest_AssertCheck(nameAgain != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, tt);
if (nameAgain != NULL) {
SDLTest_AssertCheck(SDL_strlen(nameAgain)>0, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, tt, nameAgain);
SDLTest_AssertCheck(nameAgain[0] != '\0', "Verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, tt, nameAgain);
SDLTest_AssertCheck(SDL_strcmp(name, nameAgain)==0,
"Verify SDL_GetAudioDeviceName(%i, %i) and SDL_GetAudioDeviceName(%i %i) return the same string",
i, t, i, tt);
Expand Down Expand Up @@ -318,7 +318,7 @@ int audio_printAudioDrivers()
SDLTest_AssertPass("Call to SDL_GetAudioDriver(%i)", i);
SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
if (name != NULL) {
SDLTest_AssertCheck(SDL_strlen(name)>0, "Verify returned name is not empty, got: '%s'", name);
SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
}
}
}
Expand All @@ -339,7 +339,7 @@ int audio_printCurrentAudioDriver()
SDLTest_AssertPass("Call to SDL_GetCurrentAudioDriver()");
SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
if (name != NULL) {
SDLTest_AssertCheck(SDL_strlen(name)>0, "Verify returned name is not empty, got: '%s'", name);
SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
}

return TEST_COMPLETED;
Expand Down Expand Up @@ -509,7 +509,7 @@ int audio_buildAudioCVTNegative()
SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
error = SDL_GetError();
SDLTest_AssertPass("Call to SDL_GetError()");
SDLTest_AssertCheck(error != NULL && SDL_strlen(error)>0, "Validate that error message was not NULL or empty");
SDLTest_AssertCheck(error != NULL && error[0] != '\0', "Validate that error message was not NULL or empty");
}

SDL_ClearError();
Expand Down
2 changes: 1 addition & 1 deletion test/testautomation_clipboard.c
Expand Up @@ -118,7 +118,7 @@ clipboard_testClipboardTextFunctions(void *arg)
charResult != NULL,
"Verify SDL_GetClipboardText did not return NULL");
SDLTest_AssertCheck(
SDL_strlen(charResult) == 0,
charResult[0] == '\0',
"Verify SDL_GetClipboardText returned string with length 0, got length %i",
SDL_strlen(charResult));
intResult = SDL_SetClipboardText((const char *)text);
Expand Down
10 changes: 5 additions & 5 deletions test/testautomation_pixels.c
Expand Up @@ -242,7 +242,7 @@ pixels_getPixelFormatName(void *arg)
SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
if (result != NULL) {
SDLTest_AssertCheck(SDL_strlen(result) > 0, "Verify result is non-empty");
SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
SDLTest_AssertCheck(SDL_strcmp(result, unknownFormat) == 0,
"Verify result text; expected: %s, got %s", unknownFormat, result);
}
Expand All @@ -257,7 +257,7 @@ pixels_getPixelFormatName(void *arg)
SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
if (result != NULL) {
SDLTest_AssertCheck(SDL_strlen(result) > 0, "Verify result is non-empty");
SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
SDLTest_AssertCheck(SDL_strcmp(result, _RGBPixelFormatsVerbose[i]) == 0,
"Verify result text; expected: %s, got %s", _RGBPixelFormatsVerbose[i], result);
}
Expand All @@ -273,7 +273,7 @@ pixels_getPixelFormatName(void *arg)
SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
if (result != NULL) {
SDLTest_AssertCheck(SDL_strlen(result) > 0, "Verify result is non-empty");
SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
SDLTest_AssertCheck(SDL_strcmp(result, _nonRGBPixelFormatsVerbose[i]) == 0,
"Verify result text; expected: %s, got %s", _nonRGBPixelFormatsVerbose[i], result);
}
Expand All @@ -290,14 +290,14 @@ pixels_getPixelFormatName(void *arg)
SDLTest_AssertPass("Call to SDL_GetPixelFormatName(%u)", format);
SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
if (result != NULL) {
SDLTest_AssertCheck(SDL_strlen(result) > 0,
SDLTest_AssertCheck(result[0] != '\0',
"Verify result is non-empty; got: %s", result);
SDLTest_AssertCheck(SDL_strcmp(result, _invalidPixelFormatsVerbose[i]) == 0,
"Validate name is UNKNOWN, expected: '%s', got: '%s'", _invalidPixelFormatsVerbose[i], result);
}
error = SDL_GetError();
SDLTest_AssertPass("Call to SDL_GetError()");
SDLTest_AssertCheck(error != NULL && SDL_strlen(error) == 0, "Validate that error message is empty");
SDLTest_AssertCheck(error != NULL && error[0] != '\0', "Validate that error message is empty");
}

return TEST_COMPLETED;
Expand Down

0 comments on commit e07d7e6

Please sign in to comment.