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

Changeset 2728

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

    r2465 r2728  
    5757        trim (source)                               // trim whitespace 
    5858        strip (source, match)                       // trim elements 
     59        stripl (source, match)                      // trim left elements 
     60        stripr (source, match)                      // trim right elements 
    5961        delimit (src, set)                          // split on delims 
    6062        split (source, pattern)                     // split on pattern 
    6163        splitLines (source);                        // split on lines 
     64        head (source, pattern, tail)                // split to head & tail 
    6265        join (source, postfix, output)              // join text segments 
    6366        repeat (source, count, output)              // repeat source  
     
    8588module tango.text.Util; 
    8689 
    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  
    9590/****************************************************************************** 
    9691 
     
    128123        while (head < tail && *head is match) 
    129124               ++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 
     139T[] 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 
     157T[] stripr(T) (T[] source, T match) 
     158{ 
     159        T*   head = source.ptr, 
     160             tail = head + source.length; 
    130161 
    131162        while (tail > head && *(tail-1) is match) 
     
    335366                 result ~= segment; 
    336367        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 
     381T[] 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; 
    337392} 
    338393 
     
    11541209        assert (strip ("%abc%%%", '%') == "abc"); 
    11551210        assert (strip ("#####", '#') == ""); 
     1211        assert (stripl ("#####", '#') == ""); 
     1212        assert (stripl (" ###", ' ') == "###"); 
     1213        assert (stripl ("#####", 's') == "#####"); 
     1214        assert (stripr ("#####", '#') == ""); 
     1215        assert (stripr ("### ", ' ') == "###"); 
     1216        assert (stripr ("#####", 's') == "#####"); 
    11561217 
    11571218        assert (replace ("abc".dup, 'b', ':') == "a:c"); 
     
    12221283        assert (x.length is 1 && x[0] == "one, two, three"); 
    12231284 
     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 
    12241293        char[][] foo = ["one", "two", "three"]; 
    12251294        auto j = join (foo);