From 8c52ada595cdef848aee371f663e5f294f836e7f Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 15 Aug 2016 18:33:37 +1000 Subject: [PATCH] Use named StringLength constant instead of hardcoding 64 everywhere, replace StringBuilder with StringBuffer. --- .../2D/Screens/Menu/EditHotkeyScreen.cs | 2 +- .../Widgets/Chat/TextInputWidget.Handlers.cs | 4 ++-- .../2D/Widgets/Chat/TextInputWidget.cs | 8 +++---- .../2D/Widgets/Menu/MenuInputWidget.cs | 4 ++-- ClassicalSharp/Commands/CommandManager.cs | 23 +++++++++---------- ClassicalSharp/Game/ChatLog.cs | 2 +- ClassicalSharp/Network/Utils/NetReader.cs | 8 +++---- ClassicalSharp/Network/Utils/NetWriter.cs | 8 +++---- ClassicalSharp/Utils/StringBuffer.cs | 18 +++++++-------- ClassicalSharp/Utils/Utils.cs | 8 ++++--- ClassicalSharp/Utils/WrappableStringBuffer.cs | 2 +- 11 files changed, 44 insertions(+), 43 deletions(-) diff --git a/ClassicalSharp/2D/Screens/Menu/EditHotkeyScreen.cs b/ClassicalSharp/2D/Screens/Menu/EditHotkeyScreen.cs index 7f08fd8c0..7dfcaeb0b 100644 --- a/ClassicalSharp/2D/Screens/Menu/EditHotkeyScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/EditHotkeyScreen.cs @@ -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 ), diff --git a/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.Handlers.cs b/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.Handlers.cs index 82461567b..5961f2a11 100644 --- a/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.Handlers.cs +++ b/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.Handlers.cs @@ -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, ' ' ); } diff --git a/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs b/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs index a3cece6d7..dc82c5c57 100644 --- a/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs +++ b/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs @@ -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 ); } diff --git a/ClassicalSharp/2D/Widgets/Menu/MenuInputWidget.cs b/ClassicalSharp/2D/Widgets/Menu/MenuInputWidget.cs index 6e723e9ce..f1658fedb 100644 --- a/ClassicalSharp/2D/Widgets/Menu/MenuInputWidget.cs +++ b/ClassicalSharp/2D/Widgets/Menu/MenuInputWidget.cs @@ -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 ); diff --git a/ClassicalSharp/Commands/CommandManager.cs b/ClassicalSharp/Commands/CommandManager.cs index 375fdcabf..2e58588f5 100644 --- a/ClassicalSharp/Commands/CommandManager.cs +++ b/ClassicalSharp/Commands/CommandManager.cs @@ -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 lines = new List(); - 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() { diff --git a/ClassicalSharp/Game/ChatLog.cs b/ClassicalSharp/Game/ChatLog.cs index 011b8ec66..1373c670c 100644 --- a/ClassicalSharp/Game/ChatLog.cs +++ b/ClassicalSharp/Game/ChatLog.cs @@ -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 diff --git a/ClassicalSharp/Network/Utils/NetReader.cs b/ClassicalSharp/Network/Utils/NetReader.cs index 27fcac8e2..3ce815c95 100644 --- a/ClassicalSharp/Network/Utils/NetReader.cs +++ b/ClassicalSharp/Network/Utils/NetReader.cs @@ -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++ ) { diff --git a/ClassicalSharp/Network/Utils/NetWriter.cs b/ClassicalSharp/Network/Utils/NetWriter.cs index f1280b9db..5a1e34839 100644 --- a/ClassicalSharp/Network/Utils/NetWriter.cs +++ b/ClassicalSharp/Network/Utils/NetWriter.cs @@ -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 ) { diff --git a/ClassicalSharp/Utils/StringBuffer.cs b/ClassicalSharp/Utils/StringBuffer.cs index f3e7af224..dee5c3909 100644 --- a/ClassicalSharp/Utils/StringBuffer.cs +++ b/ClassicalSharp/Utils/StringBuffer.cs @@ -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--; diff --git a/ClassicalSharp/Utils/Utils.cs b/ClassicalSharp/Utils/Utils.cs index 47fef272b..a7ef7fa0c 100644 --- a/ClassicalSharp/Utils/Utils.cs +++ b/ClassicalSharp/Utils/Utils.cs @@ -25,6 +25,8 @@ namespace ClassicalSharp { public static class Utils { + public const int StringLength = 64; + /// Creates a vector with all components at 1E25. 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; } diff --git a/ClassicalSharp/Utils/WrappableStringBuffer.cs b/ClassicalSharp/Utils/WrappableStringBuffer.cs index 88c77ba39..093381c7d 100644 --- a/ClassicalSharp/Utils/WrappableStringBuffer.cs +++ b/ClassicalSharp/Utils/WrappableStringBuffer.cs @@ -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; }