[macruby-changes] [1852] MacRuby/branches/experimental

source_changes at macosforge.org source_changes at macosforge.org
Sat Jun 13 17:26:09 PDT 2009


Revision: 1852
          http://trac.macosforge.org/projects/ruby/changeset/1852
Author:   eloy.de.enige at gmail.com
Date:     2009-06-13 17:26:05 -0700 (Sat, 13 Jun 2009)
Log Message:
-----------
Fixed edge cases in File.join, all examples are now green except for passing a recursive array.

Modified Paths:
--------------
    MacRuby/branches/experimental/file.c
    MacRuby/branches/experimental/spec/frozen/core/file/join_spec.rb
    MacRuby/branches/experimental/spec/frozen/tags/macruby/core/file/join_tags.txt

Modified: MacRuby/branches/experimental/file.c
===================================================================
--- MacRuby/branches/experimental/file.c	2009-06-13 02:32:21 UTC (rev 1851)
+++ MacRuby/branches/experimental/file.c	2009-06-14 00:26:05 UTC (rev 1852)
@@ -2634,32 +2634,45 @@
 static VALUE
 rb_file_join(VALUE ary, VALUE sep)
 {
+    CFMutableStringRef res = CFStringCreateMutable(NULL, 0);
+    CFStringRef sep_cf = (CFStringRef)sep;
     long count;
 
-    CFMutableStringRef mstr = CFStringCreateMutable(NULL, 0);
-
     count = RARRAY_LEN(ary);
     if (count > 0) {
-	long i;
-	for (i = 0; i < count; i++) {
-	    VALUE tmp = RARRAY_AT(ary, i);
-	    switch (TYPE(tmp)) {
-		case T_STRING:
-		    break;
-		case T_ARRAY:
-		    tmp = rb_file_join(tmp, sep);
-		    break;
-		default:
-		    FilePathStringValue(tmp);
-	    }
-	    if (i > 0)
-		CFStringAppend(mstr, (CFStringRef)sep);
-	    CFStringAppend(mstr, (CFStringRef)tmp);
-	}
+      long i;
+      for (i = 0; i < count; i++) {
+        VALUE tmp = RARRAY_AT(ary, i);
+        switch (TYPE(tmp)) {
+        case T_STRING:
+          break;
+        case T_ARRAY:
+          tmp = rb_file_join(tmp, sep);
+          break;
+        default:
+          FilePathStringValue(tmp);
+        }
+
+        CFStringRef tmp_cf = (CFStringRef)tmp;
+
+        if (i > 0) {
+          if (CFStringHasSuffix(res, sep_cf)) {
+            if (CFStringHasPrefix(tmp_cf, sep_cf)) {
+              // Remove trailing slash from res if tmp starts with a slash.
+              CFStringDelete(res, CFRangeMake(CFStringGetLength(res) - 1, 1));
+            }
+          }
+          else if (!CFStringHasPrefix(tmp_cf, sep_cf)) {
+            CFStringAppend(res, sep_cf);
+          }
+        }
+
+        CFStringAppend(res, tmp_cf);
+      }
     }
-    CFMakeCollectable(mstr);
 
-    return (VALUE)mstr;
+    CFMakeCollectable(res);
+    return (VALUE)res;
 }
 
 /*

Modified: MacRuby/branches/experimental/spec/frozen/core/file/join_spec.rb
===================================================================
--- MacRuby/branches/experimental/spec/frozen/core/file/join_spec.rb	2009-06-13 02:32:21 UTC (rev 1851)
+++ MacRuby/branches/experimental/spec/frozen/core/file/join_spec.rb	2009-06-14 00:26:05 UTC (rev 1852)
@@ -30,7 +30,7 @@
     File.join(["a", ["b", ["c"]]]).should == "a/b/c"
   end
   
-  it "inserts the separator in between empty strings and arrays" do  
+  it "inserts the separator in between empty strings and arrays" do
     File.join("").should == ""
     File.join("", "").should == "/"
     File.join(["", ""]).should == "/"

Modified: MacRuby/branches/experimental/spec/frozen/tags/macruby/core/file/join_tags.txt
===================================================================
--- MacRuby/branches/experimental/spec/frozen/tags/macruby/core/file/join_tags.txt	2009-06-13 02:32:21 UTC (rev 1851)
+++ MacRuby/branches/experimental/spec/frozen/tags/macruby/core/file/join_tags.txt	2009-06-14 00:26:05 UTC (rev 1852)
@@ -1,4 +1 @@
-critical:File.join raises an ArgumentError if passed a recursive array
-fails:File.join handles leading parts edge cases
-fails:File.join handles trailing parts edge cases
-fails:File.join handles middle parts edge cases
+critical:File.join raises an ArgumentError if passed a recursive array
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090613/1da0a95d/attachment.html>


More information about the macruby-changes mailing list