diff options
author | Flaviu Tamas <tamasflaviu@gmail.com> | 2015-01-10 16:41:55 -0500 |
---|---|---|
committer | Flaviu Tamas <tamasflaviu@gmail.com> | 2015-01-10 16:41:55 -0500 |
commit | 0888b84126dae6526a3dedc5c996033eb7d3a418 (patch) | |
tree | 1480cd41618e52def5991127ade69d0a23d25dde /src/nre.nim | |
parent | 48c29ac9052b4ade187c51063693c3ed2ec27855 (diff) | |
download | Nim-0888b84126dae6526a3dedc5c996033eb7d3a418.tar.gz |
Move some things around
Diffstat (limited to 'src/nre.nim')
-rw-r--r-- | src/nre.nim | 119 |
1 files changed, 60 insertions, 59 deletions
diff --git a/src/nre.nim b/src/nre.nim index e457306d6..5a5cbaa45 100644 --- a/src/nre.nim +++ b/src/nre.nim @@ -6,64 +6,7 @@ from strutils import toLower, `%` from math import ceil import optional_t -# 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 definitions {{{ type Regex* = ref object pattern: string # not nil @@ -88,6 +31,7 @@ type pattern*: string ## the pattern that caused the problem StudyError* = ref object of Exception +# }}} proc getinfo[T](self: Regex, opt: cint): T = let retcode = pcre.fullinfo(self.pcreObj, self.pcreExtra, opt, addr result) @@ -161,13 +105,70 @@ proc `[]`*(self: Captures, name: string): string = # }}} # 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) = pcre.free_substring(cast[cstring](self.pcreObj)) self.pcreObj = nil if self.pcreExtra != nil: pcre.free_study(self.pcreExtra) -type UncheckedArray {.unchecked.}[T] = array[0 .. 0, T] proc getNameToNumberTable(self: Regex): Table[string, int] = let entryCount = getinfo[cint](self, pcre.INFO_NAMECOUNT) let entrySize = getinfo[cint](self, pcre.INFO_NAMEENTRYSIZE) |