1 /* string.h
2  *  Copyright (C) 2001-2003, Parrot Foundation.
3  *  Overview:
4  *     This is the api header for the string subsystem
5  *  Data Structure and Algorithms:
6  *  History:
7  *  Notes:
8  *  References:
9  */
10 
11 #ifndef PARROT_STRING_H_GUARD
12 #define PARROT_STRING_H_GUARD
13 
14 #include "parrot/core_types.h"
15 #include "parrot/config.h"
16 
17 #ifdef PARROT_IN_CORE
18 
19 #include "parrot/compiler.h"
20 #include "parrot/pobj.h"
21 #include "parrot/cclass.h"
22 
23 #define STREQ(x, y)  (strcmp((x), (y))==0)
24 #define STRNEQ(x, y) (strcmp((x), (y))!=0)
25 
26 #define STRING_length(src) ((src) ? (src)->strlen : 0U)
27 #define STRING_byte_length(src) ((src) ? (src)->bufused : 0U)
28 #define STRING_max_bytes_per_codepoint(src) ((src)->encoding)->max_bytes_per_codepoint
29 
30 #define STRING_equal(interp, lhs, rhs) ((lhs)->encoding)->equal((interp), (lhs), (rhs))
31 #define STRING_compare(interp, lhs, rhs) ((lhs)->encoding)->compare((interp), (lhs), (rhs))
32 #define STRING_index(interp, src, search, offset) \
33     ((src)->encoding)->index((interp), (src), (search), (offset))
34 #define STRING_rindex(interp, src, search, offset) \
35     ((src)->encoding)->rindex((interp), (src), (search), (offset))
36 #define STRING_hash(i, src, seed) ((src)->encoding)->hash((i), (src), (seed))
37 
38 #define STRING_scan(i, src) ((src)->encoding)->scan((i), (src))
39 #define STRING_ord(i, src, offset) ((src)->encoding)->ord((i), (src), (offset))
40 #define STRING_substr(i, src, offset, count) \
41     ((src)->encoding)->substr((i), (src), (offset), (count))
42 
43 #define STRING_is_cclass(interp, flags, src, offset) \
44     ((src)->encoding)->is_cclass((interp), (flags), (src), (offset))
45 #define STRING_find_cclass(interp, flags, src, offset, count) \
46     ((src)->encoding)->find_cclass((interp), (flags), (src), (offset), (count))
47 #define STRING_find_not_cclass(interp, flags, src, offset, count) \
48     ((src)->encoding)->find_not_cclass((interp), (flags), (src), (offset), (count))
49 
50 #define STRING_get_graphemes(interp, src, offset, count) \
51     ((src)->encoding)->get_graphemes((interp), (src), (offset), (count))
52 #define STRING_compose(interp, src) ((src)->encoding)->compose((interp), (src))
53 #define STRING_decompose(interp, src) ((src)->encoding)->decompose((interp), (src))
54 
55 #define STRING_upcase(interp, src) ((src)->encoding)->upcase((interp), (src))
56 #define STRING_downcase(interp, src) ((src)->encoding)->downcase((interp), (src))
57 #define STRING_titlecase(interp, src) ((src)->encoding)->titlecase((interp), (src))
58 #define STRING_upcase_first(interp, src) ((src)->encoding)->upcase_first((interp), (src))
59 #define STRING_downcase_first(interp, src) ((src)->encoding)->downcase_first((interp), (src))
60 #define STRING_titlecase_first(interp, src) ((src)->encoding)->titlecase_first((interp), (src))
61 
62 #define STRING_ITER_INIT(i, iter) (iter)->charpos = (iter)->bytepos = 0
63 #define STRING_iter_get(i, str, iter, offset) \
64     ((str)->encoding)->iter_get((i), (str), (iter), (offset))
65 #define STRING_iter_skip(i, str, iter, skip) \
66     ((str)->encoding)->iter_skip((i), (str), (iter), (skip))
67 #define STRING_iter_get_and_advance(i, str, iter) \
68     ((str)->encoding)->iter_get_and_advance((i), (str), (iter))
69 #define STRING_iter_set_and_advance(i, str, iter, c) \
70     ((str)->encoding)->iter_set_and_advance((i), (str), (iter), (c))
71 
72 /* stringinfo parameters */
73 
74 /* &gen_from_def(stringinfo.pasm) */
75 
76 #define STRINGINFO_HEADER   1
77 #define STRINGINFO_STRSTART 2
78 #define STRINGINFO_BUFLEN   3
79 #define STRINGINFO_FLAGS    4
80 #define STRINGINFO_BUFUSED  5
81 #define STRINGINFO_STRLEN   6
82 
83 /* &end_gen */
84 
85 typedef struct parrot_string_t STRING;
86 
87 /* String iterator */
88 typedef struct string_iterator_t {
89     UINTVAL bytepos;
90     UINTVAL charpos;
91 } String_iter;
92 
93 typedef struct _Parrot_String_Bounds {
94     UINTVAL bytes;
95     INTVAL  chars;
96     INTVAL  delim;
97 } Parrot_String_Bounds;
98 
99 /* constructors */
100 typedef STRING * (*str_vtable_to_encoding_t)(PARROT_INTERP, ARGIN(const STRING *src));
101 typedef STRING * (*str_vtable_chr_t)(PARROT_INTERP, UINTVAL codepoint);
102 
103 typedef INTVAL   (*str_vtable_equal_t)(PARROT_INTERP, ARGIN(const STRING *lhs),
104                                             ARGIN(const STRING *rhs));
105 typedef INTVAL   (*str_vtable_compare_t)(PARROT_INTERP, ARGIN(const STRING *lhs),
106                                             ARGIN(const STRING *rhs));
107 typedef INTVAL   (*str_vtable_index_t)(PARROT_INTERP, ARGIN(const STRING *src),
108                                             ARGIN(const STRING *search_string), INTVAL offset);
109 typedef INTVAL   (*str_vtable_rindex_t)(PARROT_INTERP, ARGIN(const STRING *src),
110                                             ARGIN(const STRING *search_string), INTVAL offset);
111 typedef size_t   (*str_vtable_hash_t)(PARROT_INTERP, ARGIN(const STRING *s), size_t hashval);
112 
113 typedef void     (*str_vtable_scan_t)(PARROT_INTERP, ARGMOD(STRING *src));
114 typedef INTVAL   (*str_vtable_partial_scan_t)(PARROT_INTERP, ARGIN(const char *buf),
115                                             ARGMOD(Parrot_String_Bounds *bounds));
116 typedef UINTVAL  (*str_vtable_ord_t)(PARROT_INTERP, ARGIN(const STRING *src), INTVAL offset);
117 typedef STRING * (*str_vtable_substr_t)(PARROT_INTERP, ARGIN(const STRING *src),
118                                             INTVAL offset, INTVAL count);
119 
120 /* character classes */
121 typedef INTVAL   (*str_vtable_is_cclass_t)(PARROT_INTERP, INTVAL,
122                                             ARGIN(const STRING *src), UINTVAL offset);
123 typedef INTVAL   (*str_vtable_find_cclass_t)(PARROT_INTERP, INTVAL,
124                                             ARGIN(const STRING *src), UINTVAL offset,
125                                             UINTVAL count);
126 typedef INTVAL   (*str_vtable_find_not_cclass_t)(PARROT_INTERP, INTVAL,
127                                             ARGIN(const STRING *src), UINTVAL offset,
128                                             UINTVAL count);
129 
130 /* graphemes */
131 typedef STRING * (*str_vtable_get_graphemes_t)(PARROT_INTERP, ARGIN(const STRING *src),
132                                             UINTVAL offset, UINTVAL count);
133 typedef STRING * (*str_vtable_compose_t)(PARROT_INTERP, ARGIN(const STRING *src));
134 typedef STRING * (*str_vtable_decompose_t)(PARROT_INTERP, ARGIN(const STRING *src));
135 
136 /* case conversion, TODO: move to single function with a flag */
137 typedef STRING * (*str_vtable_upcase_t)(PARROT_INTERP, ARGIN(const STRING *src));
138 typedef STRING * (*str_vtable_downcase_t)(PARROT_INTERP, ARGIN(const STRING *src));
139 typedef STRING * (*str_vtable_titlecase_t)(PARROT_INTERP, ARGIN(const STRING *src));
140 typedef STRING * (*str_vtable_upcase_first_t)(PARROT_INTERP, ARGIN(const STRING *src));
141 typedef STRING * (*str_vtable_downcase_first_t)(PARROT_INTERP, ARGIN(const STRING *src));
142 typedef STRING * (*str_vtable_titlecase_first_t)(PARROT_INTERP, ARGIN(const STRING *src));
143 
144 /* iterator functions */
145 typedef UINTVAL  (*str_vtable_iter_get_t)(PARROT_INTERP, ARGIN(const STRING *str),
146                     ARGIN(const String_iter *i), INTVAL offset);
147 typedef void     (*str_vtable_iter_skip_t)(PARROT_INTERP, ARGIN(const STRING *str),
148                     ARGIN(String_iter *i), INTVAL skip);
149 typedef UINTVAL  (*str_vtable_iter_get_and_advance_t)(PARROT_INTERP,
150                     ARGIN(const STRING *str), ARGMOD(String_iter *i));
151 typedef void     (*str_vtable_iter_set_and_advance_t)(PARROT_INTERP, ARGIN(STRING *str),
152                     ARGMOD(String_iter *i), UINTVAL c);
153 
154 struct _str_vtable {
155     int         num;
156     const char *name;
157     STRING     *name_str;
158     UINTVAL     bytes_per_unit;
159     UINTVAL     max_bytes_per_codepoint;
160 
161     str_vtable_to_encoding_t            to_encoding;
162     str_vtable_chr_t                    chr;
163 
164     str_vtable_equal_t                  equal;
165     str_vtable_compare_t                compare;
166     str_vtable_index_t                  index;
167     str_vtable_rindex_t                 rindex;
168     str_vtable_hash_t                   hash;
169 
170     str_vtable_scan_t                   scan;
171     str_vtable_partial_scan_t           partial_scan;
172     str_vtable_ord_t                    ord;
173     str_vtable_substr_t                 substr;
174 
175     str_vtable_is_cclass_t              is_cclass;
176     str_vtable_find_cclass_t            find_cclass;
177     str_vtable_find_not_cclass_t        find_not_cclass;
178 
179     str_vtable_get_graphemes_t          get_graphemes;
180     str_vtable_compose_t                compose;
181     str_vtable_decompose_t              decompose;
182 
183     str_vtable_upcase_t                 upcase;
184     str_vtable_downcase_t               downcase;
185     str_vtable_titlecase_t              titlecase;
186     str_vtable_upcase_first_t           upcase_first;
187     str_vtable_downcase_first_t         downcase_first;
188     str_vtable_titlecase_first_t        titlecase_first;
189 
190     str_vtable_iter_get_t               iter_get;
191     str_vtable_iter_skip_t              iter_skip;
192     str_vtable_iter_get_and_advance_t   iter_get_and_advance;
193     str_vtable_iter_set_and_advance_t   iter_set_and_advance;
194 };
195 
196 typedef struct _str_vtable STR_VTABLE;
197 
198 #endif /* PARROT_IN_CORE */
199 #endif /* PARROT_STRING_H_GUARD */
200 
201 /*
202  * Local variables:
203  *   c-file-style: "parrot"
204  * End:
205  * vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
206  */
207