Le lundi 30 avril 2012 à 14:52 +0200, bioinfornatics a écrit : > Hi, > I would like to know how compute in on pass 3 tokens position in a > sequence. > curently i do: > File f = File( "reader.d", "r" ); > scope(exit) f.close(); > char[1024] buffer; > char[] content = f.rawRead(buffer); > char[sizediff_t] token = ['(', '{', ';']; > auto position = map!( a => content.countUntil( a ) )( [ ['('], ['{'], > [';'] ] ); > > > if i use reduce instead map the build fail >
----------------- CODE ----------------------- import std.stdio; import std.conv; import std.c.process; sizediff_t[string] counter( in char[] sequence, string[] token...) in{ assert(sequence !is null, "Error given sequence is null"); } body{ bool isComputing = true; size_t index = 0; size_t flag = 0; sizediff_t[string] result; foreach( tok; token) result[tok] = -1; while(isComputing){ if( index >= sequence.length ) isComputing = false; else if( flag == token.length ) isComputing = false; else{ foreach( tok; token){ if( sequence.length - index >= tok.length ){ string currentToken = to!string(sequence[index .. index + tok.length]); if( currentToken in result && result[currentToken] == -1 ){ result[currentToken] = index; flag++; } } } index++; } } return result; } void main( string[] args ){ if( args.length == 1 ){ writefln( "Usage %s <token1> <token2> <token3> <token4>...", args[0] ); exit(0); } writeln( counter( "This a cool statement such as D is fun. Use it and ot the D power. Oh yeah! Are you ready? Try it!! Have fun.", args[1..$]) ); } ---------------------------------- END CODE ----------------------- $ ./test This D ! ["D":30, "!":74, "This":0] import std.string; if all token is found befoore reach end sequence it stop and save us from some loop it works too if token do not has same length as shown in example