Skip to content
Browse files

some misc fixes/cleanup

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/branches/icu@3632 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
1 parent 20ea16d commit 4ee0afb808725979fa5b0390e124eb1d28e370fe Laurent Sansonetti committed Feb 26, 2010
Showing with 70 additions and 79 deletions.
  1. +1 −1 include/ruby/intern.h
  2. +10 −18 parse.y
  3. +19 −3 string.c
  4. +40 −57 symbol.c
View
2 include/ruby/intern.h
@@ -588,7 +588,7 @@ VALUE rb_str_append(VALUE, VALUE);
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);
View
28 parse.y
@@ -266,23 +266,10 @@ struct parser_params {
};
#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 @@ rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
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 @@ rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
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*
View
22 string.c
@@ -456,7 +456,7 @@ str_get_uchar(rb_str_t *self, long pos, bool ucs2_mode)
// 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 @@ rb_str_get_uchars(VALUE str, UChar **chars_p, long *chars_len_p,
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 @@ rb_str_dup(VALUE str)
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 @@ rb_str_hash_uchars(const UChar *chars, long len)
#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)
{
View
97 symbol.c
@@ -24,7 +24,6 @@ typedef struct {
VALUE klass;
rb_str_t *str;
ID id;
- SEL sel;
} rb_sym_t;
#define RSYM(obj) ((rb_sym_t *)(obj))
@@ -38,72 +37,67 @@ sym_alloc(rb_str_t *str, ID id)
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;
-
- case '@':
- if (chars_len > 1 && chars[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;
+ 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 && rb_str_get_uchar(str, 1) == '@') {
+ id = ID_CLASS;
}
- id = ID_ATTRSET;
- }
- else if (iswupper(chars[0])) {
- id = ID_CONST;
- }
- else {
- id = ID_LOCAL;
- }
- break;
+ else {
+ id = ID_INSTANCE;
+ }
+ break;
+
+ 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;
+ }
+ 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 @@ Init_PreSymbol(void)
// 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);
- }
}
}

0 comments on commit 4ee0afb

Please sign in to comment.
Something went wrong with that request. Please try again.