-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcre.jl
222 lines (186 loc) · 7.1 KB
/
pcre.jl
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
# This file is a part of Julia. License is MIT: https://julialang.org/license
## low-level pcre2 interface ##
module PCRE
import ..RefValue
include(string(length(Core.ARGS) >= 2 ? Core.ARGS[2] : "", "pcre_h.jl")) # include($BUILDROOT/base/pcre_h.jl)
const PCRE_LIB = "libpcre2-8"
function create_match_context()
JIT_STACK_START_SIZE = 32768
JIT_STACK_MAX_SIZE = 1048576
jit_stack = ccall((:pcre2_jit_stack_create_8, PCRE_LIB), Ptr{Cvoid},
(Cint, Cint, Ptr{Cvoid}),
JIT_STACK_START_SIZE, JIT_STACK_MAX_SIZE, C_NULL)
ctx = ccall((:pcre2_match_context_create_8, PCRE_LIB),
Ptr{Cvoid}, (Ptr{Cvoid},), C_NULL)
ccall((:pcre2_jit_stack_assign_8, PCRE_LIB), Cvoid,
(Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}), ctx, C_NULL, jit_stack)
return ctx
end
const THREAD_MATCH_CONTEXTS = Ptr{Cvoid}[C_NULL]
PCRE_COMPILE_LOCK = nothing
_tid() = Int(ccall(:jl_threadid, Int16, ())+1)
_nth() = Int(unsafe_load(cglobal(:jl_n_threads, Cint)))
function get_local_match_context()
tid = _tid()
ctx = @inbounds THREAD_MATCH_CONTEXTS[tid]
if ctx == C_NULL
@inbounds THREAD_MATCH_CONTEXTS[tid] = ctx = create_match_context()
end
return ctx
end
function __init__()
resize!(THREAD_MATCH_CONTEXTS, _nth())
fill!(THREAD_MATCH_CONTEXTS, C_NULL)
global PCRE_COMPILE_LOCK = Threads.SpinLock()
end
# supported options for different use cases
const COMPILE_MASK =
ANCHORED |
CASELESS |
DOLLAR_ENDONLY |
DOTALL |
ENDANCHORED |
EXTENDED |
FIRSTLINE |
MULTILINE |
NEWLINE_ANY |
NEWLINE_ANYCRLF |
NEWLINE_CR |
NEWLINE_CRLF |
NEWLINE_LF |
NO_AUTO_CAPTURE |
NO_START_OPTIMIZE |
NO_UTF_CHECK |
UNGREEDY |
UTF |
UCP
const EXECUTE_MASK =
NEWLINE_ANY |
NEWLINE_ANYCRLF |
NEWLINE_CR |
NEWLINE_CRLF |
NEWLINE_LF |
NOTBOL |
NOTEMPTY |
NOTEMPTY_ATSTART |
NOTEOL |
NO_START_OPTIMIZE |
NO_UTF_CHECK |
PARTIAL_HARD |
PARTIAL_SOFT
const OPTIONS_MASK = COMPILE_MASK | EXECUTE_MASK
const UNSET = ~Csize_t(0) # Indicates that an output vector element is unset
function info(regex::Ptr{Cvoid}, what::Integer, ::Type{T}) where T
buf = RefValue{T}()
ret = ccall((:pcre2_pattern_info_8, PCRE_LIB), Int32,
(Ptr{Cvoid}, Int32, Ptr{Cvoid}),
regex, what, buf) % UInt32
if ret != 0
error(ret == ERROR_NULL ? "NULL regex object" :
ret == ERROR_BADMAGIC ? "invalid regex object" :
ret == ERROR_BADOPTION ? "invalid option flags" :
"unknown error $ret")
end
buf[]
end
function ovec_length(match_data)
n = ccall((:pcre2_get_ovector_count_8, PCRE_LIB), UInt32,
(Ptr{Cvoid},), match_data)
return 2n
end
function ovec_ptr(match_data)
ptr = ccall((:pcre2_get_ovector_pointer_8, PCRE_LIB), Ptr{Csize_t},
(Ptr{Cvoid},), match_data)
return ptr
end
function compile(pattern::AbstractString, options::Integer)
errno = RefValue{Cint}(0)
erroff = RefValue{Csize_t}(0)
re_ptr = ccall((:pcre2_compile_8, PCRE_LIB), Ptr{Cvoid},
(Ptr{UInt8}, Csize_t, UInt32, Ref{Cint}, Ref{Csize_t}, Ptr{Cvoid}),
pattern, sizeof(pattern), options, errno, erroff, C_NULL)
re_ptr == C_NULL && error("PCRE compilation error: $(err_message(errno[])) at offset $(erroff[])")
re_ptr
end
function jit_compile(regex::Ptr{Cvoid})
errno = ccall((:pcre2_jit_compile_8, PCRE_LIB), Cint,
(Ptr{Cvoid}, UInt32), regex, JIT_COMPLETE) % UInt32
errno == 0 && return true
errno == ERROR_JIT_BADOPTION && return false
error("PCRE JIT error: $(err_message(errno))")
end
free_match_data(match_data) =
ccall((:pcre2_match_data_free_8, PCRE_LIB), Cvoid, (Ptr{Cvoid},), match_data)
free_re(re) =
ccall((:pcre2_code_free_8, PCRE_LIB), Cvoid, (Ptr{Cvoid},), re)
free_jit_stack(stack) =
ccall((:pcre2_jit_stack_free_8, PCRE_LIB), Cvoid, (Ptr{Cvoid},), stack)
free_match_context(context) =
ccall((:pcre2_match_context_free_8, PCRE_LIB), Cvoid, (Ptr{Cvoid},), context)
function err_message(errno)
buffer = Vector{UInt8}(undef, 256)
ccall((:pcre2_get_error_message_8, PCRE_LIB), Cvoid,
(Int32, Ptr{UInt8}, Csize_t), errno, buffer, sizeof(buffer))
GC.@preserve buffer unsafe_string(pointer(buffer))
end
function exec(re, subject, offset, options, match_data)
rc = ccall((:pcre2_match_8, PCRE_LIB), Cint,
(Ptr{Cvoid}, Ptr{UInt8}, Csize_t, Csize_t, Cuint, Ptr{Cvoid}, Ptr{Cvoid}),
re, subject, sizeof(subject), offset, options, match_data, get_local_match_context())
# rc == -1 means no match, -2 means partial match.
rc < -2 && error("PCRE.exec error: $(err_message(rc))")
rc >= 0
end
function exec_r(re, subject, offset, options)
match_data = create_match_data(re)
ans = exec(re, subject, offset, options, match_data)
free_match_data(match_data)
return ans
end
function exec_r_data(re, subject, offset, options)
match_data = create_match_data(re)
ans = exec(re, subject, offset, options, match_data)
return ans, match_data
end
function create_match_data(re)
ccall((:pcre2_match_data_create_from_pattern_8, PCRE_LIB),
Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}), re, C_NULL)
end
function substring_number_from_name(re, name)
ccall((:pcre2_substring_number_from_name_8, PCRE_LIB), Cint,
(Ptr{Cvoid}, Cstring), re, name)
end
function substring_length_bynumber(match_data, number)
s = RefValue{Csize_t}()
rc = ccall((:pcre2_substring_length_bynumber_8, PCRE_LIB), Cint,
(Ptr{Cvoid}, UInt32, Ref{Csize_t}), match_data, number, s)
rc < 0 && error("PCRE error: $(err_message(rc))")
convert(Int, s[])
end
function substring_copy_bynumber(match_data, number, buf, buf_size)
s = RefValue{Csize_t}(buf_size)
rc = ccall((:pcre2_substring_copy_bynumber_8, PCRE_LIB), Cint,
(Ptr{Cvoid}, UInt32, Ptr{UInt8}, Ref{Csize_t}),
match_data, number, buf, s)
rc < 0 && error("PCRE error: $(err_message(rc))")
convert(Int, s[])
end
function capture_names(re)
name_count = info(re, INFO_NAMECOUNT, UInt32)
name_entry_size = info(re, INFO_NAMEENTRYSIZE, UInt32)
nametable_ptr = info(re, INFO_NAMETABLE, Ptr{UInt8})
names = Dict{Int, String}()
for i=1:name_count
offset = (i-1)*name_entry_size + 1
# The capture group index corresponding to name 'i' is stored as a
# big-endian 16-bit value.
high_byte = UInt16(unsafe_load(nametable_ptr, offset))
low_byte = UInt16(unsafe_load(nametable_ptr, offset+1))
idx = (high_byte << 8) | low_byte
# The capture group name is a null-terminated string located directly
# after the index.
names[idx] = unsafe_string(nametable_ptr+offset+1)
end
names
end
end # module