Skip to content

Commit 35aee55

Browse files
OrihTimothée Peignier
authored andcommitted
Support comment written in multibyte characters
1 parent 36ab805 commit 35aee55

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

helpers.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,31 @@ func asComment(c string) string {
122122
removeNewlines := func(s string) string {
123123
return strings.Replace(s, "\n", "\n// ", -1)
124124
}
125-
for len(c) > 0 {
126-
line := c
125+
findLastIndex := func(rs []rune, r rune) int {
126+
l := -1
127+
for i, v := range rs {
128+
if v == r {
129+
l = i
130+
}
131+
}
132+
return l
133+
}
134+
r := []rune(c)
135+
for len(r) > 0 {
136+
line := r
127137
if len(line) < maxLen {
128-
fmt.Fprintf(&buf, "// %s\n", removeNewlines(line))
138+
fmt.Fprintf(&buf, "// %s\n", removeNewlines(string(line)))
129139
break
130140
}
131141
line = line[:maxLen]
132-
si := strings.LastIndex(line, " ")
142+
si := findLastIndex(line, []rune(" ")[0])
133143
if si != -1 {
134144
line = line[:si]
135145
}
136-
fmt.Fprintf(&buf, "// %s\n", removeNewlines(line))
137-
c = c[len(line):]
146+
fmt.Fprintf(&buf, "// %s\n", removeNewlines(string(line)))
147+
r = r[len(line):]
138148
if si != -1 {
139-
c = c[1:]
149+
r = r[1:]
140150
}
141151
}
142152
return buf.String()

0 commit comments

Comments
 (0)