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