-
-
Notifications
You must be signed in to change notification settings - Fork 16
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 semicolon and hex notation support to the MOS VDU command #118
base: main
Are you sure you want to change the base?
Conversation
Conscious decision to not support comma (rather than whitespace) delimited values, as this would increase code complexity and depth exponentially given the potential for whitespace, comma and semicolon to all act as a delimiter.
src/mos.c
Outdated
value_str += 2; | ||
len -= 2; |
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 think these 2 lines should be removed and let strtol() call below to deal with this prefix. Otherwise, the parsing code will allow input like 0x0xFE to be parsed as a valid number.
Also, the independent checks for prefix/suffix should probably be converted to sequence of if / elseif. The idea is to accept as valid only numbers with at most one prefix or suffix, not any combination of all possible prefixes/suffixes.
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.
Agreed on those two lines, since strtol can handle 0x prefixes there's no need.
We discussed the second point and it costs us nothing to deal with situations where a user may double up on notation.
// Check for '0x' or '0X' prefix | ||
if (len > 2 && (value_str[0] == '0' && tolower(value_str[1] == 'x'))) { | ||
base = 16; | ||
} | ||
|
||
// Check for '&' prefix | ||
if (value_str[0] == '&') { | ||
base = 16; | ||
value_str++; | ||
len--; | ||
} | ||
// Check for 'h' suffix | ||
if (len > 0 && tolower(value_str[len - 1]) == 'h') { | ||
value_str[len - 1] = '\0'; | ||
base = 16; | ||
} |
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.
this is cool - belt & braces style
I had a wild thought on hex handling... it might be nice for hex values to automatically be variable/arbitrary length. so 0x1
or 0x12
would send a single byte, 0x123
or 0x1234
two bytes, 0x12345
or 0x123456
three bytes, and so on
you could just interpret the string one byte at a time. the only tricky part really is dealing with odd length strings, where the first character is a stand-alone nibble rather than a full byte character pair.
Conscious decision to not support comma (rather than whitespace) delimited values, as this would increase code complexity and depth exponentially given the potential for whitespace, comma and semicolon to all act as a delimiter.