www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Difference between toLower() and asLowerCase() for strings?

reply Jon D <jond noreply.com> writes:
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
Jan 24
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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.
Jan 24
parent Jon D <jond noreply.com> writes:
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!
Jan 24