Make store_into() respect a preceding scan() format - #434
Open
afonsojanu wants to merge 1 commit into
Open
Conversation
store_into() for integral and floating-point types always re-parsed the
raw argument as base-10 (or general float), even when scan() had already
been called on the same argument to configure a different format like
hex. That meant something like
program.add_argument("foo").scan<'x', uint64_t>().store_into(foo);
would throw std::invalid_argument on any hex input, since store_into's
own action tried to read it as decimal.
Now, if an earlier action already produced a value of the right type,
store_into just reuses it. If not (e.g. a side-effect-only custom
action ran first), it falls back to the old behavior of parsing the
raw string itself, so existing usages keep working the same way.
Fixes p-ranav#415.
|
亲,来信已经收到!祝你快乐!
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #415.
store_into()for integral and floating-point types always parses the raw argument itself, hardcoded to base-10 (or a general float format). That's fine on its own, but ifscan()was called first to pick a different format, the two collide:This throws
std::invalid_argument, becausestore_into's own action tries to read"0xABCD"as a decimal number, ignoring thatscan<'x', ...>already parsed it correctly as hex.program.get<uint64_t>()actually returns the right value in this scenario since it just reads the first parsed result, so the mismatch was specific tostore_into.The fix: when another action (like
scan()) already ran beforestore_intoand produced a value of the target type, reuse that value instead of re-parsing the string with a hardcoded format. If there's no such value available (e.g. a side-effect-only custom action ran first, which is exercised by an existing test in this file), it falls back to parsing the raw string itself exactly like before, so nothing about the existing single-action behavior changes.Added two regression tests covering
scan<'x', uint64_t>().store_into(...)andscan<'g', double>().store_into(...). Ran the full suite locally (247 cases, 899 assertions) before and after - all green, including the existing test that mixes a custom action withstore_into, which is what caught the edge case where the earlier action doesn't produce a reusable value.