forked from fstab/grok_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oniguruma.go
205 lines (184 loc) · 5.86 KB
/
oniguruma.go
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
// Copyright 2016 The grok_exporter Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package exporter
/*
#cgo CFLAGS: -I/usr/local/include
#cgo LDFLAGS: -L/usr/local/lib -lonig
#include <stdlib.h>
#include <string.h>
#include <oniguruma.h>
#include "oniguruma_helper.h"
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
var (
// See the #define statements in oniguruma.h
ONIG_ENCODING_UTF8 = &C.OnigEncodingUTF8
)
type OnigurumaLib struct {
encoding C.OnigEncoding
}
type OnigurumaRegexp struct {
regex C.OnigRegex
cachedCaptureGroupNums map[string][]C.int
}
type OnigurumaMatchResult struct {
match bool
regex *OnigurumaRegexp
region *C.OnigRegion
input string
}
// Warning: The Oniguruma library is not thread save, it should be used in a single thread.
func InitOnigurumaLib() (*OnigurumaLib, error) {
result := &OnigurumaLib{
encoding: ONIG_ENCODING_UTF8, // TODO: This is the encoding of the logfile. Should be configurable and default to the system encoding.
}
encodings := []C.OnigEncoding{result.encoding}
ret := C.oniguruma_helper_initialize(&encodings[0], C.int(len(encodings)))
if ret != 0 {
return nil, errors.New("failed to initialize encoding for the Oniguruma regular expression library.")
}
return result, nil
}
func (o *OnigurumaLib) Version() string {
return C.GoString(C.onig_version())
}
func (o *OnigurumaLib) Compile(pattern string) (*OnigurumaRegexp, error) {
result := &OnigurumaRegexp{
cachedCaptureGroupNums: make(map[string][]C.int),
}
patternStart, patternEnd := pointers(pattern)
defer free(patternStart, patternEnd)
var errorInfo C.OnigErrorInfo
r := C.onig_new(&result.regex, patternStart, patternEnd, C.ONIG_OPTION_DEFAULT, o.encoding, C.ONIG_SYNTAX_DEFAULT, &errorInfo)
if r != C.ONIG_NORMAL {
return nil, errors.New(errMsgWithInfo(r, &errorInfo))
}
return result, nil
}
func (regex *OnigurumaRegexp) Free() {
C.onig_free(regex.regex)
}
func (regex *OnigurumaRegexp) HasCaptureGroup(name string) bool {
_, err := regex.getCaptureGroupNums(name)
return err == nil
}
func (r *OnigurumaRegexp) getCaptureGroupNums(name string) ([]C.int, error) {
cached, ok := r.cachedCaptureGroupNums[name]
if ok {
return cached, nil
}
nameStart, nameEnd := pointers(name)
defer free(nameStart, nameEnd)
var groupNums *C.int
n := C.onig_name_to_group_numbers(r.regex, nameStart, nameEnd, &groupNums)
if n <= 0 {
return nil, fmt.Errorf("%v: no such capture group in pattern", name)
}
result := make([]C.int, 0, int(n))
for i := 0; i < int(n); i++ {
result = append(result, getPos(groupNums, C.int(i)))
}
r.cachedCaptureGroupNums[name] = result
return result, nil
}
func (regex *OnigurumaRegexp) Match(input string) (*OnigurumaMatchResult, error) {
region := C.onig_region_new()
inputStart, inputEnd := pointers(input)
defer free(inputStart, inputEnd)
r := C.onig_match(regex.regex, inputStart, inputEnd, inputStart, region, C.ONIG_OPTION_NONE)
if r == C.ONIG_MISMATCH {
C.onig_region_free(region, 1)
return &OnigurumaMatchResult{
match: false,
}, nil
} else if r < 0 {
C.onig_region_free(region, 1)
return nil, errors.New(errMsg(r))
} else {
return &OnigurumaMatchResult{
match: true,
regex: regex,
region: region,
input: input,
}, nil
}
}
func (m *OnigurumaMatchResult) Get(name string) (string, error) {
if !m.match {
return "", nil // no match -> no capture group
}
groupNums, err := m.regex.getCaptureGroupNums(name)
if err != nil {
return "", err
}
for _, groupNum := range groupNums {
beg := getPos(m.region.beg, groupNum)
end := getPos(m.region.end, groupNum)
if beg > end || beg < 0 || int(end) > len(m.input) {
return "", fmt.Errorf("%v: unexpected result when calling onig_name_to_group_numbers()", name)
} else if beg == end {
continue // return empty string unless there are other matches for that name.
} else {
return m.input[beg:end], nil
}
}
return "", nil
}
func (m *OnigurumaMatchResult) IsMatch() bool {
return m.match
}
func (m *OnigurumaMatchResult) Free() {
if m.match {
C.onig_region_free(m.region, 1)
}
}
// returns a pointer to the start of the string and a pointer to the end of the string
func pointers(s string) (start, end *C.OnigUChar) {
start = (*C.OnigUChar)(unsafe.Pointer(C.CString(s)))
end = (*C.OnigUChar)(unsafe.Pointer(uintptr(unsafe.Pointer(start)) + uintptr(len(s))))
return
}
// returns p[i]
func getPos(p *C.int, i C.int) C.int {
return *(*C.int)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + uintptr(i)*unsafe.Sizeof(C.int(0))))
}
// free the pointers returned by pointers()
func free(start *C.OnigUChar, end *C.OnigUChar) {
// The memset call is for debugging: We zero the content, such that the program crashes if the content was used after calling free()
C.memset(unsafe.Pointer(start), C.int(0), C.size_t(uintptr(unsafe.Pointer(end))-uintptr(unsafe.Pointer(start))))
C.free(unsafe.Pointer(start))
}
func errMsgWithInfo(returnCode C.int, errorInfo *C.OnigErrorInfo) string {
msg := make([]byte, C.ONIG_MAX_ERROR_MESSAGE_LEN)
l := C.oniguruma_helper_error_code_with_info_to_str((*C.UChar)(&msg[0]), returnCode, errorInfo)
if l <= 0 {
return "unknown error"
} else {
return string(msg[:l])
}
}
func errMsg(returnCode C.int) string {
msg := make([]byte, C.ONIG_MAX_ERROR_MESSAGE_LEN)
l := C.oniguruma_helper_error_code_to_str((*C.UChar)(&msg[0]), returnCode)
if l <= 0 {
return "unknown error"
} else {
return string(msg[:l])
}
}