]> gitweb.factorcode.org Git - factor.git/blob - core/strings/strings.factor
Merge branch 'master' of git://factorcode.org/git/factor
[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 : set-string-nth ( ch n str -- )
20     pick HEX: 7f fixnum<=
21     [ set-string-nth-fast ] [ set-string-nth-slow ] if ; inline
22
23 PRIVATE>
24
25 M: string equal?
26     over string? [
27         over hashcode over hashcode eq?
28         [ sequence= ] [ 2drop f ] if
29     ] [
30         2drop f
31     ] if ;
32
33 M: string hashcode*
34     nip
35     dup string-hashcode
36     [ ] [ dup rehash-string string-hashcode ] ?if ;
37
38 M: string length
39     length>> ;
40
41 M: string nth-unsafe
42     [ >fixnum ] dip string-nth ;
43
44 M: string set-nth-unsafe
45     dup reset-string-hashcode
46     [ >fixnum ] [ >fixnum ] [ ] tri* set-string-nth ;
47
48 M: string clone
49     (clone) [ clone ] change-aux ;
50
51 M: string resize resize-string ;
52
53 : 1string ( ch -- str ) 1 swap <string> ;
54
55 : >string ( seq -- str ) "" clone-like ;
56
57 M: string new-sequence drop 0 <string> ;
58
59 INSTANCE: string sequence