--- libutf/utf.c | 12 ++++++++++++ libutf/utftorunestr.c | 12 ++++++++++++ paste.c | 27 +++++++++++++++------------ utf.h | 4 +++- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/libutf/utf.c b/libutf/utf.c index 897c5ef..fc78f29 100644 --- a/libutf/utf.c +++ b/libutf/utf.c _AT_@ -62,6 +62,18 @@ utfnlen(const char *s, size_t len) return i; } +size_t +utfmemlen(const char *s, size_t len) +{ + const char *p = s, *end = s + len; + size_t i; + Rune r; + + for(i = 0; p < end; i++) + p += charntorune(&r, p, end - p); + return i; +} + char * utfrune(const char *s, Rune r) { diff --git a/libutf/utftorunestr.c b/libutf/utftorunestr.c index 005fe8a..d350c77 100644 --- a/libutf/utftorunestr.c +++ b/libutf/utftorunestr.c _AT_@ -11,3 +11,15 @@ utftorunestr(const char *str, Rune *r) return i; } + +int +utfntorunestr(const char *str, size_t len, Rune *r) +{ + int i, n; + const char *p = str, *end = str + len; + + for(i = 0; (n = charntorune(&r[i], p, end - p)) && p < end; i++) + p += n; + + return i; +} diff --git a/paste.c b/paste.c index b0ac761..051ca72 100644 --- a/paste.c +++ b/paste.c _AT_@ -53,7 +53,8 @@ nextline: for (; efgetrune(&c, dsc[i].fp, dsc[i].name) ;) { for (m = last + 1; m < i; m++) - efputrune(&(delim[m % delimlen]), stdout, "<stdout>"); + if (delim[m % delimlen] != '\0') + efputrune(&(delim[m % delimlen]), stdout, "<stdout>"); last = i; if (c == '\n') { if (i != fdescrlen - 1) _AT_@ -68,7 +69,8 @@ nextline: if (i == fdescrlen - 1) putchar('\n'); else - efputrune(&d, stdout, "<stdout>"); + if (d != '\0') + efputrune(&d, stdout, "<stdout>"); last++; } } _AT_@ -86,18 +88,18 @@ int main(int argc, char *argv[]) { struct fdescr *dsc; - Rune *delim; - size_t delimlen, i; + Rune *delim_rune = NULL; + size_t delim_runelen, i, delim_bytelen = 1; int seq = 0, ret = 0; - char *adelim = "\t"; + char *delim = "\t"; ARGBEGIN { case 's': seq = 1; break; case 'd': - adelim = EARGF(usage()); - unescape(adelim); + delim = EARGF(usage()); + delim_bytelen = unescape(delim); break; default: usage(); _AT_@ -107,10 +109,11 @@ main(int argc, char *argv[]) usage(); /* populate delimiters */ - /* TODO: fix libutf to accept sizes */ - delim = ereallocarray(NULL, utflen(adelim) + 1, sizeof(*delim)); - if (!(delimlen = utftorunestr(adelim, delim))) + delim_rune = ereallocarray(NULL, + utfmemlen(delim, delim_bytelen) + 1, sizeof(*delim_rune)); + if (!(delim_runelen = utfntorunestr(delim, delim_bytelen, delim_rune))) { usage(); + } /* populate file list */ dsc = ereallocarray(NULL, argc, sizeof(*dsc)); _AT_@ -126,9 +129,9 @@ main(int argc, char *argv[]) } if (seq) { - sequential(dsc, argc, delim, delimlen); + sequential(dsc, argc, delim_rune, delim_runelen); } else { - parallel(dsc, argc, delim, delimlen); + parallel(dsc, argc, delim_rune, delim_runelen); } for (i = 0; i < argc; i++) diff --git a/utf.h b/utf.h index 23a9887..fa04f6b 100644 --- a/utf.h +++ b/utf.h _AT_@ -38,6 +38,7 @@ int fullrune(const char *, size_t); char *utfecpy(char *, char *, const char *); size_t utflen(const char *); size_t utfnlen(const char *, size_t); +size_t utfmemlen(const char *, size_t); char *utfrune(const char *, Rune); char *utfrrune(const char *, Rune); char *utfutf(const char *, const char *); _AT_@ -59,7 +60,8 @@ int isxdigitrune(Rune); Rune tolowerrune(Rune); Rune toupperrune(Rune); -int utftorunestr(const char*, Rune *); +int utftorunestr(const char *, Rune *); +int utfntorunestr(const char *, size_t, Rune *); int fgetrune(Rune *, FILE *); int efgetrune(Rune *, FILE *, const char *); -- 2.25.1Received on Fri Mar 27 2020 - 18:58:30 CET
This archive was generated by hypermail 2.3.0 : Fri Mar 27 2020 - 19:00:40 CET