factor/library/collections/strings.factor

51 lines
1.3 KiB
Factor
Raw Normal View History

! 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
! Strings
DEFER: string?
BUILTIN: string 12 string? [ 1 length f ] [ 2 hashcode f ] ;
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 ;
: 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? ;
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?
2005-05-18 16:26:22 -04:00
dup printable? swap "\"\\" 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
2005-05-18 16:26:22 -04:00
swap "/_?." contains? or ;