 |
Changeset 2728
- Timestamp:
- 10/27/07 22:30:47
(1 year ago)
- Author:
- kris
- Message:
added functions stripl, stripr, and head
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r2465 |
r2728 |
|
| 57 | 57 | trim (source) // trim whitespace |
|---|
| 58 | 58 | strip (source, match) // trim elements |
|---|
| | 59 | stripl (source, match) // trim left elements |
|---|
| | 60 | stripr (source, match) // trim right elements |
|---|
| 59 | 61 | delimit (src, set) // split on delims |
|---|
| 60 | 62 | split (source, pattern) // split on pattern |
|---|
| 61 | 63 | splitLines (source); // split on lines |
|---|
| | 64 | head (source, pattern, tail) // split to head & tail |
|---|
| 62 | 65 | join (source, postfix, output) // join text segments |
|---|
| 63 | 66 | repeat (source, count, output) // repeat source |
|---|
| … | … | |
| 85 | 88 | module tango.text.Util; |
|---|
| 86 | 89 | |
|---|
| 87 | | /// use split() instead |
|---|
| 88 | | deprecated T[][] demarcate (T) (T[] src, T[] pattern) |
|---|
| 89 | | {return split!(T) (src, pattern);} |
|---|
| 90 | | |
|---|
| 91 | | deprecated T[][] delineate (T) (T[] src) |
|---|
| 92 | | {return splitLines!(T) (src);} |
|---|
| 93 | | |
|---|
| 94 | | |
|---|
| 95 | 90 | /****************************************************************************** |
|---|
| 96 | 91 | |
|---|
| … | … | |
| 128 | 123 | while (head < tail && *head is match) |
|---|
| 129 | 124 | ++head; |
|---|
| | 125 | |
|---|
| | 126 | while (tail > head && *(tail-1) is match) |
|---|
| | 127 | --tail; |
|---|
| | 128 | |
|---|
| | 129 | return head [0 .. tail - head]; |
|---|
| | 130 | } |
|---|
| | 131 | |
|---|
| | 132 | /****************************************************************************** |
|---|
| | 133 | |
|---|
| | 134 | Trim the given array by stripping the provided match from |
|---|
| | 135 | the left hand side. Returns a slice of the original content |
|---|
| | 136 | |
|---|
| | 137 | ******************************************************************************/ |
|---|
| | 138 | |
|---|
| | 139 | T[] stripl(T) (T[] source, T match) |
|---|
| | 140 | { |
|---|
| | 141 | T* head = source.ptr, |
|---|
| | 142 | tail = head + source.length; |
|---|
| | 143 | |
|---|
| | 144 | while (head < tail && *head is match) |
|---|
| | 145 | ++head; |
|---|
| | 146 | |
|---|
| | 147 | return head [0 .. tail - head]; |
|---|
| | 148 | } |
|---|
| | 149 | |
|---|
| | 150 | /****************************************************************************** |
|---|
| | 151 | |
|---|
| | 152 | Trim the given array by stripping the provided match from |
|---|
| | 153 | the right hand side. Returns a slice of the original content |
|---|
| | 154 | |
|---|
| | 155 | ******************************************************************************/ |
|---|
| | 156 | |
|---|
| | 157 | T[] stripr(T) (T[] source, T match) |
|---|
| | 158 | { |
|---|
| | 159 | T* head = source.ptr, |
|---|
| | 160 | tail = head + source.length; |
|---|
| 130 | 161 | |
|---|
| 131 | 162 | while (tail > head && *(tail-1) is match) |
|---|
| … | … | |
| 335 | 366 | result ~= segment; |
|---|
| 336 | 367 | return result; |
|---|
| | 368 | } |
|---|
| | 369 | |
|---|
| | 370 | /****************************************************************************** |
|---|
| | 371 | |
|---|
| | 372 | Split the provided array on the first pattern instance, and |
|---|
| | 373 | return the resultant head and tail. The pattern is excluded |
|---|
| | 374 | from each of the segments. |
|---|
| | 375 | |
|---|
| | 376 | Where a segment is not found, tail will be null and the return |
|---|
| | 377 | value will be the original array. |
|---|
| | 378 | |
|---|
| | 379 | ******************************************************************************/ |
|---|
| | 380 | |
|---|
| | 381 | T[] head(T) (T[] src, T[] pattern, out T[] tail) |
|---|
| | 382 | { |
|---|
| | 383 | T[][] result; |
|---|
| | 384 | |
|---|
| | 385 | foreach (segment; patterns (src, pattern)) |
|---|
| | 386 | { |
|---|
| | 387 | if (segment.length < src.length) |
|---|
| | 388 | tail = src [segment.length+pattern.length .. $]; |
|---|
| | 389 | return segment; |
|---|
| | 390 | } |
|---|
| | 391 | return src; |
|---|
| 337 | 392 | } |
|---|
| 338 | 393 | |
|---|
| … | … | |
| 1154 | 1209 | assert (strip ("%abc%%%", '%') == "abc"); |
|---|
| 1155 | 1210 | assert (strip ("#####", '#') == ""); |
|---|
| | 1211 | assert (stripl ("#####", '#') == ""); |
|---|
| | 1212 | assert (stripl (" ###", ' ') == "###"); |
|---|
| | 1213 | assert (stripl ("#####", 's') == "#####"); |
|---|
| | 1214 | assert (stripr ("#####", '#') == ""); |
|---|
| | 1215 | assert (stripr ("### ", ' ') == "###"); |
|---|
| | 1216 | assert (stripr ("#####", 's') == "#####"); |
|---|
| 1156 | 1217 | |
|---|
| 1157 | 1218 | assert (replace ("abc".dup, 'b', ':') == "a:c"); |
|---|
| … | … | |
| 1222 | 1283 | assert (x.length is 1 && x[0] == "one, two, three"); |
|---|
| 1223 | 1284 | |
|---|
| | 1285 | char[] h, t; |
|---|
| | 1286 | h = head ("one:two:three", ":", t); |
|---|
| | 1287 | assert (h == "one" && t == "two:three"); |
|---|
| | 1288 | h = head ("one:::two:three", ":::", t); |
|---|
| | 1289 | assert (h == "one" && t == "two:three"); |
|---|
| | 1290 | h = head ("one:two:three", "*", t); |
|---|
| | 1291 | assert (h == "one:two:three" && t is null); |
|---|
| | 1292 | |
|---|
| 1224 | 1293 | char[][] foo = ["one", "two", "three"]; |
|---|
| 1225 | 1294 | auto j = join (foo); |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic