Use named StringLength constant instead of hardcoding 64 everywhere, replace StringBuilder with StringBuffer.

This commit is contained in:
UnknownShadow200 2016-08-15 18:33:37 +10:00
parent b3e0b8b32b
commit 8c52ada595
11 changed files with 44 additions and 43 deletions

View File

@ -81,7 +81,7 @@ namespace ClassicalSharp.Gui {
MenuInputWidget.Create(
game, 0, -35, 500, 30, curHotkey.Text,
Anchor.Centre, Anchor.Centre,
regularFont, titleFont, new StringValidator( 64 ) ),
regularFont, titleFont, new StringValidator( Utils.StringLength ) ),
Make( -100, 10, "Input stays open: " + staysOpen,
301, 40, titleFont, LeaveOpenClick ),

View File

@ -75,7 +75,7 @@ namespace ClassicalSharp.Gui {
if( caretPos != -1 ) caretPos -= len;
AppendText( matches[0] );
} else if( matches.Count > 1 ) {
StringBuffer sb = new StringBuffer( 64 );
StringBuffer sb = new StringBuffer( Utils.StringLength );
int index = 0;
sb.Append( ref index, "&e" );
sb.AppendNum( ref index, matches.Count );
@ -83,7 +83,7 @@ namespace ClassicalSharp.Gui {
for( int i = 0; i < matches.Count; i++) {
string match = matches[i];
if( (match.Length + 1 + sb.Length) > LineLength ) break;
if( (sb.Length + match.Length + 1) > sb.Capacity ) break;
sb.Append( ref index, match );
sb.Append( ref index, ' ' );
}

View File

@ -15,7 +15,7 @@ namespace ClassicalSharp.Gui {
HorizontalAnchor = Anchor.LeftOrTop;
VerticalAnchor = Anchor.BottomOrRight;
typingLogPos = game.Chat.InputLog.Count; // Index of newest entry + 1.
buffer = new WrappableStringBuffer( 64 * lines );
buffer = new WrappableStringBuffer( Utils.StringLength * lines );
DrawTextArgs args = new DrawTextArgs( "_", font, true );
caretTex = game.Drawer2D.MakeChatTextTexture( ref args, 0, 0 );
@ -234,9 +234,9 @@ namespace ClassicalSharp.Gui {
void SendWithPartial( string allText ) {
// don't automatically word wrap the message.
while( allText.Length > 64 ) {
game.Chat.Send( allText.Substring( 0, 64 ), true );
allText = allText.Substring( 64 );
while( allText.Length > Utils.StringLength ) {
game.Chat.Send( allText.Substring( 0, Utils.StringLength ), true );
allText = allText.Substring( Utils.StringLength );
}
game.Chat.Send( allText, false );
}

View File

@ -15,7 +15,7 @@ namespace ClassicalSharp.Gui {
VerticalAnchor = Anchor.BottomOrRight;
this.font = font;
this.boldFont = boldFont;
chatInputText = new StringBuffer( 64 );
chatInputText = new StringBuffer( Utils.StringLength );
}
public static MenuInputWidget Create( Game game, int x, int y, int width, int height, string text, Anchor horizontal,
@ -130,7 +130,7 @@ namespace ClassicalSharp.Gui {
}
public override bool HandlesKeyPress( char key ) {
if( chatInputText.Length < 64 && !IsInvalidChar( key ) ) {
if( chatInputText.Length < Utils.StringLength && !IsInvalidChar( key ) ) {
if( !Validator.IsValidChar( key ) ) return true;
chatInputText.Append( chatInputText.Length, key );

View File

@ -1,7 +1,6 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassicalSharp.Commands {
@ -77,24 +76,24 @@ namespace ClassicalSharp.Commands {
}
public void PrintDefinedCommands( Game game ) {
List<string> lines = new List<string>();
StringBuilder buffer = new StringBuilder( 64 );
StringBuffer sb = new StringBuffer( Utils.StringLength );
int index = 0;
for( int i = 0; i < RegisteredCommands.Count; i++ ) {
Command cmd = RegisteredCommands[i];
string name = cmd.Name;
if( buffer.Length + name.Length > 64 ) {
lines.Add( buffer.ToString() );
buffer.Length = 0;
if( (sb.Length + name.Length + 2) > sb.Capacity ) {
game.Chat.Add( sb.ToString() );
sb.Clear();
index = 0;
}
buffer.Append( name );
buffer.Append( ", " );
sb.Append( ref index, name );
sb.Append( ref index, ", " );
}
if( buffer.Length > 0 )
lines.Add( buffer.ToString() );
for( int i = 0; i < lines.Count; i++ )
game.Chat.Add( lines[i] );
if( sb.Length > 0 )
game.Chat.Add( sb.ToString() );
}
public void Dispose() {

View File

@ -116,7 +116,7 @@ namespace ClassicalSharp {
last = now;
if( writer == null ) return;
if( 32 + text.Length > logBuffer.capacity )
if( 32 + text.Length > logBuffer.Capacity )
logBuffer = new StringBuffer( 32 + text.Length );
int index = 0;
logBuffer.Clear() // [HH:mm:ss] text

View File

@ -73,12 +73,12 @@ namespace ClassicalSharp.Network {
}
public string ReadCp437String() {
int length = GetString( false, 64 );
int length = GetString( false, Utils.StringLength );
return new String( characters, 0, length );
}
public string ReadAsciiString() {
int length = GetString( true, 64 );
int length = GetString( true, Utils.StringLength );
return new String( characters, 0, length );
}
@ -88,7 +88,7 @@ namespace ClassicalSharp.Network {
}
internal string ReadChatString( ref byte messageType ) {
int length = GetString( false, 64 );
int length = GetString( false, Utils.StringLength );
int offset = 0;
if( length >= womDetail.Length && IsWomDetailString() ) {
@ -99,7 +99,7 @@ namespace ClassicalSharp.Network {
return new String( characters, offset, length );
}
static char[] characters = new char[64];
static char[] characters = new char[Utils.StringLength];
const string womDetail = "^detail.user=";
static bool IsWomDetailString() {
for( int i = 0; i < womDetail.Length; i++ ) {

View File

@ -15,7 +15,7 @@ namespace ClassicalSharp.Network {
}
public void WriteString( string value ) {
int count = Math.Min( value.Length, 64 );
int count = Math.Min( value.Length, Utils.StringLength );
for( int i = 0; i < count; i++ ) {
char c = value[i];
int cpIndex = 0;
@ -31,10 +31,10 @@ namespace ClassicalSharp.Network {
buffer[index + i] = (byte)'?';
}
}
for( int i = value.Length; i < 64; i++ ) {
for( int i = value.Length; i < Utils.StringLength; i++ )
buffer[index + i] = (byte)' ';
}
index += 64;
index += Utils.StringLength;
}
public void WriteUInt8( byte value ) {

View File

@ -6,10 +6,10 @@ namespace ClassicalSharp {
public class StringBuffer {
public char[] value;
public int capacity;
public int Capacity;
public StringBuffer( int capacity ) {
this.capacity = capacity;
this.Capacity = capacity;
value = new char[capacity];
}
@ -72,19 +72,19 @@ namespace ClassicalSharp {
}
public StringBuffer Clear() {
for( int i = 0; i < capacity; i++ )
for( int i = 0; i < Capacity; i++ )
value[i] = '\0';
return this;
}
public void DeleteAt( int index ) {
for( int i = index; i < capacity - 1; i++ )
for( int i = index; i < Capacity - 1; i++ )
value[i] = value[i + 1];
value[capacity - 1] = '\0';
value[Capacity - 1] = '\0';
}
public void InsertAt( int index, char c ) {
for( int i = capacity - 1; i > index; i-- )
for( int i = Capacity - 1; i > index; i-- )
value[i] = value[i - 1];
value[index] = c;
}
@ -96,7 +96,7 @@ namespace ClassicalSharp {
public bool Empty {
get {
for( int i = 0; i < capacity; i++ ) {
for( int i = 0; i < Capacity; i++ ) {
if( value[i] != '\0' )
return false;
}
@ -106,8 +106,8 @@ namespace ClassicalSharp {
public int Length {
get {
int len = capacity;
for( int i = capacity - 1; i >= 0; i-- ) {
int len = Capacity;
for( int i = Capacity - 1; i >= 0; i-- ) {
if( value[i] != '\0' )
break;
len--;

View File

@ -25,6 +25,8 @@ namespace ClassicalSharp {
public static class Utils {
public const int StringLength = 64;
/// <summary> Creates a vector with all components at 1E25. </summary>
public static Vector3 MaxPos() { return new Vector3( 1E25f, 1E25f, 1E25f ); }
@ -268,9 +270,9 @@ namespace ClassicalSharp {
internal static byte FastByte( string s ) {
int sum = 0;
switch( s.Length ) {
case 1: sum = (s[0] - '0'); break;
case 2: sum = (s[0] - '0') * 10 + (s[1] - '0'); break;
case 3: sum = (s[0] - '0') * 100 + (s[1] - '0') * 10 + (s[2] - '0'); break;
case 1: sum = (s[0] - '0'); break;
case 2: sum = (s[0] - '0') * 10 + (s[1] - '0'); break;
case 3: sum = (s[0] - '0') * 100 + (s[1] - '0') * 10 + (s[2] - '0'); break;
}
return (byte)sum;
}

View File

@ -56,7 +56,7 @@ namespace ClassicalSharp {
for( int i = 0; i < len; i++ )
wrap[i] = value[i];
for( int i = len; i < capacity; i++ )
for( int i = len; i < Capacity; i++ )
wrap[i] = '\0';
value = wrap;
}