Ruby 1.9.2p136(2010-12-25revision30365)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# include/ruby/encoding.h

Go to the documentation of this file.

    00001 /**********************************************************************
    00002
    00003   encoding.h -
    00004
    00005   $Author: matz $
    00006   created at: Thu May 24 11:49:41 JST 2007
    00007
    00008   Copyright (C) 2007 Yukihiro Matsumoto
    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 } /* satisfy cc-mode */
    00019 #endif
    00020 #endif
    00021
    00022 #include <stdarg.h>
    00023 #include "ruby/oniguruma.h"
    00024
    00025 #define ENCODING_INLINE_MAX 1023
    00026 #define ENCODING_SHIFT (FL_USHIFT+10)
    00027 #define ENCODING_MASK (((VALUE)ENCODING_INLINE_MAX)<<ENCODING_SHIFT)
    00028
    00029 #define ENCODING_SET_INLINED(obj,i) do {\
    00030     RBASIC(obj)->flags &= ~ENCODING_MASK;\
    00031     RBASIC(obj)->flags |= (VALUE)(i) << ENCODING_SHIFT;\
    00032 } while (0)
    00033 #define ENCODING_SET(obj,i) do {\
    00034     VALUE rb_encoding_set_obj = (obj); \
    00035     int encoding_set_enc_index = (i); \
    00036     if (encoding_set_enc_index < ENCODING_INLINE_MAX) \
    00037         ENCODING_SET_INLINED(rb_encoding_set_obj, encoding_set_enc_index); \
    00038     else \
    00039         rb_enc_set_index(rb_encoding_set_obj, encoding_set_enc_index); \
    00040 } while (0)
    00041
    00042 #define ENCODING_GET_INLINED(obj) (int)((RBASIC(obj)->flags & ENCODING_MASK)>>ENCODING_SHIFT)
    00043 #define ENCODING_GET(obj) \
    00044     (ENCODING_GET_INLINED(obj) != ENCODING_INLINE_MAX ? \
    00045      ENCODING_GET_INLINED(obj) : \
    00046      rb_enc_get_index(obj))
    00047
    00048 #define ENCODING_IS_ASCII8BIT(obj) (ENCODING_GET_INLINED(obj) == 0)
    00049
    00050 #define ENCODING_MAXNAMELEN 42
    00051
    00052 #define ENC_CODERANGE_MASK      ((int)(FL_USER8|FL_USER9))
    00053 #define ENC_CODERANGE_UNKNOWN   0
    00054 #define ENC_CODERANGE_7BIT      ((int)FL_USER8)
    00055 #define ENC_CODERANGE_VALID     ((int)FL_USER9)
    00056 #define ENC_CODERANGE_BROKEN    ((int)(FL_USER8|FL_USER9))
    00057 #define ENC_CODERANGE(obj) ((int)RBASIC(obj)->flags & ENC_CODERANGE_MASK)
    00058 #define ENC_CODERANGE_ASCIIONLY(obj) (ENC_CODERANGE(obj) == ENC_CODERANGE_7BIT)
    00059 #define ENC_CODERANGE_SET(obj,cr) (RBASIC(obj)->flags = \
    00060                                    (RBASIC(obj)->flags & ~ENC_CODERANGE_MASK) | (cr))
    00061 #define ENC_CODERANGE_CLEAR(obj) ENC_CODERANGE_SET(obj,0)
    00062
    00063 /* assumed ASCII compatibility */
    00064 #define ENC_CODERANGE_AND(a, b) \
    00065     (a == ENC_CODERANGE_7BIT ? b : \
    00066      a == ENC_CODERANGE_VALID ? (b == ENC_CODERANGE_7BIT ? ENC_CODERANGE_VALID : b) : \
    00067      ENC_CODERANGE_UNKNOWN)
    00068
    00069 #define ENCODING_CODERANGE_SET(obj, encindex, cr) \
    00070     do { \
    00071         VALUE rb_encoding_coderange_obj = (obj); \
    00072         ENCODING_SET(rb_encoding_coderange_obj, (encindex)); \
    00073         ENC_CODERANGE_SET(rb_encoding_coderange_obj, (cr)); \
    00074     } while (0)
    00075
    00076 typedef OnigEncodingType rb_encoding;
    00077
    00078 int rb_enc_replicate(const char *, rb_encoding *);
    00079 int rb_define_dummy_encoding(const char *);
    00080 #define rb_enc_to_index(enc) ((enc) ? ENC_TO_ENCINDEX(enc) : 0)
    00081 int rb_enc_get_index(VALUE obj);
    00082 void rb_enc_set_index(VALUE obj, int encindex);
    00083 int rb_enc_find_index(const char *name);
    00084 int rb_to_encoding_index(VALUE);
    00085 rb_encoding* rb_to_encoding(VALUE);
    00086 rb_encoding* rb_enc_get(VALUE);
    00087 rb_encoding* rb_enc_compatible(VALUE,VALUE);
    00088 rb_encoding* rb_enc_check(VALUE,VALUE);
    00089 VALUE rb_enc_associate_index(VALUE, int);
    00090 VALUE rb_enc_associate(VALUE, rb_encoding*);
    00091 void rb_enc_copy(VALUE dst, VALUE src);
    00092
    00093 VALUE rb_enc_str_new(const char*, long, rb_encoding*);
    00094 VALUE rb_enc_reg_new(const char*, long, rb_encoding*, int);
    00095 PRINTF_ARGS(VALUE rb_enc_sprintf(rb_encoding *, const char*, ...), 2, 3);
    00096 VALUE rb_enc_vsprintf(rb_encoding *, const char*, va_list);
    00097 long rb_enc_strlen(const char*, const char*, rb_encoding*);
    00098 char* rb_enc_nth(const char*, const char*, long, rb_encoding*);
    00099 VALUE rb_obj_encoding(VALUE);
    00100 VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *enc);
    00101 VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc);
    00102
    00103 VALUE rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *);
    00104 VALUE rb_str_export_to_enc(VALUE, rb_encoding *);
    00105 VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to);
    00106 VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts);
    00107
    00108 /* index -> rb_encoding */
    00109 rb_encoding* rb_enc_from_index(int idx);
    00110
    00111 /* name -> rb_encoding */
    00112 rb_encoding * rb_enc_find(const char *name);
    00113
    00114 /* rb_encoding * -> name */
    00115 #define rb_enc_name(enc) (enc)->name
    00116
    00117 /* rb_encoding * -> minlen/maxlen */
    00118 #define rb_enc_mbminlen(enc) (enc)->min_enc_len
    00119 #define rb_enc_mbmaxlen(enc) (enc)->max_enc_len
    00120
    00121 /* -> mbclen (no error notification: 0 < ret <= e-p, no exception) */
    00122 int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc);
    00123
    00124 /* -> mbclen (only for valid encoding) */
    00125 int rb_enc_fast_mbclen(const char *p, const char *e, rb_encoding *enc);
    00126
    00127 /* -> chlen, invalid or needmore */
    00128 int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc);
    00129 #define MBCLEN_CHARFOUND_P(ret)     ONIGENC_MBCLEN_CHARFOUND_P(ret)
    00130 #define MBCLEN_CHARFOUND_LEN(ret)     ONIGENC_MBCLEN_CHARFOUND_LEN(ret)
    00131 #define MBCLEN_INVALID_P(ret)       ONIGENC_MBCLEN_INVALID_P(ret)
    00132 #define MBCLEN_NEEDMORE_P(ret)      ONIGENC_MBCLEN_NEEDMORE_P(ret)
    00133 #define MBCLEN_NEEDMORE_LEN(ret)      ONIGENC_MBCLEN_NEEDMORE_LEN(ret)
    00134
    00135 /* -> 0x00..0x7f, -1 */
    00136 int rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc);
    00137
    00138
    00139 /* -> code (and len) or raise exception */
    00140 unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len, rb_encoding *enc);
    00141
    00142 /* prototype for obsolete function */
    00143 unsigned int rb_enc_codepoint(const char *p, const char *e, rb_encoding *enc);
    00144 /* overriding macro */
    00145 #define rb_enc_codepoint(p,e,enc) rb_enc_codepoint_len((p),(e),0,(enc))
    00146 #define rb_enc_mbc_to_codepoint(p, e, enc) ONIGENC_MBC_TO_CODE(enc,(UChar*)(p),(UChar*)(e))
    00147
    00148 /* -> codelen>0 or raise exception */
    00149 int rb_enc_codelen(int code, rb_encoding *enc);
    00150
    00151 /* code,ptr,encoding -> write buf */
    00152 #define rb_enc_mbcput(c,buf,enc) ONIGENC_CODE_TO_MBC(enc,c,(UChar*)(buf))
    00153
    00154 /* start, ptr, end, encoding -> prev_char */
    00155 #define rb_enc_prev_char(s,p,e,enc) (char *)onigenc_get_prev_char_head(enc,(UChar*)(s),(UChar*)(p),(UChar*)(e))
    00156 /* start, ptr, end, encoding -> next_char */
    00157 #define rb_enc_left_char_head(s,p,e,enc) (char *)onigenc_get_left_adjust_char_head(enc,(UChar*)(s),(UChar*)(p),(UChar*)(e))
    00158 #define rb_enc_right_char_head(s,p,e,enc) (char *)onigenc_get_right_adjust_char_head(enc,(UChar*)(s),(UChar*)(p),(UChar*)(e))
    00159
    00160 /* ptr, ptr, encoding -> newline_or_not */
    00161 #define rb_enc_is_newline(p,end,enc)  ONIGENC_IS_MBC_NEWLINE(enc,(UChar*)(p),(UChar*)(end))
    00162
    00163 #define rb_enc_isctype(c,t,enc) ONIGENC_IS_CODE_CTYPE(enc,c,t)
    00164 #define rb_enc_isascii(c,enc) ONIGENC_IS_CODE_ASCII(c)
    00165 #define rb_enc_isalpha(c,enc) ONIGENC_IS_CODE_ALPHA(enc,c)
    00166 #define rb_enc_islower(c,enc) ONIGENC_IS_CODE_LOWER(enc,c)
    00167 #define rb_enc_isupper(c,enc) ONIGENC_IS_CODE_UPPER(enc,c)
    00168 #define rb_enc_ispunct(c,enc) ONIGENC_IS_CODE_PUNCT(enc,c)
    00169 #define rb_enc_isalnum(c,enc) ONIGENC_IS_CODE_ALNUM(enc,c)
    00170 #define rb_enc_isprint(c,enc) ONIGENC_IS_CODE_PRINT(enc,c)
    00171 #define rb_enc_isspace(c,enc) ONIGENC_IS_CODE_SPACE(enc,c)
    00172 #define rb_enc_isdigit(c,enc) ONIGENC_IS_CODE_DIGIT(enc,c)
    00173
    00174 #define rb_enc_asciicompat(enc) (rb_enc_mbminlen(enc)==1 && !rb_enc_dummy_p(enc))
    00175
    00176 int rb_enc_casefold(char *to, const char *p, const char *e, rb_encoding *enc);
    00177 int rb_enc_toupper(int c, rb_encoding *enc);
    00178 int rb_enc_tolower(int c, rb_encoding *enc);
    00179 ID rb_intern3(const char*, long, rb_encoding*);
    00180 ID rb_interned_id_p(const char *, long, rb_encoding *);
    00181 int rb_enc_symname_p(const char*, rb_encoding*);
    00182 int rb_enc_symname2_p(const char*, long, rb_encoding*);
    00183 int rb_enc_str_coderange(VALUE);
    00184 long rb_str_coderange_scan_restartable(const char*, const char*, rb_encoding*, int*);
    00185 int rb_enc_str_asciionly_p(VALUE);
    00186 #define rb_enc_str_asciicompat_p(str) rb_enc_asciicompat(rb_enc_get(str))
    00187 VALUE rb_enc_from_encoding(rb_encoding *enc);
    00188 int rb_enc_unicode_p(rb_encoding *enc);
    00189 rb_encoding *rb_ascii8bit_encoding(void);
    00190 rb_encoding *rb_utf8_encoding(void);
    00191 rb_encoding *rb_usascii_encoding(void);
    00192 rb_encoding *rb_locale_encoding(void);
    00193 rb_encoding *rb_filesystem_encoding(void);
    00194 rb_encoding *rb_default_external_encoding(void);
    00195 rb_encoding *rb_default_internal_encoding(void);
    00196 int rb_ascii8bit_encindex(void);
    00197 int rb_utf8_encindex(void);
    00198 int rb_usascii_encindex(void);
    00199 int rb_locale_encindex(void);
    00200 int rb_filesystem_encindex(void);
    00201 VALUE rb_enc_default_external(void);
    00202 VALUE rb_enc_default_internal(void);
    00203 void rb_enc_set_default_external(VALUE encoding);
    00204 void rb_enc_set_default_internal(VALUE encoding);
    00205 VALUE rb_locale_charmap(VALUE klass);
    00206 long rb_memsearch(const void*,long,const void*,long,rb_encoding*);
    00207
    00208 RUBY_EXTERN VALUE rb_cEncoding;
    00209 #define ENC_DUMMY_FLAG (1<<24)
    00210 #define ENC_INDEX_MASK (~(~0U<<24))
    00211
    00212 #define ENC_TO_ENCINDEX(enc) (int)((enc)->ruby_encoding_index & ENC_INDEX_MASK)
    00213
    00214 #define ENC_DUMMY_P(enc) ((enc)->ruby_encoding_index & ENC_DUMMY_FLAG)
    00215 #define ENC_SET_DUMMY(enc) ((enc)->ruby_encoding_index |= ENC_DUMMY_FLAG)
    00216
    00217 static inline int
    00218 rb_enc_dummy_p(rb_encoding *enc)
    00219 {
    00220     return ENC_DUMMY_P(enc) != 0;
    00221 }
    00222
    00223 /* econv stuff */
    00224
    00225 typedef enum {
    00226     econv_invalid_byte_sequence,
    00227     econv_undefined_conversion,
    00228     econv_destination_buffer_full,
    00229     econv_source_buffer_empty,
    00230     econv_finished,
    00231     econv_after_output,
    00232     econv_incomplete_input
    00233 } rb_econv_result_t;
    00234
    00235 typedef struct rb_econv_t rb_econv_t;
    00236
    00237 VALUE rb_str_encode(VALUE str, VALUE to, int ecflags, VALUE ecopts);
    00238 int rb_econv_has_convpath_p(const char* from_encoding, const char* to_encoding);
    00239
    00240 int rb_econv_prepare_opts(VALUE opthash, VALUE *ecopts);
    00241
    00242 rb_econv_t *rb_econv_open(const char *source_encoding, const char *destination_encoding, int ecflags);
    00243 rb_econv_t *rb_econv_open_opts(const char *source_encoding, const char *destination_encoding, int ecflags, VALUE ecopts);
    00244
    00245 rb_econv_result_t rb_econv_convert(rb_econv_t *ec,
    00246     const unsigned char **source_buffer_ptr, const unsigned char *source_buffer_end,
    00247     unsigned char **destination_buffer_ptr, unsigned char *destination_buffer_end,
    00248     int flags);
    00249 void rb_econv_close(rb_econv_t *ec);
    00250
    00251 /* result: 0:success -1:failure */
    00252 int rb_econv_set_replacement(rb_econv_t *ec, const unsigned char *str, size_t len, const char *encname);
    00253
    00254 /* result: 0:success -1:failure */
    00255 int rb_econv_decorate_at_first(rb_econv_t *ec, const char *decorator_name);
    00256 int rb_econv_decorate_at_last(rb_econv_t *ec, const char *decorator_name);
    00257
    00258 VALUE rb_econv_open_exc(const char *senc, const char *denc, int ecflags);
    00259
    00260 /* result: 0:success -1:failure */
    00261 int rb_econv_insert_output(rb_econv_t *ec,
    00262     const unsigned char *str, size_t len, const char *str_encoding);
    00263
    00264 /* encoding that rb_econv_insert_output doesn't need conversion */
    00265 const char *rb_econv_encoding_to_insert_output(rb_econv_t *ec);
    00266
    00267 /* raise an error if the last rb_econv_convert is error */
    00268 void rb_econv_check_error(rb_econv_t *ec);
    00269
    00270 /* returns an exception object or nil */
    00271 VALUE rb_econv_make_exception(rb_econv_t *ec);
    00272
    00273 int rb_econv_putbackable(rb_econv_t *ec);
    00274 void rb_econv_putback(rb_econv_t *ec, unsigned char *p, int n);
    00275
    00276 /* returns the corresponding ASCII compatible encoding for encname,
    00277  * or NULL if encname is not ASCII incompatible encoding. */
    00278 const char *rb_econv_asciicompat_encoding(const char *encname);
    00279
    00280 VALUE rb_econv_str_convert(rb_econv_t *ec, VALUE src, int flags);
    00281 VALUE rb_econv_substr_convert(rb_econv_t *ec, VALUE src, long byteoff, long bytesize, int flags);
    00282 VALUE rb_econv_str_append(rb_econv_t *ec, VALUE src, VALUE dst, int flags);
    00283 VALUE rb_econv_substr_append(rb_econv_t *ec, VALUE src, long byteoff, long bytesize, VALUE dst, int flags);
    00284
    00285 void rb_econv_binmode(rb_econv_t *ec);
    00286
    00287 /* flags for rb_econv_open */
    00288
    00289 #define ECONV_ERROR_HANDLER_MASK                0x000000ff
    00290
    00291 #define ECONV_INVALID_MASK                      0x0000000f
    00292 #define ECONV_INVALID_REPLACE                   0x00000002
    00293
    00294 #define ECONV_UNDEF_MASK                        0x000000f0
    00295 #define ECONV_UNDEF_REPLACE                     0x00000020
    00296 #define ECONV_UNDEF_HEX_CHARREF                 0x00000030
    00297
    00298 #define ECONV_DECORATOR_MASK                    0x0000ff00
    00299
    00300 #define ECONV_UNIVERSAL_NEWLINE_DECORATOR       0x00000100
    00301 #define ECONV_CRLF_NEWLINE_DECORATOR            0x00001000
    00302 #define ECONV_CR_NEWLINE_DECORATOR              0x00002000
    00303 #define ECONV_XML_TEXT_DECORATOR                0x00004000
    00304 #define ECONV_XML_ATTR_CONTENT_DECORATOR        0x00008000
    00305
    00306 #define ECONV_STATEFUL_DECORATOR_MASK           0x00f00000
    00307 #define ECONV_XML_ATTR_QUOTE_DECORATOR          0x00100000
    00308
    00309 /* end of flags for rb_econv_open */
    00310
    00311 /* flags for rb_econv_convert */
    00312 #define ECONV_PARTIAL_INPUT                     0x00010000
    00313 #define ECONV_AFTER_OUTPUT                      0x00020000
    00314 /* end of flags for rb_econv_convert */
    00315
    00316 #if defined(__cplusplus)
    00317 #if 0
    00318 { /* satisfy cc-mode */
    00319 #endif
    00320 }  /* extern "C" { */
    00321 #endif
    00322
    00323 #endif /* RUBY_ENCODING_H */
    00324