Skip to content

[API Proposal]: ReadOnlySpan<char> overloads for StringNormalizationExtensions #87757

Closed

Description

@udaken

Edited by @tarekgh

Background and motivation

If we try to verify that the string represented by ReadOnlySpan<char> is Unicode normalized, we currently have to turn it into a string once.
Overloading for ReadOnlySpan<char> is needed to reduce allocation.

API Proposal

namespace System;

// Doesn't throw on the operation even when having wrong character sequence. 

public static class StringNormalizationExtensions {
+    public static bool IsNormalized(this ReadOnlySpan<char> source, NormalizationForm normalizationForm = NormalizationForm.FormC); // will return false in invalid sequence
+    public static OperationStatus TryNormalize(this ReadOnlySpan<char> source, Span<char> destination, out int charsWritten, NormalizationForm normalizationForm = NormalizationForm.FormC);
}

Original Proposal - Alternative Proposal

namespace System;

// throws on wrong sequence as String.Normalize does. 

public static class StringNormalizationExtensions {
+    public static bool IsNormalized(this ReadOnlySpan<char> strInput, NormalizationForm normalizationForm = NormalizationForm.FormC);
+    public static bool TryNormalize(this ReadOnlySpan<char> strInput, Span<char> destination, out int charsWritten, NormalizationForm normalizationForm = NormalizationForm.FormC);
}

API Usage

ReadOnlySpan<char> partOfhugeArray =  hugeArray.AsSpan(start, length);
if (!partOfhugeArray.IsNormalized())
    throw new Exception("Text must be Unicode normalized.");
/// ...

No response

Risks

No response

Activity

  1. added
    api-suggestion
    on Jun 19, 2023
  2. ghost added
    untriaged
    on Jun 19, 2023
  3. changed the title [-][API Proposal]: [/-] [+][API Proposal]: `ReadOnlySpan<char>` overloads for `StringNormalizationExtensions`[/+] on Jun 19, 2023
  4. added this to the Future milestone on Jun 19, 2023
  5. ghost removed
    untriaged
    on Jun 19, 2023
  6. modified the milestones: Future, 10.0.0 on Nov 18, 2024
  7. tarekgh commented on Nov 18, 2024

    @tarekgh
    Member

    @udaken I made a few edits to the proposal and hope that works for you.

  8. added
    api-ready-for-review
    blocking
    on Nov 19, 2024
  9. bartonjs commented on Nov 19, 2024

    @bartonjs
    Member

    Video

    • TryNormalize as proposed mixes semantics of OperationStatus and the Try pattern, and needs to pick one or the other.
      • We went with Try
    • We added GetNormalizedLength for callers who are concerned about too many double-and-call-again for the Try-Write.
    namespace System;
    
    public static partial class StringNormalizationExtensions
    {
        public static bool IsNormalized(this ReadOnlySpan<char> source, NormalizationForm normalizationForm = NormalizationForm.FormC);
        public static bool TryNormalize(this ReadOnlySpan<char> source, Span<char> destination, out int charsWritten, NormalizationForm normalizationForm = NormalizationForm.FormC);
        public static int GetNormalizedLength(this ReadOnlySpan<char> source, NormalizationForm normalizationForm = NormalizationForm.FormC);
    }
  10. added
    api-approved
    and removed
    api-suggestion
    api-ready-for-review
    blocking
    on Nov 19, 2024
  11. added
    in-pr
    on Dec 6, 2024
  12. locked and limited conversation to collaborators on Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Globalizationin-pr

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions