Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit57d06d9

Browse files
committed
Simplify integer parsing
Simply treat the special octal case and let Python handle the rest.Also add extra tests, to show additional edge cases that are handledcorrectly.
1 parent5c84d08 commit57d06d9

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

‎esp32_ulp/util.py‎

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,14 @@ def validate_expression(param):
7979

8080
defparse_int(literal):
8181
"""
82-
Parses string literals into integers, using base prefixes
83-
0xNNN (hex), 0bNNN (binary), and 0oNNN or 0NNN (octal).
84-
Without prefix will be treated as decimal.
82+
GNU as compatible parsing of string literals into integers
83+
Specifically, GNU as treats literals starting with 0 as octal
84+
All other literals are correctly parsed by Python
85+
See: https://sourceware.org/binutils/docs/as/Integers.html
8586
"""
86-
iflen(literal)>2:
87-
prefix_start=1ifliteral[0]=='-'else0# skip negative sign if present
88-
89-
ifliteral[prefix_start]=="0":
90-
prefix=literal[prefix_start+1]
91-
ifprefix=="x":# Hex
92-
returnint(literal,16)
93-
elifprefix=="b":# Binary
94-
returnint(literal,2)
95-
elifprefix=="o":# Octal, Python style (0oNNN)
96-
returnint(literal,8)
97-
returnint(literal,8)# Octal, GNU as style (0NNN)
98-
99-
returnint(literal)# implicit decimal (base10)
87+
iflen(literal)>=2and (literal.startswith("0")orliteral.startswith("-0"))andliteral.lstrip("-0").isdigit():
88+
returnint(literal,8)
89+
returnint(literal,0)
10090

10191

10292
deffile_exists(filename):

‎tests/util.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ def test_validate_expression():
8585
@test
8686
deftest_parse_int():
8787
# decimal
88+
assertparse_int("0")==0,"0 == 0"
8889
assertparse_int("5")==5,"5 == 5"
90+
assertparse_int("-0")==0,"-0 == 0"
8991
assertparse_int("-5")==-5,"-5 == -5"
9092
# hex
9193
assertparse_int("0x5")==5,"0x5 == 5"
@@ -95,10 +97,14 @@ def test_parse_int():
9597
assertparse_int("0b1001")==9,"0b1001 == 9"
9698
assertparse_int("-0b1001")==-9,"-0b1001 == 9"
9799
# octal
100+
assertparse_int("07")==7,"07 == 7"
98101
assertparse_int("0100")==64,"0100 == 64"
99102
assertparse_int("0o210")==136,"0o210 == 136"
103+
assertparse_int("00000010")==8,"00000010 == 8"
104+
assertparse_int("-07")==-7,"-07 == -7"
100105
assertparse_int("-0100")==-64,"-0100 == -64"
101106
assertparse_int("-0o210")==-136,"-0o210 == -136"
107+
assertparse_int("-00000010")==-8,"-00000010 == -8"
102108
# negative cases
103109
assert_raises(ValueError,parse_int,'0b123',message="invalid syntax for integer with base 2: '123'")
104110
assert_raises(ValueError,parse_int,'0900',message="invalid syntax for integer with base 8: '0900'")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp