<Reference Include="System.Drawing.Primitives" />
<Reference Include="System.IO.FileSystem" />
<Reference Include="System.Linq" />
+ <Reference Include="System.Memory" />
<Reference Include="System.Net.Primitives" />
<Reference Include="System.Net.Security" />
<Reference Include="System.ObjectModel" />
if (commaIndex != -1)
{
- className = _tabClassNames[i].Substring(0, commaIndex).Trim();
- assemblyName = _tabClassNames[i].Substring(commaIndex + 1).Trim();
+ className = _tabClassNames[i].AsSpan(0, commaIndex).Trim().ToString();
+ assemblyName = _tabClassNames[i].AsSpan(commaIndex + 1).Trim().ToString();
}
else
{
bool descending = false;
if (length >= 5 && string.Compare(current, length - 4, " ASC", 0, 4, StringComparison.OrdinalIgnoreCase) == 0)
{
- current = current.Substring(0, length - 4).Trim();
+ current = current.AsSpan(0, length - 4).Trim().ToString();
}
else if (length >= 6 && string.Compare(current, length - 5, " DESC", 0, 5, StringComparison.OrdinalIgnoreCase) == 0)
{
descending = true;
- current = current.Substring(0, length - 5).Trim();
+ current = current.AsSpan(0, length - 5).Trim().ToString();
}
// handle brackets.
if (j < 0 || j == i + 1)
return null;
- attrValue = headerValue.Substring(i + 1, j - i - 1).Trim();
+ attrValue = headerValue.AsSpan(i + 1, j - i - 1).Trim().ToString();
}
else
{
if (j == i)
return null;
- attrValue = headerValue.Substring(i, j - i).Trim();
+ attrValue = headerValue.AsSpan(i, j - i).Trim().ToString();
}
return attrValue;
return;
}
- string name = header.Substring(0, colon).Trim();
- string val = header.Substring(colon + 1).Trim();
+ string name = header.AsSpan(0, colon).Trim().ToString();
+ string val = header.AsSpan(colon + 1).Trim().ToString();
if (name.Equals("content-length", StringComparison.OrdinalIgnoreCase))
{
// To match Windows behavior:
{
_host = uri.Substring(start_host, colon - start_host);
root = uri.IndexOf('/', colon, length - colon);
- _port = (ushort)int.Parse(uri.Substring(colon + 1, root - colon - 1));
+ _port = (ushort)int.Parse(uri.AsSpan(colon + 1, root - colon - 1));
_path = uri.Substring(root);
}
else
// TryParse will parse in base 10 by default. do not allow any styles of input beyond the default
// which is numeric values only
- if (!int.TryParse(offset.Substring(1, 2), NumberStyles.None, CultureInfo.InvariantCulture, out hours))
+ if (!int.TryParse(offset.AsSpan(1, 2), NumberStyles.None, CultureInfo.InvariantCulture, out hours))
{
throw new FormatException(SR.MailDateInvalidFormat);
}
- if (!int.TryParse(offset.Substring(3, 2), NumberStyles.None, CultureInfo.InvariantCulture, out minutes))
+ if (!int.TryParse(offset.AsSpan(3, 2), NumberStyles.None, CultureInfo.InvariantCulture, out minutes))
{
throw new FormatException(SR.MailDateInvalidFormat);
}
}
// extract the time portion and remove all leading and trailing whitespace
- string date = data.Substring(0, indexOfTimeZoneSeparator).Trim();
+ string date = data.AsSpan(0, indexOfTimeZoneSeparator).Trim().ToString();
// attempt to parse the DateTime component.
DateTime dateValue;
// extract the second half of the string. This will start with at least one whitespace character.
// Trim the string to remove these characters.
- string timeZoneString = data.Substring(indexOfTimeZoneSeparator).Trim();
+ string timeZoneString = data.AsSpan(indexOfTimeZoneSeparator).Trim().ToString();
// find, if any, the first whitespace character after the timezone.
// These will be CFWS and must be ignored. Remove them.
{
pos1++;
long result;
- if (long.TryParse(str.Substring(pos1, pos2 - pos1),
+ if (long.TryParse(str.AsSpan(pos1, pos2 - pos1),
NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite,
NumberFormatInfo.InvariantInfo, out result))
{
//length. since j points to the next ; the operation j -i
//gives the length of the charset
if (j > i)
- _characterSet = contentType.Substring(i, j - i).Trim();
+ _characterSet = contentType.AsSpan(i, j - i).Trim().ToString();
else
- _characterSet = contentType.Substring(i).Trim();
+ _characterSet = contentType.AsSpan(i).Trim().ToString();
//done
break;
<Reference Include="System.Collections.NonGeneric" />
<Reference Include="System.Collections.Specialized" />
<Reference Include="System.Linq" />
+ <Reference Include="System.Memory" />
<Reference Include="System.Reflection.Emit.ILGeneration" />
<Reference Include="System.Reflection.Emit.Lightweight" />
<Reference Include="System.Reflection.Primitives" />
startIndex = typeName.IndexOf('.', endIndex);
if (startIndex < 0)
{
- nestedParamCounts.Add(int.Parse(typeName.Substring(endIndex + 1), CultureInfo.InvariantCulture));
+ nestedParamCounts.Add(int.Parse(typeName.AsSpan(endIndex + 1), provider: CultureInfo.InvariantCulture));
break;
}
else
- nestedParamCounts.Add(int.Parse(typeName.Substring(endIndex + 1, startIndex - endIndex - 1), CultureInfo.InvariantCulture));
+ nestedParamCounts.Add(int.Parse(typeName.AsSpan(endIndex + 1, startIndex - endIndex - 1), provider: CultureInfo.InvariantCulture));
}
if (localName != null)
localName.Append("Of");
else
{
int paramIndex;
- if (!int.TryParse(format.Substring(start, i - start), out paramIndex) || paramIndex < 0 || paramIndex >= genericNameProvider.GetParameterCount())
+ if (!int.TryParse(format.AsSpan(start, i - start), out paramIndex) || paramIndex < 0 || paramIndex >= genericNameProvider.GetParameterCount())
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.GenericParameterNotValid, format.Substring(start, i - start), genericNameProvider.GetGenericTypeName(), genericNameProvider.GetParameterCount() - 1)));
typeName.Append(genericNameProvider.GetParameterName(paramIndex));
}
switch (serverTypeNamespace[0])
{
case '#':
- serverTypeNamespace = string.Concat(Globals.DataContractXsdBaseNamespace, serverTypeNamespace.Substring(1));
+ serverTypeNamespace = string.Concat(Globals.DataContractXsdBaseNamespace, serverTypeNamespace.AsSpan(1));
break;
case '\\':
if (serverTypeNamespace.Length >= 2)
switch (serverTypeNamespace[0])
{
case '#':
- serverTypeNamespace = string.Concat(Globals.DataContractXsdBaseNamespace, serverTypeNamespace.Substring(1));
+ serverTypeNamespace = string.Concat(Globals.DataContractXsdBaseNamespace, serverTypeNamespace.AsSpan(1));
break;
case '\\':
if (serverTypeNamespace.Length >= 2)
}
else if (dataContractNamespace.StartsWith(Globals.DataContractXsdBaseNamespace, StringComparison.Ordinal))
{
- return string.Concat("#", dataContractNamespace.Substring(JsonGlobals.DataContractXsdBaseNamespaceLength));
+ return string.Concat("#", dataContractNamespace.AsSpan(JsonGlobals.DataContractXsdBaseNamespaceLength));
}
}
private char[] _indentChars;
private static readonly char[] s_defaultIndentChars = CreateDefaultIndentChars();
- // This method is needed as the native code compiler fails when this initialization is inline
private static char[] CreateDefaultIndentChars()
{
- return new string(DefaultIndentChar, IndentArrayLength).ToCharArray();
+ var result = new char[IndentArrayLength];
+ result.AsSpan().Fill(DefaultIndentChar);
+ return result;
}
// element stack
{
EscapeKeywords(name.Substring(0, nameEnd), sb);
sb.Append('<');
- int arguments = int.Parse(name.Substring(nameEnd + 1), CultureInfo.InvariantCulture) + index;
+ int arguments = int.Parse(name.AsSpan(nameEnd + 1), provider: CultureInfo.InvariantCulture) + index;
for (; index < arguments; index++)
{
sb.Append(GetCSharpName(parameters[index]));
}
if (numeric)
{
- long index = long.Parse(name.Substring(prefixLength), NumberStyles.Integer, CultureInfo.InvariantCulture);
+ long index = long.Parse(name.AsSpan(prefixLength), NumberStyles.Integer, CultureInfo.InvariantCulture);
if (index <= int.MaxValue)
{
int newIndex = (int)index;
// 7 + 6 = 13, round up to the nearest power-of-two.
const int InitalRdnSize = 16;
List<byte[]> encodedSets = new List<byte[]>(InitalRdnSize);
- char[] chars = stringForm.ToCharArray();
+ ReadOnlySpan<char> chars = stringForm;
int pos;
int end = chars.Length;
Debug.Assert(valueEnd != -1);
Debug.Assert(valueStart != -1);
- encodedSets.Add(ParseRdn(tagOid, chars, valueStart, valueEnd, hadEscapedQuote));
+ encodedSets.Add(ParseRdn(tagOid, chars[valueStart..valueEnd], hadEscapedQuote));
tagOid = null;
valueStart = -1;
valueEnd = -1;
Debug.Assert(valueStart != -1);
Debug.Assert(valueEnd != -1);
- encodedSets.Add(ParseRdn(tagOid, chars, valueStart, valueEnd, hadEscapedQuote));
+ encodedSets.Add(ParseRdn(tagOid, chars[valueStart..valueEnd], hadEscapedQuote));
break;
// If the entire string was empty, or ended in a dnSeparator.
return new Oid(stringForm.Substring(tagStart, length));
}
- private static byte[] ParseRdn(Oid tagOid, char[] chars, int valueStart, int valueEnd, bool hadEscapedQuote)
+ private static byte[] ParseRdn(Oid tagOid, ReadOnlySpan<char> chars, bool hadEscapedQuote)
{
- ReadOnlySpan<char> charValue;
-
if (hadEscapedQuote)
{
- charValue = ExtractValue(chars, valueStart, valueEnd);
- }
- else
- {
- charValue = new ReadOnlySpan<char>(chars, valueStart, valueEnd - valueStart);
+ chars = ExtractValue(chars);
}
using (AsnWriter writer = new AsnWriter(AsnEncodingRules.DER))
try
{
// An email address with an invalid value will throw.
- writer.WriteCharacterString(UniversalTagNumber.IA5String, charValue);
+ writer.WriteCharacterString(UniversalTagNumber.IA5String, chars);
}
catch (EncoderFallbackException)
{
throw new CryptographicException(SR.Cryptography_Invalid_IA5String);
}
}
- else if (IsValidPrintableString(charValue))
+ else if (IsValidPrintableString(chars))
{
- writer.WriteCharacterString(UniversalTagNumber.PrintableString, charValue);
+ writer.WriteCharacterString(UniversalTagNumber.PrintableString, chars);
}
else
{
- writer.WriteCharacterString(UniversalTagNumber.UTF8String, charValue);
+ writer.WriteCharacterString(UniversalTagNumber.UTF8String, chars);
}
writer.PopSequence();
}
}
- private static char[] ExtractValue(char[] chars, int valueStart, int valueEnd)
+ private static char[] ExtractValue(ReadOnlySpan<char> chars)
{
- // The string is guaranteed to be between ((valueEnd - valueStart) / 2) (all quotes) and
- // (valueEnd - valueStart - 1) (one escaped quote)
- List<char> builder = new List<char>(valueEnd - valueStart - 1);
-
+ var builder = new List<char>(chars.Length);
bool skippedQuote = false;
- for (int i = valueStart; i < valueEnd; i++)
+ foreach (char c in chars)
{
- char c = chars[i];
-
if (c == '"' && !skippedQuote)
{
skippedQuote = true;
if (s_includePatternInName)
{
const int DescriptionLimit = 100; // arbitrary limit to avoid very long method names
- description = "_" + (pattern.Length > DescriptionLimit ? pattern.Substring(0, DescriptionLimit) : pattern);
+ description = string.Concat("_", pattern.Length > DescriptionLimit ? pattern.AsSpan(0, DescriptionLimit) : pattern);
}
DynamicMethod goMethod = DefineDynamicMethod($"Regex{regexNum}_Go{description}", null, typeof(CompiledRegexRunner));
return base.ConvertFrom(context, culture, value);
}
- if ((strFormat[0] == '[') && (strFormat.Length >= 50) && Guid.TryParse(strFormat.Substring(14, 36), out Guid guid))
+ if ((strFormat[0] == '[') && (strFormat.Length >= 50) && Guid.TryParse(strFormat.AsSpan(14, 36), out Guid guid))
{
// case #2, this is probably a long format (guid)
return new ImageFormat(guid);
public static XamlAccessLevel PrivateAccessTo(string assemblyQualifiedTypeName)
{
int nameBoundary = assemblyQualifiedTypeName.IndexOf(',');
- string typeName = assemblyQualifiedTypeName.Substring(0, nameBoundary).Trim();
- string assemblyFullName = assemblyQualifiedTypeName.Substring(nameBoundary + 1).Trim();
+ string typeName = assemblyQualifiedTypeName.AsSpan(0, nameBoundary).Trim().ToString();
+ string assemblyFullName = assemblyQualifiedTypeName.AsSpan(nameBoundary + 1).Trim().ToString();
AssemblyName assemblyName = new AssemblyName(assemblyFullName);
return new XamlAccessLevel(assemblyName.FullName, typeName);
}