Skip to content

Commit

Permalink
Merge pull request #5307 from larsewi/raw-3.18.x
Browse files Browse the repository at this point in the history
ENT-4277: Added RvalWriteRaw function that doesn't escape quotes (3.18.x)
  • Loading branch information
larsewi authored Aug 14, 2023
2 parents 397765a + c6a8d61 commit 47ca280
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion libpromises/fncall.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void FnCallWrite(Writer *writer, const FnCall *call)
switch (rp->val.type)
{
case RVAL_TYPE_SCALAR:
ScalarWrite(writer, RlistScalarValue(rp), true);
ScalarWrite(writer, RlistScalarValue(rp), true, false);
break;

case RVAL_TYPE_FNCALL:
Expand Down
2 changes: 1 addition & 1 deletion libpromises/policy.c
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,7 @@ void BundleToString(Writer *writer, Bundle *bundle)
}

IndentPrint(writer, 2);
ScalarWrite(writer, pp->promiser, true);
ScalarWrite(writer, pp->promiser, true, false);

/* FIX: add support
*
Expand Down
17 changes: 11 additions & 6 deletions libpromises/rlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -1339,15 +1339,15 @@ void RlistWrite(Writer *writer, const Rlist *list)
WriterWriteChar(writer, '}');
}

void ScalarWrite(Writer *writer, const char *s, bool quote)
void ScalarWrite(Writer *writer, const char *s, bool quote, bool raw)
{
if (quote)
{
WriterWriteChar(writer, '"');
}
for (; *s; s++)
{
if (*s == '"')
if (*s == '"' && !raw)
{
WriterWriteChar(writer, '\\');
}
Expand All @@ -1359,7 +1359,7 @@ void ScalarWrite(Writer *writer, const char *s, bool quote)
}
}

static void RvalWriteParts(Writer *writer, const void* item, RvalType type, bool quote)
static void RvalWriteParts(Writer *writer, const void* item, RvalType type, bool quote, bool raw)
{
if (item == NULL)
{
Expand All @@ -1369,7 +1369,7 @@ static void RvalWriteParts(Writer *writer, const void* item, RvalType type, bool
switch (type)
{
case RVAL_TYPE_SCALAR:
ScalarWrite(writer, item, quote);
ScalarWrite(writer, item, quote, raw);
break;

case RVAL_TYPE_LIST:
Expand All @@ -1392,12 +1392,17 @@ static void RvalWriteParts(Writer *writer, const void* item, RvalType type, bool

void RvalWrite(Writer *writer, Rval rval)
{
RvalWriteParts(writer, rval.item, rval.type, false);
RvalWriteParts(writer, rval.item, rval.type, false, false);
}

void RvalWriteQuoted(Writer *writer, Rval rval)
{
RvalWriteParts(writer, rval.item, rval.type, true);
RvalWriteParts(writer, rval.item, rval.type, true, false);
}

void RvalWriteRaw(Writer *writer, Rval rval)
{
RvalWriteParts(writer, rval.item, rval.type, false, true);
}

char *RvalToString(Rval rval)
Expand Down
3 changes: 2 additions & 1 deletion libpromises/rlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ char *RvalToString(Rval rval);
char *RlistToString(const Rlist *rlist);
void RvalWrite(Writer *writer, Rval rval);
void RvalWriteQuoted(Writer *writer, Rval rval);
void RvalWriteRaw(Writer *writer, Rval rval);
unsigned RvalHash(Rval rval, unsigned seed);

Rlist *RlistCopy(const Rlist *list);
Expand Down Expand Up @@ -109,7 +110,7 @@ void RlistWrite(Writer *writer, const Rlist *list);
Rlist *RlistLast(Rlist *start);
void RlistFilter(Rlist **list, bool (*KeepPredicate)(void *item, void *predicate_data), void *predicate_user_data, void (*DestroyItem)(void *item));
void RlistReverse(Rlist **list);
void ScalarWrite(Writer *w, const char *s, bool quote);
void ScalarWrite(Writer *w, const char *s, bool quote, bool raw);
void RlistFlatten(EvalContext *ctx, Rlist **list);
bool RlistEqual (const Rlist *list1, const Rlist *list2);
bool RlistEqual_untyped(const void *list1, const void *list2);
Expand Down
35 changes: 34 additions & 1 deletion tests/unit/rlist_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,36 @@ static void test_regex_split_overlapping_delimiters()
RlistDestroy(list);
}

static void test_rval_write()
{
Rval rval = { "\"Hello World!\"", RVAL_TYPE_SCALAR };
Writer *writer = StringWriter();
RvalWrite(writer, rval);
const char *actual = StringWriterData(writer);
assert_string_equal(actual, "\\\"Hello World!\\\"");
WriterClose(writer);
}

static void test_rval_write_quoted()
{
Rval rval = { "\"Hello World!\"", RVAL_TYPE_SCALAR };
Writer *writer = StringWriter();
RvalWriteQuoted(writer, rval);
const char *actual = StringWriterData(writer);
assert_string_equal(actual, "\"\\\"Hello World!\\\"\"");
WriterClose(writer);
}

static void test_rval_write_raw()
{
Rval rval = { "\"Hello World!\"", RVAL_TYPE_SCALAR };
Writer *writer = StringWriter();
RvalWriteRaw(writer, rval);
const char *actual = StringWriterData(writer);
assert_string_equal(actual, "\"Hello World!\"");
WriterClose(writer);
}

int main()
{
PRINT_TEST_BANNER();
Expand Down Expand Up @@ -744,7 +774,10 @@ int main()
unit_test(test_regex_split_no_match),
unit_test(test_regex_split_adjacent_separators),
unit_test(test_regex_split_real_regex),
unit_test(test_regex_split_overlapping_delimiters)
unit_test(test_regex_split_overlapping_delimiters),
unit_test(test_rval_write),
unit_test(test_rval_write_quoted),
unit_test(test_rval_write_raw),
};

return run_tests(tests);
Expand Down

0 comments on commit 47ca280

Please sign in to comment.