phobos icon indicating copy to clipboard operation
phobos copied to clipboard

add fromHexString

Open Tynukua opened this issue 1 year ago • 16 comments

As I understood Phobos does not contain any functions for converting text strings to byte array

hexString - template(not usable in runtime parse, to - cannot convert into byte array

Tynukua avatar Jul 31 '22 14:07 Tynukua

Thanks for your pull request and interest in making D better, @Tynukua! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#8523"

dlang-bot avatar Aug 01 '22 13:08 dlang-bot

Just a high-level observation: having toDigest return a range would avoid the gc allocation.

I don't get you It already does not contains GC allocation for static arrays

Tynukua avatar Aug 08 '22 11:08 Tynukua

I don't get you

The first overload allocates with the gcc. My point was that toDigestImpl may return a range that lazily computes the bytes when needed. Isn't that possible?

RazvanN7 avatar Aug 08 '22 11:08 RazvanN7

I don't get you

The first overload allocates with the gcc. My point was that toDigestImpl may return a range that lazily computes the bytes when needed. Isn't that possible?

Do you mean I should calculate the count of bytes to allocate more accurately?

Tynukua avatar Aug 08 '22 13:08 Tynukua

No, I mean you could implement a ByteRange that lazily computes the bytes:

struct ByteRange
{
      string hexString;    // stores a reference to the hex string that needs to be digested
      size_t pos;

      bool empty() { return pos == hexString; }
      ubyte front() { /* compute the next byte to be returned */ }
      void popFront() {/* advance `pos` to the next hex number */}

      // maybe add other range primitives as well to make it a more powerful range
}

Then toDigest will return such a range and no gc allocation will be required. I noticed that other digest functions do not make use of ranges, however, I do not know if it is anything preventing them for doing so or people just didn't think about this. What do you think @atilaneves ?

RazvanN7 avatar Aug 09 '22 10:08 RazvanN7

In all likelihood they were written ages ago when allocating was all the rage. I don't think repeating the mistakes of the past makes sense.

atilaneves avatar Aug 09 '22 20:08 atilaneves

@atilaneves Do you think this is a worthwhile addition?

RazvanN7 avatar Sep 26 '22 02:09 RazvanN7

@atilaneves i updated PR now it contains only generator

Tynukua avatar Jan 29 '24 01:01 Tynukua

As I understood Phobos does not contain any functions for converting text strings to byte array

hexString - template(not usable in runtime parse, to - cannot convert into byte array

Why can't they convert to a byte array?

atilaneves avatar Jan 29 '24 12:01 atilaneves

As I understood Phobos does not contain any functions for converting text strings to byte array hexString - template(not usable in runtime parse, to - cannot convert into byte array

Why can't they convert to a byte array?

Actually it's possible by to, but u need chunk your hexstring https://forum.dlang.org/thread/[email protected]

Tynukua avatar Feb 05 '24 07:02 Tynukua

Where does this sit now with compiler support for casting to arrays?

https://dlang.org/changelog/pending.html#dmd.hexstring-cast

I think it was always possible to cast them to a byte array, which gets its range interface from std.array.

ibuclaw avatar Feb 05 '24 08:02 ibuclaw

As I understood Phobos does not contain any functions for converting text strings to byte array hexString - template(not usable in runtime parse, to - cannot convert into byte array

Why can't they convert to a byte array?

Actually it's possible by to, but u need chunk your hexstring https://forum.dlang.org/thread/[email protected]

Why isn't the implementation in this PR the one using chunk in the forum, then?

atilaneves avatar Feb 05 '24 09:02 atilaneves

Where does this sit now with compiler support for casting to arrays?

https://dlang.org/changelog/pending.html#dmd.hexstring-cast

I think it was always possible to cast them to a byte array, which gets its range interface from std.array.

it's compiler time feature sometimes you need proceed with hex string in runtime

Tynukua avatar Feb 06 '24 17:02 Tynukua

Why isn't the implementation in this PR the one using chunk in the forum, then?

I thought @nogc solution will be better, but now i m not sure Have u any suggestions what implementation is better for std?

Tynukua avatar Feb 06 '24 17:02 Tynukua

Why isn't the implementation in this PR the one using chunk in the forum, then?

I thought @nogc solution will be better, but now i m not sure Have u any suggestions what implementation is better for std?

Unless I'm missing something, the only thing that isn't @nogc in the solution in the forum is .array. Leave that out, and there's the implementation. Users can call .array if they want to.

atilaneves avatar Feb 07 '24 10:02 atilaneves

Unless I'm missing something, the only thing that isn't @nogc in the solution in the forum is .array. Leave that out, and there's the implementation. Users can call .array if they want to.

.array part only in example...

Tynukua avatar Feb 08 '24 16:02 Tynukua