Skip to content

Commit

Permalink
use strscans
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jun 3, 2021
1 parent 0f35442 commit 7c3f0e6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
11 changes: 11 additions & 0 deletions tests/tools/tnimdigger.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import tools/nimdigger {.all.}

block: # parseNimGitTag
doAssert parseNimGitTag("v1.4.2") == (1, 4, 2)
doAssertRaises(ValueError): discard parseNimGitTag("v1.4")
doAssertRaises(ValueError): discard parseNimGitTag("v1.4.2a")
doAssertRaises(ValueError): discard parseNimGitTag("av1.4.2")

block: # isGitNimTag
doAssert isGitNimTag("v1.4.2")
doAssert not isGitNimTag("v1.4.2a")
23 changes: 10 additions & 13 deletions tools/nimdigger.nim
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,18 @@ proc gitIsAncestorOf(dir: string, rev1, rev2: string): bool =
gitCheck(dir)
execShellCmd(fmt"git -C {dir.quoteShell} merge-base --is-ancestor {rev1} {rev2}") == 0

proc isGitNimTag(tag: string): bool =
if not tag.startsWith "v":
return false
let ver = tag[1..^1].split(".")
return ver.len == 3
import std/strscans

proc parseNimGitTag(tag: string): (int, int, int) =
doAssert tag.isGitNimTag, tag
let ver = tag[1..^1].split(".")
template impl(i) =
# improve pending https://github.com/nim-lang/Nim/pull/18038
result[i] = ver[i].parseInt
impl 0
impl 1
impl 2
if not scanf(tag, "v$i.$i.$i$.", result[0], result[1], result[2]):
raise newException(ValueError, tag)

proc isGitNimTag(tag: string): bool =
try:
discard parseNimGitTag(tag)
return true
except ValueError:
return false

proc toNimCsourcesExe(binDir: string, name: string, rev: string): string =
let rev2 = rev.replace(".", "_")
Expand Down

0 comments on commit 7c3f0e6

Please sign in to comment.