Move some things around
This commit is contained in:
parent
48c29ac905
commit
0888b84126
1 changed files with 60 additions and 59 deletions
119
src/nre.nim
119
src/nre.nim
|
|
@ -6,64 +6,7 @@ from strutils import toLower, `%`
|
||||||
from math import ceil
|
from math import ceil
|
||||||
import optional_t
|
import optional_t
|
||||||
|
|
||||||
# PCRE Options {{{
|
# Type definitions {{{
|
||||||
|
|
||||||
let Options: Table[string, int] = {
|
|
||||||
"8" : pcre.UTF8,
|
|
||||||
"9" : pcre.NEVER_UTF,
|
|
||||||
"?" : pcre.NO_UTF8_CHECK,
|
|
||||||
"A" : pcre.ANCHORED,
|
|
||||||
# "C" : pcre.AUTO_CALLOUT, unsuported XXX
|
|
||||||
"E" : pcre.DOLLAR_ENDONLY,
|
|
||||||
"f" : pcre.FIRSTLINE,
|
|
||||||
"i" : pcre.CASELESS,
|
|
||||||
"m" : pcre.MULTILINE,
|
|
||||||
"N" : pcre.NO_AUTO_CAPTURE,
|
|
||||||
"O" : pcre.NO_AUTO_POSSESS,
|
|
||||||
"s" : pcre.DOTALL,
|
|
||||||
"U" : pcre.UNGREEDY,
|
|
||||||
"W" : pcre.UCP,
|
|
||||||
"X" : pcre.EXTRA,
|
|
||||||
"x" : pcre.EXTENDED,
|
|
||||||
"Y" : pcre.NO_START_OPTIMIZE,
|
|
||||||
|
|
||||||
"any" : pcre.NEWLINE_ANY,
|
|
||||||
"anycrlf" : pcre.NEWLINE_ANYCRLF,
|
|
||||||
"cr" : pcre.NEWLINE_CR,
|
|
||||||
"crlf" : pcre.NEWLINE_CRLF,
|
|
||||||
"lf" : pcre.NEWLINE_LF,
|
|
||||||
"bsr_anycrlf" : pcre.BSR_ANYCRLF,
|
|
||||||
"bsr_unicode" : pcre.BSR_UNICODE,
|
|
||||||
"js" : pcre.JAVASCRIPT_COMPAT,
|
|
||||||
}.toTable
|
|
||||||
|
|
||||||
proc tokenizeOptions(opts: string): tuple[flags: int, study: bool] =
|
|
||||||
result = (0, false)
|
|
||||||
|
|
||||||
var longOpt: string = nil
|
|
||||||
for i, c in opts:
|
|
||||||
# Handle long options {{{
|
|
||||||
if c == '<':
|
|
||||||
longOpt = ""
|
|
||||||
continue
|
|
||||||
|
|
||||||
if longOpt != nil:
|
|
||||||
if c == '>':
|
|
||||||
result.flags = result.flags or Options.fget(longOpt)
|
|
||||||
longOpt = nil
|
|
||||||
else:
|
|
||||||
longOpt.add(c.toLower)
|
|
||||||
continue
|
|
||||||
# }}}
|
|
||||||
|
|
||||||
if c == 'S': # handle study
|
|
||||||
result.study = true
|
|
||||||
continue
|
|
||||||
|
|
||||||
result.flags = result.flags or Options.fget($c)
|
|
||||||
|
|
||||||
# }}}
|
|
||||||
|
|
||||||
type
|
type
|
||||||
Regex* = ref object
|
Regex* = ref object
|
||||||
pattern: string # not nil
|
pattern: string # not nil
|
||||||
|
|
@ -88,6 +31,7 @@ type
|
||||||
pattern*: string ## the pattern that caused the problem
|
pattern*: string ## the pattern that caused the problem
|
||||||
|
|
||||||
StudyError* = ref object of Exception
|
StudyError* = ref object of Exception
|
||||||
|
# }}}
|
||||||
|
|
||||||
proc getinfo[T](self: Regex, opt: cint): T =
|
proc getinfo[T](self: Regex, opt: cint): T =
|
||||||
let retcode = pcre.fullinfo(self.pcreObj, self.pcreExtra, opt, addr result)
|
let retcode = pcre.fullinfo(self.pcreObj, self.pcreExtra, opt, addr result)
|
||||||
|
|
@ -161,13 +105,70 @@ proc `[]`*(self: Captures, name: string): string =
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# Creation & Destruction {{{
|
# Creation & Destruction {{{
|
||||||
|
# PCRE Options {{{
|
||||||
|
let Options: Table[string, int] = {
|
||||||
|
"8" : pcre.UTF8,
|
||||||
|
"9" : pcre.NEVER_UTF,
|
||||||
|
"?" : pcre.NO_UTF8_CHECK,
|
||||||
|
"A" : pcre.ANCHORED,
|
||||||
|
# "C" : pcre.AUTO_CALLOUT, unsuported XXX
|
||||||
|
"E" : pcre.DOLLAR_ENDONLY,
|
||||||
|
"f" : pcre.FIRSTLINE,
|
||||||
|
"i" : pcre.CASELESS,
|
||||||
|
"m" : pcre.MULTILINE,
|
||||||
|
"N" : pcre.NO_AUTO_CAPTURE,
|
||||||
|
"O" : pcre.NO_AUTO_POSSESS,
|
||||||
|
"s" : pcre.DOTALL,
|
||||||
|
"U" : pcre.UNGREEDY,
|
||||||
|
"W" : pcre.UCP,
|
||||||
|
"X" : pcre.EXTRA,
|
||||||
|
"x" : pcre.EXTENDED,
|
||||||
|
"Y" : pcre.NO_START_OPTIMIZE,
|
||||||
|
|
||||||
|
"any" : pcre.NEWLINE_ANY,
|
||||||
|
"anycrlf" : pcre.NEWLINE_ANYCRLF,
|
||||||
|
"cr" : pcre.NEWLINE_CR,
|
||||||
|
"crlf" : pcre.NEWLINE_CRLF,
|
||||||
|
"lf" : pcre.NEWLINE_LF,
|
||||||
|
"bsr_anycrlf" : pcre.BSR_ANYCRLF,
|
||||||
|
"bsr_unicode" : pcre.BSR_UNICODE,
|
||||||
|
"js" : pcre.JAVASCRIPT_COMPAT,
|
||||||
|
}.toTable
|
||||||
|
|
||||||
|
proc tokenizeOptions(opts: string): tuple[flags: int, study: bool] =
|
||||||
|
result = (0, false)
|
||||||
|
|
||||||
|
var longOpt: string = nil
|
||||||
|
for i, c in opts:
|
||||||
|
# Handle long options {{{
|
||||||
|
if c == '<':
|
||||||
|
longOpt = ""
|
||||||
|
continue
|
||||||
|
|
||||||
|
if longOpt != nil:
|
||||||
|
if c == '>':
|
||||||
|
result.flags = result.flags or Options.fget(longOpt)
|
||||||
|
longOpt = nil
|
||||||
|
else:
|
||||||
|
longOpt.add(c.toLower)
|
||||||
|
continue
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
if c == 'S': # handle study
|
||||||
|
result.study = true
|
||||||
|
continue
|
||||||
|
|
||||||
|
result.flags = result.flags or Options.fget($c)
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
type UncheckedArray {.unchecked.}[T] = array[0 .. 0, T]
|
||||||
|
|
||||||
proc destroyRegex(self: Regex) =
|
proc destroyRegex(self: Regex) =
|
||||||
pcre.free_substring(cast[cstring](self.pcreObj))
|
pcre.free_substring(cast[cstring](self.pcreObj))
|
||||||
self.pcreObj = nil
|
self.pcreObj = nil
|
||||||
if self.pcreExtra != nil:
|
if self.pcreExtra != nil:
|
||||||
pcre.free_study(self.pcreExtra)
|
pcre.free_study(self.pcreExtra)
|
||||||
|
|
||||||
type UncheckedArray {.unchecked.}[T] = array[0 .. 0, T]
|
|
||||||
proc getNameToNumberTable(self: Regex): Table[string, int] =
|
proc getNameToNumberTable(self: Regex): Table[string, int] =
|
||||||
let entryCount = getinfo[cint](self, pcre.INFO_NAMECOUNT)
|
let entryCount = getinfo[cint](self, pcre.INFO_NAMECOUNT)
|
||||||
let entrySize = getinfo[cint](self, pcre.INFO_NAMEENTRYSIZE)
|
let entrySize = getinfo[cint](self, pcre.INFO_NAMEENTRYSIZE)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue