lower case only first letter of word

Steven Schveighoffer schveiguy at yahoo.com
Wed Dec 6 01:37:51 UTC 2017


On 12/5/17 2:41 PM, kdevel wrote:
> On Tuesday, 5 December 2017 at 17:25:57 UTC, Steven Schveighoffer wrote:
> [...]
>> 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.
> 
> But one cannot use the return value of lowerCaseFirst as argument for 
> foo(string).

Define foo as:

foo(R)(R r) if (isInputRange!R && isSomeChar!(ElementType!R))

Then it can take any range of char types.

> Only the use as argument to writeln seems to work. Also I 
> had to put
> 
>      import std.range.primitives : front, popFront, empty;
> 
> outside the struct otherwise the compiler complains about missing front, 
> popFront and empty for type string.

Yeah, it wasn't a complete example. These are the extensions to arrays 
that allow them to work as ranges.

-Steve


More information about the Digitalmars-d-learn mailing list