diff options
author | Austin Clements <austin@google.com> | 2019-12-02 17:32:01 -0500 |
---|---|---|
committer | Austin Clements <austin@google.com> | 2019-12-05 01:48:12 +0000 |
commit | fa3a121a79f85a4c957f29372b5ebfde7211a980 (patch) | |
tree | 499bc58b5e809186bd76d63b072dc7cabab1493a /src/runtime/string.go | |
parent | 709dbd28708eab97993ca06adea74be392c05c1c (diff) | |
download | go-fa3a121a79f85a4c957f29372b5ebfde7211a980.tar.gz go-fa3a121a79f85a4c957f29372b5ebfde7211a980.zip |
runtime: add a simple version number parser
This will be used to parse the Linux kernel versions, but this code is
generic and can be tested on its own.
For #35777.
Change-Id: If1df48d07250e5855dde45bc9d57c66f777b9fb4
Reviewed-on: https://go-review.googlesource.com/c/go/+/209597
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/runtime/string.go')
-rw-r--r-- | src/runtime/string.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/runtime/string.go b/src/runtime/string.go index d198f73756..184245b105 100644 --- a/src/runtime/string.go +++ b/src/runtime/string.go @@ -495,3 +495,37 @@ func gostringw(strw *uint16) string { b[n2] = 0 // for luck return s[:n2] } + +// parseRelease parses a dot-separated version number. It follows the +// semver syntax, but allows the minor and patch versions to be +// elided. +func parseRelease(rel string) (major, minor, patch int, ok bool) { + // Strip anything after a dash or plus. + for i := 0; i < len(rel); i++ { + if rel[i] == '-' || rel[i] == '+' { + rel = rel[:i] + break + } + } + + next := func() (int, bool) { + for i := 0; i < len(rel); i++ { + if rel[i] == '.' { + ver, ok := atoi(rel[:i]) + rel = rel[i+1:] + return ver, ok + } + } + ver, ok := atoi(rel) + rel = "" + return ver, ok + } + if major, ok = next(); !ok || rel == "" { + return + } + if minor, ok = next(); !ok || rel == "" { + return + } + patch, ok = next() + return +} |