Additionale export

- SafeFuncname
- Tiny doc patch
pull/4/head
Pierre Baillet 2013-09-30 21:15:16 +02:00
parent e6d1b0ecbd
commit de61f8ffd8
4 changed files with 38 additions and 35 deletions

Binary file not shown.

View File

@ -26,7 +26,8 @@ var go_bindata = make(map[string]func() []byte)`, pkgname)
return ioutil.WriteFile(file, []byte(code), 0600)
}
// writeTOCInit writes the TOC init function for a given data file.
// WriteTOCInit writes the TOC init function for a given data file
// replacing the prefix in the filename by "", funcname being the translated function name
func WriteTOCInit(output io.Writer, filename, prefix, funcname string) {
filename = strings.Replace(filename, prefix, "", 1)
fmt.Fprintf(output, `

View File

@ -8,8 +8,13 @@ import (
"compress/gzip"
"fmt"
"io"
"regexp"
"strings"
"unicode"
)
var regFuncName = regexp.MustCompile(`[^a-zA-Z0-9_]`)
// translate translates the input file to go source code.
func Translate(input io.Reader, output io.Writer, pkgname, funcname string, uncompressed, nomemcpy bool) {
if nomemcpy {
@ -151,3 +156,32 @@ func %s() []byte {
}
`, funcname, funcname, funcname, funcname)
}
// safeFuncname creates a safe function name from the input path.
func SafeFuncname(in, prefix string) string {
name := strings.Replace(in, prefix, "", 1)
if len(name) == 0 {
name = in
}
name = strings.ToLower(name)
name = regFuncName.ReplaceAllString(name, "_")
if unicode.IsDigit(rune(name[0])) {
// Identifier can't start with a digit.
name = "_" + name
}
// Get rid of "__" instances for niceness.
for strings.Index(name, "__") > -1 {
name = strings.Replace(name, "__", "_", -1)
}
// Leading underscore is silly.
if name[0] == '_' {
name = name[1:]
}
return name
}

36
main.go
View File

@ -11,8 +11,6 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strings"
"unicode"
)
@ -28,7 +26,6 @@ var (
tags = flag.String("tags", "", "Optional build tags")
toc = flag.Bool("toc", false, "Generate a table of contents for this and other files. The input filepath becomes the map key. This option is only useable in non-pipe mode.")
version = flag.Bool("version", false, "Display version information.")
regFuncName = regexp.MustCompile(`[^a-zA-Z0-9_]`)
)
func main() {
@ -73,7 +70,7 @@ func main() {
return
}
bindata.WriteTOCInit(fd, in, *prefix, *funcname)
bindata.WriteTOCInit(fd, in, "", *funcname)
}
}
@ -115,7 +112,7 @@ func parseArgs() {
os.Exit(1)
}
*funcname = safeFuncname(in, *prefix)
*funcname = bindata.SafeFuncname(in, *prefix)
fmt.Fprintf(os.Stderr, "[w] No function name specified. Using %s.\n", *funcname)
}
}
@ -158,32 +155,3 @@ func safeFilename(out, in string) string {
return filename
}
// safeFuncname creates a safe function name from the input path.
func safeFuncname(in, prefix string) string {
name := strings.Replace(in, prefix, "", 1)
if len(name) == 0 {
name = in
}
name = strings.ToLower(name)
name = regFuncName.ReplaceAllString(name, "_")
if unicode.IsDigit(rune(name[0])) {
// Identifier can't start with a digit.
name = "_" + name
}
// Get rid of "__" instances for niceness.
for strings.Index(name, "__") > -1 {
name = strings.Replace(name, "__", "_", -1)
}
// Leading underscore is silly.
if name[0] == '_' {
name = name[1:]
}
return name
}