Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 440 lines (386 sloc) 13.469 kb
96ab900 more work
Laurent Sansonetti authored
1 /*
2 * MacRuby implementation of Ruby 1.9 String.
3 *
4 * This file is covered by the Ruby license. See COPYING for more details.
5 *
6 * Copyright (C) 2007-2010, Apple Inc. All rights reserved.
7 * Copyright (C) 1993-2007 Yukihiro Matsumoto
8 * Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
9 * Copyright (C) 2000 Information-technology Promotion Agency, Japan
10 */
11
2b7d5d5 import vincent's work
Laurent Sansonetti authored
12 #include <string.h>
9c1d230 committing experimental branch content
Laurent Sansonetti authored
13
39b55f1 some work on string
Laurent Sansonetti authored
14 #include "ruby.h"
15 #include "ruby/encoding.h"
16 #include "encoding.h"
17
96ab900 more work
Laurent Sansonetti authored
18 VALUE rb_cEncoding;
8b9745b define Encoding::ASCII_8BIT as a shortcut to US_ASCII (for now)
Laurent Sansonetti authored
19
d0ac593 @vincentisambart an (incomplete) implementation of String#encode
vincentisambart authored
20 rb_encoding_t *default_internal = NULL;
96ab900 more work
Laurent Sansonetti authored
21 static rb_encoding_t *default_external = NULL;
22 rb_encoding_t *rb_encodings[ENCODINGS_COUNT];
9c1d230 committing experimental branch content
Laurent Sansonetti authored
23
96ab900 more work
Laurent Sansonetti authored
24 static void str_undefined_update_flags(rb_str_t *self) { abort(); }
25 static void str_undefined_make_data_binary(rb_str_t *self) { abort(); }
26 static bool str_undefined_try_making_data_uchars(rb_str_t *self) { abort(); }
27 static long str_undefined_length(rb_str_t *self, bool ucs2_mode) { abort(); }
28 static long str_undefined_bytesize(rb_str_t *self) { abort(); }
29 static character_boundaries_t str_undefined_get_character_boundaries(rb_str_t *self, long index, bool ucs2_mode) { abort(); }
30 static long str_undefined_offset_in_bytes_to_index(rb_str_t *self, long offset_in_bytes, bool ucs2_mode) { abort(); }
d0ac593 @vincentisambart an (incomplete) implementation of String#encode
vincentisambart authored
31 static void str_undefined_transcode_to_utf16(struct rb_encoding *src_enc, rb_str_t *self, long *pos, UChar **utf16, long *utf16_length) { abort(); }
32 static void str_undefined_transcode_from_utf16(struct rb_encoding *dst_enc, UChar *utf16, long utf16_length, long *pos, char **bytes, long *bytes_length) { abort(); }
9c1d230 committing experimental branch content
Laurent Sansonetti authored
33
34 static VALUE
2b7d5d5 import vincent's work
Laurent Sansonetti authored
35 mr_enc_s_list(VALUE klass, SEL sel)
9c1d230 committing experimental branch content
Laurent Sansonetti authored
36 {
2b7d5d5 import vincent's work
Laurent Sansonetti authored
37 VALUE ary = rb_ary_new2(ENCODINGS_COUNT);
38 for (unsigned int i = 0; i < ENCODINGS_COUNT; ++i) {
96ab900 more work
Laurent Sansonetti authored
39 rb_ary_push(ary, (VALUE)rb_encodings[i]);
9c1d230 committing experimental branch content
Laurent Sansonetti authored
40 }
2b7d5d5 import vincent's work
Laurent Sansonetti authored
41 return ary;
9c1d230 committing experimental branch content
Laurent Sansonetti authored
42 }
43
44 static VALUE
2b7d5d5 import vincent's work
Laurent Sansonetti authored
45 mr_enc_s_name_list(VALUE klass, SEL sel)
46 {
47 VALUE ary = rb_ary_new();
48 for (unsigned int i = 0; i < ENCODINGS_COUNT; ++i) {
96ab900 more work
Laurent Sansonetti authored
49 rb_encoding_t *encoding = RENC(rb_encodings[i]);
2b7d5d5 import vincent's work
Laurent Sansonetti authored
50 // TODO: use US-ASCII strings
96ab900 more work
Laurent Sansonetti authored
51 rb_ary_push(ary, rb_usascii_str_new2(encoding->public_name));
2b7d5d5 import vincent's work
Laurent Sansonetti authored
52 for (unsigned int j = 0; j < encoding->aliases_count; ++j) {
96ab900 more work
Laurent Sansonetti authored
53 rb_ary_push(ary, rb_usascii_str_new2(encoding->aliases[j]));
2b7d5d5 import vincent's work
Laurent Sansonetti authored
54 }
9c1d230 committing experimental branch content
Laurent Sansonetti authored
55 }
56 return ary;
57 }
58
59 static VALUE
2b7d5d5 import vincent's work
Laurent Sansonetti authored
60 mr_enc_s_aliases(VALUE klass, SEL sel)
61 {
62 VALUE hash = rb_hash_new();
63 for (unsigned int i = 0; i < ENCODINGS_COUNT; ++i) {
96ab900 more work
Laurent Sansonetti authored
64 rb_encoding_t *encoding = RENC(rb_encodings[i]);
2b7d5d5 import vincent's work
Laurent Sansonetti authored
65 for (unsigned int j = 0; j < encoding->aliases_count; ++j) {
96ab900 more work
Laurent Sansonetti authored
66 rb_hash_aset(hash, rb_usascii_str_new2(encoding->aliases[j]),
67 rb_usascii_str_new2(encoding->public_name));
2b7d5d5 import vincent's work
Laurent Sansonetti authored
68 }
9c1d230 committing experimental branch content
Laurent Sansonetti authored
69 }
2b7d5d5 import vincent's work
Laurent Sansonetti authored
70 return hash;
9c1d230 committing experimental branch content
Laurent Sansonetti authored
71 }
72
73 static VALUE
4ede652 added #find
Laurent Sansonetti authored
74 mr_enc_s_find(VALUE klass, SEL sel, VALUE name)
75 {
76 StringValue(name);
77 rb_encoding_t *enc = rb_enc_find(RSTRING_PTR(name));
78 if (enc == NULL) {
79 rb_raise(rb_eArgError, "unknown encoding name - %s",
80 RSTRING_PTR(name));
81 }
82 return (VALUE)enc;
83 }
84
85 static VALUE
2b7d5d5 import vincent's work
Laurent Sansonetti authored
86 mr_enc_s_default_internal(VALUE klass, SEL sel)
9c1d230 committing experimental branch content
Laurent Sansonetti authored
87 {
2b7d5d5 import vincent's work
Laurent Sansonetti authored
88 return (VALUE)default_internal;
9c1d230 committing experimental branch content
Laurent Sansonetti authored
89 }
90
91 static VALUE
5051413 added #default_external=, #default_internal=
Laurent Sansonetti authored
92 mr_enc_set_default_internal(VALUE klass, SEL sel, VALUE enc)
93 {
94 default_internal = rb_to_encoding(enc);
95 return (VALUE)default_internal;
96 }
97
98 static VALUE
2b7d5d5 import vincent's work
Laurent Sansonetti authored
99 mr_enc_s_default_external(VALUE klass, SEL sel)
9c1d230 committing experimental branch content
Laurent Sansonetti authored
100 {
2b7d5d5 import vincent's work
Laurent Sansonetti authored
101 return (VALUE)default_external;
9c1d230 committing experimental branch content
Laurent Sansonetti authored
102 }
103
104 static VALUE
5051413 added #default_external=, #default_internal=
Laurent Sansonetti authored
105 mr_enc_set_default_external(VALUE klass, SEL sel, VALUE enc)
106 {
107 default_external = rb_to_encoding(enc);
108 return (VALUE)default_external;
109 }
110
111 static VALUE
2b7d5d5 import vincent's work
Laurent Sansonetti authored
112 mr_enc_name(VALUE self, SEL sel)
9c1d230 committing experimental branch content
Laurent Sansonetti authored
113 {
96ab900 more work
Laurent Sansonetti authored
114 return rb_usascii_str_new2(RENC(self)->public_name);
9c1d230 committing experimental branch content
Laurent Sansonetti authored
115 }
116
117 static VALUE
2b7d5d5 import vincent's work
Laurent Sansonetti authored
118 mr_enc_inspect(VALUE self, SEL sel)
9c1d230 committing experimental branch content
Laurent Sansonetti authored
119 {
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
120 return rb_sprintf("#<%s:%s>", rb_obj_classname(self),
96ab900 more work
Laurent Sansonetti authored
121 RENC(self)->public_name);
9c1d230 committing experimental branch content
Laurent Sansonetti authored
122 }
123
124 static VALUE
2b7d5d5 import vincent's work
Laurent Sansonetti authored
125 mr_enc_names(VALUE self, SEL sel)
9c1d230 committing experimental branch content
Laurent Sansonetti authored
126 {
96ab900 more work
Laurent Sansonetti authored
127 rb_encoding_t *encoding = RENC(self);
9c1d230 committing experimental branch content
Laurent Sansonetti authored
128
2b7d5d5 import vincent's work
Laurent Sansonetti authored
129 VALUE ary = rb_ary_new2(encoding->aliases_count + 1);
96ab900 more work
Laurent Sansonetti authored
130 rb_ary_push(ary, rb_usascii_str_new2(encoding->public_name));
2b7d5d5 import vincent's work
Laurent Sansonetti authored
131 for (unsigned int i = 0; i < encoding->aliases_count; ++i) {
96ab900 more work
Laurent Sansonetti authored
132 rb_ary_push(ary, rb_usascii_str_new2(encoding->aliases[i]));
2b7d5d5 import vincent's work
Laurent Sansonetti authored
133 }
134 return ary;
1623532 added Encoding#default_external= and Encoding#default_internal= which…
Laurent Sansonetti authored
135 }
136
2b7d5d5 import vincent's work
Laurent Sansonetti authored
137 static VALUE
138 mr_enc_ascii_compatible_p(VALUE self, SEL sel)
9c1d230 committing experimental branch content
Laurent Sansonetti authored
139 {
96ab900 more work
Laurent Sansonetti authored
140 return RENC(self)->ascii_compatible ? Qtrue : Qfalse;
9c1d230 committing experimental branch content
Laurent Sansonetti authored
141 }
142
143 static VALUE
2b7d5d5 import vincent's work
Laurent Sansonetti authored
144 mr_enc_dummy_p(VALUE self, SEL sel)
9c1d230 committing experimental branch content
Laurent Sansonetti authored
145 {
2b7d5d5 import vincent's work
Laurent Sansonetti authored
146 return Qfalse;
9c1d230 committing experimental branch content
Laurent Sansonetti authored
147 }
148
2b7d5d5 import vincent's work
Laurent Sansonetti authored
149 static void
96ab900 more work
Laurent Sansonetti authored
150 define_encoding_constant(const char *name, rb_encoding_t *encoding)
9c1d230 committing experimental branch content
Laurent Sansonetti authored
151 {
2b7d5d5 import vincent's work
Laurent Sansonetti authored
152 char c = name[0];
153 if ((c >= '0') && (c <= '9')) {
154 // constants can't start with a number
155 return;
9c1d230 committing experimental branch content
Laurent Sansonetti authored
156 }
157
2b7d5d5 import vincent's work
Laurent Sansonetti authored
158 char *name_copy = strdup(name);
159 if ((c >= 'a') && (c <= 'z')) {
160 // the first character must be upper case
161 name_copy[0] = c - ('a' - 'A');
162 }
9c1d230 committing experimental branch content
Laurent Sansonetti authored
163
2b7d5d5 import vincent's work
Laurent Sansonetti authored
164 // '.' and '-' must be transformed into '_'
165 for (int i = 0; name_copy[i]; ++i) {
166 if ((name_copy[i] == '.') || (name_copy[i] == '-')) {
167 name_copy[i] = '_';
023dd4d fixed Encoding#name for 10.6
Laurent Sansonetti authored
168 }
169 }
b881853 s/MR//
Laurent Sansonetti authored
170 rb_define_const(rb_cEncoding, name_copy, (VALUE)encoding);
2b7d5d5 import vincent's work
Laurent Sansonetti authored
171 free(name_copy);
9c1d230 committing experimental branch content
Laurent Sansonetti authored
172 }
173
96ab900 more work
Laurent Sansonetti authored
174 extern void enc_init_ucnv_encoding(rb_encoding_t *encoding);
9c1d230 committing experimental branch content
Laurent Sansonetti authored
175
2b7d5d5 import vincent's work
Laurent Sansonetti authored
176 enum {
177 ENCODING_TYPE_SPECIAL = 0,
178 ENCODING_TYPE_UCNV
179 };
9c1d230 committing experimental branch content
Laurent Sansonetti authored
180
2b7d5d5 import vincent's work
Laurent Sansonetti authored
181 static void
182 add_encoding(
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
183 unsigned int encoding_index, // index of the encoding in the encodings
184 // array
96ab900 more work
Laurent Sansonetti authored
185 unsigned int rb_encoding_type,
2b7d5d5 import vincent's work
Laurent Sansonetti authored
186 const char *public_name, // public name for the encoding
187 unsigned char min_char_size,
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
188 bool single_byte_encoding, // in the encoding a character takes only
189 // one byte
2b7d5d5 import vincent's work
Laurent Sansonetti authored
190 bool ascii_compatible, // is the encoding ASCII compatible or not
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
191 ... // aliases for the encoding (should no include the public name)
192 // - must end with a NULL
2b7d5d5 import vincent's work
Laurent Sansonetti authored
193 )
194 {
195 assert(encoding_index < ENCODINGS_COUNT);
196
197 // create an array for the aliases
198 unsigned int aliases_count = 0;
199 va_list va_aliases;
200 va_start(va_aliases, ascii_compatible);
201 while (va_arg(va_aliases, const char *) != NULL) {
202 ++aliases_count;
203 }
204 va_end(va_aliases);
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
205 const char **aliases = (const char **)
206 malloc(sizeof(const char *) * aliases_count);
2b7d5d5 import vincent's work
Laurent Sansonetti authored
207 va_start(va_aliases, ascii_compatible);
208 for (unsigned int i = 0; i < aliases_count; ++i) {
209 aliases[i] = va_arg(va_aliases, const char *);
210 }
211 va_end(va_aliases);
212
213 // create the MacRuby object
96ab900 more work
Laurent Sansonetti authored
214 NEWOBJ(encoding, rb_encoding_t);
2b7d5d5 import vincent's work
Laurent Sansonetti authored
215 encoding->basic.flags = 0;
b881853 s/MR//
Laurent Sansonetti authored
216 encoding->basic.klass = rb_cEncoding;
96ab900 more work
Laurent Sansonetti authored
217 rb_encodings[encoding_index] = encoding;
218 GC_RETAIN(encoding); // it should never be deallocated
2b7d5d5 import vincent's work
Laurent Sansonetti authored
219
220 // fill the fields
221 encoding->index = encoding_index;
222 encoding->public_name = public_name;
223 encoding->min_char_size = min_char_size;
224 encoding->single_byte_encoding = single_byte_encoding;
225 encoding->ascii_compatible = ascii_compatible;
226 encoding->aliases_count = aliases_count;
227 encoding->aliases = aliases;
228
229 // fill the default implementations with aborts
230 encoding->methods.update_flags = str_undefined_update_flags;
231 encoding->methods.make_data_binary = str_undefined_make_data_binary;
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
232 encoding->methods.try_making_data_uchars =
233 str_undefined_try_making_data_uchars;
2b7d5d5 import vincent's work
Laurent Sansonetti authored
234 encoding->methods.length = str_undefined_length;
235 encoding->methods.bytesize = str_undefined_bytesize;
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
236 encoding->methods.get_character_boundaries =
237 str_undefined_get_character_boundaries;
238 encoding->methods.offset_in_bytes_to_index =
239 str_undefined_offset_in_bytes_to_index;
d0ac593 @vincentisambart an (incomplete) implementation of String#encode
vincentisambart authored
240 encoding->methods.transcode_to_utf16 =
241 str_undefined_transcode_to_utf16;
242 encoding->methods.transcode_from_utf16 =
243 str_undefined_transcode_from_utf16;
2b7d5d5 import vincent's work
Laurent Sansonetti authored
244
96ab900 more work
Laurent Sansonetti authored
245 switch (rb_encoding_type) {
2b7d5d5 import vincent's work
Laurent Sansonetti authored
246 case ENCODING_TYPE_SPECIAL:
022cd7c fixed ByteString#encoding to always return US_ASCII (for now)
Laurent Sansonetti authored
247 break;
2b7d5d5 import vincent's work
Laurent Sansonetti authored
248 case ENCODING_TYPE_UCNV:
249 enc_init_ucnv_encoding(encoding);
250 break;
251 default:
252 abort();
9c1d230 committing experimental branch content
Laurent Sansonetti authored
253 }
254 }
255
ae4da82 more work
Laurent Sansonetti authored
256 // This Init function is called very early. Do not use any runtime method
257 // because things may not be initialized properly yet.
258 void
259 Init_PreEncoding(void)
2b7d5d5 import vincent's work
Laurent Sansonetti authored
260 {
0f75583 @vincentisambart added the encodings used in the specs to be able to remove tags
vincentisambart authored
261 add_encoding(ENCODING_BINARY, ENCODING_TYPE_SPECIAL, "ASCII-8BIT", 1, true, true, "BINARY", NULL);
262 add_encoding(ENCODING_ASCII, ENCODING_TYPE_UCNV, "US-ASCII", 1, true, true, "ASCII", "ANSI_X3.4-1968", "646", NULL);
4e2db64 Improves core/env pass rate: add 'locale' as an alias of UTF-8
Thibault Martin-Lagardette authored
263 add_encoding(ENCODING_UTF8, ENCODING_TYPE_UCNV, "UTF-8", 1, false, true, "CP65001", "locale", NULL);
0f75583 @vincentisambart added the encodings used in the specs to be able to remove tags
vincentisambart authored
264 add_encoding(ENCODING_UTF16BE, ENCODING_TYPE_UCNV, "UTF-16BE", 2, false, false, NULL);
265 add_encoding(ENCODING_UTF16LE, ENCODING_TYPE_UCNV, "UTF-16LE", 2, false, false, NULL);
266 add_encoding(ENCODING_UTF32BE, ENCODING_TYPE_UCNV, "UTF-32BE", 4, false, false, "UCS-4BE", NULL);
267 add_encoding(ENCODING_UTF32LE, ENCODING_TYPE_UCNV, "UTF-32LE", 4, false, false, "UCS-4LE", NULL);
268 add_encoding(ENCODING_ISO8859_1, ENCODING_TYPE_UCNV, "ISO-8859-1", 1, true, true, "ISO8859-1", NULL);
269 add_encoding(ENCODING_MACROMAN, ENCODING_TYPE_UCNV, "macRoman", 1, true, true, NULL);
270 add_encoding(ENCODING_MACCYRILLIC, ENCODING_TYPE_UCNV, "macCyrillic", 1, true, true, NULL);
271 add_encoding(ENCODING_BIG5, ENCODING_TYPE_UCNV, "Big5", 1, false, true, "CP950", NULL);
2b7d5d5 import vincent's work
Laurent Sansonetti authored
272 // FIXME: the ICU conversion tables do not seem to match Ruby's Japanese conversion tables
0f75583 @vincentisambart added the encodings used in the specs to be able to remove tags
vincentisambart authored
273 add_encoding(ENCODING_EUCJP, ENCODING_TYPE_UCNV, "EUC-JP", 1, false, true, "eucJP", NULL);
2b7d5d5 import vincent's work
Laurent Sansonetti authored
274 //add_encoding(ENCODING_EUCJP, ENCODING_TYPE_RUBY, "EUC-JP", 1, false, true, "eucJP", NULL);
275 //add_encoding(ENCODING_SJIS, ENCODING_TYPE_RUBY, "Shift_JIS", 1, false, true, "SJIS", NULL);
276 //add_encoding(ENCODING_CP932, ENCODING_TYPE_RUBY, "Windows-31J", 1, false, true, "CP932", "csWindows31J", NULL);
277
96ab900 more work
Laurent Sansonetti authored
278 default_external = rb_encodings[ENCODING_UTF8];
279 default_internal = rb_encodings[ENCODING_UTF8];
9c1d230 committing experimental branch content
Laurent Sansonetti authored
280 }
281
2b7d5d5 import vincent's work
Laurent Sansonetti authored
282 void
b881853 s/MR//
Laurent Sansonetti authored
283 Init_Encoding(void)
2b7d5d5 import vincent's work
Laurent Sansonetti authored
284 {
ae4da82 more work
Laurent Sansonetti authored
285 // rb_cEncoding is defined earlier in Init_PreVM().
286 rb_set_class_path(rb_cEncoding, rb_cObject, "Encoding");
287 rb_const_set(rb_cObject, rb_intern("Encoding"), rb_cEncoding);
288
b881853 s/MR//
Laurent Sansonetti authored
289 rb_undef_alloc_func(rb_cEncoding);
290
291 rb_objc_define_method(rb_cEncoding, "to_s", mr_enc_name, 0);
292 rb_objc_define_method(rb_cEncoding, "inspect", mr_enc_inspect, 0);
293 rb_objc_define_method(rb_cEncoding, "name", mr_enc_name, 0);
294 rb_objc_define_method(rb_cEncoding, "names", mr_enc_names, 0);
295 rb_objc_define_method(rb_cEncoding, "dummy?", mr_enc_dummy_p, 0);
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
296 rb_objc_define_method(rb_cEncoding, "ascii_compatible?",
297 mr_enc_ascii_compatible_p, 0);
4ede652 added #find
Laurent Sansonetti authored
298 rb_objc_define_method(*(VALUE *)rb_cEncoding, "list", mr_enc_s_list, 0);
299 rb_objc_define_method(*(VALUE *)rb_cEncoding, "name_list",
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
300 mr_enc_s_name_list, 0);
4ede652 added #find
Laurent Sansonetti authored
301 rb_objc_define_method(*(VALUE *)rb_cEncoding, "aliases",
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
302 mr_enc_s_aliases, 0);
4ede652 added #find
Laurent Sansonetti authored
303 rb_objc_define_method(*(VALUE *)rb_cEncoding, "find", mr_enc_s_find, 1);
304 rb_objc_define_method(*(VALUE *)rb_cEncoding, "compatible?",
39b55f1 some work on string
Laurent Sansonetti authored
305 mr_enc_s_is_compatible, 2); // in string.c
2b7d5d5 import vincent's work
Laurent Sansonetti authored
306
307 //rb_define_method(rb_cEncoding, "_dump", enc_dump, -1);
308 //rb_define_singleton_method(rb_cEncoding, "_load", enc_load, 1);
309
4ede652 added #find
Laurent Sansonetti authored
310 rb_objc_define_method(*(VALUE *)rb_cEncoding, "default_external",
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
311 mr_enc_s_default_external, 0);
5051413 added #default_external=, #default_internal=
Laurent Sansonetti authored
312 rb_objc_define_method(*(VALUE *)rb_cEncoding, "default_external=",
313 mr_enc_set_default_external, 1);
4ede652 added #find
Laurent Sansonetti authored
314 rb_objc_define_method(*(VALUE *)rb_cEncoding, "default_internal",
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
315 mr_enc_s_default_internal, 0);
5051413 added #default_external=, #default_internal=
Laurent Sansonetti authored
316 rb_objc_define_method(*(VALUE *)rb_cEncoding, "default_internal=",
317 mr_enc_set_default_internal, 1);
b881853 s/MR//
Laurent Sansonetti authored
318 //rb_define_singleton_method(rb_cEncoding, "locale_charmap", rb_locale_charmap, 0);
2b7d5d5 import vincent's work
Laurent Sansonetti authored
319
ae4da82 more work
Laurent Sansonetti authored
320 // Create constants.
321 for (unsigned int i = 0; i < ENCODINGS_COUNT; i++) {
322 rb_encoding_t *enc = rb_encodings[i];
323 define_encoding_constant(enc->public_name, enc);
324 for (unsigned int j = 0; j < enc->aliases_count; j++) {
325 define_encoding_constant(enc->aliases[j], enc);
326 }
327 }
9c1d230 committing experimental branch content
Laurent Sansonetti authored
328 }
96ab900 more work
Laurent Sansonetti authored
329
330 // MRI C-API compatibility.
331
332 rb_encoding_t *
333 rb_enc_find(const char *name)
334 {
335 for (unsigned int i = 0; i < ENCODINGS_COUNT; i++) {
336 rb_encoding_t *enc = rb_encodings[i];
337 if (strcasecmp(enc->public_name, name) == 0) {
338 return enc;
339 }
340 for (unsigned int j = 0; j < enc->aliases_count; j++) {
341 const char *alias = enc->aliases[j];
342 if (strcasecmp(alias, name) == 0) {
343 return enc;
344 }
345 }
346 }
347 return NULL;
348 }
349
350 VALUE
351 rb_enc_from_encoding(rb_encoding_t *enc)
352 {
353 return (VALUE)enc;
354 }
355
356 rb_encoding_t *
357 rb_enc_get(VALUE obj)
358 {
359 if (IS_RSTR(obj)) {
360 return RSTR(obj)->encoding;
361 }
362 // TODO support symbols
363 return NULL;
364 }
365
366 rb_encoding_t *
367 rb_to_encoding(VALUE obj)
368 {
369 rb_encoding_t *enc;
370 if (CLASS_OF(obj) == rb_cEncoding) {
371 enc = RENC(obj);
372 }
373 else {
374 StringValue(obj);
375 enc = rb_enc_find(RSTRING_PTR(obj));
376 if (enc == NULL) {
377 rb_raise(rb_eArgError, "unknown encoding name - %s",
378 RSTRING_PTR(obj));
379 }
380 }
381 return enc;
382 }
383
384 const char *
385 rb_enc_name(rb_encoding_t *enc)
386 {
387 return RENC(enc)->public_name;
388 }
389
390 VALUE
391 rb_enc_name2(rb_encoding_t *enc)
392 {
393 return rb_usascii_str_new2(rb_enc_name(enc));
394 }
395
396 long
397 rb_enc_mbminlen(rb_encoding_t *enc)
398 {
399 return enc->min_char_size;
400 }
401
402 long
403 rb_enc_mbmaxlen(rb_encoding_t *enc)
404 {
405 return enc->single_byte_encoding ? 1 : 10; // XXX 10?
406 }
407
4cd5f5e added missing MRI methods
Laurent Sansonetti authored
408 rb_encoding *
409 rb_ascii8bit_encoding(void)
410 {
411 return rb_encodings[ENCODING_BINARY];
412 }
413
414 rb_encoding *
415 rb_utf8_encoding(void)
416 {
417 return rb_encodings[ENCODING_UTF8];
418 }
419
420 rb_encoding *
421 rb_usascii_encoding(void)
422 {
423 return rb_encodings[ENCODING_ASCII];
424 }
425
96ab900 more work
Laurent Sansonetti authored
426 rb_encoding_t *
427 rb_locale_encoding(void)
428 {
429 // XXX
430 return rb_encodings[ENCODING_UTF8];
431 }
432
433 void
434 rb_enc_set_default_external(VALUE encoding)
435 {
436 assert(CLASS_OF(encoding) == rb_cEncoding);
437 default_external = RENC(encoding);
438 }
439
Something went wrong with that request. Please try again.