factor/library/collections/strings.factor

118 lines
3.0 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
2005-04-19 20:28:01 -04:00
BUILTIN: string 12 [ 1 length f ] [ 2 hashcode f ] ;
M: string = string= ;
BUILTIN: sbuf 13 ;
2004-12-23 18:26:04 -05:00
UNION: text string integer ;
2005-04-02 02:39:33 -05:00
M: string nth string-nth ;
2005-04-19 20:28:01 -04:00
: length< ( seq seq -- ? )
#! Compare sequence lengths.
swap length swap length < ;
2004-07-16 02:26:21 -04:00
2004-11-25 21:51:47 -05:00
: cat2 ( "a" "b" -- "ab" )
swap
80 <sbuf>
2005-04-17 21:59:11 -04:00
[ sbuf-append ] keep
[ sbuf-append ] keep
sbuf>string ;
2004-11-25 21:51:47 -05:00
2004-07-16 02:26:21 -04:00
: cat3 ( "a" "b" "c" -- "abc" )
2005-04-17 21:59:11 -04:00
>r >r >r 80 <sbuf>
r> over sbuf-append
r> over sbuf-append
r> over sbuf-append sbuf>string ;
2004-07-16 02:26:21 -04:00
: index-of ( string substring -- index )
0 -rot index-of* ;
2005-04-19 20:28:01 -04:00
: string-contains? ( substr str -- ? )
swap index-of -1 = not ;
: 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-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.
2005-04-19 20:28:01 -04:00
[ 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 -- ? )
2005-04-19 20:28:01 -04:00
2dup length< [
2004-12-19 03:04:03 -05:00
2drop f
] [
2005-04-19 20:28:01 -04:00
dup 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? [
2005-04-19 20:28:01 -04:00
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 -- ? )
2005-04-19 20:28:01 -04:00
2dup length< [
2004-12-19 03:04:03 -05:00
2drop f
] [
2005-04-19 20:28:01 -04:00
dup length pick length swap - rot string-tail =
2004-12-19 03:04:03 -05:00
] ifte ;
2004-07-16 02:26:21 -04:00
2005-04-19 20:28:01 -04:00
: ?string-tail ( str end -- str ? )
2dup string-tail? [
2005-04-19 20:28:01 -04:00
length swap [ 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
] [
2005-04-19 20:28:01 -04:00
[ swap 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 ;
2005-04-19 20:28:01 -04:00
: string-length ( deprecated ) length ;