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