Change to options module

This commit is contained in:
Flaviu Tamas 2015-05-26 18:18:11 -04:00
commit 9576bc087d
6 changed files with 25 additions and 24 deletions

View file

@ -5,7 +5,7 @@ import unsigned
from future import lc, `[]` from future import lc, `[]`
from strutils import toLower, `%` from strutils import toLower, `%`
from math import ceil from math import ceil
import optional_t import options
from unicode import runeLenAt from unicode import runeLenAt
@ -115,9 +115,7 @@ type
RegexMatch* = object RegexMatch* = object
## Usually seen as Option[RegexMatch], it represents the result of an ## Usually seen as Option[RegexMatch], it represents the result of an
## execution. On failure, it is ``None[RegexMatch]``, but if you want ## execution. On failure, it is none, on success, it is some.
## automated derefrence, import ``optional_t.nonstrict``. The available
## fields are as follows:
## ##
## ``pattern: Regex`` ## ``pattern: Regex``
## the pattern that is being matched ## the pattern that is being matched
@ -235,15 +233,15 @@ proc `[]`*(pattern: CaptureBounds, i: int): Option[Slice[int]] =
let pattern = RegexMatch(pattern) let pattern = RegexMatch(pattern)
if pattern.pcreMatchBounds[i + 1].a != -1: if pattern.pcreMatchBounds[i + 1].a != -1:
let bounds = pattern.pcreMatchBounds[i + 1] let bounds = pattern.pcreMatchBounds[i + 1]
return Some(int(bounds.a) .. int(bounds.b-1)) return some(int(bounds.a) .. int(bounds.b-1))
else: else:
return None[Slice[int]]() return none(Slice[int])
proc `[]`*(pattern: Captures, i: int): string = proc `[]`*(pattern: Captures, i: int): string =
let pattern = RegexMatch(pattern) let pattern = RegexMatch(pattern)
let bounds = pattern.captureBounds[i] let bounds = pattern.captureBounds[i]
if bounds: if bounds.isSome:
let bounds = bounds.get let bounds = bounds.get
return pattern.str.substr(bounds.a, bounds.b) return pattern.str.substr(bounds.a, bounds.b)
else: else:
@ -275,7 +273,7 @@ proc toTable*(pattern: Captures, default: string = nil): Table[string, string] =
result = initTable[string, string]() result = initTable[string, string]()
toTableImpl(nextVal == nil) toTableImpl(nextVal == nil)
proc toTable*(pattern: CaptureBounds, default = None[Slice[int]]()): proc toTable*(pattern: CaptureBounds, default = none(Slice[int])):
Table[string, Option[Slice[int]]] = Table[string, Option[Slice[int]]] =
result = initTable[string, Option[Slice[int]]]() result = initTable[string, Option[Slice[int]]]()
toTableImpl(nextVal.isNone) toTableImpl(nextVal.isNone)
@ -288,13 +286,13 @@ template itemsImpl(cond: bool): stmt {.immediate, dirty.} =
else: else:
yield nextVal yield nextVal
iterator items*(pattern: CaptureBounds, default = None[Slice[int]]()): Option[Slice[int]] = iterator items*(pattern: CaptureBounds, default = none(Slice[int])): Option[Slice[int]] =
itemsImpl(nextVal.isNone) itemsImpl(nextVal.isNone)
iterator items*(pattern: Captures, default: string = nil): string = iterator items*(pattern: Captures, default: string = nil): string =
itemsImpl(nextVal == nil) itemsImpl(nextVal == nil)
proc toSeq*(pattern: CaptureBounds, default = None[Slice[int]]()): seq[Option[Slice[int]]] = proc toSeq*(pattern: CaptureBounds, default = none(Slice[int])): seq[Option[Slice[int]]] =
accumulateResult(pattern.items(default)) accumulateResult(pattern.items(default))
proc toSeq*(pattern: Captures, default: string = nil): seq[string] = proc toSeq*(pattern: Captures, default: string = nil): seq[string] =
@ -454,11 +452,11 @@ proc matchImpl(str: string, pattern: Regex, start, endpos: int, flags: int): Opt
cast[ptr cint](addr myResult.pcreMatchBounds[0]), cast[ptr cint](addr myResult.pcreMatchBounds[0]),
cint(vecsize)) cint(vecsize))
if execRet >= 0: if execRet >= 0:
return Some(myResult) return some(myResult)
case execRet: case execRet:
of pcre.ERROR_NOMATCH: of pcre.ERROR_NOMATCH:
return None[RegexMatch]() return none(RegexMatch)
of pcre.ERROR_NULL: of pcre.ERROR_NULL:
raise newException(AccessViolationError, "Expected non-null parameters") raise newException(AccessViolationError, "Expected non-null parameters")
of pcre.ERROR_BADOPTION: of pcre.ERROR_BADOPTION:
@ -497,7 +495,7 @@ iterator findIter*(str: string, pattern: Regex, start = 0, endpos = int.high): R
while true: while true:
var flags = 0 var flags = 0
if match and if match.isSome and
match.get.matchBounds.a > match.get.matchBounds.b: match.get.matchBounds.a > match.get.matchBounds.b:
# 0-len match # 0-len match
flags = pcre.NOTEMPTY_ATSTART flags = pcre.NOTEMPTY_ATSTART

View file

@ -1,4 +1,4 @@
import unittest, optional_t.nonstrict import unittest, optional_nonstrict
include nre include nre
suite "captures": suite "captures":
@ -31,8 +31,8 @@ suite "captures":
test "named capture bounds": test "named capture bounds":
let ex1 = "foo".find(re("(?<foo>foo)(?<bar>bar)?")) let ex1 = "foo".find(re("(?<foo>foo)(?<bar>bar)?"))
check(ex1.captureBounds["foo"] == Some(0..2)) check(ex1.captureBounds["foo"] == some(0..2))
check(ex1.captureBounds["bar"] == None[Slice[int]]()) check(ex1.captureBounds["bar"] == none(Slice[int]))
test "capture count": test "capture count":
let ex1 = re("(?<foo>foo)(?<bar>bar)?") let ex1 = re("(?<foo>foo)(?<bar>bar)?")
@ -42,7 +42,7 @@ suite "captures":
test "named capture table": test "named capture table":
let ex1 = "foo".find(re("(?<foo>foo)(?<bar>bar)?")) let ex1 = "foo".find(re("(?<foo>foo)(?<bar>bar)?"))
check(ex1.captures.toTable == {"foo" : "foo", "bar" : nil}.toTable()) check(ex1.captures.toTable == {"foo" : "foo", "bar" : nil}.toTable())
check(ex1.captureBounds.toTable == {"foo" : Some(0..2), "bar" : None[Slice[int]]()}.toTable()) check(ex1.captureBounds.toTable == {"foo" : some(0..2), "bar" : none(Slice[int])}.toTable())
check(ex1.captures.toTable("") == {"foo" : "foo", "bar" : ""}.toTable()) check(ex1.captures.toTable("") == {"foo" : "foo", "bar" : ""}.toTable())
let ex2 = "foobar".find(re("(?<foo>foo)(?<bar>bar)?")) let ex2 = "foobar".find(re("(?<foo>foo)(?<bar>bar)?"))
@ -51,7 +51,7 @@ suite "captures":
test "capture sequence": test "capture sequence":
let ex1 = "foo".find(re("(?<foo>foo)(?<bar>bar)?")) let ex1 = "foo".find(re("(?<foo>foo)(?<bar>bar)?"))
check(ex1.captures.toSeq == @["foo", nil]) check(ex1.captures.toSeq == @["foo", nil])
check(ex1.captureBounds.toSeq == @[Some(0..2), None[Slice[int]]()]) check(ex1.captureBounds.toSeq == @[some(0..2), none(Slice[int])])
check(ex1.captures.toSeq("") == @["foo", ""]) check(ex1.captures.toSeq("") == @["foo", ""])
let ex2 = "foobar".find(re("(?<foo>foo)(?<bar>bar)?")) let ex2 = "foobar".find(re("(?<foo>foo)(?<bar>bar)?"))

View file

@ -1,4 +1,4 @@
import unittest, sequtils, nre, optional_t.nonstrict import unittest, sequtils, nre, optional_nonstrict
suite "find": suite "find":
test "find text": test "find text":

View file

@ -1,10 +1,10 @@
include nre, unittest, optional_t.nonstrict include nre, unittest, optional_nonstrict
suite "match": suite "match":
test "upper bound must be inclusive": test "upper bound must be inclusive":
check("abc".match(re"abc", endpos = -1) == None[RegexMatch]()) check("abc".match(re"abc", endpos = -1) == none(RegexMatch))
check("abc".match(re"abc", endpos = 1) == None[RegexMatch]()) check("abc".match(re"abc", endpos = 1) == none(RegexMatch))
check("abc".match(re"abc", endpos = 2) != None[RegexMatch]()) check("abc".match(re"abc", endpos = 2) != none(RegexMatch))
test "match examples": test "match examples":
check("abc".match(re"(\w)").captures[0] == "a") check("abc".match(re"(\w)").captures[0] == "a")

View file

@ -1,4 +1,4 @@
import unittest, nre, strutils, optional_t.nonstrict import unittest, nre, strutils, optional_nonstrict
suite "Misc tests": suite "Misc tests":
test "unicode": test "unicode":

View file

@ -0,0 +1,3 @@
import options
converter option2val*[T](val: Option[T]): T =
return val.get()