diff options
author | Mattias Andrée <maandree@kth.se> | 2018-08-26 12:49:12 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-08-26 12:49:12 +0200 |
commit | f2378869e1a3b1cb44b5c7973e0a9acfb2559d0f (patch) | |
tree | e8caadeb4359bb62c7ba0b13ce94704220dd9ac7 /minimise_number_string.c | |
parent | Tests and minor improvements (diff) | |
download | libsimple-f2378869e1a3b1cb44b5c7973e0a9acfb2559d0f.tar.gz libsimple-f2378869e1a3b1cb44b5c7973e0a9acfb2559d0f.tar.bz2 libsimple-f2378869e1a3b1cb44b5c7973e0a9acfb2559d0f.tar.xz |
Fixes, more test, and add minimise_number_string
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'minimise_number_string.c')
-rw-r--r-- | minimise_number_string.c | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/minimise_number_string.c b/minimise_number_string.c new file mode 100644 index 0000000..5993564 --- /dev/null +++ b/minimise_number_string.c @@ -0,0 +1,130 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +char * +libsimple_minimise_number_string(char *s) +{ + char *p, *ret = s; + size_t n; + + if (*s == '+') + memmove(s, &s[1], strlen(s)); + + p = strchr(s, '.'); + if (p) { + p = strchr(p, '\0'); + while (p[-1] == '0') + *--p = '\0'; + if (p[-1] == '.') { + if (s[0] == '.') { + s[0] = '0'; + return ret; + } else if (s[0] == '-' && s[1] == '.') { + s[1] = '0'; + return ret; + } + *--p = '\0'; + } + } + + if (*s == '-') + s = &s[1]; + + for (p = s; *p == '0'; p++); + n = (size_t)(p - s); + if (!*p || *p == '.') + n -= !!n; + memmove(s, &s[n], strlen(&s[n]) + 1); + + return ret; +} + + +#else +#include "test.h" + +int +main(void) +{ + char buf[1000]; + + /* Some aspects are tested in libsimple.c */ + + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "00100.00000")), "100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "00100.1")), "100.1")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "00100.000001")), "100.000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "00100.1000001")), "100.1000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "00100.0")), "100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "00100.")), "100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "100.00000")), "100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "100.1")), "100.1")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "100.000001")), "100.000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "100.1000001")), "100.1000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "100.0")), "100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "100.")), "100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "0.00000")), "0")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "0.1")), "0.1")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "0.000001")), "0.000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "0.1000001")), "0.1000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "0.0")), "0")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "0.")), "0")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, ".00000")), "0")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, ".1")), ".1")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, ".000001")), ".000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, ".1000001")), ".1000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, ".0")), "0")); + + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+00100.00000")), "100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+00100.1")), "100.1")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+00100.000001")), "100.000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+00100.1000001")), "100.1000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+00100.0")), "100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+00100.")), "100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+100.00000")), "100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+100.1")), "100.1")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+100.000001")), "100.000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+100.1000001")), "100.1000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+100.0")), "100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+100.")), "100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+0.00000")), "0")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+0.1")), "0.1")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+0.000001")), "0.000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+0.1000001")), "0.1000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+0.0")), "0")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+0.")), "0")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+.00000")), "0")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+.1")), ".1")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+.000001")), ".000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+.1000001")), ".1000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "+.0")), "0")); + + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-00100.00000")), "-100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-00100.1")), "-100.1")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-00100.000001")), "-100.000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-00100.1000001")), "-100.1000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-00100.0")), "-100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-00100.")), "-100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-100.00000")), "-100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-100.1")), "-100.1")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-100.000001")), "-100.000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-100.1000001")), "-100.1000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-100.0")), "-100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-100.")), "-100")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-0.00000")), "-0")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-0.1")), "-0.1")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-0.000001")), "-0.000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-0.1000001")), "-0.1000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-0.0")), "-0")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-0.")), "-0")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-.00000")), "-0")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-.1")), "-.1")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-.000001")), "-.000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-.1000001")), "-.1000001")); + assert(!strcmp(libsimple_minimise_number_string(strcpy(buf, "-.0")), "-0")); + + return 0; +} + +#endif |