www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error in trying to use an inout(char)[] with a regex

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
Hello. Please see the following code:

import std.range;
string textAttr(T)(T spec) if (is(ElementType!(T): const(char)[]))
{
    return "";

    // made dummy for illustration; actually this runs a foreach loop on the
    // individual items in spec, analyses them and passes them to
    // appropriate subroutines for processing and returns their output to 
    // the caller
    //
    // made into a separate function taking either a range or array so that
    // it can be passed either the output of splitter or args[1 .. $]
}

string textAttr(const(char)[] spec)
{
    import std.algorithm: splitter;
    return textAttr(splitter(spec));
}

inout(char)[] applyTextAttr(inout(char)[] text)
{
    import std.regex;
    static auto inlineRE = ctRegex!`\$\(ta (.*?)\)`;
    return text.replaceAll!(m => textAttr(m[1]))(inlineRE);
}

void main(string [] args)
{
    import std.stdio;
    if (args.length == 1) return;
    alias ta = textAttr;
    writeln(ta(args[1 .. $]), "text1", ta("u g /w"), "text2", ta("off"));
}

Now upon trying to compile this I'm getting the errors:

$ dmd inout_test.d
/usr/include/dmd/phobos/std/regex/package.d(557): Error: variable 
std.regex.RegexMatch!(inout(char)[], BacktrackingMatcher).RegexMatch._input 
only parameters or stack based variables can be inout
/usr/include/dmd/phobos/std/regex/package.d(374): Error: variable 
std.regex.Captures!(inout(char)[], ulong).Captures._input only parameters or 
stack based variables can be inout
/usr/include/dmd/phobos/std/regex/package.d(419): Error: inout on return 
means inout must be on a parameter as well for  property R()
/usr/include/dmd/phobos/std/regex/package.d(425): Error: inout on return 
means inout must be on a parameter as well for  property R()
/usr/include/dmd/phobos/std/regex/package.d(431): Error: inout on return 
means inout must be on a parameter as well for  property R()
/usr/include/dmd/phobos/std/regex/package.d(438): Error: inout on return 
means inout must be on a parameter as well for  property R()
/usr/include/dmd/phobos/std/regex/package.d(445): Error: inout on return 
means inout must be on a parameter as well for  property R()
/usr/include/dmd/phobos/std/regex/package.d(558): Error: template instance 
std.regex.Captures!(inout(char)[], ulong) error instantiating
/usr/include/dmd/phobos/std/regex/package.d(684):        instantiated from 
here: RegexMatch!(inout(char)[], BacktrackingMatcher)
/usr/include/dmd/phobos/std/regex/package.d(878):        instantiated from 
here: matchMany!(BacktrackingMatcher, StaticRegex!char, inout(char)[])
/usr/include/dmd/phobos/std/regex/package.d(751):        instantiated from 
here: matchAll!(inout(char)[], StaticRegex!char)
/usr/include/dmd/phobos/std/regex/package.d(1210):        instantiated from 
here: replaceAllWith!((m, sink) => sink.put(fun(m)), matchAll, inout(char)
[], StaticRegex!char)
inout_test.d(22):        instantiated from here: replaceAll!((m) => 
textAttr(m[1]), inout(char)[], StaticRegex!char)
/usr/include/dmd/phobos/std/regex/package.d(599): Error: inout on return 
means inout must be on a parameter as well for  property R()
/usr/include/dmd/phobos/std/regex/package.d(605): Error: inout on return 
means inout must be on a parameter as well for  property R()
/usr/include/dmd/phobos/std/regex/package.d(611): Error: inout on return 
means inout must be on a parameter as well for  property R()

I'm totally not clear as to what the error about inout means, and how I can 
fix this. Please advise. Thanks!

-- 
Shriramana Sharma, Penguin #395953
Oct 16 2015
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 10/16/2015 07:06 PM, Shriramana Sharma wrote:

 /usr/include/dmd/phobos/std/regex/package.d(557): Error: variable
 std.regex.RegexMatch!(inout(char)[], 
BacktrackingMatcher).RegexMatch._input
 only parameters or stack based variables can be inout
Reduced: inout(char)[] foo(inout(char)[] text) { import std.regex; static auto inlineRE = ctRegex!`\$\(ta (.*?)\)`; return text.replaceAll!(m => textAttr(m[1]))(inlineRE); } void main() { } This may be an oversight in the regex module that it may not be well-tested with inout data. If others agree, please open a bug report. Meanwhile, can you try the following template which works at least for the reduced code: import std.range; auto foo(S)(S text) if (isSomeString!S) { import std.regex; static auto inlineRE = ctRegex!`\$\(ta (.*?)\)`; return text.replaceAll!(m => textAttr(m[1]))(inlineRE); } void main() { } Ali
Oct 16 2015
parent Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
Ali Çehreli wrote:

 Meanwhile, can you try the following template which works at least for
 the reduced code:
 
 import std.range;
 
 auto foo(S)(S text)
 if (isSomeString!S) {
 import std.regex;
 static auto inlineRE = ctRegex!`\$\(ta (.*?)\)`;
 return text.replaceAll!(m => textAttr(m[1]))(inlineRE);
 }
Well I didn't think isSomeString was appropriate since it also allows wstring-s and dstring-s which I'm not sure my other functions with work well with, but the following worked nicely for me: thank you! auto applyTextAttr(T)(T text) if (is(T: const(char)[])) { import std.regex; static auto inlineRE = ctRegex!`\$\(ta (.*?)\)`; return text.replaceAll!(m => textAttr(m[1]))(inlineRE); } -- Shriramana Sharma, Penguin #395953
Oct 16 2015