Skip to content

Commit

Permalink
select: fix -x option to work properly in conjunction with -n
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidaty committed Sep 30, 2023
1 parent 917c7c6 commit f9fce07
Show file tree
Hide file tree
Showing 4 changed files with 567 additions and 11 deletions.
28 changes: 24 additions & 4 deletions app/select-pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,30 @@ static inline unsigned char *zsv_select_get_header_name(struct zsv_select_data *
static inline char zsv_select_excluded_current_header_name(struct zsv_select_data *data, unsigned in_ix) {
if(data->exclusion_count) {
unsigned char *header_name = zsv_select_get_header_name(data, in_ix);
if(header_name) {
for(unsigned int i = 0; i < data->exclusion_count; i++)
if(!zsv_stricmp(header_name, data->exclusions[i]))
return 1;
if(data->use_header_indexes) {
for(unsigned int ix = 0; ix < data->exclusion_count; ix++) {
unsigned i, j;
switch(zsv_select_column_index_selection(data->exclusions[ix], &i, &j)) {
case zsv_select_column_index_selection_type_none:
// not expected!
break;
case zsv_select_column_index_selection_type_single:
if(in_ix + 1 == i) return 1;
break;
case zsv_select_column_index_selection_type_range:
if(i <= in_ix + 1 && in_ix + 1 <= j) return 1;
break;
case zsv_select_column_index_selection_type_lower_bounded:
if(i <= in_ix + 1) return 1;
break;
}
}
} else {
if(header_name) {
for(unsigned int i = 0; i < data->exclusion_count; i++)
if(!zsv_stricmp(header_name, data->exclusions[i]))
return 1;
}
}
}
return 0;
Expand Down
31 changes: 25 additions & 6 deletions app/select.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,31 @@ static inline unsigned char *zsv_select_get_header_name(struct zsv_select_data *

static inline char zsv_select_excluded_current_header_name(struct zsv_select_data *data, unsigned in_ix) {
if(data->exclusion_count) {
// to do: cache this result
unsigned char *header_name = zsv_select_get_header_name(data, in_ix);
if(header_name) {
for(unsigned int i = 0; i < data->exclusion_count; i++)
if(!zsv_stricmp(header_name, data->exclusions[i]))
return 1;
if(data->use_header_indexes) {
for(unsigned int ix = 0; ix < data->exclusion_count; ix++) {
unsigned i, j;
switch(zsv_select_column_index_selection(data->exclusions[ix], &i, &j)) {
case zsv_select_column_index_selection_type_none:
// not expected!
break;
case zsv_select_column_index_selection_type_single:
if(in_ix + 1 == i) return 1;
break;
case zsv_select_column_index_selection_type_range:
if(i <= in_ix + 1 && in_ix + 1 <= j) return 1;
break;
case zsv_select_column_index_selection_type_lower_bounded:
if(i <= in_ix + 1) return 1;
break;
}
}
} else {
unsigned char *header_name = zsv_select_get_header_name(data, in_ix);
if(header_name) {
for(unsigned int i = 0; i < data->exclusion_count; i++)
if(!zsv_stricmp(header_name, data->exclusions[i]))
return 1;
}
}
}
return 0;
Expand Down
7 changes: 6 additions & 1 deletion app/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ test-2-count test-2-count-pull: ${BUILD_DIR}/bin/zsv_count${EXE} ${TEST_DATA_DIR
@for x in 5000 5002 5004 5006 5008 5010 5013 5015 5017 5019 5021 5101 5105 5111 5113 5115 5117 5119 5121 5123 5125 5127 5129 5131 5211 5213 5215 5217 5311 5313 5315 5317 5413 5431 5433 5455 6133 ; do $< -r $$x ${TEST_DATA_DIR}/test/buffsplit_quote.csv ; done > ${TMP_DIR}/$@.out
@${CMP} ${TMP_DIR}/$@.out expected/test-2-count.out && ${TEST_PASS} || ${TEST_FAIL}

test-select test-select-pull: test-% : test-n-% test-6-% test-7-% test-8-% test-9-% test-10-% test-quotebuff-% test-fixed-1-% test-fixed-2-% test-fixed-3-% test-fixed-4-% test-merge-%
test-select test-select-pull: test-% : test-n-% test-6-% test-7-% test-8-% test-9-% test-10-% test-11-% test-12-% test-quotebuff-% test-fixed-1-% test-fixed-2-% test-fixed-3-% test-fixed-4-% test-merge-%

test-merge-select test-merge-select-pull: test-merge-% : ${BUILD_DIR}/bin/zsv_%${EXE}
@${TEST_INIT}
Expand Down Expand Up @@ -224,6 +224,11 @@ test-11-select test-11-select-pull: test-11-% : ${BUILD_DIR}/bin/zsv_%${EXE}
@${PREFIX} (echo "A1,B1" | $< --header-row "column1,column2") > /tmp/$@.out
@cmp /tmp/$@.out expected/test-11-select.out && ${TEST_PASS} || ${TEST_FAIL}

test-12-select test-12-select-pull: test-12-% : ${BUILD_DIR}/bin/zsv_%${EXE}
@${TEST_INIT}
@${PREFIX} $< -n -x 1-2 -x 6 -x 8-10 -x 13- ${TEST_DATA_DIR}/test/desc.csv > /tmp/$@.out
@${CMP} /tmp/$@.out expected/test-12-select.out && ${TEST_PASS} || ${TEST_FAIL}

test-fixed-1-select test-fixed-1-select-pull: ${BUILD_DIR}/bin/zsv_select${EXE}
@${TEST_INIT}
@${PREFIX} $< ${TEST_DATA_DIR}/fixed.csv --fixed 3,7,12,18,20,21,22 ${REDIRECT} ${TMP_DIR}/$@.out
Expand Down
Loading

0 comments on commit f9fce07

Please sign in to comment.