2014-11-17 15:41:44 +00:00
|
|
|
/* MIT/X Consortium Copyright (c) 2012 Connor Lane Smith <cls@lubutu.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2014-11-21 16:20:15 +00:00
|
|
|
#include <stdio.h>
|
2014-11-17 15:41:44 +00:00
|
|
|
|
|
|
|
typedef int Rune;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
UTFmax = 6, /* maximum bytes per rune */
|
|
|
|
Runeself = 0x80, /* rune and utf are equal (<) */
|
|
|
|
Runeerror = 0xFFFD, /* decoding error in utf */
|
|
|
|
Runemax = 0x10FFFF /* maximum rune value */
|
|
|
|
};
|
|
|
|
|
|
|
|
int runetochar(char *, const Rune *);
|
|
|
|
int chartorune(Rune *, const char *);
|
|
|
|
int charntorune(Rune *, const char *, size_t);
|
|
|
|
int runelen(const Rune);
|
|
|
|
size_t runenlen(const Rune *, size_t);
|
|
|
|
int fullrune(const char *, size_t);
|
|
|
|
char *utfecpy(char *, char *, const char *);
|
|
|
|
size_t utflen(const char *);
|
|
|
|
size_t utfnlen(const char *, size_t);
|
|
|
|
char *utfrune(const char *, Rune);
|
|
|
|
char *utfrrune(const char *, Rune);
|
|
|
|
char *utfutf(const char *, const char *);
|
|
|
|
|
2015-02-11 14:44:54 +00:00
|
|
|
int isalnumrune(Rune);
|
2014-11-17 15:41:44 +00:00
|
|
|
int isalpharune(Rune);
|
2015-02-11 14:44:54 +00:00
|
|
|
int isblankrune(Rune);
|
2015-02-11 11:57:36 +00:00
|
|
|
int iscntrlrune(Rune);
|
2014-11-17 15:41:44 +00:00
|
|
|
int isdigitrune(Rune);
|
2015-02-11 11:57:36 +00:00
|
|
|
int isgraphrune(Rune);
|
2015-02-11 14:44:54 +00:00
|
|
|
int islowerrune(Rune);
|
|
|
|
int isprintrune(Rune);
|
2015-02-11 11:57:36 +00:00
|
|
|
int ispunctrune(Rune);
|
2015-02-11 14:44:54 +00:00
|
|
|
int isspacerune(Rune);
|
|
|
|
int istitlerune(Rune);
|
|
|
|
int isupperrune(Rune);
|
2015-02-11 11:57:36 +00:00
|
|
|
int isxdigitrune(Rune);
|
|
|
|
|
|
|
|
Rune tolowerrune(Rune);
|
|
|
|
Rune toupperrune(Rune);
|
2014-11-21 16:20:15 +00:00
|
|
|
|
|
|
|
int readrune(const char *, FILE *, Rune *);
|
2014-11-21 16:34:44 +00:00
|
|
|
void writerune(const char *, FILE *, 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
|
|
|
int chartorunearr(const char*, Rune **);
|