Skip to content

Commit e888d75

Browse files
committed
Reduce code duplication
1 parent 739965e commit e888d75

File tree

4 files changed

+10
-37
lines changed

4 files changed

+10
-37
lines changed

extra/detail/decode_decimal128.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Distributed under the Boost Software License, Version 1.0.
33
# https://www.boost.org/LICENSE_1_0.txt
44

5+
from generate_string import generate_string
6+
57
def decode_decimal128(bits):
68

79
bits_high = bits >> 64
@@ -41,17 +43,6 @@ def decode_decimal128(bits):
4143

4244
exp -= 6176 # Bias Value
4345

44-
if significand == 0:
45-
result = "0.0e+0"
46-
else:
47-
sig_str = str(significand)
48-
n_digits = len(sig_str)
49-
if n_digits == 1:
50-
normalized_str = sig_str
51-
total_exp = exp
52-
else:
53-
normalized_str = sig_str[0] + '.' + sig_str[1:]
54-
total_exp = exp + n_digits - 1
55-
result = f"{'-' if sign else ''}{normalized_str}e{total_exp:+}"
46+
result = generate_string(significand, exp, sign)
5647

5748
return result

extra/detail/decode_decimal32.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Distributed under the Boost Software License, Version 1.0.
33
# https://www.boost.org/LICENSE_1_0.txt
44

5+
from generate_string import generate_string
6+
57
def decode_decimal32(bits):
68
sign = bits & 2147483648 != 0
79
isnan = False
@@ -37,17 +39,6 @@ def decode_decimal32(bits):
3739

3840
exp -= 101 # Bias Value
3941

40-
if significand == 0:
41-
result = "0.0e+0"
42-
else:
43-
sig_str = str(significand)
44-
n_digits = len(sig_str)
45-
if n_digits == 1:
46-
normalized_str = sig_str
47-
total_exp = exp
48-
else:
49-
normalized_str = sig_str[0] + '.' + sig_str[1:]
50-
total_exp = exp + n_digits - 1
51-
result = f"{'-' if sign else ''}{normalized_str}e{total_exp:+}"
42+
result = generate_string(significand, exp, sign)
5243

5344
return result

extra/detail/decode_decimal64.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Distributed under the Boost Software License, Version 1.0.
33
# https://www.boost.org/LICENSE_1_0.txt
44

5+
from generate_string import generate_string
6+
57
def decode_decimal64(bits):
68
sign = bits & 9223372036854775808 != 0
79
isnan = False
@@ -36,17 +38,6 @@ def decode_decimal64(bits):
3638

3739
exp -= 398 # Bias Value
3840

39-
if significand == 0:
40-
result = "0.0e+0"
41-
else:
42-
sig_str = str(significand)
43-
n_digits = len(sig_str)
44-
if n_digits == 1:
45-
normalized_str = sig_str
46-
total_exp = exp
47-
else:
48-
normalized_str = sig_str[0] + '.' + sig_str[1:]
49-
total_exp = exp + n_digits - 1
50-
result = f"{'-' if sign else ''}{normalized_str}e{total_exp:+}"
41+
result = generate_string(significand, exp, sign)
5142

5243
return result

extra/detail/decode_fast_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
def decode_fast_type(significand, exp, sign):
88
return generate_string(significand, exp, sign)
9-
9+

0 commit comments

Comments
 (0)