mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-17 16:38:54 +00:00
Performance and style fixes (#1981)
* Switch to integer ranges in for loops Signed-off-by: Stephen Kitt <steve@sk2.org> * Switch to slices functions where appropriate A number of utility functions can be replaced outright; since Miller can technically be used as a library, these are deprecated rather than removed. go:fix directives ensure that they can be replaced automatically. Signed-off-by: Stephen Kitt <steve@sk2.org> * Switch to reflect.TypeFor This is slightly more efficient than TypeOf when the type is known at compile time. Signed-off-by: Stephen Kitt <steve@sk2.org> * Switch to strings.SplitSeq instead of strings.Split SplitSeq results in fewer allocations. Signed-off-by: Stephen Kitt <steve@sk2.org> * Drop obsolete build directives Signed-off-by: Stephen Kitt <steve@sk2.org> * Use min/max instead of explicit comparisons Signed-off-by: Stephen Kitt <steve@sk2.org> * Append slices instead of looping Signed-off-by: Stephen Kitt <steve@sk2.org> --------- Signed-off-by: Stephen Kitt <steve@sk2.org>
This commit is contained in:
parent
e8c4187e67
commit
f0a7c5832f
47 changed files with 117 additions and 175 deletions
|
|
@ -108,7 +108,7 @@ func hexDumpFile(istream *os.File, doRaw bool) {
|
|||
}
|
||||
|
||||
// Print hex payload
|
||||
for i := 0; i < bufferSize; i++ {
|
||||
for i := range bufferSize {
|
||||
if i < numBytesRead {
|
||||
fmt.Printf("%02x ", buffer[i])
|
||||
} else {
|
||||
|
|
@ -125,7 +125,7 @@ func hexDumpFile(istream *os.File, doRaw bool) {
|
|||
if !doRaw {
|
||||
fmt.Printf("|")
|
||||
|
||||
for i := 0; i < numBytesRead; i++ {
|
||||
for i := range numBytesRead {
|
||||
if buffer[i] >= 0x20 && buffer[i] <= 0x7e {
|
||||
fmt.Printf("%c", buffer[i])
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -458,7 +458,7 @@ func BIF_sort_collection(collection *mlrval.Mlrval) *mlrval.Mlrval {
|
|||
arrayval := collection.AcquireArrayValue()
|
||||
n := len(arrayval)
|
||||
array = make([]*mlrval.Mlrval, n)
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
array[i] = arrayval[i].Copy()
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
func stats_test_array(n int) *mlrval.Mlrval {
|
||||
a := make([]*mlrval.Mlrval, n)
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
a[i] = mlrval.FromInt(int64(i))
|
||||
}
|
||||
return mlrval.FromArray(a)
|
||||
|
|
@ -20,7 +20,7 @@ func stats_test_array(n int) *mlrval.Mlrval {
|
|||
func array_to_map_for_test(a *mlrval.Mlrval) *mlrval.Mlrval {
|
||||
array := a.AcquireArrayValue()
|
||||
m := mlrval.NewMlrmap()
|
||||
for i := 0; i < len(array); i++ {
|
||||
for i := range array {
|
||||
key := fmt.Sprint(i)
|
||||
val := array[i]
|
||||
m.PutCopy(key, val)
|
||||
|
|
@ -34,7 +34,7 @@ func TestBIF_count(t *testing.T) {
|
|||
output := BIF_count(input)
|
||||
assert.True(t, output.IsError())
|
||||
|
||||
for n := 0; n < 5; n++ {
|
||||
for n := range 5 {
|
||||
input = stats_test_array(n)
|
||||
assert.True(t, mlrval.Equals(BIF_count(input), mlrval.FromInt(int64(n))))
|
||||
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ package cli
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
|
|
@ -390,12 +391,7 @@ func (flag *Flag) Owns(input string) bool {
|
|||
if flag.name == input {
|
||||
return true
|
||||
}
|
||||
for _, name := range flag.altNames {
|
||||
if name == input {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
return slices.Contains(flag.altNames, input)
|
||||
}
|
||||
|
||||
// Matches is like Owns but is for substring matching, for on-line help with
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ func (node *ASTNode) Print() {
|
|||
// printAux is a recursion-helper for Print.
|
||||
func (node *ASTNode) printAux(depth int) {
|
||||
// Indent
|
||||
for i := 0; i < depth; i++ {
|
||||
for range depth {
|
||||
fmt.Print(" ")
|
||||
}
|
||||
|
||||
|
|
@ -83,14 +83,14 @@ func (node *ASTNode) PrintParex() {
|
|||
// printParexAux is a recursion-helper for PrintParex.
|
||||
func (node *ASTNode) printParexAux(depth int) {
|
||||
if node.IsLeaf() {
|
||||
for i := 0; i < depth; i++ {
|
||||
for range depth {
|
||||
fmt.Print(" ")
|
||||
}
|
||||
fmt.Println(node.Text())
|
||||
|
||||
} else if node.ChildrenAreAllLeaves() {
|
||||
// E.g. (= sum 0) or (+ 1 2)
|
||||
for i := 0; i < depth; i++ {
|
||||
for range depth {
|
||||
fmt.Print(" ")
|
||||
}
|
||||
fmt.Print("(")
|
||||
|
|
@ -104,7 +104,7 @@ func (node *ASTNode) printParexAux(depth int) {
|
|||
|
||||
} else {
|
||||
// Parent and opening parenthesis on first line
|
||||
for i := 0; i < depth; i++ {
|
||||
for range depth {
|
||||
fmt.Print(" ")
|
||||
}
|
||||
fmt.Print("(")
|
||||
|
|
@ -116,7 +116,7 @@ func (node *ASTNode) printParexAux(depth int) {
|
|||
}
|
||||
|
||||
// Closing parenthesis on last line
|
||||
for i := 0; i < depth; i++ {
|
||||
for range depth {
|
||||
fmt.Print(" ")
|
||||
}
|
||||
fmt.Println(")")
|
||||
|
|
|
|||
|
|
@ -695,7 +695,7 @@ func sortM(
|
|||
|
||||
// Make a new map with entries in the new sort order.
|
||||
outmap := mlrval.NewMlrmap()
|
||||
for i := int64(0); i < n; i++ {
|
||||
for i := range n {
|
||||
entry := entries[i]
|
||||
outmap.PutCopy(entry.Key, entry.Value)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -229,7 +229,7 @@ func (reader *RecordReaderCSV) getRecordBatch(
|
|||
if reader.header == nil { // implicit CSV header
|
||||
n := len(csvRecord)
|
||||
reader.header = make([]string, n)
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
reader.header[i] = strconv.Itoa(i + 1)
|
||||
}
|
||||
}
|
||||
|
|
@ -240,7 +240,7 @@ func (reader *RecordReaderCSV) getRecordBatch(
|
|||
nd := int64(len(csvRecord))
|
||||
|
||||
if nh == nd {
|
||||
for i := int64(0); i < nh; i++ {
|
||||
for i := range nh {
|
||||
key := reader.header[i]
|
||||
value := mlrval.FromDeferredType(csvRecord[i])
|
||||
_, err := record.PutReferenceMaybeDedupe(key, value, dedupeFieldNames)
|
||||
|
|
|
|||
|
|
@ -332,7 +332,7 @@ func getRecordBatchImplicitCSVHeader(
|
|||
if reader.headerStrings == nil {
|
||||
n := len(fields)
|
||||
reader.headerStrings = make([]string, n)
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
reader.headerStrings[i] = strconv.Itoa(i + 1)
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -283,7 +283,7 @@ func (bsr *JSONCommentEnabledReader) populateFromLine(p []byte) int {
|
|||
numBytesWritten = len(bsr.lineBytes)
|
||||
bsr.lineBytes = nil
|
||||
} else {
|
||||
for i := 0; i < len(p); i++ {
|
||||
for i := range p {
|
||||
p[i] = bsr.lineBytes[i]
|
||||
}
|
||||
numBytesWritten = len(p)
|
||||
|
|
|
|||
|
|
@ -367,7 +367,7 @@ func getRecordBatchImplicitPprintHeader(
|
|||
if reader.headerStrings == nil {
|
||||
n := len(fields)
|
||||
reader.headerStrings = make([]string, n)
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
reader.headerStrings[i] = strconv.Itoa(i + 1)
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -297,7 +297,7 @@ func getRecordBatchImplicitTSVHeader(
|
|||
if reader.headerStrings == nil {
|
||||
n := len(fields)
|
||||
reader.headerStrings = make([]string, n)
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
reader.headerStrings[i] = strconv.Itoa(i + 1)
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -117,7 +117,7 @@ func GetRealSymmetricEigensystem(
|
|||
n := 2
|
||||
|
||||
found := false
|
||||
for iter := 0; iter < JACOBI_MAXITER; iter++ {
|
||||
for range JACOBI_MAXITER {
|
||||
sum := 0.0
|
||||
for i := 1; i < n; i++ {
|
||||
for j := 0; j < i; j++ {
|
||||
|
|
@ -129,7 +129,7 @@ func GetRealSymmetricEigensystem(
|
|||
break
|
||||
}
|
||||
|
||||
for p := 0; p < n; p++ {
|
||||
for p := range n {
|
||||
for q := p + 1; q < n; q++ {
|
||||
numer := L[p][p] - L[q][q]
|
||||
denom := L[p][q] + L[q][p]
|
||||
|
|
@ -145,8 +145,8 @@ func GetRealSymmetricEigensystem(
|
|||
c := 1.0 / math.Sqrt(t*t+1)
|
||||
s := t * c
|
||||
|
||||
for pi := 0; pi < n; pi++ {
|
||||
for pj := 0; pj < n; pj++ {
|
||||
for pi := range n {
|
||||
for pj := range n {
|
||||
if pi == pj {
|
||||
P[pi][pj] = 1.0
|
||||
} else {
|
||||
|
|
@ -205,18 +205,18 @@ func matmul2(
|
|||
) {
|
||||
var T [2][2]float64
|
||||
n := 2
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 0; j < n; j++ {
|
||||
for i := range n {
|
||||
for j := range n {
|
||||
sum := 0.0
|
||||
for k := 0; k < n; k++ {
|
||||
for k := range n {
|
||||
sum += A[i][k] * B[k][j]
|
||||
}
|
||||
T[i][j] = sum
|
||||
}
|
||||
}
|
||||
// Needs copy in case C's memory is the same as A and/or B
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 0; j < n; j++ {
|
||||
for i := range n {
|
||||
for j := range n {
|
||||
C[i][j] = T[i][j]
|
||||
}
|
||||
}
|
||||
|
|
@ -230,18 +230,18 @@ func matmul2t(
|
|||
) {
|
||||
var T [2][2]float64
|
||||
n := 2
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 0; j < n; j++ {
|
||||
for i := range n {
|
||||
for j := range n {
|
||||
sum := 0.0
|
||||
for k := 0; k < n; k++ {
|
||||
for k := range n {
|
||||
sum += A[k][i] * B[k][j]
|
||||
}
|
||||
T[i][j] = sum
|
||||
}
|
||||
}
|
||||
// Needs copy in case C's memory is the same as A and/or B
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 0; j < n; j++ {
|
||||
for i := range n {
|
||||
for j := range n {
|
||||
C[i][j] = T[i][j]
|
||||
}
|
||||
}
|
||||
|
|
@ -353,7 +353,7 @@ func logisticRegressionAux(
|
|||
d2ldb2 := 0.0
|
||||
ell0 := 0.0
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
xi := xs[i]
|
||||
yi := ys[i]
|
||||
pi := lrp(xi, m0, b0)
|
||||
|
|
@ -391,7 +391,7 @@ func logisticRegressionAux(
|
|||
b = b0 - Hinvgradb
|
||||
|
||||
ell := 0.0
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
xi := xs[i]
|
||||
yi := ys[i]
|
||||
qi := lrq(xi, m, b)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package lib
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
|
@ -49,35 +50,29 @@ func StringListToSet(stringList []string) map[string]bool {
|
|||
return stringSet
|
||||
}
|
||||
|
||||
// SortStrings sorts strs in place.
|
||||
//
|
||||
// Deprecated: use slices.Sort instead.
|
||||
//
|
||||
//go:fix inline
|
||||
func SortStrings(strs []string) {
|
||||
// Go sort API: for ascending sort, return true if element i < element j.
|
||||
sort.Slice(strs, func(i, j int) bool {
|
||||
return strs[i] < strs[j]
|
||||
})
|
||||
slices.Sort(strs)
|
||||
}
|
||||
|
||||
// ReverseStringList reverses strs in place.
|
||||
//
|
||||
// Deprecated: use slices.Reverse instead.
|
||||
//
|
||||
//go:fix inline
|
||||
func ReverseStringList(strs []string) {
|
||||
n := len(strs)
|
||||
i := 0
|
||||
j := n - 1
|
||||
for i < j {
|
||||
temp := strs[i]
|
||||
strs[i] = strs[j]
|
||||
strs[j] = temp
|
||||
i++
|
||||
j--
|
||||
}
|
||||
slices.Reverse(strs)
|
||||
}
|
||||
|
||||
// SortedStrings returns a new slice containing the strings in strs in ascending order.
|
||||
func SortedStrings(strs []string) []string {
|
||||
result := make([]string, len(strs))
|
||||
for i, s := range strs {
|
||||
result[i] = s
|
||||
}
|
||||
// Go sort API: for ascending sort, return true if element i < element j.
|
||||
sort.Slice(result, func(i, j int) bool {
|
||||
return result[i] < result[j]
|
||||
})
|
||||
copy(result, strs)
|
||||
slices.Sort(result)
|
||||
return result
|
||||
}
|
||||
|
||||
|
|
@ -202,13 +197,13 @@ func WriteTempFileOrDie(contents string) string {
|
|||
return handle.Name()
|
||||
}
|
||||
|
||||
// CopyStringArray returns a copy of input.
|
||||
//
|
||||
// Deprecated: use slices.Clone instead.
|
||||
//
|
||||
//go:fix inline
|
||||
func CopyStringArray(input []string) []string {
|
||||
if input == nil {
|
||||
return nil
|
||||
}
|
||||
output := make([]string, len(input))
|
||||
copy(output, input)
|
||||
return output
|
||||
return slices.Clone(input)
|
||||
}
|
||||
|
||||
func StripEmpties(input []string) []string {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package mlrval
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strconv"
|
||||
|
||||
"github.com/johnkerl/miller/v6/pkg/lib"
|
||||
|
|
@ -751,7 +752,7 @@ func (mlrmap *Mlrmap) Label(newNames []string) {
|
|||
func (mlrmap *Mlrmap) SortByKey() {
|
||||
keys := mlrmap.GetKeys()
|
||||
|
||||
lib.SortStrings(keys)
|
||||
slices.Sort(keys)
|
||||
|
||||
other := NewMlrmapAsRecord()
|
||||
|
||||
|
|
@ -766,7 +767,7 @@ func (mlrmap *Mlrmap) SortByKey() {
|
|||
func (mlrmap *Mlrmap) SortByKeyRecursively() {
|
||||
keys := mlrmap.GetKeys()
|
||||
|
||||
lib.SortStrings(keys)
|
||||
slices.Sort(keys)
|
||||
|
||||
other := NewMlrmapAsRecord()
|
||||
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ func (mlrmap *Mlrmap) marshalJSONAuxMultiline(
|
|||
for pe := mlrmap.Head; pe != nil; pe = pe.Next {
|
||||
// Write the key which is necessarily string-valued in Miller, and in
|
||||
// JSON for that matter :)
|
||||
for i := 0; i < elementNestingDepth; i++ {
|
||||
for range elementNestingDepth {
|
||||
buffer.WriteString(JSON_INDENT_STRING)
|
||||
}
|
||||
encoded := string(millerJSONEncodeString(pe.Key))
|
||||
|
|
|
|||
|
|
@ -491,7 +491,7 @@ func (mv *Mlrval) marshalJSONArrayMultipleLines(
|
|||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for i := 0; i < elementNestingDepth; i++ {
|
||||
for range elementNestingDepth {
|
||||
buffer.WriteString(JSON_INDENT_STRING)
|
||||
}
|
||||
buffer.WriteString(elementString)
|
||||
|
|
|
|||
|
|
@ -133,9 +133,9 @@ func (mv *Mlrval) StringifyValuesRecursively() {
|
|||
}
|
||||
|
||||
func (mv *Mlrval) ShowSizes() {
|
||||
fmt.Printf("TOTAL %p %d\n", mv, reflect.TypeOf(*mv).Size())
|
||||
fmt.Printf("TOTAL %p %d\n", mv, reflect.TypeFor[Mlrval]().Size())
|
||||
//fmt.Printf("mv.intf %p %d\n", &mv.intf, reflect.TypeOf(mv.intf).Size())
|
||||
fmt.Printf("mv.printrep %p %d\n", &mv.printrep, reflect.TypeOf(mv.printrep).Size())
|
||||
fmt.Printf("mv.printrepValid %p %d\n", &mv.printrepValid, reflect.TypeOf(mv.printrepValid).Size())
|
||||
fmt.Printf("mv.mvtype %p %d\n", &mv.mvtype, reflect.TypeOf(mv.mvtype).Size())
|
||||
fmt.Printf("mv.printrep %p %d\n", &mv.printrep, reflect.TypeFor[string]().Size())
|
||||
fmt.Printf("mv.printrepValid %p %d\n", &mv.printrepValid, reflect.TypeFor[bool]().Size())
|
||||
fmt.Printf("mv.mvtype %p %d\n", &mv.mvtype, reflect.TypeFor[MVType]().Size())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -339,7 +339,7 @@ func (writer *RecordWriterPPRINT) writePadding(
|
|||
textWidth := utf8.RuneCountInString(text)
|
||||
padWidth := fieldWidth - textWidth
|
||||
ofs := writer.writerOptions.OFS
|
||||
for i := 0; i < padWidth; i++ {
|
||||
for range padWidth {
|
||||
bufferedOutputStream.WriteString(ofs)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ package errors
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/johnkerl/miller/v6/pkg/parsing/token"
|
||||
|
|
@ -60,13 +61,7 @@ func (e *Error) Error() string {
|
|||
if e.Err != nil {
|
||||
fmt.Fprintf(w, "%+v\n", e.Err)
|
||||
} else {
|
||||
suggestSemicolons := false
|
||||
for _, expected := range e.ExpectedTokens {
|
||||
if expected == ";" {
|
||||
suggestSemicolons = true
|
||||
break
|
||||
}
|
||||
}
|
||||
suggestSemicolons := slices.Contains(e.ExpectedTokens, ";")
|
||||
|
||||
if suggestSemicolons {
|
||||
fmt.Fprintf(w, "Please check for missing semicolon.\n")
|
||||
|
|
|
|||
|
|
@ -90,8 +90,8 @@ func ParseLocation(value, format string, location *time.Location) (time.Time, er
|
|||
func Check(format string) error {
|
||||
format = expandShorthands(format)
|
||||
|
||||
parts := strings.Split(format, "%")
|
||||
for _, ps := range parts {
|
||||
parts := strings.SplitSeq(format, "%")
|
||||
for ps := range parts {
|
||||
// Since we split on '%', this is the format code
|
||||
|
||||
// This is for "%%"
|
||||
|
|
@ -289,11 +289,7 @@ func strptime_tz(
|
|||
return time.Time{}, ErrFormatMismatch
|
||||
}
|
||||
// Allow single-digit at end of string (e.g. "1989-1-2" for %Y-%m-%d); we zero-pad when building.
|
||||
if want > remaining {
|
||||
sil = remaining
|
||||
} else {
|
||||
sil = want
|
||||
}
|
||||
sil = min(want, remaining)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
// Handling diff or fc for regression-test.
|
||||
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package platform
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
// Handling diff or fc for regression-test.
|
||||
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package platform
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
// particular care is taken, which is what this file does.
|
||||
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package platform
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
// particular care is taken, which is what this file does.
|
||||
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package platform
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
// Wraps 'sh -c foo bar' or 'cmd /c foo bar', nominally for regression-testing.
|
||||
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package platform
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
// Wraps 'sh -c foo bar' or 'cmd /c foo bar', nominally for regression-testing.
|
||||
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package platform
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package platform
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package platform
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
package runtime
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"github.com/johnkerl/miller/v6/pkg/cli"
|
||||
"github.com/johnkerl/miller/v6/pkg/lib"
|
||||
"github.com/johnkerl/miller/v6/pkg/mlrval"
|
||||
|
|
@ -78,12 +80,11 @@ func (state *State) Update(
|
|||
func (state *State) SetRegexCaptures(
|
||||
captures []string,
|
||||
) {
|
||||
state.regexCapturesByFrame[0] = lib.CopyStringArray(captures)
|
||||
state.regexCapturesByFrame[0] = slices.Clone(captures)
|
||||
}
|
||||
|
||||
func (state *State) GetRegexCaptures() []string {
|
||||
regexCaptures := state.regexCapturesByFrame[0]
|
||||
return lib.CopyStringArray(regexCaptures)
|
||||
return slices.Clone(state.regexCapturesByFrame[0])
|
||||
}
|
||||
|
||||
func (state *State) PushRegexCapturesFrame() {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import (
|
|||
|
||||
func TestIsDecimalDigit(t *testing.T) {
|
||||
var c byte
|
||||
for c = 0x00; c < 0xff; c++ {
|
||||
for c = range byte(0xff) {
|
||||
if c >= '0' && c <= '9' {
|
||||
assert.True(t, isDecimalDigit(c))
|
||||
} else {
|
||||
|
|
@ -19,7 +19,7 @@ func TestIsDecimalDigit(t *testing.T) {
|
|||
|
||||
func TestIsOctalDigit(t *testing.T) {
|
||||
var c byte
|
||||
for c = 0x00; c < 0xff; c++ {
|
||||
for c = range byte(0xff) {
|
||||
if c >= '0' && c <= '7' {
|
||||
assert.True(t, isOctalDigit(c))
|
||||
} else {
|
||||
|
|
@ -30,7 +30,7 @@ func TestIsOctalDigit(t *testing.T) {
|
|||
|
||||
func TestIsHexDigit(t *testing.T) {
|
||||
var c byte
|
||||
for c = 0x00; c < 0xff; c++ {
|
||||
for c = range byte(0xff) {
|
||||
if c >= '0' && c <= '9' {
|
||||
assert.True(t, isHexDigit(c))
|
||||
} else if c >= 'a' && c <= 'f' {
|
||||
|
|
@ -45,7 +45,7 @@ func TestIsHexDigit(t *testing.T) {
|
|||
|
||||
func TestIsFloatDigit(t *testing.T) {
|
||||
var c byte
|
||||
for c = 0x00; c < 0xff; c++ {
|
||||
for c = range byte(0xff) {
|
||||
if c >= '0' && c <= '9' {
|
||||
assert.True(t, isFloatDigit(c))
|
||||
} else if c == '.' || c == '-' || c == '+' || c == 'e' || c == 'E' {
|
||||
|
|
|
|||
|
|
@ -831,8 +831,8 @@ func (rt *RegTester) loadEnvFile(
|
|||
}
|
||||
|
||||
keyValuePairs := lib.NewOrderedMap[string]()
|
||||
lines := strings.Split(contents, "\n")
|
||||
for _, line := range lines {
|
||||
lines := strings.SplitSeq(contents, "\n")
|
||||
for line := range lines {
|
||||
line = strings.TrimSuffix(line, "\r")
|
||||
if line == "" {
|
||||
continue
|
||||
|
|
@ -868,8 +868,8 @@ func (rt *RegTester) loadStringPairFile(
|
|||
}
|
||||
|
||||
pairs = []stringPair{}
|
||||
lines := strings.Split(contents, "\n")
|
||||
for _, line := range lines {
|
||||
lines := strings.SplitSeq(contents, "\n")
|
||||
for line := range lines {
|
||||
line = strings.TrimSuffix(line, "\r")
|
||||
if line == "" {
|
||||
continue
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ package repl
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/johnkerl/miller/v6/pkg/colorizer"
|
||||
|
|
@ -59,10 +60,8 @@ func init() {
|
|||
// No hash-table acceleration; things here are small, and the tool is interactive.
|
||||
func (repl *Repl) findHandler(verbName string) *handlerInfo {
|
||||
for _, entry := range handlerLookupTable {
|
||||
for _, entryVerbName := range entry.verbNames {
|
||||
if entryVerbName == verbName {
|
||||
return &entry
|
||||
}
|
||||
if slices.Contains(entry.verbNames, verbName) {
|
||||
return &entry
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -151,7 +151,7 @@ func ChainTransformer(
|
|||
}
|
||||
|
||||
intermediateDownstreamDoneChannels := make([]chan bool, n)
|
||||
for i = 0; i < n; i++ {
|
||||
for i = range n {
|
||||
intermediateDownstreamDoneChannels[i] = make(chan bool, 1)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -246,13 +246,7 @@ func (tr *TransformerBar) processNoAuto(
|
|||
if !ok {
|
||||
continue
|
||||
}
|
||||
idx := int(float64(tr.width) * (floatValue - tr.lo) / (tr.hi - tr.lo))
|
||||
if idx < 0 {
|
||||
idx = 0
|
||||
}
|
||||
if idx > tr.width {
|
||||
idx = tr.width
|
||||
}
|
||||
idx := min(max(int(float64(tr.width)*(floatValue-tr.lo)/(tr.hi-tr.lo)), 0), tr.width)
|
||||
inrec.PutReference(fieldName, mlrval.FromString(tr.bars[idx]))
|
||||
}
|
||||
|
||||
|
|
@ -322,13 +316,7 @@ func (tr *TransformerBar) processAuto(
|
|||
continue
|
||||
}
|
||||
|
||||
idx := int((float64(tr.width) * (floatValue - lo) / (hi - lo)))
|
||||
if idx < 0 {
|
||||
idx = 0
|
||||
}
|
||||
if idx > tr.width {
|
||||
idx = tr.width
|
||||
}
|
||||
idx := min(max(int((float64(tr.width)*(floatValue-lo)/(hi-lo))), 0), tr.width)
|
||||
|
||||
var buffer bytes.Buffer
|
||||
buffer.WriteString("[")
|
||||
|
|
@ -342,9 +330,7 @@ func (tr *TransformerBar) processAuto(
|
|||
}
|
||||
}
|
||||
|
||||
for _, recordAndContext := range tr.recordsForAutoMode {
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, recordAndContext)
|
||||
}
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, tr.recordsForAutoMode...)
|
||||
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, inrecAndContext) // Emit the end-of-stream marker
|
||||
}
|
||||
|
|
|
|||
|
|
@ -128,10 +128,7 @@ func (tr *TransformerGroupBy) Transform(
|
|||
|
||||
} else {
|
||||
for outer := tr.recordListsByGroup.Head; outer != nil; outer = outer.Next {
|
||||
recordListForGroup := outer.Value
|
||||
for _, recordAndContext := range *recordListForGroup {
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, recordAndContext)
|
||||
}
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, *outer.Value...)
|
||||
}
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, inrecAndContext) // end-of-stream marker
|
||||
}
|
||||
|
|
|
|||
|
|
@ -108,10 +108,7 @@ func (tr *TransformerGroupLike) Transform(
|
|||
|
||||
} else {
|
||||
for outer := tr.recordListsByGroup.Head; outer != nil; outer = outer.Next {
|
||||
recordListForGroup := outer.Value
|
||||
for _, recordAndContext := range *recordListForGroup {
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, recordAndContext)
|
||||
}
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, *outer.Value...)
|
||||
}
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, inrecAndContext) // end-of-stream marker
|
||||
}
|
||||
|
|
|
|||
|
|
@ -172,7 +172,7 @@ func NewTransformerHistogram(
|
|||
countsByField := make(map[string][]int64)
|
||||
for _, valueFieldName := range valueFieldNames {
|
||||
countsByField[valueFieldName] = make([]int64, nbins)
|
||||
for i := int64(0); i < nbins; i++ {
|
||||
for i := range nbins {
|
||||
countsByField[valueFieldName][i] = 0
|
||||
}
|
||||
}
|
||||
|
|
@ -323,7 +323,7 @@ func (tr *TransformerHistogram) emitAuto(
|
|||
for _, valueFieldName := range tr.valueFieldNames {
|
||||
vector := tr.vectorsByFieldName[valueFieldName]
|
||||
n := len(vector)
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
value := vector[i]
|
||||
if haveLoHi {
|
||||
if lo > value {
|
||||
|
|
@ -347,7 +347,7 @@ func (tr *TransformerHistogram) emitAuto(
|
|||
counts := tr.countsByField[valueFieldName]
|
||||
lib.InternalCodingErrorIf(counts == nil)
|
||||
n := len(vector)
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
value := vector[i]
|
||||
if (value >= lo) && (value < hi) {
|
||||
idx := int(((value - lo) * mul))
|
||||
|
|
@ -365,7 +365,7 @@ func (tr *TransformerHistogram) emitAuto(
|
|||
countFieldNames[valueFieldName] = tr.outputPrefix + valueFieldName + "_count"
|
||||
}
|
||||
|
||||
for i := int64(0); i < nbins; i++ {
|
||||
for i := range nbins {
|
||||
outrec := mlrval.NewMlrmapAsRecord()
|
||||
|
||||
outrec.PutReference(
|
||||
|
|
|
|||
|
|
@ -573,7 +573,7 @@ func (tr *TransformerJoin) formAndEmitPairs(
|
|||
|
||||
// Add the joined-on fields to the new output record
|
||||
n := len(tr.opts.leftJoinFieldNames)
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
// These arrays are already guaranteed same-length by CLI parser
|
||||
leftJoinFieldName := tr.opts.leftJoinFieldNames[i]
|
||||
outputJoinFieldName := tr.opts.outputJoinFieldNames[i]
|
||||
|
|
@ -625,10 +625,7 @@ func (tr *TransformerJoin) formAndEmitPairs(
|
|||
func (tr *TransformerJoin) emitLeftUnpairables(
|
||||
outputRecordsAndContexts *[]*types.RecordAndContext, // list of *types.RecordAndContext
|
||||
) {
|
||||
// Loop over each to-be-paired-with record from the left file.
|
||||
for _, leftRecordAndContext := range tr.leftUnpairableRecordsAndContexts {
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, leftRecordAndContext)
|
||||
}
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, tr.leftUnpairableRecordsAndContexts...)
|
||||
}
|
||||
|
||||
func (tr *TransformerJoin) emitLeftUnpairedBuckets(
|
||||
|
|
@ -637,9 +634,7 @@ func (tr *TransformerJoin) emitLeftUnpairedBuckets(
|
|||
for pe := tr.leftBucketsByJoinFieldValues.Head; pe != nil; pe = pe.Next {
|
||||
bucket := pe.Value
|
||||
if !bucket.WasPaired {
|
||||
for _, recordAndContext := range bucket.RecordsAndContexts {
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, recordAndContext)
|
||||
}
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, bucket.RecordsAndContexts...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -263,10 +263,7 @@ func (tr *TransformerMostOrLeastFrequent) Transform(
|
|||
}
|
||||
|
||||
// Emit top n
|
||||
outputLength := inputLength
|
||||
if inputLength > tr.maxOutputLength {
|
||||
outputLength = tr.maxOutputLength
|
||||
}
|
||||
outputLength := min(inputLength, tr.maxOutputLength)
|
||||
for i := int64(0); i < outputLength; i++ {
|
||||
outrec := mlrval.NewMlrmapAsRecord()
|
||||
groupByFieldValues := tr.valuesForGroup[sortPairs[i].groupingKey]
|
||||
|
|
|
|||
|
|
@ -458,8 +458,8 @@ func (tr *TransformerNest) explodeValuesAcrossRecords(
|
|||
svalue := mvalue.String()
|
||||
|
||||
// Not lib.SplitString so 'x=' will map to 'x=', rather than no field at all
|
||||
pieces := strings.Split(svalue, tr.nestedFS)
|
||||
for _, piece := range pieces {
|
||||
pieces := strings.SplitSeq(svalue, tr.nestedFS)
|
||||
for piece := range pieces {
|
||||
outrec := inrec.Copy()
|
||||
outrec.PutReference(fieldName, mlrval.FromString(piece))
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, types.NewRecordAndContext(outrec, &inrecAndContext.Context))
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/johnkerl/miller/v6/pkg/cli"
|
||||
|
|
@ -180,7 +181,7 @@ func NewTransformerReorder(
|
|||
tr.recordTransformerFunc = tr.reorderToStartWithRegex
|
||||
} else {
|
||||
tr.recordTransformerFunc = tr.reorderToStartNoRegex
|
||||
lib.ReverseStringList(tr.fieldNames)
|
||||
slices.Reverse(tr.fieldNames)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -104,12 +104,12 @@ func (tr *TransformerShuffle) Transform(
|
|||
// * Make a pseudorandom permutation using pseudorandom swaps in the image map.
|
||||
n := int64(len(tr.recordsAndContexts))
|
||||
images := make([]int64, n)
|
||||
for i := int64(0); i < n; i++ {
|
||||
for i := range n {
|
||||
images[i] = i
|
||||
}
|
||||
unusedStart := int64(0)
|
||||
numUnused := n
|
||||
for i := int64(0); i < n; i++ {
|
||||
for i := range n {
|
||||
// Select a pseudorandom element from the pool of unused images.
|
||||
u := lib.RandRange(unusedStart, unusedStart+numUnused)
|
||||
temp := images[u]
|
||||
|
|
@ -127,7 +127,7 @@ func (tr *TransformerShuffle) Transform(
|
|||
// Transfer from input array to output list. Because permutations are one-to-one maps,
|
||||
// all input records have ownership transferred exactly once. So, there are no
|
||||
// records to copy here.
|
||||
for i := int64(0); i < n; i++ {
|
||||
for i := range n {
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, array[images[i]])
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -456,14 +456,10 @@ func (tr *TransformerSort) Transform(
|
|||
// Now output the groups
|
||||
for _, groupingKeyAndMlrvals := range groupingKeysAndMlrvals {
|
||||
recordsInGroup := tr.recordListsByGroup.Get(groupingKeyAndMlrvals.groupingKey)
|
||||
for _, recordAndContext := range *recordsInGroup {
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, recordAndContext)
|
||||
}
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, *recordsInGroup...)
|
||||
}
|
||||
|
||||
for _, recordAndContext := range tr.spillGroup {
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, recordAndContext)
|
||||
}
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, tr.spillGroup...)
|
||||
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, inrecAndContext) // end-of-stream marker
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/johnkerl/miller/v6/pkg/cli"
|
||||
|
|
@ -180,7 +181,7 @@ func transformerStats1ParseCLI(
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groupByFieldNameList = lib.CopyStringArray(valueFieldNameList)
|
||||
groupByFieldNameList = slices.Clone(valueFieldNameList)
|
||||
|
||||
} else if opt == "-i" {
|
||||
doInterpolatedPercentiles = true
|
||||
|
|
|
|||
|
|
@ -161,10 +161,7 @@ func (tr *TransformerTail) Transform(
|
|||
|
||||
} else {
|
||||
for outer := tr.recordListsByGroup.Head; outer != nil; outer = outer.Next {
|
||||
recordListForGroup := outer.Value
|
||||
for _, recordAndContext := range *recordListForGroup {
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, recordAndContext)
|
||||
}
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, *outer.Value...)
|
||||
}
|
||||
*outputRecordsAndContexts = append(*outputRecordsAndContexts, inrecAndContext) // end-of-stream marker
|
||||
}
|
||||
|
|
|
|||
|
|
@ -581,7 +581,7 @@ func compareLexically(
|
|||
) int {
|
||||
lib.InternalCodingErrorIf(len(leftFieldValues) != len(rightFieldValues))
|
||||
n := len(leftFieldValues)
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
left := leftFieldValues[i].String()
|
||||
right := rightFieldValues[i].String()
|
||||
// Returns -1, 0, 1 as left <, ==, > right
|
||||
|
|
|
|||
|
|
@ -515,13 +515,13 @@ func (acc *Stats1MeanAbsDevAccumulator) Emit() *mlrval.Mlrval {
|
|||
mn := mlrval.FromInt(int64(n))
|
||||
|
||||
mean := mlrval.FromInt(0)
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
mean = bifs.BIF_plus_binary(mean, acc.samples[i])
|
||||
}
|
||||
mean = bifs.BIF_divide(mean, mn)
|
||||
|
||||
meanAbsDev := mlrval.FromInt(0)
|
||||
for i := 0; i < n; i++ {
|
||||
for i := range n {
|
||||
diff := bifs.BIF_minus_binary(mean, acc.samples[i])
|
||||
meanAbsDev = bifs.BIF_plus_binary(meanAbsDev, bifs.BIF_abs(diff))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue