-
-
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
ce586d4 to
cf4e844
Compare
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
cf4e844 to
d50cfec
Compare
| return str(self.held_object).zfill(kwargs['fill']) | ||
| def to_string_method(self, args: T.List[TYPE_var], kwargs: 'ToStringKw') -> str: | ||
| format_codes = {"hex": "x", "oct": "o", "bin": "b", "dec": "d"} | ||
| return format(self.held_object, f'#0{kwargs["fill"]}{format_codes[kwargs["format"]]}') |
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.
| return format(self.held_object, f'#0{kwargs["fill"]}{format_codes[kwargs["format"]]}') | |
| return '{:#0{fill}{format}}'.format(self.held_object, fill=kwargs['fill'], format=format_codes[kwargs['format']]) |
| class ToStringKw(TypedDict): | ||
|
|
||
| fill: int | ||
| format: Literal["dec", "hex", "oct", "bin"] |
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 the quoting style in the meson code base is to use single quotes.
| s = self.held_object.strip() | ||
| s_unsigned = s.lstrip('+-').lower() | ||
|
|
||
| if s_unsigned.startswith('0x'): | ||
| base = 16 | ||
| elif s_unsigned.startswith('0o'): | ||
| base = 8 | ||
| elif s_unsigned.startswith('0b'): | ||
| base = 2 | ||
| else: | ||
| base = 10 | ||
|
|
||
| return int(s, base) |
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.
| s = self.held_object.strip() | |
| s_unsigned = s.lstrip('+-').lower() | |
| if s_unsigned.startswith('0x'): | |
| base = 16 | |
| elif s_unsigned.startswith('0o'): | |
| base = 8 | |
| elif s_unsigned.startswith('0b'): | |
| base = 2 | |
| else: | |
| base = 10 | |
| return int(s, base) | |
| return int(s, base=0) |
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