FreeCalypso > hg > themwi-system-sw
annotate libutil/numstring.c @ 247:c6a3fb913c53
top Makefile: mtctest changed to use libnumdb2
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 14 Aug 2023 22:39:28 -0800 |
parents | d712d518059e |
children |
rev | line source |
---|---|
3
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * Utility functions for number string initial parsing or preening. |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * grok_number_string() checks whether or not a user-supplied string |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * argument is fully numeric (with possibility of allowing hyphens), |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 * and returns the number of digits. dehyphen_number_string() copies |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 * a possibly-hyphenated number string to a new buffer with all hyphens |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 * taken out. |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 */ |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <ctype.h> |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 grok_number_string(str, allow_hyphen) |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 char *str; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 { |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 char *cp; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 int c, n, last_hyphen; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 n = 0; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 last_hyphen = 0; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 for (cp = str; *cp; ) { |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 c = *cp++; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 if (isdigit(c)) { |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 n++; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 last_hyphen = 0; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 } else if (c == '-') { |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 if (!allow_hyphen || !n || last_hyphen) |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 return(-1); |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 last_hyphen = 1; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 } else |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 return(-1); |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 } |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 if (last_hyphen) |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 return(-1); |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 return n; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 } |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 dehyphen_number_string(src, dest) |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 char *src, *dest; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 { |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 char *cp, *dp; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 int c; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 dp = dest; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 for (cp = src; *cp; ) { |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 c = *cp++; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 if (isdigit(c)) |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 *dp++ = c; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 *dp = '\0'; |
d712d518059e
libutil: add functions for number string parsing/preening
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 } |