-
Notifications
You must be signed in to change notification settings - Fork 221
Add user chosable base support to StringToInt() #1588
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
base: master
Are you sure you want to change the base?
Add user chosable base support to StringToInt() #1588
Conversation
peternewman
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if HexStringToInt should be switched to use this too now?
A few minor improvement requests too please.
Added evaluation of expected values.
|
@peternewman Unless there is something more you want me to add can you re-trigger travis? |
As per the other I'd like to get #1589 in so at least some compilation is tested. |
peternewman
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I thought I'd submitted this review, but it seems not.
peternewman
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please can we switch the strtoul/strtoll bits of HexStringToInt to use this new function?
Leave HexStringToInt() intact or replace the calls to it with StringToInt()? Edit: according to a quick grep HexStringToInt() is not used very much (5 usages outside of unit tests and function definitions) so I think just replacing these 5 instances with StringToInt() is the way to go. Still leaves me with the question are they "strict" or not? |
…s, since we already had some it could be we're double checking some stuff.
|
@peternewman I took the liberty to replace HexStringToInt hope you don't mind. Also looking at its' usage it seemed to me that strict mode was most appropriate (though I guess the way it used to work was non-strict, but the data delivered to it should not be longer then the wanted number string) |
I meant just replace the bit of the call within HexStringToInt() that currently calls strtoul/strtoll with your new options to StringToInt(). You've dropped the protection that the check for ABCDEFabcdef0123456789 offered, or is that still covered by the base argument to strtoul/strtoll etc? Also we can't just remove the function entirely, we need to add a wrapper version of it and mark it as deprecated with a date in the Doxygen in case other people are using it via our libraries. Personally I'd be tempted to say that converting a hex string to an int is important enough we could keep the wrapper function which makes it a lot more obvious what it's doing without needing to know what the arguments are. I'm rather surprised we only use it five times, although I think that's because the actual calls are wrapped within a UID/MAC FromString function. |
I actually tried that first but the compiler had a really hard time (ie. failed) to select the right versions of the function and thus failed to compile.
I'm not sure what you are asking here? base = 16 covers hex in all its' permutations as far as I understand, I copied all the HexStringToInt() unit tests and replaced them to be StringToInt() with base = 16 and as far as I can tell it passes them.
Understood but I have no solution for the problem I mentioned above...
So maybe have a single instance of HexStringToInt() wrapping StringToInt() instead of the 6 different versions we used to have? |
|
@peternewman I see what you mean by asking about the "ABCDEFabcdef0123456789" protection, this would be strict = true in StringToInt() which causes a false to be returned if strtoul/strtoll finished to consume the string anywhere before the end of the string (due to a character not being part of the encoding being consumed). |
Was that with one function or six? The different versions are to deal with two things, firstly the max value part, but also how it needs to cast things back and forth, see also various other functions in this file. |
How are you running the tests your end? Via |
Yup as far as I understood the output there are failed checks but they are from a different section, but now that I look at the logs I see that this is not the case 🤦♂️ The following tests fail: It is unclear to me why the first test should fail... 0xf is valid hex so why should the function fail? The second one I need to be more awake to be able to analyze. |
Six.
Yeah but for some reason even though the same 6 overloaded versions of StringToInt() exist using them in the HexStringToInt() function caused errors that the compiler could not select the right version needed 🤷♂️ |
|
OK after looking at this a bit more here are my observations on the 2 test results:
|
|
PrefixedHexStringToInt() is only used twice:
Also excellent candidate for deprecation & replacement with StringToInt() |
|
🤦♂️ When a test fails further tests aren't being run... I'll be pushing an update once I figure out what is failing here... |
|
@peternewman I may be wrong here but I'm pretty sure that the current implementation of StringToInt() has a few mistakes in implementation the most glaring seeming to be that in the "int" output version it is doing a static_cast to unsigned int on the output. ola/common/utils/StringUtils.cpp Line 221 in 90c2691
Or is this on purpose? |
|
After reviewing the original code and my code and also looking at the way things function it seems to me that the error in the -1 tests comes from the fact that strtol and strtoll are storing in and dealing with variables that are much larger then any of the ints we are dealing with so when it gets for instance The solution seems to be that we should check for It seems that strtol will basically read the whole sequence of characters as numbers and only a -/+ as the sign for the number from my tests and also from the documentation. |
|
The reason why in the "old" code HexStringToInt() worked just fine and dandy while using StringToInt() to take over its' job fails is that HexStringToInt() never bothers to check whether the resulting value is within bounds or not, it just returns the result of strtoll() or strtoul() respectively and lets the runtime handle type conversion etc. StringToInt() OTOH is checking whether the resulting value is within bounds or not, then since strtoll() treats the string as a whole as a number and only the minus (-) sign is a sign a negative number provided in an encoding that has a sign bit will always exceed INT*_MAX @peternewman @nomis52 I don't know how to resolve this we seem to have 3x3x2 different wrappers of strtol/strtoul that implement different logic surrounding the function (and some wrappers of the wrappers). |
… in 32b which is within the limit of both output variables.
|
@peternewman @nomis52 I think I resolved most/all of the logic there is one case of behavior that changes as reflected in 7aef267 however I believe the old behavior was wrong if we wanted |
|
Also travis is blowing up on some xenial apt packages... |
Needed for FTDI plugin to create a unique ID for RDM packets based on base36 serial stored in the EEPROM.