Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 346 lines (274 sloc) 14.658 kB
8354436 @mimaki add file header
mimaki authored
1 /*
2 ** encoding.h - Encoding class
4ec6d41 rm whitespace
roco authored
3 **
8354436 @mimaki add file header
mimaki authored
4 ** See Copyright Notice in mruby.h
5 */
e0d6430 @mimaki add mruby sources
mimaki authored
6
7 #ifndef RUBY_ENCODING_H
8 #define RUBY_ENCODING_H 1
9
10 #if defined(__cplusplus)
11 extern "C" {
12 #endif
13
14 #include <stdarg.h>
15 #include "oniguruma.h"
d114422 @matz move src/mdata.h to include/mruby/data.h
matz authored
16 #include "mruby/data.h"
e0d6430 @mimaki add mruby sources
mimaki authored
17
18 #define FL_USHIFT 12
19
20 #define FL_USER0 (((int)1)<<(FL_USHIFT+0))
21 #define FL_USER1 (((int)1)<<(FL_USHIFT+1))
22 #define FL_USER2 (((int)1)<<(FL_USHIFT+2))
23 #define FL_USER3 (((int)1)<<(FL_USHIFT+3))
24 #define FL_USER4 (((int)1)<<(FL_USHIFT+4))
25 #define FL_USER5 (((int)1)<<(FL_USHIFT+5))
26 #define FL_USER6 (((int)1)<<(FL_USHIFT+6))
27 #define FL_USER7 (((int)1)<<(FL_USHIFT+7))
28 #define FL_USER8 (((int)1)<<(FL_USHIFT+8))
29 #define FL_USER9 (((int)1)<<(FL_USHIFT+9))
30
31 #define ENCODING_INLINE_MAX 1023
32 /* 1023 = 0x03FF */
33 /*#define ENCODING_SHIFT (FL_USHIFT+10)*/
34 #define ENCODING_SHIFT (10)
35 #define ENCODING_MASK (((unsigned int)ENCODING_INLINE_MAX)<<ENCODING_SHIFT)
36
37 #define ENCODING_SET_INLINED(obj,i) do {\
38 RBASIC(obj)->flags &= ~ENCODING_MASK;\
39 RBASIC(obj)->flags |= (unsigned int)(i) << ENCODING_SHIFT;\
40 } while (0)
41 #define ENCODING_SET(mrb, obj,i) do {\
42 mrb_value mrb_encoding_set_obj = (obj); \
43 int encoding_set_enc_index = (i); \
44 if (encoding_set_enc_index < ENCODING_INLINE_MAX) \
45 ENCODING_SET_INLINED(mrb_encoding_set_obj, encoding_set_enc_index); \
46 else \
47 mrb_enc_set_index(mrb, mrb_encoding_set_obj, encoding_set_enc_index); \
48 } while (0)
49
50 #define ENCODING_GET_INLINED(obj) (unsigned int)((RSTRING(obj)->flags & ENCODING_MASK)>>ENCODING_SHIFT)
51 #define ENCODING_GET(mrb, obj) \
52 (ENCODING_GET_INLINED(obj) != ENCODING_INLINE_MAX ? \
53 ENCODING_GET_INLINED(obj) : \
54 mrb_enc_get_index(mrb, obj))
55
56 #define ENCODING_IS_ASCII8BIT(obj) (ENCODING_GET_INLINED(obj) == 0)
57
58 #define ENCODING_MAXNAMELEN 42
59
4ec6d41 rm whitespace
roco authored
60 #define ENC_CODERANGE_MASK ((int)(FL_USER8|FL_USER9))
61 #define ENC_CODERANGE_UNKNOWN 0
62 #define ENC_CODERANGE_7BIT ((int)FL_USER8)
63 #define ENC_CODERANGE_VALID ((int)FL_USER9)
64 #define ENC_CODERANGE_BROKEN ((int)(FL_USER8|FL_USER9))
e0d6430 @mimaki add mruby sources
mimaki authored
65 #define ENC_CODERANGE(obj) ((int)(RSTRING(obj)->flags & ENC_CODERANGE_MASK))
66 #define ENC_CODERANGE_ASCIIONLY(obj) (ENC_CODERANGE(obj) == ENC_CODERANGE_7BIT)
67 #ifdef INCLUDE_ENCODING
68 #define ENC_CODERANGE_SET(obj,cr) (RSTRING(obj)->flags = \
4ec6d41 rm whitespace
roco authored
69 (RSTRING(obj)->flags & ~ENC_CODERANGE_MASK) | (cr))
e0d6430 @mimaki add mruby sources
mimaki authored
70 #else
71 #define ENC_CODERANGE_SET(obj,cr)
72 #endif //INCLUDE_ENCODING
73 #define ENC_CODERANGE_CLEAR(obj) ENC_CODERANGE_SET(obj,0)
74
75 /* assumed ASCII compatibility */
76 #define ENC_CODERANGE_AND(a, b) \
77 (a == ENC_CODERANGE_7BIT ? b : \
78 a == ENC_CODERANGE_VALID ? (b == ENC_CODERANGE_7BIT ? ENC_CODERANGE_VALID : b) : \
79 ENC_CODERANGE_UNKNOWN)
80
81 #define ENCODING_CODERANGE_SET(mrb, obj, encindex, cr) \
82 do { \
83 mrb_value mrb_encoding_coderange_obj = (obj); \
84 ENCODING_SET(mrb, mrb_encoding_coderange_obj, (encindex)); \
85 ENC_CODERANGE_SET(mrb_encoding_coderange_obj, (cr)); \
86 } while (0)
87
88 typedef OnigEncodingType mrb_encoding;
89
90 /* mrb_encoding * -> name */
91 #define mrb_enc_name(enc) (enc)->name
92 int mrb_enc_get_index(mrb_state *mrb, mrb_value obj);
93
94 int mrb_enc_replicate(mrb_state *, const char *, mrb_encoding *);
95 int mrb_define_dummy_encoding(mrb_state *mrb, const char *);
96 #define mrb_enc_to_index(enc) ((enc) ? ENC_TO_ENCINDEX(enc) : 0)
97 void mrb_enc_set_index(mrb_state *mrb, mrb_value obj, int encindex);
98 int mrb_enc_find_index(mrb_state *mrb, const char *name);
99 int mrb_to_encoding_index(mrb_state *mrb, mrb_value);
100 mrb_encoding* mrb_to_encoding(mrb_state *mrb, mrb_value);
101 mrb_encoding* mrb_enc_get(mrb_state *, mrb_value);
102 mrb_encoding* mrb_enc_compatible(mrb_state *, mrb_value, mrb_value);
103 mrb_encoding* mrb_enc_check(mrb_state *, mrb_value, mrb_value);
104 mrb_value mrb_enc_associate_index(mrb_state *mrb, mrb_value, int);
105 #ifdef INCLUDE_ENCODING
106 mrb_value mrb_enc_associate(mrb_state *mrb, mrb_value, mrb_encoding*);
107 #else
108 #define mrb_enc_associate(mrb,value,enc)
109 #endif //INCLUDE_ENCODING
110 void mrb_enc_copy(mrb_state *mrb, mrb_value dst, mrb_value src);
111
112 mrb_value mrb_enc_reg_new(const char*, long, mrb_encoding*, int);
113 //PRINTF_ARGS(mrb_value rb_enc_sprintf(mrb_encoding *, const char*, ...), 2, 3);
114 mrb_value mrb_enc_vsprintf(mrb_encoding *, const char*, va_list);
115 long mrb_enc_strlen(const char*, const char*, mrb_encoding*);
116 char* mrb_enc_nth(mrb_state *, const char*, const char*, long, mrb_encoding*);
117 mrb_value mrb_obj_encoding(mrb_state *, mrb_value);
118 mrb_value mrb_enc_str_buf_cat(mrb_state *mrb, mrb_value str, const char *ptr, long len, mrb_encoding *enc);
119 mrb_value rb_enc_uint_chr(mrb_state *mrb, unsigned int code, mrb_encoding *enc);
120
121 mrb_value mrb_external_str_new_with_enc(mrb_state *mrb, const char *ptr, long len, mrb_encoding *);
122 mrb_value mrb_str_export_to_enc(mrb_value, mrb_encoding *);
123
124 /* index -> mrb_encoding */
125 mrb_encoding* mrb_enc_from_index(mrb_state *mrb, int idx);
126
127 /* name -> mrb_encoding */
128 mrb_encoding * mrb_enc_find(mrb_state *mrb, const char *name);
129
130 /* mrb_encoding * -> name */
131 #define mrb_enc_name(enc) (enc)->name
132
133 /* mrb_encoding * -> minlen/maxlen */
134 #define mrb_enc_mbminlen(enc) (enc)->min_enc_len
135 #define mrb_enc_mbmaxlen(enc) (enc)->max_enc_len
136
137 /* -> mbclen (no error notification: 0 < ret <= e-p, no exception) */
138 int mrb_enc_mbclen(const char *p, const char *e, mrb_encoding *enc);
139
140 /* -> mbclen (only for valid encoding) */
141 int mrb_enc_fast_mbclen(const char *p, const char *e, mrb_encoding *enc);
142
143 /* -> chlen, invalid or needmore */
144 int mrb_enc_precise_mbclen(const char *p, const char *e, mrb_encoding *enc);
145 #define MBCLEN_CHARFOUND_P(ret) ONIGENC_MBCLEN_CHARFOUND_P(ret)
146 #define MBCLEN_CHARFOUND_LEN(ret) ONIGENC_MBCLEN_CHARFOUND_LEN(ret)
147 #define MBCLEN_INVALID_P(ret) ONIGENC_MBCLEN_INVALID_P(ret)
148 #define MBCLEN_NEEDMORE_P(ret) ONIGENC_MBCLEN_NEEDMORE_P(ret)
149 #define MBCLEN_NEEDMORE_LEN(ret) ONIGENC_MBCLEN_NEEDMORE_LEN(ret)
150
151 /* -> 0x00..0x7f, -1 */
152 int mrb_enc_ascget(mrb_state *mrb, const char *p, const char *e, int *len, mrb_encoding *enc);
153
154
155 /* -> code (and len) or raise exception */
156 unsigned int mrb_enc_codepoint_len(mrb_state *mrb, const char *p, const char *e, int *len, mrb_encoding *enc);
157
158 /* prototype for obsolete function */
159 unsigned int mrb_enc_codepoint(mrb_state *mrb, const char *p, const char *e, mrb_encoding *enc);
160 /* overriding macro */
161 #define mrb_enc_codepoint(mrb,p,e,enc) mrb_enc_codepoint_len((mrb),(p),(e),0,(enc))
162 #define mrb_enc_mbc_to_codepoint(p, e, enc) ONIGENC_MBC_TO_CODE(enc,(UChar*)(p),(UChar*)(e))
163
164 /* -> codelen>0 or raise exception */
165 #ifdef INCLUDE_ENCODING
166 int mrb_enc_codelen(mrb_state *mrb, int code, mrb_encoding *enc);
167 #else
168 #define mrb_enc_codelen(mrb,code,enc) 1
169 #endif //INCLUDE_ENCODING
170
171 /* code,ptr,encoding -> write buf */
64fc4ac @matz resolve conflict
matz authored
172 #define mrb_enc_mbcput(c,buf,enc) ((*(buf) = (char)(c)),1)
e0d6430 @mimaki add mruby sources
mimaki authored
173
174 /* start, ptr, end, encoding -> prev_char */
175 #define mrb_enc_prev_char(s,p,e,enc) (char *)onigenc_get_prev_char_head(enc,(UChar*)(s),(UChar*)(p),(UChar*)(e))
176 /* start, ptr, end, encoding -> next_char */
177 #define mrb_enc_left_char_head(s,p,e,enc) (char *)onigenc_get_left_adjust_char_head(enc,(UChar*)(s),(UChar*)(p),(UChar*)(e))
178 #define mrb_enc_right_char_head(s,p,e,enc) (char *)onigenc_get_right_adjust_char_head(enc,(UChar*)(s),(UChar*)(p),(UChar*)(e))
179
180 /* ptr, ptr, encoding -> newline_or_not */
181 #define mrb_enc_is_newline(p,end,enc) ONIGENC_IS_MBC_NEWLINE(enc,(UChar*)(p),(UChar*)(end))
182
183 #define mrb_enc_isctype(c,t,enc) ONIGENC_IS_CODE_CTYPE(enc,c,t)
184 #define mrb_enc_isascii(c,enc) ONIGENC_IS_CODE_ASCII(c)
185 #define mrb_enc_isalpha(c,enc) ONIGENC_IS_CODE_ALPHA(enc,c)
186 #define mrb_enc_islower(c,enc) ONIGENC_IS_CODE_LOWER(enc,c)
187 #define mrb_enc_isupper(c,enc) ONIGENC_IS_CODE_UPPER(enc,c)
188 #define mrb_enc_ispunct(c,enc) ONIGENC_IS_CODE_PUNCT(enc,c)
189 #define mrb_enc_isalnum(c,enc) ONIGENC_IS_CODE_ALNUM(enc,c)
190 #define mrb_enc_isprint(c,enc) ONIGENC_IS_CODE_PRINT(enc,c)
191 #define mrb_enc_isspace(c,enc) ONIGENC_IS_CODE_SPACE(enc,c)
192 #define mrb_enc_isdigit(c,enc) ONIGENC_IS_CODE_DIGIT(enc,c)
193
194 #define mrb_enc_asciicompat(mrb, enc) (mrb_enc_mbminlen(enc)==1 && !mrb_enc_dummy_p(enc))
195
196 int mrb_enc_casefold(char *to, const char *p, const char *e, mrb_encoding *enc);
197 int mrb_enc_toupper(int c, mrb_encoding *enc);
198 int mrb_enc_tolower(int c, mrb_encoding *enc);
199 //ID mrb_intern3(const char*, long, mrb_encoding*);
200 //ID mrb_interned_id_p(const char *, long, mrb_encoding *);
201 int mrb_enc_symname_p(const char*, mrb_encoding*);
202 int mrb_enc_symname2_p(const char*, long, mrb_encoding*);
203 int mrb_enc_str_coderange(mrb_state *mrb, mrb_value);
204 long mrb_str_coderange_scan_restartable(const char*, const char*, mrb_encoding*, int*);
205 int mrb_enc_str_asciionly_p(mrb_state *mrb, mrb_value);
206 #define mrb_enc_str_asciicompat_p(mrb, str) mrb_enc_asciicompat(mrb, mrb_enc_get(mrb, str))
207 mrb_value mrb_enc_from_encoding(mrb_state *mrb, mrb_encoding *enc);
208 int mrb_enc_unicode_p(mrb_encoding *enc);
209 mrb_encoding *mrb_ascii8bit_encoding(mrb_state *mrb);
210 mrb_encoding *mrb_utf8_encoding(mrb_state *mrb);
211 mrb_encoding *mrb_usascii_encoding(mrb_state *mrb);
212 mrb_encoding *mrb_locale_encoding(mrb_state *mrb);
213 mrb_encoding *mrb_filesystem_encoding(mrb_state *mrb);
214 mrb_encoding *mrb_default_external_encoding(mrb_state *mrb);
215 mrb_encoding *mrb_default_internal_encoding(mrb_state *mrb);
216 int mrb_ascii8bit_encindex(void);
217 int mrb_utf8_encindex(void);
218 int mrb_usascii_encindex(void);
219 int mrb_locale_encindex(mrb_state *mrb);
220 int mrb_filesystem_encindex(void);
221 mrb_value mrb_enc_default_external(mrb_state *mrb);
222 mrb_value mrb_enc_default_internal(mrb_state *mrb);
223 void mrb_enc_set_default_external(mrb_state *mrb, mrb_value encoding);
224 void mrb_enc_set_default_internal(mrb_state *mrb, mrb_value encoding);
225 mrb_value mrb_locale_charmap(mrb_state *mrb, mrb_value klass);
226 mrb_value mrb_usascii_str_new_cstr(mrb_state *mrb, const char *ptr);
227 int mrb_str_buf_cat_escaped_char(mrb_state *mrb, mrb_value result, unsigned int c, int unicode_p);
228
229 #define ENC_DUMMY_FLAG (1<<24)
230 #define ENC_INDEX_MASK (~(~0U<<24))
231
232 #define ENC_TO_ENCINDEX(enc) (int)((enc)->ruby_encoding_index & ENC_INDEX_MASK)
233
234 #define ENC_DUMMY_P(enc) ((enc)->ruby_encoding_index & ENC_DUMMY_FLAG)
235 #define ENC_SET_DUMMY(enc) ((enc)->ruby_encoding_index |= ENC_DUMMY_FLAG)
236
237 static inline int
238 mrb_enc_dummy_p(mrb_encoding *enc)
239 {
240 return ENC_DUMMY_P(enc) != 0;
241 }
242
243 /* econv stuff */
244
245 typedef enum {
246 econv_invalid_byte_sequence,
247 econv_undefined_conversion,
248 econv_destination_buffer_full,
249 econv_source_buffer_empty,
250 econv_finished,
251 econv_after_output,
252 econv_incomplete_input
253 } mrb_econv_result_t;
254
255 typedef struct mrb_econv_t mrb_econv_t;
256
257 mrb_value mrb_str_encode(mrb_state *mrb, mrb_value str, mrb_value to, int ecflags, mrb_value ecopts);
258 int mrb_econv_has_convpath_p(mrb_state *mrb, const char* from_encoding, const char* to_encoding);
259
260 int mrb_econv_prepare_opts(mrb_state *mrb, mrb_value opthash, mrb_value *ecopts);
261
262 mrb_econv_t *mrb_econv_open(mrb_state *mrb, const char *source_encoding, const char *destination_encoding, int ecflags);
263 mrb_econv_t *mrb_econv_open_opts(mrb_state *mrb, const char *source_encoding, const char *destination_encoding, int ecflags, mrb_value ecopts);
264
265 mrb_econv_result_t mrb_econv_convert(mrb_state *mrb, mrb_econv_t *ec,
266 const unsigned char **source_buffer_ptr, const unsigned char *source_buffer_end,
267 unsigned char **destination_buffer_ptr, unsigned char *destination_buffer_end,
268 int flags);
269 void mrb_econv_close(mrb_econv_t *ec);
270
271 /* result: 0:success -1:failure */
272 int mrb_econv_set_replacement(mrb_state *mrb, mrb_econv_t *ec, const unsigned char *str, size_t len, const char *encname);
273
274 /* result: 0:success -1:failure */
275 int mrb_econv_decorate_at_first(mrb_state *mrb, mrb_econv_t *ec, const char *decorator_name);
276 int mrb_econv_decorate_at_last(mrb_state *mrb, mrb_econv_t *ec, const char *decorator_name);
277
278 mrb_value mrb_econv_open_exc(mrb_state *mrb, const char *senc, const char *denc, int ecflags);
279
280 /* result: 0:success -1:failure */
281 int mrb_econv_insert_output(mrb_state *mrb, mrb_econv_t *ec,
282 const unsigned char *str, size_t len, const char *str_encoding);
283
284 /* encoding that mrb_econv_insert_output doesn't need conversion */
285 const char *mrb_econv_encoding_to_insert_output(mrb_econv_t *ec);
286
287 /* raise an error if the last mrb_econv_convert is error */
288 void mrb_econv_check_error(mrb_state *mrb, mrb_econv_t *ec);
289
290 /* returns an exception object or nil */
291 mrb_value mrb_econv_make_exception(mrb_state *mrb, mrb_econv_t *ec);
292
293 int mrb_econv_putbackable(mrb_econv_t *ec);
294 void mrb_econv_putback(mrb_econv_t *ec, unsigned char *p, int n);
295
296 /* returns the corresponding ASCII compatible encoding for encname,
297 * or NULL if encname is not ASCII incompatible encoding. */
298 const char *mrb_econv_asciicompat_encoding(const char *encname);
299
300 mrb_value mrb_econv_str_convert(mrb_state *mrb, mrb_econv_t *ec, mrb_value src, int flags);
301 mrb_value mrb_econv_substr_convert(mrb_state *mrb, mrb_econv_t *ec, mrb_value src, long byteoff, long bytesize, int flags);
302 mrb_value mrb_econv_str_append(mrb_state *mrb, mrb_econv_t *ec, mrb_value src, mrb_value dst, int flags);
303 mrb_value mrb_econv_substr_append(mrb_state *mrb, mrb_econv_t *ec, mrb_value src, long byteoff, long bytesize, mrb_value dst, int flags);
304
305 void mrb_econv_binmode(mrb_econv_t *ec);
306
307 /* flags for mrb_econv_open */
308
309 #define ECONV_ERROR_HANDLER_MASK 0x000000ff
310
311 #define ECONV_INVALID_MASK 0x0000000f
312 #define ECONV_INVALID_REPLACE 0x00000002
313
314 #define ECONV_UNDEF_MASK 0x000000f0
315 #define ECONV_UNDEF_REPLACE 0x00000020
316 #define ECONV_UNDEF_HEX_CHARREF 0x00000030
317
318 #define ECONV_DECORATOR_MASK 0x0000ff00
319
320 #define ECONV_UNIVERSAL_NEWLINE_DECORATOR 0x00000100
321 #define ECONV_CRLF_NEWLINE_DECORATOR 0x00001000
322 #define ECONV_CR_NEWLINE_DECORATOR 0x00002000
323 #define ECONV_XML_TEXT_DECORATOR 0x00004000
324 #define ECONV_XML_ATTR_CONTENT_DECORATOR 0x00008000
325
326 #define ECONV_STATEFUL_DECORATOR_MASK 0x00f00000
327 #define ECONV_XML_ATTR_QUOTE_DECORATOR 0x00100000
328
329 /* end of flags for mrb_econv_open */
330
331 /* flags for mrb_econv_convert */
332 #define ECONV_PARTIAL_INPUT 0x00010000
333 #define ECONV_AFTER_OUTPUT 0x00020000
334 /* end of flags for mrb_econv_convert */
335
336 int mrb_isspace(int c);
337
affb5cb @matz remove encode_class and converter_class from mrb_state
matz authored
338 #define ENCODE_CLASS (mrb_class_obj_get(mrb, "Encoding"))
339 #define CONVERTER_CLASS (mrb_class_obj_get(mrb, "Converter"))
340
e0d6430 @mimaki add mruby sources
mimaki authored
341 #if defined(__cplusplus)
342 } /* extern "C" { */
343 #endif
344
345 #endif /* RUBY_ENCODING_H */
Something went wrong with that request. Please try again.