@@ -6,15 +6,16 @@ module PCRE
6
6
7
7
import .. RefValue
8
8
9
- include (string (length (Core. ARGS ) >= 2 ? Core. ARGS [2 ] : " " , " pcre_h.jl" )) # include($BUILDROOT/base/pcre_h.jl)
9
+ # include($BUILDROOT/base/pcre_h.jl)
10
+ include (string (length (Core. ARGS ) >= 2 ? Core. ARGS [2 ] : " " , " pcre_h.jl" ))
10
11
11
12
const PCRE_LIB = " libpcre2-8"
12
13
13
14
function create_match_context ()
14
15
JIT_STACK_START_SIZE = 32768
15
16
JIT_STACK_MAX_SIZE = 1048576
16
17
jit_stack = ccall ((:pcre2_jit_stack_create_8 , PCRE_LIB), Ptr{Cvoid},
17
- (Cint, Cint , Ptr{Cvoid}),
18
+ (Csize_t, Csize_t , Ptr{Cvoid}),
18
19
JIT_STACK_START_SIZE, JIT_STACK_MAX_SIZE, C_NULL )
19
20
ctx = ccall ((:pcre2_match_context_create_8 , PCRE_LIB),
20
21
Ptr{Cvoid}, (Ptr{Cvoid},), C_NULL )
@@ -90,22 +91,22 @@ const UNSET = ~Csize_t(0) # Indicates that an output vector element is unset
90
91
91
92
function info (regex:: Ptr{Cvoid} , what:: Integer , :: Type{T} ) where T
92
93
buf = RefValue {T} ()
93
- ret = ccall ((:pcre2_pattern_info_8 , PCRE_LIB), Int32 ,
94
- (Ptr{Cvoid}, Int32 , Ptr{Cvoid}),
95
- regex, what, buf) % UInt32
94
+ ret = ccall ((:pcre2_pattern_info_8 , PCRE_LIB), Cint ,
95
+ (Ptr{Cvoid}, UInt32 , Ptr{Cvoid}),
96
+ regex, what, buf)
96
97
if ret != 0
97
- error (ret == ERROR_NULL ? " NULL regex object" :
98
- ret == ERROR_BADMAGIC ? " invalid regex object" :
99
- ret == ERROR_BADOPTION ? " invalid option flags" :
100
- " unknown error $ret " )
98
+ error (ret == ERROR_NULL ? " PCRE error: NULL regex object" :
99
+ ret == ERROR_BADMAGIC ? " PCRE error: invalid regex object" :
100
+ ret == ERROR_BADOPTION ? " PCRE error: invalid option flags" :
101
+ " PCRE error: unknown error ( $ret ) " )
101
102
end
102
- buf[]
103
+ return buf[]
103
104
end
104
105
105
106
function ovec_length (match_data)
106
107
n = ccall ((:pcre2_get_ovector_count_8 , PCRE_LIB), UInt32,
107
108
(Ptr{Cvoid},), match_data)
108
- return 2 n
109
+ return 2 Int (n)
109
110
end
110
111
111
112
function ovec_ptr (match_data)
@@ -115,18 +116,23 @@ function ovec_ptr(match_data)
115
116
end
116
117
117
118
function compile (pattern:: AbstractString , options:: Integer )
119
+ if ! (pattern isa Union{String,SubString{String}})
120
+ pattern = String (pattern)
121
+ end
118
122
errno = RefValue {Cint} (0 )
119
123
erroff = RefValue {Csize_t} (0 )
120
124
re_ptr = ccall ((:pcre2_compile_8 , PCRE_LIB), Ptr{Cvoid},
121
125
(Ptr{UInt8}, Csize_t, UInt32, Ref{Cint}, Ref{Csize_t}, Ptr{Cvoid}),
122
- pattern, sizeof (pattern), options, errno, erroff, C_NULL )
123
- re_ptr == C_NULL && error (" PCRE compilation error: $(err_message (errno[])) at offset $(erroff[]) " )
124
- re_ptr
126
+ pattern, ncodeunits (pattern), options, errno, erroff, C_NULL )
127
+ if re_ptr == C_NULL
128
+ error (" PCRE compilation error: $(err_message (errno[])) at offset $(erroff[]) " )
129
+ end
130
+ return re_ptr
125
131
end
126
132
127
133
function jit_compile (regex:: Ptr{Cvoid} )
128
134
errno = ccall ((:pcre2_jit_compile_8 , PCRE_LIB), Cint,
129
- (Ptr{Cvoid}, UInt32), regex, JIT_COMPLETE) % UInt32
135
+ (Ptr{Cvoid}, UInt32), regex, JIT_COMPLETE)
130
136
errno == 0 && return true
131
137
errno == ERROR_JIT_BADOPTION && return false
132
138
error (" PCRE JIT error: $(err_message (errno)) " )
@@ -144,20 +150,25 @@ free_jit_stack(stack) =
144
150
free_match_context (context) =
145
151
ccall ((:pcre2_match_context_free_8 , PCRE_LIB), Cvoid, (Ptr{Cvoid},), context)
146
152
147
- function err_message (errno)
148
- buffer = Vector {UInt8} (undef, 256 )
149
- ccall ((:pcre2_get_error_message_8 , PCRE_LIB), Cvoid,
150
- (UInt32, Ptr{UInt8}, Csize_t), errno, buffer, sizeof (buffer))
151
- GC. @preserve buffer unsafe_string (pointer (buffer))
153
+ function err_message (errno:: Integer )
154
+ buffer = Vector {UInt8} (undef, 1024 )
155
+ ret = ccall ((:pcre2_get_error_message_8 , PCRE_LIB), Cint,
156
+ (Cint, Ptr{UInt8}, Csize_t), errno, buffer, length (buffer))
157
+ ret == ERROR_BADDATA && error (" PCRE error: invalid errno ($errno )" )
158
+ # TODO : seems like there should be a better way to get this string
159
+ return GC. @preserve buffer unsafe_string (pointer (buffer))
152
160
end
153
161
154
162
function exec (re, subject, offset, options, match_data)
163
+ if ! (subject isa Union{String,SubString{String}})
164
+ subject = String (subject)
165
+ end
155
166
rc = ccall ((:pcre2_match_8 , PCRE_LIB), Cint,
156
- (Ptr{Cvoid}, Ptr{UInt8}, Csize_t, Csize_t, Cuint , Ptr{Cvoid}, Ptr{Cvoid}),
157
- re, subject, sizeof (subject), offset, options, match_data, get_local_match_context ())
167
+ (Ptr{Cvoid}, Ptr{UInt8}, Csize_t, Csize_t, UInt32 , Ptr{Cvoid}, Ptr{Cvoid}),
168
+ re, subject, ncodeunits (subject), offset, options, match_data, get_local_match_context ())
158
169
# rc == -1 means no match, -2 means partial match.
159
170
rc < - 2 && error (" PCRE.exec error: $(err_message (rc)) " )
160
- rc >= 0
171
+ return rc >= 0
161
172
end
162
173
163
174
function exec_r (re, subject, offset, options)
@@ -174,21 +185,25 @@ function exec_r_data(re, subject, offset, options)
174
185
end
175
186
176
187
function create_match_data (re)
177
- ccall ((:pcre2_match_data_create_from_pattern_8 , PCRE_LIB),
178
- Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}), re, C_NULL )
188
+ p = ccall ((:pcre2_match_data_create_from_pattern_8 , PCRE_LIB),
189
+ Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}), re, C_NULL )
190
+ p == C_NULL && error (" PCRE error: could not allocate memory" )
191
+ return p
179
192
end
180
193
181
194
function substring_number_from_name (re, name)
182
- ccall ((:pcre2_substring_number_from_name_8 , PCRE_LIB), Cint,
183
- (Ptr{Cvoid}, Cstring), re, name)
195
+ n = ccall ((:pcre2_substring_number_from_name_8 , PCRE_LIB), Cint,
196
+ (Ptr{Cvoid}, Cstring), re, name)
197
+ n < 0 && error (" PCRE error: $(err_message (n)) " )
198
+ return Int (n)
184
199
end
185
200
186
201
function substring_length_bynumber (match_data, number)
187
202
s = RefValue {Csize_t} ()
188
203
rc = ccall ((:pcre2_substring_length_bynumber_8 , PCRE_LIB), Cint,
189
- (Ptr{Cvoid}, UInt32 , Ref{Csize_t}), match_data, number, s)
204
+ (Ptr{Cvoid}, Cint , Ref{Csize_t}), match_data, number, s)
190
205
rc < 0 && error (" PCRE error: $(err_message (rc)) " )
191
- convert (Int, s[])
206
+ return Int ( s[])
192
207
end
193
208
194
209
function substring_copy_bynumber (match_data, number, buf, buf_size)
@@ -197,15 +212,15 @@ function substring_copy_bynumber(match_data, number, buf, buf_size)
197
212
(Ptr{Cvoid}, UInt32, Ptr{UInt8}, Ref{Csize_t}),
198
213
match_data, number, buf, s)
199
214
rc < 0 && error (" PCRE error: $(err_message (rc)) " )
200
- convert (Int, s[])
215
+ return Int ( s[])
201
216
end
202
217
203
218
function capture_names (re)
204
219
name_count = info (re, INFO_NAMECOUNT, UInt32)
205
220
name_entry_size = info (re, INFO_NAMEENTRYSIZE, UInt32)
206
221
nametable_ptr = info (re, INFO_NAMETABLE, Ptr{UInt8})
207
- names = Dict {Int, String} ()
208
- for i= 1 : name_count
222
+ names = Dict {Int,String} ()
223
+ for i = 1 : name_count
209
224
offset = (i- 1 )* name_entry_size + 1
210
225
# The capture group index corresponding to name 'i' is stored as a
211
226
# big-endian 16-bit value.
@@ -216,7 +231,7 @@ function capture_names(re)
216
231
# after the index.
217
232
names[idx] = unsafe_string (nametable_ptr+ offset+ 1 )
218
233
end
219
- names
234
+ return names
220
235
end
221
236
222
237
end # module
0 commit comments