]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: add a simple version number parser
authorAustin Clements <austin@google.com>
Mon, 2 Dec 2019 22:32:01 +0000 (17:32 -0500)
committerAustin Clements <austin@google.com>
Thu, 5 Dec 2019 01:48:12 +0000 (01:48 +0000)
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>
src/runtime/export_test.go
src/runtime/string.go
src/runtime/string_test.go

index 75882d02b68c1614df9369742ac2716afa8dc26f..5206fa0109a4df3b95353f5c16696ee3d9a1f978 100644 (file)
@@ -43,6 +43,8 @@ var PhysHugePageSize = physHugePageSize
 
 var NetpollGenericInit = netpollGenericInit
 
+var ParseRelease = parseRelease
+
 const PreemptMSupported = preemptMSupported
 
 type LFNode struct {
index d198f73756b787a274015da79b8514e5cbabc819..184245b1059257c8058b0ea38ead8a89faa4c014 100644 (file)
@@ -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
+}
index a1716fa32f2ec56a5af6ffc1497d375696b1e76f..80c5fa6406124ba3ea652712384f86bffd03e20d 100644 (file)
@@ -454,3 +454,34 @@ func TestAtoi32(t *testing.T) {
                }
        }
 }
+
+type parseReleaseTest struct {
+       in                  string
+       major, minor, patch int
+}
+
+var parseReleaseTests = []parseReleaseTest{
+       {"", -1, -1, -1},
+       {"x", -1, -1, -1},
+       {"5", 5, 0, 0},
+       {"5.12", 5, 12, 0},
+       {"5.12-x", 5, 12, 0},
+       {"5.12.1", 5, 12, 1},
+       {"5.12.1-x", 5, 12, 1},
+       {"5.12.1.0", 5, 12, 1},
+       {"5.20496382327982653440", -1, -1, -1},
+}
+
+func TestParseRelease(t *testing.T) {
+       for _, test := range parseReleaseTests {
+               major, minor, patch, ok := runtime.ParseRelease(test.in)
+               if !ok {
+                       major, minor, patch = -1, -1, -1
+               }
+               if test.major != major || test.minor != minor || test.patch != patch {
+                       t.Errorf("parseRelease(%q) = (%v, %v, %v) want (%v, %v, %v)",
+                               test.in, major, minor, patch,
+                               test.major, test.minor, test.patch)
+               }
+       }
+}