Reduce temporary string allocations.

This commit is contained in:
UnknownShadow200 2015-05-09 18:50:15 +10:00
parent 989870e623
commit 0f0b3e57d5

View File

@ -62,30 +62,38 @@ namespace ClassicalSharp {
/// <summary> Reads a string, then converts control characters into the
/// unicode values of their equivalent code page 437 graphical representations. </summary>
public string ReadTextString() {
string value = GetTextString( buffer ).TrimEnd( '\0', ' ' ); // servers can use either null or spaces for padding.
string value = GetTextString( buffer );
Remove( 64 );
return value;
}
public string ReadString() {
string value = GetAsciiString( buffer ).TrimEnd( '\0', ' ' );
string value = GetAsciiString( buffer );
Remove( 64 );
return value;
}
static char[] characters = new char[64];
static string GetAsciiString( byte[] data ) {
for( int i = 0; i < 64; i++ ) {
int endIndex = 0;
for( int i = 63; i >= 0; i-- ) {
byte code = data[i];
characters[i] = code >= 0x80 ? '?' : (char)code;
if( endIndex == 0 && !( code == 0 || code == 0x20 ) )
endIndex = i + 1;
characters[i] = code >= 0x80 ? '?' : (char)code;
}
return new String( characters );
return new String( characters, 0, endIndex );
}
static string GetTextString( byte[] data ) {
// code page 437 indices --> actual unicode characters
for( int i = 0; i < 64; i++ ) {
int endIndex = 0;
for( int i = 63; i >= 0; i-- ) {
byte code = data[i];
if( endIndex == 0 && !( code == 0 || code == 0x20 ) )
endIndex = i + 1;
if( code < 0x20 ) { // general control characters
characters[i] = controlCharReplacements[code];
} else if( code < 0x7F ) { // normal ascii character
@ -96,7 +104,7 @@ namespace ClassicalSharp {
characters[i] = extendedCharReplacements[code - 0x80];
}
}
return new String( characters );
return new String( characters, 0, endIndex );
}
static char[] controlCharReplacements = new char[] { // 00 -> 1F