From ba0d19f4e82a90783c4c7ae052cfd1b45e106aac Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 9 Aug 2024 16:21:57 +0200 Subject: Refactor the mintage parser --- mintages/parser.go | 46 +++++++++++++++++++++----------------------- mintages/parser_test.go | 51 ++++++++++++++++--------------------------------- 2 files changed, 38 insertions(+), 59 deletions(-) diff --git a/mintages/parser.go b/mintages/parser.go index 87b9d19..f93fdf4 100644 --- a/mintages/parser.go +++ b/mintages/parser.go @@ -8,7 +8,6 @@ import ( "path/filepath" "strconv" "strings" - "time" "unicode" ) @@ -53,7 +52,10 @@ func parse(reader io.Reader, file string) (Data, error) { scanner := bufio.NewScanner(reader) for linenr := 1; scanner.Scan(); linenr++ { - var mintmark string + var mintmark struct { + s string + star bool + } line := scanner.Text() tokens := strings.FieldsFunc(strings.TrimSpace(line), unicode.IsSpace) @@ -94,7 +96,13 @@ func parse(reader io.Reader, file string) (Data, error) { year = data.StartYear - 1 case isLabel(tokens[0]): - mintmark = tokens[0][:len(tokens[0])-1] + n := len(tokens[0]) + if n > 2 && tokens[0][n-2] == '*' { + mintmark.star = true + mintmark.s = tokens[0][:n-2] + } else { + mintmark.s = tokens[0][:n-1] + } tokens = tokens[1:] if !isNumeric(tokens[0], true) && tokens[0] != "?" { return Data{}, SyntaxError{ @@ -141,15 +149,14 @@ func parse(reader io.Reader, file string) (Data, error) { var row Row switch { - case mintmark == "": + case mintmark.s == "": year += 1 row.Label = strconv.Itoa(year) - case mintmark[len(mintmark)-1] == '*': + case mintmark.star: year += 1 - mintmark = mintmark[:len(mintmark)-1] fallthrough default: - row.Label = fmt.Sprintf("%d %s", year, mintmark) + row.Label = fmt.Sprintf("%d %s", year, mintmark.s) } for i, tok := range tokens { @@ -170,22 +177,6 @@ func parse(reader io.Reader, file string) (Data, error) { } } - /* Pad rows of ‘unknown’ mintages at the end of each set of mintages - for each year that we haven’t filled in info for. This avoids - things accidentally breaking if the new year comes and we forget - to add extra rows. */ - for _, ms := range [...]*[]Row{&data.Circ, &data.BU, &data.Proof} { - finalYear := len(*ms) + data.StartYear - 1 - missing := time.Now().Year() - finalYear - for i := 0; i < missing; i++ { - label := strconv.Itoa(finalYear + i + 1) - *ms = append(*ms, Row{ - Label: label, - Cols: [8]int{-1, -1, -1, -1, -1, -1, -1, -1}, - }) - } - } - return data, nil } @@ -205,7 +196,14 @@ func isNumeric(s string, dot bool) bool { } func isLabel(s string) bool { - return s[len(s)-1] == ':' && len(s) > 1 + n := len(s) + switch { + case len(s) > 2 && s[n-1] == ':' && s[n-2] == '*', + len(s) > 1 && s[n-1] == ':': + return true + default: + return false + } } func atoiWithDots(s string) int { diff --git a/mintages/parser_test.go b/mintages/parser_test.go index b2e4ef9..5d70722 100644 --- a/mintages/parser_test.go +++ b/mintages/parser_test.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "testing" - "time" ) func TestParserComplete(t *testing.T) { @@ -29,17 +28,12 @@ func TestParserComplete(t *testing.T) { data.StartYear) } - /* The following 3 loops assert that we have correct mintages, - including padding mintages. After the loops we assert that the - number of padding mintages is actually correct. */ - for i, row := range data.Circ { for j, col := range row.Cols { var n int - switch { - case i == 1 && j == 1, i >= 2: + if i == 1 && j == 1 { n = -1 - default: + } else { n = 1000*i + j + 1000 } if col != n { @@ -51,10 +45,9 @@ func TestParserComplete(t *testing.T) { for i, row := range data.BU { for j, col := range row.Cols { var n int - switch { - case i == 1 && j == 1, i >= 2: + if i == 1 && j == 1 { n = -1 - default: + } else { n = 1000*i + j + 1100 } if col != n { @@ -66,10 +59,9 @@ func TestParserComplete(t *testing.T) { for i, row := range data.Proof { for j, col := range row.Cols { var n int - switch { - case i == 1 && j == 1, i >= 2: + if i == 1 && j == 1 { n = -1 - default: + } else { n = 1000*i + j + 1200 } if col != n { @@ -78,15 +70,14 @@ func TestParserComplete(t *testing.T) { } } - rowsWant := time.Now().Year() - data.StartYear + 1 - if len(data.Circ) != rowsWant { - t.Fatalf("Expected len(data.Circ)=%d; got %d", rowsWant, len(data.Circ)) + if len(data.Circ) != 2 { + t.Fatalf("Expected len(data.Circ)=2; got %d", len(data.Circ)) } - if len(data.BU) != rowsWant { - t.Fatalf("Expected len(data.BU)=%d; got %d", rowsWant, len(data.BU)) + if len(data.BU) != 2 { + t.Fatalf("Expected len(data.BU)=2; got %d", len(data.BU)) } - if len(data.Proof) != rowsWant { - t.Fatalf("Expected len(data.Proof)=%d; got %d", rowsWant, len(data.Proof)) + if len(data.Proof) != 2 { + t.Fatalf("Expected len(data.Proof)=2; got %d", len(data.Proof)) } } @@ -105,17 +96,8 @@ func TestParserNoProof(t *testing.T) { t.Fatalf(`Expected err=nil; got "%s"`, err) } - for _, row := range data.Proof { - for _, col := range row.Cols { - if col != -1 { - t.Fatalf("Expected data.Proof[i][j]=-1; got %d", col) - } - } - } - - rowsWant := time.Now().Year() - data.StartYear + 1 - if len(data.Proof) != rowsWant { - t.Fatalf("Expected len(data.Proof)=%d; got %d", rowsWant, len(data.Proof)) + if len(data.Proof) != 0 { + t.Fatalf("Expected len(data.Proof)=0; got %d", len(data.Proof)) } } @@ -135,10 +117,9 @@ func TestParserMintmarks(t *testing.T) { for i, row := range data.Circ { for j, col := range row.Cols { var n int - switch { - case i > 0 && j == 1, i >= 3: + if i > 0 && j == 1 { n = -1 - default: + } else { n = 1000*i + j + 1000 } if col != n { -- cgit v1.2.3