1 /**********************************************************************
6 created at: Thu May 24 11:49:41 JST 2007
8 Copyright (C) 2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #ifndef RUBY_ENCODING_H
13 #define RUBY_ENCODING_H 1
16 #include "ruby/oniguruma.h"
18 #define ENCODING_INLINE_MAX 1023
19 #define ENCODING_SHIFT (FL_USHIFT+10)
20 #define ENCODING_MASK (ENCODING_INLINE_MAX<<ENCODING_SHIFT)
22 #define ENCODING_SET_INLINED(obj,i) do {\
23 RBASIC(obj)->flags &= ~ENCODING_MASK;\
24 RBASIC(obj)->flags |= (i) << ENCODING_SHIFT;\
26 #define ENCODING_SET(obj,i) do {\
27 VALUE rb_encoding_set_obj = (obj); \
28 int encoding_set_enc_index = (i); \
29 if (encoding_set_enc_index < ENCODING_INLINE_MAX) \
30 ENCODING_SET_INLINED(rb_encoding_set_obj, encoding_set_enc_index); \
32 rb_enc_set_index(rb_encoding_set_obj, encoding_set_enc_index); \
35 #define ENCODING_GET_INLINED(obj) ((RBASIC(obj)->flags & ENCODING_MASK)>>ENCODING_SHIFT)
36 #define ENCODING_GET(obj) \
37 (ENCODING_GET_INLINED(obj) != ENCODING_INLINE_MAX ? \
38 ENCODING_GET_INLINED(obj) : \
39 rb_enc_get_index(obj))
41 #define ENCODING_IS_ASCII8BIT(obj) (ENCODING_GET_INLINED(obj) == 0)
43 #define ENCODING_MAXNAMELEN 42
45 #define ENC_CODERANGE_MASK (FL_USER8|FL_USER9)
46 #define ENC_CODERANGE_UNKNOWN 0
47 #define ENC_CODERANGE_7BIT FL_USER8
48 #define ENC_CODERANGE_VALID FL_USER9
49 #define ENC_CODERANGE_BROKEN (FL_USER8|FL_USER9)
50 #define ENC_CODERANGE(obj) (RBASIC(obj)->flags & ENC_CODERANGE_MASK)
51 #define ENC_CODERANGE_ASCIIONLY(obj) (ENC_CODERANGE(obj) == ENC_CODERANGE_7BIT)
52 #define ENC_CODERANGE_SET(obj,cr) (RBASIC(obj)->flags = \
53 (RBASIC(obj)->flags & ~ENC_CODERANGE_MASK) | (cr))
54 #define ENC_CODERANGE_CLEAR(obj) ENC_CODERANGE_SET(obj,0)
56 /* assumed ASCII compatiblity */
57 #define ENC_CODERANGE_AND(a, b) \
58 (a == ENC_CODERANGE_7BIT ? b : \
59 a == ENC_CODERANGE_VALID ? (b == ENC_CODERANGE_7BIT ? ENC_CODERANGE_VALID : b) : \
60 ENC_CODERANGE_UNKNOWN)
62 #define ENCODING_CODERANGE_SET(obj, encindex, cr) \
64 VALUE rb_encoding_coderange_obj = (obj); \
65 ENCODING_SET(rb_encoding_coderange_obj, (encindex)); \
66 ENC_CODERANGE_SET(rb_encoding_coderange_obj, (cr)); \
69 typedef OnigEncodingType rb_encoding
;
71 int rb_enc_replicate(const char *, rb_encoding
*);
72 int rb_define_dummy_encoding(const char *);
73 #define rb_enc_to_index(enc) ((enc) ? ENC_TO_ENCINDEX(enc) : 0)
74 int rb_enc_get_index(VALUE obj
);
75 void rb_enc_set_index(VALUE obj
, int encindex
);
76 int rb_enc_find_index(const char *name
);
77 int rb_to_encoding_index(VALUE
);
78 rb_encoding
* rb_to_encoding(VALUE
);
79 rb_encoding
* rb_enc_get(VALUE
);
80 rb_encoding
* rb_enc_compatible(VALUE
,VALUE
);
81 rb_encoding
* rb_enc_check(VALUE
,VALUE
);
82 VALUE
rb_enc_associate_index(VALUE
, int);
83 VALUE
rb_enc_associate(VALUE
, rb_encoding
*);
84 void rb_enc_copy(VALUE dst
, VALUE src
);
86 VALUE
rb_enc_str_new(const char*, long, rb_encoding
*);
87 VALUE
rb_enc_reg_new(const char*, long, rb_encoding
*, int);
88 PRINTF_ARGS(VALUE
rb_enc_sprintf(rb_encoding
*, const char*, ...), 2, 3);
89 VALUE
rb_enc_vsprintf(rb_encoding
*, const char*, va_list);
90 long rb_enc_strlen(const char*, const char*, rb_encoding
*);
91 char* rb_enc_nth(const char*, const char*, int, rb_encoding
*);
92 VALUE
rb_obj_encoding(VALUE
);
93 VALUE
rb_enc_str_buf_cat(VALUE str
, const char *ptr
, long len
, rb_encoding
*enc
);
95 /* index -> rb_encoding */
96 rb_encoding
* rb_enc_from_index(int idx
);
98 /* name -> rb_encoding */
99 rb_encoding
* rb_enc_find(const char *name
);
101 /* encoding -> name */
102 #define rb_enc_name(enc) (enc)->name
104 /* encoding -> minlen/maxlen */
105 #define rb_enc_mbminlen(enc) (enc)->min_enc_len
106 #define rb_enc_mbmaxlen(enc) (enc)->max_enc_len
108 /* -> mbclen (no error notification: 0 < ret <= e-p, no exception) */
109 int rb_enc_mbclen(const char *p
, const char *e
, rb_encoding
*enc
);
111 /* -> chlen, invalid or needmore */
112 int rb_enc_precise_mbclen(const char *p
, const char *e
, rb_encoding
*enc
);
113 #define MBCLEN_CHARFOUND_P(ret) ONIGENC_MBCLEN_CHARFOUND_P(ret)
114 #define MBCLEN_CHARFOUND_LEN(ret) ONIGENC_MBCLEN_CHARFOUND_LEN(ret)
115 #define MBCLEN_INVALID_P(ret) ONIGENC_MBCLEN_INVALID_P(ret)
116 #define MBCLEN_NEEDMORE_P(ret) ONIGENC_MBCLEN_NEEDMORE_P(ret)
117 #define MBCLEN_NEEDMORE_LEN(ret) ONIGENC_MBCLEN_NEEDMORE_LEN(ret)
119 /* -> 0x00..0x7f, -1 */
120 int rb_enc_ascget(const char *p
, const char *e
, int *len
, rb_encoding
*enc
);
122 /* -> code or raise exception */
123 int rb_enc_codepoint(const char *p
, const char *e
, rb_encoding
*enc
);
124 #define rb_enc_mbc_to_codepoint(p, e, enc) ONIGENC_MBC_TO_CODE(enc,(UChar*)(p),(UChar*)(e))
126 /* -> codelen>0 or raise exception */
127 int rb_enc_codelen(int code
, rb_encoding
*enc
);
129 /* code,ptr,encoding -> write buf */
130 #define rb_enc_mbcput(c,buf,enc) ONIGENC_CODE_TO_MBC(enc,c,(UChar*)(buf))
132 /* ptr, ptr, encoding -> prev_char */
133 #define rb_enc_prev_char(s,p,enc) (char *)onigenc_get_prev_char_head(enc,(UChar*)(s),(UChar*)(p))
134 /* ptr, ptr, encoding -> next_char */
135 #define rb_enc_left_char_head(s,p,enc) (char *)onigenc_get_left_adjust_char_head(enc,(UChar*)(s),(UChar*)(p))
136 #define rb_enc_right_char_head(s,p,enc) (char *)onigenc_get_right_adjust_char_head(enc,(UChar*)(s),(UChar*)(p))
138 /* ptr, ptr, encoding -> newline_or_not */
139 #define rb_enc_is_newline(p,end,enc) ONIGENC_IS_MBC_NEWLINE(enc,(UChar*)(p),(UChar*)(end))
141 #define rb_enc_isctype(c,t,enc) ONIGENC_IS_CODE_CTYPE(enc,c,t)
142 #define rb_enc_isascii(c,enc) ONIGENC_IS_CODE_ASCII(c)
143 #define rb_enc_isalpha(c,enc) ONIGENC_IS_CODE_ALPHA(enc,c)
144 #define rb_enc_islower(c,enc) ONIGENC_IS_CODE_LOWER(enc,c)
145 #define rb_enc_isupper(c,enc) ONIGENC_IS_CODE_UPPER(enc,c)
146 #define rb_enc_ispunct(c,enc) ONIGENC_IS_CODE_PUNCT(enc,c)
147 #define rb_enc_isalnum(c,enc) ONIGENC_IS_CODE_ALNUM(enc,c)
148 #define rb_enc_isprint(c,enc) ONIGENC_IS_CODE_PRINT(enc,c)
149 #define rb_enc_isspace(c,enc) ONIGENC_IS_CODE_SPACE(enc,c)
150 #define rb_enc_isdigit(c,enc) ONIGENC_IS_CODE_DIGIT(enc,c)
152 #define rb_enc_asciicompat(enc) (rb_enc_mbminlen(enc)==1 && !rb_enc_dummy_p(enc))
154 int rb_enc_casefold(char *to
, const char *p
, const char *e
, rb_encoding
*enc
);
155 int rb_enc_toupper(int c
, rb_encoding
*enc
);
156 int rb_enc_tolower(int c
, rb_encoding
*enc
);
157 ID
rb_intern3(const char*, long, rb_encoding
*);
158 ID
rb_interned_id_p(const char *, long, rb_encoding
*);
159 int rb_enc_symname_p(const char*, rb_encoding
*);
160 int rb_enc_str_coderange(VALUE
);
161 long rb_str_coderange_scan_restartable(const char*, const char*, rb_encoding
*, int*);
162 int rb_enc_str_asciionly_p(VALUE
);
163 #define rb_enc_str_asciicompat_p(str) rb_enc_asciicompat(rb_enc_get(str))
164 VALUE
rb_enc_from_encoding(rb_encoding
*enc
);
165 rb_encoding
*rb_ascii8bit_encoding(void);
166 rb_encoding
*rb_utf8_encoding(void);
167 rb_encoding
*rb_usascii_encoding(void);
168 rb_encoding
*rb_locale_encoding(void);
169 rb_encoding
*rb_filesystem_encoding(void);
170 rb_encoding
*rb_default_external_encoding(void);
171 int rb_ascii8bit_encindex(void);
172 int rb_utf8_encindex(void);
173 int rb_usascii_encindex(void);
174 VALUE
rb_enc_default_external(void);
175 void rb_enc_set_default_external(VALUE encoding
);
176 VALUE
rb_locale_charmap(VALUE klass
);
177 long rb_memsearch(const void*,long,const void*,long,rb_encoding
*);
179 RUBY_EXTERN VALUE rb_cEncoding
;
180 #define enc_initialized_p(enc) ((enc)->ruby_encoding_index != ENC_UNINITIALIZED)
181 #define ENC_DUMMY_FLAG (1<<24)
182 #define ENC_INDEX_MASK (~(~0U<<24))
184 #define ENC_TO_ENCINDEX(enc) ((enc)->ruby_encoding_index & ENC_INDEX_MASK)
185 #define ENC_FROM_ENCINDEX(idx) (RARRAY_PTR(rb_encoding_list)[idx])
186 #define ENC_FROM_ENCODING(enc) ENC_FROM_ENCINDEX(ENC_TO_ENCINDEX(enc))
188 #define ENC_DUMMY_P(enc) ((enc)->ruby_encoding_index & ENC_DUMMY_FLAG)
189 #define ENC_SET_DUMMY(enc) ((enc)->ruby_encoding_index |= ENC_DUMMY_FLAG)
192 rb_enc_dummy_p(rb_encoding
*enc
)
194 return ENC_DUMMY_P(enc
) != 0;
197 VALUE
rb_str_transcode(VALUE str
, VALUE to
);
202 econv_invalid_byte_sequence
,
203 econv_undefined_conversion
,
204 econv_destination_buffer_full
,
205 econv_source_buffer_empty
,
207 econv_output_followed_by_input
,
213 struct rb_transcoding
*tc
;
214 unsigned char *out_buf_start
;
215 unsigned char *out_data_start
;
216 unsigned char *out_data_end
;
217 unsigned char *out_buf_end
;
218 rb_econv_result_t last_result
;
222 unsigned char *in_buf_start
;
223 unsigned char *in_data_start
;
224 unsigned char *in_data_end
;
225 unsigned char *in_buf_end
;
226 rb_econv_elem_t
*elems
;
229 int last_trans_index
; /* last trans, not including universal newline */
230 struct rb_transcoding
*last_tc
;
234 rb_econv_result_t result
;
235 struct rb_transcoding
*error_tc
;
236 const char *source_encoding
;
237 const char *destination_encoding
;
238 const unsigned char *error_bytes_start
;
239 size_t error_bytes_len
;
240 size_t readagain_len
;
244 /* The following fields are only for Encoding::Converter.
245 * rb_econv_open set them NULL. */
246 rb_encoding
*source_encoding
;
247 rb_encoding
*destination_encoding
;
250 rb_econv_t
*rb_econv_open(const char *source_encoding
, const char *destination_encoding
, int flags
);
251 rb_econv_result_t
rb_econv_convert(rb_econv_t
*ec
,
252 const unsigned char **source_buffer_ptr
, const unsigned char *source_buffer_end
,
253 unsigned char **destination_buffer_ptr
, unsigned char *destination_buffer_end
,
257 /* result: 0:success -1:failure */
258 int rb_econv_insert_output(rb_econv_t
*ec
,
259 const unsigned char *str
, size_t len
, const char *str_encoding
);
261 /* encoding that rb_econv_insert_output doesn't need conversion */
262 const char *rb_econv_encoding_to_insert_output(rb_econv_t
*ec
);
264 void rb_econv_close(rb_econv_t
*ec
);
266 /* flags for rb_econv_open */
267 #define ECONV_UNIVERSAL_NEWLINE_DECODER 0x100
268 #define ECONV_CRLF_NEWLINE_ENCODER 0x200
269 #define ECONV_CR_NEWLINE_ENCODER 0x400
271 /* flags for rb_econv_convert */
272 #define ECONV_PARTIAL_INPUT 0x10000
273 #define ECONV_OUTPUT_FOLLOWED_BY_INPUT 0x20000
275 #endif /* RUBY_ENCODING_H */