Permalink
Browse files

indented code, better type checking, removed rb_cCFString, started ad…

…ding MRI functions

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/branches/icu@3547 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
1 parent b881853 commit 0382b34bce0ef71ec6f8f33aec0f17c5ef04e9c7 @lrz lrz committed Feb 16, 2010
Showing with 381 additions and 207 deletions.
  1. +10 −12 class.c
  2. +2 −4 dispatcher.cpp
  3. +31 −19 encoding.c
  4. +56 −23 encoding.h
  5. +2 −7 include/ruby/ruby.h
  6. +3 −3 object.c
  7. +277 −139 string.c
View
22 class.c
@@ -225,18 +225,16 @@ rb_objc_create_class(const char *name, VALUE super)
{
VALUE klass;
- if (super == rb_cCFString) {
- super = rb_cNSMutableString;
- }
- else {
- if (!RCLASS_RUBY(super)) {
- const long v = RCLASS_VERSION(super);
- if (v & RCLASS_IS_HASH_SUBCLASS) {
- super = rb_cNSMutableHash;
- }
- else if (v & RCLASS_IS_ARRAY_SUBCLASS) {
- super = rb_cNSMutableArray;
- }
+ if (!RCLASS_RUBY(super)) {
+ const long v = RCLASS_VERSION(super);
+ if (v & RCLASS_IS_STRING_SUBCLASS) {
+ super = rb_cNSMutableString;
+ }
+ else if (v & RCLASS_IS_HASH_SUBCLASS) {
+ super = rb_cNSMutableHash;
+ }
+ else if (v & RCLASS_IS_ARRAY_SUBCLASS) {
+ super = rb_cNSMutableArray;
}
}
View
@@ -824,10 +824,6 @@ __rb_vm_dispatch(RoxorVM *vm, struct mcache *cache, VALUE top, VALUE self,
if (self == Qfalse) {
return rb_cFalseClass;
}
- if (klass == (Class)rb_cCFString) {
- return RSTRING_IMMUTABLE(self)
- ? rb_cNSString : rb_cNSMutableString;
- }
return rb_class_real((VALUE)klass);
}
@@ -1325,12 +1321,14 @@ rb_vm_fast_shift(VALUE obj, VALUE other, struct mcache *cache,
}
break;
+#if 0 // TODO
case T_STRING:
if (*(VALUE *)obj == rb_cCFString) {
rb_str_concat(obj, other);
return obj;
}
break;
+#endif
}
}
return __rb_vm_dispatch(GET_VM(), cache, 0, obj, NULL, selLTLT, NULL, 0, 1,
View
@@ -7,7 +7,6 @@
VALUE rb_cEncoding = 0;
#define ENC(x) ((encoding_t *)(x))
-#define OBJC_CLASS(x) (*(VALUE *)(x))
encoding_t *default_internal = NULL;
encoding_t *default_external = NULL;
@@ -82,7 +81,8 @@ mr_enc_name(VALUE self, SEL sel)
static VALUE
mr_enc_inspect(VALUE self, SEL sel)
{
- return rb_sprintf("#<%s:%s>", rb_obj_classname(self), ENC(self)->public_name);
+ return rb_sprintf("#<%s:%s>", rb_obj_classname(self),
+ ENC(self)->public_name);
}
static VALUE
@@ -144,13 +144,16 @@ enum {
static void
add_encoding(
- unsigned int encoding_index, // index of the encoding in the encodings array
+ unsigned int encoding_index, // index of the encoding in the encodings
+ // array
unsigned int encoding_type,
const char *public_name, // public name for the encoding
unsigned char min_char_size,
- bool single_byte_encoding, // in the encoding a character takes only one byte
+ bool single_byte_encoding, // in the encoding a character takes only
+ // one byte
bool ascii_compatible, // is the encoding ASCII compatible or not
- ... // aliases for the encoding (should no include the public name) - must end with a NULL
+ ... // aliases for the encoding (should no include the public name)
+ // - must end with a NULL
)
{
assert(encoding_index < ENCODINGS_COUNT);
@@ -163,7 +166,8 @@ add_encoding(
++aliases_count;
}
va_end(va_aliases);
- const char **aliases = (const char **) malloc(sizeof(const char *) * aliases_count);
+ const char **aliases = (const char **)
+ malloc(sizeof(const char *) * aliases_count);
va_start(va_aliases, ascii_compatible);
for (unsigned int i = 0; i < aliases_count; ++i) {
aliases[i] = va_arg(va_aliases, const char *);
@@ -189,11 +193,14 @@ add_encoding(
// fill the default implementations with aborts
encoding->methods.update_flags = str_undefined_update_flags;
encoding->methods.make_data_binary = str_undefined_make_data_binary;
- encoding->methods.try_making_data_uchars = str_undefined_try_making_data_uchars;
+ encoding->methods.try_making_data_uchars =
+ str_undefined_try_making_data_uchars;
encoding->methods.length = str_undefined_length;
encoding->methods.bytesize = str_undefined_bytesize;
- encoding->methods.get_character_boundaries = str_undefined_get_character_boundaries;
- encoding->methods.offset_in_bytes_to_index = str_undefined_offset_in_bytes_to_index;
+ encoding->methods.get_character_boundaries =
+ str_undefined_get_character_boundaries;
+ encoding->methods.offset_in_bytes_to_index =
+ str_undefined_offset_in_bytes_to_index;
switch (encoding_type) {
case ENCODING_TYPE_SPECIAL:
@@ -210,8 +217,6 @@ add_encoding(
for (unsigned int i = 0; i < aliases_count; ++i) {
define_encoding_constant(aliases[i], encoding);
}
-
- free(aliases);
}
static void
@@ -249,20 +254,27 @@ Init_Encoding(void)
rb_objc_define_method(rb_cEncoding, "name", mr_enc_name, 0);
rb_objc_define_method(rb_cEncoding, "names", mr_enc_names, 0);
rb_objc_define_method(rb_cEncoding, "dummy?", mr_enc_dummy_p, 0);
- rb_objc_define_method(rb_cEncoding, "ascii_compatible?", mr_enc_ascii_compatible_p, 0);
- rb_objc_define_method(OBJC_CLASS(rb_cEncoding), "list", mr_enc_s_list, 0);
- rb_objc_define_method(OBJC_CLASS(rb_cEncoding), "name_list", mr_enc_s_name_list, 0);
- rb_objc_define_method(OBJC_CLASS(rb_cEncoding), "aliases", mr_enc_s_aliases, 0);
+ rb_objc_define_method(rb_cEncoding, "ascii_compatible?",
+ mr_enc_ascii_compatible_p, 0);
+ rb_objc_define_method(CLASS_OF(rb_cEncoding), "list", mr_enc_s_list, 0);
+ rb_objc_define_method(CLASS_OF(rb_cEncoding), "name_list",
+ mr_enc_s_name_list, 0);
+ rb_objc_define_method(CLASS_OF(rb_cEncoding), "aliases",
+ mr_enc_s_aliases, 0);
//rb_define_singleton_method(rb_cEncoding, "find", enc_find, 1);
- // it's defined on Encoding, but it requires String's internals so it's defined with String
- rb_objc_define_method(OBJC_CLASS(rb_cEncoding), "compatible?", mr_enc_s_is_compatible, 2);
+ // it's defined on Encoding, but it requires String's internals so it's
+ // defined with String
+ rb_objc_define_method(CLASS_OF(rb_cEncoding), "compatible?",
+ mr_enc_s_is_compatible, 2);
//rb_define_method(rb_cEncoding, "_dump", enc_dump, -1);
//rb_define_singleton_method(rb_cEncoding, "_load", enc_load, 1);
- rb_objc_define_method(OBJC_CLASS(rb_cEncoding), "default_external", mr_enc_s_default_external, 0);
+ rb_objc_define_method(CLASS_OF(rb_cEncoding), "default_external",
+ mr_enc_s_default_external, 0);
//rb_define_singleton_method(rb_cEncoding, "default_external=", set_default_external, 1);
- rb_objc_define_method(OBJC_CLASS(rb_cEncoding), "default_internal", mr_enc_s_default_internal, 0);
+ rb_objc_define_method(CLASS_OF(rb_cEncoding), "default_internal",
+ mr_enc_s_default_internal, 0);
//rb_define_singleton_method(rb_cEncoding, "default_internal=", set_default_internal, 1);
//rb_define_singleton_method(rb_cEncoding, "locale_charmap", rb_locale_charmap, 0);
View
@@ -21,12 +21,18 @@ extern "C" {
#define ENCODING_UTF32_NON_NATIVE ENCODING_UTF32LE
#endif
-#define NATIVE_UTF16_ENC(encoding) ((encoding) == encodings[ENCODING_UTF16_NATIVE])
-#define NON_NATIVE_UTF16_ENC(encoding) ((encoding) == encodings[ENCODING_UTF16_NON_NATIVE])
-#define UTF16_ENC(encoding) (NATIVE_UTF16_ENC(encoding) || NON_NATIVE_UTF16_ENC(encoding))
-#define NATIVE_UTF32_ENC(encoding) ((encoding) == encodings[ENCODING_UTF32_NATIVE])
-#define NON_NATIVE_UTF32_ENC(encoding) ((encoding) == encodings[ENCODING_UTF32_NON_NATIVE])
-#define UTF32_ENC(encoding) (NATIVE_UTF32_ENC(encoding) || NON_NATIVE_UTF32_ENC(encoding))
+#define NATIVE_UTF16_ENC(encoding) \
+ ((encoding) == encodings[ENCODING_UTF16_NATIVE])
+#define NON_NATIVE_UTF16_ENC(encoding) \
+ ((encoding) == encodings[ENCODING_UTF16_NON_NATIVE])
+#define UTF16_ENC(encoding) \
+ (NATIVE_UTF16_ENC(encoding) || NON_NATIVE_UTF16_ENC(encoding))
+#define NATIVE_UTF32_ENC(encoding) \
+ ((encoding) == encodings[ENCODING_UTF32_NATIVE])
+#define NON_NATIVE_UTF32_ENC(encoding) \
+ ((encoding) == encodings[ENCODING_UTF32_NON_NATIVE])
+#define UTF32_ENC(encoding) \
+ (NATIVE_UTF32_ENC(encoding) || NON_NATIVE_UTF32_ENC(encoding))
#define BINARY_ENC(encoding) ((encoding) == encodings[ENCODING_BINARY])
typedef uint8_t str_flag_t;
@@ -43,6 +49,26 @@ typedef struct {
str_flag_t flags;
} string_t;
+#define RSTR(x) ((string_t *)x)
+
+static inline bool
+rb_klass_is_rstr(VALUE klass)
+{
+ do {
+ if (klass == rb_cRubyString) {
+ return true;
+ }
+ if (klass == rb_cNSString) {
+ return false;
+ }
+ klass = RCLASS_SUPER(klass);
+ }
+ while (klass != 0);
+ return false;
+}
+
+#define IS_RSTR(x) (rb_klass_is_rstr(*(VALUE *)x))
+
typedef struct {
long start_offset_in_bytes;
long end_offset_in_bytes;
@@ -103,8 +129,6 @@ extern VALUE rb_cMREncoding;
#define STRING_REQUIRED_FLAGS STRING_STORED_IN_UCHARS
-#define STR(x) ((string_t *)(x))
-
#define BYTES_TO_UCHARS(len) ((len) / sizeof(UChar))
#define UCHARS_TO_BYTES(len) ((len) * sizeof(UChar))
@@ -116,29 +140,32 @@ div_round_up(long a, long b)
return ((a) + (b - 1)) / b;
}
-void
-str_update_flags(string_t *self);
+void str_update_flags(string_t *self);
static inline void
str_unset_facultative_flags(string_t *self)
{
- self->flags &= ~STRING_HAS_SUPPLEMENTARY_SET & ~STRING_ASCII_ONLY_SET & ~STRING_VALID_ENCODING_SET;
+ self->flags &= ~STRING_HAS_SUPPLEMENTARY_SET & ~STRING_ASCII_ONLY_SET
+ & ~STRING_VALID_ENCODING_SET;
}
static inline bool
str_known_to_have_an_invalid_encoding(string_t *self)
{
- return (self->flags & (STRING_VALID_ENCODING_SET | STRING_VALID_ENCODING)) == STRING_VALID_ENCODING_SET;
+ return (self->flags & (STRING_VALID_ENCODING_SET
+ | STRING_VALID_ENCODING)) == STRING_VALID_ENCODING_SET;
}
static inline bool
str_known_not_to_have_any_supplementary(string_t *self)
{
- return (self->flags & (STRING_HAS_SUPPLEMENTARY_SET | STRING_HAS_SUPPLEMENTARY)) == STRING_HAS_SUPPLEMENTARY_SET;
+ return (self->flags & (STRING_HAS_SUPPLEMENTARY_SET
+ | STRING_HAS_SUPPLEMENTARY)) == STRING_HAS_SUPPLEMENTARY_SET;
}
static inline bool
-str_check_flag_and_update_if_needed(string_t *self, str_flag_t flag_set, str_flag_t flag)
+str_check_flag_and_update_if_needed(string_t *self, str_flag_t flag_set,
+ str_flag_t flag)
{
if (!(self->flags & flag_set)) {
str_update_flags(self);
@@ -150,24 +177,26 @@ str_check_flag_and_update_if_needed(string_t *self, str_flag_t flag_set, str_fla
static inline bool
str_is_valid_encoding(string_t *self)
{
- return str_check_flag_and_update_if_needed(self, STRING_VALID_ENCODING_SET, STRING_VALID_ENCODING);
+ return str_check_flag_and_update_if_needed(self, STRING_VALID_ENCODING_SET,
+ STRING_VALID_ENCODING);
}
static inline bool
str_is_ascii_only(string_t *self)
{
- return str_check_flag_and_update_if_needed(self, STRING_ASCII_ONLY_SET, STRING_ASCII_ONLY);
+ return str_check_flag_and_update_if_needed(self, STRING_ASCII_ONLY_SET,
+ STRING_ASCII_ONLY);
}
static inline bool
str_is_ruby_ascii_only(string_t *self)
{
// for MRI, a string in a non-ASCII-compatible encoding (like UTF-16)
- // containing only ASCII characters is not "ASCII only" though for us it is internally
+ // containing only ASCII characters is not "ASCII only" though for us it
+ // is internally
if (!self->encoding->ascii_compatible) {
return false;
}
-
return str_is_ascii_only(self);
}
@@ -195,7 +224,8 @@ str_set_stored_in_uchars(string_t *self, bool status)
}
static inline void
-str_set_facultative_flag(string_t *self, bool status, str_flag_t flag_set, str_flag_t flag)
+str_set_facultative_flag(string_t *self, bool status, str_flag_t flag_set,
+ str_flag_t flag)
{
if (status) {
self->flags = self->flags | flag_set | flag;
@@ -208,23 +238,26 @@ str_set_facultative_flag(string_t *self, bool status, str_flag_t flag_set, str_f
static inline void
str_set_has_supplementary(string_t *self, bool status)
{
- str_set_facultative_flag(self, status, STRING_HAS_SUPPLEMENTARY_SET, STRING_HAS_SUPPLEMENTARY);
+ str_set_facultative_flag(self, status, STRING_HAS_SUPPLEMENTARY_SET,
+ STRING_HAS_SUPPLEMENTARY);
}
static inline void
str_set_ascii_only(string_t *self, bool status)
{
- str_set_facultative_flag(self, status, STRING_ASCII_ONLY_SET, STRING_ASCII_ONLY);
+ str_set_facultative_flag(self, status, STRING_ASCII_ONLY_SET,
+ STRING_ASCII_ONLY);
}
static inline void
str_set_valid_encoding(string_t *self, bool status)
{
- str_set_facultative_flag(self, status, STRING_VALID_ENCODING_SET, STRING_VALID_ENCODING);
+ str_set_facultative_flag(self, status, STRING_VALID_ENCODING_SET,
+ STRING_VALID_ENCODING);
}
#if defined(__cplusplus)
-}
+} // extern "C"
#endif
#endif /* __ENCODING_H_ */
View
@@ -1134,9 +1134,9 @@ RUBY_EXTERN VALUE rb_cEnv;
RUBY_EXTERN VALUE rb_cRandom;
#if WITH_OBJC
-RUBY_EXTERN VALUE rb_cCFString;
RUBY_EXTERN VALUE rb_cNSString;
RUBY_EXTERN VALUE rb_cNSMutableString;
+RUBY_EXTERN VALUE rb_cRubyString;
RUBY_EXTERN VALUE rb_cNSArray;
RUBY_EXTERN VALUE rb_cNSMutableArray;
RUBY_EXTERN VALUE rb_cRubyArray;
@@ -1147,11 +1147,6 @@ RUBY_EXTERN VALUE rb_cCFNumber;
RUBY_EXTERN VALUE rb_cBoxed;
RUBY_EXTERN VALUE rb_cPointer;
RUBY_EXTERN VALUE rb_cTopLevel;
-
-bool __CFStringIsMutable(void *);
-#define RSTRING_IMMUTABLE(o) \
- (*(VALUE *)o == rb_cCFString ? !__CFStringIsMutable((void *)o) : false)
-
#endif
RUBY_EXTERN VALUE rb_eException;
@@ -1257,7 +1252,7 @@ rb_type(VALUE obj)
}
#if WITH_OBJC
else if ((k = *(Class *)obj) != NULL) {
- if (k == (Class)rb_cCFString) {
+ if (k == (Class)rb_cRubyString) {
return T_STRING;
}
if (k == (Class)rb_cRubyArray) {
View
@@ -163,11 +163,11 @@ rb_class_real(VALUE cl)
while (RCLASS_SINGLETON(cl)) {
cl = RCLASS_SUPER(cl);
}
- if (cl == rb_cCFString) {
- return rb_cNSMutableString;
- }
if (!RCLASS_RUBY(cl)) {
const long v = RCLASS_VERSION(cl);
+ if (v & RCLASS_IS_STRING_SUBCLASS) {
+ return rb_cRubyString;
+ }
if (v & RCLASS_IS_HASH_SUBCLASS) {
return rb_cRubyHash;
}
Oops, something went wrong.

0 comments on commit 0382b34

Please sign in to comment.