summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-07-29 13:29:20 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-07-29 13:29:20 +0000
commit3608baa9909b4b20a8366fcc48393b7763b4be41 (patch)
tree278ce1063a8ef53b7e774569858a5c3b381d84fd
parent8d228b25795dc2f598a9511ec18769285fd11109 (diff)
* ext/pathname/pathname.c (path_cmp): Pathname#<=> translated
from pathname.rb. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28783 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--ext/pathname/lib/pathname.rb6
-rw-r--r--ext/pathname/pathname.c38
3 files changed, 43 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index e3b79d01d3..9c7eeb1c84 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Thu Jul 29 22:28:35 2010 Tanaka Akira <akr@fsij.org>
+
+ * ext/pathname/pathname.c (path_cmp): Pathname#<=> translated
+ from pathname.rb.
+
Thu Jul 29 06:51:30 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* common.mk (EXT_SRCS): add ext/json/parser/parser.c.
diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb
index 8abbd667e5..0c99b95892 100644
--- a/ext/pathname/lib/pathname.rb
+++ b/ext/pathname/lib/pathname.rb
@@ -208,12 +208,6 @@ class Pathname
# :startdoc:
- # Provides for comparing pathnames, case-sensitively.
- def <=>(other)
- return nil unless Pathname === other
- @path.tr('/', "\0") <=> other.to_s.tr('/', "\0")
- end
-
def hash # :nodoc:
@path.hash
end
diff --git a/ext/pathname/pathname.c b/ext/pathname/pathname.c
index 17256fd81d..888116cfaf 100644
--- a/ext/pathname/pathname.c
+++ b/ext/pathname/pathname.c
@@ -82,6 +82,43 @@ path_eq(VALUE self, VALUE other)
return rb_str_equal(get_strpath(self), get_strpath(other));
}
+/*
+ * Provides for comparing pathnames, case-sensitively.
+ */
+static VALUE
+path_cmp(VALUE self, VALUE other)
+{
+ VALUE s1, s2;
+ char *p1, *p2;
+ char *e1, *e2;
+ if (!rb_obj_is_kind_of(other, rb_cPathname))
+ return Qnil;
+ s1 = get_strpath(self);
+ s2 = get_strpath(other);
+ p1 = RSTRING_PTR(s1);
+ p2 = RSTRING_PTR(s2);
+ e1 = p1 + RSTRING_LEN(s1);
+ e2 = p2 + RSTRING_LEN(s2);
+ while (p1 < e1 && p2 < e2) {
+ int c1, c2;
+ c1 = (unsigned char)*p1++;
+ c2 = (unsigned char)*p2++;
+ if (c1 == '/') c1 = '\0';
+ if (c2 == '/') c2 = '\0';
+ if (c1 != c2) {
+ if (c1 < c2)
+ return INT2FIX(-1);
+ else
+ return INT2FIX(1);
+ }
+ }
+ if (p1 < e1)
+ return INT2FIX(1);
+ if (p2 < e2)
+ return INT2FIX(-1);
+ return INT2FIX(0);
+}
+
void
Init_pathname()
{
@@ -96,4 +133,5 @@ Init_pathname()
rb_define_method(rb_cPathname, "==", path_eq, 1);
rb_define_method(rb_cPathname, "===", path_eq, 1);
rb_define_method(rb_cPathname, "eql?", path_eq, 1);
+ rb_define_method(rb_cPathname, "<=>", path_cmp, 1);
}