Skip to content

Commit

Permalink
Fix split() to have same behavior for single-char FS
Browse files Browse the repository at this point in the history
Fix case of empty input line (NF should be 0)
  • Loading branch information
benhoyt committed May 27, 2019
1 parent 3fba858 commit 1a4e25c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion interp/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"
"syscall"
"time"
"unicode/utf8"

. "github.com/benhoyt/goawk/internal/ast"
. "github.com/benhoyt/goawk/lexer"
Expand Down Expand Up @@ -540,7 +541,11 @@ func (p *interp) split(s string, scope VarScope, index int, fs string) (int, err
var parts []string
if fs == " " {
parts = strings.Fields(s)
} else if s != "" {
} else if s == "" {
// NF should be 0 on empty line
} else if utf8.RuneCountInString(fs) <= 1 {
parts = strings.Split(s, fs)
} else {
re, err := p.compileRegex(fs)
if err != nil {
return 0, err
Expand Down
4 changes: 2 additions & 2 deletions interp/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,11 @@ func (p *interp) ensureFields() {
if p.fieldSep == " " {
// FS space (default) means split fields on any whitespace
p.fields = strings.Fields(p.line)
} else if p.line == "" {
p.fields = nil
} else if utf8.RuneCountInString(p.fieldSep) <= 1 {
// 1-char FS is handled as plain split (not regex)
p.fields = strings.Split(p.line, p.fieldSep)
} else if p.line == "" {
p.fields = nil
} else {
// Split on FS as a regex
p.fields = p.fieldSepRegex.Split(p.line, -1)
Expand Down

0 comments on commit 1a4e25c

Please sign in to comment.