Skip to content

Flow System.Text.Rune through more APIs #27912

@GrabYourPitchforks

Description

@GrabYourPitchforks

System.Text.Rune was recently introduced (see dotnet/coreclr#20935) to make it easier for applications to work with Unicode text. We should do a pass of our APIs which currently take or return System.Char instances and enlighten them to understand System.Text.Rune. Here's an initial proposal that can serve as a first pass.

namespace System
{
    // Adds overloads to existing char-consuming methods
    // TODO: Do we need overloads of MemoryExtensions methods? May cause overload explosion.
    // If we had such overloads, they'd basically look like the below, but with a "this ROS<char>" parameter.
    public partial sealed class String
    {
        public bool Contains(Rune value);
        public bool Contains(Rune value, StringComparison comparisonType);

        public bool EndsWith(Rune value);
        public bool EndsWith(Rune value, StringComparison comparisonType);
        public bool EndsWith(char value, StringComparison comparisonType); // missing overload

        public int IndexOf(Rune value);
        public int IndexOf(Rune value, int startIndex);
        public int IndexOf(Rune value, int startIndex, int count);
        public int IndexOf(Rune value, StringComparison comparisonType);

        // no IndexOfAny overloads for now due to complexity of implementation, can add later if there's demand
        // no Join overloads for now, can add later if there's demand

        public int LastIndexOf(Rune value);
        public int LastIndexOf(Rune value, int startIndex);
        public int LastIndexOf(Rune value, int startIndex, int count);
        public int LastIndexOf(Rune value, StringComparison comparisonType);

        public string Replace(Rune oldRune, Rune newRune);

        public string[] Split(Rune separator, int count, StringSplitOptions options = default);
        public string[] Split(Rune separator, StringSplitOptions options = default);

        public bool StartsWith(Rune value);
        public bool StartsWith(Rune value, StringComparison comparisonType);
        public bool StartsWith(char value, StringComparison comparisonType); // missing overload

        // no Trim*(Rune[]) overloads for now due to complexity of implementation, can add later if there's demand
        public string Trim(Rune trimRune);
        public string TrimEnd(Rune trimRune);
        public string TrimStart(Rune trimRune);
    }
}

namespace System.Text
{
    // New methods on newly-introduced type
    public partial struct Rune
    {
        public static bool Equals(Rune left, Rune right); // mimics (left == right), ordinal
        public static bool Equals(Rune left, Rune right, StringComparison comparisonType); // for any non-ordinal comparison
    }

    // Adds overloads to existing char-consuming methods
    // Not all methods get overloads (e.g., AppendJoin) due to perceived rare usage and complexity of implementation
    public partial sealed class StringBuilder
    {
        public StringBuilder Append(Rune value);

        public Rune GetRuneAt(int index); // new method to match String, not an overload of an existing method

        public StringBuilder Insert(int index, Rune value);

        public StringBuilder Replace(Rune oldRune, Rune newRune);
        public StringBuilder Replace(Rune oldRune, Rune newRune, int startIndex, int count);

        public bool TryGetRuneAt(int index, out Rune value); // new method, not overload of existing method
    }
}

namespace System.IO
{
    // Adds overloads to existing char-consuming methods
    public partial abstract class TextWriter
    {
        public virtual void Write(Rune value);

        public virtual Task WriteAsync(Rune value);

        public virtual void WriteLine(Rune value);

        public virtual Task WriteLineAsync(Rune value);
    }
}

namespace System.Globalization
{
    public partial class TextInfo
    {
        public Rune ToLower(Rune r);
        public Rune ToUpper(Rune r);
    }
}

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions