J2N icon indicating copy to clipboard operation
J2N copied to clipboard

Add IsCSharpIdentifier() and IsCSharpIdentifierPart() methods to Character class

Open NightOwl888 opened this issue 4 years ago • 0 comments

Similar methods were part of the JDK implementation. The rules for how to implement these methods for C# are documented here.

The spot reserved for them to match the Apache Harmony's implementation's order is here.

See this usage example for a real-world perspective of how these methods work together to detect a valid class name.

NOTES:

  • We also should have overloads for IsJavaIdentifier and IsJavaIdentifierStart, since this library is a bridge between Java and .NET and the original implementations might come in handy.
  • Prefer the implementation style of the Apache Harmony Character class is 10 years old, so we should take a look at the current JavaDocs to ensure the implementation is up to date.

Note there is also a port of the Java identifier code to C# in Spatial4n which might come in useful for working out some of the more complex rules, but we should prefer the implementation style of Apache Harmony and avoid the Regex class, if possible. The Regex class documentation might come in handy for some clues about how to handle certain character classes, see Character classes in regular expressions.

Also, the Spatial4n implementation has some shortcomings:

  • I suspect it actually detects Java identifiers rather than C# identifiers because of the link over to the Javadoc, which might not be the right choice for Spatial4n
  • The Unicode support is broken - it is ignoring characters outside of the range c < 0x00d800 && c > 0x00dfff

In the latter case, the code point would have to be converted to a surrogate pair before passed into CharUnicodeInfo.GetUnicodeCategory() as was done in other methods of the Character class, such as GetType(). However, since the Apache Harmony implementation uses the GetType() method directly, using that example will avoid this issue.

NightOwl888 avatar Dec 31 '19 10:12 NightOwl888