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