]> gitweb.factorcode.org Git - factor.git/blob - core/strings/strings.factor
8ff5a7caf4b63981cd531ce4d796822c4b739cbb
[factor.git] / core / strings / strings.factor
1 ! Copyright (C) 2003, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel math.private sequences kernel.private
4 math sequences.private slots.private byte-arrays
5 alien.accessors ;
6 IN: strings
7
8 <PRIVATE
9
10 : string-hashcode 3 slot ; inline
11
12 : set-string-hashcode 3 set-slot ; inline
13
14 : reset-string-hashcode f swap set-string-hashcode ; inline
15
16 : rehash-string ( str -- )
17     1 over sequence-hashcode swap set-string-hashcode ; inline
18
19 PRIVATE>
20
21 M: string equal?
22     over string? [
23         over hashcode over hashcode number=
24         [ sequence= ] [ 2drop f ] if
25     ] [
26         2drop f
27     ] if ;
28
29 M: string hashcode*
30     nip dup string-hashcode [ ]
31     [ dup rehash-string string-hashcode ] ?if ;
32
33 M: string length
34     length>> ;
35
36 M: string nth-unsafe
37     >r >fixnum r> string-nth ;
38
39 M: string set-nth-unsafe
40     dup reset-string-hashcode
41     >r >fixnum >r >fixnum r> r> set-string-nth ;
42
43 M: string clone
44     (clone) [ clone ] change-aux ;
45
46 M: string resize resize-string ;
47
48 : 1string ( ch -- str ) 1 swap <string> ;
49
50 : >string ( seq -- str ) "" clone-like ;
51
52 M: string new-sequence drop 0 <string> ;
53
54 INSTANCE: string sequence