ruby-changes:16788
From: akr <ko1@a...>
Date: Thu, 29 Jul 2010 22:29:39 +0900 (JST)
Subject: [ruby-changes:16788] Ruby:r28783 (trunk): * ext/pathname/pathname.c (path_cmp): Pathname#<=> translated
akr 2010-07-29 22:29:20 +0900 (Thu, 29 Jul 2010) New Revision: 28783 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=28783 Log: * ext/pathname/pathname.c (path_cmp): Pathname#<=> translated from pathname.rb. Modified files: trunk/ChangeLog trunk/ext/pathname/lib/pathname.rb trunk/ext/pathname/pathname.c Index: ChangeLog =================================================================== --- ChangeLog (revision 28782) +++ ChangeLog (revision 28783) @@ -1,3 +1,8 @@ +Thu Jul 29 22:28:35 2010 Tanaka Akira <akr@f...> + + * ext/pathname/pathname.c (path_cmp): Pathname#<=> translated + from pathname.rb. + Thu Jul 29 06:51:30 2010 Nobuyoshi Nakada <nobu@r...> * common.mk (EXT_SRCS): add ext/json/parser/parser.c. Index: ext/pathname/lib/pathname.rb =================================================================== --- ext/pathname/lib/pathname.rb (revision 28782) +++ ext/pathname/lib/pathname.rb (revision 28783) @@ -208,12 +208,6 @@ # :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 Index: ext/pathname/pathname.c =================================================================== --- ext/pathname/pathname.c (revision 28782) +++ ext/pathname/pathname.c (revision 28783) @@ -82,6 +82,43 @@ 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 @@ 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); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/