diff options
| author | Shulhan <ms@kilabit.info> | 2024-03-05 17:59:13 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-03-05 18:00:34 +0700 |
| commit | dc67158aeeb6035f0755a3156449a342edbf24b0 (patch) | |
| tree | 6af1d09cb0160ef068d11a3bde37edca87e8b179 | |
| parent | 2a51183943b29f9011d2ffa439a1cce502ce36cb (diff) | |
| download | asciidoctor-go-dc67158aeeb6035f0755a3156449a342edbf24b0.tar.xz | |
all: replace if-else bytes.Equals with static string case comparisons
| -rw-r--r-- | element.go | 13 | ||||
| -rw-r--r-- | parser.go | 49 |
2 files changed, 26 insertions, 36 deletions
@@ -511,14 +511,14 @@ func (el *element) parseListUnorderedItem(line []byte) { } if len(line[x:]) > 3 { var ( - checklist = line[x : x+3] - sym string + checklist = line[x : x+3] + strchecklist = string(checklist) + sym string ) - if bytes.Equal(checklist, []byte(`[ ]`)) { + switch strchecklist { + case `[ ]`: sym = symbolUnchecked - } else if bytes.Equal(checklist, []byte(`[x]`)) || - bytes.Equal(checklist, []byte(`[X]`)) || - bytes.Equal(checklist, []byte(`[*]`)) { + case `[x]`, `[X]`, `[*]`: sym = symbolChecked } if len(sym) != 0 { @@ -532,7 +532,6 @@ func (el *element) parseListUnorderedItem(line []byte) { } break } - } } el.Write(line[x:]) @@ -718,6 +718,8 @@ func whatKindOfLine(line []byte) (kind int, spaces, got []byte) { line = bytes.TrimRight(line, " \f\n\r\t\v") + // All of the comparison MUST be in order. + if len(line) == 0 { return lineKindEmpty, nil, line } @@ -731,48 +733,37 @@ func whatKindOfLine(line []byte) (kind int, spaces, got []byte) { // for example "//comment". return lineKindComment, spaces, line } - if bytes.Equal(line, []byte(`'''`)) || - bytes.Equal(line, []byte(`---`)) || - bytes.Equal(line, []byte(`- - -`)) || - bytes.Equal(line, []byte(`***`)) || - bytes.Equal(line, []byte(`* * *`)) { + + var strline = string(line) + + switch strline { + case `'''`, `---`, `- - -`, `***`, `* * *`: return lineKindHorizontalRule, spaces, line - } - if bytes.Equal(line, []byte(`<<<`)) { + case `<<<`: return lineKindPageBreak, spaces, line - } - if bytes.Equal(line, []byte(`--`)) { + case `--`: return elKindBlockOpen, spaces, line - } - if bytes.Equal(line, []byte(`____`)) { + case `____`: return elKindBlockExcerpts, spaces, line - } - if bytes.Equal(line, []byte(`....`)) { + case `....`: return elKindBlockLiteral, nil, line - } - if bytes.Equal(line, []byte(`++++`)) { + case `++++`: return elKindBlockPassthrough, spaces, line - } - if bytes.Equal(line, []byte(`****`)) { + case `****`: return elKindBlockSidebar, nil, line - } - if bytes.Equal(line, []byte(`====`)) { + case `====`: return elKindBlockExample, spaces, line + case `[listing]`: + return elKindBlockListingNamed, nil, line + case `[literal]`: + return elKindBlockLiteralNamed, nil, line + case `toc::[]`: + return elKindMacroTOC, spaces, line } if bytes.HasPrefix(line, []byte(`|===`)) { return elKindTable, nil, line } - - if bytes.Equal(line, []byte(`[listing]`)) { - return elKindBlockListingNamed, nil, line - } - if bytes.Equal(line, []byte(`[literal]`)) { - return elKindBlockLiteralNamed, nil, line - } - if bytes.Equal(line, []byte(`toc::[]`)) { - return elKindMacroTOC, spaces, line - } if bytes.HasPrefix(line, []byte(`image::`)) { return elKindBlockImage, spaces, line } |
