Skip to content

Commit 2fd9816

Browse files
authored
[arith] Fix reversed digits in $((base#num)). (#724)
1 parent 6966b67 commit 2fd9816

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

osh/sh_expr_eval.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ def _StringToInteger(self, s, span_id=runtime.NO_SPID):
229229
e_strict('Invalid base for numeric constant %r', b, span_id=span_id)
230230

231231
integer = 0
232-
n = 1
233232
for ch in digits:
234233
if 'a' <= ch and ch <= 'z':
235234
digit = ord(ch) - ord('a') + 10
@@ -247,8 +246,7 @@ def _StringToInteger(self, s, span_id=runtime.NO_SPID):
247246
if digit >= base:
248247
e_strict('Digits %r out of range for base %d', digits, base, span_id=span_id)
249248

250-
integer += digit * n
251-
n *= base
249+
integer = integer * base + digit
252250
return integer
253251

254252
try:

spec/arith.test.sh

+6
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ echo $((64#a))-$((64#z)), $((64#A))-$((64#Z)), $((64#@)), $(( 64#_ ))
237237
## N-I mksh/zsh stdout-json: ""
238238
## N-I mksh/zsh status: 1
239239

240+
#### Multiple digit constants with base N
241+
echo $((10#0123)), $((16#1b))
242+
## stdout: 123, 27
243+
## N-I dash stdout-json: ""
244+
## N-I dash status: 2
245+
240246
#### Dynamic base constants
241247
base=16
242248
echo $(( ${base}#a ))

0 commit comments

Comments
 (0)