summaryrefslogtreecommitdiffstats
path: root/usr.bin/grep/regex/fastmatch.c
blob: 6e3e3f8055a9721f3667be536434be3d5c8d10d3 (plain)
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
/* $FreeBSD$ */

/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (C) 2011 Gabor Kovesdan <gabor@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "glue.h"

#include <errno.h>
#include <fastmatch.h>
#include <regex.h>
#include <string.h>

#include "tre-fastmatch.h"

int
tre_fixncomp(fastmatch_t *preg, const char *regex, size_t n, int cflags)
{
  int ret;
  tre_char_t *wregex;
  size_t wlen;

  if (n != 0)
    {
      ret = tre_convert_pattern(regex, n, &wregex, &wlen);
      if (ret != REG_OK)
	return ret;
      else 
	ret = tre_compile_literal(preg, wregex, wlen, cflags);
      tre_free_pattern(wregex);
      return ret;
    }
  else
    return tre_compile_literal(preg, NULL, 0, cflags);
}

int
tre_fastncomp(fastmatch_t *preg, const char *regex, size_t n, int cflags)
{
  int ret;
  tre_char_t *wregex;
  size_t wlen;

  if (n != 0)
    {
      ret = tre_convert_pattern(regex, n, &wregex, &wlen);
      if (ret != REG_OK)
	return ret;
      else
	ret = (cflags & REG_LITERAL)
	      ? tre_compile_literal(preg, wregex, wlen, cflags)
	      : tre_compile_fast(preg, wregex, wlen, cflags);
      tre_free_pattern(wregex);
      return ret;
    }
  else
    return tre_compile_literal(preg, NULL, 0, cflags);
}


int
tre_fixcomp(fastmatch_t *preg, const char *regex, int cflags)
{
  return tre_fixncomp(preg, regex, regex ? strlen(regex) : 0, cflags);
}

int
tre_fastcomp(fastmatch_t *preg, const char *regex, int cflags)
{
  return tre_fastncomp(preg, regex, regex ? strlen(regex) : 0, cflags);
}

int
tre_fixwncomp(fastmatch_t *preg, const wchar_t *regex, size_t n, int cflags)
{
  return tre_compile_literal(preg, regex, n, cflags);
}

int
tre_fastwncomp(fastmatch_t *preg, const wchar_t *regex, size_t n, int cflags)
{
  return (cflags & REG_LITERAL) ?
    tre_compile_literal(preg, regex, n, cflags) :
    tre_compile_fast(preg, regex, n, cflags);
}

int
tre_fixwcomp(fastmatch_t *preg, const wchar_t *regex, int cflags)
{
  return tre_fixwncomp(preg, regex, regex ? tre_strlen(regex) : 0, cflags);
}

int
tre_fastwcomp(fastmatch_t *preg, const wchar_t *regex, int cflags)
{
  return tre_fastwncomp(preg, regex, regex ? tre_strlen(regex) : 0, cflags);
}

void
tre_fastfree(fastmatch_t *preg)
{
  tre_free_fast(preg);
}

int
tre_fastnexec(const fastmatch_t *preg, const char *string, size_t len,
         size_t nmatch, regmatch_t pmatch[], int eflags)
{
  tre_str_type_t type = (TRE_MB_CUR_MAX == 1) ? STR_BYTE : STR_MBS;

  if (eflags & REG_STARTEND)
    CALL_WITH_OFFSET(tre_match_fast(preg, &string[offset], slen,
		     type, nmatch, pmatch, eflags));
  else
    return tre_match_fast(preg, string, len, type, nmatch,
      pmatch, eflags);
}

int
tre_fastexec(const fastmatch_t *preg, const char *string, size_t nmatch,
	     regmatch_t pmatch[], int eflags)
{
  return tre_fastnexec(preg, string, (size_t)-1, nmatch, pmatch, eflags);
}

int
tre_fastwnexec(const fastmatch_t *preg, const wchar_t *string, size_t len,
          size_t nmatch, regmatch_t pmatch[], int eflags)
{
  tre_str_type_t type = STR_WIDE;

  if (eflags & REG_STARTEND)
    CALL_WITH_OFFSET(tre_match_fast(preg, &string[offset], slen,
		     type, nmatch, pmatch, eflags));
  else
    return tre_match_fast(preg, string, len, type, nmatch,
      pmatch, eflags);
}

int
tre_fastwexec(const fastmatch_t *preg, const wchar_t *string,
         size_t nmatch, regmatch_t pmatch[], int eflags)
{
  return tre_fastwnexec(preg, string, (size_t)-1, nmatch, pmatch, eflags);
}

OpenPOWER on IntegriCloud