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