Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / extensions / spellcheck / hunspell / src / replist.cpp
blobe4b251b12fee208d78f4c84325a10f0f8d9b0962
1 /******* BEGIN LICENSE BLOCK *******
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Initial Developers of the Original Code are Kevin Hendricks (MySpell)
15 * and László Németh (Hunspell). Portions created by the Initial Developers
16 * are Copyright (C) 2002-2005 the Initial Developers. All Rights Reserved.
18 * Contributor(s): László Németh (nemethl@gyorsposta.hu)
20 * Alternatively, the contents of this file may be used under the terms of
21 * either the GNU General Public License Version 2 or later (the "GPL"), or
22 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23 * in which case the provisions of the GPL or the LGPL are applicable instead
24 * of those above. If you wish to allow use of your version of this file only
25 * under the terms of either the GPL or the LGPL, and not to allow others to
26 * use your version of this file under the terms of the MPL, indicate your
27 * decision by deleting the provisions above and replace them with the notice
28 * and other provisions required by the GPL or the LGPL. If you do not delete
29 * the provisions above, a recipient may use your version of this file under
30 * the terms of any one of the MPL, the GPL or the LGPL.
32 ******* END LICENSE BLOCK *******/
34 #ifndef MOZILLA_CLIENT
35 #include <cstdlib>
36 #include <cstring>
37 #include <cstdio>
38 #else
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdio.h>
42 #endif
44 #include "replist.hxx"
45 #include "csutil.hxx"
47 RepList::RepList(int n) {
48 dat = (replentry **) malloc(sizeof(replentry *) * n);
49 if (dat == 0) size = 0; else size = n;
50 pos = 0;
53 RepList::~RepList()
55 for (int i = 0; i < pos; i++) {
56 free(dat[i]->pattern);
57 free(dat[i]->pattern2);
58 free(dat[i]);
60 free(dat);
63 int RepList::get_pos() {
64 return pos;
67 replentry * RepList::item(int n) {
68 return dat[n];
71 int RepList::near(const char * word) {
72 int p1 = 0;
73 int p2 = pos;
74 while ((p2 - p1) > 1) {
75 int m = (p1 + p2) / 2;
76 // fprintf(stderr, "m: %d p1: %d p2: %d dat: %s\n", m, p1, p2, dat[m]->pattern);
77 int c = strcmp(word, dat[m]->pattern);
78 if (c <= 0) {
79 if (c < 0) p2 = m; else p1 = p2 = m;
80 } else p1 = m;
82 // fprintf(stderr, "NEAR: %s (word: %s)\n", dat[p1]->pattern, word);
83 return p1;
86 int RepList::match(const char * word, int n) {
87 if (strncmp(word, dat[n]->pattern, strlen(dat[n]->pattern)) == 0) return strlen(dat[n]->pattern);
88 return 0;
91 int RepList::add(char * pat1, char * pat2) {
92 if (pos >= size || pat1 == NULL || pat2 == NULL) return 1;
93 replentry * r = (replentry *) malloc(sizeof(replentry));
94 if (r == NULL) return 1;
95 r->pattern = mystrrep(pat1, "_", " ");
96 r->pattern2 = mystrrep(pat2, "_", " ");
97 dat[pos++] = r;
98 for (int i = pos - 1; i > 0; i--) {
99 r = dat[i];
100 if (strcmp(r->pattern, dat[i - 1]->pattern) < 0) {
101 dat[i] = dat[i - 1];
102 dat[i - 1] = r;
103 } else break;
105 return 0;
108 int RepList::conv(const char * word, char * dest) {
109 int stl = 0;
110 int change = 0;
111 // for (int i = 0; i < pos; i++) fprintf(stderr, "%d. %s\n", i, dat[i]->pattern);
112 for (int i = 0; i < strlen(word); i++) {
113 int n = near(word + i);
114 int l = match(word + i, n);
115 if (l) {
116 strcpy(dest + stl, dat[n]->pattern2);
117 stl += strlen(dat[n]->pattern2);
118 i += l - 1;
119 change = 1;
120 } else dest[stl++] = word[i];
122 dest[stl] = '\0';
123 // fprintf(stderr, "i: %s o: %s change: %d\n", word, dest, change);
124 return change;