···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 csv
1616+1717+import (
1818+ "bytes"
1919+ "encoding/csv"
2020+ "io"
2121+2222+ "cuelang.org/go/cue"
2323+)
2424+2525+// Encode encode the given list of lists to CSV.
2626+func Encode(x cue.Value) (string, error) {
2727+ buf := &bytes.Buffer{}
2828+ w := csv.NewWriter(buf)
2929+ iter, err := x.List()
3030+ if err != nil {
3131+ return "", err
3232+ }
3333+ for iter.Next() {
3434+ row, err := iter.Value().List()
3535+ if err != nil {
3636+ return "", err
3737+ }
3838+ a := []string{}
3939+ for row.Next() {
4040+ col := row.Value()
4141+ if str, err := col.String(); err == nil {
4242+ a = append(a, str)
4343+ } else {
4444+ b, err := col.MarshalJSON()
4545+ if err != nil {
4646+ return "", err
4747+ }
4848+ a = append(a, string(b))
4949+ }
5050+ }
5151+ w.Write(a)
5252+ }
5353+ w.Flush()
5454+ return buf.String(), nil
5555+}
5656+5757+// Decode reads in a csv into a list of lists.
5858+func Decode(r io.Reader) ([][]string, error) {
5959+ return csv.NewReader(r).ReadAll()
6060+}
+22
pkg/encoding/hex/manual.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 hex
1616+1717+import "encoding/hex"
1818+1919+// Encode returns the hexadecimal encoding of src.
2020+func Encode(src []byte) string {
2121+ return hex.EncodeToString(src)
2222+}
+104
pkg/encoding/json/manual.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 json
1616+1717+import (
1818+ "bytes"
1919+ "encoding/json"
2020+ "fmt"
2121+2222+ "cuelang.org/go/cue"
2323+ "cuelang.org/go/cue/parser"
2424+ "cuelang.org/go/cue/token"
2525+)
2626+2727+// Compact generates the JSON-encoded src with insignificant space characters
2828+// elided.
2929+func Compact(src []byte) (string, error) {
3030+ dst := bytes.Buffer{}
3131+ if err := json.Compact(&dst, src); err != nil {
3232+ return "", err
3333+ }
3434+ return dst.String(), nil
3535+}
3636+3737+// Indent creates an indented form of the JSON-encoded src.
3838+// Each element in a JSON object or array begins on a new,
3939+// indented line beginning with prefix followed by one or more
4040+// copies of indent according to the indentation nesting.
4141+// The data appended to dst does not begin with the prefix nor
4242+// any indentation, to make it easier to embed inside other formatted JSON data.
4343+// Although leading space characters (space, tab, carriage return, newline)
4444+// at the beginning of src are dropped, trailing space characters
4545+// at the end of src are preserved and copied to dst.
4646+// For example, if src has no trailing spaces, neither will dst;
4747+// if src ends in a trailing newline, so will dst.
4848+func Indent(src []byte, prefix, indent string) (string, error) {
4949+ dst := bytes.Buffer{}
5050+ if err := json.Indent(&dst, src, prefix, indent); err != nil {
5151+ return "", err
5252+ }
5353+ return dst.String(), nil
5454+}
5555+5656+// HTMLEscape returns the JSON-encoded src with <, >, &, U+2028 and
5757+// U+2029 characters inside string literals changed to \u003c, \u003e, \u0026,
5858+// \u2028, \u2029 so that the JSON will be safe to embed inside HTML <script>
5959+// tags. For historical reasons, web browsers don't honor standard HTML escaping
6060+// within <script> tags, so an alternative JSON encoding must be used.
6161+func HTMLEscape(src []byte) string {
6262+ dst := &bytes.Buffer{}
6363+ json.HTMLEscape(dst, src)
6464+ return dst.String()
6565+}
6666+6767+// Marshal returns the JSON encoding of v.
6868+func Marshal(v cue.Value) (string, error) {
6969+ b, err := json.Marshal(v)
7070+ return string(b), err
7171+}
7272+7373+// MarshalStream turns a list into a stream of JSON objects.
7474+func MarshalStream(v cue.Value) (string, error) {
7575+ // TODO: return an io.Reader and allow asynchronous processing.
7676+ iter, err := v.List()
7777+ if err != nil {
7878+ return "", err
7979+ }
8080+ buf := &bytes.Buffer{}
8181+ for iter.Next() {
8282+ b, err := json.Marshal(iter.Value())
8383+ if err != nil {
8484+ return "", err
8585+ }
8686+ buf.Write(b)
8787+ buf.WriteByte('\n')
8888+ }
8989+ return buf.String(), nil
9090+}
9191+9292+// Unmarshal parses the JSON-encoded data.
9393+func Unmarshal(b []byte) (*cue.Instance, error) {
9494+ if !json.Valid(b) {
9595+ return nil, fmt.Errorf("json: invalid JSON")
9696+ }
9797+ fset := token.NewFileSet()
9898+ expr, err := parser.ParseExpr(fset, "json", b)
9999+ if err != nil {
100100+ // NOTE: should never happen.
101101+ return nil, fmt.Errorf("json: could not parse JSON: %v", err)
102102+ }
103103+ return cue.FromExpr(fset, expr)
104104+}
+61
pkg/encoding/yaml/manual.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 yaml
1616+1717+import (
1818+ "bytes"
1919+2020+ "cuelang.org/go/cue"
2121+ "cuelang.org/go/cue/token"
2222+ "cuelang.org/go/internal/third_party/yaml"
2323+ goyaml "github.com/ghodss/yaml"
2424+)
2525+2626+// Marshal returns the YAML encoding of v.
2727+func Marshal(v cue.Value) (string, error) {
2828+ b, err := goyaml.Marshal(v)
2929+ return string(b), err
3030+}
3131+3232+// MarshalStream returns the YAML encoding of v.
3333+func MarshalStream(v cue.Value) (string, error) {
3434+ // TODO: return an io.Reader and allow asynchronous processing.
3535+ iter, err := v.List()
3636+ if err != nil {
3737+ return "", err
3838+ }
3939+ buf := &bytes.Buffer{}
4040+ for i := 0; iter.Next(); i++ {
4141+ if i > 0 {
4242+ buf.WriteString("---\n")
4343+ }
4444+ b, err := goyaml.Marshal(iter.Value())
4545+ if err != nil {
4646+ return "", err
4747+ }
4848+ buf.Write(b)
4949+ }
5050+ return buf.String(), nil
5151+}
5252+5353+// Unmarshal parses the YAML to a CUE instance.
5454+func Unmarshal(data []byte) (*cue.Instance, error) {
5555+ fset := token.NewFileSet()
5656+ expr, err := yaml.Unmarshal(fset, "", data)
5757+ if err != nil {
5858+ return nil, err
5959+ }
6060+ return cue.FromExpr(fset, expr)
6161+}
+129
pkg/math/bits/manual.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+package bits
2020+2121+import (
2222+ "math/big"
2323+ "math/bits"
2424+)
2525+2626+// And returns the bitwise and of a and b (a & b in Go).
2727+//
2828+func And(a, b *big.Int) *big.Int {
2929+ wa := a.Bits()
3030+ wb := b.Bits()
3131+ n := len(wa)
3232+ if len(wb) < n {
3333+ n = len(wb)
3434+ }
3535+ w := make([]big.Word, n)
3636+ for i := range w {
3737+ w[i] = wa[i] & wb[i]
3838+ }
3939+ i := &big.Int{}
4040+ i.SetBits(w)
4141+ return i
4242+}
4343+4444+// Or returns the bitwise or of a and b (a | b in Go).
4545+//
4646+func Or(a, b *big.Int) *big.Int {
4747+ wa := a.Bits()
4848+ wb := b.Bits()
4949+ var w []big.Word
5050+ n := len(wa)
5151+ if len(wa) > len(wb) {
5252+ w = append(w, wa...)
5353+ n = len(wb)
5454+ } else {
5555+ w = append(w, wb...)
5656+ }
5757+ for i := 0; i < n; i++ {
5858+ w[i] = wa[i] | wb[i]
5959+ }
6060+ i := &big.Int{}
6161+ i.SetBits(w)
6262+ return i
6363+}
6464+6565+// Xor returns the bitwise xor of a and b (a ^ b in Go).
6666+//
6767+func Xor(a, b *big.Int) *big.Int {
6868+ wa := a.Bits()
6969+ wb := b.Bits()
7070+ var w []big.Word
7171+ n := len(wa)
7272+ if len(wa) > len(wb) {
7373+ w = append(w, wa...)
7474+ n = len(wb)
7575+ } else {
7676+ w = append(w, wb...)
7777+ }
7878+ for i := 0; i < n; i++ {
7979+ w[i] = wa[i] ^ wb[i]
8080+ }
8181+ i := &big.Int{}
8282+ i.SetBits(w)
8383+ return i
8484+}
8585+8686+// Clear returns the bitwise and not of a and b (a &^ b in Go).
8787+//
8888+func Clear(a, b *big.Int) *big.Int {
8989+ wa := a.Bits()
9090+ wb := b.Bits()
9191+ w := append([]big.Word(nil), wa...)
9292+ for i, m := range wb {
9393+ if i >= len(w) {
9494+ break
9595+ }
9696+ w[i] = wa[i] &^ m
9797+ }
9898+ i := &big.Int{}
9999+ i.SetBits(w)
100100+ return i
101101+}
102102+103103+// TODO: ShiftLeft, maybe trailing and leading zeros
104104+105105+// OnesCount returns the number of one bits ("population count") in x.
106106+func OnesCount(x uint64) int {
107107+ return bits.OnesCount64(x)
108108+}
109109+110110+// RotateLeft returns the value of x rotated left by (k mod UintSize) bits.
111111+// To rotate x right by k bits, call RotateLeft(x, -k).
112112+func RotateLeft(x uint64, k int) uint64 {
113113+ return bits.RotateLeft64(x, k)
114114+}
115115+116116+// Reverse returns the value of x with its bits in reversed order.
117117+func Reverse(x uint64) uint64 {
118118+ return bits.Reverse64(x)
119119+}
120120+121121+// ReverseBytes returns the value of x with its bytes in reversed order.
122122+func ReverseBytes(x uint64) uint64 {
123123+ return bits.ReverseBytes64(x)
124124+}
125125+126126+// Len returns the minimum number of bits required to represent x; the result is 0 for x == 0.
127127+func Len(x uint64) int {
128128+ return bits.Len64(x)
129129+}
+28
pkg/path/manual.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 path
1616+1717+import "path"
1818+1919+var split = path.Split
2020+2121+// Split splits path immediately following the final slash and returns them as
2222+// the list [dir, file], separating it into a directory and file name component.
2323+// If there is no slash in path, Split returns an empty dir and file set to
2424+// path. The returned values have the property that path = dir+file.
2525+func Split(path string) []string {
2626+ file, dir := split(path)
2727+ return []string{file, dir}
2828+}
+24
pkg/strconv/manual.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 strconv
1616+1717+// Unquote interprets s as a single-quoted, double-quoted,
1818+// or backquoted CUE string literal, returning the string value
1919+// that s quotes.
2020+func Unquote(s string) (string, error) {
2121+ return Unquote(s)
2222+}
2323+2424+// TODO: replace parsing functions with parsing to apd
+58
pkg/strings/manual.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 strings
1616+1717+import (
1818+ "strings"
1919+ "unicode"
2020+)
2121+2222+// ToTitle returns a copy of the string s with all Unicode letters that begin
2323+// words mapped to their title case.
2424+func ToTitle(s string) string {
2525+ // Use a closure here to remember state.
2626+ // Hackish but effective. Depends on Map scanning in order and calling
2727+ // the closure once per rune.
2828+ prev := ' '
2929+ return strings.Map(
3030+ func(r rune) rune {
3131+ if unicode.IsSpace(prev) {
3232+ prev = r
3333+ return unicode.ToTitle(r)
3434+ }
3535+ prev = r
3636+ return r
3737+ },
3838+ s)
3939+}
4040+4141+// ToCamel returns a copy of the string s with all Unicode letters that begin
4242+// words mapped to lower case.
4343+func ToCamel(s string) string {
4444+ // Use a closure here to remember state.
4545+ // Hackish but effective. Depends on Map scanning in order and calling
4646+ // the closure once per rune.
4747+ prev := ' '
4848+ return strings.Map(
4949+ func(r rune) rune {
5050+ if unicode.IsSpace(prev) {
5151+ prev = r
5252+ return unicode.ToLower(r)
5353+ }
5454+ prev = r
5555+ return r
5656+ },
5757+ s)
5858+}
+38
pkg/text/tabwriter/manual.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 tabwriter
1616+1717+import (
1818+ "bytes"
1919+ "text/tabwriter"
2020+2121+ "cuelang.org/go/cue"
2222+)
2323+2424+// Write formats text in columns. See golang.org/pkg/text/tabwriter for more
2525+// info.
2626+func Write(data cue.Value) (string, error) {
2727+ buf := &bytes.Buffer{}
2828+ tw := tabwriter.NewWriter(buf, 0, 4, 1, ' ', 0)
2929+ b, err := data.Bytes()
3030+ if err != nil {
3131+ return "", err
3232+ }
3333+ _, err = tw.Write(b)
3434+ if err != nil {
3535+ return "", err
3636+ }
3737+ return buf.String(), err
3838+}
+35
pkg/text/template/manual.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 template
1616+1717+import (
1818+ "bytes"
1919+ "text/template"
2020+2121+ "cuelang.org/go/cue"
2222+)
2323+2424+// Execute executes a Go-style template.
2525+func Execute(templ string, data cue.Value) (string, error) {
2626+ t, err := template.New("").Parse(templ)
2727+ if err != nil {
2828+ return "", err
2929+ }
3030+ buf := &bytes.Buffer{}
3131+ if err := t.Execute(buf, data); err != nil {
3232+ return "", err
3333+ }
3434+ return buf.String(), nil
3535+}