Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use runes #299

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion field/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"strconv"
"strings"
"unicode/utf8"

"github.com/moov-io/iso8583/utils"
)
Expand Down Expand Up @@ -81,7 +82,9 @@ func (f *String) Pack() ([]byte, error) {
return nil, fmt.Errorf("failed to encode content: %w", err)
}

packedLength, err := f.spec.Pref.EncodeLength(f.spec.Length, len(data))
runeCount := utf8.RuneCount(data)

packedLength, err := f.spec.Pref.EncodeLength(f.spec.Length, runeCount)
if err != nil {
return nil, fmt.Errorf("failed to encode length: %w", err)
}
Expand Down
77 changes: 77 additions & 0 deletions field/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/moov-io/iso8583/encoding"
"github.com/moov-io/iso8583/padding"
"github.com/moov-io/iso8583/prefix"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -59,6 +60,82 @@ func TestStringField(t *testing.T) {
require.Equal(t, "hello", str.Value())
}

func TestStringWithUTF8Encoding(t *testing.T) {
spec := &Spec{
Length: 10,
Description: "Field",
Enc: encoding.EBCDIC1047,
Pref: prefix.EBCDIC1047.Fixed,
Pad: padding.Left(' '),
}
str := NewStringValue("hüllo")
str.SetSpec(spec)
packed, err := str.Pack()
require.NoError(t, err)

assert.Len(t, packed, 10)

str2 := NewString(spec)
_, err = str2.Unpack(packed)
require.NoError(t, err)

assert.Equal(t, "hüllo", str2.Value())
}

func TestStringWithNonUTF8Encoding(t *testing.T) {
spec := &Spec{
Length: 10,
Description: "Field",
Enc: encoding.EBCDIC1047,
Pref: prefix.EBCDIC1047.Fixed,
Pad: padding.Left(' '),
}
str := NewString(spec)

hullo := []byte{0x88, 0xDC, 0x93, 0x93, 0x96}
olluh := []byte{0x96, 0x93, 0x93, 0xDC, 0x88}

// SetBytes takes UTF-8 encoded bytes
str.SetBytes([]byte("hüllo"))
require.Equal(t, "hüllo", str.Value())

packed, err := str.Pack()
require.NoError(t, err)
require.Equal(t, append([]byte{0x40, 0x40, 0x40, 0x40, 0x40}, hullo...), packed)

length, err := str.Unpack(append([]byte{0x40, 0x40, 0x40, 0x40, 0x40}, olluh...))
require.NoError(t, err)
require.Equal(t, 10, length)

// Bytes returns the UTF-8 encoding of the value
b, err := str.Bytes()
require.NoError(t, err)
require.Equal(t, []byte("ollüh"), b)

require.Equal(t, "ollüh", str.Value())

str = NewString(spec)
str.Marshal(NewStringValue("hüllo"))
packed, err = str.Pack()
require.NoError(t, err)
require.Equal(t, append([]byte{0x40, 0x40, 0x40, 0x40, 0x40}, hullo...), packed)

str = NewString(spec)
length, err = str.Unpack(append([]byte{0x40, 0x40, 0x40, 0x40, 0x40}, olluh...))
require.NoError(t, err)
require.Equal(t, 10, length)
require.Equal(t, "ollüh", str.Value())

str = NewString(spec)
err = str.SetBytes([]byte("hüllo"))
require.NoError(t, err)
require.Equal(t, "hüllo", str.Value())

str = NewString(spec)
str.SetValue("hüllo")
require.Equal(t, "hüllo", str.Value())
}

func TestStringNil(t *testing.T) {
var str *String = nil

Expand Down
5 changes: 3 additions & 2 deletions padding/left.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ func NewLeftPadder(pad rune) Padder {
}

func (p *leftPadder) Pad(data []byte, length int) []byte {
if len(data) >= length {
runeCount := utf8.RuneCount(data)
if runeCount >= length {
return data
}

padding := bytes.Repeat(p.pad, length-len(data))
padding := bytes.Repeat(p.pad, length-runeCount)
return append(padding, data...)
}

Expand Down
Loading