2005-01-29 14:18:28 -05:00
|
|
|
! 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 ] ;
|
2005-03-05 16:33:40 -05:00
|
|
|
M: string = string= ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2005-02-20 19:03:37 -05:00
|
|
|
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
|
2005-03-05 16:33:40 -05:00
|
|
|
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 ;
|
|
|
|
|
2005-03-05 16:33:40 -05:00
|
|
|
: string> ( str1 str2 -- ? )
|
2004-07-16 02:26:21 -04:00
|
|
|
! Returns if the first string lexicographically follows str2
|
2005-03-05 16:33:40 -05:00
|
|
|
string-compare 0 > ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-03-05 16:33:40 -05: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.
|
2004-11-15 12:33:21 -05:00
|
|
|
0 -rot substring ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-03-05 16:33:40 -05: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
|
|
|
|
2005-03-05 16:33:40 -05:00
|
|
|
: string/ ( str index -- str str )
|
2004-07-16 02:26:21 -04:00
|
|
|
#! Returns 2 strings, that when concatenated yield the
|
|
|
|
#! original string.
|
2005-03-05 16:33:40 -05:00
|
|
|
[ swap string-head ] 2keep swap string-tail ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-03-05 16:33:40 -05: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.
|
2005-03-05 16:33:40 -05:00
|
|
|
[ swap string-head ] 2keep 1 + swap string-tail ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-03-05 16:33:40 -05: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
|
|
|
|
2005-03-05 16:33:40 -05: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
|
|
|
|
2005-03-05 16:33:40 -05: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 ? )
|
2005-03-05 16:33:40 -05:00
|
|
|
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
|
|
|
|
2004-08-10 21:32:10 -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
|
2005-03-05 16:33:40 -05:00
|
|
|
rot string-head swap
|
2004-07-16 02:26:21 -04:00
|
|
|
] ifte ;
|
|
|
|
|
2005-03-05 16:33:40 -05:00
|
|
|
PREDICATE: integer blank " \t\n\r" string-contains? ;
|
2004-12-18 23:18:32 -05:00
|
|
|
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? ;
|
2004-08-08 17:20:54 -04:00
|
|
|
|
|
|
|
: quotable? ( ch -- ? )
|
|
|
|
#! In a string literal, can this character be used without
|
|
|
|
#! escaping?
|
2005-03-05 16:33:40 -05:00
|
|
|
dup printable? swap "\"\\" string-contains? not and ;
|
2004-08-08 17:20:54 -04:00
|
|
|
|
|
|
|
: url-quotable? ( ch -- ? )
|
|
|
|
#! In a URL, can this character be used without
|
|
|
|
#! URL-encoding?
|
2004-12-11 18:18:43 -05:00
|
|
|
dup letter?
|
|
|
|
over LETTER? or
|
|
|
|
over digit? or
|
2005-03-05 16:33:40 -05:00
|
|
|
swap "/_?." string-contains? or ;
|
2005-04-19 20:28:01 -04:00
|
|
|
|
|
|
|
: string-length ( deprecated ) length ;
|