From c4cb781c3d76245a729f1283e660cc8724adf836 Mon Sep 17 00:00:00 2001 From: Flaviu Tamas Date: Mon, 12 Jan 2015 20:56:34 -0500 Subject: Flip parameter string-pattern order --- src/nre.nim | 132 +++++++++++++++++++++++++++--------------------------- test/captures.nim | 28 ++++++------ test/find.nim | 6 +-- test/split.nim | 8 ++-- 4 files changed, 87 insertions(+), 87 deletions(-) diff --git a/src/nre.nim b/src/nre.nim index c9e9d9ecd..13d0a2056 100644 --- a/src/nre.nim +++ b/src/nre.nim @@ -37,27 +37,27 @@ type StudyError* = ref object of Exception # }}} -proc getinfo[T](self: Regex, opt: cint): T = - let retcode = pcre.fullinfo(self.pcreObj, self.pcreExtra, opt, addr result) +proc getinfo[T](pattern: Regex, opt: cint): T = + let retcode = pcre.fullinfo(pattern.pcreObj, pattern.pcreExtra, opt, addr result) if retcode < 0: # XXX Error message that doesn't expose implementation details raise newException(FieldError, "Invalid getinfo for $1, errno $2" % [$opt, $retcode]) # Regex accessors {{{ -proc captureCount*(self: Regex): int = +proc captureCount*(pattern: Regex): int = ## Get the maximum number of captures ## ## Does not return the number of captured captures - return getinfo[int](self, pcre.INFO_CAPTURECOUNT) + return getinfo[int](pattern, pcre.INFO_CAPTURECOUNT) -proc captureNameId*(self: Regex): Table[string, int] = +proc captureNameId*(pattern: Regex): Table[string, int] = ## Returns a map from named capture groups to their numerical ## identifier - return self.captureNameToId + return pattern.captureNameToId -proc matchesCrLf(self: Regex): bool = - let flags = getinfo[cint](self, pcre.INFO_OPTIONS) +proc matchesCrLf(pattern: Regex): bool = + let flags = getinfo[cint](pattern, pcre.INFO_OPTIONS) let newlineFlags = flags and (pcre.NEWLINE_CRLF or pcre.NEWLINE_ANY or pcre.NEWLINE_ANYCRLF) @@ -79,71 +79,71 @@ proc matchesCrLf(self: Regex): bool = # }}} # Capture accessors {{{ -proc captureBounds*(self: RegexMatch): CaptureBounds = return CaptureBounds(self) +proc captureBounds*(pattern: RegexMatch): CaptureBounds = return CaptureBounds(pattern) -proc captures*(self: RegexMatch): Captures = return Captures(self) +proc captures*(pattern: RegexMatch): Captures = return Captures(pattern) -proc `[]`*(self: CaptureBounds, i: int): Option[Slice[int]] = +proc `[]`*(pattern: CaptureBounds, i: int): Option[Slice[int]] = ## Gets the bounds of the `i`th capture. ## Undefined behavior if `i` is out of bounds ## If `i` is a failed optional capture, returns None ## If `i == -1`, returns the whole match - let self = RegexMatch(self) - if self.pcreMatchBounds[i + 1].a != -1: - let bounds = self.pcreMatchBounds[i + 1] + 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)) else: return None[Slice[int]]() -proc `[]`*(self: Captures, i: int): string = +proc `[]`*(pattern: Captures, i: int): string = ## gets the `i`th capture ## Undefined behavior if `i` is out of bounds ## If `i` is a failed optional capture, returns nil ## If `i == -1`, returns the whole match - let self = RegexMatch(self) - let bounds = self.captureBounds[i] + let pattern = RegexMatch(pattern) + let bounds = pattern.captureBounds[i] if bounds: let bounds = bounds.get - if self.matchCache == nil: + if pattern.matchCache == nil: # capture count, plus the entire string - self.matchCache = newSeq[string](self.pattern.captureCount + 1) - if self.matchCache[i + 1] == nil: - self.matchCache[i + 1] = self.str[bounds.a .. bounds.b-1] - return self.matchCache[i + 1] + pattern.matchCache = newSeq[string](pattern.pattern.captureCount + 1) + if pattern.matchCache[i + 1] == nil: + pattern.matchCache[i + 1] = pattern.str[bounds.a .. bounds.b-1] + return pattern.matchCache[i + 1] else: return nil -proc match*(self: RegexMatch): string = - return self.captures[-1] +proc match*(pattern: RegexMatch): string = + return pattern.captures[-1] -proc matchBounds*(self: RegexMatch): Slice[int] = - return self.captureBounds[-1].get +proc matchBounds*(pattern: RegexMatch): Slice[int] = + return pattern.captureBounds[-1].get -proc `[]`*(self: CaptureBounds, name: string): Option[Slice[int]] = +proc `[]`*(pattern: CaptureBounds, name: string): Option[Slice[int]] = ## Will fail with KeyError if `name` is not a real named capture - let self = RegexMatch(self) - return self.captureBounds[self.pattern.captureNameToId.fget(name)] + let pattern = RegexMatch(pattern) + return pattern.captureBounds[pattern.pattern.captureNameToId.fget(name)] -proc `[]`*(self: Captures, name: string): string = +proc `[]`*(pattern: Captures, name: string): string = ## Will fail with KeyError if `name` is not a real named capture - let self = RegexMatch(self) - return self.captures[self.pattern.captureNameToId.fget(name)] + let pattern = RegexMatch(pattern) + return pattern.captures[pattern.pattern.captureNameToId.fget(name)] template asTableImpl(cond: bool): stmt {.immediate, dirty.} = - for key in RegexMatch(self).pattern.captureNameId.keys: - let nextVal = self[key] + for key in RegexMatch(pattern).pattern.captureNameId.keys: + let nextVal = pattern[key] if cond: result[key] = default else: result[key] = nextVal -proc asTable*(self: Captures, default: string = nil): Table[string, string] = +proc asTable*(pattern: Captures, default: string = nil): Table[string, string] = ## Gets all the named captures and returns them result = initTable[string, string]() asTableImpl(nextVal == nil) -proc asTable*(self: CaptureBounds, default = None[Slice[int]]()): +proc asTable*(pattern: CaptureBounds, default = None[Slice[int]]()): Table[string, Option[Slice[int]]] = ## Gets all the named captures and returns them result = initTable[string, Option[Slice[int]]]() @@ -151,17 +151,17 @@ proc asTable*(self: CaptureBounds, default = None[Slice[int]]()): template asSeqImpl(cond: bool): stmt {.immediate, dirty.} = result = @[] - for i in 0 .. foo)(?bar)").match("foobar").get + let ex1 = "foobar".match(initRegex("(?foo)(?bar)")).get check(ex1.captures["foo"] == "foo") check(ex1.captures["bar"] == "bar") - let ex2 = initRegex("(?foo)(?bar)?").match("foo").get + let ex2 = "foo".match(initRegex("(?foo)(?bar)?")).get check(ex2.captures["foo"] == "foo") check(ex2.captures["bar"] == nil) test "named capture bounds": - let ex1 = initRegex("(?foo)(?bar)?").match("foo").get + let ex1 = "foo".match(initRegex("(?foo)(?bar)?")).get check(ex1.captureBounds["foo"] == Some(0..3)) check(ex1.captureBounds["bar"] == None[Slice[int]]()) @@ -40,20 +40,20 @@ suite "captures": check(ex1.captureNameId == {"foo" : 0, "bar" : 1}.toTable()) test "named capture table": - let ex1 = initRegex("(?foo)(?bar)?").match("foo").get + let ex1 = "foo".match(initRegex("(?foo)(?bar)?")).get check(ex1.captures.asTable == {"foo" : "foo", "bar" : nil}.toTable()) check(ex1.captureBounds.asTable == {"foo" : Some(0..3), "bar" : None[Slice[int]]()}.toTable()) check(ex1.captures.asTable("") == {"foo" : "foo", "bar" : ""}.toTable()) - let ex2 = initRegex("(?foo)(?bar)?").match("foobar").get + let ex2 = "foobar".match(initRegex("(?foo)(?bar)?")).get check(ex2.captures.asTable == {"foo" : "foo", "bar" : "bar"}.toTable()) test "capture sequence": - let ex1 = initRegex("(?foo)(?bar)?").match("foo").get + let ex1 = "foo".match(initRegex("(?foo)(?bar)?")).get check(ex1.captures.asSeq == @["foo", nil]) check(ex1.captureBounds.asSeq == @[Some(0..3), None[Slice[int]]()]) check(ex1.captures.asSeq("") == @["foo", ""]) - let ex2 = initRegex("(?foo)(?bar)?").match("foobar").get + let ex2 = "foobar".match(initRegex("(?foo)(?bar)?")).get check(ex2.captures.asSeq == @["foo", "bar"]) diff --git a/test/find.nim b/test/find.nim index a8dbdf016..fc58bb48c 100644 --- a/test/find.nim +++ b/test/find.nim @@ -3,12 +3,12 @@ include nre suite "find": test "find text": - check(initRegex(r"[a-z]").find("3213a").get.match == "a") - check(initRegex(r" ", "S").findAll("1 2 3 4 5 6 7 8 ").map( + check("3213a".find(initRegex(r"[a-z]")).get.match == "a") + check("1 2 3 4 5 6 7 8 ".findAll(initRegex(r" ", "S")).map( proc (a: RegexMatch): string = a.match ) == @[" ", " ", " ", " ", " ", " ", " ", " "]) test "find bounds": - check(initRegex(r" ", "S").findAll("1 2 3 4 5 ").map( + check("1 2 3 4 5 ".findAll(initRegex(r" ", "S")).map( proc (a: RegexMatch): Slice[int] = a.matchBounds ) == @[1..2, 3..4, 5..6, 7..8, 9..10]) diff --git a/test/split.nim b/test/split.nim index acdd41be9..aee522991 100644 --- a/test/split.nim +++ b/test/split.nim @@ -3,7 +3,7 @@ include nre suite "string splitting": test "splitting strings": - check(initRegex("").split("12345") == @["1", "2", "3", "4", "5"]) - check(initRegex(" ", "S").split("1 2 3 4 5 6 ") == @["1", "2", "3", "4", "5", "6", ""]) - check(initRegex(" ", "S").split("1 2 ") == @["1", "", "2", "", ""]) - check(initRegex(" ", "S").split("1 2") == @["1", "2"]) + check("12345".split(initRegex("")) == @["1", "2", "3", "4", "5"]) + check("1 2 3 4 5 6 ".split(initRegex(" ", "S")) == @["1", "2", "3", "4", "5", "6", ""]) + check("1 2 ".split(initRegex(" ", "S")) == @["1", "", "2", "", ""]) + check("1 2".split(initRegex(" ", "S")) == @["1", "2"]) -- cgit 1.4.1-2-gfad0