Ruby
2.1.3p242(2014-09-19revision47630)
Main Page
Modules
Data Structures
Files
File List
Globals
include
ruby
encoding.h
Go to the documentation of this file.
1
/**********************************************************************
2
3
encoding.h -
4
5
$Author: matz $
6
created at: Thu May 24 11:49:41 JST 2007
7
8
Copyright (C) 2007 Yukihiro Matsumoto
9
10
**********************************************************************/
11
12
#ifndef RUBY_ENCODING_H
13
#define RUBY_ENCODING_H 1
14
15
#if defined(__cplusplus)
16
extern
"C"
{
17
#if 0
18
}
/* satisfy cc-mode */
19
#endif
20
#endif
21
22
#include <stdarg.h>
23
#include "
ruby/oniguruma.h
"
24
25
RUBY_SYMBOL_EXPORT_BEGIN
26
27
#define ENCODING_INLINE_MAX 127
28
#define ENCODING_SHIFT (FL_USHIFT+10)
29
#define ENCODING_MASK (((VALUE)ENCODING_INLINE_MAX)<<ENCODING_SHIFT)
/* FL_USER10|FL_USER11|FL_USER12|FL_USER13|FL_USER14|FL_USER15|FL_USER16 */
30
31
#define ENCODING_SET_INLINED(obj,i) do {\
32
RBASIC(obj)->flags &= ~ENCODING_MASK;\
33
RBASIC(obj)->flags |= (VALUE)(i) << ENCODING_SHIFT;\
34
} while (0)
35
#define ENCODING_SET(obj,i) rb_enc_set_index((obj), (i))
36
37
#define ENCODING_GET_INLINED(obj) (int)((RBASIC(obj)->flags & ENCODING_MASK)>>ENCODING_SHIFT)
38
#define ENCODING_GET(obj) \
39
(ENCODING_GET_INLINED(obj) != ENCODING_INLINE_MAX ? \
40
ENCODING_GET_INLINED(obj) : \
41
rb_enc_get_index(obj))
42
43
#define ENCODING_IS_ASCII8BIT(obj) (ENCODING_GET_INLINED(obj) == 0)
44
45
#define ENCODING_MAXNAMELEN 42
46
47
#define ENC_CODERANGE_MASK ((int)(FL_USER8|FL_USER9))
48
#define ENC_CODERANGE_UNKNOWN 0
49
#define ENC_CODERANGE_7BIT ((int)FL_USER8)
50
#define ENC_CODERANGE_VALID ((int)FL_USER9)
51
#define ENC_CODERANGE_BROKEN ((int)(FL_USER8|FL_USER9))
52
#define ENC_CODERANGE(obj) ((int)RBASIC(obj)->flags & ENC_CODERANGE_MASK)
53
#define ENC_CODERANGE_ASCIIONLY(obj) (ENC_CODERANGE(obj) == ENC_CODERANGE_7BIT)
54
#define ENC_CODERANGE_SET(obj,cr) (RBASIC(obj)->flags = \
55
(RBASIC(obj)->flags & ~ENC_CODERANGE_MASK) | (cr))
56
#define ENC_CODERANGE_CLEAR(obj) ENC_CODERANGE_SET((obj),0)
57
58
/* assumed ASCII compatibility */
59
#define ENC_CODERANGE_AND(a, b) \
60
((a) == ENC_CODERANGE_7BIT ? (b) : \
61
(a) == ENC_CODERANGE_VALID ? ((b) == ENC_CODERANGE_7BIT ? ENC_CODERANGE_VALID : (b)) : \
62
ENC_CODERANGE_UNKNOWN)
63
64
#define ENCODING_CODERANGE_SET(obj, encindex, cr) \
65
do { \
66
VALUE rb_encoding_coderange_obj = (obj); \
67
ENCODING_SET(rb_encoding_coderange_obj, (encindex)); \
68
ENC_CODERANGE_SET(rb_encoding_coderange_obj, (cr)); \
69
} while (0)
70
71
typedef
OnigEncodingType
rb_encoding
;
72
73
int
rb_char_to_option_kcode
(
int
c
,
int
*option,
int
*kcode);
74
75
int
rb_enc_replicate
(
const
char
*,
rb_encoding
*);
76
int
rb_define_dummy_encoding
(
const
char
*);
77
#define rb_enc_to_index(enc) ((enc) ? ENC_TO_ENCINDEX(enc) : 0)
78
int
rb_enc_get_index
(
VALUE
obj);
79
void
rb_enc_set_index
(
VALUE
obj,
int
encindex);
80
int
rb_enc_find_index
(
const
char
*
name
);
81
int
rb_to_encoding_index
(
VALUE
);
82
rb_encoding
*
rb_to_encoding
(
VALUE
);
83
rb_encoding
*
rb_find_encoding
(
VALUE
);
84
rb_encoding
*
rb_enc_get
(
VALUE
);
85
rb_encoding
*
rb_enc_compatible
(
VALUE
,
VALUE
);
86
rb_encoding
*
rb_enc_check
(
VALUE
,
VALUE
);
87
VALUE
rb_enc_associate_index
(
VALUE
,
int
);
88
VALUE
rb_enc_associate
(
VALUE
,
rb_encoding
*);
89
void
rb_enc_copy
(
VALUE
dst,
VALUE
src);
90
91
VALUE
rb_enc_str_new
(
const
char
*,
long
,
rb_encoding
*);
92
VALUE
rb_enc_str_new_cstr
(
const
char
*,
rb_encoding
*);
93
VALUE
rb_enc_reg_new
(
const
char
*,
long
,
rb_encoding
*,
int
);
94
PRINTF_ARGS
(
VALUE
rb_enc_sprintf
(
rb_encoding
*,
const
char
*, ...), 2, 3);
95
VALUE
rb_enc_vsprintf
(
rb_encoding
*,
const
char
*, va_list);
96
long
rb_enc_strlen
(
const
char
*,
const
char
*,
rb_encoding
*);
97
char
*
rb_enc_nth
(
const
char
*,
const
char
*,
long
,
rb_encoding
*);
98
VALUE
rb_obj_encoding
(
VALUE
);
99
VALUE
rb_enc_str_buf_cat
(
VALUE
str,
const
char
*ptr,
long
len,
rb_encoding
*enc);
100
VALUE
rb_enc_uint_chr
(
unsigned
int
code,
rb_encoding
*enc);
101
102
VALUE
rb_external_str_new_with_enc
(
const
char
*ptr,
long
len,
rb_encoding
*);
103
VALUE
rb_str_export_to_enc
(
VALUE
,
rb_encoding
*);
104
VALUE
rb_str_conv_enc
(
VALUE
str,
rb_encoding
*from,
rb_encoding
*to);
105
VALUE
rb_str_conv_enc_opts
(
VALUE
str,
rb_encoding
*from,
rb_encoding
*to,
int
ecflags,
VALUE
ecopts);
106
107
#if defined(__GNUC__) && !defined(__PCC__)
108
#define rb_enc_str_new_cstr(str, enc) __extension__ ( \
109
{ \
110
(__builtin_constant_p(str)) ? \
111
rb_enc_str_new((str), (long)strlen(str), (enc)) : \
112
rb_enc_str_new_cstr((str), (enc)); \
113
})
114
#endif
115
116
PRINTF_ARGS
(
NORETURN
(
void
rb_enc_raise
(
rb_encoding
*,
VALUE
,
const
char
*, ...)), 3, 4);
117
118
/* index -> rb_encoding */
119
rb_encoding
*
rb_enc_from_index
(
int
idx);
120
121
/* name -> rb_encoding */
122
rb_encoding
*
rb_enc_find
(
const
char
*
name
);
123
124
/* rb_encoding * -> name */
125
#define rb_enc_name(enc) (enc)->name
126
127
/* rb_encoding * -> minlen/maxlen */
128
#define rb_enc_mbminlen(enc) (enc)->min_enc_len
129
#define rb_enc_mbmaxlen(enc) (enc)->max_enc_len
130
131
/* -> mbclen (no error notification: 0 < ret <= e-p, no exception) */
132
int
rb_enc_mbclen
(
const
char
*p,
const
char
*e,
rb_encoding
*enc);
133
134
/* -> mbclen (only for valid encoding) */
135
int
rb_enc_fast_mbclen
(
const
char
*p,
const
char
*e,
rb_encoding
*enc);
136
137
/* -> chlen, invalid or needmore */
138
int
rb_enc_precise_mbclen
(
const
char
*p,
const
char
*e,
rb_encoding
*enc);
139
#define MBCLEN_CHARFOUND_P(ret) ONIGENC_MBCLEN_CHARFOUND_P(ret)
140
#define MBCLEN_CHARFOUND_LEN(ret) ONIGENC_MBCLEN_CHARFOUND_LEN(ret)
141
#define MBCLEN_INVALID_P(ret) ONIGENC_MBCLEN_INVALID_P(ret)
142
#define MBCLEN_NEEDMORE_P(ret) ONIGENC_MBCLEN_NEEDMORE_P(ret)
143
#define MBCLEN_NEEDMORE_LEN(ret) ONIGENC_MBCLEN_NEEDMORE_LEN(ret)
144
145
/* -> 0x00..0x7f, -1 */
146
int
rb_enc_ascget
(
const
char
*p,
const
char
*e,
int
*len,
rb_encoding
*enc);
147
148
149
/* -> code (and len) or raise exception */
150
unsigned
int
rb_enc_codepoint_len
(
const
char
*p,
const
char
*e,
int
*len,
rb_encoding
*enc);
151
152
/* prototype for obsolete function */
153
unsigned
int
rb_enc_codepoint
(
const
char
*p,
const
char
*e,
rb_encoding
*enc);
154
/* overriding macro */
155
#define rb_enc_codepoint(p,e,enc) rb_enc_codepoint_len((p),(e),0,(enc))
156
#define rb_enc_mbc_to_codepoint(p, e, enc) ONIGENC_MBC_TO_CODE((enc),(UChar*)(p),(UChar*)(e))
157
158
/* -> codelen>0 or raise exception */
159
int
rb_enc_codelen
(
int
code,
rb_encoding
*enc);
160
/* -> 0 for invalid codepoint */
161
int
rb_enc_code_to_mbclen
(
int
code,
rb_encoding
*enc);
162
#define rb_enc_code_to_mbclen(c, enc) ONIGENC_CODE_TO_MBCLEN((enc), (c));
163
164
/* code,ptr,encoding -> write buf */
165
#define rb_enc_mbcput(c,buf,enc) ONIGENC_CODE_TO_MBC((enc),(c),(UChar*)(buf))
166
167
/* start, ptr, end, encoding -> prev_char */
168
#define rb_enc_prev_char(s,p,e,enc) ((char *)onigenc_get_prev_char_head((enc),(UChar*)(s),(UChar*)(p),(UChar*)(e)))
169
/* start, ptr, end, encoding -> next_char */
170
#define rb_enc_left_char_head(s,p,e,enc) ((char *)onigenc_get_left_adjust_char_head((enc),(UChar*)(s),(UChar*)(p),(UChar*)(e)))
171
#define rb_enc_right_char_head(s,p,e,enc) ((char *)onigenc_get_right_adjust_char_head((enc),(UChar*)(s),(UChar*)(p),(UChar*)(e)))
172
#define rb_enc_step_back(s,p,e,n,enc) ((char *)onigenc_step_back((enc),(UChar*)(s),(UChar*)(p),(UChar*)(e),(int)(n)))
173
174
/* ptr, ptr, encoding -> newline_or_not */
175
#define rb_enc_is_newline(p,end,enc) ONIGENC_IS_MBC_NEWLINE((enc),(UChar*)(p),(UChar*)(end))
176
177
#define rb_enc_isctype(c,t,enc) ONIGENC_IS_CODE_CTYPE((enc),(c),(t))
178
#define rb_enc_isascii(c,enc) ONIGENC_IS_CODE_ASCII(c)
179
#define rb_enc_isalpha(c,enc) ONIGENC_IS_CODE_ALPHA((enc),(c))
180
#define rb_enc_islower(c,enc) ONIGENC_IS_CODE_LOWER((enc),(c))
181
#define rb_enc_isupper(c,enc) ONIGENC_IS_CODE_UPPER((enc),(c))
182
#define rb_enc_ispunct(c,enc) ONIGENC_IS_CODE_PUNCT((enc),(c))
183
#define rb_enc_isalnum(c,enc) ONIGENC_IS_CODE_ALNUM((enc),(c))
184
#define rb_enc_isprint(c,enc) ONIGENC_IS_CODE_PRINT((enc),(c))
185
#define rb_enc_isspace(c,enc) ONIGENC_IS_CODE_SPACE((enc),(c))
186
#define rb_enc_isdigit(c,enc) ONIGENC_IS_CODE_DIGIT((enc),(c))
187
188
#define rb_enc_asciicompat(enc) (rb_enc_mbminlen(enc)==1 && !rb_enc_dummy_p(enc))
189
190
int
rb_enc_casefold
(
char
*to,
const
char
*p,
const
char
*e,
rb_encoding
*enc);
191
int
rb_enc_toupper
(
int
c
,
rb_encoding
*enc);
192
int
rb_enc_tolower
(
int
c
,
rb_encoding
*enc);
193
ID
rb_intern3
(
const
char
*,
long
,
rb_encoding
*);
194
ID
rb_interned_id_p
(
const
char
*,
long
,
rb_encoding
*);
195
int
rb_enc_symname_p
(
const
char
*,
rb_encoding
*);
196
int
rb_enc_symname2_p
(
const
char
*,
long
,
rb_encoding
*);
197
int
rb_enc_str_coderange
(
VALUE
);
198
long
rb_str_coderange_scan_restartable
(
const
char
*,
const
char
*,
rb_encoding
*,
int
*);
199
int
rb_enc_str_asciionly_p
(
VALUE
);
200
#define rb_enc_str_asciicompat_p(str) rb_enc_asciicompat(rb_enc_get(str))
201
VALUE
rb_enc_from_encoding
(
rb_encoding
*enc);
202
int
rb_enc_unicode_p
(
rb_encoding
*enc);
203
rb_encoding
*
rb_ascii8bit_encoding
(
void
);
204
rb_encoding
*
rb_utf8_encoding
(
void
);
205
rb_encoding
*
rb_usascii_encoding
(
void
);
206
rb_encoding
*
rb_locale_encoding
(
void
);
207
rb_encoding
*
rb_filesystem_encoding
(
void
);
208
rb_encoding
*
rb_default_external_encoding
(
void
);
209
rb_encoding
*
rb_default_internal_encoding
(
void
);
210
#ifndef rb_ascii8bit_encindex
211
int
rb_ascii8bit_encindex
(
void
);
212
#endif
213
#ifndef rb_utf8_encindex
214
int
rb_utf8_encindex
(
void
);
215
#endif
216
#ifndef rb_usascii_encindex
217
int
rb_usascii_encindex
(
void
);
218
#endif
219
int
rb_locale_encindex
(
void
);
220
int
rb_filesystem_encindex
(
void
);
221
VALUE
rb_enc_default_external
(
void
);
222
VALUE
rb_enc_default_internal
(
void
);
223
void
rb_enc_set_default_external
(
VALUE
encoding);
224
void
rb_enc_set_default_internal
(
VALUE
encoding);
225
VALUE
rb_locale_charmap
(
VALUE
klass
);
226
long
rb_memsearch
(
const
void
*,
long
,
const
void
*,
long
,
rb_encoding
*);
227
char
*
rb_enc_path_next
(
const
char
*,
const
char
*,
rb_encoding
*);
228
char
*
rb_enc_path_skip_prefix
(
const
char
*,
const
char
*,
rb_encoding
*);
229
char
*
rb_enc_path_last_separator
(
const
char
*,
const
char
*,
rb_encoding
*);
230
char
*
rb_enc_path_end
(
const
char
*,
const
char
*,
rb_encoding
*);
231
const
char
*
ruby_enc_find_basename
(
const
char
*
name
,
long
*baselen,
long
*alllen,
rb_encoding
*enc);
232
const
char
*
ruby_enc_find_extname
(
const
char
*
name
,
long
*len,
rb_encoding
*enc);
233
ID
rb_check_id_cstr
(
const
char
*ptr,
long
len,
rb_encoding
*enc);
234
235
RUBY_EXTERN
VALUE
rb_cEncoding
;
236
#define ENC_DUMMY_FLAG (1<<24)
237
#define ENC_INDEX_MASK (~(~0U<<24))
238
239
#define ENC_TO_ENCINDEX(enc) (int)((enc)->ruby_encoding_index & ENC_INDEX_MASK)
240
241
#define ENC_DUMMY_P(enc) ((enc)->ruby_encoding_index & ENC_DUMMY_FLAG)
242
#define ENC_SET_DUMMY(enc) ((enc)->ruby_encoding_index |= ENC_DUMMY_FLAG)
243
244
static
inline
int
245
rb_enc_dummy_p
(
rb_encoding
*enc)
246
{
247
return
ENC_DUMMY_P
(enc) != 0;
248
}
249
250
/* econv stuff */
251
252
typedef
enum
{
253
econv_invalid_byte_sequence
,
254
econv_undefined_conversion
,
255
econv_destination_buffer_full
,
256
econv_source_buffer_empty
,
257
econv_finished
,
258
econv_after_output
,
259
econv_incomplete_input
260
}
rb_econv_result_t
;
261
262
typedef
struct
rb_econv_t
rb_econv_t
;
263
264
VALUE
rb_str_encode
(
VALUE
str,
VALUE
to,
int
ecflags,
VALUE
ecopts);
265
int
rb_econv_has_convpath_p
(
const
char
* from_encoding,
const
char
* to_encoding);
266
267
int
rb_econv_prepare_options
(
VALUE
opthash,
VALUE
*ecopts,
int
ecflags);
268
int
rb_econv_prepare_opts
(
VALUE
opthash,
VALUE
*ecopts);
269
270
rb_econv_t
*
rb_econv_open
(
const
char
*
source_encoding
,
const
char
*
destination_encoding
,
int
ecflags);
271
rb_econv_t
*
rb_econv_open_opts
(
const
char
*
source_encoding
,
const
char
*
destination_encoding
,
int
ecflags,
VALUE
ecopts);
272
273
rb_econv_result_t
rb_econv_convert
(
rb_econv_t
*ec,
274
const
unsigned
char
**source_buffer_ptr,
const
unsigned
char
*source_buffer_end,
275
unsigned
char
**destination_buffer_ptr,
unsigned
char
*destination_buffer_end,
276
int
flags
);
277
void
rb_econv_close
(
rb_econv_t
*ec);
278
279
/* result: 0:success -1:failure */
280
int
rb_econv_set_replacement
(
rb_econv_t
*ec,
const
unsigned
char
*str,
size_t
len,
const
char
*encname);
281
282
/* result: 0:success -1:failure */
283
int
rb_econv_decorate_at_first
(
rb_econv_t
*ec,
const
char
*decorator_name);
284
int
rb_econv_decorate_at_last
(
rb_econv_t
*ec,
const
char
*decorator_name);
285
286
VALUE
rb_econv_open_exc
(
const
char
*senc,
const
char
*denc,
int
ecflags);
287
288
/* result: 0:success -1:failure */
289
int
rb_econv_insert_output
(
rb_econv_t
*ec,
290
const
unsigned
char
*str,
size_t
len,
const
char
*str_encoding);
291
292
/* encoding that rb_econv_insert_output doesn't need conversion */
293
const
char
*
rb_econv_encoding_to_insert_output
(
rb_econv_t
*ec);
294
295
/* raise an error if the last rb_econv_convert is error */
296
void
rb_econv_check_error
(
rb_econv_t
*ec);
297
298
/* returns an exception object or nil */
299
VALUE
rb_econv_make_exception
(
rb_econv_t
*ec);
300
301
int
rb_econv_putbackable
(
rb_econv_t
*ec);
302
void
rb_econv_putback
(
rb_econv_t
*ec,
unsigned
char
*p,
int
n);
303
304
/* returns the corresponding ASCII compatible encoding for encname,
305
* or NULL if encname is not ASCII incompatible encoding. */
306
const
char
*
rb_econv_asciicompat_encoding
(
const
char
*encname);
307
308
VALUE
rb_econv_str_convert
(
rb_econv_t
*ec,
VALUE
src,
int
flags
);
309
VALUE
rb_econv_substr_convert
(
rb_econv_t
*ec,
VALUE
src,
long
byteoff,
long
bytesize,
int
flags
);
310
VALUE
rb_econv_str_append
(
rb_econv_t
*ec,
VALUE
src,
VALUE
dst,
int
flags
);
311
VALUE
rb_econv_substr_append
(
rb_econv_t
*ec,
VALUE
src,
long
byteoff,
long
bytesize,
VALUE
dst,
int
flags
);
312
VALUE
rb_econv_append
(
rb_econv_t
*ec,
const
char
*bytesrc,
long
bytesize,
VALUE
dst,
int
flags
);
313
314
void
rb_econv_binmode
(
rb_econv_t
*ec);
315
316
/* flags for rb_econv_open */
317
318
#define ECONV_ERROR_HANDLER_MASK 0x000000ff
319
320
#define ECONV_INVALID_MASK 0x0000000f
321
#define ECONV_INVALID_REPLACE 0x00000002
322
323
#define ECONV_UNDEF_MASK 0x000000f0
324
#define ECONV_UNDEF_REPLACE 0x00000020
325
#define ECONV_UNDEF_HEX_CHARREF 0x00000030
326
327
#define ECONV_DECORATOR_MASK 0x0000ff00
328
#define ECONV_NEWLINE_DECORATOR_MASK 0x00003f00
329
#define ECONV_NEWLINE_DECORATOR_READ_MASK 0x00000f00
330
#define ECONV_NEWLINE_DECORATOR_WRITE_MASK 0x00003000
331
332
#define ECONV_UNIVERSAL_NEWLINE_DECORATOR 0x00000100
333
#define ECONV_CRLF_NEWLINE_DECORATOR 0x00001000
334
#define ECONV_CR_NEWLINE_DECORATOR 0x00002000
335
#define ECONV_XML_TEXT_DECORATOR 0x00004000
336
#define ECONV_XML_ATTR_CONTENT_DECORATOR 0x00008000
337
338
#define ECONV_STATEFUL_DECORATOR_MASK 0x00f00000
339
#define ECONV_XML_ATTR_QUOTE_DECORATOR 0x00100000
340
341
#if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
342
#define ECONV_DEFAULT_NEWLINE_DECORATOR ECONV_CRLF_NEWLINE_DECORATOR
343
#else
344
#define ECONV_DEFAULT_NEWLINE_DECORATOR 0
345
#endif
346
347
/* end of flags for rb_econv_open */
348
349
/* flags for rb_econv_convert */
350
#define ECONV_PARTIAL_INPUT 0x00010000
351
#define ECONV_AFTER_OUTPUT 0x00020000
352
/* end of flags for rb_econv_convert */
353
354
RUBY_SYMBOL_EXPORT_END
355
356
#if defined(__cplusplus)
357
#if 0
358
{
/* satisfy cc-mode */
359
#endif
360
}
/* extern "C" { */
361
#endif
362
363
#endif
/* RUBY_ENCODING_H */
364
Generated on Mon Sep 22 2014 11:05:32 for Ruby by
1.8.2