digitalmars.D.learn - Can I convert the Range returned by asUpperCase to ubyte[]?
- Dr.No (13/13) May 01 2018 I'm trying to do an optimization here: a hash function which
- ag0aep6g (2/8) May 01 2018 Yup. Not possible. There's no such thing as a lazy ubyte[].
- Jonathan M Davis (17/30) May 01 2018 If you want a ubyte[], then you'd need to convert the range to char[] so
I'm trying to do an optimization here: a hash function which expect a ubye[] array as argument, would just work if I cast string to ubyte[] but I need to convert it to upper case, so I'd like to do that lazily, so that the byte is converted to its upper case version soon as it's requested. I'm not sure if this possible because I think the function should also work with Range and not ubyte[] to work. So, adding some code example, can I convert the string to upper case then convert it to ubyte[] without memory allocation? something like this: import xxhash; import std.uni : asUpperCase; uint hash = xxhashOf(cast(ubyte[])(word.asUpperCase));
May 01 2018
On 05/01/2018 10:13 PM, Dr.No wrote:I'm trying to do an optimization here: a hash function which expect a ubye[] array as argument, would just work if I cast string to ubyte[] but I need to convert it to upper case, so I'd like to do that lazily, so that the byte is converted to its upper case version soon as it's requested. I'm not sure if this possible because I think the function should also work with Range and not ubyte[] to work.Yup. Not possible. There's no such thing as a lazy ubyte[].
May 01 2018
On Tuesday, May 01, 2018 20:13:41 Dr.No via Digitalmars-d-learn wrote:I'm trying to do an optimization here: a hash function which expect a ubye[] array as argument, would just work if I cast string to ubyte[] but I need to convert it to upper case, so I'd like to do that lazily, so that the byte is converted to its upper case version soon as it's requested. I'm not sure if this possible because I think the function should also work with Range and not ubyte[] to work. So, adding some code example, can I convert the string to upper case then convert it to ubyte[] without memory allocation? something like this: import xxhash; import std.uni : asUpperCase; uint hash = xxhashOf(cast(ubyte[])(word.asUpperCase));If you want a ubyte[], then you'd need to convert the range to char[] so string and then do something like call representation on it to get an array of ubytes. But at that point, using asUpperCase instead of toUpper is pointless. Pretty much the only way that you could avoid allocating to get a ubyte[] would be if you did something like have a static array that you filled and then sliced, which requires knowing ahead of time how much space you need (or at least knowing the upper bound). At that point, you could use asUpperCase and then put each character into the static array, and then slice the static array. But that's a royal pain in comparison to just allocating a dynamic array. You'd do better to make it so that your hashing code operates on a range rather than requiring an array of ubytes. Then you wouldn't need to allocate or array or jump through a bunch of hoops to get a dynamic array that's a slice of a static array or something similar. - Jonathan M Davis
May 01 2018