fix UTF-8 alignment for pprint and xtab formats

This commit is contained in:
John Kerl 2015-08-27 22:15:12 -04:00
parent f761f33c51
commit 2de70a96da
8 changed files with 102 additions and 10 deletions

View file

@ -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);
}

View file

@ -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);
@ -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", max_widths[j], value);
} else {
fprintf(output_stream, "%s", 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);

View file

@ -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
}

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,4 @@
191º test
191 test2
francois français
françois francais

View file

@ -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

View file

@ -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