diff --git a/binary/binary b/binary/binary deleted file mode 100755 index 843555d..0000000 Binary files a/binary/binary and /dev/null differ diff --git a/lib/toc.go b/lib/toc.go index b752678..a534d93 100644 --- a/lib/toc.go +++ b/lib/toc.go @@ -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, ` diff --git a/lib/translate.go b/lib/translate.go index a03160f..4e8cb2f 100644 --- a/lib/translate.go +++ b/lib/translate.go @@ -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 +} diff --git a/main.go b/main.go index 9dab45b..a8bcd68 100644 --- a/main.go +++ b/main.go @@ -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 -}