From 0683ded364790dbef17dd7b4ba5ad51408671793 Mon Sep 17 00:00:00 2001
From: Thomas Voss <mail@thomasvoss.com>
Date: Tue, 13 Aug 2024 01:01:43 +0200
Subject: Make the autoformatter even better

---
 cmd/mfmt/main.go      | 51 ++++++++++++++++++++++++++++++--------
 cmd/mfmt/main_test.go | 68 +++++++++++++++++++++++++--------------------------
 2 files changed, 75 insertions(+), 44 deletions(-)

(limited to 'cmd')

diff --git a/cmd/mfmt/main.go b/cmd/mfmt/main.go
index 7f7ec8b..f2aa24f 100644
--- a/cmd/mfmt/main.go
+++ b/cmd/mfmt/main.go
@@ -15,16 +15,14 @@ import (
 	"strings"
 )
 
-const (
-	ws         = " \t"
-	longestNum = len("1.000.000.000")
-)
+const ws = " \t"
 
 var (
 	rv int
 
 	reMintageYear = regexp.MustCompile(`^\d{4}(-[^ \t]+)?`)
-	reMintageRow  = regexp.MustCompile(`^(([0-9.]+|\?)[ \t]+)*([0-9.]+|\?)$`)
+	reMintageRowC = regexp.MustCompile(`^[ \t]*(([0-9.]+|\?)[ \t]+){2}([0-9.]+|\?)$`)
+	reMintageRowS = regexp.MustCompile(`^[ \t]*(([0-9.]+|\?)[ \t]+){7}([0-9.]+|\?)$`)
 )
 
 func main() {
@@ -44,6 +42,12 @@ func main() {
 }
 
 func mfmt(file string, r io.Reader, w io.Writer) {
+	var (
+		buf         [3]string
+		bufsz       int
+		longestNums [8]int
+	)
+
 	scanner := bufio.NewScanner(r)
 	for linenr := 1; scanner.Scan(); linenr++ {
 		line := strings.Trim(scanner.Text(), ws)
@@ -53,8 +57,25 @@ func mfmt(file string, r io.Reader, w io.Writer) {
 			fmt.Fprintln(w, line)
 		case reMintageYear.MatchString(line):
 			fmtMintageYear(line, w)
-		case reMintageRow.MatchString(line):
-			fmtMintageRow(line, w)
+		case reMintageRowS.MatchString(line):
+			switch bufsz {
+			case len(buf):
+				bufsz = 0
+				clear(longestNums[:])
+				fallthrough
+			default:
+				setLongestNum(longestNums[:], line)
+				buf[bufsz] = line
+				if bufsz++; bufsz == len(buf) {
+					fmtMintageRow(buf[0], longestNums[:], w)
+					fmtMintageRow(buf[1], longestNums[:], w)
+					fmtMintageRow(buf[2], longestNums[:], w)
+				}
+			}
+		case reMintageRowC.MatchString(line):
+			var ns [3]int
+			setLongestNum(ns[:], line)
+			fmtMintageRow(line, ns[:], w)
 		default:
 			warn(fmt.Sprintf("%s:%d: potential syntax error", file, linenr))
 			fmt.Fprintln(w, line)
@@ -62,6 +83,16 @@ func mfmt(file string, r io.Reader, w io.Writer) {
 	}
 }
 
+func setLongestNum(longest []int, line string) {
+	for i, x := range strings.FieldsFunc(line, func(r rune) bool {
+		return strings.ContainsRune(ws, r)
+	}) {
+		n := len(strings.ReplaceAll(x, ".", ""))
+		n += (n - 1) / 3 // Thousands separators
+		longest[i] = max(longest[i], n)
+	}
+}
+
 func fmtMintageYear(line string, w io.Writer) {
 	switch i := strings.IndexAny(line, ws); i {
 	case -1:
@@ -71,7 +102,7 @@ func fmtMintageYear(line string, w io.Writer) {
 	}
 }
 
-func fmtMintageRow(line string, w io.Writer) {
+func fmtMintageRow(line string, longest []int, w io.Writer) {
 	tokens := strings.FieldsFunc(line, func(r rune) bool {
 		return strings.ContainsRune(ws, r)
 	})
@@ -80,9 +111,9 @@ func fmtMintageRow(line string, w io.Writer) {
 		s := formatMintage(tok)
 
 		if i == 0 {
-			fmt.Fprintf(w, "\t%*s", longestNum, s)
+			fmt.Fprintf(w, "\t%*s", longest[i], s)
 		} else {
-			fmt.Fprintf(w, "%*s", longestNum+1, s)
+			fmt.Fprintf(w, "%*s", longest[i]+1, s)
 		}
 	}
 
diff --git a/cmd/mfmt/main_test.go b/cmd/mfmt/main_test.go
index 7366ae0..ef91473 100644
--- a/cmd/mfmt/main_test.go
+++ b/cmd/mfmt/main_test.go
@@ -71,63 +71,63 @@ func TestComplete(t *testing.T) {
 `
 	out := `
 2014
-	       60.000        60.000       860.000       860.000       860.000       340.000       511.843       360.000
-	       70.000        70.000        70.000        70.000        70.000        70.000        70.000        70.000
-	        3.000         3.000         3.000         3.000         3.000         3.000         3.000         3.000
+	60.000 60.000 860.000 860.000 860.000 340.000 511.843 360.000
+	70.000 70.000  70.000  70.000  70.000  70.000  70.000  70.000
+	 3.000  3.000   3.000   3.000   3.000   3.000   3.000   3.000
 
 # Ref: https://example.com
 2015
-	            0             0             0             0             0             0             0     1.072.400
-	       40.000        40.000        40.000        40.000        40.000        40.000        40.000        40.000
-	            ?             ?             ?             ?             ?             ?             ?             ?
+	     0      0      0      0      0      0      0 1.072.400
+	40.000 40.000 40.000 40.000 40.000 40.000 40.000    40.000
+	     ?      ?      ?      ?      ?      ?      ?         ?
 
 2016
-	            0             0             0             0             0             0     2.339.200             0
-	       35.000        35.000        35.000        35.000        35.000        35.000        35.000        35.000
-	            ?             ?             ?             ?             ?             ?             ?             ?
+	     0      0      0      0      0      0 2.339.200      0
+	35.000 35.000 35.000 35.000 35.000 35.000    35.000 35.000
+	     ?      ?      ?      ?      ?      ?         ?      ?
 
 2017
-	    2.582.395     1.515.000     2.191.421     1.103.000     1.213.000       968.800        17.000       794.588
-	       22.000        22.000        22.000        22.000        22.000        22.000        22.000        22.000
-	            ?             ?             ?             ?             ?             ?             ?             ?
+	2.582.395 1.515.000 2.191.421 1.103.000 1.213.000 968.800 17.000 794.588
+	   22.000    22.000    22.000    22.000    22.000  22.000 22.000  22.000
+	        ?         ?         ?         ?         ?       ?      ?       ?
 
 2018
-	    2.430.000     2.550.000     1.800.000       980.000     1.014.000       890.000             0       868.000
-	       20.000        20.000        20.000        20.000        20.000        20.000        20.000        20.000
-	            ?             ?             ?             ?             ?             ?             ?             ?
+	2.430.000 2.550.000 1.800.000 980.000 1.014.000 890.000      0 868.000
+	   20.000    20.000    20.000  20.000    20.000  20.000 20.000  20.000
+	        ?         ?         ?       ?         ?       ?      ?       ?
 
 2019
-	    2.447.000     1.727.000     2.100.000     1.610.000     1.570.000       930.000             0     1.058.310
-	       15.000        15.000        15.000        15.000        15.000        15.000        15.000        15.000
-	            ?             ?             ?             ?             ?             ?             ?             ?
+	2.447.000 1.727.000 2.100.000 1.610.000 1.570.000 930.000      0 1.058.310
+	   15.000    15.000    15.000    15.000    15.000  15.000 15.000    15.000
+	        ?         ?         ?         ?         ?       ?      ?         ?
 
 2020
-	            0             0             0       860.000       175.000       740.000             0     1.500.000
-	       12.000        12.000        12.000        12.000        12.000        12.000        12.000        12.000
-	            ?             ?             ?             ?             ?             ?             ?             ?
+	     0      0      0 860.000 175.000 740.000      0 1.500.000
+	12.000 12.000 12.000  12.000  12.000  12.000 12.000    12.000
+	     ?      ?      ?       ?       ?       ?      ?         ?
 
 2021
-	      200.000       700.000             0     1.400.000     1.420.000       600.000        50.000     1.474.500
-	       10.500        10.500        10.500        10.500        10.500        10.500        10.500        10.500
-	            ?             ?             ?             ?             ?             ?             ?             ?
+	200.000 700.000      0 1.400.000 1.420.000 600.000 50.000 1.474.500
+	 10.500  10.500 10.500    10.500    10.500  10.500 10.500    10.500
+	      ?       ?      ?         ?         ?       ?      ?         ?
 
 2022
-	      700.000       450.000       400.000       700.000       700.000       380.000             0     1.708.000
-	       10.500        10.500        10.500        10.500        10.500        10.500        10.500        10.500
-	            ?             ?             ?             ?             ?             ?             ?             ?
+	700.000 450.000 400.000 700.000 700.000 380.000      0 1.708.000
+	 10.500  10.500  10.500  10.500  10.500  10.500 10.500    10.500
+	      ?       ?       ?       ?       ?       ?      ?         ?
 
 2023
-	            0             0             0             0             0             0             0     2.075.250
-	       10.500        10.500        10.500        10.500        10.500        10.500        10.500        10.500
-	            ?             ?             ?             ?             ?             ?             ?             ?
+	     0      0      0      0      0      0      0 2.075.250
+	10.500 10.500 10.500 10.500 10.500 10.500 10.500    10.500
+	     ?      ?      ?      ?      ?      ?      ?         ?
 
 2024
-	            ?             ?             ?             ?             ?             ?             ?             ?
-	            ?             ?             ?             ?             ?             ?             ?             ?
-	            ?             ?             ?             ?             ?             ?             ?             ?
+	? ? ? ? ? ? ? ?
+	? ? ? ? ? ? ? ?
+	? ? ? ? ? ? ? ?
 
 2014 "Council of Europe"
-	            ?             ?             ?
+	? ? ?
 `
 
 	r := bytes.NewReader([]byte(in))
-- 
cgit v1.2.3