Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 419 lines (368 sloc) 12.863 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 {
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);
263 add_encoding(ENCODING_UTF8, ENCODING_TYPE_UCNV, "UTF-8", 1, false, true, "CP65001", NULL);
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 // FIXME: the ICU conversion tables do not seem to match Ruby's Japanese conversion tables
271 //add_encoding(ENCODING_EUCJP, ENCODING_TYPE_RUBY, "EUC-JP", 1, false, true, "eucJP", NULL);
272 //add_encoding(ENCODING_SJIS, ENCODING_TYPE_RUBY, "Shift_JIS", 1, false, true, "SJIS", NULL);
273 //add_encoding(ENCODING_CP932, ENCODING_TYPE_RUBY, "Windows-31J", 1, false, true, "CP932", "csWindows31J", NULL);
274
96ab900 more work
Laurent Sansonetti authored
275 default_external = rb_encodings[ENCODING_UTF8];
276 default_internal = rb_encodings[ENCODING_UTF8];
9c1d230 committing experimental branch content
Laurent Sansonetti authored
277 }
278
2b7d5d5 import vincent's work
Laurent Sansonetti authored
279 void
b881853 s/MR//
Laurent Sansonetti authored
280 Init_Encoding(void)
2b7d5d5 import vincent's work
Laurent Sansonetti authored
281 {
ae4da82 more work
Laurent Sansonetti authored
282 // rb_cEncoding is defined earlier in Init_PreVM().
283 rb_set_class_path(rb_cEncoding, rb_cObject, "Encoding");
284 rb_const_set(rb_cObject, rb_intern("Encoding"), rb_cEncoding);
285
b881853 s/MR//
Laurent Sansonetti authored
286 rb_undef_alloc_func(rb_cEncoding);
287
288 rb_objc_define_method(rb_cEncoding, "to_s", mr_enc_name, 0);
289 rb_objc_define_method(rb_cEncoding, "inspect", mr_enc_inspect, 0);
290 rb_objc_define_method(rb_cEncoding, "name", mr_enc_name, 0);
291 rb_objc_define_method(rb_cEncoding, "names", mr_enc_names, 0);
292 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
293 rb_objc_define_method(rb_cEncoding, "ascii_compatible?",
294 mr_enc_ascii_compatible_p, 0);
4ede652 added #find
Laurent Sansonetti authored
295 rb_objc_define_method(*(VALUE *)rb_cEncoding, "list", mr_enc_s_list, 0);
296 rb_objc_define_method(*(VALUE *)rb_cEncoding, "name_list",
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
297 mr_enc_s_name_list, 0);
4ede652 added #find
Laurent Sansonetti authored
298 rb_objc_define_method(*(VALUE *)rb_cEncoding, "aliases",
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
299 mr_enc_s_aliases, 0);
4ede652 added #find
Laurent Sansonetti authored
300 rb_objc_define_method(*(VALUE *)rb_cEncoding, "find", mr_enc_s_find, 1);
301 rb_objc_define_method(*(VALUE *)rb_cEncoding, "compatible?",
39b55f1 some work on string
Laurent Sansonetti authored
302 mr_enc_s_is_compatible, 2); // in string.c
2b7d5d5 import vincent's work
Laurent Sansonetti authored
303
304 //rb_define_method(rb_cEncoding, "_dump", enc_dump, -1);
305 //rb_define_singleton_method(rb_cEncoding, "_load", enc_load, 1);
306
4ede652 added #find
Laurent Sansonetti authored
307 rb_objc_define_method(*(VALUE *)rb_cEncoding, "default_external",
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
308 mr_enc_s_default_external, 0);
5051413 added #default_external=, #default_internal=
Laurent Sansonetti authored
309 rb_objc_define_method(*(VALUE *)rb_cEncoding, "default_external=",
310 mr_enc_set_default_external, 1);
4ede652 added #find
Laurent Sansonetti authored
311 rb_objc_define_method(*(VALUE *)rb_cEncoding, "default_internal",
0382b34 indented code, better type checking, removed rb_cCFString, started ad…
Laurent Sansonetti authored
312 mr_enc_s_default_internal, 0);
5051413 added #default_external=, #default_internal=
Laurent Sansonetti authored
313 rb_objc_define_method(*(VALUE *)rb_cEncoding, "default_internal=",
314 mr_enc_set_default_internal, 1);
b881853 s/MR//
Laurent Sansonetti authored
315 //rb_define_singleton_method(rb_cEncoding, "locale_charmap", rb_locale_charmap, 0);
2b7d5d5 import vincent's work
Laurent Sansonetti authored
316
ae4da82 more work
Laurent Sansonetti authored
317 // Create constants.
318 for (unsigned int i = 0; i < ENCODINGS_COUNT; i++) {
319 rb_encoding_t *enc = rb_encodings[i];
320 define_encoding_constant(enc->public_name, enc);
321 for (unsigned int j = 0; j < enc->aliases_count; j++) {
322 define_encoding_constant(enc->aliases[j], enc);
323 }
324 }
9c1d230 committing experimental branch content
Laurent Sansonetti authored
325 }
96ab900 more work
Laurent Sansonetti authored
326
327 // MRI C-API compatibility.
328
329 rb_encoding_t *
330 rb_enc_find(const char *name)
331 {
332 for (unsigned int i = 0; i < ENCODINGS_COUNT; i++) {
333 rb_encoding_t *enc = rb_encodings[i];
334 if (strcasecmp(enc->public_name, name) == 0) {
335 return enc;
336 }
337 for (unsigned int j = 0; j < enc->aliases_count; j++) {
338 const char *alias = enc->aliases[j];
339 if (strcasecmp(alias, name) == 0) {
340 return enc;
341 }
342 }
343 }
344 return NULL;
345 }
346
347 VALUE
348 rb_enc_from_encoding(rb_encoding_t *enc)
349 {
350 return (VALUE)enc;
351 }
352
353 rb_encoding_t *
354 rb_enc_get(VALUE obj)
355 {
356 if (IS_RSTR(obj)) {
357 return RSTR(obj)->encoding;
358 }
359 // TODO support symbols
360 return NULL;
361 }
362
363 rb_encoding_t *
364 rb_to_encoding(VALUE obj)
365 {
366 rb_encoding_t *enc;
367 if (CLASS_OF(obj) == rb_cEncoding) {
368 enc = RENC(obj);
369 }
370 else {
371 StringValue(obj);
372 enc = rb_enc_find(RSTRING_PTR(obj));
373 if (enc == NULL) {
374 rb_raise(rb_eArgError, "unknown encoding name - %s",
375 RSTRING_PTR(obj));
376 }
377 }
378 return enc;
379 }
380
381 const char *
382 rb_enc_name(rb_encoding_t *enc)
383 {
384 return RENC(enc)->public_name;
385 }
386
387 VALUE
388 rb_enc_name2(rb_encoding_t *enc)
389 {
390 return rb_usascii_str_new2(rb_enc_name(enc));
391 }
392
393 long
394 rb_enc_mbminlen(rb_encoding_t *enc)
395 {
396 return enc->min_char_size;
397 }
398
399 long
400 rb_enc_mbmaxlen(rb_encoding_t *enc)
401 {
402 return enc->single_byte_encoding ? 1 : 10; // XXX 10?
403 }
404
405 rb_encoding_t *
406 rb_locale_encoding(void)
407 {
408 // XXX
409 return rb_encodings[ENCODING_UTF8];
410 }
411
412 void
413 rb_enc_set_default_external(VALUE encoding)
414 {
415 assert(CLASS_OF(encoding) == rb_cEncoding);
416 default_external = RENC(encoding);
417 }
418
Something went wrong with that request. Please try again.