Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit bc20f4a

Browse files
committed
Merge pull request #2691 from James-Ko/cbuf
Eliminate char buffer allocations in ConsolePal.Unix
2 parents b039676 + e232585 commit bc20f4a

File tree

1 file changed

+2
-28
lines changed

1 file changed

+2
-28
lines changed

src/System.Console/src/System/ConsolePal.Unix.cs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -310,32 +310,6 @@ private static unsafe void Write(int fd, byte[] buffer, int offset, int count)
310310
}
311311
}
312312

313-
/// <summary>Creates a string from an array of ASCII bytes.</summary>
314-
/// <param name="buffer">The byte buffer.</param>
315-
/// <param name="offset">The starting location in the buffer from which to begin the string.</param>
316-
/// <param name="length">The length of the resulting string.</param>
317-
/// <returns>
318-
/// A string containing characters copied from the buffer, one character per byte starting
319-
/// from <paramref name="offset"/> and going for <paramref name="length"/> bytes.
320-
/// </returns>
321-
private static string StringFromAsciiBytes(byte[] buffer, int offset, int length)
322-
{
323-
// Special-case for empty strings
324-
if (length == 0)
325-
{
326-
return string.Empty;
327-
}
328-
329-
// new string(sbyte*, ...) doesn't exist in the targeted reference assembly,
330-
// so we first copy to an array of chars, and then create a string from that.
331-
char[] chars = new char[length];
332-
for (int i = 0, j = offset; i < length; i++, j++)
333-
{
334-
chars[i] = (char)buffer[j];
335-
}
336-
return new string(chars);
337-
}
338-
339313
/// <summary>Provides a stream to use for Unix console input or output.</summary>
340314
private sealed class UnixConsoleStream : ConsoleStream
341315
{
@@ -699,7 +673,7 @@ private static string ReadString(byte[] buffer, int pos)
699673
{
700674
findNullEnding++;
701675
}
702-
return StringFromAsciiBytes(buffer, pos, findNullEnding - pos);
676+
return Encoding.ASCII.GetString(buffer, pos, findNullEnding - pos);
703677
}
704678
}
705679

@@ -1043,7 +1017,7 @@ private static unsafe string FormatPrintF(string format, object arg)
10431017
throw new InvalidOperationException(SR.InvalidOperation_PrintF);
10441018
}
10451019
}
1046-
return StringFromAsciiBytes(bytes, 0, neededLength);
1020+
return Encoding.ASCII.GetString(bytes, 0, neededLength);
10471021
}
10481022

10491023
/// <summary>Gets the lazily-initialized dynamic or static variables collection, based on the supplied variable name.</summary>

0 commit comments

Comments
 (0)