Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions bstring/bstraux.c
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,25 @@ bYDecode(const bstring src)
return out;
}

/* int bSGMLEncode (bstring b)
*
* Change the string into a version that is quotable in SGML (HTML, XML).
*/
int
bSGMLEncode(bstring b)
{
static struct tagbstring fr[4][2] = {
{ bsStatic("&"), bsStatic("&") },
{ bsStatic("\""), bsStatic(""") },
{ bsStatic("<"), bsStatic("&lt;") },
{ bsStatic(">"), bsStatic("&gt;") } };
for (int i = 0; i < 4; i++) {
int ret = bfindreplace(b, &fr[i][0], &fr[i][1], 0);
if (0 > ret) return ret;
}
return 0;
}

bstring
bStrfTime(const char * fmt, const struct tm * timeptr)
{
Expand Down
10 changes: 10 additions & 0 deletions bstring/bstraux.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ bYEncode(const bstring src);
BSTR_PUBLIC bstring
bYDecode(const bstring src);

/**
* Change the string into a version that is quotable in SGML (HTML, XML).
* Replaces &, ", <, > with their SGML entity equivalents.
*
* @param b the bstring to encode in-place
* @return BSTR_OK on success, BSTR_ERR on error
*/
BSTR_PUBLIC int
bSGMLEncode(bstring b);

/* Writable stream */
typedef int
(*bNwrite)(const void *buf, size_t elsize, size_t nelem, void *parm);
Expand Down
28 changes: 28 additions & 0 deletions tests/testaux.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,33 @@
}
END_TEST

START_TEST(core_014)

Check warning on line 486 in tests/testaux.c

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move declarations of types of parameters for function into list of parameters.

See more on https://sonarcloud.io/project/issues?id=msteinert_bstring&issues=AZy_MTakZhNr1N5gF3IB&open=AZy_MTakZhNr1N5gF3IB&pullRequest=147
{
bstring b;

/* All four entities: &, ", <, > */
b = bfromcstr("<\"Hello, you, me, & world\">");
ck_assert_int_eq(bSGMLEncode(b), BSTR_OK);
ck_assert_int_eq(biseqcstr(b, "&lt;&quot;Hello, you, me, &amp; world&quot;&gt;"), 1);
bdestroy(b);

/* No special characters — string should be unchanged */
b = bfromcstr("Hello, world");
ck_assert_int_eq(bSGMLEncode(b), BSTR_OK);
ck_assert_int_eq(biseqcstr(b, "Hello, world"), 1);
bdestroy(b);

/* Empty string */
b = bfromcstr("");
ck_assert_int_eq(bSGMLEncode(b), BSTR_OK);
ck_assert_int_eq(biseqcstr(b, ""), 1);
bdestroy(b);

/* NULL input */
ck_assert_int_eq(bSGMLEncode(NULL), BSTR_ERR);
}
END_TEST

int
main(void)
{
Expand All @@ -504,6 +531,7 @@
tcase_add_test(core, core_011);
tcase_add_test(core, core_012);
tcase_add_test(core, core_013);
tcase_add_test(core, core_014);
suite_add_tcase(suite, core);
/* Run tests */
SRunner *runner = srunner_create(suite);
Expand Down