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 math.private
4 sequences kernel.private math sequences.private slots.private ;
9 : string-hashcode ( str -- n ) 3 slot ; inline
11 : set-string-hashcode ( n str -- ) 3 set-slot ; inline
13 : reset-string-hashcode ( str -- )
14 f swap set-string-hashcode ; inline
16 : rehash-string ( str -- )
17 1 over sequence-hashcode swap set-string-hashcode ; inline
19 : (aux) ( n string -- byte-array m )
20 aux>> { byte-array } declare swap 1 fixnum-shift-fast ; inline
22 : small-char? ( ch -- ? )
23 dup 0 fixnum>= [ 0x7f fixnum<= ] [ drop f ] if ; inline
25 : string-nth ( n string -- ch )
26 2dup string-nth-fast dup small-char?
28 [ (aux) alien-unsigned-2 7 fixnum-shift-fast ] dip
32 : ensure-aux ( string -- string )
33 dup aux>> [ dup length 2 * (byte-array) >>aux ] unless ; inline
35 : set-string-nth-slow ( ch n string -- )
36 [ [ 0x80 fixnum-bitor ] 2dip set-string-nth-fast ]
39 [ -7 fixnum-shift-fast 1 fixnum-bitxor ] 2dip
40 (aux) set-alien-unsigned-2
43 : set-string-nth ( ch n string -- )
45 [ set-string-nth-fast ] [ set-string-nth-slow ] if ; inline
51 2dup [ hashcode ] bi@ eq?
52 [ sequence= ] [ 2drop f ] if
60 [ ] [ dup rehash-string string-hashcode ] ?if ;
66 [ >fixnum ] dip string-nth ; inline
68 M: string set-nth-unsafe
69 dup reset-string-hashcode
70 [ >fixnum ] [ >fixnum ] [ ] tri* set-string-nth ; inline
73 (clone) [ clone ] change-aux ; inline
75 M: string resize resize-string ; inline
77 : 1string ( ch -- str ) 1 swap <string> ; inline
79 : >string ( seq -- str ) "" clone-like ; inline
81 M: string new-sequence drop 0 <string> ; inline
83 INSTANCE: string sequence