This commit is contained in:
ringabout
2024-11-05 14:14:22 +08:00
parent cb802af44e
commit 27fc4fedb5
3 changed files with 15 additions and 26 deletions

View File

@@ -272,7 +272,7 @@ proc initRegex(pattern: string, flags: csize_t, options: uint32): Regex =
errorCode: cint = 0
errOffset: csize_t = 0
result.pcreObj = pcre2.compile(cast[ptr uint8](cstring(pattern)),
result.pcreObj = pcre2.compile(cstring(pattern),
flags, options, addr(errorCode),
addr(errOffset), nil)
if result.pcreObj == nil:
@@ -508,7 +508,7 @@ proc matchImpl(str: string, pattern: Regex, start, endpos: int, options: uint32)
var matchData = pcre2.match_data_create_from_pattern(pattern.pcreObj, nil)
defer: pcre2.match_data_free(matchData)
let execRet = pcre2.match(pattern.pcreObj,
cast[ptr uint8](cstring(str)),
cstring(str),
csize_t(strlen),
csize_t(start),
options,

View File

@@ -83,7 +83,7 @@ proc rawCompile(pattern: string, flags: csize_t, options: uint32): ptr Pcre =
var
errorCode: cint = 0
offset: csize_t = 0
result = pcre2.compile(cast[ptr uint8](pattern.cstring), flags, options, addr(errorCode), addr(offset), nil)
result = pcre2.compile(pattern.cstring, flags, options, addr(errorCode), addr(offset), nil)
if result == nil:
raiseInvalidRegex($errorCode & "\n" & pattern & "\n" & spaces(offset) & "^\n")
@@ -146,7 +146,7 @@ proc matchOrFind(buf: cstring, pattern: Regex, matches: var openArray[string],
rawMatches = rtarray.getRawData
var matchData = match_data_create_from_pattern(pattern.h, nil)
defer: match_data_free(matchData)
var res = pcre2.match(pattern.h, cast[ptr uint8](buf), bufSize.csize_t, start.csize_t, options,
var res = pcre2.match(pattern.h, buf, bufSize.csize_t, start.csize_t, options,
matchData, nil)
rawMatches = cast[ptr UncheckedArray[csize_t]](get_ovector_pointer(matchData))
if res < 0: return res
@@ -177,7 +177,7 @@ proc findBounds*(buf: cstring, pattern: Regex, matches: var openArray[string],
rawMatches = rtarray.getRawData
var matchData = match_data_create_from_pattern(pattern.h, nil)
defer: match_data_free(matchData)
var res = pcre2.match(pattern.h, cast[ptr uint8](buf), bufSize.csize_t, start.csize_t, 0'u32,
var res = pcre2.match(pattern.h, buf, bufSize.csize_t, start.csize_t, 0'u32,
matchData, nil)
rawMatches = cast[ptr UncheckedArray[csize_t]](get_ovector_pointer(matchData))
if res < 0: return (-1, 0)
@@ -220,7 +220,7 @@ proc findBounds*(buf: cstring, pattern: Regex,
rawMatches = rtarray.getRawData
var matchData = match_data_create_from_pattern(pattern.h, nil)
defer: match_data_free(matchData)
var res = pcre2.match(pattern.h, cast[ptr uint8](buf), bufSize.csize_t, start.csize_t, 0'u32,
var res = pcre2.match(pattern.h, buf, bufSize.csize_t, start.csize_t, 0'u32,
matchData, nil)
rawMatches = cast[ptr UncheckedArray[csize_t]](get_ovector_pointer(matchData))
if res < 0'i32: return (-1, 0)
@@ -255,7 +255,7 @@ proc findBoundsImpl(buf: cstring, pattern: Regex,
var rawMatches = rtarray.getRawData
var matchData = match_data_create_from_pattern(pattern.h, nil)
defer: match_data_free(matchData)
var res = pcre2.match(pattern.h, cast[ptr uint8](buf), bufSize.csize_t, start.csize_t, options,
var res = pcre2.match(pattern.h, buf, bufSize.csize_t, start.csize_t, options,
matchData, nil)
rawMatches = cast[ptr UncheckedArray[csize_t]](get_ovector_pointer(matchData))
if res < 0'i32:
@@ -273,7 +273,7 @@ proc findBounds*(buf: cstring, pattern: Regex,
var rawMatches = rtarray.getRawData
var matchData = match_data_create_from_pattern(pattern.h, nil)
defer: match_data_free(matchData)
var res = pcre2.match(pattern.h, cast[ptr uint8](buf), bufSize.csize_t, start.csize_t, 0'u32,
var res = pcre2.match(pattern.h, buf, bufSize.csize_t, start.csize_t, 0'u32,
matchData, nil)
rawMatches = cast[ptr UncheckedArray[csize_t]](get_ovector_pointer(matchData))
if res < 0'i32: return (int(res), 0)
@@ -296,7 +296,7 @@ proc matchOrFind(buf: cstring, pattern: Regex, start, bufSize: int, options: uin
rawMatches = rtarray.getRawData
var matchData = match_data_create_from_pattern(pattern.h, nil)
defer: match_data_free(matchData)
result = pcre2.match(pattern.h, cast[ptr uint8](buf), bufSize.csize_t, start.csize_t, options,
result = pcre2.match(pattern.h, buf, bufSize.csize_t, start.csize_t, options,
matchData, nil)
rawMatches = cast[ptr UncheckedArray[csize_t]](get_ovector_pointer(matchData))
@@ -381,7 +381,7 @@ proc find*(buf: cstring, pattern: Regex, matches: var openArray[string],
rawMatches = rtarray.getRawData
var matchData = match_data_create_from_pattern(pattern.h, nil)
defer: match_data_free(matchData)
var res = pcre2.match(pattern.h, cast[ptr uint8](buf), bufSize.csize_t, start.csize_t, 0'u32,
var res = pcre2.match(pattern.h, buf, bufSize.csize_t, start.csize_t, 0'u32,
matchData, nil)
rawMatches = cast[ptr UncheckedArray[csize_t]](get_ovector_pointer(matchData))
if res < 0'i32: return res
@@ -410,7 +410,7 @@ proc find*(buf: cstring, pattern: Regex, start = 0, bufSize: int): int =
rawMatches = rtarray.getRawData
var matchData = match_data_create_from_pattern(pattern.h, nil)
defer: match_data_free(matchData)
var res = pcre2.match(pattern.h, cast[ptr uint8](buf), bufSize.csize_t, start.csize_t, 0'u32,
var res = pcre2.match(pattern.h, buf, bufSize.csize_t, start.csize_t, 0'u32,
matchData, nil)
rawMatches = cast[ptr UncheckedArray[csize_t]](get_ovector_pointer(matchData))
if res < 0'i32: return res
@@ -441,7 +441,7 @@ iterator findAll*(s: string, pattern: Regex, start = 0): string =
var matchData = match_data_create_from_pattern(pattern.h, nil)
defer: match_data_free(matchData)
while true:
let res = pcre2.match(pattern.h, cast[ptr uint8](s.cstring), len(s).csize_t, i.csize_t, 0'u32,
let res = pcre2.match(pattern.h, s.cstring, len(s).csize_t, i.csize_t, 0'u32,
matchData, nil)
rawMatches = cast[ptr UncheckedArray[csize_t]](get_ovector_pointer(matchData))
if res < 0'i32: break
@@ -463,7 +463,7 @@ iterator findAll*(buf: cstring, pattern: Regex, start = 0, bufSize: int): string
var matchData = match_data_create_from_pattern(pattern.h, nil)
defer: match_data_free(matchData)
while true:
let res = pcre2.match(pattern.h, cast[ptr uint8](buf), bufSize.csize_t, i.csize_t, 0'u32,
let res = pcre2.match(pattern.h, buf, bufSize.csize_t, i.csize_t, 0'u32,
matchData, nil)
rawMatches = cast[ptr UncheckedArray[csize_t]](get_ovector_pointer(matchData))
if res < 0'i32: break

View File

@@ -533,7 +533,7 @@ else:
# Exported PCRE functions
proc compile*(pattern: ptr uint8,
proc compile*(pattern: cstring,
options: csize_t,
flags: uint32,
errorCode: ptr cint,
@@ -576,24 +576,13 @@ proc dfa_match*(code: ptr Pcre,
wscount: cint): cint
proc match*(code: ptr Pcre,
subject: ptr uint8,
subject: cstring,
length: csize_t,
startoffset: csize_t,
options: uint32,
ovector: ptr MatchData,
ovecsize: pointer): cint
proc match*(code: ptr Pcre,
subject: cstring,
length: cint,
startoffset: cint,
options: cint,
ovector: ptr MatchData,
ovecsize: cint): cint =
result = match(code, cast[ptr uint8](subject), csize_t length, csize_t startoffset,
uint32 options,
ovector, nil)
proc match_data_create*(size: uint32, ctx: ptr GeneralContext): ptr MatchData
proc match_data_create_from_pattern*(