Skip to content

Commit

Permalink
fix: truncate utf8 strings correctly
Browse files Browse the repository at this point in the history
Fixes: #1510
  • Loading branch information
adamdecaf committed Nov 21, 2024
1 parent 1b5f7ab commit c836575
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
13 changes: 7 additions & 6 deletions converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ func (c *converters) parseNumField(r string) (s int) {
}

func (c *converters) parseStringField(r string) (s string) {
s = strings.TrimSpace(r)
return s
return strings.TrimSpace(r)
}

func (c *converters) parseStringFieldWithOpts(r string, opts *ValidateOpts) string {
Expand Down Expand Up @@ -90,8 +89,9 @@ func (c *converters) alphaField(s string, max uint) string {
}

ln := uint(count)
if ln > max {
return s[:max]
if ln > max && len(s) >= count {
// Find which index corresponds to the max characters allowed
return string([]rune(s)[:max])
}

m := int(max) - int(ln) //nolint:gosec
Expand Down Expand Up @@ -150,8 +150,9 @@ func (c *converters) stringField(s string, max uint) string {
}

ln := uint(count)
if ln > max {
return s[:max]
if ln > max && len(s) >= count {
// Find which index corresponds to the max characters allowed
return string([]rune(s)[:max])
}

m := int(max) - int(ln) //nolint:gosec
Expand Down
13 changes: 13 additions & 0 deletions converters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,16 @@ func TestLeastSignificantDigits(t *testing.T) {
}
}
}

func TestConvertors_UTF8Truncation(t *testing.T) {
c := converters{}

require.Equal(t, "John Doe ", c.alphaField("John Doe", 15))
require.Equal(t, "0000000John Doe", c.stringField("John Doe", 15))

require.Equal(t, "Testée Samples ", c.alphaField("Testée Samples", 15))
require.Equal(t, "0Testée Samples", c.stringField("Testée Samples", 15))

require.Equal(t, "Testée Samples0", c.alphaField("Testée Samples01", 15))
require.Equal(t, "Testée Samples0", c.stringField("Testée Samples01", 15))
}
10 changes: 10 additions & 0 deletions entryDetail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"testing"

"github.com/moov-io/base"

"github.com/stretchr/testify/require"
)

// mockEntryDetail creates an entry detail
Expand Down Expand Up @@ -766,3 +768,11 @@ func TestEntryDetail__InvalidCheckDigitNotAllowedWithNullValidateOpt(t *testing.
ed = nil
ed.SetValidation(&ValidateOpts{})
}

func TestEntryDetail_UTF8Truncation(t *testing.T) {
ed := mockEntryDetail()
ed.IdentificationNumber = "Testée Samples01"

require.Equal(t, "Testée Samples0", ed.IdentificationNumberField())
require.Equal(t, "Testée Samples01", ed.IdentificationNumber)
}
5 changes: 4 additions & 1 deletion reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,9 +988,12 @@ func testFileBatchControlValidate(t testing.TB) {
bh := mockBatchHeader()
ed := mockEntryDetail()
bc := mockBatchControl()
bc.CompanyIdentification = "B1G C0MP®NY"
bc.CompanyIdentification = "B1G C0MPANY"

line := bh.String() + "\n" + ed.String() + "\n" + bc.String()

r := NewReader(strings.NewReader(line))

_, err := r.Read()
if !base.Has(err, NewErrBatchHeaderControlEquality(220, 200)) {
t.Errorf("%T: %s", err, err)
Expand Down

0 comments on commit c836575

Please sign in to comment.