lower case only first letter of word

Ali Çehreli acehreli at yahoo.com
Tue Dec 5 19:34:16 UTC 2017


On 12/05/2017 09:25 AM, Steven Schveighoffer wrote:

> Non-allocating version:
> 
> struct LowerCaseFirst(R) // if(isSomeString!R)
> {
>     R src;
>     bool notFirst; // terrible name, but I want default false
>     dchar front() {
>        import std.uni: toLower;
>        return notFirst ? src.front : src.front.toLower;
>     }
>     void popFront() { notFirst = true; src.popFront; }
>     bool empty() { return src.empty; }
> }
> 
> auto lowerCaseFirst(R)(R r)
> {
>     return LowerCaseFirst!R(r);
> }
> 
> Warning: it ain't going to be fast. Auto-decoding everywhere.
> 
> -Steve

One using existing facilities:

import std.range;
import std.uni;
import std.algorithm;

auto lowerCaseFirst(R)(R r) {
     R rest = r.save;
     rest.popFront();
     return chain(r.front.toLower.only, rest);
}

unittest {
     assert(lowerCaseFirst("SchveiGoffer").equal("schveiGoffer"));
     assert(lowerCaseFirst("ŞchveıĞöffer").equal("şchveıĞöffer"));
}

void main() {
}

Ali


More information about the Digitalmars-d-learn mailing list