Difference between toLower() and asLowerCase() for strings?

2016-01-24 Thread Jon D via Digitalmars-d-learn
I'm trying to identify the preferred ways to lower case a string. 
In std.uni there are two functions that return the lower case 
form of a string: toLower() and asLowerCase(). There is also 
toLowerInPlace().


I'm having trouble figuring out what the relationship is between 
these, and when to prefer one over the other. Both take a 
strings, asLowerCase also takes range. Otherwise, I couldn't find 
the differences in the documentation. Implementations are 
apparently different, but not clear what the real difference is.


Are there reasons to prefer one over the other?

--Jon


Re: Difference between toLower() and asLowerCase() for strings?

2016-01-24 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 24 January 2016 at 20:56:20 UTC, Jon D wrote:
I'm trying to identify the preferred ways to lower case a 
string. In std.uni there are two functions that return the 
lower case form of a string: toLower() and asLowerCase(). There 
is also toLowerInPlace().


toLower will allocate a new string, leaving the original 
untouched.


toLowerInPlace will modify the existing string.

asLowerCase will returned the modified data as you iterate over 
it, but will not actually allocate the new string.



toLower is convenient if you need to store a copy of the string 
somewhere or pass it to a function that expects an existing 
string, but allocating the new one means it is the slowest of the 
three.


toLowerInPlace is only usable if your buffer is writable, which 
many strings aren't, and might still allocate once. It is a 
middle ground for use in a relatively rare case, but if you are 
building a string and need to store it somewhere, this is a 
decent choice.


asLowerCase is the only one that will never actually build a new 
string, and thus typically gives best performance, but at the 
cost of a little bit of lesser convenience. If you are just going 
to loop over it though (including passing it to further 
transforming algorithms), use this! Or if you want to manage the 
allocation of the new string yourself, you can use this too.





As a general rule, the asLowerCase (etc.) version should be your 
first go since it is the most efficient. But the others are 
around for convenience in cases where you need a new string built 
anyway.




Re: Difference between toLower() and asLowerCase() for strings?

2016-01-24 Thread Jon D via Digitalmars-d-learn

On Sunday, 24 January 2016 at 21:04:46 UTC, Adam D. Ruppe wrote:

On Sunday, 24 January 2016 at 20:56:20 UTC, Jon D wrote:
I'm trying to identify the preferred ways to lower case a 
string. In std.uni there are two functions that return the 
lower case form of a string: toLower() and asLowerCase(). 
There is also toLowerInPlace().


toLower will allocate a new string, leaving the original 
untouched.


toLowerInPlace will modify the existing string.

asLowerCase will returned the modified data as you iterate over 
it, but will not actually allocate the new string.


[snip...]

As a general rule, the asLowerCase (etc.) version should be 
your first go since it is the most efficient. But the others 
are around for convenience in cases where you need a new string 
built anyway.


Great explanation, thank you!