extensions/spellcheck/hunspell/src/replist.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/spellcheck/hunspell/src/replist.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     1.4 +/******* BEGIN LICENSE BLOCK *******
     1.5 + * Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.6 + * 
     1.7 + * The contents of this file are subject to the Mozilla Public License Version
     1.8 + * 1.1 (the "License"); you may not use this file except in compliance with
     1.9 + * the License. You may obtain a copy of the License at
    1.10 + * http://www.mozilla.org/MPL/
    1.11 + * 
    1.12 + * Software distributed under the License is distributed on an "AS IS" basis,
    1.13 + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.14 + * for the specific language governing rights and limitations under the
    1.15 + * License.
    1.16 + * 
    1.17 + * The Initial Developers of the Original Code are Kevin Hendricks (MySpell)
    1.18 + * and László Németh (Hunspell). Portions created by the Initial Developers
    1.19 + * are Copyright (C) 2002-2005 the Initial Developers. All Rights Reserved.
    1.20 + * 
    1.21 + * Contributor(s): László Németh (nemethl@gyorsposta.hu)
    1.22 + *                 Caolan McNamara (caolanm@redhat.com)
    1.23 + * 
    1.24 + * Alternatively, the contents of this file may be used under the terms of
    1.25 + * either the GNU General Public License Version 2 or later (the "GPL"), or
    1.26 + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.27 + * in which case the provisions of the GPL or the LGPL are applicable instead
    1.28 + * of those above. If you wish to allow use of your version of this file only
    1.29 + * under the terms of either the GPL or the LGPL, and not to allow others to
    1.30 + * use your version of this file under the terms of the MPL, indicate your
    1.31 + * decision by deleting the provisions above and replace them with the notice
    1.32 + * and other provisions required by the GPL or the LGPL. If you do not delete
    1.33 + * the provisions above, a recipient may use your version of this file under
    1.34 + * the terms of any one of the MPL, the GPL or the LGPL.
    1.35 + *
    1.36 + ******* END LICENSE BLOCK *******/
    1.37 +
    1.38 +#include <stdlib.h>
    1.39 +#include <string.h>
    1.40 +#include <stdio.h>
    1.41 +
    1.42 +#include "replist.hxx"
    1.43 +#include "csutil.hxx"
    1.44 +
    1.45 +RepList::RepList(int n) {
    1.46 +    dat = (replentry **) malloc(sizeof(replentry *) * n);
    1.47 +    if (dat == 0) size = 0; else size = n;
    1.48 +    pos = 0;
    1.49 +}
    1.50 +
    1.51 +RepList::~RepList()
    1.52 +{
    1.53 +    for (int i = 0; i < pos; i++) {
    1.54 +        free(dat[i]->pattern);
    1.55 +        free(dat[i]->pattern2);
    1.56 +        free(dat[i]);
    1.57 +    }
    1.58 +    free(dat);
    1.59 +}
    1.60 +
    1.61 +int RepList::get_pos() {
    1.62 +    return pos;
    1.63 +}
    1.64 +
    1.65 +replentry * RepList::item(int n) {
    1.66 +    return dat[n];
    1.67 +}
    1.68 +
    1.69 +int RepList::near(const char * word) {
    1.70 +    int p1 = 0;
    1.71 +    int p2 = pos;
    1.72 +    while ((p2 - p1) > 1) {
    1.73 +      int m = (p1 + p2) / 2;
    1.74 +      int c = strcmp(word, dat[m]->pattern);
    1.75 +      if (c <= 0) {
    1.76 +        if (c < 0) p2 = m; else p1 = p2 = m;
    1.77 +      } else p1 = m;
    1.78 +    }
    1.79 +    return p1;
    1.80 +}
    1.81 +
    1.82 +int RepList::match(const char * word, int n) {
    1.83 +    if (strncmp(word, dat[n]->pattern, strlen(dat[n]->pattern)) == 0) return strlen(dat[n]->pattern);
    1.84 +    return 0;
    1.85 +}
    1.86 +
    1.87 +int RepList::add(char * pat1, char * pat2) {
    1.88 +    if (pos >= size || pat1 == NULL || pat2 == NULL) return 1;
    1.89 +    replentry * r = (replentry *) malloc(sizeof(replentry));
    1.90 +    if (r == NULL) return 1;
    1.91 +    r->pattern = mystrrep(pat1, "_", " ");
    1.92 +    r->pattern2 = mystrrep(pat2, "_", " ");
    1.93 +    r->start = false;
    1.94 +    r->end = false;
    1.95 +    dat[pos++] = r;
    1.96 +    for (int i = pos - 1; i > 0; i--) {
    1.97 +      r = dat[i];
    1.98 +      if (strcmp(r->pattern, dat[i - 1]->pattern) < 0) {
    1.99 +          dat[i] = dat[i - 1];
   1.100 +          dat[i - 1] = r;
   1.101 +      } else break;
   1.102 +    }
   1.103 +    return 0;
   1.104 +}
   1.105 +
   1.106 +int RepList::conv(const char * word, char * dest) {
   1.107 +    int stl = 0;
   1.108 +    int change = 0;
   1.109 +    for (size_t i = 0; i < strlen(word); i++) {
   1.110 +        int n = near(word + i);
   1.111 +        int l = match(word + i, n);
   1.112 +        if (l) {
   1.113 +          strcpy(dest + stl, dat[n]->pattern2);
   1.114 +          stl += strlen(dat[n]->pattern2);
   1.115 +          i += l - 1;
   1.116 +          change = 1;
   1.117 +        } else dest[stl++] = word[i];
   1.118 +    }
   1.119 +    dest[stl] = '\0';
   1.120 +    return change;
   1.121 +}

mercurial