1// Copyright 2017 The Wuffs Authors. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// commonflags holds flag defaults and usage messages that are common to the 16// Wuffs command line tools. 17// 18// It also holds functions to parse and validate these flag values. 19package commonflags 20 21import ( 22 "fmt" 23 "path" 24 "strings" 25) 26 27const ( 28 CcompilersDefault = "clang-5.0,gcc" 29 CcompilersUsage = `comma-separated list of C compilers` 30 31 CformatterDefault = "clang-format-5.0" 32 CformatterUsage = `C formatter` 33 34 FocusDefault = "" 35 FocusUsage = `comma-separated list of tests or benchmarks (name prefixes) to focus on, e.g. "wuffs_gif_decode"` 36 37 GenlinenumDefault = false 38 GenlinenumUsage = `whether to generate filename:line_number comments` 39 40 IterscaleDefault = 100 41 IterscaleMin = 0 42 IterscaleMax = 1000000 43 IterscaleUsage = `a scaling factor for the number of iterations per benchmark` 44 45 MimicDefault = false 46 MimicUsage = `whether to compare Wuffs' output with other libraries' output` 47 48 RepsDefault = 5 49 RepsMin = 0 50 RepsMax = 1000000 51 RepsUsage = `the number of repetitions per benchmark` 52 53 VersionDefault = "0.0.0" 54 VersionUsage = `version string, e.g. "1.2.3-beta.4"` 55) 56 57// TODO: do IsAlphaNumericIsh and IsValidUsePath belong in a separate package, 58// such as lang/validate? Perhaps together with token.Unescape? 59 60// IsAlphaNumericIsh returns whether s contains only ASCII alpha-numerics and a 61// limited set of punctuation such as commas and slashes, but not containing 62// e.g. spaces, semi-colons, colons or backslashes. 63// 64// The intent is that if s is alpha-numeric-ish, then it should not need 65// escaping when passed to other programs as command line arguments. 66func IsAlphaNumericIsh(s string) bool { 67 for i := 0; i < len(s); i++ { 68 c := s[i] 69 if c == ',' || c == '-' || c == '.' || c == '/' || ('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || 70 c == '_' || ('a' <= c && c <= 'z') { 71 continue 72 } 73 return false 74 } 75 return true 76} 77 78func IsValidUsePath(s string) bool { 79 return s == path.Clean(s) && s != "" && s[0] != '.' && s[0] != '/' 80} 81 82type Version struct { 83 Major uint32 84 Minor uint16 85 Patch uint16 86 Extension string 87} 88 89func ParseVersion(s string) (v Version, ok bool) { 90 if !IsAlphaNumericIsh(s) { 91 return Version{}, false 92 } 93 if i := strings.IndexByte(s, '-'); i >= 0 { 94 s, v.Extension = s[:i], s[i+1:] 95 } 96 if _, err := fmt.Sscanf(s, "%d.%d.%d", &v.Major, &v.Minor, &v.Patch); err != nil { 97 return Version{}, false 98 } 99 return v, true 100} 101 102func (v Version) String() string { 103 s := fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch) 104 if v.Extension != "" { 105 s += "-" + v.Extension 106 } 107 return s 108} 109 110func (v Version) Uint64() uint64 { 111 return (uint64(v.Major) << 32) | (uint64(v.Minor) << 16) | (uint64(v.Patch) << 0) 112} 113