www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.regex.replace issues

reply Andrej Mitrovic <none none.none> writes:
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?
Mar 17 2011
next sibling parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
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.
Mar 17 2011
next sibling parent Jesse Phillips <jessekphillips+D gmail.com> writes:
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.
Mar 17 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Okie.
Mar 17 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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?
Mar 17 2011