未验证 提交 11d8e264 编写于 作者: S Stephen Toub 提交者: GitHub

Use IndexOf{Any} in a few more places (#70176)

* Use IndexOf{Any} in a few more places

* Address PR feedback
上级 ae49f52e
......@@ -498,9 +498,8 @@ private static string ReadString(byte[] buffer, int pos)
/// <summary>Finds the null-terminator for a string that begins at the specified position.</summary>
private static int FindNullTerminator(byte[] buffer, int pos)
{
int termPos = pos;
while (termPos < buffer.Length && buffer[termPos] != '\0') termPos++;
return termPos;
int i = buffer.AsSpan(pos).IndexOf((byte)'\0');
return i >= 0 ? pos + i : buffer.Length;
}
}
......
......@@ -843,16 +843,11 @@ private string ReadName(int position, int nameLength)
Debug.Assert(position > 0);
Debug.Assert(nameLength >= 0 && (position + nameLength) <= _buffer.Length);
int lengthWithoutNullTerm = nameLength;
for (int i = 0; i < nameLength; i++)
int lengthWithoutNullTerm = _buffer.AsSpan(position, nameLength).IndexOf((byte)'\0');
if (lengthWithoutNullTerm < 0)
{
if (_buffer[position + i] == '\0')
{
lengthWithoutNullTerm = i;
break;
}
lengthWithoutNullTerm = nameLength;
}
Debug.Assert(lengthWithoutNullTerm <= nameLength); // should be null terminated or empty
return lengthWithoutNullTerm > 0 ?
Encoding.UTF8.GetString(_buffer, position, lengthWithoutNullTerm) :
......
......@@ -92,7 +92,7 @@ public HttpStatusCode StatusCode
}
set
{
if ((value != null) && ContainsNewLineCharacter(value))
if ((value != null) && HttpRuleParser.ContainsNewLine(value))
{
throw new FormatException(SR.net_http_reasonphrase_format_error);
}
......@@ -207,18 +207,6 @@ public override string ToString()
return sb.ToString();
}
private static bool ContainsNewLineCharacter(string value)
{
foreach (char character in value)
{
if ((character == HttpRuleParser.CR) || (character == HttpRuleParser.LF))
{
return true;
}
}
return false;
}
#region IDisposable Members
protected virtual void Dispose(bool disposing)
......
......@@ -379,23 +379,12 @@ private static bool CheckForUnicode(char ch, bool allowUnicode)
return true;
}
internal static bool IsAllowedWhiteSpace(char c)
{
internal static bool IsAllowedWhiteSpace(char c) =>
// all allowed whitespace characters
return c == Tab || c == Space || c == CR || c == LF;
}
c == Tab || c == Space || c == CR || c == LF;
internal static bool HasCROrLF(string data)
{
for (int i = 0; i < data.Length; i++)
{
if (data[i] == '\r' || data[i] == '\n')
{
return true;
}
}
return false;
}
internal static bool HasCROrLF(string data) =>
data.AsSpan().IndexOfAny(CR, LF) >= 0;
// Is there a FWS ("\r\n " or "\r\n\t") starting at the given index?
internal static bool IsFWSAt(string data, int index)
......
......@@ -246,11 +246,14 @@ private void SetUseAngleBracketsForGenerics(bool value)
private void Append(string pStr)
{
foreach (char c in pStr)
int i = pStr.IndexOf('\0');
if (i < 0)
{
if (c == '\0')
break;
_str.Append(c);
_str.Append(pStr);
}
else if (i > 0)
{
_str.Append(pStr.AsSpan(0, i));
}
}
......
......@@ -9,23 +9,15 @@ public sealed partial class TimeZoneInfo
{
private static unsafe bool TryConvertIanaIdToWindowsId(string ianaId, bool allocate, out string? windowsId)
{
if (GlobalizationMode.Invariant || GlobalizationMode.UseNls || ianaId is null)
if (GlobalizationMode.Invariant ||
GlobalizationMode.UseNls ||
ianaId is null ||
ianaId.AsSpan().IndexOfAny('\\', '\n', '\r') >= 0) // ICU uses these characters as a separator
{
windowsId = null;
return false;
}
foreach (char c in ianaId)
{
// ICU uses some characters as a separator and trim the id at that character.
// while we should fail if the Id contained one of these characters.
if (c == '\\' || c == '\n' || c == '\r')
{
windowsId = null;
return false;
}
}
char* buffer = stackalloc char[100];
int length = Interop.Globalization.IanaIdToWindowsId(ianaId, buffer, 100);
if (length > 0)
......@@ -54,15 +46,10 @@ private static unsafe bool TryConvertWindowsIdToIanaId(string windowsId, string?
return true;
}
foreach (char c in windowsId)
if (windowsId.AsSpan().IndexOfAny('\\', '\n', '\r') >= 0) // ICU uses these characters as a separator
{
// ICU uses some characters as a separator and trim the id at that character.
// while we should fail if the Id contained one of these characters.
if (c == '\\' || c == '\n' || c == '\r')
{
ianaId = null;
return false;
}
ianaId = null;
return false;
}
// regionPtr will point at the region name encoded as ASCII.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册