~mna/siberian

914f2adea0750e69d4676aa44f216f862faf699f — Martin Angers 4 years ago fc2b97a
fix printing ascii table when last char is allowed
2 files changed, 26 insertions(+), 11 deletions(-)

M ebnf.go
M ebnf_test.go
M ebnf.go => ebnf.go +24 -11
@@ 46,7 46,12 @@ func (t *asciiTable) allowed(ch byte) bool {
}

func (t asciiTable) GoString() string {
	return fmt.Sprintf("...6.........5.........4.........3.........2.........1.........0\n%064b\n.......2.........1.........0.........9.........8.........7......\n%064b", t[0], t[1])
	return strings.TrimSpace(fmt.Sprintf(`
...6.........5.........4.........3.........2.........1.........0
%064b
.......2.........1.........0.........9.........8.........7......
%064b
`, t[0], t[1]))
}

func (t *asciiTable) printable(b byte) string {


@@ 61,6 66,19 @@ func (t *asciiTable) String() string {
	buf.WriteByte('[')

	var last byte = 0xff
	writeFromLastTo := func(end byte) {
		if buf.Len() > 1 {
			buf.WriteByte(',')
		}
		if last == end {
			buf.WriteString(t.printable(last))
		} else {
			buf.WriteString(t.printable(last))
			buf.WriteByte('-')
			buf.WriteString(t.printable(end))
		}
	}

	for i := byte(0); i <= unicode.MaxASCII; i++ {
		if t.allowed(i) {
			if last == 0xff {


@@ 71,18 89,13 @@ func (t *asciiTable) String() string {
		if last == 0xff {
			continue
		}
		if buf.Len() > 1 {
			buf.WriteByte(',')
		}
		if last == i-1 {
			buf.WriteString(t.printable(last))
		} else {
			buf.WriteString(t.printable(last))
			buf.WriteByte('-')
			buf.WriteString(t.printable(i - 1))
		}
		writeFromLastTo(i - 1)
		last = 0xff
	}
	if last != 0xff {
		writeFromLastTo(unicode.MaxASCII)
	}

	buf.WriteByte(']')
	return buf.String()
}

M ebnf_test.go => ebnf_test.go +2 -0
@@ 22,6 22,8 @@ func TestASCIITable(t *testing.T) {
		{"abcde", "d", "[a-c,e]"},
		{"", "a", "[]"},
		{"\x00\x01\x02\x03IBM", "\x03B", "[x00-x02,I,M]"},
		{"\x7f", "", "[x7f]"},
		{"\x7c\x7d\x7e\x7f", "\x7c", "[}-x7f]"},
	}
	for _, c := range cases {
		t.Run(fmt.Sprintf("set: %q, unset: %q", c.set, c.unset), func(t *testing.T) {