Skip to content

Commit

Permalink
echo: add --trim option (#150)
Browse files Browse the repository at this point in the history
* add --trim option to echo and related test
  • Loading branch information
liquidaty authored Feb 23, 2024
1 parent 1231cbb commit 3acfe72
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
12 changes: 11 additions & 1 deletion app/echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ struct zsv_echo_data {
const char *sql;
} sqlite3;
} o;

unsigned char trim_white:1;
unsigned char _:7;
};

/**
Expand Down Expand Up @@ -86,15 +89,19 @@ static void zsv_echo_row(void *hook) {
if(VERY_UNLIKELY(data->row_ix == 0)) { // header
for(size_t i = 0, j = zsv_cell_count(data->parser); i < j; i++) {
struct zsv_cell cell = zsv_get_cell(data->parser, i);
if(UNLIKELY(data->trim_white))
cell.str = (unsigned char *)zsv_strtrim(cell.str, &cell.len);
zsv_writer_cell(data->csv_writer, i == 0, cell.str, cell.len, cell.quoted);
}
} else {
for(size_t i = 0, j = zsv_cell_count(data->parser); i < j; i++) {
if(data->overwrite.row_ix == data->row_ix && data->overwrite.col_ix == i) {
if(VERY_UNLIKELY(data->overwrite.row_ix == data->row_ix && data->overwrite.col_ix == i)) {
zsv_writer_cell(data->csv_writer, i == 0, data->overwrite.str, data->overwrite.len, 1);
zsv_echo_get_next_overwrite(data);
} else {
struct zsv_cell cell = zsv_get_cell(data->parser, i);
if(UNLIKELY(data->trim_white))
cell.str = (unsigned char *)zsv_strtrim(cell.str, &cell.len);
zsv_writer_cell(data->csv_writer, i == 0, cell.str, cell.len, cell.quoted);
}
}
Expand All @@ -111,6 +118,7 @@ const char *zsv_echo_usage_msg[] = {
"",
"Options:",
" -b : output with BOM",
" --trim : trim whitespace",
" --overwrite <source>: overwrite cells using given source. Source may be:",
" - sqlite3://<filename>[?sql=<query>]",
" ex: sqlite3://overwrites.db?sql=select row, column, value from overwrites order by row, column",
Expand Down Expand Up @@ -206,6 +214,8 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
const char *arg = argv[arg_i];
if(!strcmp(arg, "-b"))
writer_opts.with_bom = 1;
else if(!strcmp(arg, "--trim"))
data.trim_white = 1;
else if(!strcmp(arg, "--overwrite")) {
if(arg_i+1 >= argc) {
fprintf(stderr, "Option %s requires a value\n", arg);
Expand Down
7 changes: 6 additions & 1 deletion app/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ test: ${TESTS}
test-prop:
EXE=${BUILD_DIR}/bin/zsv_prop${EXE} make -C prop test

test-echo : test-echo1 test-echo-overwrite test-echo-eol test-echo-overwrite-csv test-echo-chars
test-echo : test-echo1 test-echo-overwrite test-echo-eol test-echo-overwrite-csv test-echo-chars test-echo-trim

test-echo1: ${BUILD_DIR}/bin/zsv_echo${EXE}
@${TEST_INIT}
Expand All @@ -114,6 +114,11 @@ test-echo-eol-%: ${BUILD_DIR}/bin/zsv_echo${EXE}
@${PREFIX} $< ${TEST_DATA_DIR}/test/no-eol-$*.csv ${REDIRECT} ${TMP_DIR}/$@.out
@${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${TEST_PASS} || ${TEST_FAIL}

test-echo-trim: ${BUILD_DIR}/bin/zsv_echo${EXE}
@${TEST_INIT}
@${PREFIX} $< ${TEST_DATA_DIR}/test/echo-trim.csv ${REDIRECT} ${TMP_DIR}/$@.out
@${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${TEST_PASS} || ${TEST_FAIL}

test-echo-chars: ${BUILD_DIR}/bin/zsv_echo${EXE}
@${TEST_INIT}
@${PREFIX} echo '東京都' | $< -u '?' ${REDIRECT} ${TMP_DIR}/$@.out
Expand Down
2 changes: 2 additions & 0 deletions app/test/expected/test-echo-trim.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hi, there
how ,are, you,?
2 changes: 2 additions & 0 deletions data/test/echo-trim.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hi, there
how ,are, you,?

0 comments on commit 3acfe72

Please sign in to comment.