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