-
Notifications
You must be signed in to change notification settings - Fork 62
[generator] Escape hyphens for callback names so they're valid C#. #497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7d42821
[generator] Escape hyphens/dollar signs for callback names so they're…
jpobst 803f946
[generator] Ensure we escape all invalid identifier characters.
jpobst 6fee2a2
[Java.Interop.Tools.JavaCallableWrappers] Create common method for va…
jpobst 1f926a2
Remove BOM
jonpryor a5c3acd
Sort filenames
jonpryor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/IdentifierValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
|
||
namespace Java.Interop.Tools.JavaCallableWrappers | ||
{ | ||
public static class IdentifierValidator | ||
{ | ||
// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#identifiers | ||
private const string FormattingCharacter = @"\p{Cf}"; | ||
private const string ConnectingCharacter = @"\p{Pc}"; | ||
private const string DecimalDigitCharacter = @"\p{Nd}"; | ||
private const string CombiningCharacter = @"\p{Mn}\p{Mc}"; | ||
private const string LetterCharacter = @"\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}"; | ||
|
||
private const string IdentifierPartCharacter = LetterCharacter + | ||
DecimalDigitCharacter + | ||
ConnectingCharacter + | ||
CombiningCharacter + | ||
FormattingCharacter; | ||
|
||
private const string IdentifierStartCharacter = "(" + LetterCharacter + "_)"; | ||
|
||
private const string Identifier = IdentifierStartCharacter + "(" + IdentifierPartCharacter + ")"; | ||
|
||
// We use [^ ...] to detect any character that is NOT a match. | ||
static Regex validIdentifier = new Regex ($"[^{Identifier}]", RegexOptions.Compiled); | ||
|
||
public static string CreateValidIdentifier (string identifier, bool useEncodedReplacements = false) | ||
{ | ||
if (string.IsNullOrWhiteSpace (identifier)) return string.Empty; | ||
|
||
var normalizedIdentifier = identifier.Normalize (); | ||
|
||
if (useEncodedReplacements) | ||
return validIdentifier.Replace (normalizedIdentifier, new MatchEvaluator (EncodeReplacement)); | ||
|
||
return validIdentifier.Replace (normalizedIdentifier, "_"); | ||
} | ||
|
||
// Makes uglier but unique identifiers by encoding each invalid character with its character value | ||
static string EncodeReplacement (Match match) => $"_x{(ushort) match.Value [0]}_"; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...CallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers/IdentifierValidatorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Java.Interop.Tools.JavaCallableWrappers; | ||
using Java.Interop.Tools.TypeNameMappings; | ||
using NUnit.Framework; | ||
|
||
namespace Java.Interop.Tools.JavaCallableWrappersTests | ||
{ | ||
[TestFixture] | ||
public class IdentifierValidatorTests | ||
{ | ||
[Test] | ||
public void CreateValidIdentifier_Simple () | ||
{ | ||
Assert.AreEqual ("my_identifier_test", IdentifierValidator.CreateValidIdentifier ("my-identifier$test")); | ||
} | ||
|
||
[Test] | ||
public void CreateValidIdentifier_Encoded () | ||
{ | ||
Assert.AreEqual ("my_x45_identifier_x36_test", IdentifierValidator.CreateValidIdentifier ("my-identifier$test", true)); | ||
Assert.AreEqual ("myidentifier_x55357__x56842_test", IdentifierValidator.CreateValidIdentifier ("myidentifier😊test", true)); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see in this instance you only replace the JavaName and not the signature. Does it matter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is
JavaName.Replace ("<", "_x60_").Replace (">", "_x62_")
and then new one is
"cb_{JavaName}{IDSignature}".Replace (...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this is relevant: #498 (comment)
Have a "single" "ensure the string is a valid C# identifier" method and use that consistently ~everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll be merging this PR, but on the whole I believe @mattleibow is correct, and
EscapedIdName
should likewise useIdentifierValidator.CreateValidIdentifier()
as well.However, AFAICT
EscapedIdName
is only used for the legacyJNIEnv
-based output format, not the newerXAJavaInterop1
type, so I'm not entirely concerned about this. We should preferXAJavaInterop
.