comparison libnumutil/numstring.c @ 0:159dd90eeafe

beginning, libnumutil compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 12 Dec 2023 23:52:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:159dd90eeafe
1 /*
2 * Utility functions for number string initial parsing or preening.
3 * grok_number_string() checks whether or not a user-supplied string
4 * argument is fully numeric (with possibility of allowing hyphens),
5 * and returns the number of digits. dehyphen_number_string() copies
6 * a possibly-hyphenated number string to a new buffer with all hyphens
7 * taken out.
8 */
9
10 #include <ctype.h>
11
12 #include <themwi/nanp/number_utils.h>
13
14 int grok_number_string(const char *str, bool allow_hyphen)
15 {
16 const char *cp;
17 int c, n;
18 bool last_hyphen;
19
20 n = 0;
21 last_hyphen = false;
22 for (cp = str; *cp; ) {
23 c = *cp++;
24 if (isdigit(c)) {
25 n++;
26 last_hyphen = false;
27 } else if (c == '-') {
28 if (!allow_hyphen || !n || last_hyphen)
29 return(-1);
30 last_hyphen = true;
31 } else
32 return(-1);
33 }
34 if (last_hyphen)
35 return(-1);
36 return n;
37 }
38
39 void dehyphen_number_string(const char *src, char *dest)
40 {
41 const char *cp;
42 char *dp;
43 int c;
44
45 dp = dest;
46 for (cp = src; *cp; ) {
47 c = *cp++;
48 if (isdigit(c))
49 *dp++ = c;
50 }
51 *dp = '\0';
52 }