Mercurial > projects > ldc
annotate dmd/speller.c @ 1643:8f121883bce8
Apply patch from klickverbot. This is his 'proper fix' patch for bug #395.
author | Kelly Wilson <wilsonk cpsc.ucalgary.ca> |
---|---|
date | Mon, 08 Mar 2010 23:37:40 -0700 |
parents | 00cd99bedf06 |
children |
rev | line source |
---|---|
1641
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
1 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
2 #include <stdio.h> |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
3 #include <string.h> |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
4 #include <stdlib.h> |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
5 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
6 #include "speller.h" |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
7 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
8 const char idchars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
9 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
10 /************************************************** |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
11 * Looks for correct spelling. |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
12 * Currently only looks a 'distance' of one from the seed[]. |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
13 * This does an exhaustive search, so can potentially be very slow. |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
14 * Input: |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
15 *seedwrongly spelled word |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
16 *fpsearch function |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
17 *fpargargument to search function |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
18 *charsetcharacter set |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
19 * Returns: |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
20 *NULLno correct spellings found |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
21 *void*value returned by fp() for first possible correct spelling |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
22 */ |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
23 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
24 void *speller(const char *seed, fp_speller_t fp, void *fparg, const char *charset) |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
25 { |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
26 size_t seedlen = strlen(seed); |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
27 if (!seedlen) |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
28 return NULL; |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
29 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
30 char *buf = (char *)alloca(seedlen + 2);// leave space for extra char |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
31 if (!buf) |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
32 return NULL;// no matches |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
33 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
34 /* Deletions */ |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
35 memcpy(buf, seed + 1, seedlen); |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
36 for (int i = 0; i < seedlen; i++) |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
37 { |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
38 //printf("del buf = '%s'\n", buf); |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
39 void *p = (*fp)(fparg, buf); |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
40 if (p) |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
41 return p; |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
42 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
43 buf[i] = seed[i]; |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
44 } |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
45 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
46 /* Transpositions */ |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
47 memcpy(buf, seed, seedlen + 1); |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
48 for (int i = 0; i + 1 < seedlen; i++) |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
49 { |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
50 // swap [i] and [i + 1] |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
51 buf[i] = seed[i + 1]; |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
52 buf[i + 1] = seed[i]; |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
53 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
54 //printf("tra buf = '%s'\n", buf); |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
55 void *p = (*fp)(fparg, buf); |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
56 if (p) |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
57 return p; |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
58 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
59 buf[i] = seed[i]; |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
60 } |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
61 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
62 if (charset && *charset) |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
63 { |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
64 /* Substitutions */ |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
65 memcpy(buf, seed, seedlen + 1); |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
66 for (int i = 0; i < seedlen; i++) |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
67 { |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
68 for (const char *s = charset; *s; s++) |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
69 { |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
70 buf[i] = *s; |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
71 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
72 //printf("sub buf = '%s'\n", buf); |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
73 void *p = (*fp)(fparg, buf); |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
74 if (p) |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
75 return p; |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
76 } |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
77 buf[i] = seed[i]; |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
78 } |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
79 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
80 /* Insertions */ |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
81 memcpy(buf + 1, seed, seedlen + 1); |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
82 for (int i = 0; i <= seedlen; i++)// yes, do seedlen+1 iterations |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
83 { |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
84 for (const char *s = charset; *s; s++) |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
85 { |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
86 buf[i] = *s; |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
87 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
88 //printf("ins buf = '%s'\n", buf); |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
89 void *p = (*fp)(fparg, buf); |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
90 if (p) |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
91 return p; |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
92 } |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
93 buf[i] = seed[i];// going past end of seed[] is ok, as we hit the 0 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
94 } |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
95 } |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
96 |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
97 return NULL;// didn't find any corrections |
00cd99bedf06
Add missing files for the new frontend spell checker
Kelly Wilson <wilsonk cpsc.ucalgary.ca>
parents:
diff
changeset
|
98 } |