Updated hunspell to 1.3.2
[TortoiseGit.git] / ext / hunspell / replist.cxx
blobca05cc1818cf8b9b882fd88fec707cc2fc096ed0
1 #include "license.hunspell"
2 #include "license.myspell"
4 #include <stdlib.h>
5 #include <string.h>
6 #include <stdio.h>
8 #include "replist.hxx"
9 #include "csutil.hxx"
11 RepList::RepList(int n) {
12 dat = (replentry **) malloc(sizeof(replentry *) * n);
13 if (dat == 0) size = 0; else size = n;
14 pos = 0;
17 RepList::~RepList()
19 for (int i = 0; i < pos; i++) {
20 free(dat[i]->pattern);
21 free(dat[i]->pattern2);
22 free(dat[i]);
24 free(dat);
27 int RepList::get_pos() {
28 return pos;
31 replentry * RepList::item(int n) {
32 return dat[n];
35 int RepList::near(const char * word) {
36 int p1 = 0;
37 int p2 = pos;
38 while ((p2 - p1) > 1) {
39 int m = (p1 + p2) / 2;
40 int c = strcmp(word, dat[m]->pattern);
41 if (c <= 0) {
42 if (c < 0) p2 = m; else p1 = p2 = m;
43 } else p1 = m;
45 return p1;
48 int RepList::match(const char * word, int n) {
49 if (strncmp(word, dat[n]->pattern, strlen(dat[n]->pattern)) == 0) return strlen(dat[n]->pattern);
50 return 0;
53 int RepList::add(char * pat1, char * pat2) {
54 if (pos >= size || pat1 == NULL || pat2 == NULL) return 1;
55 replentry * r = (replentry *) malloc(sizeof(replentry));
56 if (r == NULL) return 1;
57 r->pattern = mystrrep(pat1, "_", " ");
58 r->pattern2 = mystrrep(pat2, "_", " ");
59 r->start = false;
60 r->end = false;
61 dat[pos++] = r;
62 for (int i = pos - 1; i > 0; i--) {
63 r = dat[i];
64 if (strcmp(r->pattern, dat[i - 1]->pattern) < 0) {
65 dat[i] = dat[i - 1];
66 dat[i - 1] = r;
67 } else break;
69 return 0;
72 int RepList::conv(const char * word, char * dest) {
73 int stl = 0;
74 int change = 0;
75 for (size_t i = 0; i < strlen(word); i++) {
76 int n = near(word + i);
77 int l = match(word + i, n);
78 if (l) {
79 strcpy(dest + stl, dat[n]->pattern2);
80 stl += strlen(dat[n]->pattern2);
81 i += l - 1;
82 change = 1;
83 } else dest[stl++] = word[i];
85 dest[stl] = '\0';
86 return change;