[macruby-changes] [4433] MacRuby/trunk/file.c

source_changes at macosforge.org source_changes at macosforge.org
Wed Aug 18 17:37:47 PDT 2010


Revision: 4433
          http://trac.macosforge.org/projects/ruby/changeset/4433
Author:   watson1978 at gmail.com
Date:     2010-08-18 17:37:46 -0700 (Wed, 18 Aug 2010)
Log Message:
-----------
Fixed a bug in File.extname. (patch imported from 1.9 upstream)

Modified Paths:
--------------
    MacRuby/trunk/file.c

Modified: MacRuby/trunk/file.c
===================================================================
--- MacRuby/trunk/file.c	2010-08-19 00:18:24 UTC (rev 4432)
+++ MacRuby/trunk/file.c	2010-08-19 00:37:46 UTC (rev 4433)
@@ -2734,6 +2734,60 @@
 }
 
 /*
+ * accept a String, and return the pointer of the extension.
+ * if len is passed, set the length of extension to it.
+ * returned pointer is in ``name'' or NULL.
+ *                 returns   *len
+ *   no dot        NULL      0
+ *   dotfile       top       0
+ *   end with dot  dot       1
+ *   .ext          dot       len of .ext
+ *   .ext:stream   dot       len of .ext without :stream (NT only)
+ *
+ */
+const char *
+ruby_find_extname(const char *name, long *len)
+{
+    const char *p, *e;
+
+    p = strrdirsep(name);	/* get the last path component */
+    if (!p) {
+	p = name;
+    }
+    else {
+	do {
+	    name = ++p;
+	} while (isdirsep(*p));
+    }
+
+    e = 0;
+    while (*p && *p == '.') { p++; }
+    while (*p) {
+	if (*p == '.') {
+	    e = p;	  /* get the last dot of the last component */
+	}
+	else if (isdirsep(*p)) {
+	    break;
+	}
+	p = CharNext(p);
+    }
+
+    if (len) {
+	/* no dot, or the only dot is first or end? */
+	if (!e || e == name) {
+	    *len = 0;
+	}
+	else if (e+1 == p) {
+	    *len = 1;
+	}
+	else {
+	    *len = p - e;
+	}
+    }
+    return e;
+}
+
+/*
  *  call-seq:
  *     File.extname(path) -> string
  *  
@@ -2750,29 +2804,17 @@
 static VALUE
 rb_file_s_extname(VALUE klass, SEL sel, VALUE fname)
 {
-    const char *name, *p, *e;
+    const char *name, *e;
+    long len;
     VALUE extname;
 
     FilePathStringValue(fname);
     name = StringValueCStr(fname);
-    p = strrdirsep(name);	/* get the last path component */
-    if (!p)
-	p = name;
-    else
-	name = ++p;
-
-    e = 0;
-    while (*p) {
-	if (*p == '.' || istrailinggabage(*p)) {
-	    e = p;	  /* get the last dot of the last component */
-	}
-	else if (isdirsep(*p))
-	    break;
-	p = CharNext(p);
+    e = ruby_find_extname(name, &len);
+    if (len <= 1) {
+	return rb_str_new(0, 0);
     }
-    if (!e || e == name || e+1 == p)	/* no dot, or the only dot is first or end? */
-	return rb_str_new(0, 0);
-    extname = rb_str_new(e, p - e);	/* keep the dot, too! */
+    extname = rb_str_new(e, len);	/* keep the dot, too! */
     OBJ_INFECT(extname, fname);
     return extname;
 }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100818/2f95f387/attachment.html>


More information about the macruby-changes mailing list