Skip to content

Commit

Permalink
PLY now handles carriage returns
Browse files Browse the repository at this point in the history
  • Loading branch information
EliCDavis committed Dec 26, 2024
1 parent c4f4765 commit 4370a99
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
18 changes: 18 additions & 0 deletions formats/ply/format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ply_test

import (
"testing"

"github.com/EliCDavis/polyform/formats/ply"
"github.com/stretchr/testify/assert"
)

func TestFormat_String(t *testing.T) {
assert.Equal(t, "ASCII", ply.ASCII.String())
assert.Equal(t, "Binary Big Endian", ply.BinaryBigEndian.String())
assert.Equal(t, "Binary Little Endian", ply.BinaryLittleEndian.String())

assert.PanicsWithError(t, "unrecognized format Test", func() {
ply.Format("Test").String()
})
}
16 changes: 16 additions & 0 deletions formats/ply/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ply_test

import (
"bytes"
"strings"
"testing"

"github.com/EliCDavis/polyform/formats/ply"
Expand Down Expand Up @@ -165,3 +166,18 @@ end_header
})
}
}

func TestHeader_ReadCarriageReturns(t *testing.T) {
// ARRANGE ================================================================
headerString := "ply\r\nformat ascii 1.0\r\nend_header\n"

// ACT ====================================================================
header, err := ply.ReadHeader(strings.NewReader(headerString))

// ASSERT =================================================================
assert.NoError(t, err)
assert.Equal(t, ply.ASCII, header.Format)
assert.Len(t, header.Comments, 0)
assert.Len(t, header.ObjInfo, 0)
assert.Len(t, header.Elements, 0)
}
9 changes: 4 additions & 5 deletions formats/ply/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func readLine(in io.Reader) (string, error) {
data := make([]byte, 0)
data := strings.Builder{}

buf := make([]byte, 1)
var err error
Expand All @@ -26,12 +26,11 @@ func readLine(in io.Reader) (string, error) {
return "", err
}

b := buf[0]
if b == '\n' {
return string(data), nil
if buf[0] == '\n' || buf[0] == '\r' {
return data.String(), nil
}

data = append(data, b)
data.WriteByte(buf[0])
}
}

Expand Down
13 changes: 10 additions & 3 deletions formats/ply/reader_list_ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ import (

type listAsciiPropertyReader struct {
property ListProperty
lastReadListSize int64
lastReadListSize int32
buf []string
}

func (lpr *listAsciiPropertyReader) Read(line []string) (offset int, err error) {
lpr.lastReadListSize, err = strconv.ParseInt(line[0], 10, 64)
v, err := strconv.ParseInt(line[0], 10, 32)
if err != nil {
return -1, err
}
lpr.lastReadListSize = int32(v)

// Resize to fit contents
if len(lpr.buf) < int(lpr.lastReadListSize) {
lpr.buf = make([]string, lpr.lastReadListSize)
}

copy(lpr.buf, line[1:lpr.lastReadListSize+1])
return int(lpr.lastReadListSize) + 1, err
}
Expand All @@ -42,7 +49,7 @@ func (lpr listAsciiPropertyReader) Int(out []int) error {
}

for i := 0; i < int(lpr.lastReadListSize); i++ {
v, err := strconv.ParseInt(lpr.buf[i], 10, 64)
v, err := strconv.ParseInt(lpr.buf[i], 10, 32)
if err != nil {
return err
}
Expand Down

0 comments on commit 4370a99

Please sign in to comment.