Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 2953

Show
Ignore:
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
  • trunk/tango/text/Util.d

    r2878 r2953  
    5656        --- 
    5757        trim (source)                               // trim whitespace 
     58        triml (source)                              // trim whitespace 
     59        trimr (source)                              // trim whitespace 
    5860        strip (source, match)                       // trim elements 
    5961        stripl (source, match)                      // trim left elements 
     
    111113/****************************************************************************** 
    112114 
     115        Trim the provided array by stripping whitespace from the left. 
     116        Returns a slice of the original content 
     117 
     118******************************************************************************/ 
     119 
     120T[] 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 
     138T[] 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 
    113151        Trim the given array by stripping the provided match from 
    114152        both ends. Returns a slice of the original content 
     
    146184 
    147185        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 
     195T[] 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 
     211T[] 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; 
    148218} 
    149219 
     
    332402/****************************************************************************** 
    333403 
     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 
     413T[] 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 
     435T[] 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 
    334448        Split the provided array wherever a delimiter-set instance is 
    335449        found, and return the resultant segments. The delimiters are 
     
    366480                 result ~= segment; 
    367481        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; 
    392482} 
    393483 
     
    12891379        assert (h == "one:two:three" && t is null); 
    12901380 
     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 
    12911398        char[][] foo = ["one", "two", "three"]; 
    12921399        auto j = join (foo);