diff --git a/heredoc.go b/heredoc.go index 1fc0469..0a62230 100644 --- a/heredoc.go +++ b/heredoc.go @@ -32,7 +32,6 @@ package heredoc import ( "fmt" "strings" - "unicode" ) const maxInt = int(^uint(0) >> 1) @@ -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 @@ -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 } diff --git a/heredoc_test.go b/heredoc_test.go index 6c53973..8eefd73 100644 --- a/heredoc_test.go +++ b/heredoc_test.go @@ -39,6 +39,7 @@ var tests = []testCase{ Foo Bar `, "Foo\nBar\n"}, + {"\n\u3000zenkaku space", "\u3000zenkaku space"}, } func TestDoc(t *testing.T) {