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-05-05 16:51:38 -04:00
|
|
|
IN: strings
|
|
|
|
|
USING: generic kernel kernel-internals lists math sequences ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-04-29 02:37:12 -04:00
|
|
|
! Strings
|
2005-05-14 17:18:45 -04:00
|
|
|
DEFER: string?
|
|
|
|
|
BUILTIN: string 12 string? [ 1 length f ] [ 2 hashcode f ] ;
|
2005-04-29 02:37:12 -04:00
|
|
|
|
2005-05-05 16:51:38 -04:00
|
|
|
M: string =
|
|
|
|
|
over string? [
|
|
|
|
|
over hashcode over hashcode number= [
|
|
|
|
|
string-compare 0 eq?
|
|
|
|
|
] [
|
|
|
|
|
2drop f
|
|
|
|
|
] ifte
|
|
|
|
|
] [
|
|
|
|
|
2drop f
|
|
|
|
|
] ifte ;
|
2004-12-23 18:26:04 -05:00
|
|
|
|
2005-05-05 22:30:58 -04:00
|
|
|
M: string nth ( n str -- ch )
|
|
|
|
|
bounds-check char-slot ;
|
2005-04-02 02:39:33 -05:00
|
|
|
|
2005-05-05 16:51:38 -04:00
|
|
|
GENERIC: >string ( seq -- string )
|
|
|
|
|
|
|
|
|
|
M: string >string ;
|
|
|
|
|
|
2005-04-29 02:37:12 -04:00
|
|
|
: string> ( str1 str2 -- ? )
|
|
|
|
|
! Returns if the first string lexicographically follows str2
|
|
|
|
|
string-compare 0 > ;
|
|
|
|
|
|
|
|
|
|
! Characters
|
2005-05-18 16:26:22 -04:00
|
|
|
PREDICATE: integer blank " \t\n\r" 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-05-18 16:26:22 -04:00
|
|
|
dup printable? swap "\"\\" 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-05-18 16:26:22 -04:00
|
|
|
swap "/_?." contains? or ;
|