Skip to content

Instantly share code, notes, and snippets.

@xtagon
Last active January 3, 2016 20:19
Show Gist options
  • Save xtagon/167f358d8c0abc20d07f to your computer and use it in GitHub Desktop.
Save xtagon/167f358d8c0abc20d07f to your computer and use it in GitHub Desktop.
import pegs, strutils
type
TSemVer* = tuple
major, minor, patch: system.Natural
special: string
proc toSemVer*(versionString: string): TSemVer {.noSideEffect.} =
if versionString =~ peg"{\d+} \. {\d+} \. {\d+} {.*}":
var
major: system.Natural = matches[0].ParseInt
minor: system.Natural = matches[1].ParseInt
patch: system.Natural = matches[2].ParseInt
special = matches[3]
result = (major, minor, patch, special)
else:
raise newException(ESystem, "Invalid version string")
proc `$`*(version: TSemVer): string =
result = join([$version.major, $version.minor, $version.patch], ".")
if version.special.len > 0: result.add "-" & version.special
when isMainModule:
# This doesn't work
const VERSION = "1.23.4".toSemVer
echo(VERSION)
# But this works
var v1 = "1.23.4".toSemVer
echo(v1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment