-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate the header before reading a large grammar file.
Saves some allocations if the file to load is not a grammar file. This is done only on modern frameworks.
- Loading branch information
1 parent
1d20533
commit 1f802ea
Showing
3 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright © Theodore Tsirpanis and Contributors. | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#if (NETCOREAPP || NETSTANDARD2_1_OR_GREATER) && !NET7_0_OR_GREATER | ||
namespace System.IO | ||
{ | ||
internal static class StreamCompat | ||
{ | ||
public static int ReadAtLeast(this Stream stream, Span<byte> buffer, int minimumLength) | ||
{ | ||
int totalRead = 0; | ||
while (totalRead < minimumLength) | ||
{ | ||
int n = stream.Read(buffer[totalRead..]); | ||
if (n == 0) | ||
{ | ||
break; | ||
} | ||
totalRead += n; | ||
} | ||
return totalRead; | ||
} | ||
|
||
public static void ReadExactly(this Stream stream, Span<byte> buffer) | ||
{ | ||
while (!buffer.IsEmpty) | ||
{ | ||
int n = stream.Read(buffer); | ||
if (n == 0) | ||
{ | ||
break; | ||
} | ||
buffer = buffer[n..]; | ||
} | ||
|
||
if (!buffer.IsEmpty) | ||
{ | ||
throw new EndOfStreamException(); | ||
} | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters