Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

echo: add --trim option #150

Merged
merged 2 commits into from
Feb 23, 2024
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
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}/[email protected]
@${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${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}/[email protected]
@${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL}

test-echo-chars: ${BUILD_DIR}/bin/zsv_echo${EXE}
@${TEST_INIT}
@${PREFIX} echo '東京都' | $< -u '?' ${REDIRECT} ${TMP_DIR}/[email protected]
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,?
Loading