Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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