Skip to content

Commit

Permalink
fix UTF-8 alignment for pprint and xtab formats
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Aug 28, 2015
1 parent f761f33 commit 2de70a9
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 10 deletions.
3 changes: 2 additions & 1 deletion c/mapping/mapper_cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

// ----------------------------------------------------------------
static sllv_t* mapper_cat_process(lrec_t* pinrec, context_t* pctx, void* pvstate) {
if (pinrec != NULL)
if (pinrec != NULL) {
return sllv_single(pinrec);
}
else
return sllv_single(NULL);
}
Expand Down
31 changes: 23 additions & 8 deletions c/output/lrec_writer_pprint.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,20 @@ static void print_and_free_record_list(sllv_t* precords, FILE* output_stream, in
fputc(' ', output_stream);
}
if (left_align) {
if (pe->pnext == NULL)
if (pe->pnext == NULL) {
fprintf(output_stream, "%s", pe->key);
else
fprintf(output_stream, "%-*s", max_widths[j], pe->key);
} else {
// "%-*s" fprintf format isn't correct for non-ASCII UTF-8
fprintf(output_stream, "%s", pe->key);
int d = max_widths[j] - strlen_for_utf8_display(pe->key);
for (int i = 0; i < d; i++)
fputc(' ', output_stream);
}
} else {
fprintf(output_stream, "%*s", max_widths[j], pe->key);
int d = max_widths[j] - strlen_for_utf8_display(pe->key);
for (int i = 0; i < d; i++)
fputc(' ', output_stream);
fprintf(output_stream, "%s", pe->key);
}
}
fputc('\n', output_stream);
Expand All @@ -99,12 +107,19 @@ static void print_and_free_record_list(sllv_t* precords, FILE* output_stream, in
if (*value == 0) // empty string
value = "-";
if (left_align) {
if (pe->pnext == NULL)
if (pe->pnext == NULL) {
fprintf(output_stream, "%s", value);
} else {
fprintf(output_stream, "%s", value);
else
fprintf(output_stream, "%-*s", max_widths[j], value);
int d = max_widths[j] - strlen_for_utf8_display(value);
for (int i = 0; i < d; i++)
fputc(' ', output_stream);
}
} else {
fprintf(output_stream, "%*s", max_widths[j], value);
int d = max_widths[j] - strlen_for_utf8_display(value);
for (int i = 0; i < d; i++)
fputc(' ', output_stream);
fprintf(output_stream, "%s", value);
}
}
fputc('\n', output_stream);
Expand Down
7 changes: 6 additions & 1 deletion c/output/lrec_writer_xtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ static void lrec_writer_xtab_process(FILE* output_stream, lrec_t* prec, void* pv
}

for (lrece_t* pe = prec->phead; pe != NULL; pe = pe->pnext) {
fprintf(output_stream, "%-*s %s\n", max_key_width, pe->key, pe->value);
// "%-*s" fprintf format isn't correct for non-ASCII UTF-8
fprintf(output_stream, "%s", pe->key);
int d = max_key_width - strlen_for_utf8_display(pe->key);
for (int i = 0; i < d; i++)
fputc(' ', output_stream);
fprintf(output_stream, " %s\n", pe->value);
}
lrec_free(prec); // xxx cmt mem-mgmt
}
Expand Down
30 changes: 30 additions & 0 deletions c/test/expected/out
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,36 @@ français langue
françois nom
vendredi jour

./test/../mlr --inidx --ifs space --opprint cat test/input/utf8-align.nidx
1 2
191º test
191 test2
francois français
françois francais

./test/../mlr --inidx --ifs space --opprint --right cat test/input/utf8-align.nidx
1 2
191º test
191 test2
francois français
françois francais

./test/../mlr --oxtab cat test/input/utf8-align.dkvp
191º test
1912 test2
cois çais
çois cais

191º test
1912 test2
ois çais
çois cais

191º test
1912 test2
coise çais
çois cais


================================================================
RFC-CSV
Expand Down
3 changes: 3 additions & 0 deletions c/test/input/utf8-align.dkvp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
191º=test,1912=test2,cois=çais,çois=cais
191º=test,1912=test2,ois=çais,çois=cais
191º=test,1912=test2,coise=çais,çois=cais
4 changes: 4 additions & 0 deletions c/test/input/utf8-align.nidx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
191º test
191 test2
francois français
françois francais
30 changes: 30 additions & 0 deletions c/test/output/out
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,36 @@ français langue
françois nom
vendredi jour

./test/../mlr --inidx --ifs space --opprint cat test/input/utf8-align.nidx
1 2
191º test
191 test2
francois français
françois francais

./test/../mlr --inidx --ifs space --opprint --right cat test/input/utf8-align.nidx
1 2
191º test
191 test2
francois français
françois francais

./test/../mlr --oxtab cat test/input/utf8-align.dkvp
191º test
1912 test2
cois çais
çois cais

191º test
1912 test2
ois çais
çois cais

191º test
1912 test2
coise çais
çois cais


================================================================
RFC-CSV
Expand Down
4 changes: 4 additions & 0 deletions c/test/run
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ run_command $mlr --icsvlite --opprint cat test/input/utf8-2.csv
run_command $mlr --icsvlite --oxtab cat test/input/utf8-1.csv
run_command $mlr --icsvlite --oxtab cat test/input/utf8-2.csv

run_command $mlr --inidx --ifs space --opprint cat test/input/utf8-align.nidx
run_command $mlr --inidx --ifs space --opprint --right cat test/input/utf8-align.nidx
run_command $mlr --oxtab cat test/input/utf8-align.dkvp

# ================================================================
announce RFC-CSV

Expand Down

0 comments on commit 2de70a9

Please sign in to comment.