Skip to content

Commit

Permalink
Replace unicode.IsSpace with my own implementation
Browse files Browse the repository at this point in the history
Fixed #6

unicode.IsSpace handles unicode spaces, but it is harmful for heredoc.
  • Loading branch information
makenowjust committed Aug 23, 2019
1 parent fd11982 commit 4d4f34d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
19 changes: 16 additions & 3 deletions heredoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ package heredoc
import (
"fmt"
"strings"
"unicode"
)

const maxInt = int(^uint(0) >> 1)
Expand All @@ -54,6 +53,20 @@ func Doc(raw string) string {
return strings.Join(lines, "\n")
}

// isSpace checks whether the rune represents space or not.
// Only white spcaes (U+0020) and horizontal tabs are treated as space character.
// It is the same as Go.
//
// See https://github.com/MakeNowJust/heredoc/issues/6#issuecomment-524231625.
func isSpace(r rune) bool {
switch r {
case ' ', '\t':
return true
default:
return false
}
}

// getMinIndent calculates the minimum indentation in lines, excluding empty lines.
func getMinIndent(lines []string, skipFirstLine bool) int {
minIndentSize := maxInt
Expand All @@ -65,8 +78,8 @@ func getMinIndent(lines []string, skipFirstLine bool) int {

indentSize := 0
for _, r := range []rune(line) {
if unicode.IsSpace(r) {
indentSize += 1
if isSpace(r) {
indentSize++
} else {
break
}
Expand Down
1 change: 1 addition & 0 deletions heredoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var tests = []testCase{
Foo
Bar
`, "Foo\nBar\n"},
{"\n\u3000zenkaku space", "\u3000zenkaku space"},
}

func TestDoc(t *testing.T) {
Expand Down

0 comments on commit 4d4f34d

Please sign in to comment.