Remove function?

bearophile bearophileHUGS at lycos.com
Wed Dec 4 15:59:17 PST 2013


Chris Cain:

> string stripPar(string S)
> {
> 	while(S[0] == '(' && S[S.length-1] == ')')
> 	{
> 	
> 	      S = S[1 .. $ - 1];
> 	}
>
> 	return S;
> }

For better compatibility with Unicode it's better to use .front 
.back .popFront and .popBack. You can also make it pure and 
@safe, and take any string in input.

Not much tested:


import std.array, std.traits;

C1[] stripPar(C1, C2)(C1[] txt, in C2 open = '(', in C2 close=')')
@safe pure if (isSomeChar!C1 && isSomeChar!C2) {
     while (txt.front == open && txt.back == close) {
         txt.popFront;
         txt.popBack;
     }

     return txt;
}

void main() {
     import std.stdio;

     "[[()()()()]"d.stripPar('[', ']').writeln;
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list