 |
Changeset 2953
- Timestamp:
- 12/02/07 14:49:00
(1 year ago)
- Author:
- kris
- Message:
tango.text.Util now has triml() and trimr() functions, along with chopl() and chopr() functions.
Fixes #709, with thanks to Yidabu
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r2878 |
r2953 |
|
| 56 | 56 | --- |
|---|
| 57 | 57 | trim (source) // trim whitespace |
|---|
| | 58 | triml (source) // trim whitespace |
|---|
| | 59 | trimr (source) // trim whitespace |
|---|
| 58 | 60 | strip (source, match) // trim elements |
|---|
| 59 | 61 | stripl (source, match) // trim left elements |
|---|
| … | … | |
| 111 | 113 | /****************************************************************************** |
|---|
| 112 | 114 | |
|---|
| | 115 | Trim the provided array by stripping whitespace from the left. |
|---|
| | 116 | Returns a slice of the original content |
|---|
| | 117 | |
|---|
| | 118 | ******************************************************************************/ |
|---|
| | 119 | |
|---|
| | 120 | T[] triml(T) (T[] source) |
|---|
| | 121 | { |
|---|
| | 122 | T* head = source.ptr, |
|---|
| | 123 | tail = head + source.length; |
|---|
| | 124 | |
|---|
| | 125 | while (head < tail && isSpace(*head)) |
|---|
| | 126 | ++head; |
|---|
| | 127 | |
|---|
| | 128 | return head [0 .. tail - head]; |
|---|
| | 129 | } |
|---|
| | 130 | |
|---|
| | 131 | /****************************************************************************** |
|---|
| | 132 | |
|---|
| | 133 | Trim the provided array by stripping whitespace from the right. |
|---|
| | 134 | Returns a slice of the original content |
|---|
| | 135 | |
|---|
| | 136 | ******************************************************************************/ |
|---|
| | 137 | |
|---|
| | 138 | T[] trimr(T) (T[] source) |
|---|
| | 139 | { |
|---|
| | 140 | T* head = source.ptr, |
|---|
| | 141 | tail = head + source.length; |
|---|
| | 142 | |
|---|
| | 143 | while (tail > head && isSpace(*(tail-1))) |
|---|
| | 144 | --tail; |
|---|
| | 145 | |
|---|
| | 146 | return head [0 .. tail - head]; |
|---|
| | 147 | } |
|---|
| | 148 | |
|---|
| | 149 | /****************************************************************************** |
|---|
| | 150 | |
|---|
| 113 | 151 | Trim the given array by stripping the provided match from |
|---|
| 114 | 152 | both ends. Returns a slice of the original content |
|---|
| … | … | |
| 146 | 184 | |
|---|
| 147 | 185 | return head [0 .. tail - head]; |
|---|
| | 186 | } |
|---|
| | 187 | |
|---|
| | 188 | /****************************************************************************** |
|---|
| | 189 | |
|---|
| | 190 | Chop the given source by stripping the provided match from |
|---|
| | 191 | the left hand side. Returns a slice of the original content |
|---|
| | 192 | |
|---|
| | 193 | ******************************************************************************/ |
|---|
| | 194 | |
|---|
| | 195 | T[] chopl(T) (T[] source, T[] match) |
|---|
| | 196 | { |
|---|
| | 197 | if (match.length <= source.length) |
|---|
| | 198 | if (source[0 .. match.length] == match) |
|---|
| | 199 | source = source [match.length .. $]; |
|---|
| | 200 | |
|---|
| | 201 | return source; |
|---|
| | 202 | } |
|---|
| | 203 | |
|---|
| | 204 | /****************************************************************************** |
|---|
| | 205 | |
|---|
| | 206 | Chop the given source by stripping the provided match from |
|---|
| | 207 | the right hand side. Returns a slice of the original content |
|---|
| | 208 | |
|---|
| | 209 | ******************************************************************************/ |
|---|
| | 210 | |
|---|
| | 211 | T[] chopr(T) (T[] source, T[] match) |
|---|
| | 212 | { |
|---|
| | 213 | if (match.length <= source.length) |
|---|
| | 214 | if (source[$-match.length .. $] == match) |
|---|
| | 215 | source = source [0 .. $-match.length]; |
|---|
| | 216 | |
|---|
| | 217 | return source; |
|---|
| 148 | 218 | } |
|---|
| 149 | 219 | |
|---|
| … | … | |
| 332 | 402 | /****************************************************************************** |
|---|
| 333 | 403 | |
|---|
| | 404 | Split the provided array on the first pattern instance, and |
|---|
| | 405 | return the resultant head and tail. The pattern is excluded |
|---|
| | 406 | from the two segments. |
|---|
| | 407 | |
|---|
| | 408 | Where a segment is not found, tail will be null and the return |
|---|
| | 409 | value will be the original array. |
|---|
| | 410 | |
|---|
| | 411 | ******************************************************************************/ |
|---|
| | 412 | |
|---|
| | 413 | T[] head(T) (T[] src, T[] pattern, out T[] tail) |
|---|
| | 414 | { |
|---|
| | 415 | auto i = locatePattern (src, pattern); |
|---|
| | 416 | if (i != src.length) |
|---|
| | 417 | { |
|---|
| | 418 | tail = src [i + pattern.length .. $]; |
|---|
| | 419 | src = src [0 .. i]; |
|---|
| | 420 | } |
|---|
| | 421 | return src; |
|---|
| | 422 | } |
|---|
| | 423 | |
|---|
| | 424 | /****************************************************************************** |
|---|
| | 425 | |
|---|
| | 426 | Split the provided array on the last pattern instance, and |
|---|
| | 427 | return the resultant head and tail. The pattern is excluded |
|---|
| | 428 | from the two segments. |
|---|
| | 429 | |
|---|
| | 430 | Where a segment is not found, head will be null and the return |
|---|
| | 431 | value will be the original array. |
|---|
| | 432 | |
|---|
| | 433 | ******************************************************************************/ |
|---|
| | 434 | |
|---|
| | 435 | T[] tail(T) (T[] src, T[] pattern, out T[] head) |
|---|
| | 436 | { |
|---|
| | 437 | auto i = locatePatternPrior (src, pattern); |
|---|
| | 438 | if (i != src.length) |
|---|
| | 439 | { |
|---|
| | 440 | head = src [0 .. i]; |
|---|
| | 441 | src = src [i + pattern.length .. $]; |
|---|
| | 442 | } |
|---|
| | 443 | return src; |
|---|
| | 444 | } |
|---|
| | 445 | |
|---|
| | 446 | /****************************************************************************** |
|---|
| | 447 | |
|---|
| 334 | 448 | Split the provided array wherever a delimiter-set instance is |
|---|
| 335 | 449 | found, and return the resultant segments. The delimiters are |
|---|
| … | … | |
| 366 | 480 | result ~= segment; |
|---|
| 367 | 481 | 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; |
|---|
| 392 | 482 | } |
|---|
| 393 | 483 | |
|---|
| … | … | |
| 1289 | 1379 | assert (h == "one:two:three" && t is null); |
|---|
| 1290 | 1380 | |
|---|
| | 1381 | t = tail ("one:two:three", ":", h); |
|---|
| | 1382 | assert (h == "one:two" && t == "three"); |
|---|
| | 1383 | t = tail ("one:::two:three", ":::", h); |
|---|
| | 1384 | assert (h == "one" && t == "two:three"); |
|---|
| | 1385 | t = tail ("one:two:three", "*", h); |
|---|
| | 1386 | assert (t == "one:two:three" && h is null); |
|---|
| | 1387 | |
|---|
| | 1388 | assert (chopl("hello world", "hello ") == "world"); |
|---|
| | 1389 | assert (chopl("hello", "hello") == ""); |
|---|
| | 1390 | assert (chopl("hello world", " ") == "hello world"); |
|---|
| | 1391 | assert (chopl("hello world", "") == "hello world"); |
|---|
| | 1392 | |
|---|
| | 1393 | assert (chopr("hello world", " world") == "hello"); |
|---|
| | 1394 | assert (chopr("hello", "hello") == ""); |
|---|
| | 1395 | assert (chopr("hello world", " ") == "hello world"); |
|---|
| | 1396 | assert (chopr("hello world", "") == "hello world"); |
|---|
| | 1397 | |
|---|
| 1291 | 1398 | char[][] foo = ["one", "two", "three"]; |
|---|
| 1292 | 1399 | auto j = join (foo); |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic