Skip to content
Merged
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
14 changes: 7 additions & 7 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// Unmarshal parses an ASCII table into a slice of the specified type.
func Unmarshal[E any](asciiTable string, v E) ([]string, []E, error) {
var delimitter = "*+|"
var delimiter = "*+|"
var skip = []string{"---"}
lines := strings.Split(strings.TrimSpace(asciiTable), "\n")
if len(lines) < 4 {
Expand All @@ -27,11 +27,11 @@ func Unmarshal[E any](asciiTable string, v E) ([]string, []E, error) {
}

for i, line := range lines[0:defaultLines] {
headerLine := strings.Trim(line, delimitter+" ")
headerLine := strings.Trim(line, delimiter+" ")
if skipLine(headerLine, skip) {
continue
}
headers = splitRow(headerLine, delimitter)
headers = splitRow(headerLine, delimiter)
if len(headers) > 1 {
headerLineNumber = i
break
Expand All @@ -51,7 +51,7 @@ func Unmarshal[E any](asciiTable string, v E) ([]string, []E, error) {
continue
}

row := splitRow(strings.Trim(line, delimitter+" "), delimitter)
row := splitRow(strings.Trim(line, delimiter+" "), delimiter)
if len(row) != len(headers) {
return nil, nil, fmt.Errorf("row length does not match header length: %v", row)
}
Expand Down Expand Up @@ -79,10 +79,10 @@ func Unmarshal[E any](asciiTable string, v E) ([]string, []E, error) {
return headers, results, nil
}

// Helper function to split a row by the first character in the delimitter string and trim whitespace.
func splitRow(row string, delimitter string) []string {
// Helper function to split a row by the first character in the delimiter string and trim whitespace.
func splitRow(row string, delimiter string) []string {
var cells []string
for _, char := range strings.Split(delimitter, "") {
for _, char := range strings.Split(delimiter, "") {
cells = strings.Split(row, char)
for i := range cells {
cells[i] = strings.TrimSpace(cells[i])
Expand Down
Loading