Imported Upstream version 1.22.4
[platform/upstream/groff.git] / src / libs / libgroff / cset.cpp
1 // -*- C++ -*-
2 /* Copyright (C) 1989-2018 Free Software Foundation, Inc.
3      Written by James Clark (jjc@jclark.com)
4
5 This file is part of groff.
6
7 groff is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
19
20 #include <ctype.h>
21
22 #include "lib.h"
23 #include "cset.h"
24
25 cset csalpha(CSET_BUILTIN);
26 cset csupper(CSET_BUILTIN);
27 cset cslower(CSET_BUILTIN);
28 cset csdigit(CSET_BUILTIN);
29 cset csxdigit(CSET_BUILTIN);
30 cset csspace(CSET_BUILTIN);
31 cset cspunct(CSET_BUILTIN);
32 cset csalnum(CSET_BUILTIN);
33 cset csprint(CSET_BUILTIN);
34 cset csgraph(CSET_BUILTIN);
35 cset cscntrl(CSET_BUILTIN);
36
37 #ifdef isascii
38 #define ISASCII(c) isascii(c)
39 #else
40 #define ISASCII(c) (1)
41 #endif
42
43 void cset::clear()
44 {
45   char *p = v;
46   for (int i = 0; i <= UCHAR_MAX; i++)
47     p[i] = 0;
48 }
49
50 cset::cset()
51 {
52   clear();
53 }
54
55 cset::cset(const char *s)
56 {
57   clear();
58   while (*s)
59     v[(unsigned char)*s++] = 1;
60 }
61
62 cset::cset(const unsigned char *s)
63 {
64   clear();
65   while (*s)
66     v[*s++] = 1;
67 }
68
69 cset::cset(cset_builtin)
70 {
71   // these are initialised by cset_init::cset_init()
72 }
73
74 cset &cset::operator|=(const cset &cs)
75 {
76   for (int i = 0; i <= UCHAR_MAX; i++)
77     if (cs.v[i])
78       v[i] = 1;
79   return *this;
80 }
81
82
83 int cset_init::initialised = 0;
84
85 cset_init::cset_init()
86 {
87   if (initialised)
88     return;
89   initialised = 1;
90   for (int i = 0; i <= UCHAR_MAX; i++) {
91     csalpha.v[i] = ISASCII(i) && isalpha(i);
92     csupper.v[i] = ISASCII(i) && isupper(i);
93     cslower.v[i] = ISASCII(i) && islower(i);
94     csdigit.v[i] = ISASCII(i) && isdigit(i);
95     csxdigit.v[i] = ISASCII(i) && isxdigit(i);
96     csspace.v[i] = ISASCII(i) && isspace(i);
97     cspunct.v[i] = ISASCII(i) && ispunct(i);
98     csalnum.v[i] = ISASCII(i) && isalnum(i);
99     csprint.v[i] = ISASCII(i) && isprint(i);
100     csgraph.v[i] = ISASCII(i) && isgraph(i);
101     cscntrl.v[i] = ISASCII(i) && iscntrl(i);
102   }
103 }