NightOwl888 / ICU4N

International Components for Unicode for .NET
Apache License 2.0
27 stars 7 forks source link

Dropped support for ICharSequence in favor of ReadOnlySpan<char>/ReadOnlyMemory<char> #71

Closed NightOwl888 closed 1 month ago

NightOwl888 commented 1 month ago

Fixes #56, Fixes #54, Closes #55

This is a very large sweeping API change. Here are the highlights:

  1. Removed support for ICharSequence.
  2. Added wide support for ReadOnlySpan<char> where supported or ReadOnlyMemory<char> where not.
  3. Dropped support for StringBuilder (as a char sequence type).
  4. Dropped direct support for char[], since ReadOnlySpan<char> implicitly converts it.
  5. Removed all code generation for char sequence types.
  6. Removed duplicated business logic in most places. String can be directly converted to ReadOnlySpan<char> or ReadOnlyMemory<char>, so most of the char sequence business logic is in methods that accept these.
  7. Added internal ValueStringBuilder ref struct for building char sequences on the stack or heap while making minimal changes to the business logic.
  8. Added internal OpenStringBuilder class for cases not covered by ValueStringBuilder, such as a class field.
  9. Replaced most usages of StringBuilder with either ValueStringBuilder or OpenStringBuilder.
  10. Changed Normalizer to do the entire operation on the stack in most cases and refactored the API for Span<char> output.
  11. Refactored StringPrep classes (IDNA, UTS46, IDNA2003) so they have a Try... version of each method that outputs an error code rather than throwing exceptions.
  12. Refactored SimpleFormatter to allow input as ReadOnlySpan<char> parameters.
  13. Ported CaseMapImpl functionality from ICU4C so we can eliminate the character iterator used in Java, thus supporting stack allocations using ReadOnlySpan<char>.
  14. Reduced allocations when loading or interacting with UCultureInfo.
  15. Removed FEATURE_SPAN and FEATURE_ARRAYPOOL.

StringBuilder is inadequate for our needs because it doesn't maintain a contiguous array of characters and we need to rapidly switch between writing and reading individual characters and/or codepoints. ValueStringBuilder and OpenStringBuilder were created so we can easily call AsSpan() or AsMemory() to pass the memory location to our business logic without allocating a temporary buffer.