Skip to content

Commit

Permalink
fix(plc4go/spi): fix issue with UTF16 encodings cpu drain
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Apr 27, 2023
1 parent 96b338e commit d28e704
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion plc4go/spi/utils/WriteBufferByteBased.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (wb *byteWriteBuffer) WriteString(_ string, bitLength uint32, encoding stri
// TODO: make this a writer arg
var nonAlphanumericRegex = regexp.MustCompile(`[^A-Z0-9]+`)
encoding = nonAlphanumericRegex.ReplaceAllLiteralString(strings.ToUpper(encoding), "")
remainingBits := bitLength
remainingBits := int64(bitLength) // we use int64 otherwise the subtraction below flips
// TODO: the implementation completely ignores encoding for now. Fix this
switch encoding {
case "UTF8":
Expand Down
23 changes: 14 additions & 9 deletions plc4go/spi/utils/WriteBufferByteBased_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,8 @@ func Test_byteWriteBuffer_WriteString(t *testing.T) {
writer: bitio.NewWriter(new(bytes.Buffer)),
},
args: args{
value: "plc4x",
bitLength: 48,
value: "plc4x",
},
wantErr: assert.NoError,
},
Expand All @@ -937,8 +938,9 @@ func Test_byteWriteBuffer_WriteString(t *testing.T) {
writer: bitio.NewWriter(new(bytes.Buffer)),
},
args: args{
encoding: "UTF8",
value: "plc4x",
bitLength: 48,
encoding: "UTF8",
value: "plc4x",
},
wantErr: assert.NoError,
},
Expand All @@ -948,8 +950,9 @@ func Test_byteWriteBuffer_WriteString(t *testing.T) {
writer: bitio.NewWriter(new(bytes.Buffer)),
},
args: args{
encoding: "UTF16",
value: "plc4x",
bitLength: 48,
encoding: "UTF16",
value: "plc4x",
},
wantErr: assert.NoError,
},
Expand All @@ -959,8 +962,9 @@ func Test_byteWriteBuffer_WriteString(t *testing.T) {
writer: bitio.NewWriter(new(bytes.Buffer)),
},
args: args{
encoding: "UTF16BE",
value: "plc4x",
bitLength: 48,
encoding: "UTF16BE",
value: "plc4x",
},
wantErr: assert.NoError,
},
Expand All @@ -970,8 +974,9 @@ func Test_byteWriteBuffer_WriteString(t *testing.T) {
writer: bitio.NewWriter(new(bytes.Buffer)),
},
args: args{
encoding: "UTF16LE",
value: "plc4x",
bitLength: 48,
encoding: "UTF16LE",
value: "plc4x",
},
wantErr: assert.NoError,
},
Expand Down

0 comments on commit d28e704

Please sign in to comment.