Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changeset 1545

Show
Ignore:
Timestamp:
05/23/10 11:52:53 (15 years ago)
Author:
rsinfu
Message:

Fixed bugzilla 3880: std.regex functions with const/immutable Regex object.

- Passing const/immutable/shared Regex object to match() and replace() is perfectly safe. Just the template constraints were unaware of the qualifiers.
- Example for splitter() was a bit wrong (compare with the correct one in the unittest around line 2990.)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/docsrc/changelog.dd

    r1544 r1545  
    77 
    88    $(WHATSNEW 
    99    $(LI std.functional: toDelegate now accepts callable(function pointers, delegates and objects implement opCall) ) 
    1010    $(LI std.traits: Added templates to get compile-time information about functions.) 
    1111    $(LI std.typecons: Added tie and AutoImplement.) 
    1212    ) 
    1313    $(BUGSFIXED 
    1414    $(LI $(BUGZILLA 2835): std.socket.TcpSocket doesn't actually connect) 
    1515    $(LI $(BUGZILLA 3200): std.xml doesn't follow spec for Tag.text) 
    1616    $(LI $(BUGZILLA 3873): std.range.repeat should have popBack defined) 
     17    $(LI $(BUGZILLA 3880): std.regex functions with const/immutable Regex object) 
    1718    $(LI $(BUGZILLA 4202): Changset 1517 doesn't compile) 
    1819    ) 
    1920) 
    2021 
    2122<div id=version> 
    2223$(UL 
    2324    $(NEW 047) 
    2425    $(NEW 046) 
    2526    $(NEW 045) 
    2627    $(NEW 044) 
  • trunk/phobos/std/regex.d

    r1495 r1545  
    26992699//------------------------------------------------------------------------------ 
    27002700 
    27012701/** 
    27022702Matches a string against a regular expression. This is the main entry 
    27032703to the module's functionality. A call to $(D match(input, regex)) 
    27042704returns a $(D RegexMatch) object that can be used for direct 
    27052705inspection or for iterating over all matches (if the regular 
    27062706expression was built with the "g" option). 
    27072707 */ 
    27082708RegexMatch!(Range) match(Range, Engine)(Range r, Engine engine) 
    2709 if (is(Engine == Regex!(Unqual!(typeof(Range.init[0]))))) 
     2709if (is(Unqual!Engine == Regex!(Unqual!(typeof(Range.init[0]))))) 
    27102710{ 
    27112711    return typeof(return)(engine, r); 
    27122712} 
    27132713 
    27142714RegexMatch!(Range) match(Range, E)(Range r, E[] engine, string opt = null) 
    27152715//if (is(Engine == Regex!(Unqual!(ElementType!(Range))))) 
    27162716{ 
    27172717    return typeof(return)(regex(engine, opt), r); 
    27182718} 
    27192719 
     
    27812781 
    27822782The replacement format can reference the matches using the $&amp;, $$, 
    27832783$', $`, $0 .. $99 notation: 
    27842784 
    27852785--- 
    27862786assert(replace("noon", regex("^n"), "[$&]") == "[n]oon"); 
    27872787--- 
    27882788 */ 
    27892789 
    27902790Range replace(Range, Engine, String)(Range input, Engine regex, String format) 
    2791 if (is(Engine == Regex!(Unqual!(typeof(Range.init[0]))))) 
     2791if (is(Unqual!Engine == Regex!(Unqual!(typeof(Range.init[0]))))) 
    27922792{ 
    27932793    return RegexMatch!(Range)(regex, input).replaceAll(format); 
    27942794} 
    27952795 
    27962796unittest 
    27972797{ 
    27982798    debug(regex) printf("regex.sub.unittest\n"); 
    27992799    assert(replace("hello", regex("ll"), "ss") == "hesso"); 
    28002800    assert(replace("barat", regex("a"), "A") == "bArat"); 
    28012801    assert(replace("barat", regex("a", "g"), "A") == "bArAt"); 
     
    29002900//------------------------------------------------------------------------------ 
    29012901 
    29022902/** 
    29032903Range that splits another range using a regular expression as a 
    29042904separator. 
    29052905 
    29062906Example: 
    29072907---- 
    29082908auto s1 = ", abc, de,  fg, hi, "; 
    29092909assert(equal(splitter(s1, regex(", *")), 
    2910     ["", "abc", "de", "fg", "hi"][])); 
     2910    ["", "abc", "de", "fg", "hi", ""][])); 
    29112911---- 
    29122912 */ 
    29132913struct Splitter(Range) 
    29142914{ 
    29152915    Range _input; 
    29162916    size_t _offset; 
    29172917    alias Regex!(Unqual!(typeof(Range.init[0]))) Rx; 
    29182918    // Rx _rx; 
    29192919    RegexMatch!(Range) _match; 
    29202920