Skip to content

Commit 7d87ff0

Browse files
committed
[C][Client] Reduce number of unnecessary strlen() calls
1 parent 42516f6 commit 7d87ff0

File tree

12 files changed

+129
-249
lines changed

12 files changed

+129
-249
lines changed

modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ end:
108108
apiClient->response_code = 0;
109109

110110
// create the path
111-
long sizeOfPath = strlen("{{{path}}}")+1;
112-
char *localVarPath = malloc(sizeOfPath);
113-
snprintf(localVarPath, sizeOfPath, "{{{path}}}");
111+
char *localVarPath = strdup("{{{path}}}");
114112

115113
{{#pathParams}}
116114
{{#isString}}
@@ -126,7 +124,7 @@ end:
126124
{{#pathParams}}
127125

128126
// Path Params
129-
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}} }");
127+
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;
130128
{{#isNumeric}}
131129
if({{paramName}} == 0){
132130
goto end;

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,11 @@ void sslConfig_free(sslConfig_t *sslConfig) {
173173
free(sslConfig);
174174
}
175175

176-
static void replaceSpaceWithPlus(char *stringToProcess) {
177-
for(int i = 0; i < strlen(stringToProcess); i++) {
178-
if(stringToProcess[i] == ' ') {
179-
stringToProcess[i] = '+';
176+
static void replaceSpaceWithPlus(char *str) {
177+
if (str) {
178+
for (; *str; str++) {
179+
if (*str == ' ')
180+
*str = '+';
180181
}
181182
}
182183
}
@@ -288,9 +289,9 @@ void apiClient_invoke(apiClient_t *apiClient,
288289
list_ForEach(listEntry, headerType) {
289290
if(strstr(listEntry->data, "xml") == NULL)
290291
{
291-
buffHeader = malloc(strlen("Accept: ") +
292-
strlen(listEntry->data) + 1);
293-
sprintf(buffHeader, "%s%s", "Accept: ",
292+
buffHeader = malloc(sizeof("Accept: ") +
293+
strlen(listEntry->data));
294+
sprintf(buffHeader, "Accept: %s",
294295
(char *) listEntry->data);
295296
headers = curl_slist_append(headers, buffHeader);
296297
free(buffHeader);
@@ -301,9 +302,9 @@ void apiClient_invoke(apiClient_t *apiClient,
301302
list_ForEach(listEntry, contentType) {
302303
if(strstr(listEntry->data, "xml") == NULL)
303304
{
304-
buffContent = malloc(strlen("Content-Type: ") +
305-
strlen(listEntry->data) + 1);
306-
sprintf(buffContent, "%s%s", "Content-Type: ",
305+
buffContent = malloc(sizeof("Content-Type: ") +
306+
strlen(listEntry->data));
307+
sprintf(buffContent, "Content-Type: %s",
307308
(char *) listEntry->data);
308309
headers = curl_slist_append(headers, buffContent);
309310
free(buffContent);

samples/client/others/c/bearerAuth/api/DefaultAPI.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ DefaultAPI_privateGet(apiClient_t *apiClient)
2626
apiClient->response_code = 0;
2727

2828
// create the path
29-
long sizeOfPath = strlen("/private")+1;
30-
char *localVarPath = malloc(sizeOfPath);
31-
snprintf(localVarPath, sizeOfPath, "/private");
29+
char *localVarPath = strdup("/private");
3230

3331

3432

@@ -98,9 +96,7 @@ DefaultAPI_publicGet(apiClient_t *apiClient)
9896
apiClient->response_code = 0;
9997

10098
// create the path
101-
long sizeOfPath = strlen("/public")+1;
102-
char *localVarPath = malloc(sizeOfPath);
103-
snprintf(localVarPath, sizeOfPath, "/public");
99+
char *localVarPath = strdup("/public");
104100

105101

106102

@@ -170,9 +166,7 @@ DefaultAPI_usersGet(apiClient_t *apiClient)
170166
apiClient->response_code = 0;
171167

172168
// create the path
173-
long sizeOfPath = strlen("/users")+1;
174-
char *localVarPath = malloc(sizeOfPath);
175-
snprintf(localVarPath, sizeOfPath, "/users");
169+
char *localVarPath = strdup("/users");
176170

177171

178172

samples/client/others/c/bearerAuth/src/apiClient.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ void sslConfig_free(sslConfig_t *sslConfig) {
8989
free(sslConfig);
9090
}
9191

92-
static void replaceSpaceWithPlus(char *stringToProcess) {
93-
for(int i = 0; i < strlen(stringToProcess); i++) {
94-
if(stringToProcess[i] == ' ') {
95-
stringToProcess[i] = '+';
92+
static void replaceSpaceWithPlus(char *str) {
93+
if (str) {
94+
for (; *str; str++) {
95+
if (*str == ' ')
96+
*str = '+';
9697
}
9798
}
9899
}
@@ -204,9 +205,9 @@ void apiClient_invoke(apiClient_t *apiClient,
204205
list_ForEach(listEntry, headerType) {
205206
if(strstr(listEntry->data, "xml") == NULL)
206207
{
207-
buffHeader = malloc(strlen("Accept: ") +
208-
strlen(listEntry->data) + 1);
209-
sprintf(buffHeader, "%s%s", "Accept: ",
208+
buffHeader = malloc(sizeof("Accept: ") +
209+
strlen(listEntry->data));
210+
sprintf(buffHeader, "Accept: %s",
210211
(char *) listEntry->data);
211212
headers = curl_slist_append(headers, buffHeader);
212213
free(buffHeader);
@@ -217,9 +218,9 @@ void apiClient_invoke(apiClient_t *apiClient,
217218
list_ForEach(listEntry, contentType) {
218219
if(strstr(listEntry->data, "xml") == NULL)
219220
{
220-
buffContent = malloc(strlen("Content-Type: ") +
221-
strlen(listEntry->data) + 1);
222-
sprintf(buffContent, "%s%s", "Content-Type: ",
221+
buffContent = malloc(sizeof("Content-Type: ") +
222+
strlen(listEntry->data));
223+
sprintf(buffContent, "Content-Type: %s",
223224
(char *) listEntry->data);
224225
headers = curl_slist_append(headers, buffContent);
225226
free(buffContent);

samples/client/petstore/c-useJsonUnformatted/api/PetAPI.c

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ PetAPI_addPet(apiClient_t *apiClient, pet_t *body)
6767
apiClient->response_code = 0;
6868

6969
// create the path
70-
long sizeOfPath = strlen("/pet")+1;
71-
char *localVarPath = malloc(sizeOfPath);
72-
snprintf(localVarPath, sizeOfPath, "/pet");
70+
char *localVarPath = strdup("/pet");
7371

7472

7573

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

141139
// create the path
142-
long sizeOfPath = strlen("/pet/{petId}")+1;
143-
char *localVarPath = malloc(sizeOfPath);
144-
snprintf(localVarPath, sizeOfPath, "/pet/{petId}");
140+
char *localVarPath = strdup("/pet/{petId}");
145141

146142

147143

148144
// Path Params
149-
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
145+
long sizeOfPathParams_petId = sizeof(petId)+3 + sizeof("{ petId }") - 1;
150146
if(petId == 0){
151147
goto end;
152148
}
@@ -232,9 +228,7 @@ PetAPI_findPetsByStatus(apiClient_t *apiClient, list_t *status)
232228
apiClient->response_code = 0;
233229

234230
// create the path
235-
long sizeOfPath = strlen("/pet/findByStatus")+1;
236-
char *localVarPath = malloc(sizeOfPath);
237-
snprintf(localVarPath, sizeOfPath, "/pet/findByStatus");
231+
char *localVarPath = strdup("/pet/findByStatus");
238232

239233

240234

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

327321
// create the path
328-
long sizeOfPath = strlen("/pet/findByTags")+1;
329-
char *localVarPath = malloc(sizeOfPath);
330-
snprintf(localVarPath, sizeOfPath, "/pet/findByTags");
322+
char *localVarPath = strdup("/pet/findByTags");
331323

332324

333325

@@ -416,9 +408,7 @@ PetAPI_getDaysWithoutIncident(apiClient_t *apiClient)
416408
apiClient->response_code = 0;
417409

418410
// create the path
419-
long sizeOfPath = strlen("/store/daysWithoutIncident")+1;
420-
char *localVarPath = malloc(sizeOfPath);
421-
snprintf(localVarPath, sizeOfPath, "/store/daysWithoutIncident");
411+
char *localVarPath = strdup("/store/daysWithoutIncident");
422412

423413

424414

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

483473
// create the path
484-
long sizeOfPath = strlen("/pet/{petId}")+1;
485-
char *localVarPath = malloc(sizeOfPath);
486-
snprintf(localVarPath, sizeOfPath, "/pet/{petId}");
474+
char *localVarPath = strdup("/pet/{petId}");
487475

488476

489477

490478
// Path Params
491-
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
479+
long sizeOfPathParams_petId = sizeof(petId)+3 + sizeof("{ petId }") - 1;
492480
if(petId == 0){
493481
goto end;
494482
}
@@ -575,9 +563,7 @@ PetAPI_getPicture(apiClient_t *apiClient)
575563
apiClient->response_code = 0;
576564

577565
// create the path
578-
long sizeOfPath = strlen("/pet/picture")+1;
579-
char *localVarPath = malloc(sizeOfPath);
580-
snprintf(localVarPath, sizeOfPath, "/pet/picture");
566+
char *localVarPath = strdup("/pet/picture");
581567

582568

583569

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

640626
// create the path
641-
long sizeOfPath = strlen("/pet/{petId}/isAvailable")+1;
642-
char *localVarPath = malloc(sizeOfPath);
643-
snprintf(localVarPath, sizeOfPath, "/pet/{petId}/isAvailable");
627+
char *localVarPath = strdup("/pet/{petId}/isAvailable");
644628

645629

646630

647631
// Path Params
648-
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
632+
long sizeOfPathParams_petId = sizeof(petId)+3 + sizeof("{ petId }") - 1;
649633
if(petId == 0){
650634
goto end;
651635
}
@@ -723,9 +707,7 @@ PetAPI_sharePicture(apiClient_t *apiClient, binary_t* picture)
723707
apiClient->response_code = 0;
724708

725709
// create the path
726-
long sizeOfPath = strlen("/pet/picture")+1;
727-
char *localVarPath = malloc(sizeOfPath);
728-
snprintf(localVarPath, sizeOfPath, "/pet/picture");
710+
char *localVarPath = strdup("/pet/picture");
729711

730712

731713

@@ -794,9 +776,7 @@ PetAPI_specialtyPet(apiClient_t *apiClient)
794776
apiClient->response_code = 0;
795777

796778
// create the path
797-
long sizeOfPath = strlen("/pet/specialty")+1;
798-
char *localVarPath = malloc(sizeOfPath);
799-
snprintf(localVarPath, sizeOfPath, "/pet/specialty");
779+
char *localVarPath = strdup("/pet/specialty");
800780

801781

802782

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

867847
// create the path
868-
long sizeOfPath = strlen("/pet")+1;
869-
char *localVarPath = malloc(sizeOfPath);
870-
snprintf(localVarPath, sizeOfPath, "/pet");
848+
char *localVarPath = strdup("/pet");
871849

872850

873851

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

947925
// create the path
948-
long sizeOfPath = strlen("/pet/{petId}")+1;
949-
char *localVarPath = malloc(sizeOfPath);
950-
snprintf(localVarPath, sizeOfPath, "/pet/{petId}");
926+
char *localVarPath = strdup("/pet/{petId}");
951927

952928

953929

954930
// Path Params
955-
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
931+
long sizeOfPathParams_petId = sizeof(petId)+3 + sizeof("{ petId }") - 1;
956932
if(petId == 0){
957933
goto end;
958934
}
@@ -1058,14 +1034,12 @@ PetAPI_uploadFile(apiClient_t *apiClient, long petId, char *additionalMetadata,
10581034
apiClient->response_code = 0;
10591035

10601036
// create the path
1061-
long sizeOfPath = strlen("/pet/{petId}/uploadImage")+1;
1062-
char *localVarPath = malloc(sizeOfPath);
1063-
snprintf(localVarPath, sizeOfPath, "/pet/{petId}/uploadImage");
1037+
char *localVarPath = strdup("/pet/{petId}/uploadImage");
10641038

10651039

10661040

10671041
// Path Params
1068-
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
1042+
long sizeOfPathParams_petId = sizeof(petId)+3 + sizeof("{ petId }") - 1;
10691043
if(petId == 0){
10701044
goto end;
10711045
}

0 commit comments

Comments
 (0)