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 -- ? ) HEX: 7f fixnum<= ; inline
24 : string-nth ( n string -- ch )
25 2dup string-nth-fast dup small-char?
27 [ (aux) alien-unsigned-2 7 fixnum-shift-fast ] dip
31 : ensure-aux ( string -- string )
32 dup aux>> [ dup length 2 * (byte-array) >>aux ] unless ; inline
34 : set-string-nth-slow ( ch n string -- )
35 [ [ HEX: 80 fixnum-bitor ] 2dip set-string-nth-fast ]
38 [ -7 fixnum-shift-fast 1 fixnum-bitxor ] 2dip
39 (aux) set-alien-unsigned-2
42 : set-string-nth ( ch n string -- )
44 [ set-string-nth-fast ] [ set-string-nth-slow ] if ; inline
50 2dup [ hashcode ] bi@ eq?
51 [ sequence= ] [ 2drop f ] if
59 [ ] [ dup rehash-string string-hashcode ] ?if ;
65 [ >fixnum ] dip string-nth ; inline
67 M: string set-nth-unsafe
68 dup reset-string-hashcode
69 [ >fixnum ] [ >fixnum ] [ ] tri* set-string-nth ; inline
72 (clone) [ clone ] change-aux ; inline
74 M: string resize resize-string ; inline
76 : 1string ( ch -- str ) 1 swap <string> ;
78 : >string ( seq -- str ) "" clone-like ;
80 M: string new-sequence drop 0 <string> ; inline
82 INSTANCE: string sequence