Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ private static string NormalizeFontFamilyReference(string fontFamilyReference, i
{
// No fragment separator. The entire string is a family name so convert to uppercase
// and add a fragment separator at the beginning.
return "#" + fontFamilyReference.Substring(startIndex, length).ToUpperInvariant();
return string.Concat("#", fontFamilyReference.AsSpan(startIndex, length)).ToUpperInvariant();
}
else if (fragmentIndex + 1 == startIndex + length)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,32 +798,32 @@ static private bool CheckContentRange(WebHeaderCollection responseHeaders, int b
}

// Get the first byte offset of the range (XXX)
int firstByteOffset = Int32.Parse(contentRange.Substring(ByteRangeUnit.Length,
int firstByteOffset = Int32.Parse(contentRange.AsSpan(ByteRangeUnit.Length,
index - ByteRangeUnit.Length),
NumberStyles.None, NumberFormatInfo.InvariantInfo);

contentRange = contentRange.Substring(index + 1);
ReadOnlySpan<char> contentRangeSpan = contentRange.AsSpan(index + 1);
// ContentRange: YYY/ZZZ
index = contentRange.IndexOf('/');
index = contentRangeSpan.IndexOf('/');

if (index == -1)
{
return false;
}

// Get the last byte offset of the range (YYY)
int lastByteOffset = Int32.Parse(contentRange.Substring(0, index), NumberStyles.None, NumberFormatInfo.InvariantInfo);
int lastByteOffset = Int32.Parse(contentRangeSpan.Slice(0, index), NumberStyles.None, NumberFormatInfo.InvariantInfo);

// Get the instance length
// ContentRange: ZZZ
contentRange = contentRange.Substring(index + 1);
if (String.CompareOrdinal(contentRange, "*") != 0)
contentRangeSpan = contentRangeSpan.Slice(index + 1);
if (!contentRangeSpan.Equals("*", StringComparison.Ordinal))
{
// Note: for firstByteOffset and lastByteOffset, we are using Int32.Parse to make sure Int32.Parse to throw
// if it is not an integer or the integer is bigger than Int32 since HttpWebRequest.AddRange
// only supports Int32
// Once HttpWebRequest.AddRange start supporting Int64 we should change it to Int64 and long
Int32.Parse(contentRange, NumberStyles.None, NumberFormatInfo.InvariantInfo);
Int32.Parse(contentRangeSpan, NumberStyles.None, NumberFormatInfo.InvariantInfo);
}

// The response is considered to be successful if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,21 +497,18 @@ private CultureInfo CreateTraditionalCulture(CultureInfo numberCulture, int firs
{
for (int i = 0; i < 10; ++i)
{
digits[i] = new string((char)(firstDigit + i), 1);
digits[i] = ((char)(firstDigit + i)).ToString();
}
}
else
{
Span<char> twoChars = stackalloc char[2];
for (int i = 0; i < 10; ++i)
{
int n = firstDigit + i - 0x10000;

digits[i] = new string(
new char[] {
(char)((n >> 10) | 0xD800), // high surrogate
(char)((n & 0x03FF) | 0xDC00) // low surrogate
}
);
twoChars[0] = (char)((n >> 10) | 0xD800); // high surrogate
twoChars[1] = (char)((n & 0x03FF) | 0xDC00); // low surrogate
digits[i] = new string(twoChars);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,7 @@
<NetCoreReference Include="System.Diagnostics.TraceSource" />
<NetCoreReference Include="System.Diagnostics.Tracing" />
<NetCoreReference Include="System.IO.FileSystem" />
<NetCoreReference Include="System.Memory" />
<NetCoreReference Include="System.Net.Primitives" />
<NetCoreReference Include="System.Net.Requests" />
<NetCoreReference Include="System.Net.WebHeaderCollection" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c

if (null != stringSource)
{
stringSource = stringSource.Trim();
ReadOnlySpan<char> spanSource = stringSource;
spanSource = spanSource.Trim();

if (-1 != stringSource.LastIndexOf('.'))
stringSource = stringSource.Substring(stringSource.LastIndexOf('.')+1);

if (!stringSource.Equals(String.Empty))
int periodPos = spanSource.LastIndexOf('.');
if (periodPos != -1)
{
sn = (InputScopeNameValue)Enum.Parse(typeof(InputScopeNameValue), stringSource);
spanSource = spanSource.Slice(periodPos + 1);
}

if (!spanSource.IsEmpty)
{
sn = Enum.Parse<InputScopeNameValue>(spanSource);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ namespace System.Windows
/// </summary>
public class KeyTimeConverter : TypeConverter
{
#region Data

private static char[] _percentCharacter = new char[] { '%' };

#endregion

/// <summary>
/// Returns whether or not this class can convert from a given type
/// to an instance of a KeyTime.
Expand Down Expand Up @@ -88,9 +82,9 @@ public override object ConvertFrom(
{
return KeyTime.Paced;
}
else if (stringValue[stringValue.Length - 1] == _percentCharacter[0])
else if (stringValue[stringValue.Length - 1] == '%')
{
stringValue = stringValue.TrimEnd(_percentCharacter);
stringValue = stringValue.TrimEnd('%');

double doubleValue = (double)TypeDescriptor.GetConverter(
typeof(double)).ConvertFrom(
Expand Down Expand Up @@ -195,7 +189,7 @@ public override object ConvertTo(
keyTime.Percent * 100.0,
destinationType);

return returnValue + _percentCharacter[0].ToString();
return string.Concat(returnValue, (ReadOnlySpan<char>)stackalloc char[] { '%' });

case KeyTimeType.TimeSpan:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1093,17 +1093,17 @@ internal void BuildInfo(System.Text.StringBuilder builder, int depth, bool inclu
if (AccelerationRatio != 0.0f)
{
builder.Append(", AccelerationRatio = ");
builder.Append(AccelerationRatio.ToString());
builder.Append(AccelerationRatio);
}
if (AutoReverse != false)
{
builder.Append(", AutoReverse = ");
builder.Append(AutoReverse.ToString());
builder.Append(AutoReverse);
}
if (DecelerationRatio != 0.0f)
{
builder.Append(", DecelerationRatio = ");
builder.Append(DecelerationRatio.ToString());
builder.Append(DecelerationRatio);
}
if (Duration != Duration.Automatic)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,15 @@ private static double[] ParseMetrics(string s)
if (k > i)
{
// Non-empty field; convert it to double.
string field = s.Substring(i, k - i);
ReadOnlySpan<char> field = s.AsSpan(i, k - i);
if (!double.TryParse(
field,
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS,
out metrics[fieldIndex]
))
{
throw new ArgumentException(SR.Get(SRID.CannotConvertStringToType, field, "double"));
throw new ArgumentException(SR.Get(SRID.CannotConvertStringToType, field.ToString(), "double"));
}
}
else if (fieldIndex < NumRequiredFields)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private void AddGlyph(int glyph, int sourceCharacter)
// remove trailing commas
RemoveTrailingCharacters(_glyphStringBuider, GlyphSubEntrySeparator);
_glyphStringBuider.Append(GlyphSeparator);
_indicesStringBuider.Append(_glyphStringBuider.ToString());
_indicesStringBuider.Append(_glyphStringBuider);

// reset for next glyph
_glyphStringBuider.Length = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ internal void Init(object value)
else if (value is char)
{
varType = (ushort)VarEnum.VT_LPSTR;
pszVal = Marshal.StringToCoTaskMemAnsi(new String(new char[] { (char)value }));
pszVal = Marshal.StringToCoTaskMemAnsi(new String(stackalloc char[] { (char)value }));
}
else if (type == typeof(short))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,7 @@ private static byte[] ParseAssemblyKey(string assemblyKey)
byte[] keyToken = new byte[byteCount];
for (int i = 0; i < byteCount; i++)
{
string byteString = assemblyKey.Substring(i * 2, 2);
keyToken[i] = byte.Parse(byteString, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
keyToken[i] = byte.Parse(assemblyKey.AsSpan(i * 2, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}

return keyToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,10 @@ private int SaveFileListToHandle(IntPtr handle, string[] files, bool doNotReallo
currentPtr = (IntPtr)((long)currentPtr + (files[i].Length * 2));

// Terminate the each of file string.
Marshal.Copy(new char[] { '\0' }, 0, currentPtr, 1);
unsafe
{
*(char*)currentPtr = '\0';
}

// Increase the current pointer by 2 since it is a unicode.
currentPtr = (IntPtr)((long)currentPtr + 2);
Expand All @@ -1948,7 +1951,10 @@ private int SaveFileListToHandle(IntPtr handle, string[] files, bool doNotReallo
#pragma warning restore 6523

// Terminate the string and add 2bytes since it is a unicode.
Marshal.Copy(new char[] { '\0' }, 0, currentPtr, 1);
unsafe
{
*(char*)currentPtr = '\0';
}
}
finally
{
Expand Down Expand Up @@ -2000,7 +2006,10 @@ private int SaveStringToHandle(IntPtr handle, string str, bool unicode, bool doN
// Terminate the string becasue of GlobalReAlloc GMEM_ZEROINIT will zero
// out only the bytes it adds to the memory object. It doesn't initialize
// any of the memory that existed before the call.
Marshal.Copy(new char[] { '\0' }, 0, (IntPtr)((ulong)ptr + (ulong)chars.Length * 2), 1);
unsafe
{
*(char*)(IntPtr)((ulong)ptr + (ulong)chars.Length * 2) = '\0';
}
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ private static string ConvertNumberToString(int number, bool oneBased, string nu

Invariant.Assert(number >= 0);

char[] result;
int b = numericSymbols.Length;
if (number < b)
{
// Optimize common case of single-digit numbers.
// Optimize common case of single-digit numbers.
result = new char[2]; // digit + suffix
result[0] = numericSymbols[number];
result[1] = NumberSuffix;
return new string(stackalloc char[2] // digit + suffix
{
numericSymbols[number],
NumberSuffix
});
}
else
{
Expand All @@ -190,17 +190,16 @@ private static string ConvertNumberToString(int number, bool oneBased, string nu
}

// Build string in reverse order starting with suffix.
// Build string in reverse order starting with suffix.
result = new char[digits + 1]; // digits + suffix
result[digits] = NumberSuffix;
for (int i = digits - 1; i >= 0; --i)
return string.Create(digits + 1, (numericSymbols, number, b, disjoint), (result, state) => // digits + suffix
{
result[i] = numericSymbols[number % b];
number = (number / b) - disjoint;
}
result[result.Length - 1] = NumberSuffix;
for (int i = result.Length - 2; i >= 0; --i)
{
state.number = Math.DivRem(state.number, state.b, out int remainder) - state.disjoint;
result[i] = state.numericSymbols[remainder];
}
});
}

return new string(result);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1464,23 +1464,24 @@ private bool ProcessFileNames()
// somehow slipped through.
//
// Strip out any extension that may be remaining and place the rest
// of the filename in s.
//
// Changed to use StringBuilder for perf reasons as per FxCop CA1818
StringBuilder s = new StringBuilder(fileName.Substring(0, fileName.Length - currentExtension.Length));
// we don't want to append the extension if it contains wild cards
if (extensions[j].IndexOfAny(new char[] { '*', '?' }) == -1)
// of the filename in s.

string newFilename;
if (((ReadOnlySpan<char>)extensions[j]).IndexOfAny('*', '?') != -1)
{
// we don't want to append the extension if it contains wild cards
newFilename = fileName.Substring(0, fileName.Length - currentExtension.Length);
}
else
{
// No wildcards, so go ahead and append
s.Append(".");
s.Append(extensions[j]);
newFilename = string.Concat(fileName.AsSpan(0, fileName.Length - currentExtension.Length), ".", extensions[j]);
}

// If OFN_FILEMUSTEXIST is not set, or if it is set but the filename we generated
// does in fact exist, we update fileName and stop trying new extensions.
if (!GetOption(NativeMethods.OFN_FILEMUSTEXIST) || File.Exists(s.ToString()))
if (!GetOption(NativeMethods.OFN_FILEMUSTEXIST) || File.Exists(newFilename))
{
fileName = s.ToString();
fileName = newFilename;
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,7 @@
<NetCoreReference Include="System.IO.FileSystem" />
<NetCoreReference Include="System.IO.Compression" />
<NetCoreReference Include="System.IO.IsolatedStorage" />
<NetCoreReference Include="System.Memory" />
<NetCoreReference Include="System.Net.Primitives" />
<NetCoreReference Include="System.Net.Requests" />
<NetCoreReference Include="System.Linq" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ private void TextUpdated(string newText, bool textBoxUpdated)
if (ShouldPreserveUserEnteredPrefix)
{
// Retain the user entered prefix in the matched text.
matchedText = String.Concat(newText, matchedText.Substring(matchedTextInfo.MatchedPrefixLength));
matchedText = String.Concat(newText, matchedText.AsSpan(matchedTextInfo.MatchedPrefixLength));
}

// If there's an IME, do the replacement asynchronously so that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ private static DataGridLength ConvertFromString(string s, CultureInfo cultureInf
(unit == DataGridLengthUnitType.Pixel) || DoubleUtil.AreClose(unitFactor, 1.0),
"unitFactor should not be other than 1.0 unless the unit type is Pixel.");

string valueString = goodString.Substring(0, strLen - strLenUnit);
value = Convert.ToDouble(valueString, cultureInfo) * unitFactor;
ReadOnlySpan<char> valueString = goodString.AsSpan(0, strLen - strLenUnit);
value = double.Parse(valueString, provider: cultureInfo) * unitFactor;
}

return new DataGridLength(value, unit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1130,9 +1130,9 @@ void FormatCollectionChangedSource(int level, object source, bool? isLikely, Lis
int index = aqn.LastIndexOf(PublicKeyToken);
if (index >= 0)
{
string token = aqn.Substring(index + PublicKeyToken.Length);
if (String.Compare(token, MS.Internal.PresentationFramework.BuildInfo.WCP_PUBLIC_KEY_TOKEN, StringComparison.OrdinalIgnoreCase) == 0 ||
String.Compare(token, MS.Internal.PresentationFramework.BuildInfo.DEVDIV_PUBLIC_KEY_TOKEN, StringComparison.OrdinalIgnoreCase) == 0)
ReadOnlySpan<char> token = aqn.AsSpan(index + PublicKeyToken.Length);
if (token.Equals(MS.Internal.PresentationFramework.BuildInfo.WCP_PUBLIC_KEY_TOKEN, StringComparison.OrdinalIgnoreCase) ||
token.Equals(MS.Internal.PresentationFramework.BuildInfo.DEVDIV_PUBLIC_KEY_TOKEN, StringComparison.OrdinalIgnoreCase))
{
isLikely = false;
}
Expand Down
Loading