this repo has no description
0
fork

Configure Feed

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

pkg: added some manually crafted builtins

Change-Id: I1e5d8b987b2fbd2605a9ff5853dc3b1e7eb9b620

+559
+60
pkg/encoding/csv/manual.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 csv 16 + 17 + import ( 18 + "bytes" 19 + "encoding/csv" 20 + "io" 21 + 22 + "cuelang.org/go/cue" 23 + ) 24 + 25 + // Encode encode the given list of lists to CSV. 26 + func Encode(x cue.Value) (string, error) { 27 + buf := &bytes.Buffer{} 28 + w := csv.NewWriter(buf) 29 + iter, err := x.List() 30 + if err != nil { 31 + return "", err 32 + } 33 + for iter.Next() { 34 + row, err := iter.Value().List() 35 + if err != nil { 36 + return "", err 37 + } 38 + a := []string{} 39 + for row.Next() { 40 + col := row.Value() 41 + if str, err := col.String(); err == nil { 42 + a = append(a, str) 43 + } else { 44 + b, err := col.MarshalJSON() 45 + if err != nil { 46 + return "", err 47 + } 48 + a = append(a, string(b)) 49 + } 50 + } 51 + w.Write(a) 52 + } 53 + w.Flush() 54 + return buf.String(), nil 55 + } 56 + 57 + // Decode reads in a csv into a list of lists. 58 + func Decode(r io.Reader) ([][]string, error) { 59 + return csv.NewReader(r).ReadAll() 60 + }
+22
pkg/encoding/hex/manual.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 hex 16 + 17 + import "encoding/hex" 18 + 19 + // Encode returns the hexadecimal encoding of src. 20 + func Encode(src []byte) string { 21 + return hex.EncodeToString(src) 22 + }
+104
pkg/encoding/json/manual.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 json 16 + 17 + import ( 18 + "bytes" 19 + "encoding/json" 20 + "fmt" 21 + 22 + "cuelang.org/go/cue" 23 + "cuelang.org/go/cue/parser" 24 + "cuelang.org/go/cue/token" 25 + ) 26 + 27 + // Compact generates the JSON-encoded src with insignificant space characters 28 + // elided. 29 + func Compact(src []byte) (string, error) { 30 + dst := bytes.Buffer{} 31 + if err := json.Compact(&dst, src); err != nil { 32 + return "", err 33 + } 34 + return dst.String(), nil 35 + } 36 + 37 + // Indent creates an indented form of the JSON-encoded src. 38 + // Each element in a JSON object or array begins on a new, 39 + // indented line beginning with prefix followed by one or more 40 + // copies of indent according to the indentation nesting. 41 + // The data appended to dst does not begin with the prefix nor 42 + // any indentation, to make it easier to embed inside other formatted JSON data. 43 + // Although leading space characters (space, tab, carriage return, newline) 44 + // at the beginning of src are dropped, trailing space characters 45 + // at the end of src are preserved and copied to dst. 46 + // For example, if src has no trailing spaces, neither will dst; 47 + // if src ends in a trailing newline, so will dst. 48 + func Indent(src []byte, prefix, indent string) (string, error) { 49 + dst := bytes.Buffer{} 50 + if err := json.Indent(&dst, src, prefix, indent); err != nil { 51 + return "", err 52 + } 53 + return dst.String(), nil 54 + } 55 + 56 + // HTMLEscape returns the JSON-encoded src with <, >, &, U+2028 and 57 + // U+2029 characters inside string literals changed to \u003c, \u003e, \u0026, 58 + // \u2028, \u2029 so that the JSON will be safe to embed inside HTML <script> 59 + // tags. For historical reasons, web browsers don't honor standard HTML escaping 60 + // within <script> tags, so an alternative JSON encoding must be used. 61 + func HTMLEscape(src []byte) string { 62 + dst := &bytes.Buffer{} 63 + json.HTMLEscape(dst, src) 64 + return dst.String() 65 + } 66 + 67 + // Marshal returns the JSON encoding of v. 68 + func Marshal(v cue.Value) (string, error) { 69 + b, err := json.Marshal(v) 70 + return string(b), err 71 + } 72 + 73 + // MarshalStream turns a list into a stream of JSON objects. 74 + func MarshalStream(v cue.Value) (string, error) { 75 + // TODO: return an io.Reader and allow asynchronous processing. 76 + iter, err := v.List() 77 + if err != nil { 78 + return "", err 79 + } 80 + buf := &bytes.Buffer{} 81 + for iter.Next() { 82 + b, err := json.Marshal(iter.Value()) 83 + if err != nil { 84 + return "", err 85 + } 86 + buf.Write(b) 87 + buf.WriteByte('\n') 88 + } 89 + return buf.String(), nil 90 + } 91 + 92 + // Unmarshal parses the JSON-encoded data. 93 + func Unmarshal(b []byte) (*cue.Instance, error) { 94 + if !json.Valid(b) { 95 + return nil, fmt.Errorf("json: invalid JSON") 96 + } 97 + fset := token.NewFileSet() 98 + expr, err := parser.ParseExpr(fset, "json", b) 99 + if err != nil { 100 + // NOTE: should never happen. 101 + return nil, fmt.Errorf("json: could not parse JSON: %v", err) 102 + } 103 + return cue.FromExpr(fset, expr) 104 + }
+61
pkg/encoding/yaml/manual.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 yaml 16 + 17 + import ( 18 + "bytes" 19 + 20 + "cuelang.org/go/cue" 21 + "cuelang.org/go/cue/token" 22 + "cuelang.org/go/internal/third_party/yaml" 23 + goyaml "github.com/ghodss/yaml" 24 + ) 25 + 26 + // Marshal returns the YAML encoding of v. 27 + func Marshal(v cue.Value) (string, error) { 28 + b, err := goyaml.Marshal(v) 29 + return string(b), err 30 + } 31 + 32 + // MarshalStream returns the YAML encoding of v. 33 + func MarshalStream(v cue.Value) (string, error) { 34 + // TODO: return an io.Reader and allow asynchronous processing. 35 + iter, err := v.List() 36 + if err != nil { 37 + return "", err 38 + } 39 + buf := &bytes.Buffer{} 40 + for i := 0; iter.Next(); i++ { 41 + if i > 0 { 42 + buf.WriteString("---\n") 43 + } 44 + b, err := goyaml.Marshal(iter.Value()) 45 + if err != nil { 46 + return "", err 47 + } 48 + buf.Write(b) 49 + } 50 + return buf.String(), nil 51 + } 52 + 53 + // Unmarshal parses the YAML to a CUE instance. 54 + func Unmarshal(data []byte) (*cue.Instance, error) { 55 + fset := token.NewFileSet() 56 + expr, err := yaml.Unmarshal(fset, "", data) 57 + if err != nil { 58 + return nil, err 59 + } 60 + return cue.FromExpr(fset, expr) 61 + }
+129
pkg/math/bits/manual.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 + package bits 20 + 21 + import ( 22 + "math/big" 23 + "math/bits" 24 + ) 25 + 26 + // And returns the bitwise and of a and b (a & b in Go). 27 + // 28 + func And(a, b *big.Int) *big.Int { 29 + wa := a.Bits() 30 + wb := b.Bits() 31 + n := len(wa) 32 + if len(wb) < n { 33 + n = len(wb) 34 + } 35 + w := make([]big.Word, n) 36 + for i := range w { 37 + w[i] = wa[i] & wb[i] 38 + } 39 + i := &big.Int{} 40 + i.SetBits(w) 41 + return i 42 + } 43 + 44 + // Or returns the bitwise or of a and b (a | b in Go). 45 + // 46 + func Or(a, b *big.Int) *big.Int { 47 + wa := a.Bits() 48 + wb := b.Bits() 49 + var w []big.Word 50 + n := len(wa) 51 + if len(wa) > len(wb) { 52 + w = append(w, wa...) 53 + n = len(wb) 54 + } else { 55 + w = append(w, wb...) 56 + } 57 + for i := 0; i < n; i++ { 58 + w[i] = wa[i] | wb[i] 59 + } 60 + i := &big.Int{} 61 + i.SetBits(w) 62 + return i 63 + } 64 + 65 + // Xor returns the bitwise xor of a and b (a ^ b in Go). 66 + // 67 + func Xor(a, b *big.Int) *big.Int { 68 + wa := a.Bits() 69 + wb := b.Bits() 70 + var w []big.Word 71 + n := len(wa) 72 + if len(wa) > len(wb) { 73 + w = append(w, wa...) 74 + n = len(wb) 75 + } else { 76 + w = append(w, wb...) 77 + } 78 + for i := 0; i < n; i++ { 79 + w[i] = wa[i] ^ wb[i] 80 + } 81 + i := &big.Int{} 82 + i.SetBits(w) 83 + return i 84 + } 85 + 86 + // Clear returns the bitwise and not of a and b (a &^ b in Go). 87 + // 88 + func Clear(a, b *big.Int) *big.Int { 89 + wa := a.Bits() 90 + wb := b.Bits() 91 + w := append([]big.Word(nil), wa...) 92 + for i, m := range wb { 93 + if i >= len(w) { 94 + break 95 + } 96 + w[i] = wa[i] &^ m 97 + } 98 + i := &big.Int{} 99 + i.SetBits(w) 100 + return i 101 + } 102 + 103 + // TODO: ShiftLeft, maybe trailing and leading zeros 104 + 105 + // OnesCount returns the number of one bits ("population count") in x. 106 + func OnesCount(x uint64) int { 107 + return bits.OnesCount64(x) 108 + } 109 + 110 + // RotateLeft returns the value of x rotated left by (k mod UintSize) bits. 111 + // To rotate x right by k bits, call RotateLeft(x, -k). 112 + func RotateLeft(x uint64, k int) uint64 { 113 + return bits.RotateLeft64(x, k) 114 + } 115 + 116 + // Reverse returns the value of x with its bits in reversed order. 117 + func Reverse(x uint64) uint64 { 118 + return bits.Reverse64(x) 119 + } 120 + 121 + // ReverseBytes returns the value of x with its bytes in reversed order. 122 + func ReverseBytes(x uint64) uint64 { 123 + return bits.ReverseBytes64(x) 124 + } 125 + 126 + // Len returns the minimum number of bits required to represent x; the result is 0 for x == 0. 127 + func Len(x uint64) int { 128 + return bits.Len64(x) 129 + }
+28
pkg/path/manual.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 path 16 + 17 + import "path" 18 + 19 + var split = path.Split 20 + 21 + // Split splits path immediately following the final slash and returns them as 22 + // the list [dir, file], separating it into a directory and file name component. 23 + // If there is no slash in path, Split returns an empty dir and file set to 24 + // path. The returned values have the property that path = dir+file. 25 + func Split(path string) []string { 26 + file, dir := split(path) 27 + return []string{file, dir} 28 + }
+24
pkg/strconv/manual.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 strconv 16 + 17 + // Unquote interprets s as a single-quoted, double-quoted, 18 + // or backquoted CUE string literal, returning the string value 19 + // that s quotes. 20 + func Unquote(s string) (string, error) { 21 + return Unquote(s) 22 + } 23 + 24 + // TODO: replace parsing functions with parsing to apd
+58
pkg/strings/manual.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 strings 16 + 17 + import ( 18 + "strings" 19 + "unicode" 20 + ) 21 + 22 + // ToTitle returns a copy of the string s with all Unicode letters that begin 23 + // words mapped to their title case. 24 + func ToTitle(s string) string { 25 + // Use a closure here to remember state. 26 + // Hackish but effective. Depends on Map scanning in order and calling 27 + // the closure once per rune. 28 + prev := ' ' 29 + return strings.Map( 30 + func(r rune) rune { 31 + if unicode.IsSpace(prev) { 32 + prev = r 33 + return unicode.ToTitle(r) 34 + } 35 + prev = r 36 + return r 37 + }, 38 + s) 39 + } 40 + 41 + // ToCamel returns a copy of the string s with all Unicode letters that begin 42 + // words mapped to lower case. 43 + func ToCamel(s string) string { 44 + // Use a closure here to remember state. 45 + // Hackish but effective. Depends on Map scanning in order and calling 46 + // the closure once per rune. 47 + prev := ' ' 48 + return strings.Map( 49 + func(r rune) rune { 50 + if unicode.IsSpace(prev) { 51 + prev = r 52 + return unicode.ToLower(r) 53 + } 54 + prev = r 55 + return r 56 + }, 57 + s) 58 + }
+38
pkg/text/tabwriter/manual.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 tabwriter 16 + 17 + import ( 18 + "bytes" 19 + "text/tabwriter" 20 + 21 + "cuelang.org/go/cue" 22 + ) 23 + 24 + // Write formats text in columns. See golang.org/pkg/text/tabwriter for more 25 + // info. 26 + func Write(data cue.Value) (string, error) { 27 + buf := &bytes.Buffer{} 28 + tw := tabwriter.NewWriter(buf, 0, 4, 1, ' ', 0) 29 + b, err := data.Bytes() 30 + if err != nil { 31 + return "", err 32 + } 33 + _, err = tw.Write(b) 34 + if err != nil { 35 + return "", err 36 + } 37 + return buf.String(), err 38 + }
+35
pkg/text/template/manual.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 template 16 + 17 + import ( 18 + "bytes" 19 + "text/template" 20 + 21 + "cuelang.org/go/cue" 22 + ) 23 + 24 + // Execute executes a Go-style template. 25 + func Execute(templ string, data cue.Value) (string, error) { 26 + t, err := template.New("").Parse(templ) 27 + if err != nil { 28 + return "", err 29 + } 30 + buf := &bytes.Buffer{} 31 + if err := t.Execute(buf, data); err != nil { 32 + return "", err 33 + } 34 + return buf.String(), nil 35 + }