1 /*@ Anything (locale agnostic: ASCII only) around char and char*.
3 * Copyright (c) 2001 - 2020 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>