]> gitweb.factorcode.org Git - factor.git/blob - basis/io/streams/string/string.factor
Solution to Project Euler problem 65
[factor.git] / basis / io / streams / string / string.factor
1 ! Copyright (C) 2003, 2009 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors io kernel math namespaces sequences sbufs
4 strings generic splitting continuations destructors sequences.private
5 io.streams.plain io.encodings math.order growable io.streams.sequence ;
6 IN: io.streams.string
7
8 ! Readers
9 TUPLE: string-reader { underlying string read-only } { i array-capacity } ;
10
11 M: string-reader stream-element-type drop +character+ ;
12 M: string-reader stream-read-partial stream-read ;
13 M: string-reader stream-read sequence-read ;
14 M: string-reader stream-read1 sequence-read1 ;
15 M: string-reader stream-read-until sequence-read-until ;
16 M: string-reader dispose drop ;
17
18 <PRIVATE
19 SINGLETON: null-encoding
20 M: null-encoding decode-char drop stream-read1 ;
21 PRIVATE>
22
23 : <string-reader> ( str -- stream )
24     0 string-reader boa null-encoding <decoder> ;
25
26 : with-string-reader ( str quot -- )
27     [ <string-reader> ] dip with-input-stream ; inline
28
29 ! Writers
30 M: sbuf stream-element-type drop +character+ ;
31
32 : <string-writer> ( -- stream )
33     512 <sbuf> ;
34
35 : with-string-writer ( quot -- str )
36     <string-writer> [
37         swap with-output-stream*
38     ] keep >string ; inline