1 ! Copyright (C) 2003, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.accessors byte-arrays kernel
4 kernel.private math math.private sequences sequences.private
10 : string-hashcode ( str -- n ) 3 slot ; inline
12 : set-string-hashcode ( n str -- ) 3 set-slot ; inline
14 : reset-string-hashcode ( str -- )
15 f swap set-string-hashcode ; inline
17 : rehash-string ( str -- )
18 1 over sequence-hashcode swap set-string-hashcode ; inline
20 : (aux) ( n string -- byte-array m )
21 aux>> { byte-array } declare swap 1 fixnum-shift-fast ; inline
23 : small-char? ( ch -- ? )
24 dup 0 fixnum>= [ 0x7f fixnum<= ] [ drop f ] if ; inline
26 : string-nth ( n string -- ch )
27 2dup string-nth-fast dup small-char?
29 [ (aux) alien-unsigned-2 7 fixnum-shift-fast ] dip
33 : ensure-aux ( string -- string )
34 dup aux>> [ dup length 2 * (byte-array) >>aux ] unless ; inline
36 : set-string-nth-slow ( ch n string -- )
37 [ [ 0x80 fixnum-bitor ] 2dip set-string-nth-fast ]
40 [ -7 fixnum-shift-fast 1 fixnum-bitxor ] 2dip
41 (aux) set-alien-unsigned-2
44 : set-string-nth ( ch n string -- )
46 [ set-string-nth-fast ] [ set-string-nth-slow ] if ; inline
52 2dup [ hashcode ] bi@ eq?
53 [ sequence= ] [ 2drop f ] if
61 [ ] [ dup rehash-string string-hashcode ] ?if ;
67 [ integer>fixnum ] dip string-nth ; inline
69 M: string set-nth-unsafe
70 dup reset-string-hashcode
71 [ integer>fixnum ] [ integer>fixnum ] [ ] tri* set-string-nth ; inline
74 (clone) [ clone ] change-aux ; inline
76 M: string resize resize-string ; inline
78 : 1string ( ch -- str ) 1 swap <string> ; inline
80 : >string ( seq -- str ) "" clone-like ; inline
82 M: string new-sequence drop 0 <string> ; inline
84 INSTANCE: string sequence