Revision: 3632 http://trac.macosforge.org/projects/ruby/changeset/3632 Author: lsansonetti@apple.com Date: 2010-02-25 21:24:06 -0800 (Thu, 25 Feb 2010) Log Message: ----------- some misc fixes/cleanup Modified Paths: -------------- MacRuby/branches/icu/include/ruby/intern.h MacRuby/branches/icu/parse.y MacRuby/branches/icu/string.c MacRuby/branches/icu/symbol.c Modified: MacRuby/branches/icu/include/ruby/intern.h =================================================================== --- MacRuby/branches/icu/include/ruby/intern.h 2010-02-26 02:52:51 UTC (rev 3631) +++ MacRuby/branches/icu/include/ruby/intern.h 2010-02-26 05:24:06 UTC (rev 3632) @@ -588,7 +588,7 @@ VALUE rb_str_concat(VALUE, VALUE); VALUE rb_str_plus(VALUE str1, VALUE str2); long rb_memhash(const void *ptr, long len); -int rb_str_hash(VALUE); +unsigned long rb_str_hash(VALUE); int rb_str_hash_cmp(VALUE,VALUE); int rb_str_comparable(VALUE, VALUE); int rb_str_cmp(VALUE, VALUE); Modified: MacRuby/branches/icu/parse.y =================================================================== --- MacRuby/branches/icu/parse.y 2010-02-26 02:52:51 UTC (rev 3631) +++ MacRuby/branches/icu/parse.y 2010-02-26 05:24:06 UTC (rev 3632) @@ -266,23 +266,10 @@ }; #if WITH_OBJC +// TODO: we should probably mimic what 1.9 does here and use the right/given +// encoding instead of always UTF8. # define UTF8_ENC() (NULL) -static inline VALUE -__new_tmp_str(const char *ptr, const size_t len) -{ - if (ptr != NULL) { - CFStringRef str = CFStringCreateWithBytes(NULL, (UInt8 *)ptr, len, - kCFStringEncodingUTF8, false); - if (str != NULL) { - CFMutableStringRef str2 = - CFStringCreateMutableCopy(NULL, 0, str); - assert(str2 != NULL); - CFRelease(str); - return (VALUE)CFMakeCollectable(str2); - } - } - return rb_usascii_str_new(ptr, len); -} +#define __new_tmp_str(p, n) (rb_str_new(p, n)) # define STR_NEW(p,n) __new_tmp_str(p, n) # define STR_NEW0() __new_tmp_str(0, 0) # define STR_NEW2(p) __new_tmp_str(p, strlen(p)) @@ -5097,7 +5084,6 @@ long chars_len = 0; bool need_free = false; rb_str_get_uchars(s, &chars, &chars_len, &need_free); - assert(!need_free); struct lex_get_str_context *ctx = (struct lex_get_str_context *) xmalloc(sizeof(struct lex_get_str_context)); @@ -5111,7 +5097,13 @@ lex_pbeg = lex_p = lex_pend = 0; compile_for_eval = rb_parse_in_eval(); - return yycompile(parser, f, line); + NODE *node = yycompile(parser, f, line); + + if (need_free && chars != NULL) { + free(chars); + } + + return node; } NODE* Modified: MacRuby/branches/icu/string.c =================================================================== --- MacRuby/branches/icu/string.c 2010-02-26 02:52:51 UTC (rev 3631) +++ MacRuby/branches/icu/string.c 2010-02-26 05:24:06 UTC (rev 3632) @@ -456,7 +456,7 @@ // FIXME: Not ucs2 compliant. return self->data.uchars[pos]; } - assert(BINARY_ENC(self->encoding)); + //assert(BINARY_ENC(self->encoding)); return self->data.bytes[pos]; } @@ -1067,7 +1067,7 @@ chars_len = str_length(RSTR(str), false); } else { - assert(BINARY_ENC(RSTR(str)->encoding)); + //assert(BINARY_ENC(RSTR(str)->encoding)); chars_len = RSTR(str)->length_in_bytes; if (chars_len > 0) { chars = (UChar *)malloc(sizeof(UChar) * chars_len); @@ -4027,13 +4027,15 @@ unsigned long rb_str_hash_uchars(const UChar *chars, long len) { + if (len == 0 || chars == NULL) { + return 0; + } #define HashNextFourUniChars(accessStart, accessEnd, pointer) \ {result = result * 67503105 + (accessStart 0 accessEnd) * 16974593 + (accessStart 1 accessEnd) * 66049 + (accessStart 2 accessEnd) * 257 + (accessStart 3 accessEnd); pointer += 4;} #define HashNextUniChar(accessStart, accessEnd, pointer) \ {result = result * 257 + (accessStart 0 accessEnd); pointer++;} - assert(len > 0); unsigned long result = len; const UChar *end4 = chars + (len & ~3); const UChar *end = chars + len; @@ -4047,6 +4049,20 @@ #undef HashNextUniChar } +unsigned long +rb_str_hash(VALUE str) +{ + UChar *chars = NULL; + long chars_len = 0; + bool need_free = false; + rb_str_get_uchars(str, &chars, &chars_len, &need_free); + const unsigned long hash = rb_str_hash_uchars(chars, chars_len); + if (need_free) { + free(chars); + } + return hash; +} + long rb_memhash(const void *ptr, long len) { Modified: MacRuby/branches/icu/symbol.c =================================================================== --- MacRuby/branches/icu/symbol.c 2010-02-26 02:52:51 UTC (rev 3631) +++ MacRuby/branches/icu/symbol.c 2010-02-26 05:24:06 UTC (rev 3632) @@ -24,7 +24,6 @@ VALUE klass; rb_str_t *str; ID id; - SEL sel; } rb_sym_t; #define RSYM(obj) ((rb_sym_t *)(obj)) @@ -38,72 +37,67 @@ GC_RETAIN(str); // never released sym->str = str; sym->id = id; - sym->sel = NULL; // lazy return sym; } ID rb_intern_str(VALUE str) { - UChar *chars = NULL; - long chars_len = 0; - bool need_free = false; - rb_str_get_uchars(str, &chars, &chars_len, &need_free); - assert(chars_len > 0); - - const unsigned long name_hash = rb_str_hash_uchars(chars, chars_len); + const unsigned long name_hash = rb_str_hash(str); ID id = (ID)CFDictionaryGetValue(sym_id, (const void *)name_hash); if (id != 0) { - goto return_id; + return id; } rb_sym_t *sym = NULL; - switch (chars[0]) { - case '$': - id = ID_GLOBAL; - break; + const long chars_len = rb_str_chars_len(str); + if (chars_len > 0) { + UChar c = rb_str_get_uchar(str, 0); + switch (c) { + case '$': + id = ID_GLOBAL; + break; - case '@': - if (chars_len > 1 && chars[1] == '@') { - id = ID_CLASS; - } - else { - id = ID_INSTANCE; - } - break; + case '@': + if (chars_len > 1 && rb_str_get_uchar(str, 1) == '@') { + id = ID_CLASS; + } + else { + id = ID_INSTANCE; + } + break; - default: - if (chars_len > 1 && chars[chars_len - 1] == '=') { - // Attribute assignment. - id = rb_intern_str(rb_str_substr(str, 0, chars_len - 1)); - if (!is_attrset_id(id)) { - id = rb_id_attrset(id); - goto id_register; + default: + if (chars_len > 1 + && rb_str_get_uchar(str, chars_len - 1) == '=') { + // Attribute assignment. + id = rb_intern_str(rb_str_substr(str, 0, chars_len - 1)); + if (!is_attrset_id(id)) { + id = rb_id_attrset(id); + goto id_register; + } + id = ID_ATTRSET; } - id = ID_ATTRSET; - } - else if (iswupper(chars[0])) { - id = ID_CONST; - } - else { - id = ID_LOCAL; - } - break; + else if (iswupper(c)) { + id = ID_CONST; + } + else { + id = ID_LOCAL; + } + break; + } } id |= ++last_id << ID_SCOPE_SHIFT; id_register: //printf("register %s hash %ld id %ld\n", RSTRING_PTR(str), name_hash, id); + assert(IS_RSTR(str)); sym = sym_alloc(RSTR(str), id); CFDictionarySetValue(sym_id, (const void *)name_hash, (const void *)id); CFDictionarySetValue(id_str, (const void *)id, (const void *)sym); -return_id: - if (need_free && chars != NULL) { - free(chars); - } return id; } @@ -233,26 +227,15 @@ // Pre-register parser symbols. for (int i = 0; rb_op_tbl[i].token != 0; i++) { - VALUE str = rb_str_new2(rb_op_tbl[i].name); - - UChar *chars = NULL; - long chars_len = 0; - bool need_free = false; - rb_str_get_uchars(str, &chars, &chars_len, &need_free); - assert(chars_len > 0); - ID id = rb_op_tbl[i].token; + VALUE str = rb_str_new2(rb_op_tbl[i].name); rb_sym_t *sym = sym_alloc(RSTR(str), id); - unsigned long name_hash = rb_str_hash_uchars(chars, chars_len); + unsigned long name_hash = rb_str_hash(str); //printf("pre-register %s hash %ld id %ld\n", RSTRING_PTR(str), name_hash, id); CFDictionarySetValue(sym_id, (const void *)name_hash, (const void *)id); CFDictionarySetValue(id_str, (const void *)id, (const void *)sym); - - if (need_free && chars != NULL) { - free(chars); - } } }
participants (1)
-
source_changes@macosforge.org