Skip to content

Commit 1a4e25c

Browse files
committed
Fix split() to have same behavior for single-char FS
Fix case of empty input line (NF should be 0)
1 parent 3fba858 commit 1a4e25c

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

interp/functions.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strings"
1616
"syscall"
1717
"time"
18+
"unicode/utf8"
1819

1920
. "github.com/benhoyt/goawk/internal/ast"
2021
. "github.com/benhoyt/goawk/lexer"
@@ -540,7 +541,11 @@ func (p *interp) split(s string, scope VarScope, index int, fs string) (int, err
540541
var parts []string
541542
if fs == " " {
542543
parts = strings.Fields(s)
543-
} else if s != "" {
544+
} else if s == "" {
545+
// NF should be 0 on empty line
546+
} else if utf8.RuneCountInString(fs) <= 1 {
547+
parts = strings.Split(s, fs)
548+
} else {
544549
re, err := p.compileRegex(fs)
545550
if err != nil {
546551
return 0, err

interp/io.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,11 @@ func (p *interp) ensureFields() {
291291
if p.fieldSep == " " {
292292
// FS space (default) means split fields on any whitespace
293293
p.fields = strings.Fields(p.line)
294+
} else if p.line == "" {
295+
p.fields = nil
294296
} else if utf8.RuneCountInString(p.fieldSep) <= 1 {
295297
// 1-char FS is handled as plain split (not regex)
296298
p.fields = strings.Split(p.line, p.fieldSep)
297-
} else if p.line == "" {
298-
p.fields = nil
299299
} else {
300300
// Split on FS as a regex
301301
p.fields = p.fieldSepRegex.Split(p.line, -1)

0 commit comments

Comments
 (0)