~ubuntu-branches/debian/wheezy/mudlet/wheezy

« back to all changes in this revision

Viewing changes to src/hunspell/replist.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Craig Small
  • Date: 2011-05-14 20:12:49 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110514201249-184gqx5jjqam02lg
Tags: 2.0-rc5-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "license.hunspell"
2
 
#include "license.myspell"
3
 
 
4
 
#include <stdlib.h>
5
 
#include <string.h>
6
 
#include <stdio.h>
7
 
 
8
 
#include "replist.hxx"
9
 
#include "csutil.hxx"
10
 
 
11
 
RepList::RepList(int n) {
12
 
    dat = (replentry **) malloc(sizeof(replentry *) * n);
13
 
    if (dat == 0) size = 0; else size = n;
14
 
    pos = 0;
15
 
}
16
 
 
17
 
RepList::~RepList()
18
 
{
19
 
    for (int i = 0; i < pos; i++) {
20
 
        free(dat[i]->pattern);
21
 
        free(dat[i]->pattern2);
22
 
        free(dat[i]);
23
 
    }
24
 
    free(dat);
25
 
}
26
 
 
27
 
int RepList::get_pos() {
28
 
    return pos;
29
 
}
30
 
 
31
 
replentry * RepList::item(int n) {
32
 
    return dat[n];
33
 
}
34
 
 
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
 
//      fprintf(stderr, "m: %d p1: %d p2: %d dat: %s\n", m, p1, p2, dat[m]->pattern);
41
 
      int c = strcmp(word, dat[m]->pattern);
42
 
      if (c <= 0) {
43
 
        if (c < 0) p2 = m; else p1 = p2 = m;
44
 
      } else p1 = m;
45
 
    }
46
 
//    fprintf(stderr, "NEAR: %s (word: %s)\n", dat[p1]->pattern, word);
47
 
    return p1;
48
 
}
49
 
 
50
 
int RepList::match(const char * word, int n) {
51
 
    if (strncmp(word, dat[n]->pattern, strlen(dat[n]->pattern)) == 0) return strlen(dat[n]->pattern);
52
 
    return 0;
53
 
}
54
 
 
55
 
int RepList::add(char * pat1, char * pat2) {
56
 
    if (pos >= size || pat1 == NULL || pat2 == NULL) return 1;
57
 
    replentry * r = (replentry *) malloc(sizeof(replentry));
58
 
    if (r == NULL) return 1;
59
 
    r->pattern = mystrrep(pat1, "_", " ");
60
 
    r->pattern2 = mystrrep(pat2, "_", " ");
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;
68
 
    }
69
 
    return 0;
70
 
}
71
 
 
72
 
int RepList::conv(const char * word, char * dest) {
73
 
    int stl = 0;
74
 
    int change = 0;
75
 
//    for (int i = 0; i < pos; i++) fprintf(stderr, "%d. %s\n", i, dat[i]->pattern);
76
 
    for (size_t i = 0; i < strlen(word); i++) {
77
 
        int n = near(word + i);
78
 
        int l = match(word + i, n);
79
 
        if (l) {
80
 
          strcpy(dest + stl, dat[n]->pattern2);
81
 
          stl += strlen(dat[n]->pattern2);
82
 
          i += l - 1;
83
 
          change = 1;
84
 
        } else dest[stl++] = word[i];
85
 
    }
86
 
    dest[stl] = '\0';
87
 
//    fprintf(stderr, "i: %s o: %s change: %d\n", word, dest, change);
88
 
    return change;
89
 
}