Skip to content

Commit

Permalink
ascii: adding title case.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Apr 5, 2012
1 parent c8e2bd6 commit 66ecd19
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
9 changes: 9 additions & 0 deletions basis/ascii/ascii-docs.factor
Expand Up @@ -57,6 +57,14 @@ HELP: >upper
{ $values { "str" "a string" } { "upper" "a string" } } { $values { "str" "a string" } { "upper" "a string" } }
{ $description "Converts an ASCII string to upper case." } ; { $description "Converts an ASCII string to upper case." } ;


HELP: >title
{ $values { "string" "a string" } { "title" "a string" } }
{ $description "Converts a string to title case." } ;

HELP: >words
{ $values { "str" "a string" } { "words" "an array of strings" } }
{ $description "Divides the string up into words." } ;

ARTICLE: "ascii" "ASCII" ARTICLE: "ascii" "ASCII"
"The " { $vocab-link "ascii" } " vocabulary implements support for the legacy ASCII character set. Most applications should use " { $link "unicode" } " instead." "The " { $vocab-link "ascii" } " vocabulary implements support for the legacy ASCII character set. Most applications should use " { $link "unicode" } " instead."
$nl $nl
Expand All @@ -77,6 +85,7 @@ $nl
ch>upper ch>upper
>lower >lower
>upper >upper
>title
} ; } ;


ABOUT: "ascii" ABOUT: "ascii"
5 changes: 4 additions & 1 deletion basis/ascii/ascii-tests.factor
@@ -1,4 +1,5 @@
USING: ascii tools.test sequences kernel math ; USING: ascii tools.test sequences kernel math strings ;

IN: ascii.tests IN: ascii.tests


[ t ] [ CHAR: a letter? ] unit-test [ t ] [ CHAR: a letter? ] unit-test
Expand All @@ -17,3 +18,5 @@ IN: ascii.tests


[ "HELLO HOW ARE YOU?" ] [ "hellO hOw arE YOU?" >upper ] unit-test [ "HELLO HOW ARE YOU?" ] [ "hellO hOw arE YOU?" >upper ] unit-test
[ "i'm good thx bai" ] [ "I'm Good THX bai" >lower ] 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
8 changes: 6 additions & 2 deletions basis/ascii/ascii.factor
@@ -1,7 +1,7 @@
! Copyright (C) 2005, 2009 Slava Pestov. ! Copyright (C) 2005, 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel math math.order sequences strings USING: combinators.short-circuit hints kernel math math.order
combinators.short-circuit hints ; sequences splitting.monotonic strings ;
IN: ascii IN: ascii


: ascii? ( ch -- ? ) 0 127 between? ; inline : ascii? ( ch -- ? ) 0 127 between? ; inline
Expand All @@ -18,6 +18,10 @@ IN: ascii
: >lower ( str -- lower ) [ ch>lower ] map ; : >lower ( str -- lower ) [ ch>lower ] map ;
: ch>upper ( ch -- upper ) dup letter? [ 0x20 - ] when ; inline : ch>upper ( ch -- upper ) dup letter? [ 0x20 - ] when ; inline
: >upper ( str -- upper ) [ ch>upper ] map ; : >upper ( str -- upper ) [ ch>upper ] map ;
: >words ( str -- words ) [ [ blank? ] bi@ = ] slice monotonic-slice ;
: capitalize ( str -- title ) unclip [ >lower ] [ ch>upper ] bi* prefix ;
: >title ( str -- title ) >words [ capitalize ] map concat ;


HINTS: >lower string ; HINTS: >lower string ;
HINTS: >upper string ; HINTS: >upper string ;

0 comments on commit 66ecd19

Please sign in to comment.