2015-01-20 15:26:08 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2013-11-15 16:25:10 +00:00
|
|
|
#include <stdlib.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
#include "utf.h"
|
2013-11-15 16:25:10 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
static int cflag = 0;
|
|
|
|
static int dflag = 0;
|
|
|
|
static int sflag = 0;
|
2014-07-14 22:49:42 +00:00
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
struct range {
|
|
|
|
Rune start;
|
|
|
|
Rune end;
|
|
|
|
size_t quant;
|
2014-07-14 22:49:42 +00:00
|
|
|
};
|
|
|
|
|
2015-01-10 14:21:09 +00:00
|
|
|
static struct {
|
2015-01-11 19:26:20 +00:00
|
|
|
char *name;
|
2015-02-11 12:03:32 +00:00
|
|
|
int (*check)(Rune);
|
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-09 19:36:27 +00:00
|
|
|
} classes[] = {
|
2015-02-11 12:03:32 +00:00
|
|
|
{ "alnum", isalnumrune },
|
|
|
|
{ "alpha", isalpharune },
|
|
|
|
{ "blank", isblankrune },
|
|
|
|
{ "cntrl", iscntrlrune },
|
|
|
|
{ "digit", isdigitrune },
|
|
|
|
{ "graph", isgraphrune },
|
|
|
|
{ "lower", islowerrune },
|
|
|
|
{ "print", isprintrune },
|
|
|
|
{ "punct", ispunctrune },
|
|
|
|
{ "space", isspacerune },
|
|
|
|
{ "upper", isupperrune },
|
|
|
|
{ "xdigit", isxdigitrune },
|
2014-07-14 22:49:42 +00:00
|
|
|
};
|
|
|
|
|
2015-02-11 12:03:32 +00:00
|
|
|
static struct range *set1 = NULL;
|
|
|
|
static size_t set1ranges = 0;
|
|
|
|
static int (*set1check)(Rune) = NULL;
|
|
|
|
static struct range *set2 = NULL;
|
|
|
|
static size_t set2ranges = 0;
|
|
|
|
static int (*set2check)(Rune) = NULL;
|
2015-01-11 19:26:20 +00:00
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
static size_t
|
|
|
|
rangelen(struct range r)
|
2013-11-15 16:25:10 +00:00
|
|
|
{
|
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-09 19:36:27 +00:00
|
|
|
return (r.end - r.start + 1) * r.quant;
|
2014-07-14 22:49:42 +00:00
|
|
|
}
|
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
static size_t
|
|
|
|
setlen(struct range *set, size_t setranges)
|
2014-07-14 22:49:42 +00:00
|
|
|
{
|
2015-01-10 16:38:28 +00:00
|
|
|
size_t len = 0, i;
|
2014-07-14 22:49:42 +00:00
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
for (i = 0; i < setranges; i++)
|
|
|
|
len += rangelen(set[i]);
|
2014-04-12 18:50:51 +00:00
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
return len;
|
2014-04-12 18:50:51 +00:00
|
|
|
}
|
|
|
|
|
2014-07-14 22:49:42 +00:00
|
|
|
static int
|
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-09 19:36:27 +00:00
|
|
|
rstrmatch(Rune *r, char *s, size_t n)
|
2014-07-14 22:49:42 +00:00
|
|
|
{
|
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-09 19:36:27 +00:00
|
|
|
size_t i;
|
2014-07-14 22:49:42 +00:00
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
if (r[i] != s[i])
|
|
|
|
return 0;
|
|
|
|
return 1;
|
2014-07-14 22:49:42 +00:00
|
|
|
}
|
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
static size_t
|
2015-02-11 12:03:32 +00:00
|
|
|
makeset(char *str, struct range **set, int (**check)(Rune))
|
2014-07-14 22:49:42 +00:00
|
|
|
{
|
2015-03-10 21:19:19 +00:00
|
|
|
Rune *rstr;
|
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-09 19:36:27 +00:00
|
|
|
size_t len, i, j, m, n;
|
2015-01-11 19:26:20 +00:00
|
|
|
size_t q, setranges = 0;
|
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-09 19:36:27 +00:00
|
|
|
int factor, base;
|
|
|
|
|
|
|
|
/* rstr defines at most len ranges */
|
2015-01-29 20:52:44 +00:00
|
|
|
unescape(str);
|
2015-03-11 09:50:18 +00:00
|
|
|
rstr = ereallocarray(NULL, utflen(str) + 1, sizeof(*rstr));
|
2015-02-11 20:32:09 +00:00
|
|
|
len = utftorunestr(str, rstr);
|
2015-03-11 09:50:18 +00:00
|
|
|
*set = ereallocarray(NULL, len, sizeof(**set));
|
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-09 19:36:27 +00:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (rstr[i] == '[') {
|
|
|
|
j = i;
|
|
|
|
nextbrack:
|
|
|
|
if (j == len)
|
|
|
|
goto literal;
|
|
|
|
for (m = j; m < len; m++)
|
|
|
|
if (rstr[m] == ']') {
|
|
|
|
j = m;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (j == i)
|
|
|
|
goto literal;
|
|
|
|
|
|
|
|
/* CLASSES [=EQUIV=] (skip) */
|
|
|
|
if (j - i > 3 && rstr[i + 1] == '=' && rstr[m - 1] == '=') {
|
2015-01-28 18:44:05 +00:00
|
|
|
if (j - i != 4)
|
|
|
|
goto literal;
|
|
|
|
(*set)[setranges].start = rstr[i + 2];
|
|
|
|
(*set)[setranges].end = rstr[i + 2];
|
|
|
|
(*set)[setranges].quant = 1;
|
|
|
|
setranges++;
|
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-09 19:36:27 +00:00
|
|
|
i = j;
|
|
|
|
continue;
|
2014-07-14 22:49:42 +00:00
|
|
|
}
|
2013-11-15 16:25:10 +00:00
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
/* CLASSES [:CLASS:] */
|
|
|
|
if (j - i > 3 && rstr[i + 1] == ':' && rstr[m - 1] == ':') {
|
|
|
|
for (n = 0; n < LEN(classes); n++) {
|
|
|
|
if (rstrmatch(rstr + i + 2, classes[n].name, j - i - 3)) {
|
2015-01-11 19:26:20 +00:00
|
|
|
*check = classes[n].check;
|
|
|
|
return 0;
|
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-09 19:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-11 23:03:48 +00:00
|
|
|
eprintf("Invalid character class.\n");
|
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-09 19:36:27 +00:00
|
|
|
}
|
2013-11-15 16:25:10 +00:00
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
/* REPEAT [_*n] (only allowed in set2) */
|
|
|
|
if (j - i > 2 && rstr[i + 2] == '*' && set1ranges > 0) {
|
|
|
|
/* check if right side of '*' is a number */
|
|
|
|
q = 0;
|
|
|
|
factor = 1;
|
|
|
|
base = (rstr[i + 3] == '0') ? 8 : 10;
|
|
|
|
for (n = j - 1; n > i + 2; n--) {
|
2015-04-20 19:17:53 +00:00
|
|
|
if (rstr[n] < '0' || rstr[n] > '9') {
|
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-09 19:36:27 +00:00
|
|
|
n = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
q += (rstr[n] - '0') * factor;
|
|
|
|
factor *= base;
|
|
|
|
}
|
|
|
|
if (n == 0) {
|
|
|
|
j = m + 1;
|
|
|
|
goto nextbrack;
|
|
|
|
}
|
|
|
|
(*set)[setranges].start = rstr[i + 1];
|
|
|
|
(*set)[setranges].end = rstr[i + 1];
|
|
|
|
(*set)[setranges].quant = q ? q : setlen(set1, set1ranges);
|
|
|
|
setranges++;
|
|
|
|
i = j;
|
|
|
|
continue;
|
|
|
|
}
|
2013-11-15 16:25:10 +00:00
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
j = m + 1;
|
|
|
|
goto nextbrack;
|
2014-07-14 22:49:42 +00:00
|
|
|
}
|
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-09 19:36:27 +00:00
|
|
|
literal:
|
|
|
|
/* RANGES [_-__-_], _-__-_ */
|
|
|
|
/* LITERALS _______ */
|
|
|
|
(*set)[setranges].start = rstr[i];
|
|
|
|
|
|
|
|
if (i < len - 2 && rstr[i + 1] == '-' && rstr[i + 2] >= rstr[i])
|
|
|
|
i += 2;
|
|
|
|
(*set)[setranges].end = rstr[i];
|
|
|
|
(*set)[setranges].quant = 1;
|
|
|
|
setranges++;
|
2013-11-15 16:25:10 +00:00
|
|
|
}
|
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
free(rstr);
|
|
|
|
return setranges;
|
2014-07-14 22:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
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-09 19:36:27 +00:00
|
|
|
usage(void)
|
2014-07-14 22:49:42 +00:00
|
|
|
{
|
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-09 19:36:27 +00:00
|
|
|
eprintf("usage: %s [-cCds] set1 [set2]\n", argv0);
|
2014-07-14 22:49:42 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 16:25:10 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
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-09 19:36:27 +00:00
|
|
|
Rune r = 0, lastrune = 0;
|
2015-01-10 19:55:37 +00:00
|
|
|
size_t off1, off2, i, m;
|
2015-05-24 23:33:19 +00:00
|
|
|
int ret = 0;
|
2013-11-15 16:25:10 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
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-09 19:36:27 +00:00
|
|
|
case 'c':
|
|
|
|
case 'C':
|
|
|
|
cflag = 1;
|
|
|
|
break;
|
2014-04-09 12:12:34 +00:00
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
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-09 19:36:27 +00:00
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
2014-07-14 22:49:42 +00:00
|
|
|
break;
|
2013-11-15 16:25:10 +00:00
|
|
|
default:
|
|
|
|
usage();
|
2015-11-01 10:16:49 +00:00
|
|
|
} ARGEND
|
2013-11-15 16:25:10 +00:00
|
|
|
|
2015-03-17 22:41:22 +00:00
|
|
|
if (!argc || argc > 2 || (argc == 1 && dflag == sflag))
|
2013-11-15 16:25:10 +00:00
|
|
|
usage();
|
2015-01-11 19:26:20 +00:00
|
|
|
set1ranges = makeset(argv[0], &set1, &set1check);
|
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-09 19:36:27 +00:00
|
|
|
if (argc == 2)
|
2015-01-11 19:26:20 +00:00
|
|
|
set2ranges = makeset(argv[1], &set2, &set2check);
|
|
|
|
if (dflag == sflag && !set2ranges && !set2check)
|
2015-01-11 23:03:48 +00:00
|
|
|
eprintf("set2 must be non-empty.\n");
|
2015-04-20 19:38:46 +00:00
|
|
|
if (argc == 2 && !set2check != !set1check)
|
|
|
|
eprintf("can't mix classes with non-classes.\n");
|
2015-02-11 12:03:32 +00:00
|
|
|
if (set2check && set2check != islowerrune && set2check != isupperrune)
|
2015-01-11 23:03:48 +00:00
|
|
|
eprintf("set2 can only be the 'lower' or 'upper' class.\n");
|
2015-02-02 18:59:41 +00:00
|
|
|
if (set2check && cflag && !dflag)
|
2015-02-07 17:09:04 +00:00
|
|
|
eprintf("set2 can't be imaged to from a complement.\n");
|
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-09 19:36:27 +00:00
|
|
|
read:
|
2015-05-24 23:33:19 +00:00
|
|
|
if (!efgetrune(&r, stdin, "<stdin>")) {
|
|
|
|
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
|
|
|
|
return ret;
|
|
|
|
}
|
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-09 19:36:27 +00:00
|
|
|
off1 = off2 = 0;
|
|
|
|
for (i = 0; i < set1ranges; i++) {
|
|
|
|
if (set1[i].start <= r && r <= set1[i].end) {
|
2015-02-02 16:57:46 +00:00
|
|
|
if (dflag) {
|
2015-02-02 18:59:41 +00:00
|
|
|
if (!cflag || (sflag && r == lastrune))
|
2015-02-02 16:57:46 +00:00
|
|
|
goto read;
|
2015-02-02 18:59:41 +00:00
|
|
|
else
|
|
|
|
goto write;
|
2015-02-02 16:57:46 +00:00
|
|
|
}
|
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-09 19:36:27 +00:00
|
|
|
if (sflag) {
|
|
|
|
if (r == lastrune)
|
|
|
|
goto read;
|
|
|
|
else
|
|
|
|
goto write;
|
|
|
|
}
|
2015-04-27 00:49:02 +00:00
|
|
|
if (cflag)
|
|
|
|
goto write;
|
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-09 19:36:27 +00:00
|
|
|
for (m = 0; m < i; m++)
|
|
|
|
off1 += rangelen(set1[m]);
|
|
|
|
off1 += r - set1[m].start;
|
|
|
|
if (off1 > setlen(set2, set2ranges) - 1) {
|
|
|
|
r = set2[set2ranges - 1].end;
|
|
|
|
goto write;
|
|
|
|
}
|
|
|
|
for (m = 0; m < set2ranges; m++) {
|
|
|
|
if (off2 + rangelen(set2[m]) > off1) {
|
|
|
|
m++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
off2 += rangelen(set2[m]);
|
|
|
|
}
|
|
|
|
m--;
|
|
|
|
r = set2[m].start + (off1 - off2) / set2[m].quant;
|
2013-11-15 16:25:10 +00:00
|
|
|
|
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-09 19:36:27 +00:00
|
|
|
goto write;
|
|
|
|
}
|
2013-11-15 16:25:10 +00:00
|
|
|
}
|
2015-02-11 12:03:32 +00:00
|
|
|
if (set1check && set1check(r)) {
|
2015-02-02 18:59:41 +00:00
|
|
|
if (dflag) {
|
|
|
|
if (!cflag || (sflag && r == lastrune))
|
2015-02-02 16:57:46 +00:00
|
|
|
goto read;
|
2015-02-02 18:59:41 +00:00
|
|
|
else
|
|
|
|
goto write;
|
|
|
|
}
|
2015-01-11 19:26:20 +00:00
|
|
|
if (sflag) {
|
|
|
|
if (r == lastrune)
|
|
|
|
goto read;
|
|
|
|
else
|
|
|
|
goto write;
|
|
|
|
}
|
2015-02-11 12:03:32 +00:00
|
|
|
if (set1check == isupperrune && set2check == islowerrune)
|
|
|
|
r = tolowerrune(r);
|
|
|
|
else if (set1check == islowerrune && set2check == isupperrune)
|
|
|
|
r = toupperrune(r);
|
2015-01-11 23:03:48 +00:00
|
|
|
else if (set2ranges > 0)
|
2015-02-02 18:59:41 +00:00
|
|
|
r = cflag ? r : set2[set2ranges - 1].end;
|
2015-01-11 23:03:48 +00:00
|
|
|
else
|
|
|
|
eprintf("Misaligned character classes.\n");
|
2015-02-02 18:59:41 +00:00
|
|
|
} else if (cflag && set2ranges > 0) {
|
|
|
|
r = set2[set2ranges - 1].end;
|
2015-01-11 19:26:20 +00:00
|
|
|
}
|
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-09 19:36:27 +00:00
|
|
|
if (dflag && cflag)
|
|
|
|
goto read;
|
|
|
|
if (dflag && sflag && r == lastrune)
|
|
|
|
goto read;
|
|
|
|
write:
|
|
|
|
lastrune = r;
|
2015-02-11 19:58:00 +00:00
|
|
|
efputrune(&r, stdout, "<stdout>");
|
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-09 19:36:27 +00:00
|
|
|
goto read;
|
2013-11-15 16:25:10 +00:00
|
|
|
}
|