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
|
2005-09-10 18:27:31 -04:00
|
|
|
USING: generic kernel kernel-internals lists math sequences
|
|
|
|
sequences-internals ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-12-25 21:05:31 -05:00
|
|
|
M: string hashcode ( string -- n )
|
|
|
|
#! Recompute cached hashcode if necessary.
|
|
|
|
dup string-hashcode [ ] [
|
|
|
|
dup rehash-string string-hashcode
|
|
|
|
] ?if ;
|
|
|
|
|
2005-07-19 04:23:33 -04:00
|
|
|
M: string nth ( n str -- ch ) bounds-check char-slot ;
|
2005-04-02 02:39:33 -05:00
|
|
|
|
2005-09-24 16:34:10 -04:00
|
|
|
M: string nth-unsafe ( n str -- ch ) >r >fixnum r> char-slot ;
|
2005-09-10 18:27:31 -04:00
|
|
|
|
2005-12-25 21:05:31 -05:00
|
|
|
M: string set-nth ( ch n str -- )
|
|
|
|
bounds-check set-nth-unsafe ;
|
|
|
|
|
|
|
|
M: string set-nth-unsafe ( ch n str -- )
|
|
|
|
#! Reset cached hashcode.
|
|
|
|
f over set-string-hashcode
|
|
|
|
>r >fixnum >r >fixnum r> r> set-char-slot ;
|
2005-05-05 16:51:38 -04:00
|
|
|
|
2005-12-25 21:05:31 -05:00
|
|
|
M: string clone ( string -- string ) (clone) ;
|
2005-05-05 16:51:38 -04:00
|
|
|
|
2005-04-29 02:37:12 -04:00
|
|
|
! Characters
|
2005-07-16 22:16:18 -04:00
|
|
|
PREDICATE: integer blank " \t\n\r" member? ;
|
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? ;
|
2005-10-07 20:26:21 -04:00
|
|
|
PREDICATE: integer control "\0\e\r\n\t\u0008\u007f" member? ;
|
2004-08-08 17:20:54 -04:00
|
|
|
|
2005-10-16 21:50:43 -04:00
|
|
|
: ch>lower ( n -- n ) dup LETTER? [ HEX: 20 + ] when ;
|
|
|
|
: ch>upper ( n -- n ) dup letter? [ HEX: 20 - ] when ;
|
|
|
|
: >lower ( str -- str ) [ ch>lower ] map ;
|
|
|
|
: >upper ( str -- str ) [ ch>upper ] map ;
|
|
|
|
|
2004-08-08 17:20:54 -04:00
|
|
|
: quotable? ( ch -- ? )
|
|
|
|
#! In a string literal, can this character be used without
|
|
|
|
#! escaping?
|
2005-08-19 22:22:15 -04:00
|
|
|
dup printable? swap "\"\\" member? not and ; foldable
|
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-08-19 22:22:15 -04:00
|
|
|
swap "/_?." member? or ; foldable
|