root/trunk/Serial/StringSourceSink.d

Revision 587, 1.1 kB (checked in by BCS, 3 years ago)

fixed reused reference implementation

Line 
1 module StringSourceSink;
2
3 import Interface;
4
5 class Si : Sink!(char)
6 {
7     char[] str;
8     int at = 0;
9
10     char[] get() { return str[0..at]; }
11
12     this(uint len = 16)
13     {
14         if (len <= at) len = 1;
15         str.length = len;
16     }
17
18     void Dump(char c)
19     {
20         if(at >= str.length) str.length = at * 2;
21         str[at++] = c;
22     }
23
24     void Dump(char[] c)
25     {
26         if(at + c.length >= str.length) str.length = at + c.length + 10;
27         str[at..at+c.length] = c;
28         at += c.length;
29     }
30 }
31
32 class So : Source!(char)
33 {
34     this(char[] s){ str = s.dup; }
35
36     char[] str;
37     uint at = 0;
38    
39     bool Peek(char t) { return at < str.length  && str[at] == t; }
40     char Peek() { return str[at]; }
41
42     bool Pick(ref char t, bool must)
43     {
44         if(at < str.length)
45         {
46             t = str[at];
47             at++;
48             return true;
49         }
50         else if(must) throw new Error("\n"~str[at..$]~"\n"~t~"<<");
51         else return false;
52     }
53
54     bool Pick(char[] ts, bool must)
55     {
56         if(at + ts.length > str.length || ts != str[at..at+ts.length])
57         {
58             if(must) throw new Error("\n"~str[at..$]~"\n"~ts);
59             return false;
60         }
61         at += ts.length;
62         return true;
63     }
64 }
Note: See TracBrowser for help on using the browser.