From 9ce20403f72e2e79eb3540a3655daad97c235bd5 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Sun, 31 Mar 2013 16:09:37 -0700 Subject: [PATCH] ascii: adding >words, >title, and capitalize. --- basis/ascii/ascii-docs.factor | 13 +++++++++++++ basis/ascii/ascii-tests.factor | 5 ++++- basis/ascii/ascii.factor | 7 +++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/basis/ascii/ascii-docs.factor b/basis/ascii/ascii-docs.factor index 54e66fea8a..ba94b11238 100644 --- a/basis/ascii/ascii-docs.factor +++ b/basis/ascii/ascii-docs.factor @@ -57,6 +57,18 @@ HELP: >upper { $values { "str" "a string" } { "upper" "a string" } } { $description "Converts an ASCII string to upper case." } ; +HELP: >title +{ $values { "str" "a string" } { "title" "a string" } } +{ $description "Converts a string to title case." } ; + +HELP: >words +{ $values { "str" "a string" } { "words" "an array of slices" } } +{ $description "Divides the string up into words." } ; + +HELP: capitalize +{ $values { "str" "a string" } { "str'" "a string" } } +{ $description "Capitalize all the words in a string." } ; + ARTICLE: "ascii" "ASCII" "The " { $vocab-link "ascii" } " vocabulary implements support for the legacy ASCII character set. Most applications should use " { $link "unicode" } " instead." $nl @@ -77,6 +89,7 @@ $nl ch>upper >lower >upper + >title } ; ABOUT: "ascii" diff --git a/basis/ascii/ascii-tests.factor b/basis/ascii/ascii-tests.factor index 8551ba53ef..a833c03f48 100644 --- a/basis/ascii/ascii-tests.factor +++ b/basis/ascii/ascii-tests.factor @@ -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 diff --git a/basis/ascii/ascii.factor b/basis/ascii/ascii.factor index 6f89dea6d9..dd34976600 100644 --- a/basis/ascii/ascii.factor +++ b/basis/ascii/ascii.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2005, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: kernel math math.order sequences strings -combinators.short-circuit hints ; +USING: combinators.short-circuit hints kernel math math.order +sequences splitting strings ; IN: ascii : ascii? ( ch -- ? ) 0 127 between? ; inline @@ -18,6 +18,9 @@ IN: ascii : >lower ( str -- lower ) [ ch>lower ] map ; : ch>upper ( ch -- upper ) dup letter? [ 0x20 - ] when ; inline : >upper ( str -- upper ) [ ch>upper ] map ; +: >words ( str -- words ) [ blank? ] split*-when-slice ; +: capitalize ( str -- str' ) unclip [ >lower ] [ ch>upper ] bi* prefix ; +: >title ( str -- title ) >words [ capitalize ] map concat ; HINTS: >lower string ; HINTS: >upper string ;