-
-
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
cf4e844 to
d50cfec
Compare
| 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) |
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 cannot be done unilaterally because of backwards compatibility. Before this, meson treats "010" as being "10" (decimal), and Python 3's int() with no base (or base=10) also treats it as decimal. However, Python 3's int() with base=0 raises ValueError for strings with leading zeros because it disallows leading zeros for decimal numbers when auto-detecting the base. So while we can indeed use base=0 to simplify some cases, we still need an explicit check for the leading-zero case to maintain backward compatibility.
4c1d7ef to
c3c68bf
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
c3c68bf to
2357ce0
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: #2047
Fixes: #15201