forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr2go.go
61 lines (54 loc) · 1011 Bytes
/
str2go.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"unicode"
)
const (
backSlash = '\\'
newLine = '\n'
doubleQuote = '"'
)
func printRune(w *bufio.Writer, r rune) {
switch r {
case backSlash:
_, _ = w.WriteRune(backSlash)
_, _ = w.WriteRune(backSlash)
case newLine:
_, _ = w.WriteString("\\n\" +\n\t\"")
case doubleQuote:
_, _ = w.WriteRune(backSlash)
_, _ = w.WriteRune(doubleQuote)
default:
if !isASCII(r) {
panic("only ASCII is supported")
}
_, _ = w.WriteRune(r)
}
}
func isASCII(r rune) bool {
return r < unicode.MaxASCII
}
func main() {
flag.Parse()
packageName := flag.Arg(0)
constantName := flag.Arg(1)
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
fmt.Fprintf(out, "package %s\n\n// %s ...\nconst %s = \"", packageName, constantName, constantName)
for {
r, _, err := in.ReadRune()
if err != nil {
if err == io.EOF {
break
}
panic(err)
}
printRune(out, r)
}
fmt.Fprint(out, "\"\n")
}