From adb83e3aca11769f70b68c79b761e2872e2f2484 Mon Sep 17 00:00:00 2001 From: liquidaty Date: Tue, 7 Mar 2023 19:46:40 -0800 Subject: [PATCH] pretty+markdown: escape backslash and vertical bar --- app/pretty.c | 20 ++++++++++++++++++- app/test/Makefile | 15 +++++++++++++- .../{test-pretty.out => test-pretty-1.out} | 0 .../expected/test-pretty-escape-chars.out | 3 +++ data/test/pretty-escape.csv | 2 ++ 5 files changed, 38 insertions(+), 2 deletions(-) rename app/test/expected/{test-pretty.out => test-pretty-1.out} (100%) create mode 100644 app/test/expected/test-pretty-escape-chars.out create mode 100644 data/test/pretty-escape.csv diff --git a/app/pretty.c b/app/pretty.c index 7d7d911b..3d426c04 100644 --- a/app/pretty.c +++ b/app/pretty.c @@ -293,7 +293,25 @@ void zsv_pretty_write_cell(unsigned char *buff, size_t bytes, struct zsv_pretty_ &used_width, &utf8_err); } if(bytes_to_print) { - data->write(buff, 1, bytes_to_print, data->write_arg); + if((data->markdown || data->markdown_pad) + && (memchr(buff, '|', bytes_to_print) + || memchr(buff, '\\', bytes_to_print)) + ) { + char *tmp = malloc(bytes_to_print*2); + if(!tmp) + data->parser_status = zsv_status_memory; + else { + size_t tmp_len = 0; + for(size_t i = 0; i < bytes_to_print; i++) { + if(memchr("|\\", buff[i], 2)) + tmp[tmp_len++] = '\\'; + tmp[tmp_len++] = buff[i]; + } + data->write(tmp, 1, tmp_len, data->write_arg); + free(tmp); + } + } else + data->write(buff, 1, bytes_to_print, data->write_arg); data->line.printed += used_width; if(ellipsis) { diff --git a/app/test/Makefile b/app/test/Makefile index fb883ad8..0621d4d3 100644 --- a/app/test/Makefile +++ b/app/test/Makefile @@ -258,12 +258,25 @@ test-stack2: ${BUILD_DIR}/bin/zsv_stack${EXE} @${PREFIX} $< ${TEST_DATA_DIR}/stack2-[12].csv ${REDIRECT} ${TMP_DIR}/$@.out @${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${TEST_PASS} || ${TEST_FAIL} -test-sql test-flatten test-pretty : test-%: ${BUILD_DIR}/bin/zsv_%${EXE} +test-sql test-flatten : test-%: ${BUILD_DIR}/bin/zsv_%${EXE} @${TEST_INIT} @( ( ! [ -s "${TEST_DATA_DIR}/test/$*.csv" ] ) && echo "No test input for $*") || \ (${PREFIX} $< ${ARGS-$*} < ${TEST_DATA_DIR}/test/$*.csv ${REDIRECT1} ${TMP_DIR}/$@.out && \ ${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${TEST_PASS} || ${TEST_FAIL}) +test-pretty: test-pretty-1 test-pretty-escape-chars + +test-pretty-1 : test-%-1: ${BUILD_DIR}/bin/zsv_%${EXE} + @${TEST_INIT} + @( ( ! [ -s "${TEST_DATA_DIR}/test/$*.csv" ] ) && echo "No test input for $*") || \ + (${PREFIX} $< ${ARGS-$*} < ${TEST_DATA_DIR}/test/$*.csv ${REDIRECT1} ${TMP_DIR}/$@.out && \ + ${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${TEST_PASS} || ${TEST_FAIL}) + +test-pretty-escape-chars: ${BUILD_DIR}/bin/zsv_pretty${EXE} + @${TEST_INIT} + @(${PREFIX} $< ${ARGS-$*} < ${TEST_DATA_DIR}/test/pretty-escape.csv -M ${REDIRECT1} ${TMP_DIR}/$@.out && \ + ${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${TEST_PASS} || ${TEST_FAIL}) + test-2tsv: test-2tsv-1 test-2tsv-2 test-2tsv-1 test-2tsv-2: test-% : ${BUILD_DIR}/bin/zsv_2tsv${EXE} diff --git a/app/test/expected/test-pretty.out b/app/test/expected/test-pretty-1.out similarity index 100% rename from app/test/expected/test-pretty.out rename to app/test/expected/test-pretty-1.out diff --git a/app/test/expected/test-pretty-escape-chars.out b/app/test/expected/test-pretty-escape-chars.out new file mode 100644 index 00000000..58f52659 --- /dev/null +++ b/app/test/expected/test-pretty-escape-chars.out @@ -0,0 +1,3 @@ +|hi |t\|\\here| +|------|-------| +|how\|xx|are | diff --git a/data/test/pretty-escape.csv b/data/test/pretty-escape.csv new file mode 100644 index 00000000..bbfe42c6 --- /dev/null +++ b/data/test/pretty-escape.csv @@ -0,0 +1,2 @@ +hi,t|\here +how|xx,are