C
C#10mo ago
Ne1gh

How can i simplify multiple conditions?

4 Replies
Jimmacle
Jimmacle10mo ago
$paste
MODiX
MODiX10mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Ne1gh
Ne1ghOP10mo ago
nice cat :)
oke
oke10mo ago
personally, i think the if statements were perfectly fine but there were a few other places that could use optimizations or just general replacements i replaced .Substring() with ranges, replaced the string with a StringBuilder in GetFormattingCode(), and a few small things
public LocaleSymbol HandleFormatting(string formattingCode, LocaleSymbol prevSymbol)
{
if (formattingCode == "#")
prevSymbol.Wrap = true;
else if (formattingCode[0] == '%')
prevSymbol.TextSpeed = int.Parse(formattingCode[1..]);
else if (formattingCode.Contains('~'))
{
var perLetter = formattingCode.Contains('@');
var parsedShakingData = formattingCode[(1 + perLetter.ToInt())..].Split(", ");
var shaking = new Shaking((Vector2.One * int.Parse(parsedShakingData[0])).ToVector3());

shaking.IsLerped = bool.Parse(parsedShakingData[1]);

prevSymbol.Shaking = shaking;
prevSymbol.ShakingWholeFragment = !perLetter;
}
else if (formattingCode[0] == '&')
{
if (formattingCode.Contains("New"))
{
string colorValuesInString = formattingCode[5..].Replace(')', '\0');

string[] colorValues = colorValuesInString.Split(", ");

prevSymbol.Color = new Color(
Convert.ToByte(colorValues[0]),
Convert.ToByte(colorValues[1]),
Convert.ToByte(colorValues[2])) * Convert.ToSingle(colorValues[3]);
}
else
prevSymbol.Color = ColorExtensions.NameToColor(formattingCode[1..]);
}

return prevSymbol;
}

public string GetFormattingCode(string text)
{
// Use a string builder to reduce memory usage
StringBuilder sb = new();

foreach (var c in text)
{
if (c == '>')
break;

formattingCode.Append(c);
}

return formattingCode.ToString();
}
public LocaleSymbol HandleFormatting(string formattingCode, LocaleSymbol prevSymbol)
{
if (formattingCode == "#")
prevSymbol.Wrap = true;
else if (formattingCode[0] == '%')
prevSymbol.TextSpeed = int.Parse(formattingCode[1..]);
else if (formattingCode.Contains('~'))
{
var perLetter = formattingCode.Contains('@');
var parsedShakingData = formattingCode[(1 + perLetter.ToInt())..].Split(", ");
var shaking = new Shaking((Vector2.One * int.Parse(parsedShakingData[0])).ToVector3());

shaking.IsLerped = bool.Parse(parsedShakingData[1]);

prevSymbol.Shaking = shaking;
prevSymbol.ShakingWholeFragment = !perLetter;
}
else if (formattingCode[0] == '&')
{
if (formattingCode.Contains("New"))
{
string colorValuesInString = formattingCode[5..].Replace(')', '\0');

string[] colorValues = colorValuesInString.Split(", ");

prevSymbol.Color = new Color(
Convert.ToByte(colorValues[0]),
Convert.ToByte(colorValues[1]),
Convert.ToByte(colorValues[2])) * Convert.ToSingle(colorValues[3]);
}
else
prevSymbol.Color = ColorExtensions.NameToColor(formattingCode[1..]);
}

return prevSymbol;
}

public string GetFormattingCode(string text)
{
// Use a string builder to reduce memory usage
StringBuilder sb = new();

foreach (var c in text)
{
if (c == '>')
break;

formattingCode.Append(c);
}

return formattingCode.ToString();
}
Want results from more Discord servers?
Add your server