1226035Sgabor/* $FreeBSD$ */
2226035Sgabor
3226035Sgabor/*-
4226035Sgabor * Copyright (C) 2011 Gabor Kovesdan <gabor@FreeBSD.org>
5226035Sgabor * All rights reserved.
6226035Sgabor *
7226035Sgabor * Redistribution and use in source and binary forms, with or without
8226035Sgabor * modification, are permitted provided that the following conditions
9226035Sgabor * are met:
10226035Sgabor * 1. Redistributions of source code must retain the above copyright
11226035Sgabor *    notice, this list of conditions and the following disclaimer.
12226035Sgabor * 2. Redistributions in binary form must reproduce the above copyright
13226035Sgabor *    notice, this list of conditions and the following disclaimer in the
14226035Sgabor *    documentation and/or other materials provided with the distribution.
15226035Sgabor *
16226035Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17226035Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18226035Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19226035Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20226035Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21226035Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22226035Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23226035Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24226035Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25226035Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26226035Sgabor * SUCH DAMAGE.
27226035Sgabor */
28226035Sgabor
29226035Sgabor#include "glue.h"
30226035Sgabor
31226035Sgabor#include <errno.h>
32226035Sgabor#include <fastmatch.h>
33226035Sgabor#include <regex.h>
34226035Sgabor#include <string.h>
35226035Sgabor
36226035Sgabor#include "tre-fastmatch.h"
37226035Sgabor#include "xmalloc.h"
38226035Sgabor
39226035Sgaborint
40226035Sgabortre_fixncomp(fastmatch_t *preg, const char *regex, size_t n, int cflags)
41226035Sgabor{
42226035Sgabor  int ret;
43226035Sgabor  tre_char_t *wregex;
44226035Sgabor  size_t wlen;
45226035Sgabor
46226035Sgabor  if (n != 0)
47226035Sgabor    {
48226035Sgabor      ret = tre_convert_pattern(regex, n, &wregex, &wlen);
49226035Sgabor      if (ret != REG_OK)
50226035Sgabor	return ret;
51226035Sgabor      else
52226035Sgabor	ret = tre_compile_literal(preg, wregex, wlen, cflags);
53226035Sgabor      tre_free_pattern(wregex);
54226035Sgabor      return ret;
55226035Sgabor    }
56226035Sgabor  else
57226035Sgabor    return tre_compile_literal(preg, NULL, 0, cflags);
58226035Sgabor}
59226035Sgabor
60226035Sgaborint
61226035Sgabortre_fastncomp(fastmatch_t *preg, const char *regex, size_t n, int cflags)
62226035Sgabor{
63226035Sgabor  int ret;
64226035Sgabor  tre_char_t *wregex;
65226035Sgabor  size_t wlen;
66226035Sgabor
67226035Sgabor  if (n != 0)
68226035Sgabor    {
69226035Sgabor      ret = tre_convert_pattern(regex, n, &wregex, &wlen);
70226035Sgabor      if (ret != REG_OK)
71226035Sgabor	return ret;
72226035Sgabor      else
73226035Sgabor	ret = (cflags & REG_LITERAL)
74226035Sgabor	      ? tre_compile_literal(preg, wregex, wlen, cflags)
75226035Sgabor	      : tre_compile_fast(preg, wregex, wlen, cflags);
76226035Sgabor      tre_free_pattern(wregex);
77226035Sgabor      return ret;
78226035Sgabor    }
79226035Sgabor  else
80226035Sgabor    return tre_compile_literal(preg, NULL, 0, cflags);
81226035Sgabor}
82226035Sgabor
83226035Sgabor
84226035Sgaborint
85226035Sgabortre_fixcomp(fastmatch_t *preg, const char *regex, int cflags)
86226035Sgabor{
87226035Sgabor  return tre_fixncomp(preg, regex, regex ? strlen(regex) : 0, cflags);
88226035Sgabor}
89226035Sgabor
90226035Sgaborint
91226035Sgabortre_fastcomp(fastmatch_t *preg, const char *regex, int cflags)
92226035Sgabor{
93226035Sgabor  return tre_fastncomp(preg, regex, regex ? strlen(regex) : 0, cflags);
94226035Sgabor}
95226035Sgabor
96226035Sgaborint
97226035Sgabortre_fixwncomp(fastmatch_t *preg, const wchar_t *regex, size_t n, int cflags)
98226035Sgabor{
99226035Sgabor  return tre_compile_literal(preg, regex, n, cflags);
100226035Sgabor}
101226035Sgabor
102226035Sgaborint
103226035Sgabortre_fastwncomp(fastmatch_t *preg, const wchar_t *regex, size_t n, int cflags)
104226035Sgabor{
105226035Sgabor  return (cflags & REG_LITERAL) ?
106226035Sgabor    tre_compile_literal(preg, regex, n, cflags) :
107226035Sgabor    tre_compile_fast(preg, regex, n, cflags);
108226035Sgabor}
109226035Sgabor
110226035Sgaborint
111226035Sgabortre_fixwcomp(fastmatch_t *preg, const wchar_t *regex, int cflags)
112226035Sgabor{
113226035Sgabor  return tre_fixwncomp(preg, regex, regex ? tre_strlen(regex) : 0, cflags);
114226035Sgabor}
115226035Sgabor
116226035Sgaborint
117226035Sgabortre_fastwcomp(fastmatch_t *preg, const wchar_t *regex, int cflags)
118226035Sgabor{
119226035Sgabor  return tre_fastwncomp(preg, regex, regex ? tre_strlen(regex) : 0, cflags);
120226035Sgabor}
121226035Sgabor
122226035Sgaborvoid
123226035Sgabortre_fastfree(fastmatch_t *preg)
124226035Sgabor{
125226035Sgabor  tre_free_fast(preg);
126226035Sgabor}
127226035Sgabor
128226035Sgaborint
129226035Sgabortre_fastnexec(const fastmatch_t *preg, const char *string, size_t len,
130226035Sgabor         size_t nmatch, regmatch_t pmatch[], int eflags)
131226035Sgabor{
132226035Sgabor  tre_str_type_t type = (TRE_MB_CUR_MAX == 1) ? STR_BYTE : STR_MBS;
133226035Sgabor
134226035Sgabor  if (eflags & REG_STARTEND)
135226035Sgabor    CALL_WITH_OFFSET(tre_match_fast(preg, &string[offset], slen,
136226035Sgabor		     type, nmatch, pmatch, eflags));
137226035Sgabor  else
138226035Sgabor    return tre_match_fast(preg, string, len, type, nmatch,
139226035Sgabor      pmatch, eflags);
140226035Sgabor}
141226035Sgabor
142226035Sgaborint
143226035Sgabortre_fastexec(const fastmatch_t *preg, const char *string, size_t nmatch,
144226035Sgabor	     regmatch_t pmatch[], int eflags)
145226035Sgabor{
146226035Sgabor  return tre_fastnexec(preg, string, (size_t)-1, nmatch, pmatch, eflags);
147226035Sgabor}
148226035Sgabor
149226035Sgaborint
150226035Sgabortre_fastwnexec(const fastmatch_t *preg, const wchar_t *string, size_t len,
151226035Sgabor          size_t nmatch, regmatch_t pmatch[], int eflags)
152226035Sgabor{
153226035Sgabor  tre_str_type_t type = STR_WIDE;
154226035Sgabor
155226035Sgabor  if (eflags & REG_STARTEND)
156226035Sgabor    CALL_WITH_OFFSET(tre_match_fast(preg, &string[offset], slen,
157226035Sgabor		     type, nmatch, pmatch, eflags));
158226035Sgabor  else
159226035Sgabor    return tre_match_fast(preg, string, len, type, nmatch,
160226035Sgabor      pmatch, eflags);
161226035Sgabor}
162226035Sgabor
163226035Sgaborint
164226035Sgabortre_fastwexec(const fastmatch_t *preg, const wchar_t *string,
165226035Sgabor         size_t nmatch, regmatch_t pmatch[], int eflags)
166226035Sgabor{
167226035Sgabor  return tre_fastwnexec(preg, string, (size_t)-1, nmatch, pmatch, eflags);
168226035Sgabor}
169226035Sgabor
170