@@ -54,9 +54,11 @@ func CopyStrPtr(sp *string) *string {
54
54
const (
55
55
// FQDNDelimiter is the default FQDN delimiter.
56
56
FQDNDelimiter = "."
57
+ // FQDNEsc is the default escape char for FQDN. Esc is used for escaping "." and itself.
58
+ FQDNEsc = "%"
57
59
)
58
60
59
- // BuildFQDN builds an FQDN from a slice of namelet strings.
61
+ // BuildFQDN builds an FQDN (dot delimited) from a slice of namelet strings.
60
62
func BuildFQDN (namelets ... string ) string {
61
63
return BuildFQDN2 (FQDNDelimiter , namelets ... )
62
64
}
@@ -66,9 +68,22 @@ func BuildFQDN2(delimiter string, namelets ...string) string {
66
68
return strings .Join (namelets , delimiter )
67
69
}
68
70
71
+ var fqdnEscReplacer = strings .NewReplacer ("." , "%." , "%" , "%%" )
72
+
73
+ // BuildFQDNWithEsc builds an FQDN (dot delimited) from a slice of namelet strings with proper escaping.
74
+ // e.g. If namelets are 'a.b', 'c%d', it will return 'a%.b.c%%d'.
75
+ // Note this function isn't optimized for alloc/perf.
76
+ func BuildFQDNWithEsc (namelets ... string ) string {
77
+ return strings .Join (
78
+ NoErrMapSlice (namelets , func (s string ) string {
79
+ return fqdnEscReplacer .Replace (s )
80
+ }),
81
+ FQDNDelimiter )
82
+ }
83
+
69
84
// LastNameletOfFQDN returns the last namelet of an FQDN delimited by default
70
85
// delimiter. If there is no delimiter in the FQDN, then the FQDN itself is
71
- // // returned.
86
+ // returned.
72
87
func LastNameletOfFQDN (fqdn string ) string {
73
88
return LastNameletOfFQDN2 (FQDNDelimiter , fqdn )
74
89
}
@@ -84,6 +99,15 @@ func LastNameletOfFQDN2(delimiter, fqdn string) string {
84
99
return fqdn [index + 1 :]
85
100
}
86
101
102
+ // LastNameletOfFQDNWithEsc returns the last namelet of an FQDN delimited by default
103
+ // delimiter, with escaping considered. If there is no delimiter in the FQDN, then the
104
+ // FQDN itself is returned.
105
+ // Note this function isn't optimized for alloc/perf.
106
+ func LastNameletOfFQDNWithEsc (fqdn string ) string {
107
+ namelets := SplitWithEsc (fqdn , FQDNDelimiter , FQDNEsc )
108
+ return Unescape (namelets [len (namelets )- 1 ], FQDNEsc )
109
+ }
110
+
87
111
// CopySlice copies a string slice. The returned slice is guaranteed to be a different
88
112
// slice (thus the name Copy) so modifying the src from the caller side won't affect
89
113
// the returned slice.
0 commit comments