···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// qgo builds CUE builtin packages from Go packages.
1616+package main
1717+1818+import (
1919+ "bytes"
2020+ "flag"
2121+ "fmt"
2222+ "go/ast"
2323+ "go/constant"
2424+ "go/format"
2525+ "go/parser"
2626+ "go/printer"
2727+ "go/token"
2828+ "go/types"
2929+ "io"
3030+ "io/ioutil"
3131+ "log"
3232+ "os"
3333+ "path/filepath"
3434+ "regexp"
3535+ "strings"
3636+3737+ "golang.org/x/tools/go/loader"
3838+)
3939+4040+const help = `
4141+Commands:
4242+extract Extract one-line signature of exported types of
4343+ the given package.
4444+4545+ Functions that have have more than one return
4646+ argument or unknown types are skipped.
4747+`
4848+4949+// Even though all of the code is generated, the documentation is copied as is.
5050+// So for proper measure, include both the CUE and Go licenses.
5151+const copyright = `// Copyright 2018 The CUE Authors
5252+//
5353+// Licensed under the Apache License, Version 2.0 (the "License");
5454+// you may not use this file except in compliance with the License.
5555+// You may obtain a copy of the License at
5656+//
5757+// http://www.apache.org/licenses/LICENSE-2.0
5858+//
5959+// Unless required by applicable law or agreed to in writing, software
6060+// distributed under the License is distributed on an "AS IS" BASIS,
6161+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6262+// See the License for the specific language governing permissions and
6363+// limitations under the License.
6464+6565+// Copyright 2018 The Go Authors. All rights reserved.
6666+// Use of this source code is governed by a BSD-style
6767+// license that can be found in the LICENSE file.
6868+`
6969+7070+var genLine string
7171+7272+var (
7373+ exclude = flag.String("exclude", "", "comma-separated list of regexps of entries to exclude")
7474+ stripstr = flag.Bool("stripstr", false, "Remove String suffix from functions")
7575+)
7676+7777+func init() {
7878+ log.SetFlags(log.Lshortfile)
7979+}
8080+8181+func main() {
8282+ flag.Parse()
8383+8484+ genLine = "//go:generate " + strings.Join(os.Args, " ")
8585+8686+ args := flag.Args()
8787+ if len(args) == 0 {
8888+ fmt.Println(strings.TrimSpace(help))
8989+ return
9090+ }
9191+9292+ command := args[0]
9393+ args = args[1:]
9494+9595+ switch command {
9696+ case "extract":
9797+ extract(args)
9898+ }
9999+}
100100+101101+var exclusions []*regexp.Regexp
102102+103103+func initExclusions() {
104104+ for _, re := range strings.Split(*exclude, ",") {
105105+ if re != "" {
106106+ exclusions = append(exclusions, regexp.MustCompile(re))
107107+ }
108108+ }
109109+}
110110+111111+func filter(name string) bool {
112112+ if !ast.IsExported(name) {
113113+ return true
114114+ }
115115+ for _, ex := range exclusions {
116116+ if ex.MatchString(name) {
117117+ return true
118118+ }
119119+ }
120120+ return false
121121+}
122122+123123+func pkgName() string {
124124+ pkg, err := os.Getwd()
125125+ if err != nil {
126126+ log.Fatal(err)
127127+ }
128128+ return filepath.Base(pkg)
129129+}
130130+131131+type extracter struct {
132132+ prog *loader.Program
133133+ pkg *loader.PackageInfo
134134+}
135135+136136+func extract(args []string) {
137137+138138+ cfg := loader.Config{
139139+ ParserMode: parser.ParseComments,
140140+ }
141141+ cfg.FromArgs(args, false)
142142+143143+ e := extracter{}
144144+ var err error
145145+ e.prog, err = cfg.Load()
146146+ if err != nil {
147147+ log.Fatal(err)
148148+ }
149149+150150+ lastPkg := ""
151151+ var w *bytes.Buffer
152152+ initExclusions()
153153+154154+ flushFile := func() {
155155+ if w != nil && w.Len() > 0 {
156156+ b, err := format.Source(w.Bytes())
157157+ if err != nil {
158158+ log.Fatal(err)
159159+ }
160160+ err = ioutil.WriteFile(lastPkg+".go", b, 0644)
161161+ if err != nil {
162162+ log.Fatal(err)
163163+ }
164164+ }
165165+ w = &bytes.Buffer{}
166166+ }
167167+168168+ for _, p := range e.prog.InitialPackages() {
169169+ e.pkg = p
170170+ for _, f := range p.Files {
171171+ if lastPkg != p.Pkg.Name() {
172172+ flushFile()
173173+ lastPkg = p.Pkg.Name()
174174+ fmt.Fprintln(w, copyright)
175175+ fmt.Fprintln(w, genLine)
176176+ fmt.Fprintln(w)
177177+ fmt.Fprintf(w, "package %s\n", pkgName())
178178+ fmt.Fprintln(w)
179179+ fmt.Fprintf(w, "import %q", p.Pkg.Path())
180180+ fmt.Fprintln(w)
181181+ }
182182+183183+ for _, d := range f.Decls {
184184+ switch x := d.(type) {
185185+ case *ast.FuncDecl:
186186+ e.reportFun(w, x)
187187+ case *ast.GenDecl:
188188+ e.reportDecl(w, x)
189189+ }
190190+ }
191191+ }
192192+ }
193193+ flushFile()
194194+}
195195+196196+func (e *extracter) reportFun(w io.Writer, x *ast.FuncDecl) {
197197+ if filter(x.Name.Name) {
198198+ return
199199+ }
200200+ pkgName := e.pkg.Pkg.Name()
201201+ override := ""
202202+ params := []ast.Expr{}
203203+ if x.Type.Params != nil {
204204+ for _, f := range x.Type.Params.List {
205205+ tx := f.Type
206206+ if star, isStar := tx.(*ast.StarExpr); isStar {
207207+ if i, ok := star.X.(*ast.Ident); ok && ast.IsExported(i.Name) {
208208+ f.Type = &ast.SelectorExpr{X: ast.NewIdent(pkgName), Sel: i}
209209+ if isStar {
210210+ f.Type = &ast.StarExpr{X: f.Type}
211211+ }
212212+ }
213213+ }
214214+ for _, n := range f.Names {
215215+ params = append(params, n)
216216+ if n.Name == pkgName {
217217+ override = pkgName + x.Name.Name
218218+ }
219219+ }
220220+ }
221221+ }
222222+ var fn ast.Expr = &ast.SelectorExpr{
223223+ X: ast.NewIdent(pkgName),
224224+ Sel: x.Name,
225225+ }
226226+ if override != "" {
227227+ fn = ast.NewIdent(override)
228228+ }
229229+ x.Body = &ast.BlockStmt{List: []ast.Stmt{
230230+ &ast.ReturnStmt{Results: []ast.Expr{&ast.CallExpr{
231231+ Fun: fn,
232232+ Args: params,
233233+ }}},
234234+ }}
235235+ if name := x.Name.Name; *stripstr && strings.HasSuffix(name, "String") {
236236+ newName := name[:len(name)-len("String")]
237237+ x.Name = ast.NewIdent(newName)
238238+ for _, c := range x.Doc.List {
239239+ c.Text = strings.Replace(c.Text, name, newName, -1)
240240+ }
241241+ }
242242+ types := []ast.Expr{}
243243+ if x.Recv == nil && x.Type != nil && x.Type.Results != nil && !strings.HasPrefix(x.Name.Name, "New") {
244244+ for _, f := range x.Type.Results.List {
245245+ if len(f.Names) == 0 {
246246+ types = append(types, f.Type)
247247+ } else {
248248+ for range f.Names {
249249+ types = append(types, f.Type)
250250+ }
251251+ }
252252+ }
253253+ }
254254+ if len(types) != 1 {
255255+ switch len(types) {
256256+ case 2:
257257+ if i, ok := types[1].(*ast.Ident); ok && i.Name == "error" {
258258+ break
259259+ }
260260+ fallthrough
261261+ default:
262262+ fmt.Printf("Skipping ")
263263+ x.Doc = nil
264264+ printer.Fprint(os.Stdout, e.prog.Fset, x)
265265+ fmt.Println()
266266+ return
267267+ }
268268+ }
269269+ fmt.Fprintln(w)
270270+ printer.Fprint(w, e.prog.Fset, x.Doc)
271271+ printer.Fprint(w, e.prog.Fset, x)
272272+ fmt.Fprint(w, "\n")
273273+ if override != "" {
274274+ fmt.Fprintf(w, "var %s = %s.%s\n\n", override, pkgName, x.Name.Name)
275275+ }
276276+}
277277+278278+func (e *extracter) reportDecl(w io.Writer, x *ast.GenDecl) {
279279+ if x.Tok != token.CONST {
280280+ return
281281+ }
282282+ k := 0
283283+ for _, s := range x.Specs {
284284+ if v, ok := s.(*ast.ValueSpec); ok && !filter(v.Names[0].Name) {
285285+ if v.Values == nil {
286286+ v.Values = make([]ast.Expr, len(v.Names))
287287+ }
288288+ for i, expr := range v.Names {
289289+ // This check can be removed if we set constants to floats.
290290+ if _, ok := v.Values[i].(*ast.BasicLit); ok {
291291+ continue
292292+ }
293293+ tv, _ := types.Eval(e.prog.Fset, e.pkg.Pkg, v.Pos(), v.Names[0].Name)
294294+ tok := token.ILLEGAL
295295+ switch tv.Value.Kind() {
296296+ case constant.Bool:
297297+ v.Values[i] = ast.NewIdent(tv.Value.ExactString())
298298+ continue
299299+ case constant.String:
300300+ tok = token.STRING
301301+ case constant.Int:
302302+ tok = token.INT
303303+ case constant.Float:
304304+ tok = token.FLOAT
305305+ default:
306306+ fmt.Printf("Skipping %s\n", v.Names)
307307+ continue
308308+ }
309309+ v.Values[i] = &ast.BasicLit{
310310+ ValuePos: expr.Pos(),
311311+ Kind: tok,
312312+ Value: tv.Value.ExactString(),
313313+ }
314314+ }
315315+ v.Type = nil
316316+ x.Specs[k] = v
317317+ k++
318318+ }
319319+ }
320320+ x.Specs = x.Specs[:k]
321321+ if len(x.Specs) == 0 {
322322+ return
323323+ }
324324+ fmt.Fprintln(w)
325325+ printer.Fprint(w, e.prog.Fset, x)
326326+ fmt.Fprintln(w)
327327+}
+34
pkg/crypto/md5/md5.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo extract crypto/md5
2020+2121+package md5
2222+2323+import "crypto/md5"
2424+2525+// The size of an MD5 checksum in bytes.
2626+const Size = 16
2727+2828+// The blocksize of MD5 in bytes.
2929+const BlockSize = 64
3030+3131+// Sum returns the MD5 checksum of the data.
3232+func Sum(data []byte) [Size]byte {
3333+ return md5.Sum(data)
3434+}
+34
pkg/crypto/sha1/sha1.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo extract crypto/sha1
2020+2121+package sha1
2222+2323+import "crypto/sha1"
2424+2525+// The size of a SHA-1 checksum in bytes.
2626+const Size = 20
2727+2828+// The blocksize of SHA-1 in bytes.
2929+const BlockSize = 64
3030+3131+// Sum returns the SHA-1 checksum of the data.
3232+func Sum(data []byte) [Size]byte {
3333+ return sha1.Sum(data)
3434+}
+42
pkg/crypto/sha256/sha256.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo extract crypto/sha256
2020+2121+package sha256
2222+2323+import "crypto/sha256"
2424+2525+// The size of a SHA256 checksum in bytes.
2626+const Size = 32
2727+2828+// The size of a SHA224 checksum in bytes.
2929+const Size224 = 28
3030+3131+// The blocksize of SHA256 and SHA224 in bytes.
3232+const BlockSize = 64
3333+3434+// Sum256 returns the SHA256 checksum of the data.
3535+func Sum256(data []byte) [Size]byte {
3636+ return sha256.Sum256(data)
3737+}
3838+3939+// Sum224 returns the SHA224 checksum of the data.
4040+func Sum224(data []byte) (sum224 [Size224]byte) {
4141+ return sha256.Sum224(data)
4242+}
+61
pkg/crypto/sha512/sha512.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo extract crypto/sha512
2020+2121+package sha512
2222+2323+import "crypto/sha512"
2424+2525+const (
2626+ // Size is the size, in bytes, of a SHA-512 checksum.
2727+ Size = 64
2828+2929+ // Size224 is the size, in bytes, of a SHA-512/224 checksum.
3030+ Size224 = 28
3131+3232+ // Size256 is the size, in bytes, of a SHA-512/256 checksum.
3333+ Size256 = 32
3434+3535+ // Size384 is the size, in bytes, of a SHA-384 checksum.
3636+ Size384 = 48
3737+3838+ // BlockSize is the block size, in bytes, of the SHA-512/224,
3939+ // SHA-512/256, SHA-384 and SHA-512 hash functions.
4040+ BlockSize = 128
4141+)
4242+4343+// Sum512 returns the SHA512 checksum of the data.
4444+func Sum512(data []byte) [Size]byte {
4545+ return sha512.Sum512(data)
4646+}
4747+4848+// Sum384 returns the SHA384 checksum of the data.
4949+func Sum384(data []byte) (sum384 [Size384]byte) {
5050+ return sha512.Sum384(data)
5151+}
5252+5353+// Sum512_224 returns the Sum512/224 checksum of the data.
5454+func Sum512_224(data []byte) (sum224 [Size224]byte) {
5555+ return sha512.Sum512_224(data)
5656+}
5757+5858+// Sum512_256 returns the Sum512/256 checksum of the data.
5959+func Sum512_256(data []byte) (sum256 [Size256]byte) {
6060+ return sha512.Sum512_256(data)
6161+}
+36
pkg/doc.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Package pkg define CUE builtin packages.
1616+//
1717+// The CUE core packages are different, but closely mimic the Go core packages.
1818+// The types, values, and functions are defined as their Go equivalence and
1919+// mapped to CUE types.
2020+//
2121+// Beware that some packages are defined in lesser-precision types than are
2222+// typically used in CUE and thus may lead to loss of precision.
2323+//
2424+// All packages in these subdirectories are hermetic. That is, the results
2525+// do not change based on environment conditions. That is:
2626+//
2727+// - no reading of files contents
2828+// - no querying of the file system of any kind
2929+// - no communication on the network
3030+// - no information about the type of environment
3131+// - only reproduceable random generators
3232+//
3333+// Hermetic configurations allow for fast and advanced analysis that otherwise
3434+// would not be possible or practical. The cue "cmd" command can be used to
3535+// mix in non-hermetic influences into configurations.
3636+package pkg
+51
pkg/encoding/hex/hex.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo -stripstr -exclude=Decode$,Encode$,EncodeToString,Dumper extract encoding/hex
2020+2121+package hex
2222+2323+import "encoding/hex"
2424+2525+// EncodedLen returns the length of an encoding of n source bytes.
2626+// Specifically, it returns n * 2.
2727+func EncodedLen(n int) int {
2828+ return hex.EncodedLen(n)
2929+}
3030+3131+// DecodedLen returns the length of a decoding of x source bytes.
3232+// Specifically, it returns x / 2.
3333+func DecodedLen(x int) int {
3434+ return hex.DecodedLen(x)
3535+}
3636+3737+// Decode returns the bytes represented by the hexadecimal string s.
3838+//
3939+// Decode expects that src contains only hexadecimal
4040+// characters and that src has even length.
4141+// If the input is malformed, Decode returns
4242+// the bytes decoded before the error.
4343+func Decode(s string) ([]byte, error) {
4444+ return hex.DecodeString(s)
4545+}
4646+4747+// Dump returns a string that contains a hex dump of the given data. The format
4848+// of the hex dump matches the output of `hexdump -C` on the command line.
4949+func Dump(data []byte) string {
5050+ return hex.Dump(data)
5151+}
+28
pkg/encoding/json/json.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo -exclude=Compact,Indent,arshal$ extract encoding/json
2020+2121+package json
2222+2323+import "encoding/json"
2424+2525+// Valid reports whether data is a valid JSON encoding.
2626+func Valid(data []byte) bool {
2727+ return json.Valid(data)
2828+}
+40
pkg/html/html.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo -stripstr extract html
2020+2121+package html
2222+2323+import "html"
2424+2525+// Escape escapes special characters like "<" to become "<". It
2626+// escapes only five such characters: <, >, &, ' and ".
2727+// UnescapeString(Escape(s)) == s always holds, but the converse isn't
2828+// always true.
2929+func Escape(s string) string {
3030+ return html.EscapeString(s)
3131+}
3232+3333+// Unescape unescapes entities like "<" to become "<". It unescapes a
3434+// larger range of entities than EscapeString escapes. For example, "á"
3535+// unescapes to "á", as does "á" and "á".
3636+// Unescape(EscapeString(s)) == s always holds, but the converse isn't
3737+// always true.
3838+func Unescape(s string) string {
3939+ return html.UnescapeString(s)
4040+}
+56
pkg/math/big.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo -exclude= extract math/big
2020+2121+package math
2222+2323+import "math/big"
2424+2525+// Exponent and precision limits.
2626+const (
2727+ MaxExp = 2147483647 // largest supported exponent
2828+ MinExp = -2147483648 // smallest supported exponent
2929+ MaxPrec = 4294967295 // largest (theoretically) supported precision; likely memory-limited
3030+)
3131+3232+// These constants define supported rounding modes.
3333+const (
3434+ ToNearestEven = 0 // == IEEE 754-2008 roundTiesToEven
3535+ ToNearestAway = 1 // == IEEE 754-2008 roundTiesToAway
3636+ ToZero = 2 // == IEEE 754-2008 roundTowardZero
3737+ AwayFromZero = 3 // no IEEE 754-2008 equivalent
3838+ ToNegativeInf = 4 // == IEEE 754-2008 roundTowardNegative
3939+ ToPositiveInf = 5 // == IEEE 754-2008 roundTowardPositive
4040+)
4141+4242+// Constants describing the Accuracy of a Float.
4343+const (
4444+ Below = -1
4545+ Exact = 0
4646+ Above = 1
4747+)
4848+4949+// Jacobi returns the Jacobi symbol (x/y), either +1, -1, or 0.
5050+// The y argument must be an odd integer.
5151+func Jacobi(x, y *big.Int) int {
5252+ return big.Jacobi(x, y)
5353+}
5454+5555+// MaxBase is the largest number base accepted for string conversions.
5656+const MaxBase = 62
+522
pkg/math/math.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo -exclude=32,^Next,^Max,^Smallest,^Min,bits,Inf,NaN,Round,Trunc,Ceil,Floor$ extract math
2020+2121+package math
2222+2323+import "math"
2424+2525+// Abs returns the absolute value of x.
2626+//
2727+// Special cases are:
2828+// Abs(±Inf) = +Inf
2929+// Abs(NaN) = NaN
3030+func Abs(x float64) float64 {
3131+ return math.Abs(x)
3232+}
3333+3434+// Acosh returns the inverse hyperbolic cosine of x.
3535+//
3636+// Special cases are:
3737+// Acosh(+Inf) = +Inf
3838+// Acosh(x) = NaN if x < 1
3939+// Acosh(NaN) = NaN
4040+func Acosh(x float64) float64 {
4141+ return math.Acosh(x)
4242+}
4343+4444+// Asin returns the arcsine, in radians, of x.
4545+//
4646+// Special cases are:
4747+// Asin(±0) = ±0
4848+// Asin(x) = NaN if x < -1 or x > 1
4949+func Asin(x float64) float64 {
5050+ return math.Asin(x)
5151+}
5252+5353+// Acos returns the arccosine, in radians, of x.
5454+//
5555+// Special case is:
5656+// Acos(x) = NaN if x < -1 or x > 1
5757+func Acos(x float64) float64 {
5858+ return math.Acos(x)
5959+}
6060+6161+// Asinh returns the inverse hyperbolic sine of x.
6262+//
6363+// Special cases are:
6464+// Asinh(±0) = ±0
6565+// Asinh(±Inf) = ±Inf
6666+// Asinh(NaN) = NaN
6767+func Asinh(x float64) float64 {
6868+ return math.Asinh(x)
6969+}
7070+7171+// Atan returns the arctangent, in radians, of x.
7272+//
7373+// Special cases are:
7474+// Atan(±0) = ±0
7575+// Atan(±Inf) = ±Pi/2
7676+func Atan(x float64) float64 {
7777+ return math.Atan(x)
7878+}
7979+8080+// Atan2 returns the arc tangent of y/x, using
8181+// the signs of the two to determine the quadrant
8282+// of the return value.
8383+//
8484+// Special cases are (in order):
8585+// Atan2(y, NaN) = NaN
8686+// Atan2(NaN, x) = NaN
8787+// Atan2(+0, x>=0) = +0
8888+// Atan2(-0, x>=0) = -0
8989+// Atan2(+0, x<=-0) = +Pi
9090+// Atan2(-0, x<=-0) = -Pi
9191+// Atan2(y>0, 0) = +Pi/2
9292+// Atan2(y<0, 0) = -Pi/2
9393+// Atan2(+Inf, +Inf) = +Pi/4
9494+// Atan2(-Inf, +Inf) = -Pi/4
9595+// Atan2(+Inf, -Inf) = 3Pi/4
9696+// Atan2(-Inf, -Inf) = -3Pi/4
9797+// Atan2(y, +Inf) = 0
9898+// Atan2(y>0, -Inf) = +Pi
9999+// Atan2(y<0, -Inf) = -Pi
100100+// Atan2(+Inf, x) = +Pi/2
101101+// Atan2(-Inf, x) = -Pi/2
102102+func Atan2(y, x float64) float64 {
103103+ return math.Atan2(y, x)
104104+}
105105+106106+// Atanh returns the inverse hyperbolic tangent of x.
107107+//
108108+// Special cases are:
109109+// Atanh(1) = +Inf
110110+// Atanh(±0) = ±0
111111+// Atanh(-1) = -Inf
112112+// Atanh(x) = NaN if x < -1 or x > 1
113113+// Atanh(NaN) = NaN
114114+func Atanh(x float64) float64 {
115115+ return math.Atanh(x)
116116+}
117117+118118+// Cbrt returns the cube root of x.
119119+//
120120+// Special cases are:
121121+// Cbrt(±0) = ±0
122122+// Cbrt(±Inf) = ±Inf
123123+// Cbrt(NaN) = NaN
124124+func Cbrt(x float64) float64 {
125125+ return math.Cbrt(x)
126126+}
127127+128128+// Mathematical constants.
129129+const (
130130+ E = 2.71828182845904523536028747135266249775724709369995957496696763 // https://oeis.org/A001113
131131+ Pi = 3.14159265358979323846264338327950288419716939937510582097494459 // https://oeis.org/A000796
132132+ Phi = 1.61803398874989484820458683436563811772030917980576286213544862 // https://oeis.org/A001622
133133+134134+ Sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974 // https://oeis.org/A002193
135135+ SqrtE = 1.64872127070012814684865078781416357165377610071014801157507931 // https://oeis.org/A019774
136136+ SqrtPi = 1.77245385090551602729816748334114518279754945612238712821380779 // https://oeis.org/A002161
137137+ SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038 // https://oeis.org/A139339
138138+139139+ Ln2 = 0.693147180559945309417232121458176568075500134360255254120680009 // https://oeis.org/A002162
140140+ Log2E = 1000000000000000000000000000000000000000000000000000000000000000 / 693147180559945309417232121458176568075500134360255254120680009
141141+ Ln10 = 2.30258509299404568401799145468436420760110148862877297603332790 // https://oeis.org/A002392
142142+ Log10E = 10000000000000000000000000000000000000000000000000000000000000 / 23025850929940456840179914546843642076011014886287729760333279
143143+)
144144+145145+// Copysign returns a value with the magnitude
146146+// of x and the sign of y.
147147+func Copysign(x, y float64) float64 {
148148+ return math.Copysign(x, y)
149149+}
150150+151151+// Dim returns the maximum of x-y or 0.
152152+//
153153+// Special cases are:
154154+// Dim(+Inf, +Inf) = NaN
155155+// Dim(-Inf, -Inf) = NaN
156156+// Dim(x, NaN) = Dim(NaN, x) = NaN
157157+func Dim(x, y float64) float64 {
158158+ return math.Dim(x, y)
159159+}
160160+161161+// Erf returns the error function of x.
162162+//
163163+// Special cases are:
164164+// Erf(+Inf) = 1
165165+// Erf(-Inf) = -1
166166+// Erf(NaN) = NaN
167167+func Erf(x float64) float64 {
168168+ return math.Erf(x)
169169+}
170170+171171+// Erfc returns the complementary error function of x.
172172+//
173173+// Special cases are:
174174+// Erfc(+Inf) = 0
175175+// Erfc(-Inf) = 2
176176+// Erfc(NaN) = NaN
177177+func Erfc(x float64) float64 {
178178+ return math.Erfc(x)
179179+}
180180+181181+// Erfinv returns the inverse error function of x.
182182+//
183183+// Special cases are:
184184+// Erfinv(1) = +Inf
185185+// Erfinv(-1) = -Inf
186186+// Erfinv(x) = NaN if x < -1 or x > 1
187187+// Erfinv(NaN) = NaN
188188+func Erfinv(x float64) float64 {
189189+ return math.Erfinv(x)
190190+}
191191+192192+// Erfcinv returns the inverse of Erfc(x).
193193+//
194194+// Special cases are:
195195+// Erfcinv(0) = +Inf
196196+// Erfcinv(2) = -Inf
197197+// Erfcinv(x) = NaN if x < 0 or x > 2
198198+// Erfcinv(NaN) = NaN
199199+func Erfcinv(x float64) float64 {
200200+ return math.Erfcinv(x)
201201+}
202202+203203+// Exp returns e**x, the base-e exponential of x.
204204+//
205205+// Special cases are:
206206+// Exp(+Inf) = +Inf
207207+// Exp(NaN) = NaN
208208+// Very large values overflow to 0 or +Inf.
209209+// Very small values underflow to 1.
210210+func Exp(x float64) float64 {
211211+ return math.Exp(x)
212212+}
213213+214214+// Exp2 returns 2**x, the base-2 exponential of x.
215215+//
216216+// Special cases are the same as Exp.
217217+func Exp2(x float64) float64 {
218218+ return math.Exp2(x)
219219+}
220220+221221+// Expm1 returns e**x - 1, the base-e exponential of x minus 1.
222222+// It is more accurate than Exp(x) - 1 when x is near zero.
223223+//
224224+// Special cases are:
225225+// Expm1(+Inf) = +Inf
226226+// Expm1(-Inf) = -1
227227+// Expm1(NaN) = NaN
228228+// Very large values overflow to -1 or +Inf.
229229+func Expm1(x float64) float64 {
230230+ return math.Expm1(x)
231231+}
232232+233233+// Gamma returns the Gamma function of x.
234234+//
235235+// Special cases are:
236236+// Gamma(+Inf) = +Inf
237237+// Gamma(+0) = +Inf
238238+// Gamma(-0) = -Inf
239239+// Gamma(x) = NaN for integer x < 0
240240+// Gamma(-Inf) = NaN
241241+// Gamma(NaN) = NaN
242242+func Gamma(x float64) float64 {
243243+ return math.Gamma(x)
244244+}
245245+246246+// Hypot returns Sqrt(p*p + q*q), taking care to avoid
247247+// unnecessary overflow and underflow.
248248+//
249249+// Special cases are:
250250+// Hypot(±Inf, q) = +Inf
251251+// Hypot(p, ±Inf) = +Inf
252252+// Hypot(NaN, q) = NaN
253253+// Hypot(p, NaN) = NaN
254254+func Hypot(p, q float64) float64 {
255255+ return math.Hypot(p, q)
256256+}
257257+258258+// J0 returns the order-zero Bessel function of the first kind.
259259+//
260260+// Special cases are:
261261+// J0(±Inf) = 0
262262+// J0(0) = 1
263263+// J0(NaN) = NaN
264264+func J0(x float64) float64 {
265265+ return math.J0(x)
266266+}
267267+268268+// Y0 returns the order-zero Bessel function of the second kind.
269269+//
270270+// Special cases are:
271271+// Y0(+Inf) = 0
272272+// Y0(0) = -Inf
273273+// Y0(x < 0) = NaN
274274+// Y0(NaN) = NaN
275275+func Y0(x float64) float64 {
276276+ return math.Y0(x)
277277+}
278278+279279+// J1 returns the order-one Bessel function of the first kind.
280280+//
281281+// Special cases are:
282282+// J1(±Inf) = 0
283283+// J1(NaN) = NaN
284284+func J1(x float64) float64 {
285285+ return math.J1(x)
286286+}
287287+288288+// Y1 returns the order-one Bessel function of the second kind.
289289+//
290290+// Special cases are:
291291+// Y1(+Inf) = 0
292292+// Y1(0) = -Inf
293293+// Y1(x < 0) = NaN
294294+// Y1(NaN) = NaN
295295+func Y1(x float64) float64 {
296296+ return math.Y1(x)
297297+}
298298+299299+// Jn returns the order-n Bessel function of the first kind.
300300+//
301301+// Special cases are:
302302+// Jn(n, ±Inf) = 0
303303+// Jn(n, NaN) = NaN
304304+func Jn(n int, x float64) float64 {
305305+ return math.Jn(n, x)
306306+}
307307+308308+// Yn returns the order-n Bessel function of the second kind.
309309+//
310310+// Special cases are:
311311+// Yn(n, +Inf) = 0
312312+// Yn(n ≥ 0, 0) = -Inf
313313+// Yn(n < 0, 0) = +Inf if n is odd, -Inf if n is even
314314+// Yn(n, x < 0) = NaN
315315+// Yn(n, NaN) = NaN
316316+func Yn(n int, x float64) float64 {
317317+ return math.Yn(n, x)
318318+}
319319+320320+// Ldexp is the inverse of Frexp.
321321+// It returns frac × 2**exp.
322322+//
323323+// Special cases are:
324324+// Ldexp(±0, exp) = ±0
325325+// Ldexp(±Inf, exp) = ±Inf
326326+// Ldexp(NaN, exp) = NaN
327327+func Ldexp(frac float64, exp int) float64 {
328328+ return math.Ldexp(frac, exp)
329329+}
330330+331331+// Log returns the natural logarithm of x.
332332+//
333333+// Special cases are:
334334+// Log(+Inf) = +Inf
335335+// Log(0) = -Inf
336336+// Log(x < 0) = NaN
337337+// Log(NaN) = NaN
338338+func Log(x float64) float64 {
339339+ return math.Log(x)
340340+}
341341+342342+// Log10 returns the decimal logarithm of x.
343343+// The special cases are the same as for Log.
344344+func Log10(x float64) float64 {
345345+ return math.Log10(x)
346346+}
347347+348348+// Log2 returns the binary logarithm of x.
349349+// The special cases are the same as for Log.
350350+func Log2(x float64) float64 {
351351+ return math.Log2(x)
352352+}
353353+354354+// Log1p returns the natural logarithm of 1 plus its argument x.
355355+// It is more accurate than Log(1 + x) when x is near zero.
356356+//
357357+// Special cases are:
358358+// Log1p(+Inf) = +Inf
359359+// Log1p(±0) = ±0
360360+// Log1p(-1) = -Inf
361361+// Log1p(x < -1) = NaN
362362+// Log1p(NaN) = NaN
363363+func Log1p(x float64) float64 {
364364+ return math.Log1p(x)
365365+}
366366+367367+// Logb returns the binary exponent of x.
368368+//
369369+// Special cases are:
370370+// Logb(±Inf) = +Inf
371371+// Logb(0) = -Inf
372372+// Logb(NaN) = NaN
373373+func Logb(x float64) float64 {
374374+ return math.Logb(x)
375375+}
376376+377377+// Ilogb returns the binary exponent of x as an integer.
378378+//
379379+// Special cases are:
380380+// Ilogb(±Inf) = MaxInt32
381381+// Ilogb(0) = MinInt32
382382+// Ilogb(NaN) = MaxInt32
383383+func Ilogb(x float64) int {
384384+ return math.Ilogb(x)
385385+}
386386+387387+// Mod returns the floating-point remainder of x/y.
388388+// The magnitude of the result is less than y and its
389389+// sign agrees with that of x.
390390+//
391391+// Special cases are:
392392+// Mod(±Inf, y) = NaN
393393+// Mod(NaN, y) = NaN
394394+// Mod(x, 0) = NaN
395395+// Mod(x, ±Inf) = x
396396+// Mod(x, NaN) = NaN
397397+func Mod(x, y float64) float64 {
398398+ return math.Mod(x, y)
399399+}
400400+401401+// Pow returns x**y, the base-x exponential of y.
402402+//
403403+// Special cases are (in order):
404404+// Pow(x, ±0) = 1 for any x
405405+// Pow(1, y) = 1 for any y
406406+// Pow(x, 1) = x for any x
407407+// Pow(NaN, y) = NaN
408408+// Pow(x, NaN) = NaN
409409+// Pow(±0, y) = ±Inf for y an odd integer < 0
410410+// Pow(±0, -Inf) = +Inf
411411+// Pow(±0, +Inf) = +0
412412+// Pow(±0, y) = +Inf for finite y < 0 and not an odd integer
413413+// Pow(±0, y) = ±0 for y an odd integer > 0
414414+// Pow(±0, y) = +0 for finite y > 0 and not an odd integer
415415+// Pow(-1, ±Inf) = 1
416416+// Pow(x, +Inf) = +Inf for |x| > 1
417417+// Pow(x, -Inf) = +0 for |x| > 1
418418+// Pow(x, +Inf) = +0 for |x| < 1
419419+// Pow(x, -Inf) = +Inf for |x| < 1
420420+// Pow(+Inf, y) = +Inf for y > 0
421421+// Pow(+Inf, y) = +0 for y < 0
422422+// Pow(-Inf, y) = Pow(-0, -y)
423423+// Pow(x, y) = NaN for finite x < 0 and finite non-integer y
424424+func Pow(x, y float64) float64 {
425425+ return math.Pow(x, y)
426426+}
427427+428428+// Pow10 returns 10**n, the base-10 exponential of n.
429429+//
430430+// Special cases are:
431431+// Pow10(n) = 0 for n < -323
432432+// Pow10(n) = +Inf for n > 308
433433+func Pow10(n int) float64 {
434434+ return math.Pow10(n)
435435+}
436436+437437+// Remainder returns the IEEE 754 floating-point remainder of x/y.
438438+//
439439+// Special cases are:
440440+// Remainder(±Inf, y) = NaN
441441+// Remainder(NaN, y) = NaN
442442+// Remainder(x, 0) = NaN
443443+// Remainder(x, ±Inf) = x
444444+// Remainder(x, NaN) = NaN
445445+func Remainder(x, y float64) float64 {
446446+ return math.Remainder(x, y)
447447+}
448448+449449+// Signbit returns true if x is negative or negative zero.
450450+func Signbit(x float64) bool {
451451+ return math.Signbit(x)
452452+}
453453+454454+// Cos returns the cosine of the radian argument x.
455455+//
456456+// Special cases are:
457457+// Cos(±Inf) = NaN
458458+// Cos(NaN) = NaN
459459+func Cos(x float64) float64 {
460460+ return math.Cos(x)
461461+}
462462+463463+// Sin returns the sine of the radian argument x.
464464+//
465465+// Special cases are:
466466+// Sin(±0) = ±0
467467+// Sin(±Inf) = NaN
468468+// Sin(NaN) = NaN
469469+func Sin(x float64) float64 {
470470+ return math.Sin(x)
471471+}
472472+473473+// Sinh returns the hyperbolic sine of x.
474474+//
475475+// Special cases are:
476476+// Sinh(±0) = ±0
477477+// Sinh(±Inf) = ±Inf
478478+// Sinh(NaN) = NaN
479479+func Sinh(x float64) float64 {
480480+ return math.Sinh(x)
481481+}
482482+483483+// Cosh returns the hyperbolic cosine of x.
484484+//
485485+// Special cases are:
486486+// Cosh(±0) = 1
487487+// Cosh(±Inf) = +Inf
488488+// Cosh(NaN) = NaN
489489+func Cosh(x float64) float64 {
490490+ return math.Cosh(x)
491491+}
492492+493493+// Sqrt returns the square root of x.
494494+//
495495+// Special cases are:
496496+// Sqrt(+Inf) = +Inf
497497+// Sqrt(±0) = ±0
498498+// Sqrt(x < 0) = NaN
499499+// Sqrt(NaN) = NaN
500500+func Sqrt(x float64) float64 {
501501+ return math.Sqrt(x)
502502+}
503503+504504+// Tan returns the tangent of the radian argument x.
505505+//
506506+// Special cases are:
507507+// Tan(±0) = ±0
508508+// Tan(±Inf) = NaN
509509+// Tan(NaN) = NaN
510510+func Tan(x float64) float64 {
511511+ return math.Tan(x)
512512+}
513513+514514+// Tanh returns the hyperbolic tangent of x.
515515+//
516516+// Special cases are:
517517+// Tanh(±0) = ±0
518518+// Tanh(±Inf) = ±1
519519+// Tanh(NaN) = NaN
520520+func Tanh(x float64) float64 {
521521+ return math.Tanh(x)
522522+}
+104
pkg/path/path.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo -exclude=Split,Join extract path
2020+2121+package path
2222+2323+import "path"
2424+2525+// Match reports whether name matches the shell pattern.
2626+// The pattern syntax is:
2727+//
2828+// pattern:
2929+// { term }
3030+// term:
3131+// '*' matches any sequence of non-/ characters
3232+// '?' matches any single non-/ character
3333+// '[' [ '^' ] { character-range } ']'
3434+// character class (must be non-empty)
3535+// c matches character c (c != '*', '?', '\\', '[')
3636+// '\\' c matches character c
3737+//
3838+// character-range:
3939+// c matches character c (c != '\\', '-', ']')
4040+// '\\' c matches character c
4141+// lo '-' hi matches character c for lo <= c <= hi
4242+//
4343+// Match requires pattern to match all of name, not just a substring.
4444+// The only possible returned error is ErrBadPattern, when pattern
4545+// is malformed.
4646+//
4747+func Match(pattern, name string) (matched bool, err error) {
4848+ return path.Match(pattern, name)
4949+}
5050+5151+// Clean returns the shortest path name equivalent to path
5252+// by purely lexical processing. It applies the following rules
5353+// iteratively until no further processing can be done:
5454+//
5555+// 1. Replace multiple slashes with a single slash.
5656+// 2. Eliminate each . path name element (the current directory).
5757+// 3. Eliminate each inner .. path name element (the parent directory)
5858+// along with the non-.. element that precedes it.
5959+// 4. Eliminate .. elements that begin a rooted path:
6060+// that is, replace "/.." by "/" at the beginning of a path.
6161+//
6262+// The returned path ends in a slash only if it is the root "/".
6363+//
6464+// If the result of this process is an empty string, Clean
6565+// returns the string ".".
6666+//
6767+// See also Rob Pike, ``Lexical File Names in Plan 9 or
6868+// Getting Dot-Dot Right,''
6969+// https://9p.io/sys/doc/lexnames.html
7070+func Clean(path string) string { return pathClean(path) }
7171+7272+var pathClean = path.Clean
7373+7474+// Ext returns the file name extension used by path.
7575+// The extension is the suffix beginning at the final dot
7676+// in the final slash-separated element of path;
7777+// it is empty if there is no dot.
7878+func Ext(path string) string { return pathExt(path) }
7979+8080+var pathExt = path.Ext
8181+8282+// Base returns the last element of path.
8383+// Trailing slashes are removed before extracting the last element.
8484+// If the path is empty, Base returns ".".
8585+// If the path consists entirely of slashes, Base returns "/".
8686+func Base(path string) string { return pathBase(path) }
8787+8888+var pathBase = path.Base
8989+9090+// IsAbs reports whether the path is absolute.
9191+func IsAbs(path string) bool { return pathIsAbs(path) }
9292+9393+var pathIsAbs = path.IsAbs
9494+9595+// Dir returns all but the last element of path, typically the path's directory.
9696+// After dropping the final element using Split, the path is Cleaned and trailing
9797+// slashes are removed.
9898+// If the path is empty, Dir returns ".".
9999+// If the path consists entirely of slashes followed by non-slash bytes, Dir
100100+// returns a single slash. In any other case, the returned path does not end in a
101101+// slash.
102102+func Dir(path string) string { return pathDir(path) }
103103+104104+var pathDir = path.Dir
+37
pkg/regexp/regexp.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo -exclude=Compile,Append,Reader,Match$ -stripstr extract regexp
2020+2121+package regexp
2222+2323+import "regexp"
2424+2525+// Match reports whether the string s
2626+// contains any match of the regular expression pattern.
2727+// More complicated queries need to use Compile and the full Regexp interface.
2828+func Match(pattern string, s string) (matched bool, err error) {
2929+ return regexp.MatchString(pattern, s)
3030+}
3131+3232+// QuoteMeta returns a string that escapes all regular expression metacharacters
3333+// inside the argument text; the returned string is a regular expression matching
3434+// the literal text.
3535+func QuoteMeta(s string) string {
3636+ return regexp.QuoteMeta(s)
3737+}
+196
pkg/strconv/strconv.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo -exclude=Append,Unquote,Itoa extract strconv
2020+2121+package strconv
2222+2323+import "strconv"
2424+2525+// ParseBool returns the boolean value represented by the string.
2626+// It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
2727+// Any other value returns an error.
2828+func ParseBool(str string) (bool, error) {
2929+ return strconv.ParseBool(str)
3030+}
3131+3232+// FormatBool returns "true" or "false" according to the value of b.
3333+func FormatBool(b bool) string {
3434+ return strconv.FormatBool(b)
3535+}
3636+3737+// ParseFloat converts the string s to a floating-point number
3838+// with the precision specified by bitSize: 32 for float32, or 64 for float64.
3939+// When bitSize=32, the result still has type float64, but it will be
4040+// convertible to float32 without changing its value.
4141+//
4242+// If s is well-formed and near a valid floating point number,
4343+// ParseFloat returns the nearest floating point number rounded
4444+// using IEEE754 unbiased rounding.
4545+//
4646+// The errors that ParseFloat returns have concrete type *NumError
4747+// and include err.Num = s.
4848+//
4949+// If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.
5050+//
5151+// If s is syntactically well-formed but is more than 1/2 ULP
5252+// away from the largest floating point number of the given size,
5353+// ParseFloat returns f = ±Inf, err.Err = ErrRange.
5454+func ParseFloat(s string, bitSize int) (float64, error) {
5555+ return strconv.ParseFloat(s, bitSize)
5656+}
5757+5858+// IntSize is the size in bits of an int or uint value.
5959+const IntSize = 64
6060+6161+// ParseUint is like ParseInt but for unsigned numbers.
6262+func ParseUint(s string, base int, bitSize int) (uint64, error) {
6363+ return strconv.ParseUint(s, base, bitSize)
6464+}
6565+6666+// ParseInt interprets a string s in the given base (0, 2 to 36) and
6767+// bit size (0 to 64) and returns the corresponding value i.
6868+//
6969+// If base == 0, the base is implied by the string's prefix:
7070+// base 16 for "0x", base 8 for "0", and base 10 otherwise.
7171+// For bases 1, below 0 or above 36 an error is returned.
7272+//
7373+// The bitSize argument specifies the integer type
7474+// that the result must fit into. Bit sizes 0, 8, 16, 32, and 64
7575+// correspond to int, int8, int16, int32, and int64.
7676+// For a bitSize below 0 or above 64 an error is returned.
7777+//
7878+// The errors that ParseInt returns have concrete type *NumError
7979+// and include err.Num = s. If s is empty or contains invalid
8080+// digits, err.Err = ErrSyntax and the returned value is 0;
8181+// if the value corresponding to s cannot be represented by a
8282+// signed integer of the given size, err.Err = ErrRange and the
8383+// returned value is the maximum magnitude integer of the
8484+// appropriate bitSize and sign.
8585+func ParseInt(s string, base int, bitSize int) (i int64, err error) {
8686+ return strconv.ParseInt(s, base, bitSize)
8787+}
8888+8989+// Atoi returns the result of ParseInt(s, 10, 0) converted to type int.
9090+func Atoi(s string) (int, error) {
9191+ return strconv.Atoi(s)
9292+}
9393+9494+// FormatFloat converts the floating-point number f to a string,
9595+// according to the format fmt and precision prec. It rounds the
9696+// result assuming that the original was obtained from a floating-point
9797+// value of bitSize bits (32 for float32, 64 for float64).
9898+//
9999+// The format fmt is one of
100100+// 'b' (-ddddp±ddd, a binary exponent),
101101+// 'e' (-d.dddde±dd, a decimal exponent),
102102+// 'E' (-d.ddddE±dd, a decimal exponent),
103103+// 'f' (-ddd.dddd, no exponent),
104104+// 'g' ('e' for large exponents, 'f' otherwise), or
105105+// 'G' ('E' for large exponents, 'f' otherwise).
106106+//
107107+// The precision prec controls the number of digits (excluding the exponent)
108108+// printed by the 'e', 'E', 'f', 'g', and 'G' formats.
109109+// For 'e', 'E', and 'f' it is the number of digits after the decimal point.
110110+// For 'g' and 'G' it is the maximum number of significant digits (trailing
111111+// zeros are removed).
112112+// The special precision -1 uses the smallest number of digits
113113+// necessary such that ParseFloat will return f exactly.
114114+func FormatFloat(f float64, fmt byte, prec, bitSize int) string {
115115+ return strconv.FormatFloat(f, fmt, prec, bitSize)
116116+}
117117+118118+// FormatUint returns the string representation of i in the given base,
119119+// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
120120+// for digit values >= 10.
121121+func FormatUint(i uint64, base int) string {
122122+ return strconv.FormatUint(i, base)
123123+}
124124+125125+// FormatInt returns the string representation of i in the given base,
126126+// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
127127+// for digit values >= 10.
128128+func FormatInt(i int64, base int) string {
129129+ return strconv.FormatInt(i, base)
130130+}
131131+132132+// Quote returns a double-quoted Go string literal representing s. The
133133+// returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for
134134+// control characters and non-printable characters as defined by
135135+// IsPrint.
136136+func Quote(s string) string {
137137+ return strconv.Quote(s)
138138+}
139139+140140+// QuoteToASCII returns a double-quoted Go string literal representing s.
141141+// The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for
142142+// non-ASCII characters and non-printable characters as defined by IsPrint.
143143+func QuoteToASCII(s string) string {
144144+ return strconv.QuoteToASCII(s)
145145+}
146146+147147+// QuoteToGraphic returns a double-quoted Go string literal representing s.
148148+// The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for
149149+// non-ASCII characters and non-printable characters as defined by IsGraphic.
150150+func QuoteToGraphic(s string) string {
151151+ return strconv.QuoteToGraphic(s)
152152+}
153153+154154+// QuoteRune returns a single-quoted Go character literal representing the
155155+// rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100)
156156+// for control characters and non-printable characters as defined by IsPrint.
157157+func QuoteRune(r rune) string {
158158+ return strconv.QuoteRune(r)
159159+}
160160+161161+// QuoteRuneToASCII returns a single-quoted Go character literal representing
162162+// the rune. The returned string uses Go escape sequences (\t, \n, \xFF,
163163+// \u0100) for non-ASCII characters and non-printable characters as defined
164164+// by IsPrint.
165165+func QuoteRuneToASCII(r rune) string {
166166+ return strconv.QuoteRuneToASCII(r)
167167+}
168168+169169+// QuoteRuneToGraphic returns a single-quoted Go character literal representing
170170+// the rune. The returned string uses Go escape sequences (\t, \n, \xFF,
171171+// \u0100) for non-ASCII characters and non-printable characters as defined
172172+// by IsGraphic.
173173+func QuoteRuneToGraphic(r rune) string {
174174+ return strconv.QuoteRuneToGraphic(r)
175175+}
176176+177177+// CanBackquote reports whether the string s can be represented
178178+// unchanged as a single-line backquoted string without control
179179+// characters other than tab.
180180+func CanBackquote(s string) bool {
181181+ return strconv.CanBackquote(s)
182182+}
183183+184184+// IsPrint reports whether the rune is defined as printable by Go, with
185185+// the same definition as unicode.IsPrint: letters, numbers, punctuation,
186186+// symbols and ASCII space.
187187+func IsPrint(r rune) bool {
188188+ return strconv.IsPrint(r)
189189+}
190190+191191+// IsGraphic reports whether the rune is defined as a Graphic by Unicode. Such
192192+// characters include letters, marks, numbers, punctuation, symbols, and
193193+// spaces, from categories L, M, N, P, S, and Zs.
194194+func IsGraphic(r rune) bool {
195195+ return strconv.IsGraphic(r)
196196+}
+219
pkg/strings/strings.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo -exclude=Rune$,Func$,^Map$,Special$,EqualFold,Byte,Title$ extract strings
2020+2121+package strings
2222+2323+import "strings"
2424+2525+// Compare returns an integer comparing two strings lexicographically.
2626+// The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
2727+//
2828+// Compare is included only for symmetry with package bytes.
2929+// It is usually clearer and always faster to use the built-in
3030+// string comparison operators ==, <, >, and so on.
3131+func Compare(a, b string) int {
3232+ return strings.Compare(a, b)
3333+}
3434+3535+// Count counts the number of non-overlapping instances of substr in s.
3636+// If substr is an empty string, Count returns 1 + the number of Unicode code points in s.
3737+func Count(s, substr string) int {
3838+ return strings.Count(s, substr)
3939+}
4040+4141+// Contains reports whether substr is within s.
4242+func Contains(s, substr string) bool {
4343+ return strings.Contains(s, substr)
4444+}
4545+4646+// ContainsAny reports whether any Unicode code points in chars are within s.
4747+func ContainsAny(s, chars string) bool {
4848+ return strings.ContainsAny(s, chars)
4949+}
5050+5151+// LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.
5252+func LastIndex(s, substr string) int {
5353+ return strings.LastIndex(s, substr)
5454+}
5555+5656+// IndexAny returns the index of the first instance of any Unicode code point
5757+// from chars in s, or -1 if no Unicode code point from chars is present in s.
5858+func IndexAny(s, chars string) int {
5959+ return strings.IndexAny(s, chars)
6060+}
6161+6262+// LastIndexAny returns the index of the last instance of any Unicode code
6363+// point from chars in s, or -1 if no Unicode code point from chars is
6464+// present in s.
6565+func LastIndexAny(s, chars string) int {
6666+ return strings.LastIndexAny(s, chars)
6767+}
6868+6969+// SplitN slices s into substrings separated by sep and returns a slice of
7070+// the substrings between those separators.
7171+//
7272+// The count determines the number of substrings to return:
7373+// n > 0: at most n substrings; the last substring will be the unsplit remainder.
7474+// n == 0: the result is nil (zero substrings)
7575+// n < 0: all substrings
7676+//
7777+// Edge cases for s and sep (for example, empty strings) are handled
7878+// as described in the documentation for Split.
7979+func SplitN(s, sep string, n int) []string {
8080+ return strings.SplitN(s, sep, n)
8181+}
8282+8383+// SplitAfterN slices s into substrings after each instance of sep and
8484+// returns a slice of those substrings.
8585+//
8686+// The count determines the number of substrings to return:
8787+// n > 0: at most n substrings; the last substring will be the unsplit remainder.
8888+// n == 0: the result is nil (zero substrings)
8989+// n < 0: all substrings
9090+//
9191+// Edge cases for s and sep (for example, empty strings) are handled
9292+// as described in the documentation for SplitAfter.
9393+func SplitAfterN(s, sep string, n int) []string {
9494+ return strings.SplitAfterN(s, sep, n)
9595+}
9696+9797+// Split slices s into all substrings separated by sep and returns a slice of
9898+// the substrings between those separators.
9999+//
100100+// If s does not contain sep and sep is not empty, Split returns a
101101+// slice of length 1 whose only element is s.
102102+//
103103+// If sep is empty, Split splits after each UTF-8 sequence. If both s
104104+// and sep are empty, Split returns an empty slice.
105105+//
106106+// It is equivalent to SplitN with a count of -1.
107107+func Split(s, sep string) []string {
108108+ return strings.Split(s, sep)
109109+}
110110+111111+// SplitAfter slices s into all substrings after each instance of sep and
112112+// returns a slice of those substrings.
113113+//
114114+// If s does not contain sep and sep is not empty, SplitAfter returns
115115+// a slice of length 1 whose only element is s.
116116+//
117117+// If sep is empty, SplitAfter splits after each UTF-8 sequence. If
118118+// both s and sep are empty, SplitAfter returns an empty slice.
119119+//
120120+// It is equivalent to SplitAfterN with a count of -1.
121121+func SplitAfter(s, sep string) []string {
122122+ return strings.SplitAfter(s, sep)
123123+}
124124+125125+// Fields splits the string s around each instance of one or more consecutive white space
126126+// characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an
127127+// empty slice if s contains only white space.
128128+func Fields(s string) []string {
129129+ return strings.Fields(s)
130130+}
131131+132132+// Join concatenates the elements of a to create a single string. The separator string
133133+// sep is placed between elements in the resulting string.
134134+func Join(a []string, sep string) string {
135135+ return strings.Join(a, sep)
136136+}
137137+138138+// HasPrefix tests whether the string s begins with prefix.
139139+func HasPrefix(s, prefix string) bool {
140140+ return strings.HasPrefix(s, prefix)
141141+}
142142+143143+// HasSuffix tests whether the string s ends with suffix.
144144+func HasSuffix(s, suffix string) bool {
145145+ return strings.HasSuffix(s, suffix)
146146+}
147147+148148+// Repeat returns a new string consisting of count copies of the string s.
149149+//
150150+// It panics if count is negative or if
151151+// the result of (len(s) * count) overflows.
152152+func Repeat(s string, count int) string {
153153+ return strings.Repeat(s, count)
154154+}
155155+156156+// ToUpper returns a copy of the string s with all Unicode letters mapped to their upper case.
157157+func ToUpper(s string) string {
158158+ return strings.ToUpper(s)
159159+}
160160+161161+// ToLower returns a copy of the string s with all Unicode letters mapped to their lower case.
162162+func ToLower(s string) string {
163163+ return strings.ToLower(s)
164164+}
165165+166166+// Trim returns a slice of the string s with all leading and
167167+// trailing Unicode code points contained in cutset removed.
168168+func Trim(s string, cutset string) string {
169169+ return strings.Trim(s, cutset)
170170+}
171171+172172+// TrimLeft returns a slice of the string s with all leading
173173+// Unicode code points contained in cutset removed.
174174+//
175175+// To remove a prefix, use TrimPrefix instead.
176176+func TrimLeft(s string, cutset string) string {
177177+ return strings.TrimLeft(s, cutset)
178178+}
179179+180180+// TrimRight returns a slice of the string s, with all trailing
181181+// Unicode code points contained in cutset removed.
182182+//
183183+// To remove a suffix, use TrimSuffix instead.
184184+func TrimRight(s string, cutset string) string {
185185+ return strings.TrimRight(s, cutset)
186186+}
187187+188188+// TrimSpace returns a slice of the string s, with all leading
189189+// and trailing white space removed, as defined by Unicode.
190190+func TrimSpace(s string) string {
191191+ return strings.TrimSpace(s)
192192+}
193193+194194+// TrimPrefix returns s without the provided leading prefix string.
195195+// If s doesn't start with prefix, s is returned unchanged.
196196+func TrimPrefix(s, prefix string) string {
197197+ return strings.TrimPrefix(s, prefix)
198198+}
199199+200200+// TrimSuffix returns s without the provided trailing suffix string.
201201+// If s doesn't end with suffix, s is returned unchanged.
202202+func TrimSuffix(s, suffix string) string {
203203+ return strings.TrimSuffix(s, suffix)
204204+}
205205+206206+// Replace returns a copy of the string s with the first n
207207+// non-overlapping instances of old replaced by new.
208208+// If old is empty, it matches at the beginning of the string
209209+// and after each UTF-8 sequence, yielding up to k+1 replacements
210210+// for a k-rune string.
211211+// If n < 0, there is no limit on the number of replacements.
212212+func Replace(s, old, new string, n int) string {
213213+ return strings.Replace(s, old, new, n)
214214+}
215215+216216+// Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.
217217+func Index(s, substr string) int {
218218+ return strings.Index(s, substr)
219219+}
+33
pkg/text/template/template.go
···11+// Copyright 2018 The CUE Authors
22+//
33+// Licensed under the Apache License, Version 2.0 (the "License");
44+// you may not use this file except in compliance with the License.
55+// You may obtain a copy of the License at
66+//
77+// http://www.apache.org/licenses/LICENSE-2.0
88+//
99+// Unless required by applicable law or agreed to in writing, software
1010+// distributed under the License is distributed on an "AS IS" BASIS,
1111+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212+// See the License for the specific language governing permissions and
1313+// limitations under the License.
1414+1515+// Copyright 2018 The Go Authors. All rights reserved.
1616+// Use of this source code is governed by a BSD-style
1717+// license that can be found in the LICENSE file.
1818+1919+//go:generate qgo -exclude=Escaper$,Must,Parse -stripstr extract text/template
2020+2121+package template
2222+2323+import "text/template"
2424+2525+// HTMLEscape returns the escaped HTML equivalent of the plain text data s.
2626+func HTMLEscape(s string) string {
2727+ return template.HTMLEscapeString(s)
2828+}
2929+3030+// JSEscape returns the escaped JavaScript equivalent of the plain text data s.
3131+func JSEscape(s string) string {
3232+ return template.JSEscapeString(s)
3333+}