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