this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

pkg: add generated core package

and internal qgo tool to generate them

Change-Id: Ic03dc27262d8769f3dc3cd56fa11d77ee0b68dc8

+1820
+327
internal/cmd/qgo/qgo.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // qgo builds CUE builtin packages from Go packages. 16 + package main 17 + 18 + import ( 19 + "bytes" 20 + "flag" 21 + "fmt" 22 + "go/ast" 23 + "go/constant" 24 + "go/format" 25 + "go/parser" 26 + "go/printer" 27 + "go/token" 28 + "go/types" 29 + "io" 30 + "io/ioutil" 31 + "log" 32 + "os" 33 + "path/filepath" 34 + "regexp" 35 + "strings" 36 + 37 + "golang.org/x/tools/go/loader" 38 + ) 39 + 40 + const help = ` 41 + Commands: 42 + extract Extract one-line signature of exported types of 43 + the given package. 44 + 45 + Functions that have have more than one return 46 + argument or unknown types are skipped. 47 + ` 48 + 49 + // Even though all of the code is generated, the documentation is copied as is. 50 + // So for proper measure, include both the CUE and Go licenses. 51 + const copyright = `// Copyright 2018 The CUE Authors 52 + // 53 + // Licensed under the Apache License, Version 2.0 (the "License"); 54 + // you may not use this file except in compliance with the License. 55 + // You may obtain a copy of the License at 56 + // 57 + // http://www.apache.org/licenses/LICENSE-2.0 58 + // 59 + // Unless required by applicable law or agreed to in writing, software 60 + // distributed under the License is distributed on an "AS IS" BASIS, 61 + // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 62 + // See the License for the specific language governing permissions and 63 + // limitations under the License. 64 + 65 + // Copyright 2018 The Go Authors. All rights reserved. 66 + // Use of this source code is governed by a BSD-style 67 + // license that can be found in the LICENSE file. 68 + ` 69 + 70 + var genLine string 71 + 72 + var ( 73 + exclude = flag.String("exclude", "", "comma-separated list of regexps of entries to exclude") 74 + stripstr = flag.Bool("stripstr", false, "Remove String suffix from functions") 75 + ) 76 + 77 + func init() { 78 + log.SetFlags(log.Lshortfile) 79 + } 80 + 81 + func main() { 82 + flag.Parse() 83 + 84 + genLine = "//go:generate " + strings.Join(os.Args, " ") 85 + 86 + args := flag.Args() 87 + if len(args) == 0 { 88 + fmt.Println(strings.TrimSpace(help)) 89 + return 90 + } 91 + 92 + command := args[0] 93 + args = args[1:] 94 + 95 + switch command { 96 + case "extract": 97 + extract(args) 98 + } 99 + } 100 + 101 + var exclusions []*regexp.Regexp 102 + 103 + func initExclusions() { 104 + for _, re := range strings.Split(*exclude, ",") { 105 + if re != "" { 106 + exclusions = append(exclusions, regexp.MustCompile(re)) 107 + } 108 + } 109 + } 110 + 111 + func filter(name string) bool { 112 + if !ast.IsExported(name) { 113 + return true 114 + } 115 + for _, ex := range exclusions { 116 + if ex.MatchString(name) { 117 + return true 118 + } 119 + } 120 + return false 121 + } 122 + 123 + func pkgName() string { 124 + pkg, err := os.Getwd() 125 + if err != nil { 126 + log.Fatal(err) 127 + } 128 + return filepath.Base(pkg) 129 + } 130 + 131 + type extracter struct { 132 + prog *loader.Program 133 + pkg *loader.PackageInfo 134 + } 135 + 136 + func extract(args []string) { 137 + 138 + cfg := loader.Config{ 139 + ParserMode: parser.ParseComments, 140 + } 141 + cfg.FromArgs(args, false) 142 + 143 + e := extracter{} 144 + var err error 145 + e.prog, err = cfg.Load() 146 + if err != nil { 147 + log.Fatal(err) 148 + } 149 + 150 + lastPkg := "" 151 + var w *bytes.Buffer 152 + initExclusions() 153 + 154 + flushFile := func() { 155 + if w != nil && w.Len() > 0 { 156 + b, err := format.Source(w.Bytes()) 157 + if err != nil { 158 + log.Fatal(err) 159 + } 160 + err = ioutil.WriteFile(lastPkg+".go", b, 0644) 161 + if err != nil { 162 + log.Fatal(err) 163 + } 164 + } 165 + w = &bytes.Buffer{} 166 + } 167 + 168 + for _, p := range e.prog.InitialPackages() { 169 + e.pkg = p 170 + for _, f := range p.Files { 171 + if lastPkg != p.Pkg.Name() { 172 + flushFile() 173 + lastPkg = p.Pkg.Name() 174 + fmt.Fprintln(w, copyright) 175 + fmt.Fprintln(w, genLine) 176 + fmt.Fprintln(w) 177 + fmt.Fprintf(w, "package %s\n", pkgName()) 178 + fmt.Fprintln(w) 179 + fmt.Fprintf(w, "import %q", p.Pkg.Path()) 180 + fmt.Fprintln(w) 181 + } 182 + 183 + for _, d := range f.Decls { 184 + switch x := d.(type) { 185 + case *ast.FuncDecl: 186 + e.reportFun(w, x) 187 + case *ast.GenDecl: 188 + e.reportDecl(w, x) 189 + } 190 + } 191 + } 192 + } 193 + flushFile() 194 + } 195 + 196 + func (e *extracter) reportFun(w io.Writer, x *ast.FuncDecl) { 197 + if filter(x.Name.Name) { 198 + return 199 + } 200 + pkgName := e.pkg.Pkg.Name() 201 + override := "" 202 + params := []ast.Expr{} 203 + if x.Type.Params != nil { 204 + for _, f := range x.Type.Params.List { 205 + tx := f.Type 206 + if star, isStar := tx.(*ast.StarExpr); isStar { 207 + if i, ok := star.X.(*ast.Ident); ok && ast.IsExported(i.Name) { 208 + f.Type = &ast.SelectorExpr{X: ast.NewIdent(pkgName), Sel: i} 209 + if isStar { 210 + f.Type = &ast.StarExpr{X: f.Type} 211 + } 212 + } 213 + } 214 + for _, n := range f.Names { 215 + params = append(params, n) 216 + if n.Name == pkgName { 217 + override = pkgName + x.Name.Name 218 + } 219 + } 220 + } 221 + } 222 + var fn ast.Expr = &ast.SelectorExpr{ 223 + X: ast.NewIdent(pkgName), 224 + Sel: x.Name, 225 + } 226 + if override != "" { 227 + fn = ast.NewIdent(override) 228 + } 229 + x.Body = &ast.BlockStmt{List: []ast.Stmt{ 230 + &ast.ReturnStmt{Results: []ast.Expr{&ast.CallExpr{ 231 + Fun: fn, 232 + Args: params, 233 + }}}, 234 + }} 235 + if name := x.Name.Name; *stripstr && strings.HasSuffix(name, "String") { 236 + newName := name[:len(name)-len("String")] 237 + x.Name = ast.NewIdent(newName) 238 + for _, c := range x.Doc.List { 239 + c.Text = strings.Replace(c.Text, name, newName, -1) 240 + } 241 + } 242 + types := []ast.Expr{} 243 + if x.Recv == nil && x.Type != nil && x.Type.Results != nil && !strings.HasPrefix(x.Name.Name, "New") { 244 + for _, f := range x.Type.Results.List { 245 + if len(f.Names) == 0 { 246 + types = append(types, f.Type) 247 + } else { 248 + for range f.Names { 249 + types = append(types, f.Type) 250 + } 251 + } 252 + } 253 + } 254 + if len(types) != 1 { 255 + switch len(types) { 256 + case 2: 257 + if i, ok := types[1].(*ast.Ident); ok && i.Name == "error" { 258 + break 259 + } 260 + fallthrough 261 + default: 262 + fmt.Printf("Skipping ") 263 + x.Doc = nil 264 + printer.Fprint(os.Stdout, e.prog.Fset, x) 265 + fmt.Println() 266 + return 267 + } 268 + } 269 + fmt.Fprintln(w) 270 + printer.Fprint(w, e.prog.Fset, x.Doc) 271 + printer.Fprint(w, e.prog.Fset, x) 272 + fmt.Fprint(w, "\n") 273 + if override != "" { 274 + fmt.Fprintf(w, "var %s = %s.%s\n\n", override, pkgName, x.Name.Name) 275 + } 276 + } 277 + 278 + func (e *extracter) reportDecl(w io.Writer, x *ast.GenDecl) { 279 + if x.Tok != token.CONST { 280 + return 281 + } 282 + k := 0 283 + for _, s := range x.Specs { 284 + if v, ok := s.(*ast.ValueSpec); ok && !filter(v.Names[0].Name) { 285 + if v.Values == nil { 286 + v.Values = make([]ast.Expr, len(v.Names)) 287 + } 288 + for i, expr := range v.Names { 289 + // This check can be removed if we set constants to floats. 290 + if _, ok := v.Values[i].(*ast.BasicLit); ok { 291 + continue 292 + } 293 + tv, _ := types.Eval(e.prog.Fset, e.pkg.Pkg, v.Pos(), v.Names[0].Name) 294 + tok := token.ILLEGAL 295 + switch tv.Value.Kind() { 296 + case constant.Bool: 297 + v.Values[i] = ast.NewIdent(tv.Value.ExactString()) 298 + continue 299 + case constant.String: 300 + tok = token.STRING 301 + case constant.Int: 302 + tok = token.INT 303 + case constant.Float: 304 + tok = token.FLOAT 305 + default: 306 + fmt.Printf("Skipping %s\n", v.Names) 307 + continue 308 + } 309 + v.Values[i] = &ast.BasicLit{ 310 + ValuePos: expr.Pos(), 311 + Kind: tok, 312 + Value: tv.Value.ExactString(), 313 + } 314 + } 315 + v.Type = nil 316 + x.Specs[k] = v 317 + k++ 318 + } 319 + } 320 + x.Specs = x.Specs[:k] 321 + if len(x.Specs) == 0 { 322 + return 323 + } 324 + fmt.Fprintln(w) 325 + printer.Fprint(w, e.prog.Fset, x) 326 + fmt.Fprintln(w) 327 + }
+34
pkg/crypto/md5/md5.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo extract crypto/md5 20 + 21 + package md5 22 + 23 + import "crypto/md5" 24 + 25 + // The size of an MD5 checksum in bytes. 26 + const Size = 16 27 + 28 + // The blocksize of MD5 in bytes. 29 + const BlockSize = 64 30 + 31 + // Sum returns the MD5 checksum of the data. 32 + func Sum(data []byte) [Size]byte { 33 + return md5.Sum(data) 34 + }
+34
pkg/crypto/sha1/sha1.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo extract crypto/sha1 20 + 21 + package sha1 22 + 23 + import "crypto/sha1" 24 + 25 + // The size of a SHA-1 checksum in bytes. 26 + const Size = 20 27 + 28 + // The blocksize of SHA-1 in bytes. 29 + const BlockSize = 64 30 + 31 + // Sum returns the SHA-1 checksum of the data. 32 + func Sum(data []byte) [Size]byte { 33 + return sha1.Sum(data) 34 + }
+42
pkg/crypto/sha256/sha256.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo extract crypto/sha256 20 + 21 + package sha256 22 + 23 + import "crypto/sha256" 24 + 25 + // The size of a SHA256 checksum in bytes. 26 + const Size = 32 27 + 28 + // The size of a SHA224 checksum in bytes. 29 + const Size224 = 28 30 + 31 + // The blocksize of SHA256 and SHA224 in bytes. 32 + const BlockSize = 64 33 + 34 + // Sum256 returns the SHA256 checksum of the data. 35 + func Sum256(data []byte) [Size]byte { 36 + return sha256.Sum256(data) 37 + } 38 + 39 + // Sum224 returns the SHA224 checksum of the data. 40 + func Sum224(data []byte) (sum224 [Size224]byte) { 41 + return sha256.Sum224(data) 42 + }
+61
pkg/crypto/sha512/sha512.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo extract crypto/sha512 20 + 21 + package sha512 22 + 23 + import "crypto/sha512" 24 + 25 + const ( 26 + // Size is the size, in bytes, of a SHA-512 checksum. 27 + Size = 64 28 + 29 + // Size224 is the size, in bytes, of a SHA-512/224 checksum. 30 + Size224 = 28 31 + 32 + // Size256 is the size, in bytes, of a SHA-512/256 checksum. 33 + Size256 = 32 34 + 35 + // Size384 is the size, in bytes, of a SHA-384 checksum. 36 + Size384 = 48 37 + 38 + // BlockSize is the block size, in bytes, of the SHA-512/224, 39 + // SHA-512/256, SHA-384 and SHA-512 hash functions. 40 + BlockSize = 128 41 + ) 42 + 43 + // Sum512 returns the SHA512 checksum of the data. 44 + func Sum512(data []byte) [Size]byte { 45 + return sha512.Sum512(data) 46 + } 47 + 48 + // Sum384 returns the SHA384 checksum of the data. 49 + func Sum384(data []byte) (sum384 [Size384]byte) { 50 + return sha512.Sum384(data) 51 + } 52 + 53 + // Sum512_224 returns the Sum512/224 checksum of the data. 54 + func Sum512_224(data []byte) (sum224 [Size224]byte) { 55 + return sha512.Sum512_224(data) 56 + } 57 + 58 + // Sum512_256 returns the Sum512/256 checksum of the data. 59 + func Sum512_256(data []byte) (sum256 [Size256]byte) { 60 + return sha512.Sum512_256(data) 61 + }
+36
pkg/doc.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Package pkg define CUE builtin packages. 16 + // 17 + // The CUE core packages are different, but closely mimic the Go core packages. 18 + // The types, values, and functions are defined as their Go equivalence and 19 + // mapped to CUE types. 20 + // 21 + // Beware that some packages are defined in lesser-precision types than are 22 + // typically used in CUE and thus may lead to loss of precision. 23 + // 24 + // All packages in these subdirectories are hermetic. That is, the results 25 + // do not change based on environment conditions. That is: 26 + // 27 + // - no reading of files contents 28 + // - no querying of the file system of any kind 29 + // - no communication on the network 30 + // - no information about the type of environment 31 + // - only reproduceable random generators 32 + // 33 + // Hermetic configurations allow for fast and advanced analysis that otherwise 34 + // would not be possible or practical. The cue "cmd" command can be used to 35 + // mix in non-hermetic influences into configurations. 36 + package pkg
+51
pkg/encoding/hex/hex.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo -stripstr -exclude=Decode$,Encode$,EncodeToString,Dumper extract encoding/hex 20 + 21 + package hex 22 + 23 + import "encoding/hex" 24 + 25 + // EncodedLen returns the length of an encoding of n source bytes. 26 + // Specifically, it returns n * 2. 27 + func EncodedLen(n int) int { 28 + return hex.EncodedLen(n) 29 + } 30 + 31 + // DecodedLen returns the length of a decoding of x source bytes. 32 + // Specifically, it returns x / 2. 33 + func DecodedLen(x int) int { 34 + return hex.DecodedLen(x) 35 + } 36 + 37 + // Decode returns the bytes represented by the hexadecimal string s. 38 + // 39 + // Decode expects that src contains only hexadecimal 40 + // characters and that src has even length. 41 + // If the input is malformed, Decode returns 42 + // the bytes decoded before the error. 43 + func Decode(s string) ([]byte, error) { 44 + return hex.DecodeString(s) 45 + } 46 + 47 + // Dump returns a string that contains a hex dump of the given data. The format 48 + // of the hex dump matches the output of `hexdump -C` on the command line. 49 + func Dump(data []byte) string { 50 + return hex.Dump(data) 51 + }
+28
pkg/encoding/json/json.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo -exclude=Compact,Indent,arshal$ extract encoding/json 20 + 21 + package json 22 + 23 + import "encoding/json" 24 + 25 + // Valid reports whether data is a valid JSON encoding. 26 + func Valid(data []byte) bool { 27 + return json.Valid(data) 28 + }
+40
pkg/html/html.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo -stripstr extract html 20 + 21 + package html 22 + 23 + import "html" 24 + 25 + // Escape escapes special characters like "<" to become "&lt;". It 26 + // escapes only five such characters: <, >, &, ' and ". 27 + // UnescapeString(Escape(s)) == s always holds, but the converse isn't 28 + // always true. 29 + func Escape(s string) string { 30 + return html.EscapeString(s) 31 + } 32 + 33 + // Unescape unescapes entities like "&lt;" to become "<". It unescapes a 34 + // larger range of entities than EscapeString escapes. For example, "&aacute;" 35 + // unescapes to "á", as does "&#225;" and "&#xE1;". 36 + // Unescape(EscapeString(s)) == s always holds, but the converse isn't 37 + // always true. 38 + func Unescape(s string) string { 39 + return html.UnescapeString(s) 40 + }
+56
pkg/math/big.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo -exclude= extract math/big 20 + 21 + package math 22 + 23 + import "math/big" 24 + 25 + // Exponent and precision limits. 26 + const ( 27 + MaxExp = 2147483647 // largest supported exponent 28 + MinExp = -2147483648 // smallest supported exponent 29 + MaxPrec = 4294967295 // largest (theoretically) supported precision; likely memory-limited 30 + ) 31 + 32 + // These constants define supported rounding modes. 33 + const ( 34 + ToNearestEven = 0 // == IEEE 754-2008 roundTiesToEven 35 + ToNearestAway = 1 // == IEEE 754-2008 roundTiesToAway 36 + ToZero = 2 // == IEEE 754-2008 roundTowardZero 37 + AwayFromZero = 3 // no IEEE 754-2008 equivalent 38 + ToNegativeInf = 4 // == IEEE 754-2008 roundTowardNegative 39 + ToPositiveInf = 5 // == IEEE 754-2008 roundTowardPositive 40 + ) 41 + 42 + // Constants describing the Accuracy of a Float. 43 + const ( 44 + Below = -1 45 + Exact = 0 46 + Above = 1 47 + ) 48 + 49 + // Jacobi returns the Jacobi symbol (x/y), either +1, -1, or 0. 50 + // The y argument must be an odd integer. 51 + func Jacobi(x, y *big.Int) int { 52 + return big.Jacobi(x, y) 53 + } 54 + 55 + // MaxBase is the largest number base accepted for string conversions. 56 + const MaxBase = 62
+522
pkg/math/math.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo -exclude=32,^Next,^Max,^Smallest,^Min,bits,Inf,NaN,Round,Trunc,Ceil,Floor$ extract math 20 + 21 + package math 22 + 23 + import "math" 24 + 25 + // Abs returns the absolute value of x. 26 + // 27 + // Special cases are: 28 + // Abs(±Inf) = +Inf 29 + // Abs(NaN) = NaN 30 + func Abs(x float64) float64 { 31 + return math.Abs(x) 32 + } 33 + 34 + // Acosh returns the inverse hyperbolic cosine of x. 35 + // 36 + // Special cases are: 37 + // Acosh(+Inf) = +Inf 38 + // Acosh(x) = NaN if x < 1 39 + // Acosh(NaN) = NaN 40 + func Acosh(x float64) float64 { 41 + return math.Acosh(x) 42 + } 43 + 44 + // Asin returns the arcsine, in radians, of x. 45 + // 46 + // Special cases are: 47 + // Asin(±0) = ±0 48 + // Asin(x) = NaN if x < -1 or x > 1 49 + func Asin(x float64) float64 { 50 + return math.Asin(x) 51 + } 52 + 53 + // Acos returns the arccosine, in radians, of x. 54 + // 55 + // Special case is: 56 + // Acos(x) = NaN if x < -1 or x > 1 57 + func Acos(x float64) float64 { 58 + return math.Acos(x) 59 + } 60 + 61 + // Asinh returns the inverse hyperbolic sine of x. 62 + // 63 + // Special cases are: 64 + // Asinh(±0) = ±0 65 + // Asinh(±Inf) = ±Inf 66 + // Asinh(NaN) = NaN 67 + func Asinh(x float64) float64 { 68 + return math.Asinh(x) 69 + } 70 + 71 + // Atan returns the arctangent, in radians, of x. 72 + // 73 + // Special cases are: 74 + // Atan(±0) = ±0 75 + // Atan(±Inf) = ±Pi/2 76 + func Atan(x float64) float64 { 77 + return math.Atan(x) 78 + } 79 + 80 + // Atan2 returns the arc tangent of y/x, using 81 + // the signs of the two to determine the quadrant 82 + // of the return value. 83 + // 84 + // Special cases are (in order): 85 + // Atan2(y, NaN) = NaN 86 + // Atan2(NaN, x) = NaN 87 + // Atan2(+0, x>=0) = +0 88 + // Atan2(-0, x>=0) = -0 89 + // Atan2(+0, x<=-0) = +Pi 90 + // Atan2(-0, x<=-0) = -Pi 91 + // Atan2(y>0, 0) = +Pi/2 92 + // Atan2(y<0, 0) = -Pi/2 93 + // Atan2(+Inf, +Inf) = +Pi/4 94 + // Atan2(-Inf, +Inf) = -Pi/4 95 + // Atan2(+Inf, -Inf) = 3Pi/4 96 + // Atan2(-Inf, -Inf) = -3Pi/4 97 + // Atan2(y, +Inf) = 0 98 + // Atan2(y>0, -Inf) = +Pi 99 + // Atan2(y<0, -Inf) = -Pi 100 + // Atan2(+Inf, x) = +Pi/2 101 + // Atan2(-Inf, x) = -Pi/2 102 + func Atan2(y, x float64) float64 { 103 + return math.Atan2(y, x) 104 + } 105 + 106 + // Atanh returns the inverse hyperbolic tangent of x. 107 + // 108 + // Special cases are: 109 + // Atanh(1) = +Inf 110 + // Atanh(±0) = ±0 111 + // Atanh(-1) = -Inf 112 + // Atanh(x) = NaN if x < -1 or x > 1 113 + // Atanh(NaN) = NaN 114 + func Atanh(x float64) float64 { 115 + return math.Atanh(x) 116 + } 117 + 118 + // Cbrt returns the cube root of x. 119 + // 120 + // Special cases are: 121 + // Cbrt(±0) = ±0 122 + // Cbrt(±Inf) = ±Inf 123 + // Cbrt(NaN) = NaN 124 + func Cbrt(x float64) float64 { 125 + return math.Cbrt(x) 126 + } 127 + 128 + // Mathematical constants. 129 + const ( 130 + E = 2.71828182845904523536028747135266249775724709369995957496696763 // https://oeis.org/A001113 131 + Pi = 3.14159265358979323846264338327950288419716939937510582097494459 // https://oeis.org/A000796 132 + Phi = 1.61803398874989484820458683436563811772030917980576286213544862 // https://oeis.org/A001622 133 + 134 + Sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974 // https://oeis.org/A002193 135 + SqrtE = 1.64872127070012814684865078781416357165377610071014801157507931 // https://oeis.org/A019774 136 + SqrtPi = 1.77245385090551602729816748334114518279754945612238712821380779 // https://oeis.org/A002161 137 + SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038 // https://oeis.org/A139339 138 + 139 + Ln2 = 0.693147180559945309417232121458176568075500134360255254120680009 // https://oeis.org/A002162 140 + Log2E = 1000000000000000000000000000000000000000000000000000000000000000 / 693147180559945309417232121458176568075500134360255254120680009 141 + Ln10 = 2.30258509299404568401799145468436420760110148862877297603332790 // https://oeis.org/A002392 142 + Log10E = 10000000000000000000000000000000000000000000000000000000000000 / 23025850929940456840179914546843642076011014886287729760333279 143 + ) 144 + 145 + // Copysign returns a value with the magnitude 146 + // of x and the sign of y. 147 + func Copysign(x, y float64) float64 { 148 + return math.Copysign(x, y) 149 + } 150 + 151 + // Dim returns the maximum of x-y or 0. 152 + // 153 + // Special cases are: 154 + // Dim(+Inf, +Inf) = NaN 155 + // Dim(-Inf, -Inf) = NaN 156 + // Dim(x, NaN) = Dim(NaN, x) = NaN 157 + func Dim(x, y float64) float64 { 158 + return math.Dim(x, y) 159 + } 160 + 161 + // Erf returns the error function of x. 162 + // 163 + // Special cases are: 164 + // Erf(+Inf) = 1 165 + // Erf(-Inf) = -1 166 + // Erf(NaN) = NaN 167 + func Erf(x float64) float64 { 168 + return math.Erf(x) 169 + } 170 + 171 + // Erfc returns the complementary error function of x. 172 + // 173 + // Special cases are: 174 + // Erfc(+Inf) = 0 175 + // Erfc(-Inf) = 2 176 + // Erfc(NaN) = NaN 177 + func Erfc(x float64) float64 { 178 + return math.Erfc(x) 179 + } 180 + 181 + // Erfinv returns the inverse error function of x. 182 + // 183 + // Special cases are: 184 + // Erfinv(1) = +Inf 185 + // Erfinv(-1) = -Inf 186 + // Erfinv(x) = NaN if x < -1 or x > 1 187 + // Erfinv(NaN) = NaN 188 + func Erfinv(x float64) float64 { 189 + return math.Erfinv(x) 190 + } 191 + 192 + // Erfcinv returns the inverse of Erfc(x). 193 + // 194 + // Special cases are: 195 + // Erfcinv(0) = +Inf 196 + // Erfcinv(2) = -Inf 197 + // Erfcinv(x) = NaN if x < 0 or x > 2 198 + // Erfcinv(NaN) = NaN 199 + func Erfcinv(x float64) float64 { 200 + return math.Erfcinv(x) 201 + } 202 + 203 + // Exp returns e**x, the base-e exponential of x. 204 + // 205 + // Special cases are: 206 + // Exp(+Inf) = +Inf 207 + // Exp(NaN) = NaN 208 + // Very large values overflow to 0 or +Inf. 209 + // Very small values underflow to 1. 210 + func Exp(x float64) float64 { 211 + return math.Exp(x) 212 + } 213 + 214 + // Exp2 returns 2**x, the base-2 exponential of x. 215 + // 216 + // Special cases are the same as Exp. 217 + func Exp2(x float64) float64 { 218 + return math.Exp2(x) 219 + } 220 + 221 + // Expm1 returns e**x - 1, the base-e exponential of x minus 1. 222 + // It is more accurate than Exp(x) - 1 when x is near zero. 223 + // 224 + // Special cases are: 225 + // Expm1(+Inf) = +Inf 226 + // Expm1(-Inf) = -1 227 + // Expm1(NaN) = NaN 228 + // Very large values overflow to -1 or +Inf. 229 + func Expm1(x float64) float64 { 230 + return math.Expm1(x) 231 + } 232 + 233 + // Gamma returns the Gamma function of x. 234 + // 235 + // Special cases are: 236 + // Gamma(+Inf) = +Inf 237 + // Gamma(+0) = +Inf 238 + // Gamma(-0) = -Inf 239 + // Gamma(x) = NaN for integer x < 0 240 + // Gamma(-Inf) = NaN 241 + // Gamma(NaN) = NaN 242 + func Gamma(x float64) float64 { 243 + return math.Gamma(x) 244 + } 245 + 246 + // Hypot returns Sqrt(p*p + q*q), taking care to avoid 247 + // unnecessary overflow and underflow. 248 + // 249 + // Special cases are: 250 + // Hypot(±Inf, q) = +Inf 251 + // Hypot(p, ±Inf) = +Inf 252 + // Hypot(NaN, q) = NaN 253 + // Hypot(p, NaN) = NaN 254 + func Hypot(p, q float64) float64 { 255 + return math.Hypot(p, q) 256 + } 257 + 258 + // J0 returns the order-zero Bessel function of the first kind. 259 + // 260 + // Special cases are: 261 + // J0(±Inf) = 0 262 + // J0(0) = 1 263 + // J0(NaN) = NaN 264 + func J0(x float64) float64 { 265 + return math.J0(x) 266 + } 267 + 268 + // Y0 returns the order-zero Bessel function of the second kind. 269 + // 270 + // Special cases are: 271 + // Y0(+Inf) = 0 272 + // Y0(0) = -Inf 273 + // Y0(x < 0) = NaN 274 + // Y0(NaN) = NaN 275 + func Y0(x float64) float64 { 276 + return math.Y0(x) 277 + } 278 + 279 + // J1 returns the order-one Bessel function of the first kind. 280 + // 281 + // Special cases are: 282 + // J1(±Inf) = 0 283 + // J1(NaN) = NaN 284 + func J1(x float64) float64 { 285 + return math.J1(x) 286 + } 287 + 288 + // Y1 returns the order-one Bessel function of the second kind. 289 + // 290 + // Special cases are: 291 + // Y1(+Inf) = 0 292 + // Y1(0) = -Inf 293 + // Y1(x < 0) = NaN 294 + // Y1(NaN) = NaN 295 + func Y1(x float64) float64 { 296 + return math.Y1(x) 297 + } 298 + 299 + // Jn returns the order-n Bessel function of the first kind. 300 + // 301 + // Special cases are: 302 + // Jn(n, ±Inf) = 0 303 + // Jn(n, NaN) = NaN 304 + func Jn(n int, x float64) float64 { 305 + return math.Jn(n, x) 306 + } 307 + 308 + // Yn returns the order-n Bessel function of the second kind. 309 + // 310 + // Special cases are: 311 + // Yn(n, +Inf) = 0 312 + // Yn(n ≥ 0, 0) = -Inf 313 + // Yn(n < 0, 0) = +Inf if n is odd, -Inf if n is even 314 + // Yn(n, x < 0) = NaN 315 + // Yn(n, NaN) = NaN 316 + func Yn(n int, x float64) float64 { 317 + return math.Yn(n, x) 318 + } 319 + 320 + // Ldexp is the inverse of Frexp. 321 + // It returns frac × 2**exp. 322 + // 323 + // Special cases are: 324 + // Ldexp(±0, exp) = ±0 325 + // Ldexp(±Inf, exp) = ±Inf 326 + // Ldexp(NaN, exp) = NaN 327 + func Ldexp(frac float64, exp int) float64 { 328 + return math.Ldexp(frac, exp) 329 + } 330 + 331 + // Log returns the natural logarithm of x. 332 + // 333 + // Special cases are: 334 + // Log(+Inf) = +Inf 335 + // Log(0) = -Inf 336 + // Log(x < 0) = NaN 337 + // Log(NaN) = NaN 338 + func Log(x float64) float64 { 339 + return math.Log(x) 340 + } 341 + 342 + // Log10 returns the decimal logarithm of x. 343 + // The special cases are the same as for Log. 344 + func Log10(x float64) float64 { 345 + return math.Log10(x) 346 + } 347 + 348 + // Log2 returns the binary logarithm of x. 349 + // The special cases are the same as for Log. 350 + func Log2(x float64) float64 { 351 + return math.Log2(x) 352 + } 353 + 354 + // Log1p returns the natural logarithm of 1 plus its argument x. 355 + // It is more accurate than Log(1 + x) when x is near zero. 356 + // 357 + // Special cases are: 358 + // Log1p(+Inf) = +Inf 359 + // Log1p(±0) = ±0 360 + // Log1p(-1) = -Inf 361 + // Log1p(x < -1) = NaN 362 + // Log1p(NaN) = NaN 363 + func Log1p(x float64) float64 { 364 + return math.Log1p(x) 365 + } 366 + 367 + // Logb returns the binary exponent of x. 368 + // 369 + // Special cases are: 370 + // Logb(±Inf) = +Inf 371 + // Logb(0) = -Inf 372 + // Logb(NaN) = NaN 373 + func Logb(x float64) float64 { 374 + return math.Logb(x) 375 + } 376 + 377 + // Ilogb returns the binary exponent of x as an integer. 378 + // 379 + // Special cases are: 380 + // Ilogb(±Inf) = MaxInt32 381 + // Ilogb(0) = MinInt32 382 + // Ilogb(NaN) = MaxInt32 383 + func Ilogb(x float64) int { 384 + return math.Ilogb(x) 385 + } 386 + 387 + // Mod returns the floating-point remainder of x/y. 388 + // The magnitude of the result is less than y and its 389 + // sign agrees with that of x. 390 + // 391 + // Special cases are: 392 + // Mod(±Inf, y) = NaN 393 + // Mod(NaN, y) = NaN 394 + // Mod(x, 0) = NaN 395 + // Mod(x, ±Inf) = x 396 + // Mod(x, NaN) = NaN 397 + func Mod(x, y float64) float64 { 398 + return math.Mod(x, y) 399 + } 400 + 401 + // Pow returns x**y, the base-x exponential of y. 402 + // 403 + // Special cases are (in order): 404 + // Pow(x, ±0) = 1 for any x 405 + // Pow(1, y) = 1 for any y 406 + // Pow(x, 1) = x for any x 407 + // Pow(NaN, y) = NaN 408 + // Pow(x, NaN) = NaN 409 + // Pow(±0, y) = ±Inf for y an odd integer < 0 410 + // Pow(±0, -Inf) = +Inf 411 + // Pow(±0, +Inf) = +0 412 + // Pow(±0, y) = +Inf for finite y < 0 and not an odd integer 413 + // Pow(±0, y) = ±0 for y an odd integer > 0 414 + // Pow(±0, y) = +0 for finite y > 0 and not an odd integer 415 + // Pow(-1, ±Inf) = 1 416 + // Pow(x, +Inf) = +Inf for |x| > 1 417 + // Pow(x, -Inf) = +0 for |x| > 1 418 + // Pow(x, +Inf) = +0 for |x| < 1 419 + // Pow(x, -Inf) = +Inf for |x| < 1 420 + // Pow(+Inf, y) = +Inf for y > 0 421 + // Pow(+Inf, y) = +0 for y < 0 422 + // Pow(-Inf, y) = Pow(-0, -y) 423 + // Pow(x, y) = NaN for finite x < 0 and finite non-integer y 424 + func Pow(x, y float64) float64 { 425 + return math.Pow(x, y) 426 + } 427 + 428 + // Pow10 returns 10**n, the base-10 exponential of n. 429 + // 430 + // Special cases are: 431 + // Pow10(n) = 0 for n < -323 432 + // Pow10(n) = +Inf for n > 308 433 + func Pow10(n int) float64 { 434 + return math.Pow10(n) 435 + } 436 + 437 + // Remainder returns the IEEE 754 floating-point remainder of x/y. 438 + // 439 + // Special cases are: 440 + // Remainder(±Inf, y) = NaN 441 + // Remainder(NaN, y) = NaN 442 + // Remainder(x, 0) = NaN 443 + // Remainder(x, ±Inf) = x 444 + // Remainder(x, NaN) = NaN 445 + func Remainder(x, y float64) float64 { 446 + return math.Remainder(x, y) 447 + } 448 + 449 + // Signbit returns true if x is negative or negative zero. 450 + func Signbit(x float64) bool { 451 + return math.Signbit(x) 452 + } 453 + 454 + // Cos returns the cosine of the radian argument x. 455 + // 456 + // Special cases are: 457 + // Cos(±Inf) = NaN 458 + // Cos(NaN) = NaN 459 + func Cos(x float64) float64 { 460 + return math.Cos(x) 461 + } 462 + 463 + // Sin returns the sine of the radian argument x. 464 + // 465 + // Special cases are: 466 + // Sin(±0) = ±0 467 + // Sin(±Inf) = NaN 468 + // Sin(NaN) = NaN 469 + func Sin(x float64) float64 { 470 + return math.Sin(x) 471 + } 472 + 473 + // Sinh returns the hyperbolic sine of x. 474 + // 475 + // Special cases are: 476 + // Sinh(±0) = ±0 477 + // Sinh(±Inf) = ±Inf 478 + // Sinh(NaN) = NaN 479 + func Sinh(x float64) float64 { 480 + return math.Sinh(x) 481 + } 482 + 483 + // Cosh returns the hyperbolic cosine of x. 484 + // 485 + // Special cases are: 486 + // Cosh(±0) = 1 487 + // Cosh(±Inf) = +Inf 488 + // Cosh(NaN) = NaN 489 + func Cosh(x float64) float64 { 490 + return math.Cosh(x) 491 + } 492 + 493 + // Sqrt returns the square root of x. 494 + // 495 + // Special cases are: 496 + // Sqrt(+Inf) = +Inf 497 + // Sqrt(±0) = ±0 498 + // Sqrt(x < 0) = NaN 499 + // Sqrt(NaN) = NaN 500 + func Sqrt(x float64) float64 { 501 + return math.Sqrt(x) 502 + } 503 + 504 + // Tan returns the tangent of the radian argument x. 505 + // 506 + // Special cases are: 507 + // Tan(±0) = ±0 508 + // Tan(±Inf) = NaN 509 + // Tan(NaN) = NaN 510 + func Tan(x float64) float64 { 511 + return math.Tan(x) 512 + } 513 + 514 + // Tanh returns the hyperbolic tangent of x. 515 + // 516 + // Special cases are: 517 + // Tanh(±0) = ±0 518 + // Tanh(±Inf) = ±1 519 + // Tanh(NaN) = NaN 520 + func Tanh(x float64) float64 { 521 + return math.Tanh(x) 522 + }
+104
pkg/path/path.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo -exclude=Split,Join extract path 20 + 21 + package path 22 + 23 + import "path" 24 + 25 + // Match reports whether name matches the shell pattern. 26 + // The pattern syntax is: 27 + // 28 + // pattern: 29 + // { term } 30 + // term: 31 + // '*' matches any sequence of non-/ characters 32 + // '?' matches any single non-/ character 33 + // '[' [ '^' ] { character-range } ']' 34 + // character class (must be non-empty) 35 + // c matches character c (c != '*', '?', '\\', '[') 36 + // '\\' c matches character c 37 + // 38 + // character-range: 39 + // c matches character c (c != '\\', '-', ']') 40 + // '\\' c matches character c 41 + // lo '-' hi matches character c for lo <= c <= hi 42 + // 43 + // Match requires pattern to match all of name, not just a substring. 44 + // The only possible returned error is ErrBadPattern, when pattern 45 + // is malformed. 46 + // 47 + func Match(pattern, name string) (matched bool, err error) { 48 + return path.Match(pattern, name) 49 + } 50 + 51 + // Clean returns the shortest path name equivalent to path 52 + // by purely lexical processing. It applies the following rules 53 + // iteratively until no further processing can be done: 54 + // 55 + // 1. Replace multiple slashes with a single slash. 56 + // 2. Eliminate each . path name element (the current directory). 57 + // 3. Eliminate each inner .. path name element (the parent directory) 58 + // along with the non-.. element that precedes it. 59 + // 4. Eliminate .. elements that begin a rooted path: 60 + // that is, replace "/.." by "/" at the beginning of a path. 61 + // 62 + // The returned path ends in a slash only if it is the root "/". 63 + // 64 + // If the result of this process is an empty string, Clean 65 + // returns the string ".". 66 + // 67 + // See also Rob Pike, ``Lexical File Names in Plan 9 or 68 + // Getting Dot-Dot Right,'' 69 + // https://9p.io/sys/doc/lexnames.html 70 + func Clean(path string) string { return pathClean(path) } 71 + 72 + var pathClean = path.Clean 73 + 74 + // Ext returns the file name extension used by path. 75 + // The extension is the suffix beginning at the final dot 76 + // in the final slash-separated element of path; 77 + // it is empty if there is no dot. 78 + func Ext(path string) string { return pathExt(path) } 79 + 80 + var pathExt = path.Ext 81 + 82 + // Base returns the last element of path. 83 + // Trailing slashes are removed before extracting the last element. 84 + // If the path is empty, Base returns ".". 85 + // If the path consists entirely of slashes, Base returns "/". 86 + func Base(path string) string { return pathBase(path) } 87 + 88 + var pathBase = path.Base 89 + 90 + // IsAbs reports whether the path is absolute. 91 + func IsAbs(path string) bool { return pathIsAbs(path) } 92 + 93 + var pathIsAbs = path.IsAbs 94 + 95 + // Dir returns all but the last element of path, typically the path's directory. 96 + // After dropping the final element using Split, the path is Cleaned and trailing 97 + // slashes are removed. 98 + // If the path is empty, Dir returns ".". 99 + // If the path consists entirely of slashes followed by non-slash bytes, Dir 100 + // returns a single slash. In any other case, the returned path does not end in a 101 + // slash. 102 + func Dir(path string) string { return pathDir(path) } 103 + 104 + var pathDir = path.Dir
+37
pkg/regexp/regexp.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo -exclude=Compile,Append,Reader,Match$ -stripstr extract regexp 20 + 21 + package regexp 22 + 23 + import "regexp" 24 + 25 + // Match reports whether the string s 26 + // contains any match of the regular expression pattern. 27 + // More complicated queries need to use Compile and the full Regexp interface. 28 + func Match(pattern string, s string) (matched bool, err error) { 29 + return regexp.MatchString(pattern, s) 30 + } 31 + 32 + // QuoteMeta returns a string that escapes all regular expression metacharacters 33 + // inside the argument text; the returned string is a regular expression matching 34 + // the literal text. 35 + func QuoteMeta(s string) string { 36 + return regexp.QuoteMeta(s) 37 + }
+196
pkg/strconv/strconv.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo -exclude=Append,Unquote,Itoa extract strconv 20 + 21 + package strconv 22 + 23 + import "strconv" 24 + 25 + // ParseBool returns the boolean value represented by the string. 26 + // It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. 27 + // Any other value returns an error. 28 + func ParseBool(str string) (bool, error) { 29 + return strconv.ParseBool(str) 30 + } 31 + 32 + // FormatBool returns "true" or "false" according to the value of b. 33 + func FormatBool(b bool) string { 34 + return strconv.FormatBool(b) 35 + } 36 + 37 + // ParseFloat converts the string s to a floating-point number 38 + // with the precision specified by bitSize: 32 for float32, or 64 for float64. 39 + // When bitSize=32, the result still has type float64, but it will be 40 + // convertible to float32 without changing its value. 41 + // 42 + // If s is well-formed and near a valid floating point number, 43 + // ParseFloat returns the nearest floating point number rounded 44 + // using IEEE754 unbiased rounding. 45 + // 46 + // The errors that ParseFloat returns have concrete type *NumError 47 + // and include err.Num = s. 48 + // 49 + // If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax. 50 + // 51 + // If s is syntactically well-formed but is more than 1/2 ULP 52 + // away from the largest floating point number of the given size, 53 + // ParseFloat returns f = ±Inf, err.Err = ErrRange. 54 + func ParseFloat(s string, bitSize int) (float64, error) { 55 + return strconv.ParseFloat(s, bitSize) 56 + } 57 + 58 + // IntSize is the size in bits of an int or uint value. 59 + const IntSize = 64 60 + 61 + // ParseUint is like ParseInt but for unsigned numbers. 62 + func ParseUint(s string, base int, bitSize int) (uint64, error) { 63 + return strconv.ParseUint(s, base, bitSize) 64 + } 65 + 66 + // ParseInt interprets a string s in the given base (0, 2 to 36) and 67 + // bit size (0 to 64) and returns the corresponding value i. 68 + // 69 + // If base == 0, the base is implied by the string's prefix: 70 + // base 16 for "0x", base 8 for "0", and base 10 otherwise. 71 + // For bases 1, below 0 or above 36 an error is returned. 72 + // 73 + // The bitSize argument specifies the integer type 74 + // that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 75 + // correspond to int, int8, int16, int32, and int64. 76 + // For a bitSize below 0 or above 64 an error is returned. 77 + // 78 + // The errors that ParseInt returns have concrete type *NumError 79 + // and include err.Num = s. If s is empty or contains invalid 80 + // digits, err.Err = ErrSyntax and the returned value is 0; 81 + // if the value corresponding to s cannot be represented by a 82 + // signed integer of the given size, err.Err = ErrRange and the 83 + // returned value is the maximum magnitude integer of the 84 + // appropriate bitSize and sign. 85 + func ParseInt(s string, base int, bitSize int) (i int64, err error) { 86 + return strconv.ParseInt(s, base, bitSize) 87 + } 88 + 89 + // Atoi returns the result of ParseInt(s, 10, 0) converted to type int. 90 + func Atoi(s string) (int, error) { 91 + return strconv.Atoi(s) 92 + } 93 + 94 + // FormatFloat converts the floating-point number f to a string, 95 + // according to the format fmt and precision prec. It rounds the 96 + // result assuming that the original was obtained from a floating-point 97 + // value of bitSize bits (32 for float32, 64 for float64). 98 + // 99 + // The format fmt is one of 100 + // 'b' (-ddddp±ddd, a binary exponent), 101 + // 'e' (-d.dddde±dd, a decimal exponent), 102 + // 'E' (-d.ddddE±dd, a decimal exponent), 103 + // 'f' (-ddd.dddd, no exponent), 104 + // 'g' ('e' for large exponents, 'f' otherwise), or 105 + // 'G' ('E' for large exponents, 'f' otherwise). 106 + // 107 + // The precision prec controls the number of digits (excluding the exponent) 108 + // printed by the 'e', 'E', 'f', 'g', and 'G' formats. 109 + // For 'e', 'E', and 'f' it is the number of digits after the decimal point. 110 + // For 'g' and 'G' it is the maximum number of significant digits (trailing 111 + // zeros are removed). 112 + // The special precision -1 uses the smallest number of digits 113 + // necessary such that ParseFloat will return f exactly. 114 + func FormatFloat(f float64, fmt byte, prec, bitSize int) string { 115 + return strconv.FormatFloat(f, fmt, prec, bitSize) 116 + } 117 + 118 + // FormatUint returns the string representation of i in the given base, 119 + // for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' 120 + // for digit values >= 10. 121 + func FormatUint(i uint64, base int) string { 122 + return strconv.FormatUint(i, base) 123 + } 124 + 125 + // FormatInt returns the string representation of i in the given base, 126 + // for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' 127 + // for digit values >= 10. 128 + func FormatInt(i int64, base int) string { 129 + return strconv.FormatInt(i, base) 130 + } 131 + 132 + // Quote returns a double-quoted Go string literal representing s. The 133 + // returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for 134 + // control characters and non-printable characters as defined by 135 + // IsPrint. 136 + func Quote(s string) string { 137 + return strconv.Quote(s) 138 + } 139 + 140 + // QuoteToASCII returns a double-quoted Go string literal representing s. 141 + // The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for 142 + // non-ASCII characters and non-printable characters as defined by IsPrint. 143 + func QuoteToASCII(s string) string { 144 + return strconv.QuoteToASCII(s) 145 + } 146 + 147 + // QuoteToGraphic returns a double-quoted Go string literal representing s. 148 + // The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for 149 + // non-ASCII characters and non-printable characters as defined by IsGraphic. 150 + func QuoteToGraphic(s string) string { 151 + return strconv.QuoteToGraphic(s) 152 + } 153 + 154 + // QuoteRune returns a single-quoted Go character literal representing the 155 + // rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) 156 + // for control characters and non-printable characters as defined by IsPrint. 157 + func QuoteRune(r rune) string { 158 + return strconv.QuoteRune(r) 159 + } 160 + 161 + // QuoteRuneToASCII returns a single-quoted Go character literal representing 162 + // the rune. The returned string uses Go escape sequences (\t, \n, \xFF, 163 + // \u0100) for non-ASCII characters and non-printable characters as defined 164 + // by IsPrint. 165 + func QuoteRuneToASCII(r rune) string { 166 + return strconv.QuoteRuneToASCII(r) 167 + } 168 + 169 + // QuoteRuneToGraphic returns a single-quoted Go character literal representing 170 + // the rune. The returned string uses Go escape sequences (\t, \n, \xFF, 171 + // \u0100) for non-ASCII characters and non-printable characters as defined 172 + // by IsGraphic. 173 + func QuoteRuneToGraphic(r rune) string { 174 + return strconv.QuoteRuneToGraphic(r) 175 + } 176 + 177 + // CanBackquote reports whether the string s can be represented 178 + // unchanged as a single-line backquoted string without control 179 + // characters other than tab. 180 + func CanBackquote(s string) bool { 181 + return strconv.CanBackquote(s) 182 + } 183 + 184 + // IsPrint reports whether the rune is defined as printable by Go, with 185 + // the same definition as unicode.IsPrint: letters, numbers, punctuation, 186 + // symbols and ASCII space. 187 + func IsPrint(r rune) bool { 188 + return strconv.IsPrint(r) 189 + } 190 + 191 + // IsGraphic reports whether the rune is defined as a Graphic by Unicode. Such 192 + // characters include letters, marks, numbers, punctuation, symbols, and 193 + // spaces, from categories L, M, N, P, S, and Zs. 194 + func IsGraphic(r rune) bool { 195 + return strconv.IsGraphic(r) 196 + }
+219
pkg/strings/strings.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo -exclude=Rune$,Func$,^Map$,Special$,EqualFold,Byte,Title$ extract strings 20 + 21 + package strings 22 + 23 + import "strings" 24 + 25 + // Compare returns an integer comparing two strings lexicographically. 26 + // The result will be 0 if a==b, -1 if a < b, and +1 if a > b. 27 + // 28 + // Compare is included only for symmetry with package bytes. 29 + // It is usually clearer and always faster to use the built-in 30 + // string comparison operators ==, <, >, and so on. 31 + func Compare(a, b string) int { 32 + return strings.Compare(a, b) 33 + } 34 + 35 + // Count counts the number of non-overlapping instances of substr in s. 36 + // If substr is an empty string, Count returns 1 + the number of Unicode code points in s. 37 + func Count(s, substr string) int { 38 + return strings.Count(s, substr) 39 + } 40 + 41 + // Contains reports whether substr is within s. 42 + func Contains(s, substr string) bool { 43 + return strings.Contains(s, substr) 44 + } 45 + 46 + // ContainsAny reports whether any Unicode code points in chars are within s. 47 + func ContainsAny(s, chars string) bool { 48 + return strings.ContainsAny(s, chars) 49 + } 50 + 51 + // LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s. 52 + func LastIndex(s, substr string) int { 53 + return strings.LastIndex(s, substr) 54 + } 55 + 56 + // IndexAny returns the index of the first instance of any Unicode code point 57 + // from chars in s, or -1 if no Unicode code point from chars is present in s. 58 + func IndexAny(s, chars string) int { 59 + return strings.IndexAny(s, chars) 60 + } 61 + 62 + // LastIndexAny returns the index of the last instance of any Unicode code 63 + // point from chars in s, or -1 if no Unicode code point from chars is 64 + // present in s. 65 + func LastIndexAny(s, chars string) int { 66 + return strings.LastIndexAny(s, chars) 67 + } 68 + 69 + // SplitN slices s into substrings separated by sep and returns a slice of 70 + // the substrings between those separators. 71 + // 72 + // The count determines the number of substrings to return: 73 + // n > 0: at most n substrings; the last substring will be the unsplit remainder. 74 + // n == 0: the result is nil (zero substrings) 75 + // n < 0: all substrings 76 + // 77 + // Edge cases for s and sep (for example, empty strings) are handled 78 + // as described in the documentation for Split. 79 + func SplitN(s, sep string, n int) []string { 80 + return strings.SplitN(s, sep, n) 81 + } 82 + 83 + // SplitAfterN slices s into substrings after each instance of sep and 84 + // returns a slice of those substrings. 85 + // 86 + // The count determines the number of substrings to return: 87 + // n > 0: at most n substrings; the last substring will be the unsplit remainder. 88 + // n == 0: the result is nil (zero substrings) 89 + // n < 0: all substrings 90 + // 91 + // Edge cases for s and sep (for example, empty strings) are handled 92 + // as described in the documentation for SplitAfter. 93 + func SplitAfterN(s, sep string, n int) []string { 94 + return strings.SplitAfterN(s, sep, n) 95 + } 96 + 97 + // Split slices s into all substrings separated by sep and returns a slice of 98 + // the substrings between those separators. 99 + // 100 + // If s does not contain sep and sep is not empty, Split returns a 101 + // slice of length 1 whose only element is s. 102 + // 103 + // If sep is empty, Split splits after each UTF-8 sequence. If both s 104 + // and sep are empty, Split returns an empty slice. 105 + // 106 + // It is equivalent to SplitN with a count of -1. 107 + func Split(s, sep string) []string { 108 + return strings.Split(s, sep) 109 + } 110 + 111 + // SplitAfter slices s into all substrings after each instance of sep and 112 + // returns a slice of those substrings. 113 + // 114 + // If s does not contain sep and sep is not empty, SplitAfter returns 115 + // a slice of length 1 whose only element is s. 116 + // 117 + // If sep is empty, SplitAfter splits after each UTF-8 sequence. If 118 + // both s and sep are empty, SplitAfter returns an empty slice. 119 + // 120 + // It is equivalent to SplitAfterN with a count of -1. 121 + func SplitAfter(s, sep string) []string { 122 + return strings.SplitAfter(s, sep) 123 + } 124 + 125 + // Fields splits the string s around each instance of one or more consecutive white space 126 + // characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an 127 + // empty slice if s contains only white space. 128 + func Fields(s string) []string { 129 + return strings.Fields(s) 130 + } 131 + 132 + // Join concatenates the elements of a to create a single string. The separator string 133 + // sep is placed between elements in the resulting string. 134 + func Join(a []string, sep string) string { 135 + return strings.Join(a, sep) 136 + } 137 + 138 + // HasPrefix tests whether the string s begins with prefix. 139 + func HasPrefix(s, prefix string) bool { 140 + return strings.HasPrefix(s, prefix) 141 + } 142 + 143 + // HasSuffix tests whether the string s ends with suffix. 144 + func HasSuffix(s, suffix string) bool { 145 + return strings.HasSuffix(s, suffix) 146 + } 147 + 148 + // Repeat returns a new string consisting of count copies of the string s. 149 + // 150 + // It panics if count is negative or if 151 + // the result of (len(s) * count) overflows. 152 + func Repeat(s string, count int) string { 153 + return strings.Repeat(s, count) 154 + } 155 + 156 + // ToUpper returns a copy of the string s with all Unicode letters mapped to their upper case. 157 + func ToUpper(s string) string { 158 + return strings.ToUpper(s) 159 + } 160 + 161 + // ToLower returns a copy of the string s with all Unicode letters mapped to their lower case. 162 + func ToLower(s string) string { 163 + return strings.ToLower(s) 164 + } 165 + 166 + // Trim returns a slice of the string s with all leading and 167 + // trailing Unicode code points contained in cutset removed. 168 + func Trim(s string, cutset string) string { 169 + return strings.Trim(s, cutset) 170 + } 171 + 172 + // TrimLeft returns a slice of the string s with all leading 173 + // Unicode code points contained in cutset removed. 174 + // 175 + // To remove a prefix, use TrimPrefix instead. 176 + func TrimLeft(s string, cutset string) string { 177 + return strings.TrimLeft(s, cutset) 178 + } 179 + 180 + // TrimRight returns a slice of the string s, with all trailing 181 + // Unicode code points contained in cutset removed. 182 + // 183 + // To remove a suffix, use TrimSuffix instead. 184 + func TrimRight(s string, cutset string) string { 185 + return strings.TrimRight(s, cutset) 186 + } 187 + 188 + // TrimSpace returns a slice of the string s, with all leading 189 + // and trailing white space removed, as defined by Unicode. 190 + func TrimSpace(s string) string { 191 + return strings.TrimSpace(s) 192 + } 193 + 194 + // TrimPrefix returns s without the provided leading prefix string. 195 + // If s doesn't start with prefix, s is returned unchanged. 196 + func TrimPrefix(s, prefix string) string { 197 + return strings.TrimPrefix(s, prefix) 198 + } 199 + 200 + // TrimSuffix returns s without the provided trailing suffix string. 201 + // If s doesn't end with suffix, s is returned unchanged. 202 + func TrimSuffix(s, suffix string) string { 203 + return strings.TrimSuffix(s, suffix) 204 + } 205 + 206 + // Replace returns a copy of the string s with the first n 207 + // non-overlapping instances of old replaced by new. 208 + // If old is empty, it matches at the beginning of the string 209 + // and after each UTF-8 sequence, yielding up to k+1 replacements 210 + // for a k-rune string. 211 + // If n < 0, there is no limit on the number of replacements. 212 + func Replace(s, old, new string, n int) string { 213 + return strings.Replace(s, old, new, n) 214 + } 215 + 216 + // Index returns the index of the first instance of substr in s, or -1 if substr is not present in s. 217 + func Index(s, substr string) int { 218 + return strings.Index(s, substr) 219 + }
+33
pkg/text/template/template.go
··· 1 + // Copyright 2018 The CUE 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 + // http://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 + // Copyright 2018 The Go Authors. All rights reserved. 16 + // Use of this source code is governed by a BSD-style 17 + // license that can be found in the LICENSE file. 18 + 19 + //go:generate qgo -exclude=Escaper$,Must,Parse -stripstr extract text/template 20 + 21 + package template 22 + 23 + import "text/template" 24 + 25 + // HTMLEscape returns the escaped HTML equivalent of the plain text data s. 26 + func HTMLEscape(s string) string { 27 + return template.HTMLEscapeString(s) 28 + } 29 + 30 + // JSEscape returns the escaped JavaScript equivalent of the plain text data s. 31 + func JSEscape(s string) string { 32 + return template.JSEscapeString(s) 33 + }