sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit e1534476570098ea257c84f77ebb96ad835a7c7b
parent 1622089a21a19e7d6fd20f213370a22a077c81cd
Author: FRIGN <dev@frign.de>
Date:   Mon,  3 Aug 2015 17:35:01 +0200

Make sort(1) utf-compliant and update README

Make it clear that <blank> characters just are spaces or tabs and
not a special group which needs special treatment for wide characters.

Also, and that was the only problem here, correctly calculate the
offset given by the key definitions for the start- and end-characters
using libutf-utility-functions.

Mark the progress in the README and put parentheses around the missing
flags which are insane to implement for no real gain.

Diffstat:
MREADME | 2+-
Msort.c | 25++++++++++++++++++-------
2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/README b/README @@ -69,7 +69,7 @@ The following tools are implemented: =*|x sha256sum . =*|x sha512sum . =*|o sleep . - sort -d, -f, -i +# sort (-d, -f, -i) =*|o split . =*|x sponge . #*|o strings . diff --git a/sort.c b/sort.c @@ -6,6 +6,7 @@ #include "queue.h" #include "text.h" +#include "utf.h" #include "util.h" struct keydef { @@ -43,7 +44,7 @@ static size_t col1siz, col2siz; static char * skipblank(char *s) { - while (isblank(*s)) + while (*s == ' ' || *s == '\t') s++; return s; } @@ -51,7 +52,7 @@ skipblank(char *s) static char * skipnonblank(char *s) { - while (*s && *s != '\n' && !isblank(*s)) + while (*s && *s != '\n' && *s != ' ' && *s != '\t') s++; return s; } @@ -74,25 +75,35 @@ skipcolumn(char *s, char *eol, int next_col) static size_t columns(char *line, const struct keydef *kd, char **col, size_t *colsiz) { + Rune r; char *start, *end, *eol = strchr(line, '\n'); - size_t len; + size_t len, utflen, rlen; int i; for (i = 1, start = line; i < kd->start_column; i++) start = skipcolumn(start, eol, 1); if (kd->flags & MOD_STARTB) start = skipblank(start); - start = MIN(eol, start + kd->start_char - 1); + for (utflen = 0; start < eol && utflen < kd->start_char - 1;) { + rlen = chartorune(&r, start); + start += rlen; + utflen++; + } if (kd->end_column) { for (i = 1, end = line; i < kd->end_column; i++) end = skipcolumn(end, eol, 1); if (kd->flags & MOD_ENDB) end = skipblank(end); - if (kd->end_char) - end = MIN(eol, end + kd->end_char); - else + if (kd->end_char) { + for (utflen = 0; end < eol && utflen < kd->end_char;) { + rlen = chartorune(&r, end); + end += rlen; + utflen++; + } + } else { end = skipcolumn(end, eol, 0); + } } else { end = eol; }