 |
Changeset 3385
- Timestamp:
- 03/21/08 02:12:09
(6 months ago)
- Author:
- kris
- Message:
fixes #970 :: foreach (index, line, delim; stream)
Thanks, Daniel919
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r2847 |
r3385 |
|
| 107 | 107 | if (i && content[i-1] is '\r') |
|---|
| 108 | 108 | --slice; |
|---|
| 109 | | set (content.ptr, 0, slice); |
|---|
| | 109 | set (content.ptr, 0, slice, i); |
|---|
| 110 | 110 | return found (i); |
|---|
| 111 | 111 | } |
|---|
| … | … | |
| 115 | 115 | } |
|---|
| 116 | 116 | |
|---|
| | 117 | |
|---|
| | 118 | |
|---|
| | 119 | /******************************************************************************* |
|---|
| | 120 | |
|---|
| | 121 | *******************************************************************************/ |
|---|
| | 122 | |
|---|
| | 123 | debug (LineIterator) |
|---|
| | 124 | { |
|---|
| | 125 | import tango.io.Buffer; |
|---|
| | 126 | import tango.io.Console; |
|---|
| | 127 | |
|---|
| | 128 | void main() |
|---|
| | 129 | { |
|---|
| | 130 | auto lines = new LineIterator!(char)(new Buffer("one\ntwo\r\nthree")); |
|---|
| | 131 | foreach (i, line, delim; lines) |
|---|
| | 132 | Cout (line) (delim); |
|---|
| | 133 | } |
|---|
| | 134 | } |
|---|
| r2809 |
r3385 |
|
| 103 | 103 | foreach (int i, T c; content) |
|---|
| 104 | 104 | if (has (delim, c)) |
|---|
| 105 | | return found (set (content.ptr, 0, i)); |
|---|
| | 105 | return found (set (content.ptr, 0, i, i)); |
|---|
| 106 | 106 | else |
|---|
| 107 | 107 | if (c is '"' || c is '\'') |
|---|
| r2809 |
r3385 |
|
| 91 | 91 | foreach (int i, T c; content) |
|---|
| 92 | 92 | if (c is delim[0]) |
|---|
| 93 | | return found (set (content.ptr, 0, i)); |
|---|
| | 93 | return found (set (content.ptr, 0, i, i)); |
|---|
| 94 | 94 | } |
|---|
| 95 | 95 | else |
|---|
| 96 | 96 | foreach (int i, T c; content) |
|---|
| 97 | 97 | if (has (delim, c)) |
|---|
| 98 | | return found (set (content.ptr, 0, i)); |
|---|
| | 98 | return found (set (content.ptr, 0, i, i)); |
|---|
| 99 | 99 | |
|---|
| 100 | 100 | return notFound; |
|---|
| r3384 |
r3385 |
|
| 55 | 55 | class StreamIterator(T) : InputStream, Buffered |
|---|
| 56 | 56 | { |
|---|
| 57 | | protected T[] slice; |
|---|
| | 57 | protected T[] slice, |
|---|
| | 58 | delim; |
|---|
| 58 | 59 | private IBuffer input; |
|---|
| 59 | 60 | |
|---|
| … | … | |
| 157 | 158 | } |
|---|
| 158 | 159 | |
|---|
| | 160 | /********************************************************************** |
|---|
| | 161 | |
|---|
| | 162 | Iterate over a set of tokens and delimiters, exposing a |
|---|
| | 163 | token count starting at zero |
|---|
| | 164 | |
|---|
| | 165 | **********************************************************************/ |
|---|
| | 166 | |
|---|
| | 167 | int opApply (int delegate(inout int, inout T[], inout T[]) dg) |
|---|
| | 168 | { |
|---|
| | 169 | bool more; |
|---|
| | 170 | int result, |
|---|
| | 171 | tokens; |
|---|
| | 172 | |
|---|
| | 173 | do { |
|---|
| | 174 | delim = null; |
|---|
| | 175 | more = consume; |
|---|
| | 176 | result = dg (tokens, slice, delim); |
|---|
| | 177 | ++tokens; |
|---|
| | 178 | } while (more && !result); |
|---|
| | 179 | return result; |
|---|
| | 180 | } |
|---|
| | 181 | |
|---|
| 159 | 182 | /*********************************************************************** |
|---|
| 160 | 183 | |
|---|
| … | … | |
| 180 | 203 | Cout(line).newline; |
|---|
| 181 | 204 | --- |
|---|
| 182 | | |
|---|
| 183 | | Note that tokens exposed via push() are returned immediately |
|---|
| 184 | | when available, taking priority over the input stream itself |
|---|
| 185 | 205 | |
|---|
| 186 | 206 | ***********************************************************************/ |
|---|
| … | … | |
| 195 | 215 | /*********************************************************************** |
|---|
| 196 | 216 | |
|---|
| 197 | | Set the content of the current slice |
|---|
| | 217 | Set the content of the current slice to the provided |
|---|
| | 218 | start and end points |
|---|
| 198 | 219 | |
|---|
| 199 | 220 | ***********************************************************************/ |
|---|
| … | … | |
| 202 | 223 | { |
|---|
| 203 | 224 | slice = content [start .. end]; |
|---|
| | 225 | return end; |
|---|
| | 226 | } |
|---|
| | 227 | |
|---|
| | 228 | /*********************************************************************** |
|---|
| | 229 | |
|---|
| | 230 | Set the content of the current slice to the provided |
|---|
| | 231 | start and end points, and delimiter to the segment |
|---|
| | 232 | between end & next (inclusive) |
|---|
| | 233 | |
|---|
| | 234 | ***********************************************************************/ |
|---|
| | 235 | |
|---|
| | 236 | protected final uint set (T* content, uint start, uint end, uint next) |
|---|
| | 237 | { |
|---|
| | 238 | slice = content [start .. end]; |
|---|
| | 239 | delim = content [end .. next+1]; |
|---|
| 204 | 240 | return end; |
|---|
| 205 | 241 | } |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic