Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Walter Bright
committed
Mar 5, 2010
1 parent
993c2a9
commit 8c55b9f
Showing
12 changed files
with
204 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
#include "speller.h" | ||
|
||
const char idchars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; | ||
|
||
/************************************************** | ||
* Looks for correct spelling. | ||
* Currently only looks a 'distance' of one from the seed[]. | ||
* This does an exhaustive search, so can potentially be very slow. | ||
* Input: | ||
* seed wrongly spelled word | ||
* fp search function | ||
* fparg argument to search function | ||
* charset character set | ||
* Returns: | ||
* NULL no correct spellings found | ||
* void* value returned by fp() for first possible correct spelling | ||
*/ | ||
|
||
void *speller(const char *seed, fp_speller_t fp, void *fparg, const char *charset) | ||
{ | ||
size_t seedlen = strlen(seed); | ||
if (!seedlen) | ||
return NULL; | ||
|
||
char *buf = (char *)alloca(seedlen + 2); // leave space for extra char | ||
if (!buf) | ||
return NULL; // no matches | ||
|
||
/* Deletions */ | ||
memcpy(buf, seed + 1, seedlen); | ||
for (int i = 0; i < seedlen; i++) | ||
{ | ||
//printf("del buf = '%s'\n", buf); | ||
void *p = (*fp)(fparg, buf); | ||
if (p) | ||
return p; | ||
|
||
buf[i] = seed[i]; | ||
} | ||
|
||
/* Transpositions */ | ||
memcpy(buf, seed, seedlen + 1); | ||
for (int i = 0; i + 1 < seedlen; i++) | ||
{ | ||
// swap [i] and [i + 1] | ||
buf[i] = seed[i + 1]; | ||
buf[i + 1] = seed[i]; | ||
|
||
//printf("tra buf = '%s'\n", buf); | ||
void *p = (*fp)(fparg, buf); | ||
if (p) | ||
return p; | ||
|
||
buf[i] = seed[i]; | ||
} | ||
|
||
if (charset && *charset) | ||
{ | ||
/* Substitutions */ | ||
memcpy(buf, seed, seedlen + 1); | ||
for (int i = 0; i < seedlen; i++) | ||
{ | ||
for (const char *s = charset; *s; s++) | ||
{ | ||
buf[i] = *s; | ||
|
||
//printf("sub buf = '%s'\n", buf); | ||
void *p = (*fp)(fparg, buf); | ||
if (p) | ||
return p; | ||
} | ||
buf[i] = seed[i]; | ||
} | ||
|
||
/* Insertions */ | ||
memcpy(buf + 1, seed, seedlen + 1); | ||
for (int i = 0; i <= seedlen; i++) // yes, do seedlen+1 iterations | ||
{ | ||
for (const char *s = charset; *s; s++) | ||
{ | ||
buf[i] = *s; | ||
|
||
//printf("ins buf = '%s'\n", buf); | ||
void *p = (*fp)(fparg, buf); | ||
if (p) | ||
return p; | ||
} | ||
buf[i] = seed[i]; // going past end of seed[] is ok, as we hit the 0 | ||
} | ||
} | ||
|
||
return NULL; // didn't find any corrections | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
typedef void *(fp_speller_t)(void *, const char *); | ||
|
||
extern const char idchars[]; | ||
|
||
void *speller(const char *seed, fp_speller_t fp, void *fparg, const char *charset); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters