factor/core/sbufs/sbufs.factor

50 lines
1.3 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2013-03-05 13:34:47 -05:00
USING: accessors growable kernel math sequences
sequences.private strings strings.private ;
2007-09-20 18:09:08 -04:00
IN: sbufs
TUPLE: sbuf
2008-06-29 22:37:57 -04:00
{ underlying string }
{ length array-capacity } ;
2008-07-01 21:19:03 -04:00
: <sbuf> ( n -- sbuf ) 0 <string> 0 sbuf boa ; inline
2007-09-20 18:09:08 -04:00
M: sbuf set-nth-unsafe
2012-08-02 17:26:39 -04:00
[ integer>fixnum ] [ integer>fixnum ] [ underlying>> ] tri*
set-string-nth ; inline
2007-09-20 18:09:08 -04:00
M: sbuf new-sequence
2012-08-02 17:26:39 -04:00
drop [ 0 <string> ] [ integer>fixnum ] bi sbuf boa ; inline
2007-09-20 18:09:08 -04:00
: >sbuf ( seq -- sbuf ) SBUF" " clone-like ; inline
2012-10-24 23:16:34 -04:00
M: sbuf contract 2drop ; inline
2007-09-20 18:09:08 -04:00
M: sbuf like
drop dup sbuf? [
2008-07-01 21:19:03 -04:00
dup string? [ dup length sbuf boa ] [ >sbuf ] if
] unless ; inline
2007-09-20 18:09:08 -04:00
M: sbuf equal?
over sbuf? [ sequence= ] [ 2drop f ] if ;
M: string new-resizable drop <sbuf> ; inline
2007-09-20 18:09:08 -04:00
M: sbuf new-resizable drop <sbuf> ; inline
2007-09-20 18:09:08 -04:00
M: string like
#! If we have a string, we're done.
#! If we have an sbuf, and it's at full capacity, we're done.
#! Otherwise, call resize-string, which is a relatively
#! fast primitive.
2007-09-20 18:09:08 -04:00
drop dup string? [
dup sbuf? [
[ length ] [ underlying>> ] bi
2dup length eq?
[ nip dup reset-string-hashcode ] [ resize-string ] if
] [ >string ] if
] unless ; inline
2007-09-20 18:09:08 -04:00
INSTANCE: sbuf growable