Is there a standard function that combines take() and popFrontExactly()

realhet real_het at hotmail.com
Fri Dec 11 19:07:23 UTC 2020


Hi,

I've just made this unicode wordreplacer function working, but It 
seems not too nice and functional-ish.
Are there ways to make it more simple?


import std.stdio, std.range, std.algorithm, std.uni, std.utf, 
std.conv;

bool isWordChar(dchar ch){
     return isAlphaNum(ch) || ch=='_';
}

string replaceWords(alias fun = isWordChar)(string str, string 
from, string to){
     auto res = appender!string();
     auto src = str.byCodePoint;
     foreach(isWord, len; str.map!fun.group){
         auto act = src.take(len).text;
         src.popFrontExactly(len);

         res.put(isWord && act==from ? to : act);
     }
	return(res[]);
}

void main(){
     immutable str = "Type=FunctionType Type; éType Type2: Type 
aTypeb Type";
     str.replace("Type", "AsmType").writeln;
     str.replaceWords("Type", "AsmType").writeln;
}

Thanks in advance!


More information about the Digitalmars-d-learn mailing list