Can I convert the Range returned by asUpperCase to ubyte[]?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed May 2 04:53:36 UTC 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



More information about the Digitalmars-d-learn mailing list