]> gitweb.factorcode.org Git - factor.git/blob - core/sbufs/sbufs.factor
Builtinn types now use new slot accessors; tuple slot type declaration work in progress
[factor.git] / core / sbufs / sbufs.factor
1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel math strings sequences.private sequences
4 strings growable strings.private ;
5 IN: sbufs
6
7 TUPLE: sbuf
8 { "underlying" string }
9 { "length" array-capacity } ;
10
11 <PRIVATE
12
13 : string>sbuf ( string length -- sbuf )
14     sbuf boa ; inline
15
16 PRIVATE>
17
18 : <sbuf> ( n -- sbuf ) 0 <string> 0 string>sbuf ; inline
19
20 M: sbuf set-nth-unsafe
21     [ >fixnum ] [ >fixnum ] [ underlying>> ] tri* set-string-nth ;
22
23 M: sbuf new-sequence
24     drop [ 0 <string> ] [ >fixnum ] bi string>sbuf ;
25
26 : >sbuf ( seq -- sbuf ) SBUF" " clone-like ; inline
27
28 M: sbuf like
29     drop dup sbuf? [
30         dup string? [ dup length string>sbuf ] [ >sbuf ] if
31     ] unless ;
32
33 M: sbuf new-resizable drop <sbuf> ;
34
35 M: sbuf equal?
36     over sbuf? [ sequence= ] [ 2drop f ] if ;
37
38 M: string new-resizable drop <sbuf> ;
39
40 M: string like
41     drop dup string? [
42         dup sbuf? [
43             dup length over underlying>> length number= [
44                 underlying>> dup reset-string-hashcode
45             ] [
46                 >string
47             ] if
48         ] [
49             >string
50         ] if
51     ] unless ;
52
53 INSTANCE: sbuf growable