aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2017-05-21 10:33:04 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2017-05-21 13:19:48 +0200
commit792deba5f3953b5dae44e0e0639444b83b7bc43b (patch)
tree8aab43935b5b436f23089d64eb1fb8b991cfbf4a
parent9eb2967340cf35abebb6bc39be4bcd5fd73f866c (diff)
downloadell-792deba5f3953b5dae44e0e0639444b83b7bc43b.tar.gz
ell-792deba5f3953b5dae44e0e0639444b83b7bc43b.tar.xz
ell-792deba5f3953b5dae44e0e0639444b83b7bc43b.zip
Omit trailing zeros when not necessary
-rwxr-xr-xell.c30
1 files changed, 15 insertions, 15 deletions
diff --git a/ell.c b/ell.c
index 8905e27..e1a2831 100755
--- a/ell.c
+++ b/ell.c
@@ -888,18 +888,18 @@ execute (struct context *ctx, struct item *body, struct item **result) {
#define defn(name) static bool name \
(struct context *ctx, struct item *args, struct item **result)
-static struct item *new_format (const char *fmt, ...) ATTRIBUTE_PRINTF (1, 2);
-
static struct item *
-new_format (const char *fmt, ...) {
- va_list ap;
- va_start (ap, fmt);
- char *s = vformat (fmt, ap);
- va_end (ap);
-
- if (!s)
+new_number (double n) {
+ char *s;
+ if (!(s = format ("%f", n)))
return NULL;
+ char *p = strchr (s, 0);
+ while (--p > s && *p == '0')
+ *p = 0;
+ if (*p == '.')
+ *p = 0;
+
struct item *item = new_string (s, strlen (s));
free (s);
return item;
@@ -1090,7 +1090,7 @@ defn (fn_system) {
return set_error (ctx, "first argument must be string");
if (command->next)
return set_error (ctx, "cannot deal with multiple arguments");
- return check (ctx, (*result = new_format ("%d", system (command->value))));
+ return check (ctx, (*result = new_number (system (command->value))));
}
defn (fn_plus) {
@@ -1100,17 +1100,17 @@ defn (fn_plus) {
return set_error (ctx, "arguments must be strings");
res += strtod (args->value, NULL);
}
- return check (ctx, (*result = new_format ("%f", res)));
+ return check (ctx, (*result = new_number (res)));
}
defn (fn_minus) {
- double res = -0.0;
+ double res = 0.0;
for (; args; args = args->next) {
if (args->type != ITEM_STRING)
return set_error (ctx, "arguments must be strings");
res -= strtod (args->value, NULL);
}
- return check (ctx, (*result = new_format ("%f", res)));
+ return check (ctx, (*result = new_number (res)));
}
defn (fn_multiply) {
@@ -1120,7 +1120,7 @@ defn (fn_multiply) {
return set_error (ctx, "arguments must be strings");
res *= strtod (args->value, NULL);
}
- return check (ctx, (*result = new_format ("%f", res)));
+ return check (ctx, (*result = new_number (res)));
}
defn (fn_divide) {
@@ -1134,7 +1134,7 @@ defn (fn_divide) {
return set_error (ctx, "division by zero");
res /= x;
}
- return check (ctx, (*result = new_format ("%f", res)));
+ return check (ctx, (*result = new_number (res)));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -