aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIñigo Martínez <inigomartinez@gmail.com>2017-11-23 11:13:24 +0100
committerJussi Pakkanen <jpakkane@gmail.com>2017-11-26 17:14:32 +0200
commit30f2c4857c4798565a3f0d0b370b630335eb4687 (patch)
tree61375007da0c2ec2687395d46447ebccc94e5099
parent2dab7c37df5d9074b4033c547b0c64e22fd4d74c (diff)
downloadmeson-30f2c4857c4798565a3f0d0b370b630335eb4687.zip
meson-30f2c4857c4798565a3f0d0b370b630335eb4687.tar.gz
meson-30f2c4857c4798565a3f0d0b370b630335eb4687.tar.bz2
interpreter: Support to_string method for int values
Although some other base types like boolean objects can be expresed as strings, this is not possible with int objects. This patch adds support to express int values as strings as hex, decimal or octal values.
-rw-r--r--mesonbuild/interpreterbase.py10
-rw-r--r--test cases/common/165 int formatting/meson.build15
2 files changed, 25 insertions, 0 deletions
diff --git a/mesonbuild/interpreterbase.py b/mesonbuild/interpreterbase.py
index cb82e56..9bb311f 100644
--- a/mesonbuild/interpreterbase.py
+++ b/mesonbuild/interpreterbase.py
@@ -449,6 +449,16 @@ class InterpreterBase:
return obj % 2 != 0
else:
raise InterpreterException('int.is_odd() must have no arguments.')
+ elif method_name == 'to_string':
+ if not posargs:
+ return str(obj)
+ elif len(posargs) == 1 and isinstance(posargs[0], str):
+ f = 'd' if len(posargs[0].strip()) == 0 else posargs[0]
+ if re.match('^[bcdoxX]$', f) is None:
+ raise InvalidCode('Invalid format for int to string conversion "%s"' % f)
+ return str(('{:' + f + '}').format(obj))
+ else:
+ raise InterpreterException('int.to_string() must have either no arguments or exactly one string arguments that signify what format to use.')
else:
raise InterpreterException('Unknown method "%s" for an integer.' % method_name)
diff --git a/test cases/common/165 int formatting/meson.build b/test cases/common/165 int formatting/meson.build
new file mode 100644
index 0000000..ea9923b
--- /dev/null
+++ b/test cases/common/165 int formatting/meson.build
@@ -0,0 +1,15 @@
+project('int formatting', 'c')
+
+values = [
+ ['', '74'],
+ ['c', 'J'],
+ ['b', '1001010'],
+ ['d', '74'],
+ ['o', '112'],
+ ['x', '4a'],
+ ['X', '4A']
+]
+
+foreach value: values
+ assert(74.to_string(value[0]) == value[1], 'conversion is broken')
+endforeach