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