@@ -54,9 +54,11 @@ func CopyStrPtr(sp *string) *string {
5454const (
5555 // FQDNDelimiter is the default FQDN delimiter.
5656 FQDNDelimiter = "."
57+ // FQDNEsc is the default escape char for FQDN. Esc is used for escaping "." and itself.
58+ FQDNEsc = "%"
5759)
5860
59- // BuildFQDN builds an FQDN from a slice of namelet strings.
61+ // BuildFQDN builds an FQDN (dot delimited) from a slice of namelet strings.
6062func BuildFQDN (namelets ... string ) string {
6163 return BuildFQDN2 (FQDNDelimiter , namelets ... )
6264}
@@ -66,9 +68,22 @@ func BuildFQDN2(delimiter string, namelets ...string) string {
6668 return strings .Join (namelets , delimiter )
6769}
6870
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+
6984// LastNameletOfFQDN returns the last namelet of an FQDN delimited by default
7085// delimiter. If there is no delimiter in the FQDN, then the FQDN itself is
71- // // returned.
86+ // returned.
7287func LastNameletOfFQDN (fqdn string ) string {
7388 return LastNameletOfFQDN2 (FQDNDelimiter , fqdn )
7489}
@@ -84,6 +99,15 @@ func LastNameletOfFQDN2(delimiter, fqdn string) string {
8499 return fqdn [index + 1 :]
85100}
86101
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+
87111// CopySlice copies a string slice. The returned slice is guaranteed to be a different
88112// slice (thus the name Copy) so modifying the src from the caller side won't affect
89113// the returned slice.
0 commit comments