updated
[gnutls.git] / src / libopts / streqvcmp.c
blob54a063affc14629d49633af5660849d38a3cf8bf
2 /**
3 * \file streqvcmp.c
5 * Time-stamp: "2012-03-31 13:17:39 bkorb"
7 * String Equivalence Comparison
9 * These routines allow any character to be mapped to any other
10 * character before comparison. In processing long option names,
11 * the characters "-", "_" and "^" all need to be equivalent
12 * (because they are treated so by different development environments).
14 * This file is part of AutoOpts, a companion to AutoGen.
15 * AutoOpts is free software.
16 * AutoOpts is Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
18 * AutoOpts is available under any one of two licenses. The license
19 * in use must be one of these two and the choice is under the control
20 * of the user of the license.
22 * The GNU Lesser General Public License, version 3 or later
23 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
25 * The Modified Berkeley Software Distribution License
26 * See the file "COPYING.mbsd"
28 * These files have the following md5sums:
30 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
31 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
32 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
34 * This array is designed for mapping upper and lower case letter
35 * together for a case independent comparison. The mappings are
36 * based upon ascii character sequences.
38 static unsigned char charmap[] = {
39 NUL, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, '\a',
40 '\b', '\t', NL, '\v', '\f', '\r', 0x0E, 0x0F,
41 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
42 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
44 ' ', '!', '"', '#', '$', '%', '&', '\'',
45 '(', ')', '*', '+', ',', '-', '.', '/',
46 '0', '1', '2', '3', '4', '5', '6', '7',
47 '8', '9', ':', ';', '<', '=', '>', '?',
49 '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
50 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
51 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
52 'x', 'y', 'z', '[', '\\', ']', '^', '_',
53 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
54 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
55 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
56 'x', 'y', 'z', '{', '|', '}', '~', 0x7f,
58 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
59 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
60 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
61 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
62 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
63 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
64 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7,
65 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
67 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
68 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
69 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7,
70 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
71 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
72 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
73 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
74 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
78 /*=export_func strneqvcmp
80 * what: compare two strings with an equivalence mapping
82 * arg: + char const* + str1 + first string +
83 * arg: + char const* + str2 + second string +
84 * arg: + int + ct + compare length +
86 * ret_type: int
87 * ret_desc: the difference between two differing characters
89 * doc:
91 * Using a character mapping, two strings are compared for "equivalence".
92 * Each input character is mapped to a comparison character and the
93 * mapped-to characters are compared for the two NUL terminated input strings.
94 * The comparison is limited to @code{ct} bytes.
95 * This function name is mapped to option_strneqvcmp so as to not conflict
96 * with the POSIX name space.
98 * err: none checked. Caller responsible for seg faults.
99 =*/
101 strneqvcmp(tCC* s1, tCC* s2, int ct)
103 for (; ct > 0; --ct) {
104 unsigned char u1 = (unsigned char) *s1++;
105 unsigned char u2 = (unsigned char) *s2++;
106 int dif = charmap[ u1 ] - charmap[ u2 ];
108 if (dif != 0)
109 return dif;
111 if (u1 == NUL)
112 return 0;
115 return 0;
119 /*=export_func streqvcmp
121 * what: compare two strings with an equivalence mapping
123 * arg: + char const* + str1 + first string +
124 * arg: + char const* + str2 + second string +
126 * ret_type: int
127 * ret_desc: the difference between two differing characters
129 * doc:
131 * Using a character mapping, two strings are compared for "equivalence".
132 * Each input character is mapped to a comparison character and the
133 * mapped-to characters are compared for the two NUL terminated input strings.
134 * This function name is mapped to option_streqvcmp so as to not conflict
135 * with the POSIX name space.
137 * err: none checked. Caller responsible for seg faults.
140 streqvcmp(tCC* s1, tCC* s2)
142 for (;;) {
143 unsigned char u1 = (unsigned char) *s1++;
144 unsigned char u2 = (unsigned char) *s2++;
145 int dif = charmap[ u1 ] - charmap[ u2 ];
147 if (dif != 0)
148 return dif;
150 if (u1 == NUL)
151 return 0;
156 /*=export_func streqvmap
158 * what: Set the character mappings for the streqv functions
160 * arg: + char + From + Input character +
161 * arg: + char + To + Mapped-to character +
162 * arg: + int + ct + compare length +
164 * doc:
166 * Set the character mapping. If the count (@code{ct}) is set to zero, then
167 * the map is cleared by setting all entries in the map to their index
168 * value. Otherwise, the "@code{From}" character is mapped to the "@code{To}"
169 * character. If @code{ct} is greater than 1, then @code{From} and @code{To}
170 * are incremented and the process repeated until @code{ct} entries have been
171 * set. For example,
172 * @example
173 * streqvmap('a', 'A', 26);
174 * @end example
175 * @noindent
176 * will alter the mapping so that all English lower case letters
177 * will map to upper case.
179 * This function name is mapped to option_streqvmap so as to not conflict
180 * with the POSIX name space.
182 * err: none.
184 void
185 streqvmap(char From, char To, int ct)
187 if (ct == 0) {
188 ct = sizeof(charmap) - 1;
189 do {
190 charmap[ct] = (unsigned char)ct;
191 } while (--ct >= 0);
194 else {
195 unsigned int chTo = (int)To & 0xFF;
196 unsigned int chFrom = (int)From & 0xFF;
198 do {
199 charmap[chFrom] = (unsigned char)chTo;
200 chFrom++;
201 chTo++;
202 if ((chFrom >= sizeof(charmap)) || (chTo >= sizeof(charmap)))
203 break;
204 } while (--ct > 0);
209 /*=export_func strequate
211 * what: map a list of characters to the same value
213 * arg: + char const* + ch_list + characters to equivalence +
215 * doc:
217 * Each character in the input string get mapped to the first character
218 * in the string.
219 * This function name is mapped to option_strequate so as to not conflict
220 * with the POSIX name space.
222 * err: none.
224 void
225 strequate(char const* s)
227 if ((s != NULL) && (*s != NUL)) {
228 unsigned char equiv = (unsigned)*s;
229 while (*s != NUL)
230 charmap[ (unsigned)*(s++) ] = equiv;
235 /*=export_func strtransform
237 * what: convert a string into its mapped-to value
239 * arg: + char* + dest + output string +
240 * arg: + char const* + src + input string +
242 * doc:
244 * Each character in the input string is mapped and the mapped-to
245 * character is put into the output.
246 * This function name is mapped to option_strtransform so as to not conflict
247 * with the POSIX name space.
249 * The source and destination may be the same.
251 * err: none.
253 void
254 strtransform(char* d, char const* s)
256 do {
257 *(d++) = (char)charmap[ (unsigned)*s ];
258 } while (*(s++) != NUL);
262 * Local Variables:
263 * mode: C
264 * c-file-style: "stroustrup"
265 * indent-tabs-mode: nil
266 * End:
267 * end of autoopts/streqvcmp.c */