From adb150d1fb801c032d62be69cb03ac60c158ecb9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 6 Apr 2026 08:45:00 +0000 Subject: [PATCH] S2-S4: tighten lexer condition, fix doc header, add Unicode example Agent-Logs-Url: https://github.com/mikefarah/yq/sessions/ec06083e-e20a-45d2-bf7e-4e1fa7be1073 Co-authored-by: mikefarah <1151925+mikefarah@users.noreply.github.com> --- pkg/yqlib/doc/operators/headers/slice-array.md | 2 +- pkg/yqlib/doc/operators/slice-array.md | 18 +++++++++++++++++- pkg/yqlib/lexer.go | 7 ++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/pkg/yqlib/doc/operators/headers/slice-array.md b/pkg/yqlib/doc/operators/headers/slice-array.md index 0113a585..448c38ea 100644 --- a/pkg/yqlib/doc/operators/headers/slice-array.md +++ b/pkg/yqlib/doc/operators/headers/slice-array.md @@ -1,4 +1,4 @@ -# Slice/Splice Array or String +# Slice Array or String The slice operator works on both arrays and strings. Like the `jq` equivalent, `.[10:15]` will return a subarray (or substring) of length 5, starting from index 10 inclusive, up to index 15 exclusive. Negative numbers count backwards from the end of the array or string. diff --git a/pkg/yqlib/doc/operators/slice-array.md b/pkg/yqlib/doc/operators/slice-array.md index aee24811..a7920406 100644 --- a/pkg/yqlib/doc/operators/slice-array.md +++ b/pkg/yqlib/doc/operators/slice-array.md @@ -1,4 +1,4 @@ -# Slice/Splice Array or String +# Slice Array or String The slice operator works on both arrays and strings. Like the `jq` equivalent, `.[10:15]` will return a subarray (or substring) of length 5, starting from index 10 inclusive, up to index 15 exclusive. Negative numbers count backwards from the end of the array or string. @@ -165,3 +165,19 @@ will output ralia ``` +## Slicing strings - Unicode +Indices are rune-based, so multibyte characters are handled correctly + +Given a sample.yml file of: +```yaml +greeting: héllo +``` +then +```bash +yq '.greeting[1:3]' sample.yml +``` +will output +```yaml +él +``` + diff --git a/pkg/yqlib/lexer.go b/pkg/yqlib/lexer.go index 25ef12ac..12e63e37 100644 --- a/pkg/yqlib/lexer.go +++ b/pkg/yqlib/lexer.go @@ -127,10 +127,15 @@ func handleToken(tokens []*token, index int, postProcessedTokens []*token) (toke if tokenIsOpType(currentToken, createMapOpType) { log.Debugf("tokenIsOpType: createMapOpType") // check the previous token is '[', means we are slice, but dont have a first number - if index > 0 && (tokens[index-1].TokenType == traverseArrayCollect || tokens[index-1].TokenType == openCollect) { + if index > 0 && tokens[index-1].TokenType == traverseArrayCollect { log.Debugf("previous token is : traverseArrayOpType") // need to put the number 0 before this token, as that is implied postProcessedTokens = append(postProcessedTokens, &token{TokenType: operationToken, Operation: createValueOperation(0, "0")}) + } else if index >= 2 && tokens[index-1].TokenType == openCollect && + (tokens[index-2].TokenType == operationToken || tokens[index-2].TokenType == closeCollect || tokens[index-2].TokenType == closeCollectObject) { + log.Debugf("previous token is : openCollect following a traversal, implying 0 start") + // need to put the number 0 before this token, as that is implied + postProcessedTokens = append(postProcessedTokens, &token{TokenType: operationToken, Operation: createValueOperation(0, "0")}) } }