Skip to content

Commit b4a7d76

Browse files
author
Timothée Peignier
committed
Fix some unicode corner case
1 parent 76f6b29 commit b4a7d76

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

cmd/schematic/schematic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/interagent/schematic"
2626
)
2727

28-
var output = flag.String("o", "", "Ouput file")
28+
var output = flag.String("o", "", "Output file")
2929

3030
func main() {
3131
defer func() {

helpers.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,21 @@ func capFirst(ident string) string {
119119
func asComment(c string) string {
120120
var buf bytes.Buffer
121121
const maxLen = 70
122-
removeNewlines := func(s string) string {
123-
return strings.Replace(s, "\n", "\n// ", -1)
124-
}
125122
r := []rune(c)
126123
for len(r) > 0 {
127124
line := r
128125
if len(line) < maxLen {
129-
fmt.Fprintf(&buf, "// %s\n", removeNewlines(string(line)))
126+
fmt.Fprintf(&buf, "// %s\n", removeNewlines(line))
130127
break
131128
}
132129
line = line[:maxLen]
133-
si := strings.LastIndexFunc(string(line), func(r rune) bool {
130+
si := lastIndex(line, func(r rune) bool {
134131
return unicode.IsSpace(r)
135132
})
136133
if si != -1 {
137134
line = line[:si]
138135
}
139-
fmt.Fprintf(&buf, "// %s\n", removeNewlines(string(line)))
136+
fmt.Fprintf(&buf, "// %s\n", removeNewlines(line))
140137
r = r[len(line):]
141138
if si != -1 {
142139
r = r[1:]
@@ -207,3 +204,16 @@ func paramType(name string, l *Link) string {
207204
func defineCustomType(s *Schema, l *Link) bool {
208205
return l.TargetSchema != nil && l.TargetSchema != s
209206
}
207+
208+
func removeNewlines(s []rune) string {
209+
return strings.Replace(string(s), "\n", "\n// ", -1)
210+
}
211+
212+
func lastIndex(s []rune, f func(rune) bool) int {
213+
for i := len(s) - 1; i > 0; i-- {
214+
if f(s[i]) {
215+
return i
216+
}
217+
}
218+
return -1
219+
}

helpers_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,17 @@ lines.`,
5757
`,
5858
},
5959
{
60-
Comment: `散りぬべき時知りてこそ世の中の花も花なれ人も人なれ`,
61-
Commented: `// 散りぬべき時知りてこそ世の中の花も花なれ人も人なれ
60+
Comment: `日本では、フランスはファッションや美術、料理など、文化的に高い評価を受ける国として有名であり、毎年多数の日本人観光客が高級ブランドや美術館巡り、グルメツアーなどを目的にフランスを訪れている。また、音楽、美術、料理などを学ぶためにフランスに渡る日本人も多く、在仏日本人は3万5千人に及ぶ`,
61+
Commented: `// 日本では、フランスはファッションや美術、料理など、文化的に高い評価を受ける国として有名であり、毎年多数の日本人観光客が高級ブランドや美術館巡
62+
// り、グルメツアーなどを目的にフランスを訪れている。また、音楽、美術、料理などを学ぶためにフランスに渡る日本人も多く、在仏日本人は3万5千人に
63+
// 及ぶ
64+
`,
65+
},
66+
{
67+
Comment: `a value that Heroku will use to sign all webhook notification requests (the signature is included in the request’s "Heroku-Webhook-Hmac-SHA256" header)`,
68+
Commented: `// a value that Heroku will use to sign all webhook notification
69+
// requests (the signature is included in the request’s
70+
// "Heroku-Webhook-Hmac-SHA256" header)
6271
`,
6372
},
6473
}

0 commit comments

Comments
 (0)