690
*-----------------------------------------------------------------------------
694
* Use backslash character as an escape character to handle comma
695
* character escaping.
698
* Returns a newly allocated string with comma and backslash characters
702
* None. It is a caller responsibility to deallocate the result.
704
*-----------------------------------------------------------------------------
708
Escape_Comma(const char *string) // IN
712
if (NULL == string) {
718
for (; *string; ++string) {
721
if (c == ',' || c == '\\') {
722
if (!DynBuf_Append(&b, "\\", 1)) {
726
if (!DynBuf_Append(&b, string, 1)) {
731
DynBuf_Append(&b, string, 1);
733
return DynBuf_Get(&b);
742
/* Unit test suite for Escape_Comma() */
751
{ "123# ", "123# ", },
752
{ "123,", "123\\,", },
753
{ "'123\\", "'123\\\\", },
757
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
760
out = Escape_Comma(tests[i].in);
761
if (strcmp(out, tests[i].out) != 0) {
762
printf("test %u failed: \"%s\" : \"%s\"\n", i, tests[i].in, out);
768
printf("all tests passed\n");