File: EST_Regex.cc

package info (click to toggle)
speech-tools 1%3A1.2.3-8
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,784 kB
  • ctags: 9,792
  • sloc: cpp: 65,444; ansic: 12,081; java: 3,748; sh: 3,466; makefile: 1,138; lisp: 703; perl: 396; awk: 82; xml: 9
file content (338 lines) | stat: -rw-r--r-- 9,149 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
 /************************************************************************/
 /*                                                                      */
 /*                Centre for Speech Technology Research                 */
 /*                     University of Edinburgh, UK                      */
 /*                        Copyright (c) 1997                            */
 /*                        All Rights Reserved.                          */
 /*                                                                      */
 /*  Permission is hereby granted, free of charge, to use and distribute */
 /*  this software and its documentation without restriction, including  */
 /*  without limitation the rights to use, copy, modify, merge, publish, */
 /*  distribute, sublicense, and/or sell copies of this work, and to     */
 /*  permit persons to whom this work is furnished to do so, subject to  */
 /*  the following conditions:                                           */
 /*   1. The code must retain the above copyright notice, this list of   */
 /*      conditions and the following disclaimer.                        */
 /*   2. Any modifications must be clearly marked as such.               */
 /*   3. Original authors' names are not deleted.                        */
 /*   4. The authors' names are not used to endorse or promote products  */
 /*      derived from this software without specific prior written       */
 /*      permission.                                                     */
 /*                                                                      */
 /*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK       */
 /*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING     */
 /*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT  */
 /*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE    */
 /*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   */
 /*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  */
 /*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,         */
 /*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF      */
 /*  THIS SOFTWARE.                                                      */
 /*                                                                      */
 /************************************************************************/
 /*	       Author : Richard Caley (rjc@cstr.ed.ac.uk)		 */
 /*                 Date  : February 1997                                */
 /* -------------------------------------------------------------------- */
 /*                                                                      */
 /* A Regular expression class to go with the CSTR EST_String class. Uses*/
 /* Henry Spencer`s regexp routines which allocate space dynamically     */
 /* using malloc, so we use free in here rather than wfree because       */
 /* wfree might at some time start doing something more than just be a   */
 /* safe wrapper around free. If you try and use another regexp          */
 /* package, beware of changes to how memory is allocated.               */
 /*                                                                      */
 /* We maintain two compiled versions, one for substring matches and     */
 /* one for whole string matches (because sometimes the regexp           */
 /* compiler can special case the latter). These are compiled when       */
 /* first used.                                                          */
 /*                                                                      */
 /************************************************************************/

#ifdef HAVE_CONFIG_H
#    include "est_string_config.h"
#endif

#ifdef NO_EST
#    include <unistd.h>
#else
#    include "EST_unix.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "EST_String.h"
#include "EST_Regex.h"

#ifdef sun
#ifndef __svr4__
/* SunOS */
#include <string.h>
#endif
#endif

// extern "C" {
#include "regexp.h"

/*
void *t_regcomp(void *v)
{
  return v;
}

void *cpp_regcomp(void *v)
{
  return v;
}
*/
// #define wfree(P) (1==1)

// These define the different escape conventions for the FSF's
// regexp code and Henry Spencer's

static const char *fsf_magic="^$*+?[].\\";
static const char *fsf_magic_backslashed="()|<>";
static const char *spencer_magic="^$*+?[].()|\\\n";
static const char *spencer_magic_backslashed="<>";

EST_Regex RXwhite("[ \n\t\r]+");
EST_Regex RXalpha("[A-Za-z]+");
EST_Regex RXlowercase("[a-z]+");
EST_Regex RXuppercase("[A-Z]+");
EST_Regex RXalphanum("[0-9A-Za-z]+");
EST_Regex RXidentifier("[A-Za-z_][0-9A-Za-z_]+");
EST_Regex RXint("-?[0-9]+");
EST_Regex RXdouble("-?\\(\\([0-9]+\\.[0-9]*\\)\\|\\([0-9]+\\)\\|\\(\\.[0-9]+\\)\\)\\([eE][---+]?[0-9]+\\)?");

// use this to free compiled regex since the regexp package uses malloc
// and walloc might end up doing something clever.

/* extern "C" void free(void *p); */

#if NSUBEXP != EST_Regex_max_subexpressions
#   error "EST_Regex_max_subexpressions must be equal to  NSUBEXP" 
#endif

EST_Regex::EST_Regex(void) : EST_String()
{
  compiled = NULL;
  compiled_match = NULL;
}

EST_Regex::EST_Regex(const char *s) : EST_String(s)

{

  compiled = NULL;
  compiled_match = NULL;

}

EST_Regex::EST_Regex(EST_String s) : EST_String(s)

{
  compiled = NULL;
  compiled_match = NULL;
}

EST_Regex::EST_Regex(const EST_Regex &ex) : EST_String(ex)
{
  compiled = NULL;
  compiled_match = NULL;
}


EST_Regex::~EST_Regex()
{
    if (compiled_match)
      free(compiled_match);
    if (compiled)
      free(compiled);
}

// Convert a regular expression from the external syntax (defined by the
// the FSF library) to the one expected by the regexp routines (which
// say it's V8 syntax).

char *EST_Regex::regularize(int match) const
{
  char *reg = walloc(char, size()*2+3);
  char *r=reg;
  const char *e;
  int magic=0,last_was_bs=0;
  const char * in_brackets=NULL;
  const char *ex = (size()==0)?"":str();

  if (match && *ex != '^')
    *(r++) = '^';

  for(e=ex; *e ; e++)
    {
     if (*e == '\\' && !last_was_bs)
       {
	 last_was_bs=1;
	 continue;
       }

     magic=strchr((last_was_bs?fsf_magic_backslashed:fsf_magic), *e)!=NULL;

     if (in_brackets)
       {
	 *(r++) = *e;
	 if (*e  == ']' && (e-in_brackets)>1)
	   in_brackets=0;
       }
     else if (magic)
       {
	 if (strchr(spencer_magic_backslashed, *e))
	   *(r++) = '\\';

	 *(r++) = *e;
	 if (*e  == '[')
	   in_brackets=e;
       }
     else 
       {
	 if (strchr(spencer_magic, *e))
	     *(r++) = '\\';

	 *(r++) = *e;
       }
     last_was_bs=0;
    }
  
  if (match && (e==ex || *(e-1) != '$'))
    {
      if (last_was_bs)
	*(r++) = '\\';
      *(r++) = '$';
    }

  *r='\0';

  //  cerr<<"reg||"<<ex<<"||"<<reg<<"\n";
  
  return reg;
}

void EST_Regex::compile()
{
  if (!compiled)
    {
      char *reg=regularize(0);
      void * t =(void *)hs_regcomp(reg);
      compiled=t;
      wfree(reg);
    }

  if (!compiled)
    cerr << "EST_Regex: can't compile '" << str() << "'\n";
}

void EST_Regex::compile_match()
{
  if (!compiled_match)
    {
      char *reg=regularize(1);

      void * t =(void *)hs_regcomp(reg);
      compiled_match=t;
      wfree(reg);
    }

  if (!compiled_match)
      cerr << "EST_Regex: can't compile '" << str() << "'\n";
}

int EST_Regex::run(const char *on, int from, int &start, int &end, int *starts, int *ends) 
{

  compile();

  if (compiled && from <= (int)strlen(on))
    {
      if (hs_regexec((hs_regexp *)compiled, on+from))
	{
	  hs_regexp *re = (hs_regexp *)compiled;

	  start = re->startp[0] - on;
	  end   = re->endp[0]- on;

	  if (starts)
	    {
	      int i;
	      for (i=0; i<EST_Regex_max_subexpressions; i++)
		starts[i] = re->startp[i]?(re->startp[i] - on):-1;
	    }
	  if (ends)
	    {
	      int i;
	      for (i=0; i<EST_Regex_max_subexpressions; i++)
		  ends[i] = re->endp[i]?(re->endp[i] - on):-1;
	    }

	  return 1;
	}
    }
  return 0;
}

int EST_Regex::run_match(const char *on, int from, int *starts, int *ends) 
{

  compile_match();

  hs_regexp *re = (hs_regexp *)compiled_match;

  if (compiled_match && from <= (int)strlen(on))
    if (hs_regexec(re, on+from))
      {
	  if (starts)
	    {
	      int i;
	      for (i=0; i<EST_Regex_max_subexpressions; i++)
		starts[i] = re->startp[i]?(re->startp[i] - on):-1;
	    }
	  if (ends)
	    {
	      int i;
	      for (i=0; i<EST_Regex_max_subexpressions; i++)
		ends[i] = re->endp[i]?(re->endp[i] - on):-1;
	    }
	  return 1;
      }

  return 0;
}

EST_Regex &EST_Regex::operator = (const EST_Regex ex)
{
  ((EST_String &)(*this)) = (EST_String)ex;
  compiled = NULL;
  compiled_match = NULL;

  return *this;
}

EST_Regex &EST_Regex::operator = (const EST_String s)
{
  ((EST_String &)(*this)) = s;
  compiled = NULL;
  compiled_match = NULL;

  return *this;
}

EST_Regex &EST_Regex::operator = (const char *s)
{
  ((EST_String &)(*this)) = s;
  compiled = NULL;
  compiled_match = NULL;

  return *this;
}

ostream &operator << (ostream &s, const EST_Regex &str)
{
  return s << (EST_String)str;
}