]> gitweb.factorcode.org Git - factor.git/commitdiff
ascii: adding title case.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 5 Apr 2012 20:56:20 +0000 (13:56 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 5 Apr 2012 20:56:20 +0000 (13:56 -0700)
basis/ascii/ascii-docs.factor
basis/ascii/ascii-tests.factor
basis/ascii/ascii.factor

index 54e66fea8a485d1f371f61590fee48e2fb65a5e9..faec03b06b6621b24a777c8862c2b0ffba039410 100644 (file)
@@ -57,6 +57,14 @@ HELP: >upper
 { $values { "str" "a string" } { "upper" "a string" } }\r
 { $description "Converts an ASCII string to upper case." } ;\r
 \r
+HELP: >title\r
+{ $values { "string" "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 strings" } }\r
+{ $description "Divides the string up into words." } ;\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 +85,7 @@ $nl
     ch>upper\r
     >lower\r
     >upper\r
+    >title\r
 } ;\r
 \r
 ABOUT: "ascii"\r
index 8551ba53efc7c6dc715b6f4f20c1ff1e2221774b..d46388d2d39034eafb46cde83a4c04bd57869a6a 100644 (file)
@@ -1,4 +1,5 @@
-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 +18,5 @@ 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..4070944ceb1072eca50068b0c3d90d00665cce46 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.monotonic strings ;\r
 IN: ascii\r
 \r
 : ascii? ( ch -- ? ) 0 127 between? ; inline\r
@@ -18,6 +18,10 @@ 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? ] bi@ = ] slice monotonic-slice ;\r
+: capitalize ( str -- title ) unclip [ >lower ] [ ch>upper ] bi* prefix ;\r
+: >title ( str -- title ) >words [ capitalize ] map concat ;\r
 \r
 HINTS: >lower string ;\r
 HINTS: >upper string ;\r
+\r