Skip to content

Commit c75edb8

Browse files
authored
Optimise ini_rstrip() (#198)
* Optimised out unnecessary strlen() in ini_rstrip() by passing a pointer to the end of the string (which has already been calculated) instead. * Improved documentation for the ini_rstrip() function.
1 parent 498f34b commit c75edb8

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

ini.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ typedef struct {
4444
size_t num_left;
4545
} ini_parse_string_ctx;
4646

47-
/* Strip whitespace chars off end of given string, in place. Return s. */
48-
static char* ini_rstrip(char* s)
47+
/* Strip whitespace chars off end of given string, in place. end must be a
48+
pointer to the NUL terminator at the end of the string. Return s. */
49+
static char* ini_rstrip(char* s, char* end)
4950
{
50-
char* p = s + strlen(s);
51-
while (p > s && isspace((unsigned char)(*--p)))
52-
*p = '\0';
51+
while (end > s && isspace((unsigned char)(*--end)))
52+
*end = '\0';
5353
return s;
5454
}
5555

@@ -178,7 +178,7 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
178178
start += 3;
179179
}
180180
#endif
181-
start = ini_rstrip(ini_lskip(start));
181+
start = ini_rstrip(ini_lskip(start), line + offset);
182182

183183
if (strchr(INI_START_COMMENT_PREFIXES, *start)) {
184184
/* Start-of-line comment */
@@ -187,9 +187,8 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
187187
else if (*prev_name && *start && start > line) {
188188
#if INI_ALLOW_INLINE_COMMENTS
189189
end = ini_find_chars_or_comment(start, NULL);
190-
if (*end)
191-
*end = '\0';
192-
ini_rstrip(start);
190+
*end = '\0';
191+
ini_rstrip(start, end);
193192
#endif
194193
/* Non-blank line with leading whitespace, treat as continuation
195194
of previous name's value (as per Python configparser). */
@@ -221,15 +220,14 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
221220
end = ini_find_chars_or_comment(start, "=:");
222221
if (*end == '=' || *end == ':') {
223222
*end = '\0';
224-
name = ini_rstrip(start);
223+
name = ini_rstrip(start, end);
225224
value = end + 1;
226225
#if INI_ALLOW_INLINE_COMMENTS
227226
end = ini_find_chars_or_comment(value, NULL);
228-
if (*end)
229-
*end = '\0';
227+
*end = '\0';
230228
#endif
231229
value = ini_lskip(value);
232-
ini_rstrip(value);
230+
ini_rstrip(value, end);
233231

234232
#if INI_ALLOW_MULTILINE
235233
ini_strncpy0(prev_name, name, sizeof(prev_name));
@@ -242,7 +240,7 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
242240
/* No '=' or ':' found on name[=:]value line */
243241
#if INI_ALLOW_NO_VALUE
244242
*end = '\0';
245-
name = ini_rstrip(start);
243+
name = ini_rstrip(start, end);
246244
if (!HANDLER(user, section, name, NULL) && !error)
247245
error = lineno;
248246
#else

0 commit comments

Comments
 (0)