00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef RUBY_ENCODING_H
00013 #define RUBY_ENCODING_H 1
00014
00015 #if defined(__cplusplus)
00016 extern "C" {
00017 #if 0
00018 }
00019 #endif
00020 #endif
00021
00022 #include <stdarg.h>
00023 #include "ruby/oniguruma.h"
00024
00025 RUBY_SYMBOL_EXPORT_BEGIN
00026
00027 #define ENCODING_INLINE_MAX 127
00028 #define ENCODING_SHIFT (FL_USHIFT+10)
00029 #define ENCODING_MASK (((VALUE)ENCODING_INLINE_MAX)<<ENCODING_SHIFT)
00030
00031 #define ENCODING_SET_INLINED(obj,i) do {\
00032 RBASIC(obj)->flags &= ~ENCODING_MASK;\
00033 RBASIC(obj)->flags |= (VALUE)(i) << ENCODING_SHIFT;\
00034 } while (0)
00035 #define ENCODING_SET(obj,i) rb_enc_set_index((obj), (i))
00036
00037 #define ENCODING_GET_INLINED(obj) (int)((RBASIC(obj)->flags & ENCODING_MASK)>>ENCODING_SHIFT)
00038 #define ENCODING_GET(obj) \
00039 (ENCODING_GET_INLINED(obj) != ENCODING_INLINE_MAX ? \
00040 ENCODING_GET_INLINED(obj) : \
00041 rb_enc_get_index(obj))
00042
00043 #define ENCODING_IS_ASCII8BIT(obj) (ENCODING_GET_INLINED(obj) == 0)
00044
00045 #define ENCODING_MAXNAMELEN 42
00046
00047 #define ENC_CODERANGE_MASK ((int)(FL_USER8|FL_USER9))
00048 #define ENC_CODERANGE_UNKNOWN 0
00049 #define ENC_CODERANGE_7BIT ((int)FL_USER8)
00050 #define ENC_CODERANGE_VALID ((int)FL_USER9)
00051 #define ENC_CODERANGE_BROKEN ((int)(FL_USER8|FL_USER9))
00052 #define ENC_CODERANGE(obj) ((int)RBASIC(obj)->flags & ENC_CODERANGE_MASK)
00053 #define ENC_CODERANGE_ASCIIONLY(obj) (ENC_CODERANGE(obj) == ENC_CODERANGE_7BIT)
00054 #define ENC_CODERANGE_SET(obj,cr) (RBASIC(obj)->flags = \
00055 (RBASIC(obj)->flags & ~ENC_CODERANGE_MASK) | (cr))
00056 #define ENC_CODERANGE_CLEAR(obj) ENC_CODERANGE_SET((obj),0)
00057
00058
00059 #define ENC_CODERANGE_AND(a, b) \
00060 ((a) == ENC_CODERANGE_7BIT ? (b) : \
00061 (a) == ENC_CODERANGE_VALID ? ((b) == ENC_CODERANGE_7BIT ? ENC_CODERANGE_VALID : (b)) : \
00062 ENC_CODERANGE_UNKNOWN)
00063
00064 #define ENCODING_CODERANGE_SET(obj, encindex, cr) \
00065 do { \
00066 VALUE rb_encoding_coderange_obj = (obj); \
00067 ENCODING_SET(rb_encoding_coderange_obj, (encindex)); \
00068 ENC_CODERANGE_SET(rb_encoding_coderange_obj, (cr)); \
00069 } while (0)
00070
00071 typedef OnigEncodingType rb_encoding;
00072
00073 int rb_char_to_option_kcode(int c, int *option, int *kcode);
00074
00075 int rb_enc_replicate(const char *, rb_encoding *);
00076 int rb_define_dummy_encoding(const char *);
00077 #define rb_enc_to_index(enc) ((enc) ? ENC_TO_ENCINDEX(enc) : 0)
00078 int rb_enc_get_index(VALUE obj);
00079 void rb_enc_set_index(VALUE obj, int encindex);
00080 int rb_enc_find_index(const char *name);
00081 int rb_to_encoding_index(VALUE);
00082 rb_encoding* rb_to_encoding(VALUE);
00083 rb_encoding* rb_find_encoding(VALUE);
00084 rb_encoding* rb_enc_get(VALUE);
00085 rb_encoding* rb_enc_compatible(VALUE,VALUE);
00086 rb_encoding* rb_enc_check(VALUE,VALUE);
00087 VALUE rb_enc_associate_index(VALUE, int);
00088 VALUE rb_enc_associate(VALUE, rb_encoding*);
00089 void rb_enc_copy(VALUE dst, VALUE src);
00090
00091 VALUE rb_enc_str_new(const char*, long, rb_encoding*);
00092 VALUE rb_enc_str_new_cstr(const char*, rb_encoding*);
00093 VALUE rb_enc_reg_new(const char*, long, rb_encoding*, int);
00094 PRINTF_ARGS(VALUE rb_enc_sprintf(rb_encoding *, const char*, ...), 2, 3);
00095 VALUE rb_enc_vsprintf(rb_encoding *, const char*, va_list);
00096 long rb_enc_strlen(const char*, const char*, rb_encoding*);
00097 char* rb_enc_nth(const char*, const char*, long, rb_encoding*);
00098 VALUE rb_obj_encoding(VALUE);
00099 VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *enc);
00100 VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc);
00101
00102 VALUE rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *);
00103 VALUE rb_str_export_to_enc(VALUE, rb_encoding *);
00104 VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to);
00105 VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts);
00106
00107 #if defined(__GNUC__) && !defined(__PCC__)
00108 #define rb_enc_str_new_cstr(str, enc) __extension__ ( \
00109 { \
00110 (__builtin_constant_p(str)) ? \
00111 rb_enc_str_new((str), (long)strlen(str), (enc)) : \
00112 rb_enc_str_new_cstr((str), (enc)); \
00113 })
00114 #endif
00115
00116 PRINTF_ARGS(NORETURN(void rb_enc_raise(rb_encoding *, VALUE, const char*, ...)), 3, 4);
00117
00118
00119 rb_encoding* rb_enc_from_index(int idx);
00120
00121
00122 rb_encoding * rb_enc_find(const char *name);
00123
00124
00125 #define rb_enc_name(enc) (enc)->name
00126
00127
00128 #define rb_enc_mbminlen(enc) (enc)->min_enc_len
00129 #define rb_enc_mbmaxlen(enc) (enc)->max_enc_len
00130
00131
00132 int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc);
00133
00134
00135 int rb_enc_fast_mbclen(const char *p, const char *e, rb_encoding *enc);
00136
00137
00138 int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc);
00139 #define MBCLEN_CHARFOUND_P(ret) ONIGENC_MBCLEN_CHARFOUND_P(ret)
00140 #define MBCLEN_CHARFOUND_LEN(ret) ONIGENC_MBCLEN_CHARFOUND_LEN(ret)
00141 #define MBCLEN_INVALID_P(ret) ONIGENC_MBCLEN_INVALID_P(ret)
00142 #define MBCLEN_NEEDMORE_P(ret) ONIGENC_MBCLEN_NEEDMORE_P(ret)
00143 #define MBCLEN_NEEDMORE_LEN(ret) ONIGENC_MBCLEN_NEEDMORE_LEN(ret)
00144
00145
00146 int rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc);
00147
00148
00149
00150 unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len, rb_encoding *enc);
00151
00152
00153 unsigned int rb_enc_codepoint(const char *p, const char *e, rb_encoding *enc);
00154
00155 #define rb_enc_codepoint(p,e,enc) rb_enc_codepoint_len((p),(e),0,(enc))
00156 #define rb_enc_mbc_to_codepoint(p, e, enc) ONIGENC_MBC_TO_CODE((enc),(UChar*)(p),(UChar*)(e))
00157
00158
00159 int rb_enc_codelen(int code, rb_encoding *enc);
00160
00161 int rb_enc_code_to_mbclen(int code, rb_encoding *enc);
00162 #define rb_enc_code_to_mbclen(c, enc) ONIGENC_CODE_TO_MBCLEN((enc), (c));
00163
00164
00165 #define rb_enc_mbcput(c,buf,enc) ONIGENC_CODE_TO_MBC((enc),(c),(UChar*)(buf))
00166
00167
00168 #define rb_enc_prev_char(s,p,e,enc) ((char *)onigenc_get_prev_char_head((enc),(UChar*)(s),(UChar*)(p),(UChar*)(e)))
00169
00170 #define rb_enc_left_char_head(s,p,e,enc) ((char *)onigenc_get_left_adjust_char_head((enc),(UChar*)(s),(UChar*)(p),(UChar*)(e)))
00171 #define rb_enc_right_char_head(s,p,e,enc) ((char *)onigenc_get_right_adjust_char_head((enc),(UChar*)(s),(UChar*)(p),(UChar*)(e)))
00172 #define rb_enc_step_back(s,p,e,n,enc) ((char *)onigenc_step_back((enc),(UChar*)(s),(UChar*)(p),(UChar*)(e),(int)(n)))
00173
00174
00175 #define rb_enc_is_newline(p,end,enc) ONIGENC_IS_MBC_NEWLINE((enc),(UChar*)(p),(UChar*)(end))
00176
00177 #define rb_enc_isctype(c,t,enc) ONIGENC_IS_CODE_CTYPE((enc),(c),(t))
00178 #define rb_enc_isascii(c,enc) ONIGENC_IS_CODE_ASCII(c)
00179 #define rb_enc_isalpha(c,enc) ONIGENC_IS_CODE_ALPHA((enc),(c))
00180 #define rb_enc_islower(c,enc) ONIGENC_IS_CODE_LOWER((enc),(c))
00181 #define rb_enc_isupper(c,enc) ONIGENC_IS_CODE_UPPER((enc),(c))
00182 #define rb_enc_ispunct(c,enc) ONIGENC_IS_CODE_PUNCT((enc),(c))
00183 #define rb_enc_isalnum(c,enc) ONIGENC_IS_CODE_ALNUM((enc),(c))
00184 #define rb_enc_isprint(c,enc) ONIGENC_IS_CODE_PRINT((enc),(c))
00185 #define rb_enc_isspace(c,enc) ONIGENC_IS_CODE_SPACE((enc),(c))
00186 #define rb_enc_isdigit(c,enc) ONIGENC_IS_CODE_DIGIT((enc),(c))
00187
00188 #define rb_enc_asciicompat(enc) (rb_enc_mbminlen(enc)==1 && !rb_enc_dummy_p(enc))
00189
00190 int rb_enc_casefold(char *to, const char *p, const char *e, rb_encoding *enc);
00191 int rb_enc_toupper(int c, rb_encoding *enc);
00192 int rb_enc_tolower(int c, rb_encoding *enc);
00193 ID rb_intern3(const char*, long, rb_encoding*);
00194 ID rb_interned_id_p(const char *, long, rb_encoding *);
00195 int rb_enc_symname_p(const char*, rb_encoding*);
00196 int rb_enc_symname2_p(const char*, long, rb_encoding*);
00197 int rb_enc_str_coderange(VALUE);
00198 long rb_str_coderange_scan_restartable(const char*, const char*, rb_encoding*, int*);
00199 int rb_enc_str_asciionly_p(VALUE);
00200 #define rb_enc_str_asciicompat_p(str) rb_enc_asciicompat(rb_enc_get(str))
00201 VALUE rb_enc_from_encoding(rb_encoding *enc);
00202 int rb_enc_unicode_p(rb_encoding *enc);
00203 rb_encoding *rb_ascii8bit_encoding(void);
00204 rb_encoding *rb_utf8_encoding(void);
00205 rb_encoding *rb_usascii_encoding(void);
00206 rb_encoding *rb_locale_encoding(void);
00207 rb_encoding *rb_filesystem_encoding(void);
00208 rb_encoding *rb_default_external_encoding(void);
00209 rb_encoding *rb_default_internal_encoding(void);
00210 #ifndef rb_ascii8bit_encindex
00211 int rb_ascii8bit_encindex(void);
00212 #endif
00213 #ifndef rb_utf8_encindex
00214 int rb_utf8_encindex(void);
00215 #endif
00216 #ifndef rb_usascii_encindex
00217 int rb_usascii_encindex(void);
00218 #endif
00219 int rb_locale_encindex(void);
00220 int rb_filesystem_encindex(void);
00221 VALUE rb_enc_default_external(void);
00222 VALUE rb_enc_default_internal(void);
00223 void rb_enc_set_default_external(VALUE encoding);
00224 void rb_enc_set_default_internal(VALUE encoding);
00225 VALUE rb_locale_charmap(VALUE klass);
00226 long rb_memsearch(const void*,long,const void*,long,rb_encoding*);
00227 char *rb_enc_path_next(const char *,const char *,rb_encoding*);
00228 char *rb_enc_path_skip_prefix(const char *,const char *,rb_encoding*);
00229 char *rb_enc_path_last_separator(const char *,const char *,rb_encoding*);
00230 char *rb_enc_path_end(const char *,const char *,rb_encoding*);
00231 const char *ruby_enc_find_basename(const char *name, long *baselen, long *alllen, rb_encoding *enc);
00232 const char *ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc);
00233 ID rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc);
00234
00235 RUBY_EXTERN VALUE rb_cEncoding;
00236 #define ENC_DUMMY_FLAG (1<<24)
00237 #define ENC_INDEX_MASK (~(~0U<<24))
00238
00239 #define ENC_TO_ENCINDEX(enc) (int)((enc)->ruby_encoding_index & ENC_INDEX_MASK)
00240
00241 #define ENC_DUMMY_P(enc) ((enc)->ruby_encoding_index & ENC_DUMMY_FLAG)
00242 #define ENC_SET_DUMMY(enc) ((enc)->ruby_encoding_index |= ENC_DUMMY_FLAG)
00243
00244 static inline int
00245 rb_enc_dummy_p(rb_encoding *enc)
00246 {
00247 return ENC_DUMMY_P(enc) != 0;
00248 }
00249
00250
00251
00252 typedef enum {
00253 econv_invalid_byte_sequence,
00254 econv_undefined_conversion,
00255 econv_destination_buffer_full,
00256 econv_source_buffer_empty,
00257 econv_finished,
00258 econv_after_output,
00259 econv_incomplete_input
00260 } rb_econv_result_t;
00261
00262 typedef struct rb_econv_t rb_econv_t;
00263
00264 VALUE rb_str_encode(VALUE str, VALUE to, int ecflags, VALUE ecopts);
00265 int rb_econv_has_convpath_p(const char* from_encoding, const char* to_encoding);
00266
00267 int rb_econv_prepare_options(VALUE opthash, VALUE *ecopts, int ecflags);
00268 int rb_econv_prepare_opts(VALUE opthash, VALUE *ecopts);
00269
00270 rb_econv_t *rb_econv_open(const char *source_encoding, const char *destination_encoding, int ecflags);
00271 rb_econv_t *rb_econv_open_opts(const char *source_encoding, const char *destination_encoding, int ecflags, VALUE ecopts);
00272
00273 rb_econv_result_t rb_econv_convert(rb_econv_t *ec,
00274 const unsigned char **source_buffer_ptr, const unsigned char *source_buffer_end,
00275 unsigned char **destination_buffer_ptr, unsigned char *destination_buffer_end,
00276 int flags);
00277 void rb_econv_close(rb_econv_t *ec);
00278
00279
00280 int rb_econv_set_replacement(rb_econv_t *ec, const unsigned char *str, size_t len, const char *encname);
00281
00282
00283 int rb_econv_decorate_at_first(rb_econv_t *ec, const char *decorator_name);
00284 int rb_econv_decorate_at_last(rb_econv_t *ec, const char *decorator_name);
00285
00286 VALUE rb_econv_open_exc(const char *senc, const char *denc, int ecflags);
00287
00288
00289 int rb_econv_insert_output(rb_econv_t *ec,
00290 const unsigned char *str, size_t len, const char *str_encoding);
00291
00292
00293 const char *rb_econv_encoding_to_insert_output(rb_econv_t *ec);
00294
00295
00296 void rb_econv_check_error(rb_econv_t *ec);
00297
00298
00299 VALUE rb_econv_make_exception(rb_econv_t *ec);
00300
00301 int rb_econv_putbackable(rb_econv_t *ec);
00302 void rb_econv_putback(rb_econv_t *ec, unsigned char *p, int n);
00303
00304
00305
00306 const char *rb_econv_asciicompat_encoding(const char *encname);
00307
00308 VALUE rb_econv_str_convert(rb_econv_t *ec, VALUE src, int flags);
00309 VALUE rb_econv_substr_convert(rb_econv_t *ec, VALUE src, long byteoff, long bytesize, int flags);
00310 VALUE rb_econv_str_append(rb_econv_t *ec, VALUE src, VALUE dst, int flags);
00311 VALUE rb_econv_substr_append(rb_econv_t *ec, VALUE src, long byteoff, long bytesize, VALUE dst, int flags);
00312 VALUE rb_econv_append(rb_econv_t *ec, const char *bytesrc, long bytesize, VALUE dst, int flags);
00313
00314 void rb_econv_binmode(rb_econv_t *ec);
00315
00316
00317
00318 #define ECONV_ERROR_HANDLER_MASK 0x000000ff
00319
00320 #define ECONV_INVALID_MASK 0x0000000f
00321 #define ECONV_INVALID_REPLACE 0x00000002
00322
00323 #define ECONV_UNDEF_MASK 0x000000f0
00324 #define ECONV_UNDEF_REPLACE 0x00000020
00325 #define ECONV_UNDEF_HEX_CHARREF 0x00000030
00326
00327 #define ECONV_DECORATOR_MASK 0x0000ff00
00328 #define ECONV_NEWLINE_DECORATOR_MASK 0x00003f00
00329 #define ECONV_NEWLINE_DECORATOR_READ_MASK 0x00000f00
00330 #define ECONV_NEWLINE_DECORATOR_WRITE_MASK 0x00003000
00331
00332 #define ECONV_UNIVERSAL_NEWLINE_DECORATOR 0x00000100
00333 #define ECONV_CRLF_NEWLINE_DECORATOR 0x00001000
00334 #define ECONV_CR_NEWLINE_DECORATOR 0x00002000
00335 #define ECONV_XML_TEXT_DECORATOR 0x00004000
00336 #define ECONV_XML_ATTR_CONTENT_DECORATOR 0x00008000
00337
00338 #define ECONV_STATEFUL_DECORATOR_MASK 0x00f00000
00339 #define ECONV_XML_ATTR_QUOTE_DECORATOR 0x00100000
00340
00341 #if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
00342 #define ECONV_DEFAULT_NEWLINE_DECORATOR ECONV_CRLF_NEWLINE_DECORATOR
00343 #else
00344 #define ECONV_DEFAULT_NEWLINE_DECORATOR 0
00345 #endif
00346
00347
00348
00349
00350 #define ECONV_PARTIAL_INPUT 0x00010000
00351 #define ECONV_AFTER_OUTPUT 0x00020000
00352
00353
00354 RUBY_SYMBOL_EXPORT_END
00355
00356 #if defined(__cplusplus)
00357 #if 0
00358 {
00359 #endif
00360 }
00361 #endif
00362
00363 #endif
00364