factor/library/strings.factor

124 lines
3.2 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2005-04-02 02:39:33 -05:00
IN: strings USING: generic kernel kernel-internals lists math
sequences ;
2004-07-16 02:26:21 -04:00
BUILTIN: string 12 [ 1 "string-length" f ] [ 2 hashcode f ] ;
M: string = string= ;
BUILTIN: sbuf 13 ;
M: sbuf = sbuf= ;
2004-12-23 18:26:04 -05:00
UNION: text string integer ;
2005-04-02 02:39:33 -05:00
M: string length string-length ;
M: string nth string-nth ;
2004-07-16 02:26:21 -04:00
: f-or-"" ( obj -- ? )
dup not swap "" = or ;
: string-length< ( str str -- boolean )
2004-07-16 02:26:21 -04:00
#! Compare string lengths.
swap string-length swap string-length < ;
2004-07-16 02:26:21 -04:00
: cat ( [ "a" "b" "c" ] -- "abc" )
! If f appears in the list, it is not appended to the
! string.
80 <sbuf> swap [ [ over sbuf-append ] when* ] each sbuf>string ;
2004-07-16 02:26:21 -04:00
2004-11-25 21:51:47 -05:00
: cat2 ( "a" "b" -- "ab" )
swap
80 <sbuf>
dup >r sbuf-append r>
dup >r sbuf-append r>
sbuf>string ;
2004-11-25 21:51:47 -05:00
2004-07-16 02:26:21 -04:00
: cat3 ( "a" "b" "c" -- "abc" )
[ ] cons cons cons cat ;
: index-of ( string substring -- index )
0 -rot index-of* ;
: string> ( str1 str2 -- ? )
2004-07-16 02:26:21 -04:00
! Returns if the first string lexicographically follows str2
string-compare 0 > ;
2004-07-16 02:26:21 -04:00
: string-head ( index str -- str )
2004-07-16 02:26:21 -04:00
#! Returns a new string, from the beginning of the string
#! until the given index.
0 -rot substring ;
2004-07-16 02:26:21 -04:00
: string-contains? ( substr str -- ? )
2004-07-16 02:26:21 -04:00
swap index-of -1 = not ;
: string-tail ( index str -- str )
2004-07-16 02:26:21 -04:00
#! Returns a new string, from the given index until the end
#! of the string.
[ string-length ] keep substring ;
2004-07-16 02:26:21 -04:00
: string/ ( str index -- str str )
2004-07-16 02:26:21 -04:00
#! Returns 2 strings, that when concatenated yield the
#! original string.
[ swap string-head ] 2keep swap string-tail ;
2004-07-16 02:26:21 -04:00
: string// ( str index -- str str )
2004-07-16 02:26:21 -04:00
#! Returns 2 strings, that when concatenated yield the
#! original string, without the character at the given
#! index.
[ swap string-head ] 2keep 1 + swap string-tail ;
2004-07-16 02:26:21 -04:00
: string-head? ( str begin -- ? )
2dup string-length< [
2004-12-19 03:04:03 -05:00
2drop f
] [
dup string-length rot string-head =
2004-12-19 03:04:03 -05:00
] ifte ;
2004-07-16 02:26:21 -04:00
: ?string-head ( str begin -- str ? )
2dup string-head? [
string-length swap string-tail t
2004-12-19 03:04:03 -05:00
] [
drop f
] ifte ;
2004-07-16 02:26:21 -04:00
: string-tail? ( str end -- ? )
2dup string-length< [
2004-12-19 03:04:03 -05:00
2drop f
] [
dup string-length pick string-length swap - rot string-tail =
2004-12-19 03:04:03 -05:00
] ifte ;
2004-07-16 02:26:21 -04:00
: ?string-tail ( str end -- ? )
2dup string-tail? [
string-length swap [ string-length swap - ] keep string-head t
2004-12-19 03:04:03 -05:00
] [
drop f
] ifte ;
2004-08-30 20:24:19 -04:00
: split1 ( string split -- before after )
2dup index-of dup -1 = [
2drop f
2004-07-16 02:26:21 -04:00
] [
[ swap string-length + over string-tail ] keep
rot string-head swap
2004-07-16 02:26:21 -04:00
] ifte ;
PREDICATE: integer blank " \t\n\r" string-contains? ;
PREDICATE: integer letter CHAR: a CHAR: z between? ;
PREDICATE: integer LETTER CHAR: A CHAR: Z between? ;
PREDICATE: integer digit CHAR: 0 CHAR: 9 between? ;
PREDICATE: integer printable CHAR: \s CHAR: ~ between? ;
: quotable? ( ch -- ? )
#! In a string literal, can this character be used without
#! escaping?
dup printable? swap "\"\\" string-contains? not and ;
: url-quotable? ( ch -- ? )
#! In a URL, can this character be used without
#! URL-encoding?
dup letter?
over LETTER? or
over digit? or
swap "/_?." string-contains? or ;