diff --git a/PKHeX.Core/PKM/Strings/StringConverter.cs b/PKHeX.Core/PKM/Strings/StringConverter.cs index 48119fbfa..8969dc405 100644 --- a/PKHeX.Core/PKM/Strings/StringConverter.cs +++ b/PKHeX.Core/PKM/Strings/StringConverter.cs @@ -43,10 +43,10 @@ public static string GetString(byte[] data, int generation, bool jp, bool isBigE /// Generation string format /// Encoding is Japanese /// Encoding is Big Endian - /// - /// - /// Pad to given length - /// Pad with value + /// Maximum length of the input + /// Language specific conversion (Chinese) + /// Pad the input to given length + /// Pad the input with this character value /// Encoded data. public static byte[] SetString(string value, int generation, bool jp, bool isBigEndian, int maxLength, int language = 0, int padTo = 0, ushort padWith = 0) { @@ -80,9 +80,9 @@ public static string GetString5(byte[] data, int offset, int count) /// Gets the bytes for a Generation 5 string. /// Decoded string. - /// Maximum length - /// Pad to given length - /// Pad with value + /// Maximum length of the input + /// Pad the input to given length + /// Pad the input with this character value /// Encoded data. public static byte[] SetString5(string value, int maxLength, int padTo = 0, ushort padWith = 0) { @@ -117,9 +117,9 @@ public static string GetString6(byte[] data, int offset, int count) /// Gets the bytes for a Generation 6 string. /// Decoded string. - /// Maximum length - /// Pad to given length - /// Pad with value + /// Maximum length of the input + /// Pad the input to given length + /// Pad the input with this character value /// Encoded data. public static byte[] SetString6(string value, int maxLength, int padTo = 0, ushort padWith = 0) { @@ -155,10 +155,10 @@ public static string GetString7(byte[] data, int offset, int count) /// Gets the bytes for a Generation 7 string. /// Decoded string. - /// Maximum length + /// Maximum length of the input /// Language specific conversion (Chinese) - /// Pad to given length - /// Pad with value + /// Pad the input to given length + /// Pad the input with this character value /// Chinese string remapping should be attempted /// Encoded data. public static byte[] SetString7(string value, int maxLength, int language, int padTo = 0, ushort padWith = 0, bool chinese = false) @@ -182,10 +182,10 @@ public static byte[] SetString7(string value, int maxLength, int language, int p /// Gets the bytes for a Generation 7 string. /// Decoded string. - /// Maximum length + /// Maximum length of the input /// Language specific conversion (Chinese) - /// Pad to given length - /// Pad with value + /// Pad the input to given length + /// Pad the input with this character value /// Chinese string remapping should be attempted /// Encoded data. public static byte[] SetString7b(string value, int maxLength, int language, int padTo = 0, ushort padWith = 0, bool chinese = false) diff --git a/PKHeX.Core/PKM/Strings/StringConverter12.cs b/PKHeX.Core/PKM/Strings/StringConverter12.cs index aa2920320..a697cd841 100644 --- a/PKHeX.Core/PKM/Strings/StringConverter12.cs +++ b/PKHeX.Core/PKM/Strings/StringConverter12.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Text; @@ -74,7 +75,7 @@ public static string GetString1(byte[] data, int offset, int count, bool jp) { var dict = jp ? RBY2U_J : RBY2U_U; - var s = new StringBuilder(); + var s = new StringBuilder(count); for (int i = 0; i < count; i++) { var val = data[offset + i]; @@ -92,20 +93,21 @@ public static string GetString1(byte[] data, int offset, int count, bool jp) /// Converts a string to Generation 1 encoded data. /// /// Decoded string. - /// Maximum length + /// Maximum length of the input /// Data destination is Japanese. - /// Pad to given length - /// Pad with value + /// Pad the input to given length + /// Pad the input with this character value /// Encoded data. public static byte[] SetString1(string value, int maxLength, bool jp, int padTo = 0, ushort padWith = 0) { - if (value.Length > maxLength) - value = value.Substring(0, maxLength); // Hard cap - if (value.StartsWith(G1TradeOTStr)) // Handle "[TRAINER]" return new[] { G1TradeOTCode, G1TerminatorCode }; - var arr = new List(padTo); + if (value.Length > maxLength) + value = value.Substring(0, maxLength); // Hard cap + + var capacity = Math.Max(value.Length, padTo); + var arr = new List(capacity); var dict = jp ? U2RBY_J : U2RBY_U; foreach (char c in value) { diff --git a/PKHeX.Core/PKM/Strings/StringConverter2KOR.cs b/PKHeX.Core/PKM/Strings/StringConverter2KOR.cs index 1257bd461..89a4d46e3 100644 --- a/PKHeX.Core/PKM/Strings/StringConverter2KOR.cs +++ b/PKHeX.Core/PKM/Strings/StringConverter2KOR.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Text; @@ -22,16 +23,16 @@ public static class StringConverter2KOR /// Decoded string. public static string GetString2KOR(byte[] data, int offset, int count) { - var s = new StringBuilder(); + var s = new StringBuilder(count); for (int i = 0; i < count; i++) { var val = data[offset + i]; var dict = val <= 0xB ? GSC2U_KOR[val] : RBY2U_U; if (val is <= 0xB and not 0) - val = data[offset + ++i]; + val = data[offset + (++i)]; if (!dict.TryGetValue(val, out var c)) // Take valid values break; - if (c == '\0') // Stop if Terminator + if (c == G1Terminator) // Stop if Terminator break; s.Append(c); } @@ -43,27 +44,29 @@ public static string GetString2KOR(byte[] data, int offset, int count) /// Converts a string to Generation 1 encoded data. /// /// Decoded string. - /// Maximum length - /// Pad to given length - /// Pad with value + /// Maximum length of the input + /// Pad the input to given length + /// Pad the input with this character value /// Encoded data. public static byte[] SetString2KOR(string value, int maxLength, int padTo = 0, ushort padWith = 0) { - if (value.Length > maxLength) - value = value.Substring(0, maxLength); // Hard cap - - var dict = U2RBY_U; if (value.StartsWith(G1TradeOTStr)) // Handle "[TRAINER]" return new[] { G1TradeOTCode, G1TerminatorCode }; - var arr = new List(padTo); + if (value.Length > maxLength) + value = value.Substring(0, maxLength); // Hard cap + + var kor = U2GSC_KOR; + var dict = U2RBY_U; + var capacity = Math.Max(value.Length * 2, padTo); + var arr = new List(capacity); foreach (char c in value) { var koreanChar = false; // although the 0x00 and 0x0B dictionaries are identical, the game only uses 0x0B. - for (byte i = 1; i < U2GSC_KOR.Length; i++) + for (byte i = 1; i < kor.Length; i++) { - var table = U2GSC_KOR[i]; + var table = kor[i]; if (!table.TryGetValue(c, out byte val)) continue; koreanChar = true; diff --git a/PKHeX.Core/PKM/Strings/StringConverter3.cs b/PKHeX.Core/PKM/Strings/StringConverter3.cs index 0c181282b..5c24b6b71 100644 --- a/PKHeX.Core/PKM/Strings/StringConverter3.cs +++ b/PKHeX.Core/PKM/Strings/StringConverter3.cs @@ -18,7 +18,7 @@ public static class StringConverter3 /// Decoded string. public static string GetString3(byte[] data, int offset, int count, bool jp) { - var s = new StringBuilder(); + var s = new StringBuilder(count); for (int i = 0; i < count; i++) { var val = data[offset + i]; @@ -35,10 +35,10 @@ public static string GetString3(byte[] data, int offset, int count, bool jp) /// Converts a string to a Generation 3 encoded value array. /// /// Decoded string. - /// Maximum length of string + /// Maximum length of the input /// String destination is Japanese font. - /// Pad to given length - /// Pad with value + /// Pad the input to given length + /// Pad the input with this character value /// public static byte[] SetString3(string value, int maxLength, bool jp, int padTo = 0, ushort padWith = 0) { @@ -85,9 +85,9 @@ public static string GetBEString3(byte[] data, int offset, int count) /// Gets the bytes for a Big Endian string. /// Decoded string. - /// Maximum length - /// Pad to given length - /// Pad with value + /// Maximum length of the input + /// Pad the input to given length + /// Pad the input with this character value /// Encoded data. public static byte[] SetBEString3(string value, int maxLength, int padTo = 0, ushort padWith = 0) { diff --git a/PKHeX.Core/PKM/Strings/StringConverter4.cs b/PKHeX.Core/PKM/Strings/StringConverter4.cs index 618c70e69..5b60c568e 100644 --- a/PKHeX.Core/PKM/Strings/StringConverter4.cs +++ b/PKHeX.Core/PKM/Strings/StringConverter4.cs @@ -17,7 +17,7 @@ public static class StringConverter4 /// Decoded string. public static string GetString4(byte[] data, int offset, int count) { - var s = new StringBuilder(); + var s = new StringBuilder(count / 2); for (int i = 0; i < count; i += 2) { var val = BitConverter.ToUInt16(data, offset + i); @@ -34,9 +34,9 @@ public static string GetString4(byte[] data, int offset, int count) /// Gets the bytes for a 4th Generation String /// Decoded string. - /// Maximum length - /// Pad to given length - /// Pad with value + /// Maximum length of the input + /// Pad the input to given length + /// Pad the input with this character value /// Encoded data. public static byte[] SetString4(string value, int maxLength, int padTo = 0, ushort padWith = 0) { @@ -71,7 +71,7 @@ public static byte[] SetString4(string value, int maxLength, int padTo = 0, usho /// Converted string. public static string GetBEString4(byte[] data, int offset, int count) { - var sb = new StringBuilder(); + var sb = new StringBuilder(count / 2); for (int i = 0; i < count; i += 2) { var val = BigEndian.ToUInt16(data, offset + i); @@ -91,8 +91,8 @@ public static string GetBEString4(byte[] data, int offset, int count) /// /// String to be converted. /// Maximum length of string - /// Pad to given length - /// Pad with value + /// Pad the input to given length + /// Pad the input with this character value /// Byte array containing encoded character data public static byte[] SetBEString4(string value, int maxLength, int padTo = 0, ushort padWith = 0) { diff --git a/PKHeX.Core/Saves/Encryption/SwishCrypto/SCBlockUtil.cs b/PKHeX.Core/Saves/Encryption/SwishCrypto/SCBlockUtil.cs index 541201cc9..92a569082 100644 --- a/PKHeX.Core/Saves/Encryption/SwishCrypto/SCBlockUtil.cs +++ b/PKHeX.Core/Saves/Encryption/SwishCrypto/SCBlockUtil.cs @@ -59,7 +59,7 @@ public static string GetBlockFileNameWithoutExtension(SCBlock block) public static string GetBlockSummary(SCBlock b) { - var sb = new StringBuilder(); + var sb = new StringBuilder(64); sb.Append("Key: ").AppendFormat("{0:X8}", b.Key).AppendLine(); sb.Append("Type: ").Append(b.Type).AppendLine(); if (b.Data.Length != 0) diff --git a/PKHeX.Drawing/Names/SpriteName.cs b/PKHeX.Drawing/Names/SpriteName.cs index ecb35758c..02f5b11f8 100644 --- a/PKHeX.Drawing/Names/SpriteName.cs +++ b/PKHeX.Drawing/Names/SpriteName.cs @@ -26,7 +26,7 @@ public static string GetResourceStringSprite(int species, int form, int gender, if (SpeciesDefaultFormSprite.Contains(species)) // Species who show their default sprite regardless of Form form = 0; - var sb = new StringBuilder(); + var sb = new StringBuilder(12); // longest expected string result sb.Append(Separator).Append(species); if (form != 0)