[macruby-changes] [137] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Tue Apr 15 01:19:49 PDT 2008


Revision: 137
          http://trac.macosforge.org/projects/ruby/changeset/137
Author:   lsansonetti at apple.com
Date:     2008-04-15 01:19:47 -0700 (Tue, 15 Apr 2008)

Log Message:
-----------
fixing some regressions, perf improvements

Modified Paths:
--------------
    MacRuby/trunk/encoding.c
    MacRuby/trunk/parse.y
    MacRuby/trunk/string.c

Modified: MacRuby/trunk/encoding.c
===================================================================
--- MacRuby/trunk/encoding.c	2008-04-12 01:10:16 UTC (rev 136)
+++ MacRuby/trunk/encoding.c	2008-04-15 08:19:47 UTC (rev 137)
@@ -648,12 +648,16 @@
 rb_encoding*
 rb_enc_check(VALUE str1, VALUE str2)
 {
+#if WITH_OBJC
+    return NULL;
+#else
     rb_encoding *enc = rb_enc_compatible(str1, str2);
     if (!enc)
 	rb_raise(rb_eArgError, "character encodings differ: %s and %s",
 		 rb_enc_name(rb_enc_get(str1)),
 		 rb_enc_name(rb_enc_get(str2)));
     return enc;
+#endif
 }
 
 rb_encoding*
@@ -729,11 +733,16 @@
 VALUE
 rb_obj_encoding(VALUE obj)
 {
+#if WITH_OBJC
+    /* TODO */
+    return Qnil;
+#else
     rb_encoding *enc = rb_enc_get(obj);
     if (!enc) {
 	rb_raise(rb_eTypeError, "unknown encoding");
     }
     return rb_enc_from_encoding(enc);
+#endif
 }
 
 int

Modified: MacRuby/trunk/parse.y
===================================================================
--- MacRuby/trunk/parse.y	2008-04-12 01:10:16 UTC (rev 136)
+++ MacRuby/trunk/parse.y	2008-04-15 08:19:47 UTC (rev 137)
@@ -4820,8 +4820,8 @@
     const char *cptr, *beg, *end, *pend;
     long clen;
 
-    cptr = beg = RSTRING_CPTR(s);
-    clen = RSTRING_CLEN(s);
+    cptr = beg = RSTRING_PTR(s);
+    clen = RSTRING_LEN(s);
     if (lex_gets_ptr) {
 	if (clen == lex_gets_ptr) return Qnil;
 	beg += lex_gets_ptr;
@@ -5000,8 +5000,8 @@
 	    }
 	    ruby_sourceline++;
 	    parser->line_count++;
-	    lex_pbeg = lex_p = RSTRING_CPTR(v);
-	    lex_pend = lex_p + RSTRING_CLEN(v);
+	    lex_pbeg = lex_p = RSTRING_PTR(v);
+	    lex_pend = lex_p + RSTRING_LEN(v);
 #ifdef RIPPER
 	    ripper_flush(parser);
 #endif
@@ -5681,8 +5681,8 @@
 #endif
     line = here->nd_orig;
     lex_lastline = line;
-    lex_pbeg = RSTRING_CPTR(line);
-    lex_pend = lex_pbeg + RSTRING_CLEN(line);
+    lex_pbeg = RSTRING_PTR(line);
+    lex_pend = lex_pbeg + RSTRING_LEN(line);
     lex_p = lex_pbeg + here->nd_nth;
     heredoc_end = ruby_sourceline;
     ruby_sourceline = nd_line(here);
@@ -5717,8 +5717,8 @@
     long len;
     VALUE str = 0;
 
-    eos = RSTRING_CPTR(here->nd_lit);
-    len = RSTRING_CLEN(here->nd_lit) - 1;
+    eos = RSTRING_PTR(here->nd_lit);
+    len = RSTRING_LEN(here->nd_lit) - 1;
     indent = (func = *eos++) & STR_FUNC_INDENT;
 
     if ((c = nextc()) == -1) {
@@ -5736,7 +5736,7 @@
 
     if (!(func & STR_FUNC_EXPAND)) {
 	do {
-	    p = RSTRING_CPTR(lex_lastline);
+	    p = RSTRING_PTR(lex_lastline);
 	    pend = lex_pend;
 	    if (pend > p) {
 		switch (pend[-1]) {
@@ -5992,13 +5992,13 @@
 	str_copy(name, beg, n);
 #ifndef RIPPER
 	do {
-	    if (STRNCASECMP(p->name, RSTRING_CPTR(name), n) == 0) {
+	    if (STRNCASECMP(p->name, RSTRING_PTR(name), n) == 0) {
 		n = vend - vbeg;
 		if (p->length) {
 		    n = (*p->length)(parser, vbeg, n);
 		}
 		str_copy(val, vbeg, n);
-		(*p->func)(parser, RSTRING_CPTR(name), RSTRING_CPTR(val));
+		(*p->func)(parser, RSTRING_PTR(name), RSTRING_PTR(val));
 		break;
 	    }
 	} while (++p < magic_comments + sizeof(magic_comments) / sizeof(*p));
@@ -6049,7 +6049,7 @@
     beg = str;
     while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
     s = rb_str_new(beg, parser_encode_length(parser, beg, str - beg));
-    parser_set_encode(parser, RSTRING_CPTR(s));
+    parser_set_encode(parser, RSTRING_PTR(s));
     rb_str_resize(s, 0);
 }
 
@@ -6077,7 +6077,7 @@
     pushback(c);
     parser->enc = rb_enc_get(lex_lastline);
     if (parser->enc == NULL)
-	parser->enc = rb_usascii_encoding();
+	parser->enc = rb_utf8_encoding();
 }
 
 #define IS_ARG() (lex_state == EXPR_ARG || lex_state == EXPR_CMDARG)
@@ -8663,7 +8663,7 @@
     err = rb_reg_check_preprocess(str);
     if (err != Qnil) {
         err = rb_obj_as_string(err);
-        compile_error(PARSER_ARG "%s", RSTRING_CPTR(err));
+        compile_error(PARSER_ARG "%s", RSTRING_PTR(err));
 	RB_GC_GUARD(err);
     }
 }
@@ -8764,7 +8764,7 @@
 	    rb_str_append(rb_str_cat(rb_attr_get(err, mesg), "\n", 1), m);
 	}
 	else {
-	    compile_error(PARSER_ARG "%s", RSTRING_CPTR(m));
+	    compile_error(PARSER_ARG "%s", RSTRING_PTR(m));
 	}
 	return Qnil;
     }
@@ -9249,7 +9249,7 @@
     else {
 	enc = rb_enc_get(str);
     }
-    id = rb_intern3(RSTRING_CPTR(str), RSTRING_CLEN(str), enc);
+    id = rb_intern3(RSTRING_PTR(str), RSTRING_LEN(str), enc);
     RB_GC_GUARD(str);
     return id;
 }
@@ -9329,7 +9329,7 @@
     VALUE str = rb_id2str(id);
 
     if (!str) return 0;
-    return RSTRING_CPTR(str);
+    return RSTRING_PTR(str);
 }
 
 static int
@@ -9951,7 +9951,7 @@
     parser_initialize(parser);
 
     parser->parser_ruby_sourcefile_string = fname2;
-    parser->parser_ruby_sourcefile = RSTRING_CPTR(fname2)+1;
+    parser->parser_ruby_sourcefile = RSTRING_PTR(fname2)+1;
     parser->parser_ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
 
     return Qnil;
@@ -10062,7 +10062,7 @@
 {
     StringValue(msg);
     if (obj == Qundef) {
-        rb_raise(rb_eArgError, "%s", RSTRING_CPTR(msg));
+        rb_raise(rb_eArgError, "%s", RSTRING_PTR(msg));
     }
     return Qnil;
 }

Modified: MacRuby/trunk/string.c
===================================================================
--- MacRuby/trunk/string.c	2008-04-12 01:10:16 UTC (rev 136)
+++ MacRuby/trunk/string.c	2008-04-15 08:19:47 UTC (rev 137)
@@ -136,7 +136,11 @@
 	    (CFStringRef)str, kCFStringEncodingUTF8, 0);
 	if (data == NULL)
 	    return NULL;
+#if 1 
 	GC_WB(&s->cfdata, (void *)CFDataCreateCopy(NULL, data));
+#else
+	GC_WB(&s->cfdata, (void *)data);
+#endif
     }
     return s->cfdata;    
 }
@@ -144,8 +148,12 @@
 char *
 rb_str_byteptr(VALUE str)
 {
+#if 1
     return (char *)CFDataGetMutableBytePtr(
 	(CFMutableDataRef)rb_str_cfdata(str));
+#else
+    return (char *)CFDataGetBytePtr((CFDataRef)rb_str_cfdata(str));
+#endif
 }
 
 long
@@ -163,7 +171,7 @@
     datalen = CFDataGetLength(data);
     CFStringRef bytestr = CFStringCreateWithBytesNoCopy(
 	NULL,
-	CFDataGetMutableBytePtr((CFMutableDataRef)data),
+	CFDataGetBytePtr((CFDataRef)data),
 	datalen,
 	kCFStringEncodingUTF8,
 	false,
@@ -455,11 +463,29 @@
 
     str = str_alloc(klass);
 #if WITH_OBJC
+    bool need_padding = len > 0;
     if (ptr != NULL) {
+	long slen = strlen(ptr);
+	if (len > 0 && slen > len && ptr[len] != '\0') {
+	    char *tmp = alloca(len);
+	    memcpy(tmp, ptr, len);
+	    tmp[len] = '\0';
+	    ptr = tmp;
+	}
 	CFStringAppendCString((CFMutableStringRef)str, ptr, 
 	    kCFStringEncodingUTF8);
-	/* TODO ptr might be a bytestring */
+	if (len > 0 && slen < len) {
+	    CFMutableDataRef data;
+	    printf("*** string %p is bytestring (given len %ld, real %ld)\n", 
+		str, len, strlen(ptr), len, slen);
+	    data = CFDataCreateMutable(NULL, 0);
+	    CFDataAppendBytes(data, (const UInt8 *)ptr, len);
+	    GC_WB(&rb_objc_str_get_struct2(str)->cfdata, (void *)data);
+	}
+	need_padding = len > 0 && slen != len;
     }
+    if (need_padding)
+	CFStringPad((CFMutableStringRef)str, CFSTR(" "), len, 0);
 #else
     if (len > RSTRING_EMBED_LEN_MAX) {
 	RSTRING(str)->as.heap.aux.capa = len;
@@ -645,7 +671,7 @@
 static VALUE
 str_new3(VALUE klass, VALUE str)
 {
-    return str_new(klass, RSTRING_CPTR(str), 0);
+    return str_new(klass, RSTRING_PTR(str), 0);
 }
 
 VALUE
@@ -982,7 +1008,7 @@
 static VALUE
 rb_str_bytesize(VALUE str)
 {
-    return INT2NUM(RSTRING_LEN(str));
+    return INT2NUM(rb_str_bytelen(str));
 }
 
 /*
@@ -998,7 +1024,7 @@
 static VALUE
 rb_str_empty(VALUE str)
 {
-    if (RSTRING_LEN(str) == 0)
+    if (CFStringGetLength((CFStringRef)str) == 0)
 	return Qtrue;
     return Qfalse;
 }
@@ -1057,7 +1083,7 @@
     VALUE str2;
     long n, len;
 
-    n = RSTRING_LEN(str);
+    n = RSTRING_CLEN(str);
     len = NUM2LONG(times);
     if (len < 0) {
 	rb_raise(rb_eArgError, "negative argument");
@@ -1066,13 +1092,12 @@
 	rb_raise(rb_eArgError, "argument too big");
     }
 
-    str2 = rb_str_new5(str, 0, len *= n);
 #if WITH_OBJC
-    while (len > 0) {
-	CFStringAppend((CFMutableStringRef)str2, (CFStringRef)str);
-	len--;
-    }
+    str2 = rb_str_new(NULL, 0);
+    CFStringPad((CFMutableStringRef)str2, (CFStringRef)str,
+	len * n, 0);
 #else
+    str2 = rb_str_new5(str, 0, len *= n);
     if (len) {
         memcpy(RSTRING_PTR(str2), RSTRING_PTR(str), n);
         while (n <= len/2) {
@@ -1570,7 +1595,9 @@
 void
 rb_str_set_len(VALUE str, long len)
 {
-#if !WITH_OBJC
+#if WITH_OBJC
+    rb_str_resize(str, len);    
+#else
     STR_SET_LEN(str, len);
     RSTRING_PTR(str)[len] = '\0';
 #endif
@@ -1589,6 +1616,8 @@
 #if WITH_OBJC
     slen = CFStringGetLength((CFStringRef)str);
     if (slen != len) {
+	struct rb_objc_str_struct *s;
+
 	if (len > slen) {
 	    CFStringPad((CFMutableStringRef)str, CFSTR(" "), len, 0);
 	}
@@ -1596,6 +1625,10 @@
 	    CFStringDelete((CFMutableStringRef)str, 
 		CFRangeMake(len, slen - len));
 	}
+
+	s = rb_objc_str_get_struct(str);
+	if (s != NULL && s->cfdata != NULL)
+	    CFDataSetLength((CFMutableDataRef)s->cfdata, len); 
     }
 #else
     slen = RSTRING_LEN(str);
@@ -2494,7 +2527,7 @@
     CFRange r;
     return (CFStringFindWithOptions((CFStringRef)str, 
 		(CFStringRef)sub,
-		CFRangeMake(pos, CFStringGetLength((CFStringRef)str) - pos),
+		CFRangeMake(0, pos),
 		kCFCompareBackwards,
 		&r))
 	? r.location : -1;
@@ -5109,7 +5142,8 @@
     CFStringRef str, void *ctx)
 {
     CFStringDelete((CFMutableStringRef)str, *result_range);
-    search_range->length -= search_range->length;
+    search_range->length -= search_range->location;
+    search_range->location = result_range->location;
     *(bool *)ctx = true;
 }
 #endif
@@ -5646,6 +5680,8 @@
     VALUE rs;
     CFArrayRef ranges;
     long i, count, n, l;
+    UInt8 *buffer;
+    long bufsize = 0;
 
     if (rb_scan_args(argc, argv, "01", &rs) == 0) {
 	rs = rb_rs;
@@ -5666,15 +5702,21 @@
     for (i = l = 0, count = CFArrayGetCount(ranges); i < count; i++) {
 	CFRange *rs_range;
        	CFRange sub_range;
-	UInt8 *buffer;
 
 	rs_range = (CFRange *)CFArrayGetValueAtIndex(ranges, i);
 	assert(rs_range->location > 0);
-	sub_range = CFRangeMake(l, rs_range->location);
-	buffer = (UInt8 *)alloca(sizeof(UInt8) * sub_range.length);
+	sub_range = CFRangeMake(l, rs_range->location - l);
+#if 1
+	rb_yield((VALUE)CFStringCreateWithSubstring(NULL, (CFStringRef)str, sub_range));
+#else
+	if (bufsize < sub_range.length) {
+	    bufsize = sub_range.length + 100;
+	    buffer = (UInt8 *)alloca(sub_range.length + 100);
+	}
 	CFStringGetBytes((CFStringRef)str, sub_range,
 	    kCFStringEncodingUTF8, 0, false, buffer, sub_range.length, NULL);
 	rb_yield(rb_str_new((const char *)buffer, sub_range.length));
+#endif
 	l = rs_range->location + rs_range->length;
     }
     return str;
@@ -6346,13 +6388,35 @@
 }
 
 static VALUE
-scan_once(VALUE str, VALUE pat, long *start)
+scan_once(VALUE str, VALUE pat, long *start, long strlen, bool pat_is_string)
 {
     rb_encoding *enc;
     VALUE result, match;
     struct re_registers *regs;
     long i;
 
+#if WITH_OBJC
+    if (pat_is_string) {
+	/* XXX this is sometimes slower than the regexp search, especially for
+	 * long pattern strings 
+	 */
+	CFRange result_range;
+	if (CFStringFindWithOptions((CFStringRef)str, 
+	    (CFStringRef)pat,
+	    CFRangeMake(*start, strlen - *start),
+	    0,
+	    &result_range)) {
+	    result = (VALUE)CFStringCreateWithSubstring(NULL, 
+		(CFStringRef)str, result_range);
+	    *start = result_range.location + result_range.length + 1;
+	}
+	else {
+	    result = Qnil;
+	}
+	return result;
+    }
+#endif
+
 #if !WITH_OBJC
     enc = STR_ENC_GET(str);
 #endif
@@ -6425,13 +6489,16 @@
     VALUE result;
     long start = 0;
     VALUE match = Qnil;
-    char *p = RSTRING_PTR(str); long len = RSTRING_LEN(str);
-
-    pat = get_pat(pat, 1);
+    long len = CFStringGetLength((CFStringRef)str);
+    bool pat_is_string = TYPE(pat) == T_STRING;
+    
+    if (!pat_is_string)
+	pat = get_pat(pat, 1);
     if (!rb_block_given_p()) {
 	VALUE ary = rb_ary_new();
 
-	while (!NIL_P(result = scan_once(str, pat, &start))) {
+	while (!NIL_P(result = scan_once(str, pat, &start, len, 
+					 pat_is_string))) {
 	    match = rb_backref_get();
 	    rb_ary_push(ary, result);
 	}
@@ -6439,11 +6506,13 @@
 	return ary;
     }
 
-    while (!NIL_P(result = scan_once(str, pat, &start))) {
+    while (!NIL_P(result = scan_once(str, pat, &start, len, pat_is_string))) {
 	match = rb_backref_get();
 	rb_match_busy(match);
 	rb_yield(result);
+#if !WITH_OBJC
 	str_mod_check(str, p, len);
+#endif
 	rb_backref_set(match);	/* restore $~ value */
     }
     rb_backref_set(match);

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macruby-changes/attachments/20080415/385a0542/attachment-0001.html


More information about the macruby-changes mailing list