lower case only first letter of word
Steven Schveighoffer
schveiguy at yahoo.com
Tue Dec 5 17:25:57 UTC 2017
On 12/5/17 10:00 AM, Mengu wrote:
> On Tuesday, 5 December 2017 at 14:34:57 UTC, Mengu wrote:
>> On Tuesday, 5 December 2017 at 14:01:35 UTC, Marc wrote:
>>> On Tuesday, 5 December 2017 at 13:40:08 UTC, Daniel Kozak wrote:
>>>>>> [...]
>>>
>>> Yes, this is not what I want. I want to convert only the first letter
>>> of the word to lower case and left all the others immutable. similar
>>> to PHP's lcfirst():
>>>
>>> http://php.net/manual/en/function.lcfirst.php
>>
>> this is how i'd do it:
>>
>> string upcaseFirst(string wut) {
>> import std.ascii : toUpper;
>> import std.array : appender;
>>
>> auto s = appender!string;
>> s ~= wut[0].toUpper;
>> s ~= wut[1..$];
>> return s.data;
>> }
>
> however a solution that does not allocate any memory would be a lot better.
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
More information about the Digitalmars-d-learn
mailing list