Set initial capacity for stringbuilder to reduce reallocation

Default capacity is 16 so it's rarely an issue, but sometimes we don't need that much.

Update xmldoc for more clarity
This commit is contained in:
Kurt 2021-01-16 17:31:05 -08:00
parent c31285fd6d
commit b5c30193fa
7 changed files with 60 additions and 55 deletions

View file

@ -43,10 +43,10 @@ namespace PKHeX.Core
/// <param name="generation">Generation string format</param>
/// <param name="jp">Encoding is Japanese</param>
/// <param name="isBigEndian">Encoding is Big Endian</param>
/// <param name="maxLength"></param>
/// <param name="language"></param>
/// <param name="padTo">Pad to given length</param>
/// <param name="padWith">Pad with value</param>
/// <param name="maxLength">Maximum length of the input <see cref="value"/></param>
/// <param name="language">Language specific conversion (Chinese)</param>
/// <param name="padTo">Pad the input <see cref="value"/> to given length</param>
/// <param name="padWith">Pad the input <see cref="value"/> with this character value</param>
/// <returns>Encoded data.</returns>
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 @@ namespace PKHeX.Core
/// <summary>Gets the bytes for a Generation 5 string.</summary>
/// <param name="value">Decoded string.</param>
/// <param name="maxLength">Maximum length</param>
/// <param name="padTo">Pad to given length</param>
/// <param name="padWith">Pad with value</param>
/// <param name="maxLength">Maximum length of the input <see cref="value"/></param>
/// <param name="padTo">Pad the input <see cref="value"/> to given length</param>
/// <param name="padWith">Pad the input <see cref="value"/> with this character value</param>
/// <returns>Encoded data.</returns>
public static byte[] SetString5(string value, int maxLength, int padTo = 0, ushort padWith = 0)
{
@ -117,9 +117,9 @@ namespace PKHeX.Core
/// <summary>Gets the bytes for a Generation 6 string.</summary>
/// <param name="value">Decoded string.</param>
/// <param name="maxLength">Maximum length</param>
/// <param name="padTo">Pad to given length</param>
/// <param name="padWith">Pad with value</param>
/// <param name="maxLength">Maximum length of the input <see cref="value"/></param>
/// <param name="padTo">Pad the input <see cref="value"/> to given length</param>
/// <param name="padWith">Pad the input <see cref="value"/> with this character value</param>
/// <returns>Encoded data.</returns>
public static byte[] SetString6(string value, int maxLength, int padTo = 0, ushort padWith = 0)
{
@ -155,10 +155,10 @@ namespace PKHeX.Core
/// <summary>Gets the bytes for a Generation 7 string.</summary>
/// <param name="value">Decoded string.</param>
/// <param name="maxLength">Maximum length</param>
/// <param name="maxLength">Maximum length of the input <see cref="value"/></param>
/// <param name="language">Language specific conversion (Chinese)</param>
/// <param name="padTo">Pad to given length</param>
/// <param name="padWith">Pad with value</param>
/// <param name="padTo">Pad the input <see cref="value"/> to given length</param>
/// <param name="padWith">Pad the input <see cref="value"/> with this character value</param>
/// <param name="chinese">Chinese string remapping should be attempted</param>
/// <returns>Encoded data.</returns>
public static byte[] SetString7(string value, int maxLength, int language, int padTo = 0, ushort padWith = 0, bool chinese = false)
@ -182,10 +182,10 @@ namespace PKHeX.Core
/// <summary>Gets the bytes for a Generation 7 string.</summary>
/// <param name="value">Decoded string.</param>
/// <param name="maxLength">Maximum length</param>
/// <param name="maxLength">Maximum length of the input <see cref="value"/></param>
/// <param name="language">Language specific conversion (Chinese)</param>
/// <param name="padTo">Pad to given length</param>
/// <param name="padWith">Pad with value</param>
/// <param name="padTo">Pad the input <see cref="value"/> to given length</param>
/// <param name="padWith">Pad the input <see cref="value"/> with this character value</param>
/// <param name="chinese">Chinese string remapping should be attempted</param>
/// <returns>Encoded data.</returns>
public static byte[] SetString7b(string value, int maxLength, int language, int padTo = 0, ushort padWith = 0, bool chinese = false)

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -74,7 +75,7 @@ namespace PKHeX.Core
{
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 @@ namespace PKHeX.Core
/// Converts a string to Generation 1 encoded data.
/// </summary>
/// <param name="value">Decoded string.</param>
/// <param name="maxLength">Maximum length</param>
/// <param name="maxLength">Maximum length of the input <see cref="value"/></param>
/// <param name="jp">Data destination is Japanese.</param>
/// <param name="padTo">Pad to given length</param>
/// <param name="padWith">Pad with value</param>
/// <param name="padTo">Pad the input <see cref="value"/> to given length</param>
/// <param name="padWith">Pad the input <see cref="value"/> with this character value</param>
/// <returns>Encoded data.</returns>
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<byte>(padTo);
if (value.Length > maxLength)
value = value.Substring(0, maxLength); // Hard cap
var capacity = Math.Max(value.Length, padTo);
var arr = new List<byte>(capacity);
var dict = jp ? U2RBY_J : U2RBY_U;
foreach (char c in value)
{

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -22,16 +23,16 @@ namespace PKHeX.Core
/// <returns>Decoded string.</returns>
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 @@ namespace PKHeX.Core
/// Converts a string to Generation 1 encoded data.
/// </summary>
/// <param name="value">Decoded string.</param>
/// <param name="maxLength">Maximum length</param>
/// <param name="padTo">Pad to given length</param>
/// <param name="padWith">Pad with value</param>
/// <param name="maxLength">Maximum length of the input <see cref="value"/></param>
/// <param name="padTo">Pad the input <see cref="value"/> to given length</param>
/// <param name="padWith">Pad the input <see cref="value"/> with this character value</param>
/// <returns>Encoded data.</returns>
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<byte>(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<byte>(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;

View file

@ -18,7 +18,7 @@ namespace PKHeX.Core
/// <returns>Decoded string.</returns>
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 @@ namespace PKHeX.Core
/// Converts a string to a Generation 3 encoded value array.
/// </summary>
/// <param name="value">Decoded string.</param>
/// <param name="maxLength">Maximum length of string</param>
/// <param name="maxLength">Maximum length of the input <see cref="value"/></param>
/// <param name="jp">String destination is Japanese font.</param>
/// <param name="padTo">Pad to given length</param>
/// <param name="padWith">Pad with value</param>
/// <param name="padTo">Pad the input <see cref="value"/> to given length</param>
/// <param name="padWith">Pad the input <see cref="value"/> with this character value</param>
/// <returns></returns>
public static byte[] SetString3(string value, int maxLength, bool jp, int padTo = 0, ushort padWith = 0)
{
@ -85,9 +85,9 @@ namespace PKHeX.Core
/// <summary>Gets the bytes for a Big Endian string.</summary>
/// <param name="value">Decoded string.</param>
/// <param name="maxLength">Maximum length</param>
/// <param name="padTo">Pad to given length</param>
/// <param name="padWith">Pad with value</param>
/// <param name="maxLength">Maximum length of the input <see cref="value"/></param>
/// <param name="padTo">Pad the input <see cref="value"/> to given length</param>
/// <param name="padWith">Pad the input <see cref="value"/> with this character value</param>
/// <returns>Encoded data.</returns>
public static byte[] SetBEString3(string value, int maxLength, int padTo = 0, ushort padWith = 0)
{

View file

@ -17,7 +17,7 @@ namespace PKHeX.Core
/// <returns>Decoded string.</returns>
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 @@ namespace PKHeX.Core
/// <summary>Gets the bytes for a 4th Generation String</summary>
/// <param name="value">Decoded string.</param>
/// <param name="maxLength">Maximum length</param>
/// <param name="padTo">Pad to given length</param>
/// <param name="padWith">Pad with value</param>
/// <param name="maxLength">Maximum length of the input <see cref="value"/></param>
/// <param name="padTo">Pad the input <see cref="value"/> to given length</param>
/// <param name="padWith">Pad the input <see cref="value"/> with this character value</param>
/// <returns>Encoded data.</returns>
public static byte[] SetString4(string value, int maxLength, int padTo = 0, ushort padWith = 0)
{
@ -71,7 +71,7 @@ namespace PKHeX.Core
/// <returns>Converted string.</returns>
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 @@ namespace PKHeX.Core
/// </summary>
/// <param name="value">String to be converted.</param>
/// <param name="maxLength">Maximum length of string</param>
/// <param name="padTo">Pad to given length</param>
/// <param name="padWith">Pad with value</param>
/// <param name="padTo">Pad the input <see cref="value"/> to given length</param>
/// <param name="padWith">Pad the input <see cref="value"/> with this character value</param>
/// <returns>Byte array containing encoded character data</returns>
public static byte[] SetBEString4(string value, int maxLength, int padTo = 0, ushort padWith = 0)
{

View file

@ -59,7 +59,7 @@ namespace PKHeX.Core
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)

View file

@ -26,7 +26,7 @@ namespace PKHeX.Drawing
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)