diff options
author | Santiago Fernandez Madero <safern@microsoft.com> | 2019-03-26 09:27:32 -0700 |
---|---|---|
committer | Santiago Fernandez Madero <safern@microsoft.com> | 2019-03-27 17:31:48 -0700 |
commit | fe73f198d553334838bfeeb9e33e9b58a0ea4c67 (patch) | |
tree | 3b6e0a9c1e9d1048661ada4f493cc4050ebb18af /src/System.Private.CoreLib/shared/System/String.Manipulation.cs | |
parent | 626633d6c52cbc91c187def2e547e3ffdbe7e095 (diff) | |
download | coreclr-fe73f198d553334838bfeeb9e33e9b58a0ea4c67.tar.gz coreclr-fe73f198d553334838bfeeb9e33e9b58a0ea4c67.tar.bz2 coreclr-fe73f198d553334838bfeeb9e33e9b58a0ea4c67.zip |
Nullable: String (#23450)
Diffstat (limited to 'src/System.Private.CoreLib/shared/System/String.Manipulation.cs')
-rw-r--r-- | src/System.Private.CoreLib/shared/System/String.Manipulation.cs | 127 |
1 files changed, 64 insertions, 63 deletions
diff --git a/src/System.Private.CoreLib/shared/System/String.Manipulation.cs b/src/System.Private.CoreLib/shared/System/String.Manipulation.cs index 82d74225c2..bd56d0c33a 100644 --- a/src/System.Private.CoreLib/shared/System/String.Manipulation.cs +++ b/src/System.Private.CoreLib/shared/System/String.Manipulation.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Buffers; using System.Collections.Generic; using System.Diagnostics; @@ -33,7 +34,7 @@ namespace System } } - public static string Concat(object arg0) + public static string Concat(object? arg0) { if (arg0 == null) { @@ -42,7 +43,7 @@ namespace System return arg0.ToString(); } - public static string Concat(object arg0, object arg1) + public static string Concat(object? arg0, object? arg1) { if (arg0 == null) { @@ -56,7 +57,7 @@ namespace System return Concat(arg0.ToString(), arg1.ToString()); } - public static string Concat(object arg0, object arg1, object arg2) + public static string Concat(object? arg0, object? arg1, object? arg2) { if (arg0 == null) { @@ -76,7 +77,7 @@ namespace System return Concat(arg0.ToString(), arg1.ToString(), arg2.ToString()); } - public static string Concat(params object[] args) + public static string Concat(params object?[] args) { if (args == null) { @@ -105,7 +106,7 @@ namespace System for (int i = 0; i < args.Length; i++) { - object value = args[i]; + object? value = args[i]; string toString = value?.ToString() ?? string.Empty; // We need to handle both the cases when value or value.ToString() is null strings[i] = toString; @@ -195,7 +196,7 @@ namespace System // Everything should be called in the order // MoveNext-Current-ToString, unless further optimizations // can be made, to avoid breaking changes - string firstString = currentValue?.ToString(); + string? firstString = currentValue?.ToString(); // If there's only 1 item, simply call ToString on that if (!en.MoveNext()) @@ -225,17 +226,17 @@ namespace System } } - public static string Concat(IEnumerable<string> values) + public static string Concat(IEnumerable<string?> values) { if (values == null) throw new ArgumentNullException(nameof(values)); - using (IEnumerator<string> en = values.GetEnumerator()) + using (IEnumerator<string?> en = values.GetEnumerator()) { if (!en.MoveNext()) return string.Empty; - string firstValue = en.Current; + string? firstValue = en.Current; if (!en.MoveNext()) { @@ -255,7 +256,7 @@ namespace System } } - public static string Concat(string str0, string str1) + public static string Concat(string? str0, string? str1) { if (IsNullOrEmpty(str0)) { @@ -281,7 +282,7 @@ namespace System return result; } - public static string Concat(string str0, string str1, string str2) + public static string Concat(string? str0, string? str1, string? str2) { if (IsNullOrEmpty(str0)) { @@ -308,7 +309,7 @@ namespace System return result; } - public static string Concat(string str0, string str1, string str2, string str3) + public static string Concat(string? str0, string? str1, string? str2, string? str3) { if (IsNullOrEmpty(str0)) { @@ -405,7 +406,7 @@ namespace System return result; } - public static string Concat(params string[] values) + public static string Concat(params string?[] values) { if (values == null) throw new ArgumentNullException(nameof(values)); @@ -428,7 +429,7 @@ namespace System long totalLengthLong = 0; for (int i = 0; i < values.Length; i++) { - string value = values[i]; + string? value = values[i]; if (value != null) { totalLengthLong += value.Length; @@ -451,7 +452,7 @@ namespace System int copiedLength = 0; for (int i = 0; i < values.Length; i++) { - string value = values[i]; + string? value = values[i]; if (!string.IsNullOrEmpty(value)) { int valueLen = value.Length; @@ -470,25 +471,25 @@ namespace System // something changed concurrently to mutate the input array: fall back to // doing the concatenation again, but this time with a defensive copy. This // fall back should be extremely rare. - return copiedLength == totalLength ? result : Concat((string[])values.Clone()); + return copiedLength == totalLength ? result : Concat((string?[])values.Clone()); } - public static string Format(string format, object arg0) + public static string Format(string format, object? arg0) { return FormatHelper(null, format, new ParamsArray(arg0)); } - public static string Format(string format, object arg0, object arg1) + public static string Format(string format, object? arg0, object? arg1) { return FormatHelper(null, format, new ParamsArray(arg0, arg1)); } - public static string Format(string format, object arg0, object arg1, object arg2) + public static string Format(string format, object? arg0, object? arg1, object? arg2) { return FormatHelper(null, format, new ParamsArray(arg0, arg1, arg2)); } - public static string Format(string format, params object[] args) + public static string Format(string format, params object?[] args) { if (args == null) { @@ -500,22 +501,22 @@ namespace System return FormatHelper(null, format, new ParamsArray(args)); } - public static string Format(IFormatProvider provider, string format, object arg0) + public static string Format(IFormatProvider? provider, string format, object? arg0) { return FormatHelper(provider, format, new ParamsArray(arg0)); } - public static string Format(IFormatProvider provider, string format, object arg0, object arg1) + public static string Format(IFormatProvider? provider, string format, object? arg0, object? arg1) { return FormatHelper(provider, format, new ParamsArray(arg0, arg1)); } - public static string Format(IFormatProvider provider, string format, object arg0, object arg1, object arg2) + public static string Format(IFormatProvider? provider, string format, object? arg0, object? arg1, object? arg2) { return FormatHelper(provider, format, new ParamsArray(arg0, arg1, arg2)); } - public static string Format(IFormatProvider provider, string format, params object[] args) + public static string Format(IFormatProvider? provider, string format, params object?[] args) { if (args == null) { @@ -527,7 +528,7 @@ namespace System return FormatHelper(provider, format, new ParamsArray(args)); } - private static string FormatHelper(IFormatProvider provider, string format, ParamsArray args) + private static string FormatHelper(IFormatProvider? provider, string format, ParamsArray args) { if (format == null) throw new ArgumentNullException(nameof(format)); @@ -574,7 +575,7 @@ namespace System return result; } - public static string Join(char separator, params string[] value) + public static string Join(char separator, params string?[] value) { if (value == null) { @@ -584,7 +585,7 @@ namespace System return Join(separator, value, 0, value.Length); } - public static unsafe string Join(char separator, params object[] values) + public static unsafe string Join(char separator, params object?[] values) { // Defer argument validation to the internal function return JoinCore(&separator, 1, values); @@ -596,7 +597,7 @@ namespace System return JoinCore(&separator, 1, values); } - public static unsafe string Join(char separator, string[] value, int startIndex, int count) + public static unsafe string Join(char separator, string?[] value, int startIndex, int count) { // Defer argument validation to the internal function return JoinCore(&separator, 1, value, startIndex, count); @@ -604,7 +605,7 @@ namespace System // Joins an array of strings together as one string with a separator between each original string. // - public static string Join(string separator, params string[] value) + public static string Join(string? separator, params string?[] value) { if (value == null) { @@ -613,7 +614,7 @@ namespace System return Join(separator, value, 0, value.Length); } - public static unsafe string Join(string separator, params object[] values) + public static unsafe string Join(string? separator, params object?[] values) { separator = separator ?? string.Empty; fixed (char* pSeparator = &separator._firstChar) @@ -623,7 +624,7 @@ namespace System } } - public static unsafe string Join<T>(string separator, IEnumerable<T> values) + public static unsafe string Join<T>(string? separator, IEnumerable<T> values) { separator = separator ?? string.Empty; fixed (char* pSeparator = &separator._firstChar) @@ -633,7 +634,7 @@ namespace System } } - public static string Join(string separator, IEnumerable<string> values) + public static string Join(string? separator, IEnumerable<string> values) { if (values == null) { @@ -672,7 +673,7 @@ namespace System // Joins an array of strings together as one string with a separator between each original string. // - public static unsafe string Join(string separator, string[] value, int startIndex, int count) + public static unsafe string Join(string? separator, string?[] value, int startIndex, int count) { separator = separator ?? string.Empty; fixed (char* pSeparator = &separator._firstChar) @@ -682,7 +683,7 @@ namespace System } } - private static unsafe string JoinCore(char* separator, int separatorLength, object[] values) + private static unsafe string JoinCore(char* separator, int separatorLength, object?[] values) { if (values == null) { @@ -694,7 +695,7 @@ namespace System return string.Empty; } - string firstString = values[0]?.ToString(); + string? firstString = values[0]?.ToString(); if (values.Length == 1) { @@ -707,7 +708,7 @@ namespace System for (int i = 1; i < values.Length; i++) { result.Append(separator, separatorLength); - object value = values[i]; + object? value = values[i]; if (value != null) { result.Append(value.ToString()); @@ -739,7 +740,7 @@ namespace System // Everything should be called in the order // MoveNext-Current-ToString, unless further optimizations // can be made, to avoid breaking changes - string firstString = currentValue?.ToString(); + string? firstString = currentValue?.ToString(); // If there's only 1 item, simply call ToString on that if (!en.MoveNext()) @@ -769,7 +770,7 @@ namespace System } } - private static unsafe string JoinCore(char* separator, int separatorLength, string[] value, int startIndex, int count) + private static unsafe string JoinCore(char* separator, int separatorLength, string?[] value, int startIndex, int count) { // If the separator is null, it is converted to an empty string before entering this function. // Even for empty strings, fixed should never return null (it should return a pointer to a null char). @@ -810,7 +811,7 @@ namespace System // Calculate the length of the resultant string so we know how much space to allocate. for (int i = startIndex, end = startIndex + count; i < end; i++) { - string currentValue = value[i]; + string? currentValue = value[i]; if (currentValue != null) { totalLength += currentValue.Length; @@ -833,7 +834,7 @@ namespace System // We range check again to avoid buffer overflows if this happens. - string currentValue = value[i]; + string? currentValue = value[i]; if (currentValue != null) { int valueLen = currentValue.Length; @@ -875,7 +876,7 @@ namespace System // fall back should be extremely rare. return copiedLength == totalLength ? result : - JoinCore(separator, separatorLength, (string[])value.Clone(), startIndex, count); + JoinCore(separator, separatorLength, (string?[])value.Clone(), startIndex, count); } public string PadLeft(int totalWidth) => PadLeft(totalWidth, ' '); @@ -973,12 +974,12 @@ namespace System return Substring(0, startIndex); } - public string Replace(string oldValue, string newValue, bool ignoreCase, CultureInfo culture) + public string Replace(string oldValue, string? newValue, bool ignoreCase, CultureInfo? culture) { return ReplaceCore(oldValue, newValue, culture, ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None); } - public string Replace(string oldValue, string newValue, StringComparison comparisonType) + public string Replace(string oldValue, string? newValue, StringComparison comparisonType) { switch (comparisonType) { @@ -1001,7 +1002,7 @@ namespace System } } - private unsafe string ReplaceCore(string oldValue, string newValue, CultureInfo culture, CompareOptions options) + private unsafe string ReplaceCore(string oldValue, string? newValue, CultureInfo? culture, CompareOptions options) { if (oldValue == null) throw new ArgumentNullException(nameof(oldValue)); @@ -1121,7 +1122,7 @@ namespace System } } - public string Replace(string oldValue, string newValue) + public string Replace(string oldValue, string? newValue) { if (oldValue == null) throw new ArgumentNullException(nameof(oldValue)); @@ -1232,7 +1233,7 @@ namespace System // If the separator is null // whitespace (i.e., Character.IsWhitespace) is used as the separator. // - public string[] Split(params char[] separator) + public string[] Split(params char[]? separator) { return SplitInternal(separator, int.MaxValue, StringSplitOptions.None); } @@ -1248,17 +1249,17 @@ namespace System // If there are more than count different strings, the last n-(count-1) // elements are concatenated and added as the last string. // - public string[] Split(char[] separator, int count) + public string[] Split(char[]? separator, int count) { return SplitInternal(separator, count, StringSplitOptions.None); } - public string[] Split(char[] separator, StringSplitOptions options) + public string[] Split(char[]? separator, StringSplitOptions options) { return SplitInternal(separator, int.MaxValue, options); } - public string[] Split(char[] separator, int count, StringSplitOptions options) + public string[] Split(char[]? separator, int count, StringSplitOptions options) { return SplitInternal(separator, count, options); } @@ -1305,27 +1306,27 @@ namespace System return result; } - public string[] Split(string separator, StringSplitOptions options = StringSplitOptions.None) + public string[] Split(string? separator, StringSplitOptions options = StringSplitOptions.None) { return SplitInternal(separator ?? string.Empty, null, int.MaxValue, options); } - public string[] Split(string separator, int count, StringSplitOptions options = StringSplitOptions.None) + public string[] Split(string? separator, int count, StringSplitOptions options = StringSplitOptions.None) { return SplitInternal(separator ?? string.Empty, null, count, options); } - public string[] Split(string[] separator, StringSplitOptions options) + public string[] Split(string[]? separator, StringSplitOptions options) { return SplitInternal(null, separator, int.MaxValue, options); } - public string[] Split(string[] separator, int count, StringSplitOptions options) + public string[] Split(string[]? separator, int count, StringSplitOptions options) { return SplitInternal(null, separator, count, options); } - private string[] SplitInternal(string separator, string[] separators, int count, StringSplitOptions options) + private string[] SplitInternal(string? separator, string?[]? separators, int count, StringSplitOptions options) { if (count < 0) { @@ -1344,7 +1345,7 @@ namespace System if (!singleSeparator && (separators == null || separators.Length == 0)) { - return SplitInternal((char[])null, count, options); + return SplitInternal(default(ReadOnlySpan<char>), count, options); } if ((count == 0) || (omitEmptyEntries && Length == 0)) @@ -1352,14 +1353,14 @@ namespace System return Array.Empty<string>(); } - if (count == 1 || (singleSeparator && separator.Length == 0)) + if (count == 1 || (singleSeparator && separator!.Length == 0)) { return new string[] { this }; } if (singleSeparator) { - return SplitInternal(separator, count, options); + return SplitInternal(separator!, count, options); } Span<int> sepListInitialSpan = stackalloc int[StackallocIntBufferSizeLimit]; @@ -1368,7 +1369,7 @@ namespace System Span<int> lengthListInitialSpan = stackalloc int[StackallocIntBufferSizeLimit]; var lengthListBuilder = new ValueListBuilder<int>(lengthListInitialSpan); - MakeSeparatorList(separators, ref sepListBuilder, ref lengthListBuilder); + MakeSeparatorList(separators!, ref sepListBuilder, ref lengthListBuilder); ReadOnlySpan<int> sepList = sepListBuilder.AsSpan(); ReadOnlySpan<int> lengthList = lengthListBuilder.AsSpan(); @@ -1616,7 +1617,7 @@ namespace System /// <param name="separators">separator strngs</param> /// <param name="sepListBuilder"><see cref="ValueListBuilder{T}"/> for separator indexes</param> /// <param name="lengthListBuilder"><see cref="ValueListBuilder{T}"/> for separator length values</param> - private void MakeSeparatorList(string[] separators, ref ValueListBuilder<int> sepListBuilder, ref ValueListBuilder<int> lengthListBuilder) + private void MakeSeparatorList(string?[] separators, ref ValueListBuilder<int> sepListBuilder, ref ValueListBuilder<int> lengthListBuilder) { Debug.Assert(separators != null && separators.Length > 0, "separators != null && separators.Length > 0"); @@ -1626,7 +1627,7 @@ namespace System { for (int j = 0; j < separators.Length; j++) { - string separator = separators[j]; + string? separator = separators[j]; if (IsNullOrEmpty(separator)) { continue; @@ -1768,7 +1769,7 @@ namespace System public unsafe string Trim(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Both); // Removes a set of characters from the beginning and end of this string. - public unsafe string Trim(params char[] trimChars) + public unsafe string Trim(params char[]? trimChars) { if (trimChars == null || trimChars.Length == 0) { @@ -1787,7 +1788,7 @@ namespace System public unsafe string TrimStart(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Head); // Removes a set of characters from the beginning of this string. - public unsafe string TrimStart(params char[] trimChars) + public unsafe string TrimStart(params char[]? trimChars) { if (trimChars == null || trimChars.Length == 0) { @@ -1806,7 +1807,7 @@ namespace System public unsafe string TrimEnd(char trimChar) => TrimHelper(&trimChar, 1, TrimType.Tail); // Removes a set of characters from the end of this string. - public unsafe string TrimEnd(params char[] trimChars) + public unsafe string TrimEnd(params char[]? trimChars) { if (trimChars == null || trimChars.Length == 0) { |