Skip to content

Commit cfb14f8

Browse files
committed
cleaned up much of the code
1 parent 167a9d7 commit cfb14f8

File tree

4 files changed

+143
-144
lines changed

4 files changed

+143
-144
lines changed

format_state.go

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ import (
77
)
88

99
// A formatState holds the current state of open tag, class, or style formats.
10-
type formatState struct {
11-
open []*Format // the list of currently open attribute tags
12-
}
10+
type formatState []*Format // the list of currently open attribute tags
1311

1412
// hasSet says if the given format is already opened.
1513
func (fs *formatState) hasSet(fm *Format) bool {
16-
for i := range fs.open {
17-
if fs.open[i].Place == fm.Place && fs.open[i].Val == fm.Val {
14+
for i := range *fs {
15+
if (*fs)[i].Place == fm.Place && (*fs)[i].Val == fm.Val {
1816
return true
1917
}
2018
}
@@ -25,19 +23,19 @@ func (fs *formatState) hasSet(fm *Format) bool {
2523
// in the opposite order in which they were opened.
2624
func (fs *formatState) closePrevious(buf *bytes.Buffer, o *Op, doingBlock bool) {
2725

28-
closedTemp := &formatState{}
26+
closedTemp := make(formatState, 0, 1)
2927

30-
for i := len(fs.open) - 1; i >= 0; i-- { // Start with the last format opened.
28+
for i := len(*fs) - 1; i >= 0; i-- { // Start with the last format opened.
3129

32-
f := fs.open[i]
30+
f := (*fs)[i]
3331

3432
// If this format is not set on the current Op, close it.
35-
if (!f.wrap && !f.fm.HasFormat(o)) || (f.wrap && f.fm.(FormatWrapper).Close(fs.open, o, doingBlock)) {
33+
if (!f.wrap && !f.fm.HasFormat(o)) || (f.wrap && f.fm.(FormatWrapper).Close(*fs, o, doingBlock)) {
3634

3735
// If we need to close a tag after which there are tags that should stay open, close the following tags for now.
38-
if i < len(fs.open)-1 {
39-
for ij := len(fs.open) - 1; ij > i; ij-- {
40-
closedTemp.add(fs.open[ij])
36+
if i < len(*fs)-1 {
37+
for ij := len(*fs) - 1; ij > i; ij-- {
38+
closedTemp.add((*fs)[ij])
4139
fs.pop(buf)
4240
}
4341
}
@@ -50,29 +48,29 @@ func (fs *formatState) closePrevious(buf *bytes.Buffer, o *Op, doingBlock bool)
5048

5149
// Re-open the temporarily closed formats.
5250
closedTemp.writeFormats(buf)
53-
fs.open = append(fs.open, closedTemp.open...) // Copy after the sorting.
51+
*fs = append(*fs, closedTemp...) // Copy after the sorting.
5452

5553
}
5654

5755
// pop removes the last format from the state of currently open formats.
5856
func (fs *formatState) pop(buf *bytes.Buffer) {
59-
indx := len(fs.open) - 1
60-
if fs.open[indx].wrap {
61-
buf.WriteString(fs.open[indx].wrapPost)
62-
} else if fs.open[indx].Place == Tag {
63-
closeTag(buf, fs.open[indx].Val)
57+
indx := len(*fs) - 1
58+
if (*fs)[indx].wrap {
59+
buf.WriteString((*fs)[indx].wrapPost)
60+
} else if (*fs)[indx].Place == Tag {
61+
closeTag(buf, (*fs)[indx].Val)
6462
} else {
6563
closeTag(buf, "span")
6664
}
67-
fs.open = fs.open[:indx]
65+
*fs = (*fs)[:indx]
6866
}
6967

7068
// add adds a format that the string that will be written to buf right after this will have.
7169
// Before calling add, check if the Format is already opened up earlier.
7270
// Do not use add to write block-level styles (those are written by o.writeBlock after being merged).
7371
func (fs *formatState) add(f *Format) {
7472
if f.Place < 3 { // Check if the Place is valid.
75-
fs.open = append(fs.open, f)
73+
*fs = append(*fs, f)
7674
}
7775
}
7876

@@ -82,24 +80,24 @@ func (fs *formatState) writeFormats(buf *bytes.Buffer) {
8280

8381
sort.Sort(fs) // Ensure that the serialization is consistent even if attribute ordering in a map changes.
8482

85-
for i := range fs.open {
83+
for i := range *fs {
8684

87-
if fs.open[i].wrap {
88-
buf.WriteString(fs.open[i].Val) // The complete opening or closing wrap is given.
85+
if (*fs)[i].wrap {
86+
buf.WriteString((*fs)[i].Val) // The complete opening or closing wrap is given.
8987
continue
9088
}
9189

9290
buf.WriteByte('<')
9391

94-
switch fs.open[i].Place {
92+
switch (*fs)[i].Place {
9593
case Tag:
96-
buf.WriteString(fs.open[i].Val)
94+
buf.WriteString((*fs)[i].Val)
9795
case Class:
9896
buf.WriteString("span class=")
99-
buf.WriteString(strconv.Quote(fs.open[i].Val))
97+
buf.WriteString(strconv.Quote((*fs)[i].Val))
10098
case Style:
10199
buf.WriteString("span style=")
102-
buf.WriteString(strconv.Quote(fs.open[i].Val))
100+
buf.WriteString(strconv.Quote((*fs)[i].Val))
103101
}
104102

105103
buf.WriteByte('>')
@@ -110,29 +108,27 @@ func (fs *formatState) writeFormats(buf *bytes.Buffer) {
110108

111109
// Implement the sort.Interface interface.
112110

113-
func (fs *formatState) Len() int {
114-
return len(fs.open)
115-
}
111+
func (fs *formatState) Len() int { return len(*fs) }
116112

117113
func (fs *formatState) Less(i, j int) bool {
118114

119115
// Formats that implement the FormatWrapper interface are written first.
120-
if _, ok := fs.open[i].fm.(FormatWrapper); ok {
116+
if _, ok := (*fs)[i].fm.(FormatWrapper); ok {
121117
return true
122-
} else if _, ok := fs.open[j].fm.(FormatWrapper); ok {
118+
} else if _, ok := (*fs)[j].fm.(FormatWrapper); ok {
123119
return false
124120
}
125121

126122
// Tags are written first, then classes, and then style attributes.
127-
if fs.open[i].Place != fs.open[j].Place {
128-
return fs.open[i].Place < fs.open[j].Place
123+
if (*fs)[i].Place != (*fs)[j].Place {
124+
return (*fs)[i].Place < (*fs)[j].Place
129125
}
130126

131127
// Simply check values.
132-
return fs.open[i].Val < fs.open[j].Val
128+
return (*fs)[i].Val < (*fs)[j].Val
133129

134130
}
135131

136132
func (fs *formatState) Swap(i, j int) {
137-
fs.open[i], fs.open[j] = fs.open[j], fs.open[i]
133+
(*fs)[i], (*fs)[j] = (*fs)[j], (*fs)[i]
138134
}

format_state_test.go

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,31 @@ func TestFormatState_add(t *testing.T) {
5555
},
5656
}
5757

58-
fs := new(formatState) // reuse
58+
fs := make(formatState, 0, 2) // reuse
5959

6060
for i, ca := range cases {
6161

62-
fs.open = ca.current
62+
fs = ca.current
6363

6464
fmTer := ca.o.getFormatter(ca.keyword, nil)
6565
fm := fmTer.Fmt()
6666
fm.fm = fmTer
6767

6868
fs.add(fm)
6969

70-
if len(ca.want) != len(fs.open) {
71-
t.Errorf("(index %d) unequal count of formats", i)
72-
t.FailNow()
70+
if len(ca.want) != len(fs) {
71+
t.Fatalf("(index %d) unequal count of formats", i)
7372
}
7473

75-
for j := range fs.open {
76-
if ca.want[j].Val != fs.open[j].Val {
77-
t.Errorf("did not add format Val correctly (index %d); got %q", i, fs.open[j].Val)
74+
for j := range fs {
75+
if ca.want[j].Val != fs[j].Val {
76+
t.Errorf("did not add format Val correctly (index %d); got %q", i, fs[j].Val)
7877
}
79-
if ca.want[j].Place != fs.open[j].Place {
80-
t.Errorf("did not add format Place correctly (index %d); got %v", i, fs.open[j].Place)
78+
if ca.want[j].Place != fs[j].Place {
79+
t.Errorf("did not add format Place correctly (index %d); got %v", i, fs[j].Place)
8180
}
82-
if ca.want[j].Block != fs.open[j].Block {
83-
t.Errorf("did not add format Block correctly (index %d); got %v", i, fs.open[j].Block)
81+
if ca.want[j].Block != fs[j].Block {
82+
t.Errorf("did not add format Block correctly (index %d); got %v", i, fs[j].Block)
8483
}
8584
}
8685

@@ -99,27 +98,27 @@ func TestFormatState_closePrevious(t *testing.T) {
9998
o2.Attrs["italic"] = "y"
10099

101100
cases := []formatState{
102-
{[]*Format{
101+
{
103102
{"em", Tag, false, false, "", "", o1.getFormatter("italic", nil)},
104103
{"strong", Tag, false, false, "", "", o1.getFormatter("bold", nil)},
105-
}},
106-
{[]*Format{
104+
},
105+
{
107106
{"background-color:#e0e0e0;", Style, false, false, "", "", o2.getFormatter("background", nil)},
108107
{"em", Tag, false, false, "", "", o2.getFormatter("italic", nil)},
109-
}},
108+
},
110109
}
111110

112111
want := []string{"</strong></em>", "</em></span>"}
113112

114-
buf := new(bytes.Buffer)
113+
var buf bytes.Buffer
115114

116115
for i := range cases {
117116

118117
o := blankOp()
119118

120-
cases[i].closePrevious(buf, o, false)
119+
cases[i].closePrevious(&buf, o, false)
121120
got := buf.String()
122-
if got != want[i] || len(cases[i].open) != 0 {
121+
if got != want[i] || len(cases[i]) != 0 {
123122
t.Errorf("closed formats wrong (index %d); wanted %q; got %q", i, want[i], got)
124123
}
125124

@@ -187,38 +186,38 @@ func TestFormatState_Sort(t *testing.T) {
187186

188187
for i := range cases {
189188

190-
fsCase := new(formatState)
189+
fsCase := make(formatState, 0, 1)
191190
for _, s := range cases[i] {
192-
fsCase.open = append(fsCase.open, &Format{
191+
fsCase = append(fsCase, &Format{
193192
Val: s.Val,
194193
Place: s.Place,
195194
fm: o.getFormatter(s.keyword, nil),
196195
})
197196
}
198197

199-
sort.Sort(fsCase)
198+
sort.Sort(&fsCase)
200199

201-
fsWant := new(formatState)
200+
fsWant := make(formatState, 0, 1)
202201
for _, s := range want[i] {
203-
fsWant.open = append(fsWant.open, &Format{
202+
fsWant = append(fsWant, &Format{
204203
Val: s.Val,
205204
Place: s.Place,
206205
fm: o.getFormatter(s.keyword, nil),
207206
})
208207
}
209208

210209
ok := true
211-
for j := range fsCase.open {
212-
if fsCase.open[j].Val != fsWant.open[j].Val {
210+
for j := range fsCase {
211+
if fsCase[j].Val != fsWant[j].Val {
213212
ok = false
214-
} else if fsCase.open[j].Place != fsWant.open[j].Place {
213+
} else if fsCase[j].Place != fsWant[j].Place {
215214
ok = false
216215
}
217216
}
218217
if !ok {
219218
t.Errorf("bad sorting (index %d); got:\n", i)
220-
for k := range fsCase.open {
221-
t.Errorf(" (%d) %+v\n", k, *fsCase.open[k])
219+
for k := range fsCase {
220+
t.Errorf(" (%d) %+v\n", k, fsCase[k])
222221
}
223222
}
224223

0 commit comments

Comments
 (0)