-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
interpreter: Add hex/octal/binary support to str and int primitives #15250
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?
Conversation
Add support for parsing and formatting integers in hexadecimal, octal, and binary representations to complement the existing decimal support. str.to_int() now accepts strings with 0x/0o/0b prefixes and optional leading signs (+/-), enabling direct parsing of hex, octal, and binary literals while maintaining full backward compatibility with decimal strings including those with leading zeros. int.to_string() gains a new format keyword argument that accepts 'dec', 'hex', 'oct', or 'bin', allowing integers to be formatted with appropriate prefixes (0x, 0o, 0b). The format parameter works correctly with the existing fill parameter and handles negative numbers properly. Round-trip conversions are fully supported: values formatted with int.to_string(format: 'hex') can be parsed back with str.to_int(). Fixes: mesonbuild#2047 Fixes: mesonbuild#15201
| validator=lambda x: ( | ||
| 'format must be "dec", "hex", "oct", or "bin"' | ||
| if x not in ("dec", "hex", "oct", "bin") | ||
| else None | ||
| ), |
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.
You can use the in_set_validator({...}) for this.
| ) | ||
| @noPosargs | ||
| @InterpreterObject.method('to_string') | ||
| def to_string_method(self, args: T.List[TYPE_var], kwargs: T.Dict[str, T.Any]) -> str: |
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.
While we're here, can you add a TypedDict to use as the type for kwargs here? there's plenty of examples, preferably with a Literal[] for the format.
| if format_type == "dec": | ||
| result = str(self.held_object) | ||
| if fill > 0: | ||
| result = result.zfill(fill) | ||
| else: | ||
| result = format(self.held_object, format_codes[format_type]) | ||
|
|
||
| if fill > 0: | ||
| if result.startswith("-"): | ||
| sign = "-" | ||
| prefixed = result[1:] | ||
| else: | ||
| sign = "" | ||
| prefixed = result | ||
|
|
||
| prefix = prefixed[:2] | ||
| digits = prefixed[2:] | ||
|
|
||
| total_prefix_len = len(sign) + len(prefix) | ||
| if fill > total_prefix_len: | ||
| digits = digits.zfill(fill - total_prefix_len) | ||
|
|
||
| result = sign + prefix + digits |
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.
Can't this all be implemented as:
template = '0=#{}{}'.format(fill, format_codes[format_code))
return format(self.held_object, template)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.
Even easier:
return '{0:#0{fill}{format}}'.format(self.held_object, fill=fill, format=format_codes[format_code])
Add support for parsing and formatting integers in hexadecimal, octal, and binary representations to complement the existing decimal support.
str.to_int() now accepts strings with 0x/0o/0b prefixes and optional leading signs (+/-), enabling direct parsing of hex, octal, and binary literals while maintaining full backward compatibility with decimal strings including those with leading zeros.
int.to_string() gains a new format keyword argument that accepts 'dec', 'hex', 'oct', or 'bin', allowing integers to be formatted with appropriate prefixes (0x, 0o, 0b). The format parameter works correctly with the existing fill parameter and handles negative numbers properly.
Round-trip conversions are fully supported: values formatted with
int.to_string(format: 'hex') can be parsed back with str.to_int().
Fixes: #2047
Fixes: #15201