Skip to content

Commit e57b7bf

Browse files
authored
Merge pull request #6 from choco-technologies/copilot/fix-inline-comments-trimming
Fix inline comment stripping from INI values
2 parents e8428a6 + ffb7bd3 commit e57b7bf

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

apps/test_dmini/test_dmini.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,38 @@ static void test_comments_whitespace(void)
301301
TEST_PASS();
302302
}
303303

304+
/**
305+
* @brief Test: Inline comments are stripped from values
306+
*/
307+
static void test_inline_comments(void)
308+
{
309+
TEST_START("Strip inline comments from values");
310+
311+
const char* ini_data =
312+
"Port=A ; some comment\n"
313+
"key1=value1 # hash comment\n"
314+
"[section1]\n"
315+
"key2 = value2 ; trailing comment\n";
316+
317+
dmini_context_t ctx = dmini_create();
318+
TEST_ASSERT(ctx != NULL, "Failed to create context");
319+
320+
int result = dmini_parse_string(ctx, ini_data);
321+
TEST_ASSERT(result == 0, "Failed to parse string");
322+
323+
const char* val = dmini_get_string(ctx, NULL, "Port", "");
324+
TEST_ASSERT(strcmp(val, "A") == 0, "Inline semicolon comment not stripped");
325+
326+
val = dmini_get_string(ctx, NULL, "key1", "");
327+
TEST_ASSERT(strcmp(val, "value1") == 0, "Inline hash comment not stripped");
328+
329+
val = dmini_get_string(ctx, "section1", "key2", "");
330+
TEST_ASSERT(strcmp(val, "value2") == 0, "Inline comment in section not stripped");
331+
332+
dmini_destroy(ctx);
333+
TEST_PASS();
334+
}
335+
304336
/**
305337
* @brief Main entry point for test application
306338
*/
@@ -320,6 +352,7 @@ int main(int argc, char** argv)
320352
test_generate_string();
321353
test_file_io();
322354
test_comments_whitespace();
355+
test_inline_comments();
323356

324357
// Print summary
325358
Dmod_Printf("\n=== Test Summary ===\n");

src/dmini.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,35 @@ static char* trim_whitespace(char* str)
7171

7272

7373

74+
/**
75+
* @brief Strip inline comment from a value string
76+
*
77+
* Finds the first ';' or '#' character and null-terminates the string there,
78+
* effectively removing the inline comment. Note: quoted strings are not
79+
* supported; a ';' or '#' inside a value will also be treated as a comment
80+
* marker.
81+
*
82+
* @param str Value string to modify in place
83+
*/
84+
static void strip_inline_comment(char* str)
85+
{
86+
if (!str)
87+
{
88+
return;
89+
}
90+
91+
char* p = str;
92+
while (*p)
93+
{
94+
if (*p == ';' || *p == '#')
95+
{
96+
*p = '\0';
97+
break;
98+
}
99+
p++;
100+
}
101+
}
102+
74103
/**
75104
* @brief Compare section names (handles NULL values)
76105
*/
@@ -489,6 +518,7 @@ int dmini_parse_string(dmini_context_t ctx, const char* data)
489518
{
490519
*equals = '\0';
491520
char* key = trim_whitespace(line);
521+
strip_inline_comment(equals + 1);
492522
char* value = trim_whitespace(equals + 1);
493523

494524
if (*key)
@@ -580,6 +610,7 @@ int dmini_parse_file(dmini_context_t ctx, const char* filename)
580610
{
581611
*equals = '\0';
582612
char* key = trim_whitespace(line);
613+
strip_inline_comment(equals + 1);
583614
char* value = trim_whitespace(equals + 1);
584615

585616
if (*key)

0 commit comments

Comments
 (0)