]> gitweb.factorcode.org Git - factor.git/commitdiff
splitting: string-lines is 30-70% faster.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sat, 14 Jul 2012 01:24:45 +0000 (18:24 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sat, 14 Jul 2012 01:24:45 +0000 (18:24 -0700)
core/splitting/splitting.factor

index 58762dd9cebd1eb5218fd6ae87129a0db2e642ca..305e06c46764be9493abad326027a13853f2b544 100644 (file)
@@ -91,15 +91,23 @@ PRIVATE>
 : split*-when ( ... seq quot: ( ... elt -- ... ? ) -- ... pieces )
     [ split*, ] { } make ; inline
 
+<PRIVATE
+
+: crlf? ( str -- ? )
+    [ dup CHAR: \r = [ drop t ] [ CHAR: \n = ] if ] find drop ;
+    inline
+
+PRIVATE>
+
 GENERIC: string-lines ( str -- seq )
 
 M: string string-lines
-    dup "\r\n" intersects? [
-        "\n" split [
-            but-last-slice [
-                "\r" ?tail drop "\r" split
-            ] map
-        ] keep last "\r" split suffix concat
+    dup crlf? [
+        "\n" split
+        [ but-last-slice [ "\r" ?tail drop "\r" split ] map! drop ]
+        [ [ length 1 - ] keep [ "\r" split ] change-nth ]
+        [ concat ]
+        tri
     ] [
         1array
     ] if ;