From 9576bc087d20e8e5cacb9cd92c635d3f9efaf0a7 Mon Sep 17 00:00:00 2001 From: Flaviu Tamas Date: Tue, 26 May 2015 18:18:11 -0400 Subject: [PATCH] Change to options module --- src/nre.nim | 24 +++++++++++------------- test/captures.nim | 10 +++++----- test/find.nim | 2 +- test/match.nim | 8 ++++---- test/misc.nim | 2 +- test/optional_nonstrict.nim | 3 +++ 6 files changed, 25 insertions(+), 24 deletions(-) create mode 100644 test/optional_nonstrict.nim diff --git a/src/nre.nim b/src/nre.nim index f96820b2c2..2706398ee0 100644 --- a/src/nre.nim +++ b/src/nre.nim @@ -5,7 +5,7 @@ import unsigned from future import lc, `[]` from strutils import toLower, `%` from math import ceil -import optional_t +import options from unicode import runeLenAt @@ -115,9 +115,7 @@ type RegexMatch* = object ## Usually seen as Option[RegexMatch], it represents the result of an - ## execution. On failure, it is ``None[RegexMatch]``, but if you want - ## automated derefrence, import ``optional_t.nonstrict``. The available - ## fields are as follows: + ## execution. On failure, it is none, on success, it is some. ## ## ``pattern: Regex`` ## the pattern that is being matched @@ -235,15 +233,15 @@ proc `[]`*(pattern: CaptureBounds, i: int): Option[Slice[int]] = let pattern = RegexMatch(pattern) if pattern.pcreMatchBounds[i + 1].a != -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: - return None[Slice[int]]() + return none(Slice[int]) proc `[]`*(pattern: Captures, i: int): string = let pattern = RegexMatch(pattern) let bounds = pattern.captureBounds[i] - if bounds: + if bounds.isSome: let bounds = bounds.get return pattern.str.substr(bounds.a, bounds.b) else: @@ -275,7 +273,7 @@ proc toTable*(pattern: Captures, default: string = nil): Table[string, string] = result = initTable[string, string]() toTableImpl(nextVal == nil) -proc toTable*(pattern: CaptureBounds, default = None[Slice[int]]()): +proc toTable*(pattern: CaptureBounds, default = none(Slice[int])): Table[string, Option[Slice[int]]] = result = initTable[string, Option[Slice[int]]]() toTableImpl(nextVal.isNone) @@ -288,13 +286,13 @@ template itemsImpl(cond: bool): stmt {.immediate, dirty.} = else: 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) iterator items*(pattern: Captures, default: string = nil): string = 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)) 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]), cint(vecsize)) if execRet >= 0: - return Some(myResult) + return some(myResult) case execRet: of pcre.ERROR_NOMATCH: - return None[RegexMatch]() + return none(RegexMatch) of pcre.ERROR_NULL: raise newException(AccessViolationError, "Expected non-null parameters") of pcre.ERROR_BADOPTION: @@ -497,7 +495,7 @@ iterator findIter*(str: string, pattern: Regex, start = 0, endpos = int.high): R while true: var flags = 0 - if match and + if match.isSome and match.get.matchBounds.a > match.get.matchBounds.b: # 0-len match flags = pcre.NOTEMPTY_ATSTART diff --git a/test/captures.nim b/test/captures.nim index 24ba021f59..4f3f154444 100644 --- a/test/captures.nim +++ b/test/captures.nim @@ -1,4 +1,4 @@ -import unittest, optional_t.nonstrict +import unittest, optional_nonstrict include nre suite "captures": @@ -31,8 +31,8 @@ suite "captures": test "named capture bounds": let ex1 = "foo".find(re("(?foo)(?bar)?")) - check(ex1.captureBounds["foo"] == Some(0..2)) - check(ex1.captureBounds["bar"] == None[Slice[int]]()) + check(ex1.captureBounds["foo"] == some(0..2)) + check(ex1.captureBounds["bar"] == none(Slice[int])) test "capture count": let ex1 = re("(?foo)(?bar)?") @@ -42,7 +42,7 @@ suite "captures": test "named capture table": let ex1 = "foo".find(re("(?foo)(?bar)?")) 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()) let ex2 = "foobar".find(re("(?foo)(?bar)?")) @@ -51,7 +51,7 @@ suite "captures": test "capture sequence": let ex1 = "foo".find(re("(?foo)(?bar)?")) 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", ""]) let ex2 = "foobar".find(re("(?foo)(?bar)?")) diff --git a/test/find.nim b/test/find.nim index 90acaf4e1c..05bfb848a0 100644 --- a/test/find.nim +++ b/test/find.nim @@ -1,4 +1,4 @@ -import unittest, sequtils, nre, optional_t.nonstrict +import unittest, sequtils, nre, optional_nonstrict suite "find": test "find text": diff --git a/test/match.nim b/test/match.nim index 16fb931c0e..38ee5214b5 100644 --- a/test/match.nim +++ b/test/match.nim @@ -1,10 +1,10 @@ -include nre, unittest, optional_t.nonstrict +include nre, unittest, optional_nonstrict suite "match": 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 = 2) != 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)) test "match examples": check("abc".match(re"(\w)").captures[0] == "a") diff --git a/test/misc.nim b/test/misc.nim index f110c3f1e8..f4a88b639b 100644 --- a/test/misc.nim +++ b/test/misc.nim @@ -1,4 +1,4 @@ -import unittest, nre, strutils, optional_t.nonstrict +import unittest, nre, strutils, optional_nonstrict suite "Misc tests": test "unicode": diff --git a/test/optional_nonstrict.nim b/test/optional_nonstrict.nim new file mode 100644 index 0000000000..d13f4fab7a --- /dev/null +++ b/test/optional_nonstrict.nim @@ -0,0 +1,3 @@ +import options +converter option2val*[T](val: Option[T]): T = + return val.get()