]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.bin/grep/regex/fastmatch.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.bin / grep / regex / fastmatch.c
1 /* $FreeBSD$ */
2
3 /*-
4  * Copyright (C) 2011 Gabor Kovesdan <gabor@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include "glue.h"
30
31 #include <errno.h>
32 #include <fastmatch.h>
33 #include <regex.h>
34 #include <string.h>
35
36 #include "tre-fastmatch.h"
37 #include "xmalloc.h"
38
39 int
40 tre_fixncomp(fastmatch_t *preg, const char *regex, size_t n, int cflags)
41 {
42   int ret;
43   tre_char_t *wregex;
44   size_t wlen;
45
46   if (n != 0)
47     {
48       ret = tre_convert_pattern(regex, n, &wregex, &wlen);
49       if (ret != REG_OK)
50         return ret;
51       else 
52         ret = tre_compile_literal(preg, wregex, wlen, cflags);
53       tre_free_pattern(wregex);
54       return ret;
55     }
56   else
57     return tre_compile_literal(preg, NULL, 0, cflags);
58 }
59
60 int
61 tre_fastncomp(fastmatch_t *preg, const char *regex, size_t n, int cflags)
62 {
63   int ret;
64   tre_char_t *wregex;
65   size_t wlen;
66
67   if (n != 0)
68     {
69       ret = tre_convert_pattern(regex, n, &wregex, &wlen);
70       if (ret != REG_OK)
71         return ret;
72       else
73         ret = (cflags & REG_LITERAL)
74               ? tre_compile_literal(preg, wregex, wlen, cflags)
75               : tre_compile_fast(preg, wregex, wlen, cflags);
76       tre_free_pattern(wregex);
77       return ret;
78     }
79   else
80     return tre_compile_literal(preg, NULL, 0, cflags);
81 }
82
83
84 int
85 tre_fixcomp(fastmatch_t *preg, const char *regex, int cflags)
86 {
87   return tre_fixncomp(preg, regex, regex ? strlen(regex) : 0, cflags);
88 }
89
90 int
91 tre_fastcomp(fastmatch_t *preg, const char *regex, int cflags)
92 {
93   return tre_fastncomp(preg, regex, regex ? strlen(regex) : 0, cflags);
94 }
95
96 int
97 tre_fixwncomp(fastmatch_t *preg, const wchar_t *regex, size_t n, int cflags)
98 {
99   return tre_compile_literal(preg, regex, n, cflags);
100 }
101
102 int
103 tre_fastwncomp(fastmatch_t *preg, const wchar_t *regex, size_t n, int cflags)
104 {
105   return (cflags & REG_LITERAL) ?
106     tre_compile_literal(preg, regex, n, cflags) :
107     tre_compile_fast(preg, regex, n, cflags);
108 }
109
110 int
111 tre_fixwcomp(fastmatch_t *preg, const wchar_t *regex, int cflags)
112 {
113   return tre_fixwncomp(preg, regex, regex ? tre_strlen(regex) : 0, cflags);
114 }
115
116 int
117 tre_fastwcomp(fastmatch_t *preg, const wchar_t *regex, int cflags)
118 {
119   return tre_fastwncomp(preg, regex, regex ? tre_strlen(regex) : 0, cflags);
120 }
121
122 void
123 tre_fastfree(fastmatch_t *preg)
124 {
125   tre_free_fast(preg);
126 }
127
128 int
129 tre_fastnexec(const fastmatch_t *preg, const char *string, size_t len,
130          size_t nmatch, regmatch_t pmatch[], int eflags)
131 {
132   tre_str_type_t type = (TRE_MB_CUR_MAX == 1) ? STR_BYTE : STR_MBS;
133
134   if (eflags & REG_STARTEND)
135     CALL_WITH_OFFSET(tre_match_fast(preg, &string[offset], slen,
136                      type, nmatch, pmatch, eflags));
137   else
138     return tre_match_fast(preg, string, len, type, nmatch,
139       pmatch, eflags);
140 }
141
142 int
143 tre_fastexec(const fastmatch_t *preg, const char *string, size_t nmatch,
144              regmatch_t pmatch[], int eflags)
145 {
146   return tre_fastnexec(preg, string, (size_t)-1, nmatch, pmatch, eflags);
147 }
148
149 int
150 tre_fastwnexec(const fastmatch_t *preg, const wchar_t *string, size_t len,
151           size_t nmatch, regmatch_t pmatch[], int eflags)
152 {
153   tre_str_type_t type = STR_WIDE;
154
155   if (eflags & REG_STARTEND)
156     CALL_WITH_OFFSET(tre_match_fast(preg, &string[offset], slen,
157                      type, nmatch, pmatch, eflags));
158   else
159     return tre_match_fast(preg, string, len, type, nmatch,
160       pmatch, eflags);
161 }
162
163 int
164 tre_fastwexec(const fastmatch_t *preg, const wchar_t *string,
165          size_t nmatch, regmatch_t pmatch[], int eflags)
166 {
167   return tre_fastwnexec(preg, string, (size_t)-1, nmatch, pmatch, eflags);
168 }
169