Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add std.conv.hexData() #7211

Closed
wants to merge 1 commit into from
Closed

Conversation

WalterBright
Copy link
Member

This is an alternative to hexString based on usage feedback from Dunnhumby.

  1. does not use templates so it does not have problems with very large strings nor object file bloat
  2. wstring and dstring are not supported, as it is hard to imagine a compelling use case for them
  3. ubyte[] is returned instead of string as it only makes sense for hex data to initialize ubyte arrays, not strings
  4. return value is immutable so it can be used to initialize global tables with need for casting
  5. works with any string - no error cases. Non-hex digits are treated as separators
  6. it is intended to be used in CTFE code as a better replacement for the removed x string core language feature
  7. it has no dependencies on anything else, and so can be cut-pasted into user code that does not rely on Phobos.

It is designed to work with dlang/DIPs#177 although that is not required for hexData() to be useful.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @WalterBright!

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 fetch digger
dub run digger -- build "master + phobos#7211"

std/conv.d Outdated
if (digits)
append(u);

return cast(immutable)result[0 .. i];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The convention is to use assumeUnique

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted this function to not have dependencies on anything but the gc.

Comment on lines +6189 to +6190


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One line space will suffice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use two lines to separate one function from the next unrelated one.

@WalterBright
Copy link
Member Author

The buildkite error is:

generated/linux/release/64/benchmark.o:benchmark/runbench.d:function _D3std5regex8internal9kickstart__T7ShiftOrTaZQl6__ctorMFNcNeKSQCiQChQCe2ir__T5RegexTaZQjAkZSQDmQDlQDiQDc__TQCvTaZQDb: error: undefined reference to '_D3std5range__T12assumeSortedVAyaa6_61203c3d2062TAkZQBlFNaNbNiNfQpZSQCoQCn__T11SortedRangeTQBqVQCna6_61203c3d2062ZQBl'

which has nothing to do with this PR.

@wilzbach
Copy link
Member

wilzbach commented Oct 3, 2019

Yeah the druntime benchmark check uses a wrong version of phobos.

@WalterBright
Copy link
Member Author

@wilzbach so what's the fix?

@thewilsonator
Copy link
Contributor

Nothing, as Buildkite is not required to pass to pull. You should double-check that the benchmark is the only failure though.

std/conv.d Outdated Show resolved Hide resolved
enum a4 = hexData("f");
assert(a4 == [0xF]);
enum a5 = hexData(" f,");
assert(a5 == [0xF]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why support odd number of digits? This is probably going to almost always be an error or typo.

enum a5 = hexData(" f,");
assert(a5 == [0xF]);
enum a6 = hexData("0BF ");
assert(a6 == [0x0B, 0x0F]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should probably be some tests to test the behavior with invalid inputs. Speaking of which, they seem to be ignored? Combined with the decision to support one-digit characters, this seems like a bad idea, e.g. in "54 32 1O" the last character is the letter O.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

G-Z should be an error, that's bug prone.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As currently specified there are no invalid inputs to this function.

Copy link
Member

@andralex andralex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Please mind @CyberShadow 's review

result.length = (s.length + 1) / 2;
size_t i;

void append(ubyte u)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems a bit much, it's called from three places

@RazvanN7
Copy link
Collaborator

RazvanN7 commented Apr 21, 2021

@WalterBright a rebase will most likely fix the buildkite error and then the autotester will be green. However, you still need to address @CyberShadow 's review.

@thewilsonator
Copy link
Contributor

Hex string were added back into the language, there is no need for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants