Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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