Thread overview
std.regex.replace issues
Mar 17, 2011
Andrej Mitrovic
Mar 17, 2011
Jesse Phillips
Mar 17, 2011
Andrej Mitrovic
Mar 18, 2011
Jesse Phillips
Mar 18, 2011
Andrej Mitrovic
March 17, 2011
I've tried using this:

    auto file = File("file.cpp", "r");
    char[] replacement;
    foreach (char[] line; file.byLine())
    {
        replacement = line.replace(regex("//.*"), ".");
        // do something with replacement while its still alive..
    }

This gives me this bombshell of an error:
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(2666): Error: cannot implicitly convert expression (result) of type string to char[]
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(2571): Error: template instance std.regex.RegexMatch!(char[]).RegexMatch.replace3!(string) error instantiating
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(1838):        instantiated from here: replace!(string)
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(2854):        instantiated from here: replaceAll!(string)
qttod.d(15):        instantiated from here: replace!(char[],Regex!(char),string)
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(1838): Error: template instance std.regex.RegexMatch!(char[]).RegexMatch.replace!(string) error instantiating
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(2854):        instantiated from here: replaceAll!(string)
qttod.d(15):        instantiated from here: replace!(char[],Regex!(char),string)
>Exit code: 1

I want to compose with `replace`. So I figured using char[]'s would avoid duplication. I want to use code like this:

    char[] replacement;
    foreach (char[] line; file.byLine())
    {
        replacement = line.replace(regex("->"), ".")
                                  .replace(regex("::"), ".")
                                  .replace(regex("//.*"), "")
                                  .replace(regex("\\*"), "");
       // do something with replacement while its still alive..
    }

How would I go about achieving this?
And if I use strings instead isn't this going to involve a lot of duplication?
March 17, 2011
Andrej Mitrovic Wrote:

>     char[] replacement;
>     foreach (char[] line; file.byLine())
>     {
>         replacement = line.replace(regex("->"), ".")
>                                   .replace(regex("::"), ".")
>                                   .replace(regex("//.*"), "")
>                                   .replace(regex("\\*"), "");
>        // do something with replacement while its still alive..
>     }
> 
> How would I go about achieving this?
> And if I use strings instead isn't this going to involve a lot of duplication?

Replace should probably work with char[], but not for efficiency. Unless I am wrong you will still cause a large amount of copying when replacing a single/multiple string with a multiple/single string. You may avoid re-allocation, but even that isn't guarantied.
March 17, 2011
You're right, that point totally slipped my mind. I'm having some ideas about an alternative though. I'll try something out later.

Btw, shouldn't byLine be able to return a dchar[]? For Unicode purposes?
March 18, 2011
Andrej Mitrovic Wrote:

> You're right, that point totally slipped my mind. I'm having some ideas about an alternative though. I'll try something out later.
> 
> Btw, shouldn't byLine be able to return a dchar[]? For Unicode purposes?

Why, if you want to operate on a dchar[] instead of a dchar range you can do the conversion yourself with to!(dstring). byLine should return the data it is iterating over, not some conversion.
March 18, 2011
Okie.