www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problems with ctRegex

reply crimaniak <crimaniak gmail.com> writes:
First problem: it doesn't understand enums, it seems to be a bug:

```
enum TopicMask : string
{
	divider = "/",
	oneLevelMask = "+",

	system = "$",
	level = "[^"~divider~oneLevelMask~multiLevelMask~"]*",
	publishMask = "^("~divider~"|"~level~")+$"
}


bool topicIsValid(string topic) pure
{
	import std.regex;
	auto mask = ctRegex!(TopicMask.publishMask);
	return !topic.matchFirst(mask).empty;
}

```

result:

/usr/include/dmd/phobos/std/regex/package.d(376,20): Error: 
template std.array.Appender!(TopicMask).Appender.put cannot 
deduce function from argument types !()(TopicMask), candidates 
are:
/usr/include/dmd/phobos/std/array.d(2983,10):        
std.array.Appender!(TopicMask).Appender.put(U)(U item) if 
(canPutItem!U)
/usr/include/dmd/phobos/std/array.d(3011,10):        
std.array.Appender!(TopicMask).Appender.put(Range)(Range items) 
if (canPutConstRange!Range)
/usr/include/dmd/phobos/std/array.d(3020,10):        
std.array.Appender!(TopicMask).Appender.put(Range)(Range items) 
if (canPutRange!Range)
/usr/include/dmd/phobos/std/regex/package.d(387,15): Error: 
cannot implicitly convert expression `app.data()` of type 
`string` to `TopicMask`
/usr/include/dmd/phobos/std/regex/package.d(422,19): Error: 
template instance std.regex.internal.parser.Parser!(TopicMask, 
CodeGen) does not match template declaration Parser(R, Generator) 
if (isForwardRange!R && is(ElementType!R : dchar))
/usr/include/dmd/phobos/std/regex/package.d(393,25): Error: 
template instance std.regex.regexImpl!(TopicMask) error 
instantiating
/usr/include/dmd/phobos/std/regex/package.d(401,17):        
instantiated from here: regex!(TopicMask)
/usr/include/dmd/phobos/std/regex/package.d(431,19):        
instantiated from here: regex!(TopicMask)
/usr/include/dmd/phobos/std/regex/package.d(453,54):        

source/mqttd/topic.d(48,14):        instantiated from here: 



If I change ctRegex!(TopicMask.publishMask) to 


source/mqttd/topic.d(48,26): Error: pure function 
'mqttd.topic.topicIsValid' cannot call impure function 
'std.regex.matchFirst!(string, StaticRegex!char).matchFirst'
source/mqttd/topic.d(48,26): Error: pure function 
'mqttd.topic.topicIsValid' cannot call impure destructor 
'std.regex.Captures!(string, ulong).Captures.~this'


Is there any change to use regex inside or pure function? I just 
need some pure bool test() method to test string against the mask.
Nov 28 2017
parent Basile B. <b2.temp gmx.com> writes:
On Tuesday, 28 November 2017 at 19:12:29 UTC, crimaniak wrote:
 First problem: it doesn't understand enums, it seems to be a 
 bug:
or just "to!string(TopicMask.publishMask)" would have worked too.
 [...]
 Is there any change to use regex inside or pure function? I 
 just need some pure bool test() method to test string against 
 the mask.
I don't think so, the factory system has static mutable data, lazily initialized, so purity seems impossible. If you put "pure" everywhere in the call chain you will land here: https://github.com/dlang/phobos/blob/ad489989ec3fac9f65f4bb9d43d2254a0b718dc7/std/regex/internal/ir.d#L521-L522
Nov 30 2017