-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmangle.go
66 lines (58 loc) · 1.51 KB
/
mangle.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
62
63
64
65
66
package main
import (
"strings"
"unicode"
"unicode/utf8"
)
// MangleCtorName mangles constructor function name.
// orig - original ctor name, gen - generic type name, inst - instantiated type name
func MangleCtorName(orig, gen, inst string) string {
n, isUpper := strUpcase(orig)
inst, _ = strUpcase(inst)
gen, _ = strUpcase(gen)
g := strings.Index(n, gen)
if g < 0 {
n = orig + inst
} else {
n = orig[0:g] + inst + n[g+len(gen):]
}
return strEnsureCase(n, isUpper)
}
// MangleDepTypeName mangles non-root generic type name.
// orig - original dependant type name, gen - "parent" type name, inst - instantiated "parent" type name
func MangleDepTypeName(orig, gen, inst string) string {
n, isUpper := strUpcase(orig)
gen, _ = strUpcase(gen)
inst, _ = strUpcase(inst)
g := strings.Index(n, gen)
if g < 0 {
n = inst + n
} else {
n = orig[0:g] + inst + n[g+len(gen):]
}
return strEnsureCase(n, isUpper)
}
func strUpcase(s string) (string, bool) {
return strReplaceFirst(s, unicode.IsUpper, unicode.ToUpper)
}
func strLocase(s string) (string, bool) {
return strReplaceFirst(s, unicode.IsLower, unicode.ToLower)
}
func strEnsureCase(s string, isUpper bool) (result string) {
if isUpper {
result, _ = strUpcase(s)
} else {
result, _ = strLocase(s)
}
return
}
func strReplaceFirst(s string, isMapped func(rune) bool, doMap func(rune) rune) (string, bool) {
if s == "" {
return "", true
}
r, p := utf8.DecodeRuneInString(s)
if isMapped(r) {
return s, true
}
return string(doMap(r)) + s[p:], false
}