1 /*@ Anything (locale agnostic: ASCII only) around char and char*.
3 * Copyright (c) 2001 - 2019 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
4 * SPDX-License-Identifier: ISC
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <su/code-in.h>
26 su_CS_CTYPE_ALNUM
= 1u<<0,
27 su_CS_CTYPE_ALPHA
= 1u<<1,
28 su_CS_CTYPE_BLANK
= 1u<<2,
29 su_CS_CTYPE_CNTRL
= 1u<<3,
30 su_CS_CTYPE_DIGIT
= 1u<<4,
31 su_CS_CTYPE_GRAPH
= 1u<<5,
32 su_CS_CTYPE_LOWER
= 1u<<6,
33 su_CS_CTYPE_PRINT
= 1u<<7,
34 su_CS_CTYPE_PUNCT
= 1u<<8,
35 su_CS_CTYPE_SPACE
= 1u<<9,
36 su_CS_CTYPE_UPPER
= 1u<<10,
37 su_CS_CTYPE_WHITE
= 1u<<11,
38 su_CS_CTYPE_XDIGIT
= 1u<<12,
39 su__CS_CTYPE_MAXSHIFT
= 13u,
40 su__CS_CTYPE_MASK
= (1u<<su__CS_CTYPE_MAXSHIFT
) - 1
42 EXPORT_DATA u16
const su__cs_ctype
[S8_MAX
+ 1];
43 EXPORT_DATA u8
const su__cs_tolower
[S8_MAX
+ 1];
44 EXPORT_DATA u8
const su__cs_toupper
[S8_MAX
+ 1];
45 EXPORT_DATA
struct su_toolbox
const su_cs_toolbox
;
46 EXPORT_DATA
struct su_toolbox
const su_cs_toolbox_case
;
47 INLINE boole
su_cs_is_ascii(s32 x
) {return (S(u32
,x
) <= S8_MAX
);}
50 return (su_cs_is_ascii(X) &&\
51 (su__cs_ctype[S(u32,X)] & su_CONCAT(su_CS_CTYPE_,F)) != 0)
52 INLINE boole
su_cs_is_alnum(s32 x
) {a_X(x
, ALNUM
);}
53 INLINE boole
su_cs_is_alpha(s32 x
) {a_X(x
, ALPHA
);}
54 INLINE boole
su_cs_is_blank(s32 x
) {a_X(x
, BLANK
);}
55 INLINE boole
su_cs_is_cntrl(s32 x
) {a_X(x
, CNTRL
);}
56 INLINE boole
su_cs_is_digit(s32 x
) {a_X(x
, DIGIT
);}
57 INLINE boole
su_cs_is_graph(s32 x
) {a_X(x
, GRAPH
);}
58 INLINE boole
su_cs_is_lower(s32 x
) {a_X(x
, LOWER
);}
59 INLINE boole
su_cs_is_print(s32 x
) {a_X(x
, PRINT
);}
60 INLINE boole
su_cs_is_punct(s32 x
) {a_X(x
, PUNCT
);}
61 INLINE boole
su_cs_is_space(s32 x
) {a_X(x
, SPACE
);}
62 INLINE boole
su_cs_is_upper(s32 x
) {a_X(x
, UPPER
);}
63 INLINE boole
su_cs_is_white(s32 x
) {a_X(x
, WHITE
);}
64 INLINE boole
su_cs_is_xdigit(s32 x
) {a_X(x
, XDIGIT
);}
66 INLINE boole
su_cs_is_ctype(s32 x
, u32 csct
){
67 return (su_cs_is_ascii(x
) && (su__cs_ctype
[x
] & csct
) != 0);
69 EXPORT sz
su_cs_cmp(char const *cp1
, char const *cp2
);
70 EXPORT sz
su_cs_cmp_n(char const *cp1
, char const *cp2
, uz n
);
71 EXPORT sz
su_cs_cmp_case(char const *cp1
, char const *cp2
);
72 EXPORT sz
su_cs_cmp_case_n(char const *cp1
, char const *cp2
, uz n
);
73 EXPORT
char *su_cs_copy_n(char *dst
, char const *src
, uz n
);
74 EXPORT
char *su_cs_dup_cbuf(char const *buf
, uz len
, u32 estate
);
75 EXPORT
char *su_cs_dup(char const *cp
, u32 estate
);
77 EXPORT boole
su_cs_ends_with_case(char const *cp
, char const *x
);
79 EXPORT
char *su_cs_find(char const *cp
, char const *xp
);
80 EXPORT
char *su_cs_find_c(char const *cp
, char xc
);
81 EXPORT
char *su_cs_find_case(char const *cp
, char const *xp
);
82 EXPORT uz
su_cs_first_of_cbuf_cbuf(char const *cp
, uz cplen
,
83 char const *xp
, uz xlen
);
84 INLINE uz
su_cs_first_of(char const *cp
, char const *xp
){
85 ASSERT_RET(cp
!= NIL
, UZ_MAX
);
86 ASSERT_RET(xp
!= NIL
, UZ_MAX
);
87 return su_cs_first_of_cbuf_cbuf(cp
, UZ_MAX
, xp
, UZ_MAX
);
89 EXPORT uz
su_cs_hash_cbuf(char const *buf
, uz len
);
90 INLINE uz
su_cs_hash(char const *cp
){
91 ASSERT_RET(cp
!= NIL
, 0);
92 return su_cs_hash_cbuf(cp
, UZ_MAX
);
94 EXPORT uz
su_cs_hash_case_cbuf(char const *buf
, uz len
);
95 INLINE uz
su_cs_hash_case(char const *cp
){
96 ASSERT_RET(cp
!= NIL
, 0);
97 return su_cs_hash_case_cbuf(cp
, UZ_MAX
);
99 EXPORT uz
su_cs_len(char const *cp
);
100 EXPORT
char *su_cs_pcopy(char *dst
, char const *src
);
101 EXPORT
char *su_cs_pcopy_n(char *dst
, char const *src
, uz n
);
102 EXPORT
char *su_cs_rfind_c(char const *cp
, char x
);
103 EXPORT
char *su_cs_sep_c(char **iolist
, char sep
, boole ignore_empty
);
104 EXPORT
char *su_cs_sep_escable_c(char **iolist
, char sep
, boole ignore_empty
);
105 EXPORT boole
su_cs_starts_with(char const *cp
, char const *x
);
106 EXPORT boole
su_cs_starts_with_n(char const *cp
, char const *x
, uz n
);
107 EXPORT boole
su_cs_starts_with_case(char const *cp
, char const *x
);
108 EXPORT boole
su_cs_starts_with_case_n(char const *cp
, char const *x
, uz n
);
109 INLINE s32
su_cs_to_lower(s32 x
){
110 return (S(u32
,x
) <= S8_MAX
? su__cs_tolower
[x
] : x
);
112 INLINE s32
su_cs_to_upper(s32 x
){
113 return (S(u32
,x
) <= S8_MAX
? su__cs_toupper
[x
] : x
);
116 #include <su/code-ou.h>
117 #if !su_C_LANG || defined CXX_DOXYGEN
118 # define su_A_T_T_DECL_ONLY
119 # include <su/a-t-t.h>
120 # define su_CXX_HEADER
121 # include <su/code-in.h>
127 ctype_none
= su_CS_CTYPE_NONE
,
128 ctype_alnum
= su_CS_CTYPE_ALNUM
,
129 ctype_alpha
= su_CS_CTYPE_ALPHA
,
130 ctype_blank
= su_CS_CTYPE_BLANK
,
131 ctype_cntrl
= su_CS_CTYPE_CNTRL
,
132 ctype_digit
= su_CS_CTYPE_DIGIT
,
133 ctype_graph
= su_CS_CTYPE_GRAPH
,
134 ctype_lower
= su_CS_CTYPE_LOWER
,
135 ctype_print
= su_CS_CTYPE_PRINT
,
136 ctype_punct
= su_CS_CTYPE_PUNCT
,
137 ctype_space
= su_CS_CTYPE_SPACE
,
138 ctype_upper
= su_CS_CTYPE_UPPER
,
139 ctype_white
= su_CS_CTYPE_WHITE
,
140 ctype_xdigit
= su_CS_CTYPE_XDIGIT
142 static NSPC(su
)type_toolbox
<char*> const * const type_toolbox
;
143 static NSPC(su
)type_toolbox
<char const*> const * const const_type_toolbox
;
144 static NSPC(su
)type_toolbox
<char*> const * const type_toolbox_case
;
145 static NSPC(su
)type_toolbox
<char const*> const * const
146 const_type_toolbox_case
;
147 static boole
is_ascii(s32 x
) {return su_cs_is_ascii(x
);}
148 static boole
is_alnum(s32 x
) {return su_cs_is_alnum(x
);}
149 static boole
is_alpha(s32 x
) {return su_cs_is_alpha(x
);}
150 static boole
is_blank(s32 x
) {return su_cs_is_blank(x
);}
151 static boole
is_cntrl(s32 x
) {return su_cs_is_cntrl(x
);}
152 static boole
is_digit(s32 x
) {return su_cs_is_digit(x
);}
153 static boole
is_graph(s32 x
) {return su_cs_is_graph(x
);}
154 static boole
is_lower(s32 x
) {return su_cs_is_lower(x
);}
155 static boole
is_print(s32 x
) {return su_cs_is_print(x
);}
156 static boole
is_punct(s32 x
) {return su_cs_is_punct(x
);}
157 static boole
is_space(s32 x
) {return su_cs_is_space(x
);}
158 static boole
is_upper(s32 x
) {return su_cs_is_upper(x
);}
159 static boole
is_white(s32 x
) {return su_cs_is_white(x
);}
160 static boole
is_xdigit(s32 x
) {return su_cs_is_xdigit(x
);}
161 static boole
is_ctype(s32 x
, u32 ct
) {return su_cs_is_ctype(x
, ct
);}
162 static sz
cmp(char const *cp1
, char const *cp2
){
163 return su_cs_cmp(cp1
, cp2
);
165 static sz
cmp(char const *cp1
, char const *cp2
, uz n
){
166 return su_cs_cmp_n(cp1
, cp2
, n
);
168 static sz
cmp_case(char const *cp1
, char const *cp2
){
169 return su_cs_cmp_case(cp1
, cp2
);
171 static sz
cmp_case(char const *cp1
, char const *cp2
, uz n
){
172 return su_cs_cmp_case_n(cp1
, cp2
, n
);
174 static char *copy(char *dst
, char const *src
, uz n
){
175 return su_cs_copy_n(dst
, src
, n
);
177 static char *dup(char const *buf
, uz len
, u32 estate
=state::none
){
178 return su_cs_dup_cbuf(buf
, len
, estate
);
180 static char *dup(char const *cp
, u32 estate
=state::none
){
181 return su_cs_dup(cp
, estate
);
183 static char *find(char const *cp
, char const *x
) {return su_cs_find(cp
, x
);}
184 static char *find(char const *cp
, char x
) {return su_cs_find_c(cp
, x
);}
185 static uz
hash(char const *buf
, uz len
) {return su_cs_hash_cbuf(buf
, len
);}
186 static uz
hash(char const *cp
) {return su_cs_hash(cp
);}
187 static uz
hash_case(char const *buf
, uz len
){
188 return su_cs_hash_case_cbuf(buf
, len
);
190 static uz
hash_case(char const *cp
) {return su_cs_hash_case(cp
);}
191 static uz
len(char const *cp
) {return su_cs_len(cp
);}
192 static char *pcopy(char *dst
, char const *src
){
193 return su_cs_pcopy(dst
, src
);
195 static char *pcopy(char *dst
, char const *src
, uz n
){
196 return su_cs_pcopy_n(dst
, src
, n
);
198 static char *rfind(char const *cp
, char x
) {return su_cs_rfind_c(cp
, x
);}
199 static char *sep(char **iolist
, char sep
, boole ignore_empty
){
200 return su_cs_sep_c(iolist
, sep
, ignore_empty
);
202 static char *sep_escable(char **iolist
, char sep
, boole ignore_empty
){
203 return su_cs_sep_escable_c(iolist
, sep
, ignore_empty
);
205 static boole
starts_with(char const *cp
, char const *x
){
206 return su_cs_starts_with(cp
, x
);
208 static s32
to_lower(s32 c
) {return su_cs_to_lower(c
);}
209 static s32
to_upper(s32 c
) {return su_cs_to_upper(c
);}
212 class auto_type_toolbox
<char*>{
214 static type_toolbox
<char*> const *get_instance(void){
215 return cs::type_toolbox
;
219 class auto_type_toolbox
<char const*>{
221 static type_toolbox
<char const*> const *get_instance(void){
222 return cs::const_type_toolbox
;
226 # include <su/code-ou.h>
227 #endif /* !C_LANG || CXX_DOXYGEN */