]> gitweb.factorcode.org Git - factor.git/blob - core/sbufs/sbufs.factor
Fix comments to be ! not #!.
[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 growable kernel math sequences
4 sequences.private strings strings.private ;
5 IN: sbufs
6
7 TUPLE: sbuf
8 { underlying string }
9 { length array-capacity } ;
10
11 : <sbuf> ( n -- sbuf ) 0 <string> 0 sbuf boa ; inline
12
13 M: sbuf set-nth-unsafe
14     [ integer>fixnum ] [ integer>fixnum ] [ underlying>> ] tri*
15     set-string-nth ; inline
16
17 M: sbuf new-sequence
18     drop [ 0 <string> ] [ integer>fixnum ] bi sbuf boa ; inline
19
20 : >sbuf ( seq -- sbuf ) SBUF" " clone-like ; inline
21
22 M: sbuf contract 2drop ; inline
23
24 M: sbuf like
25     drop dup sbuf? [
26         dup string? [ dup length sbuf boa ] [ >sbuf ] if
27     ] unless ; inline
28
29 M: sbuf equal?
30     over sbuf? [ sequence= ] [ 2drop f ] if ;
31
32 M: string new-resizable drop <sbuf> ; inline
33
34 M: sbuf new-resizable drop <sbuf> ; inline
35
36 M: string like
37     ! If we have a string, we're done.
38     ! If we have an sbuf, and it's at full capacity, we're done.
39     ! Otherwise, call resize-string, which is a relatively
40     ! fast primitive.
41     drop dup string? [
42         dup sbuf? [
43             [ length ] [ underlying>> ] bi
44             2dup length eq?
45             [ nip dup reset-string-hashcode ] [ resize-string ] if
46         ] [ >string ] if
47     ] unless ; inline
48
49 INSTANCE: sbuf growable