1 /*******************************************************************************
2 
3         copyright:      Copyright (c) 2004 Kris Bell. All rights reserved
4 
5         license:        BSD style: $(LICENSE)
6 
7         version:        Initial release: January 2006
8 
9         author:         Kris
10 
11 *******************************************************************************/
12 
13 module tango.io.stream.Delimiters;
14 
15 private import tango.io.stream.Iterator;
16 
17 /*******************************************************************************
18 
19         Iterate across a set of text patterns.
20 
21         Each pattern is exposed to the client as a slice of the original
22         content, where the slice is transient. If you need to retain the
23         exposed content, then you should .dup it appropriately.
24 
25         The content exposed via an iterator is supposed to be entirely
26         read-only. All current iterators abide by this rule, but it is
27         possible a user could mutate the content through a get() slice.
28         To enforce the desired read-only aspect, the code would have to
29         introduce redundant copying or the compiler would have to support
30         read-only arrays.
31 
32         See Lines, Patterns, Quotes.
33 
34 *******************************************************************************/
35 
36 class Delimiters(T) : Iterator!(T)
37 {
38         private const(T)[] delim;
39 
40         /***********************************************************************
41 
42                 Construct an uninitialized iterator. For example:
43                 ---
44                 auto lines = new Lines!(char);
45 
46                 void somefunc (InputStream stream)
47                 {
48                         foreach (line; lines.set(stream))
49                                  Cout (line).newline;
50                 }
51                 ---
52 
53                 Construct a streaming iterator upon a stream:
54                 ---
55                 void somefunc (InputStream stream)
56                 {
57                         foreach (line; new Lines!(char) (stream))
58                                  Cout (line).newline;
59                 }
60                 ---
61 
62                 Construct a streaming iterator upon a conduit:
63                 ---
64                 foreach (line; new Lines!(char) (new File ("myfile")))
65                          Cout (line).newline;
66                 ---
67 
68         ***********************************************************************/
69 
70         this (const(T)[] delim, InputStream stream = null)
71         {
72                 this.delim = delim;
73                 super (stream);
74         }
75 
76         /***********************************************************************
77 
78         ***********************************************************************/
79 
80         protected override size_t scan (const(void)[] data)
81         {
82                 auto content = (cast(const(T)*) data.ptr) [0 .. data.length / T.sizeof];
83 
84                 if (delim.length is 1)
85                    {
86                    foreach (int i, T c; content)
87                             if (c is delim[0])
88                                 return found (set (content.ptr, 0, i, i));
89                    }
90                 else
91                    foreach (int i, T c; content)
92                             if (has (delim, c))
93                                 return found (set (content.ptr, 0, i, i));
94 
95                 return notFound();
96         }
97 }
98 
99 
100 
101 /*******************************************************************************
102 
103 *******************************************************************************/
104 
105 debug(UnitTest)
106 {
107         private import tango.io.device.Array;
108 
109         unittest
110         {
111                 auto p = new Delimiters!(char) (", ", new Array("blah".dup));
112         }
113 }