Skip to content

Commit

Permalink
[C][Client] Reduce number of unnecessary strlen() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
imaami committed Jan 8, 2025
1 parent 42516f6 commit 99ef14e
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ end:
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("{{{path}}}")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "{{{path}}}");
char *localVarPath = strdup("{{{path}}}");

{{#pathParams}}
{{#isString}}
Expand All @@ -126,7 +124,7 @@ end:
{{#pathParams}}

// Path Params
long sizeOfPathParams_{{{paramName}}} = {{#pathParams}}{{#isLong}}sizeof({{paramName}})+3{{/isLong}}{{#isString}}strlen({{^isEnum}}{{paramName}}{{/isEnum}}{{#isEnum}}{{{operationId}}}_{{enumName}}_ToString({{paramName}}){{/isEnum}})+3{{/isString}}{{^-last}} + {{/-last}}{{/pathParams}} + strlen("{ {{baseName}} }");
long sizeOfPathParams_{{{paramName}}} = {{#pathParams}}{{#isLong}}sizeof({{paramName}})+3{{/isLong}}{{#isString}}strlen({{^isEnum}}{{paramName}}{{/isEnum}}{{#isEnum}}{{{operationId}}}_{{enumName}}_ToString({{paramName}}){{/isEnum}})+3{{/isString}}{{^-last}} + {{/-last}}{{/pathParams}} + sizeof("{ {{baseName}} }") - 1;
{{#isNumeric}}
if({{paramName}} == 0){
goto end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,11 @@ void sslConfig_free(sslConfig_t *sslConfig) {
free(sslConfig);
}

static void replaceSpaceWithPlus(char *stringToProcess) {
for(int i = 0; i < strlen(stringToProcess); i++) {
if(stringToProcess[i] == ' ') {
stringToProcess[i] = '+';
static void replaceSpaceWithPlus(char *str) {
if (str) {
for (; *str; str++) {
if (*str == ' ')
*str = '+';
}
}
}
Expand Down Expand Up @@ -288,9 +289,9 @@ void apiClient_invoke(apiClient_t *apiClient,
list_ForEach(listEntry, headerType) {
if(strstr(listEntry->data, "xml") == NULL)
{
buffHeader = malloc(strlen("Accept: ") +
strlen(listEntry->data) + 1);
sprintf(buffHeader, "%s%s", "Accept: ",
buffHeader = malloc(sizeof("Accept: ") +
strlen(listEntry->data));
sprintf(buffHeader, "Accept: %s",
(char *) listEntry->data);
headers = curl_slist_append(headers, buffHeader);
free(buffHeader);
Expand All @@ -301,9 +302,9 @@ void apiClient_invoke(apiClient_t *apiClient,
list_ForEach(listEntry, contentType) {
if(strstr(listEntry->data, "xml") == NULL)
{
buffContent = malloc(strlen("Content-Type: ") +
strlen(listEntry->data) + 1);
sprintf(buffContent, "%s%s", "Content-Type: ",
buffContent = malloc(sizeof("Content-Type: ") +
strlen(listEntry->data));
sprintf(buffContent, "Content-Type: %s",
(char *) listEntry->data);
headers = curl_slist_append(headers, buffContent);
free(buffContent);
Expand Down
12 changes: 3 additions & 9 deletions samples/client/others/c/bearerAuth/api/DefaultAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ DefaultAPI_privateGet(apiClient_t *apiClient)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/private")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/private");
char *localVarPath = strdup("/private");



Expand Down Expand Up @@ -98,9 +96,7 @@ DefaultAPI_publicGet(apiClient_t *apiClient)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/public")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/public");
char *localVarPath = strdup("/public");



Expand Down Expand Up @@ -170,9 +166,7 @@ DefaultAPI_usersGet(apiClient_t *apiClient)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/users")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/users");
char *localVarPath = strdup("/users");



Expand Down
21 changes: 11 additions & 10 deletions samples/client/others/c/bearerAuth/src/apiClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ void sslConfig_free(sslConfig_t *sslConfig) {
free(sslConfig);
}

static void replaceSpaceWithPlus(char *stringToProcess) {
for(int i = 0; i < strlen(stringToProcess); i++) {
if(stringToProcess[i] == ' ') {
stringToProcess[i] = '+';
static void replaceSpaceWithPlus(char *str) {
if (str) {
for (; *str; str++) {
if (*str == ' ')
*str = '+';
}
}
}
Expand Down Expand Up @@ -204,9 +205,9 @@ void apiClient_invoke(apiClient_t *apiClient,
list_ForEach(listEntry, headerType) {
if(strstr(listEntry->data, "xml") == NULL)
{
buffHeader = malloc(strlen("Accept: ") +
strlen(listEntry->data) + 1);
sprintf(buffHeader, "%s%s", "Accept: ",
buffHeader = malloc(sizeof("Accept: ") +
strlen(listEntry->data));
sprintf(buffHeader, "Accept: %s",
(char *) listEntry->data);
headers = curl_slist_append(headers, buffHeader);
free(buffHeader);
Expand All @@ -217,9 +218,9 @@ void apiClient_invoke(apiClient_t *apiClient,
list_ForEach(listEntry, contentType) {
if(strstr(listEntry->data, "xml") == NULL)
{
buffContent = malloc(strlen("Content-Type: ") +
strlen(listEntry->data) + 1);
sprintf(buffContent, "%s%s", "Content-Type: ",
buffContent = malloc(sizeof("Content-Type: ") +
strlen(listEntry->data));
sprintf(buffContent, "Content-Type: %s",
(char *) listEntry->data);
headers = curl_slist_append(headers, buffContent);
free(buffContent);
Expand Down
62 changes: 18 additions & 44 deletions samples/client/petstore/c-useJsonUnformatted/api/PetAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ PetAPI_addPet(apiClient_t *apiClient, pet_t *body)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/pet")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet");
char *localVarPath = strdup("/pet");



Expand Down Expand Up @@ -139,14 +137,12 @@ PetAPI_deletePet(apiClient_t *apiClient, long petId, char *api_key)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/pet/{petId}")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/{petId}");
char *localVarPath = strdup("/pet/{petId}");



// Path Params
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
long sizeOfPathParams_petId = sizeof(petId)+3 + sizeof("{ petId }") - 1;
if(petId == 0){
goto end;
}
Expand Down Expand Up @@ -232,9 +228,7 @@ PetAPI_findPetsByStatus(apiClient_t *apiClient, list_t *status)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/pet/findByStatus")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/findByStatus");
char *localVarPath = strdup("/pet/findByStatus");



Expand Down Expand Up @@ -325,9 +319,7 @@ PetAPI_findPetsByTags(apiClient_t *apiClient, list_t *tags)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/pet/findByTags")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/findByTags");
char *localVarPath = strdup("/pet/findByTags");



Expand Down Expand Up @@ -416,9 +408,7 @@ PetAPI_getDaysWithoutIncident(apiClient_t *apiClient)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/store/daysWithoutIncident")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/store/daysWithoutIncident");
char *localVarPath = strdup("/store/daysWithoutIncident");



Expand Down Expand Up @@ -481,14 +471,12 @@ PetAPI_getPetById(apiClient_t *apiClient, long petId)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/pet/{petId}")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/{petId}");
char *localVarPath = strdup("/pet/{petId}");



// Path Params
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
long sizeOfPathParams_petId = sizeof(petId)+3 + sizeof("{ petId }") - 1;
if(petId == 0){
goto end;
}
Expand Down Expand Up @@ -575,9 +563,7 @@ PetAPI_getPicture(apiClient_t *apiClient)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/pet/picture")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/picture");
char *localVarPath = strdup("/pet/picture");



Expand Down Expand Up @@ -638,14 +624,12 @@ PetAPI_isPetAvailable(apiClient_t *apiClient, long petId)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/pet/{petId}/isAvailable")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/{petId}/isAvailable");
char *localVarPath = strdup("/pet/{petId}/isAvailable");



// Path Params
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
long sizeOfPathParams_petId = sizeof(petId)+3 + sizeof("{ petId }") - 1;
if(petId == 0){
goto end;
}
Expand Down Expand Up @@ -723,9 +707,7 @@ PetAPI_sharePicture(apiClient_t *apiClient, binary_t* picture)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/pet/picture")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/picture");
char *localVarPath = strdup("/pet/picture");



Expand Down Expand Up @@ -794,9 +776,7 @@ PetAPI_specialtyPet(apiClient_t *apiClient)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/pet/specialty")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/specialty");
char *localVarPath = strdup("/pet/specialty");



Expand Down Expand Up @@ -865,9 +845,7 @@ PetAPI_updatePet(apiClient_t *apiClient, pet_t *body)
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/pet")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet");
char *localVarPath = strdup("/pet");



Expand Down Expand Up @@ -945,14 +923,12 @@ PetAPI_updatePetWithForm(apiClient_t *apiClient, long petId, char *name, char *s
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/pet/{petId}")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/{petId}");
char *localVarPath = strdup("/pet/{petId}");



// Path Params
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
long sizeOfPathParams_petId = sizeof(petId)+3 + sizeof("{ petId }") - 1;
if(petId == 0){
goto end;
}
Expand Down Expand Up @@ -1058,14 +1034,12 @@ PetAPI_uploadFile(apiClient_t *apiClient, long petId, char *additionalMetadata,
apiClient->response_code = 0;

// create the path
long sizeOfPath = strlen("/pet/{petId}/uploadImage")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/{petId}/uploadImage");
char *localVarPath = strdup("/pet/{petId}/uploadImage");



// Path Params
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
long sizeOfPathParams_petId = sizeof(petId)+3 + sizeof("{ petId }") - 1;
if(petId == 0){
goto end;
}
Expand Down
Loading

0 comments on commit 99ef14e

Please sign in to comment.