diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index 393d23f3..974b98da 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -297,9 +297,7 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl) if (Config.StripEnumMemberTypeName) { - Regex stripParentNameRegex = new($"^{Regex.Escape(parentName)}_*", RegexOptions.IgnoreCase); - var strippedName = stripParentNameRegex.Replace(escapedName, string.Empty); - escapedName = strippedName; + escapedName = PrefixAndStrip(escapedName, parentName, trimChar: '_'); } var kind = isAnonymousEnum ? ValueKind.Primitive : ValueKind.Enumerator; @@ -543,12 +541,12 @@ private void VisitFunctionDecl(FunctionDecl functionDecl) if ((cxxMethodDecl is not null) && cxxMethodDecl.IsVirtual) { isVirtual = true; - escapedName = PrefixAndStripName(name, GetOverloadIndex(cxxMethodDecl)); + escapedName = PrefixAndStripMethodName(name, GetOverloadIndex(cxxMethodDecl)); } else { isVirtual = false; - escapedName = EscapeAndStripName(name); + escapedName = EscapeAndStripMethodName(name); } var returnType = functionDecl.ReturnType; @@ -2032,7 +2030,7 @@ void OutputMarkerInterface(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethodD var desc = new FunctionOrDelegateDesc { AccessSpecifier = AccessSpecifier.Public, - EscapedName = EscapeAndStripName(name), + EscapedName = EscapeAndStripMethodName(name), IsMemberFunction = true, NativeTypeName = nativeTypeName, HasFnPtrCodeGen = !_config.ExcludeFnptrCodegen, @@ -2120,7 +2118,7 @@ void OutputVtblEntry(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethodDecl) } var remappedName = FixupNameForMultipleHits(cxxMethodDecl); - var escapedName = EscapeAndStripName(remappedName); + var escapedName = EscapeAndStripMethodName(remappedName); var desc = new FieldDesc { @@ -2189,7 +2187,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod var desc = new FunctionOrDelegateDesc { AccessSpecifier = AccessSpecifier.Public, IsAggressivelyInlined = _config.GenerateAggressiveInlining, - EscapedName = EscapeAndStripName(name), + EscapedName = EscapeAndStripMethodName(name), ParentName = parentName, IsMemberFunction = true, IsInherited = isInherited, @@ -2269,7 +2267,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod { body.Write("Marshal.GetDelegateForFunctionPointer<"); body.BeginMarker("delegate"); - body.Write(PrefixAndStripName(name, GetOverloadIndex(cxxMethodDecl))); + body.Write(PrefixAndStripMethodName(name, GetOverloadIndex(cxxMethodDecl))); body.EndMarker("delegate"); body.Write(">("); } @@ -2278,7 +2276,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod { body.Write("lpVtbl->"); body.BeginMarker("vtbl", new KeyValuePair("explicit", true)); - body.Write(EscapeAndStripName(remappedName)); + body.Write(EscapeAndStripMethodName(remappedName)); body.EndMarker("vtbl"); } else diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs index e216d889..3fd39a09 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs @@ -970,7 +970,7 @@ private void VisitDeclRefExpr(DeclRefExpr declRefExpr) if (declRefExpr.Decl is FunctionDecl) { - escapedName = EscapeAndStripName(name); + escapedName = EscapeAndStripMethodName(name); } } diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index 6edb8098..76ee2b96 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -2326,13 +2326,9 @@ private static string EscapeName(string name) } } - private string EscapeAndStripName(string name) + private string EscapeAndStripMethodName(string name) { - if (name.StartsWith(_config.MethodPrefixToStrip, StringComparison.Ordinal)) - { - name = name[_config.MethodPrefixToStrip.Length..]; - } - + name = PrefixAndStrip(name, _config.MethodPrefixToStrip); return EscapeName(name); } @@ -6198,13 +6194,32 @@ private void ParenthesizeStmt(Stmt stmt) } } - private string PrefixAndStripName(string name, uint overloadIndex) + /// + /// Checks whether the specified name starts with a prefix, optionally trims a separator character following the prefix and returns the remainder. + /// + /// The name to strip. + /// The prefix to strip from . + /// Additional separator that may follow which should also be trimmed away. + /// + /// if it does not start with ; + /// otherwise, + /// the remainder of after stripping and all immediate following occurences of from it beginning. + /// + private static string PrefixAndStrip(string name, string prefix, char trimChar = '\0') { - if (name.StartsWith(_config.MethodPrefixToStrip, StringComparison.Ordinal)) + var nameSpan = name.AsSpan(); + if (nameSpan.StartsWith(prefix, StringComparison.Ordinal)) { - name = name[_config.MethodPrefixToStrip.Length..]; + nameSpan = nameSpan[prefix.Length..]; + nameSpan = nameSpan.TrimStart(trimChar); + return nameSpan.ToString(); + } + return name; } + private string PrefixAndStripMethodName(string name, uint overloadIndex) + { + name = PrefixAndStrip(name, _config.MethodPrefixToStrip); return $"_{name}{((overloadIndex != 0) ? overloadIndex.ToString(CultureInfo.InvariantCulture) : "")}"; }