Skip to content

Commit

Permalink
Code-dedupe logic for array slices and string slices (#960)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl authored Feb 22, 2022
1 parent 36298be commit 7872753
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 53 deletions.
73 changes: 73 additions & 0 deletions internal/pkg/bifs/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,3 +865,76 @@ func unaliasArrayLengthIndex(n int, mindex int) (int, bool) {
return -1, false
}
}

// MillerSliceAccess is code shared by the string-slicer and the array-slicer.
func MillerSliceAccess(
lowerIndexMlrval *mlrval.Mlrval,
upperIndexMlrval *mlrval.Mlrval,
n int,
) (
sliceIsEmpty bool, // true if the output of the slice should empty string/array
absentOrError *mlrval.Mlrval, // non-nil if the output of the slice should be absent/error
lowerZindex int, // lower zindex if first two return values are false & nil
upperZindex int, // upper zindex if first two return values are false & nil
) {

if lowerIndexMlrval.IsAbsent() {
return false, mlrval.ABSENT, 0, 0
}
if upperIndexMlrval.IsAbsent() {
return false, mlrval.ABSENT, 0, 0
}

lowerIndex, ok := lowerIndexMlrval.GetIntValue()
if !ok {
if lowerIndexMlrval.IsVoid() {
lowerIndex = 1
} else {
return false, mlrval.ERROR, 0, 0
}
}
upperIndex, ok := upperIndexMlrval.GetIntValue()
if !ok {
if upperIndexMlrval.IsVoid() {
upperIndex = int64(n)
} else {
return false, mlrval.ERROR, 0, 0
}
}

// UnaliasArrayIndex returns a boolean second return value to indicate
// whether the index is in range. But here, for the slicing operation, we
// inspect the in-range-ness ourselves so we discard that 2nd return value.
lowerZindex, _ = mlrval.UnaliasArrayLengthIndex(n, int(lowerIndex))
upperZindex, _ = mlrval.UnaliasArrayLengthIndex(n, int(upperIndex))

if lowerZindex > upperZindex {
return true, nil, 0, 0
}

// Semantics: say x=[1,2,3,4,5]. Then x[3:10] is [3,4,5].
//
// Cases:
// [* * * * *] actual data
// [o o] 1. attempted indexing: lo, hi both out of bounds
// [o o o o o o ] 2. attempted indexing: hi in bounds, lo out
// [o o o o o o o o o o o o] 3. attempted indexing: lo, hi both out of bounds
// [o o o] 4. attempted indexing: lo, hi in bounds
// [o o o o o o ] 5. attempted indexing: lo in bounds, hi out
// [o o o o] 6. attempted indexing: lo, hi both out of bounds

if lowerZindex < 0 {
lowerZindex = 0
if lowerZindex > upperZindex {
return true, nil, 0, 0
}
}
if upperZindex > n-1 {
upperZindex = n - 1
if lowerZindex > upperZindex {
return true, nil, 0, 0
}
}

return false, nil, lowerZindex, upperZindex
}
58 changes: 5 additions & 53 deletions internal/pkg/dsl/cst/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,62 +173,14 @@ func (node *ArraySliceAccessNode) Evaluate(
}
n := len(array)

if lowerIndexMlrval.IsAbsent() {
return mlrval.ABSENT
}
if upperIndexMlrval.IsAbsent() {
return mlrval.ABSENT
}

lowerIndex, ok := lowerIndexMlrval.GetIntValue()
if !ok {
if lowerIndexMlrval.IsVoid() {
lowerIndex = 1
} else {
return mlrval.ERROR
}
}
upperIndex, ok := upperIndexMlrval.GetIntValue()
if !ok {
if upperIndexMlrval.IsVoid() {
upperIndex = int64(n)
} else {
return mlrval.ERROR
}
}
sliceIsEmpty, absentOrError, lowerZindex, upperZindex :=
bifs.MillerSliceAccess(lowerIndexMlrval, upperIndexMlrval, n)

// UnaliasArrayIndex returns a boolean second return value to indicate
// whether the index is in range. But here, for the slicing operation, we
// inspect the in-range-ness ourselves so we discard that 2nd return value.
lowerZindex, _ := mlrval.UnaliasArrayIndex(&array, int(lowerIndex))
upperZindex, _ := mlrval.UnaliasArrayIndex(&array, int(upperIndex))

if lowerZindex > upperZindex {
if sliceIsEmpty {
return mlrval.FromEmptyArray()
}

// Semantics: say x=[1,2,3,4,5]. Then x[3:10] is [3,4,5].
//
// Cases:
// [* * * * *] actual data
// [o o] 1. attempted indexing: lo, hi both out of bounds
// [o o o o o o ] 2. attempted indexing: hi in bounds, lo out
// [o o o o o o o o o o o o] 3. attempted indexing: lo, hi both out of bounds
// [o o o] 4. attempted indexing: lo, hi in bounds
// [o o o o o o ] 5. attempted indexing: lo in bounds, hi out
// [o o o o] 6. attempted indexing: lo, hi both out of bounds

if lowerZindex < 0 {
lowerZindex = 0
if lowerZindex > upperZindex {
return mlrval.FromEmptyArray()
}
}
if upperZindex > n-1 {
upperZindex = n - 1
if lowerZindex > upperZindex {
return mlrval.FromEmptyArray()
}
if absentOrError != nil {
return absentOrError
}

// Go slices have inclusive lower bound, exclusive upper bound.
Expand Down

0 comments on commit 7872753

Please sign in to comment.