From 7c3f0e6aa4b2e8c4570e5027ac7d661e1080617c Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Thu, 3 Jun 2021 16:05:19 -0700 Subject: [PATCH] use strscans --- tests/tools/tnimdigger.nim | 11 +++++++++++ tools/nimdigger.nim | 23 ++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) create mode 100644 tests/tools/tnimdigger.nim diff --git a/tests/tools/tnimdigger.nim b/tests/tools/tnimdigger.nim new file mode 100644 index 0000000000000..d2d307277b2d9 --- /dev/null +++ b/tests/tools/tnimdigger.nim @@ -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") diff --git a/tools/nimdigger.nim b/tools/nimdigger.nim index 421490a7eb062..401e07f0657d4 100644 --- a/tools/nimdigger.nim +++ b/tools/nimdigger.nim @@ -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(".", "_")