sbase/libutf/chartorunearr.c
FRIGN a582cb8a2f Rewrite tr(1) in a sane way
tr(1) always used to be a saddening part of sbase, which was
inherently broken and crufted.
But to be fair, the POSIX-standard doesn't make it very simple.
Given the current version was unfixable and broken by design, I
sat down and rewrote tr(1) very close to the concept of set theory
and the POSIX-standard with a few exceptions:

 - UTF-8: not allowed in POSIX, but in my opinion a must. This
          finally allows you to work with UTF-8 streams without
          problems or unexpected behaviour.
 - Equivalence classes: Left out, even GNU coreutils ignore them
                        and depending on LC_COLLATE, which sucks.
 - Character classes: No experiments or environment-variable-trickery.
                      Just plain definitions derived from the POSIX-
                      standard, working as expected.

I tested this thoroughly, but expect problems to show up in some
way given the wide range of input this program has to handle.
The only thing left on the TODO is to add support for literal
expressions ('\n', '\t', '\001', ...) and probably rethinking
the way [_*n] is unnecessarily restricted to string2.
2015-01-10 14:26:30 +00:00

28 lines
583 B
C

/* See LICENSE file for copyright and license details. */
#include <stdlib.h>
#include <string.h>
#include "../util.h"
#include "../utf.h"
int
chartorunearr(const char *str, Rune **r)
{
size_t len = strlen(str), rlen, roff, ret, i;
Rune s;
for (rlen = 0, roff = 0; roff < len && ret; rlen++) {
ret = charntorune(&s, str + roff, MAX(UTFmax, len - roff));
roff += ret;
}
*r = emalloc(rlen * sizeof(Rune) + 1);
(*r)[rlen] = 0;
for (i = 0, roff = 0; roff < len && i < rlen; i++) {
roff += charntorune(&(*r)[i], str + roff, MAX(UTFmax, len - roff));
}
return rlen;
}