diff --git a/go/reg-test/cases/case-slice.sh b/go/reg-test/cases/case-slice.sh index b22358799..a10ed7dd6 100644 --- a/go/reg-test/cases/case-slice.sh +++ b/go/reg-test/cases/case-slice.sh @@ -1,4 +1,4 @@ -mlr -n --ojson put ' +run_mlr -n --ojson put ' end { a = [1,2,3,4,5,6,7]; m = {"a": 1, "b": 2}; diff --git a/go/reg-test/expected/case-go-dsl-substr.sh.out b/go/reg-test/expected/case-go-dsl-substr.sh.out index d08d1b2ae..332406b7b 100644 --- a/go/reg-test/expected/case-go-dsl-substr.sh.out +++ b/go/reg-test/expected/case-go-dsl-substr.sh.out @@ -21,7 +21,7 @@ mlr put $y = substr($x, 3, 3) x=abcdefg,y=c mlr put $y = substr($x, 4, 3) -x=abcdefg,y= +x=abcdefg,y=(error) mlr put $y = substr($x, 2, 5) x=1234567,y=2345 diff --git a/go/reg-test/expected/case-slice.sh.out b/go/reg-test/expected/case-slice.sh.out index 8b1378917..c5edb844d 100644 --- a/go/reg-test/expected/case-slice.sh.out +++ b/go/reg-test/expected/case-slice.sh.out @@ -1 +1,73 @@ +mlr -n --ojson put + end { + a = [1,2,3,4,5,6,7]; + m = {"a": 1, "b": 2}; + s = "abcdefg"; + + emit { + "a35": a[3:5], + "a37": a[3:7], + "a17": a[1:7], + "a07": a[0:7], + "a39": a[3:9], + "a53": a[5:3], + "a93": a[9:3], + }; + + emit { + "m11": m[1:1], + }; + + emit { + "s35": s[3:5], + "s37": s[3:7], + "s17": s[1:7], + "s07": s[0:7], + "s39": s[3:9], + "s53": s[5:3], + "s93": s[9:3], + }; + + emit { + "u35": substr(s,3,5), + "u37": substr(s,3,7), + "u17": substr(s,1,7), + "u07": substr(s,0,7), + "u39": substr(s,3,9), + "u53": substr(s,5,3), + "u93": substr(s,9,3), + }; + } + +{ + "a35": [3, 4, 5], + "a37": [3, 4, 5, 6, 7], + "a17": [1, 2, 3, 4, 5, 6, 7], + "a07": (error), + "a39": (error), + "a53": [], + "a93": (error) +} +{ + "m11": (error) +} +{ + "s35": "cde", + "s37": "cdefg", + "s17": "abcdefg", + "s07": "", + "s39": "", + "s53": (error), + "s93": "" +} +{ + "u35": "cde", + "u37": "cdefg", + "u17": "abcdefg", + "u07": "", + "u39": "", + "u53": (error), + "u93": "" +} + diff --git a/go/src/miller/dsl/cst/collections.go b/go/src/miller/dsl/cst/collections.go index 96660329a..c1d15a805 100644 --- a/go/src/miller/dsl/cst/collections.go +++ b/go/src/miller/dsl/cst/collections.go @@ -155,11 +155,19 @@ func (this *ArraySliceAccessNode) Evaluate(state *State) types.Mlrval { lowerIndex, ok := lowerIndexMlrval.GetIntValue() if !ok { - return types.MlrvalFromError() + if lowerIndexMlrval.IsEmpty() { + lowerIndex = 1 + } else { + return types.MlrvalFromError() + } } upperIndex, ok := upperIndexMlrval.GetIntValue() if !ok { - return types.MlrvalFromError() + if upperIndexMlrval.IsEmpty() { + upperIndex = int64(len(array)) + } else { + return types.MlrvalFromError() + } } lowerZindex, ok := types.UnaliasArrayIndex(&array, lowerIndex) diff --git a/go/todo.txt b/go/todo.txt index 51537112d..f0da77958 100644 --- a/go/todo.txt +++ b/go/todo.txt @@ -5,6 +5,7 @@ small: * justWroteEmptyLine -> pprint too ... ! nidx @ 10-min ! text-put @ 10-min +! slice: empty lower/higher medium: ! gating after: 'int x = 1; x = "abc"' @@ -377,3 +378,4 @@ NITS/NON-IMMEDIATE: o .... o put '$array = [1,2,3,[4,5]]' is a syntax error unfortunately; need '$array = [1,2,3,[4,5] ]' * doc: string index/slice access on RHS +* substr(7,5) -- "" or error?