Skip to content

Commit

Permalink
Fix some unicode corner case
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothée Peignier committed Feb 15, 2018
1 parent 76f6b29 commit b4a7d76
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/schematic/schematic.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/interagent/schematic"
)

var output = flag.String("o", "", "Ouput file")
var output = flag.String("o", "", "Output file")

func main() {
defer func() {
Expand Down
22 changes: 16 additions & 6 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,21 @@ func capFirst(ident string) string {
func asComment(c string) string {
var buf bytes.Buffer
const maxLen = 70
removeNewlines := func(s string) string {
return strings.Replace(s, "\n", "\n// ", -1)
}
r := []rune(c)
for len(r) > 0 {
line := r
if len(line) < maxLen {
fmt.Fprintf(&buf, "// %s\n", removeNewlines(string(line)))
fmt.Fprintf(&buf, "// %s\n", removeNewlines(line))
break
}
line = line[:maxLen]
si := strings.LastIndexFunc(string(line), func(r rune) bool {
si := lastIndex(line, func(r rune) bool {
return unicode.IsSpace(r)
})
if si != -1 {
line = line[:si]
}
fmt.Fprintf(&buf, "// %s\n", removeNewlines(string(line)))
fmt.Fprintf(&buf, "// %s\n", removeNewlines(line))
r = r[len(line):]
if si != -1 {
r = r[1:]
Expand Down Expand Up @@ -207,3 +204,16 @@ func paramType(name string, l *Link) string {
func defineCustomType(s *Schema, l *Link) bool {
return l.TargetSchema != nil && l.TargetSchema != s
}

func removeNewlines(s []rune) string {
return strings.Replace(string(s), "\n", "\n// ", -1)
}

func lastIndex(s []rune, f func(rune) bool) int {
for i := len(s) - 1; i > 0; i-- {
if f(s[i]) {
return i
}
}
return -1
}
13 changes: 11 additions & 2 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,17 @@ lines.`,
`,
},
{
Comment: `散りぬべき時知りてこそ世の中の花も花なれ人も人なれ`,
Commented: `// 散りぬべき時知りてこそ世の中の花も花なれ人も人なれ
Comment: `日本では、フランスはファッションや美術、料理など、文化的に高い評価を受ける国として有名であり、毎年多数の日本人観光客が高級ブランドや美術館巡り、グルメツアーなどを目的にフランスを訪れている。また、音楽、美術、料理などを学ぶためにフランスに渡る日本人も多く、在仏日本人は3万5千人に及ぶ`,
Commented: `// 日本では、フランスはファッションや美術、料理など、文化的に高い評価を受ける国として有名であり、毎年多数の日本人観光客が高級ブランドや美術館巡
// り、グルメツアーなどを目的にフランスを訪れている。また、音楽、美術、料理などを学ぶためにフランスに渡る日本人も多く、在仏日本人は3万5千人に
// 及ぶ
`,
},
{
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)`,
Commented: `// 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)
`,
},
}
Expand Down

0 comments on commit b4a7d76

Please sign in to comment.