-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from tsingbx/llcppsymg/struct_methodname
llcppsymg: go symbol name for go struct methodname
- Loading branch information
Showing
8 changed files
with
185 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
*.dll | ||
*.so | ||
*.dylib | ||
*.DS_Store | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#stdout | ||
=== Test ToGoName === | ||
Before: lua_closethread After: Closethread | ||
Before: luaL_checknumber After: Checknumber | ||
Before: sqlite3_close_v2 After: CloseV2 | ||
Before: sqlite3_callback After: Callback | ||
Before: GetReal After: GetReal | ||
Before: GetBoolean After: GetBoolean | ||
Before: INIReader After: Reader | ||
|
||
|
||
#stderr | ||
|
||
#exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/goplus/llcppg/_xtool/llcppsymg/names" | ||
"github.com/goplus/llcppg/_xtool/llcppsymg/parse" | ||
) | ||
|
||
func main() { | ||
TestToGoName() | ||
} | ||
|
||
func TestToGoName() { | ||
fmt.Println("=== Test ToGoName ===") | ||
process1 := parse.NewSymbolProcessor([]string{}, []string{"lua_", "luaL_"}) | ||
process2 := parse.NewSymbolProcessor([]string{}, []string{"sqlite3_", "sqlite3_"}) | ||
process3 := parse.NewSymbolProcessor([]string{}, []string{"INI"}) | ||
|
||
testCases := []struct { | ||
processor *parse.SymbolProcessor | ||
input string | ||
}{ | ||
{process1, "lua_closethread"}, | ||
{process1, "luaL_checknumber"}, | ||
{process2, "sqlite3_close_v2"}, | ||
{process2, "sqlite3_callback"}, | ||
{process3, "GetReal"}, | ||
{process3, "GetBoolean"}, | ||
{process3, "INIReader"}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
result := names.GoName(tc.input, tc.processor.Prefixes, true) | ||
fmt.Printf("Before: %s After: %s\n", tc.input, result) | ||
} | ||
fmt.Println() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package names | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func GoName(name string, trimPrefixes []string, inCurPkg bool) string { | ||
if inCurPkg { | ||
name = removePrefixedName(name, trimPrefixes) | ||
} | ||
return pubName(name) | ||
} | ||
|
||
func removePrefixedName(name string, trimPrefixes []string) string { | ||
if len(trimPrefixes) == 0 { | ||
return name | ||
} | ||
for _, prefix := range trimPrefixes { | ||
if strings.HasPrefix(name, prefix) { | ||
return strings.TrimPrefix(name, prefix) | ||
} | ||
} | ||
return name | ||
} | ||
|
||
func pubName(name string) string { | ||
if len(name) == 0 { | ||
return name | ||
} | ||
toCamelCase := func(s string) string { | ||
parts := strings.Split(s, "_") | ||
for i := 0; i < len(parts); i++ { | ||
if len(parts[i]) > 0 { | ||
parts[i] = strings.ToUpper(parts[i][:1]) + parts[i][1:] | ||
} | ||
} | ||
return strings.Join(parts, "") | ||
} | ||
if name[0] == '_' { | ||
i := 0 | ||
for i < len(name) && name[i] == '_' { | ||
i++ | ||
} | ||
prefix := name[:i] | ||
return "X" + prefix + toCamelCase(name[i:]) | ||
} | ||
return toCamelCase(name) | ||
} |
Oops, something went wrong.