]> gitweb.factorcode.org Git - factor.git/commitdiff
ascii: adding >words, >title, and capitalize.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 31 Mar 2013 23:09:37 +0000 (16:09 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 31 Mar 2013 23:09:37 +0000 (16:09 -0700)
basis/ascii/ascii-docs.factor
basis/ascii/ascii-tests.factor
basis/ascii/ascii.factor

index 54e66fea8a485d1f371f61590fee48e2fb65a5e9..ba94b1123897aa8f3bed3b63b23688426a0e2007 100644 (file)
@@ -57,6 +57,18 @@ HELP: >upper
 { $values { "str" "a string" } { "upper" "a string" } }\r
 { $description "Converts an ASCII string to upper case." } ;\r
 \r
+HELP: >title\r
+{ $values { "str" "a string" } { "title" "a string" } }\r
+{ $description "Converts a string to title case." } ;\r
+\r
+HELP: >words\r
+{ $values { "str" "a string" } { "words" "an array of slices" } }\r
+{ $description "Divides the string up into words." } ;\r
+\r
+HELP: capitalize\r
+{ $values { "str" "a string" } { "str'" "a string" } }\r
+{ $description "Capitalize all the words in a string." } ;\r
+\r
 ARTICLE: "ascii" "ASCII"\r
 "The " { $vocab-link "ascii" } " vocabulary implements support for the legacy ASCII character set. Most applications should use " { $link "unicode" } " instead."\r
 $nl\r
@@ -77,6 +89,7 @@ $nl
     ch>upper\r
     >lower\r
     >upper\r
+    >title\r
 } ;\r
 \r
 ABOUT: "ascii"\r
index 8551ba53efc7c6dc715b6f4f20c1ff1e2221774b..a833c03f48e662f9009da99a1dcf137b8c5239bd 100644 (file)
@@ -1,4 +1,4 @@
-USING: ascii tools.test sequences kernel math ;
+USING: ascii tools.test sequences kernel math strings ;
 IN: ascii.tests
 
 [ t ] [ CHAR: a letter? ] unit-test
@@ -17,3 +17,6 @@ IN: ascii.tests
 
 [ "HELLO HOW ARE YOU?" ] [ "hellO hOw arE YOU?" >upper ] unit-test
 [ "i'm good thx bai" ] [ "I'm Good THX bai" >lower ] unit-test
+
+[ "Hello How Are You?" ] [ "hEllo how ARE yOU?" >title ] unit-test
+[ { " " "Hello" "    " "World" } ] [ " Hello    World" >words [ >string ] map ] unit-test
index 6f89dea6d92ce199cb5883d6ad0280bb187dc4d4..dd34976600e436514f3425d07e8b20cffdaea71b 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2005, 2009 Slava Pestov.\r
 ! See http://factorcode.org/license.txt for BSD license.\r
-USING: kernel math math.order sequences strings\r
-combinators.short-circuit hints ;\r
+USING: combinators.short-circuit hints kernel math math.order\r
+sequences splitting strings ;\r
 IN: ascii\r
 \r
 : ascii? ( ch -- ? ) 0 127 between? ; inline\r
@@ -18,6 +18,9 @@ IN: ascii
 : >lower ( str -- lower ) [ ch>lower ] map ;\r
 : ch>upper ( ch -- upper ) dup letter? [ 0x20 - ] when ; inline\r
 : >upper ( str -- upper ) [ ch>upper ] map ;\r
+: >words ( str -- words ) [ blank? ] split*-when-slice ;\r
+: capitalize ( str -- str' ) unclip [ >lower ] [ ch>upper ] bi* prefix ;\r
+: >title ( str -- title ) >words [ capitalize ] map concat ;\r
 \r
 HINTS: >lower string ;\r
 HINTS: >upper string ;\r