/usr/share/gocode/src/pault.ag/go/debian/version/version.go is in golang-pault-go-debian-dev 0.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | /* {{{ Copyright © 2012 Michael Stapelberg and contributors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Michael Stapelberg nor the
* names of contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Michael Stapelberg ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Michael Stapelberg BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. }}} */
// version is a pure-go implementation of dpkg version string functions
// (parsing, comparison) which is compatible with dpkg(1).
package version
import (
"fmt"
"strconv"
"strings"
"unicode"
)
// Slice is a slice versions, satisfying sort.Interface
type Slice []Version
func (a Slice) Len() int {
return len(a)
}
func (a Slice) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a Slice) Less(i, j int) bool {
return Compare(a[i], a[j]) < 0
}
type Version struct {
Epoch uint
Version string
Revision string
}
func (v *Version) IsNative() bool {
return len(v.Revision) == 0
}
func (version *Version) UnmarshalControl(data string) error {
return parseInto(version, data)
}
func (version Version) MarshalControl() (string, error) {
return version.String(), nil
}
func (v Version) String() string {
var result string
if v.Epoch > 0 {
result = strconv.Itoa(int(v.Epoch)) + ":" + v.Version
} else {
result = v.Version
}
if len(v.Revision) > 0 {
result += "-" + v.Revision
}
return result
}
func cisdigit(r rune) bool {
return r >= '0' && r <= '9'
}
func cisalpha(r rune) bool {
return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')
}
func order(r rune) int {
if cisdigit(r) {
return 0
}
if cisalpha(r) {
return int(r)
}
if r == '~' {
return -1
}
if int(r) != 0 {
return int(r) + 256
}
return 0
}
func verrevcmp(a string, b string) int {
i := 0
j := 0
for i < len(a) || j < len(b) {
var first_diff int
for (i < len(a) && !cisdigit(rune(a[i]))) ||
(j < len(b) && !cisdigit(rune(b[j]))) {
ac := 0
if i < len(a) {
ac = order(rune(a[i]))
}
bc := 0
if j < len(b) {
bc = order(rune(b[j]))
}
if ac != bc {
return ac - bc
}
i++
j++
}
for i < len(a) && a[i] == '0' {
i++
}
for j < len(b) && b[j] == '0' {
j++
}
for i < len(a) && cisdigit(rune(a[i])) && j < len(b) && cisdigit(rune(b[j])) {
if first_diff == 0 {
first_diff = int(rune(a[i]) - rune(b[j]))
}
i++
j++
}
if i < len(a) && cisdigit(rune(a[i])) {
return 1
}
if j < len(b) && cisdigit(rune(b[j])) {
return -1
}
if first_diff != 0 {
return first_diff
}
}
return 0
}
// Compare compares the two provided Debian versions. It returns 0 if a and b
// are equal, a value < 0 if a is smaller than b and a value > 0 if a is
// greater than b.
func Compare(a Version, b Version) int {
if a.Epoch > b.Epoch {
return 1
}
if a.Epoch < b.Epoch {
return -1
}
rc := verrevcmp(a.Version, b.Version)
if rc != 0 {
return rc
}
return verrevcmp(a.Revision, b.Revision)
}
// Parse returns a Version struct filled with the epoch, version and revision
// specified in input. It verifies the version string as a whole, just like
// dpkg(1), and even returns roughly the same error messages.
func Parse(input string) (Version, error) {
result := Version{}
return result, parseInto(&result, input)
}
func parseInto(result *Version, input string) error {
trimmed := strings.TrimSpace(input)
if trimmed == "" {
return fmt.Errorf("version string is empty")
}
if strings.IndexFunc(trimmed, unicode.IsSpace) != -1 {
return fmt.Errorf("version string has embedded spaces")
}
colon := strings.Index(trimmed, ":")
if colon != -1 {
epoch, err := strconv.ParseInt(trimmed[:colon], 10, 64)
if err != nil {
return fmt.Errorf("epoch: %v", err)
}
if epoch < 0 {
return fmt.Errorf("epoch in version is negative")
}
result.Epoch = uint(epoch)
}
result.Version = trimmed[colon+1:]
if len(result.Version) == 0 {
return fmt.Errorf("nothing after colon in version number")
}
if hyphen := strings.LastIndex(result.Version, "-"); hyphen != -1 {
result.Revision = result.Version[hyphen+1:]
result.Version = result.Version[:hyphen]
}
if len(result.Version) > 0 && !unicode.IsDigit(rune(result.Version[0])) {
return fmt.Errorf("version number does not start with digit")
}
if strings.IndexFunc(result.Version, func(c rune) bool {
return !cisdigit(c) && !cisalpha(c) && c != '.' && c != '-' && c != '+' && c != '~' && c != ':'
}) != -1 {
return fmt.Errorf("invalid character in version number")
}
if strings.IndexFunc(result.Revision, func(c rune) bool {
return !cisdigit(c) && !cisalpha(c) && c != '.' && c != '+' && c != '~'
}) != -1 {
return fmt.Errorf("invalid character in revision number")
}
return nil
}
// vim:ts=4:sw=4:noexpandtab foldmethod=marker
|