Skip to content

Commit 6703d6a

Browse files
committed
Simplify prefix stripping logic and consolidate
1 parent fa6653d commit 6703d6a

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,7 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl)
297297

298298
if (Config.StripEnumMemberTypeName)
299299
{
300-
Regex stripParentNameRegex = new($"^{Regex.Escape(parentName)}_*", RegexOptions.IgnoreCase);
301-
var strippedName = stripParentNameRegex.Replace(escapedName, string.Empty);
302-
escapedName = strippedName;
300+
escapedName = PrefixAndStrip(escapedName, parentName, trimChar: '_');
303301
}
304302

305303
var kind = isAnonymousEnum ? ValueKind.Primitive : ValueKind.Enumerator;
@@ -543,12 +541,12 @@ private void VisitFunctionDecl(FunctionDecl functionDecl)
543541
if ((cxxMethodDecl is not null) && cxxMethodDecl.IsVirtual)
544542
{
545543
isVirtual = true;
546-
escapedName = PrefixAndStripName(name, GetOverloadIndex(cxxMethodDecl));
544+
escapedName = PrefixAndStripMethodName(name, GetOverloadIndex(cxxMethodDecl));
547545
}
548546
else
549547
{
550548
isVirtual = false;
551-
escapedName = EscapeAndStripName(name);
549+
escapedName = EscapeAndStripMethodName(name);
552550
}
553551

554552
var returnType = functionDecl.ReturnType;
@@ -2032,7 +2030,7 @@ void OutputMarkerInterface(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethodD
20322030

20332031
var desc = new FunctionOrDelegateDesc {
20342032
AccessSpecifier = AccessSpecifier.Public,
2035-
EscapedName = EscapeAndStripName(name),
2033+
EscapedName = EscapeAndStripMethodName(name),
20362034
IsMemberFunction = true,
20372035
NativeTypeName = nativeTypeName,
20382036
HasFnPtrCodeGen = !_config.ExcludeFnptrCodegen,
@@ -2120,7 +2118,7 @@ void OutputVtblEntry(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethodDecl)
21202118
}
21212119

21222120
var remappedName = FixupNameForMultipleHits(cxxMethodDecl);
2123-
var escapedName = EscapeAndStripName(remappedName);
2121+
var escapedName = EscapeAndStripMethodName(remappedName);
21242122

21252123
var desc = new FieldDesc
21262124
{
@@ -2189,7 +2187,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
21892187
var desc = new FunctionOrDelegateDesc {
21902188
AccessSpecifier = AccessSpecifier.Public,
21912189
IsAggressivelyInlined = _config.GenerateAggressiveInlining,
2192-
EscapedName = EscapeAndStripName(name),
2190+
EscapedName = EscapeAndStripMethodName(name),
21932191
ParentName = parentName,
21942192
IsMemberFunction = true,
21952193
IsInherited = isInherited,
@@ -2269,7 +2267,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
22692267
{
22702268
body.Write("Marshal.GetDelegateForFunctionPointer<");
22712269
body.BeginMarker("delegate");
2272-
body.Write(PrefixAndStripName(name, GetOverloadIndex(cxxMethodDecl)));
2270+
body.Write(PrefixAndStripMethodName(name, GetOverloadIndex(cxxMethodDecl)));
22732271
body.EndMarker("delegate");
22742272
body.Write(">(");
22752273
}
@@ -2278,7 +2276,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
22782276
{
22792277
body.Write("lpVtbl->");
22802278
body.BeginMarker("vtbl", new KeyValuePair<string, object>("explicit", true));
2281-
body.Write(EscapeAndStripName(remappedName));
2279+
body.Write(EscapeAndStripMethodName(remappedName));
22822280
body.EndMarker("vtbl");
22832281
}
22842282
else

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ private void VisitDeclRefExpr(DeclRefExpr declRefExpr)
970970

971971
if (declRefExpr.Decl is FunctionDecl)
972972
{
973-
escapedName = EscapeAndStripName(name);
973+
escapedName = EscapeAndStripMethodName(name);
974974
}
975975
}
976976

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,13 +2326,9 @@ private static string EscapeName(string name)
23262326
}
23272327
}
23282328

2329-
private string EscapeAndStripName(string name)
2329+
private string EscapeAndStripMethodName(string name)
23302330
{
2331-
if (name.StartsWith(_config.MethodPrefixToStrip, StringComparison.Ordinal))
2332-
{
2333-
name = name[_config.MethodPrefixToStrip.Length..];
2334-
}
2335-
2331+
name = PrefixAndStrip(name, _config.MethodPrefixToStrip);
23362332
return EscapeName(name);
23372333
}
23382334

@@ -6198,13 +6194,32 @@ private void ParenthesizeStmt(Stmt stmt)
61986194
}
61996195
}
62006196

6201-
private string PrefixAndStripName(string name, uint overloadIndex)
6197+
/// <summary>
6198+
/// Checks whether the specified name starts with a prefix, optionally trims a separator character following the prefix and returns the remainder.
6199+
/// </summary>
6200+
/// <param name="name">The name to strip.</param>
6201+
/// <param name="prefix">The prefix to strip from <paramref name="name"/>.</param>
6202+
/// <param name="trimChar">Additional separator that may follow <paramref name="prefix"/> which should also be trimmed away.</param>
6203+
/// <returns>
6204+
/// <paramref name="name"/> if it does not start with <paramref name="prefix"/>;
6205+
/// otherwise,
6206+
/// the remainder of <paramref name="name"/> after stripping <paramref name="prefix"/> and all immediate following occurences of <paramref name="trimChar"/> from it beginning.
6207+
/// </returns>
6208+
private static string PrefixAndStrip(string name, string prefix, char trimChar = '\0')
62026209
{
6203-
if (name.StartsWith(_config.MethodPrefixToStrip, StringComparison.Ordinal))
6210+
var nameSpan = name.AsSpan();
6211+
if (nameSpan.StartsWith(prefix, StringComparison.Ordinal))
62046212
{
6205-
name = name[_config.MethodPrefixToStrip.Length..];
6213+
nameSpan = nameSpan[prefix.Length..];
6214+
nameSpan = nameSpan.TrimStart(trimChar);
6215+
return nameSpan.ToString();
6216+
}
6217+
return name;
62066218
}
62076219

6220+
private string PrefixAndStripMethodName(string name, uint overloadIndex)
6221+
{
6222+
name = PrefixAndStrip(name, _config.MethodPrefixToStrip);
62086223
return $"_{name}{((overloadIndex != 0) ? overloadIndex.ToString(CultureInfo.InvariantCulture) : "")}";
62096224
}
62106225

0 commit comments

Comments
 (0)