private static Rune ChangeCaseCultureAware(Rune rune, TextInfo textInfo, bool toUpper) { Debug.Assert(!GlobalizationMode.Invariant, "This should've been checked by the caller."); Debug.Assert(textInfo != null, "This should've been checked by the caller."); Span <char> original = stackalloc char[2]; // worst case scenario = 2 code units (for a surrogate pair) Span <char> modified = stackalloc char[2]; // case change should preserve UTF-16 code unit count int charCount = rune.EncodeToUtf16(original); original = original.Slice(0, charCount); modified = modified.Slice(0, charCount); if (toUpper) { textInfo.ChangeCaseToUpper(original, modified); } else { textInfo.ChangeCaseToLower(original, modified); } // We use simple case folding rules, which disallows moving between the BMP and supplementary // planes when performing a case conversion. The helper methods which reconstruct a Rune // contain debug asserts for this condition. if (rune.IsBmp) { return(UnsafeCreate(modified[0])); } else { return(UnsafeCreate(UnicodeUtility.GetScalarFromUtf16SurrogatePair(modified[0], modified[1]))); } }
/// <summary> /// Copies the characters from the source span into the destination, converting each character to uppercase, /// using the casing rules of the specified culture. /// </summary> /// <param name="source">The source span.</param> /// <param name="destination">The destination span which contains the transformed characters.</param> /// <param name="culture">An object that supplies culture-specific casing rules.</param> /// <remarks>If <paramref name="culture"/> is null, <see cref="System.Globalization.CultureInfo.CurrentCulture"/> will be used.</remarks> /// <returns>The number of characters written into the destination span. If the destination is too small, returns -1.</returns> /// <exception cref="InvalidOperationException">The source and destination buffers overlap.</exception> public static int ToUpper(this ReadOnlySpan <char> source, Span <char> destination, CultureInfo?culture) { if (source.Overlaps(destination)) { throw new InvalidOperationException(SR.InvalidOperation_SpanOverlappedOperation); } if (culture == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.culture); } // Assuming that changing case does not affect length if (destination.Length < source.Length) { return(-1); } if (GlobalizationMode.Invariant) { TextInfo.ToUpperAsciiInvariant(source, destination); } else { culture !.TextInfo.ChangeCaseToUpper(source, destination); // TODO-NULLABLE: Remove ! when [DoesNotReturn] respected } return(source.Length); }
/// <summary> /// Copies the characters from the source span into the destination, converting each character to uppercase, /// using the casing rules of the specified culture. /// </summary> /// <param name="source">The source span.</param> /// <param name="destination">The destination span which contains the transformed characters.</param> /// <param name="culture">An object that supplies culture-specific casing rules.</param> /// <remarks>If the source and destinations overlap, this method behaves as if the original values are in /// a temporary location before the destination is overwritten.</remarks> /// <returns>The number of characters written into the destination span. If the destination is too small, returns -1.</returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="culture"/> is null. /// </exception> public static int ToUpper(this ReadOnlySpan <char> source, Span <char> destination, CultureInfo culture) { if (culture == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.culture); } // Assuming that changing case does not affect length if (destination.Length < source.Length) { return(-1); } if (GlobalizationMode.Invariant) { TextInfo.ToUpperAsciiInvariant(source, destination); } else { culture !.TextInfo.ChangeCaseToUpper(source, destination); // TODO-NULLABLE: https://github.com/dotnet/csharplang/issues/538 } return(source.Length); }