Sublime Forum

Allow C-style type specifiers for Arithmetic calculator

#1

I would like to propose adding support for e.g. 10u or 10.0f (and so on) in the embedded Arithmetic calculator. Actually those letters just need to be ignored.

This would especially help in case you select source code like (1u << 11u) in Sublime Text and run the Arithmetic option on it.

Thanks!
Andi

0 Likes

#2

I think this is most likely a plugin you’ve installed. In that case, you’d want to contact the author of that plugin for the feature support you mentioned.

Well I need to recant. I looked in the Command Palette and lo and behold there’s an Arithmetic command. I never knew that before :smiley:

0 Likes

#3

image

The source code of Arithmetic is in the Default package so it’s kind of a built-in plugin indeed :slight_smile:

1 Like

#4

The arithmetic plugin uses eval to evaluate the expression. It would be possible to ignore suffixes of numbers like that if you strip them with a regex replace before. In fact, you can apply the following changes using the ViewPackageResource package:

--- Shipped Packages/Default/arithmetic.py  2019-03-01 16:50:58
+++ Packages/Default/arithmetic.py  2019-08-07 18:17:02
@@ -1,5 +1,6 @@
 import sublime_plugin
 import math
+import re
 
 
 def try_eval(str):
@@ -11,6 +12,7 @@
 
 def eval_expr(orig, i, expr):
     x = try_eval(orig) or 0
+    expr = re.sub(r"([\d.]+)[^\We]", r"\1", expr)
 
     return eval(expr, {"s": orig, "x": x, "i": i, "math": math}, {})

Note that this isn’t exactly a proper fix because I’m lazy about adjusting orig, but you’ll understand what to change if you need to.

0 Likes

#5

Ok, understood. This is good enough for me.
Thanks a lot for taking care!

0 Likes