optimize string splits

This commit is contained in:
Jesse Plamondon-Willard 2022-10-09 14:30:40 -04:00
parent 3d10d08a1a
commit 8dc12fd01c
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
8 changed files with 12 additions and 11 deletions

View File

@ -11,6 +11,7 @@
* For players: * For players:
* The SMAPI installer now also detects game folders listed in Steam's `.vdf` library data on Windows (thanks to pizzaoverhead!). * The SMAPI installer now also detects game folders listed in Steam's `.vdf` library data on Windows (thanks to pizzaoverhead!).
* Optimized performance and memory usage (thanks to atravita!). * Optimized performance and memory usage (thanks to atravita!).
* Other internal optimizations.
* Added more file extensions to ignore when searching for mod folders: `.7z`, `.tar`, `.tar.gz`, and `.xcf` (thanks to atravita!). * Added more file extensions to ignore when searching for mod folders: `.7z`, `.tar`, `.tar.gz`, and `.xcf` (thanks to atravita!).
* Fixed update alerts incorrectly shown for prerelease versions on GitHub that aren't marked as prerelease. * Fixed update alerts incorrectly shown for prerelease versions on GitHub that aren't marked as prerelease.

View File

@ -63,7 +63,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player
/// <param name="color">The color to set.</param> /// <param name="color">The color to set.</param>
private bool TryParseColor(string input, out Color color) private bool TryParseColor(string input, out Color color)
{ {
string[] colorHexes = input.Split(new[] { ',' }, 3); string[] colorHexes = input.Split(',', 3);
if (int.TryParse(colorHexes[0], out int r) && int.TryParse(colorHexes[1], out int g) && int.TryParse(colorHexes[2], out int b)) if (int.TryParse(colorHexes[0], out int r) && int.TryParse(colorHexes[1], out int g) && int.TryParse(colorHexes[2], out int b))
{ {
color = new Color(r, g, b); color = new Color(r, g, b);

View File

@ -136,11 +136,11 @@ namespace SMAPI.Tests.Utilities
foreach (string rawPair in stateMap.Split(',')) foreach (string rawPair in stateMap.Split(','))
{ {
// parse values // parse values
string[] parts = rawPair.Split(new[] { ':' }, 2); string[] parts = rawPair.Split(':', 2, StringSplitOptions.TrimEntries);
if (!Enum.TryParse(parts[0], ignoreCase: true, out SButton curButton)) if (!Enum.TryParse(parts[0], ignoreCase: true, out SButton curButton))
Assert.Fail($"The state map is invalid: unknown button value '{parts[0].Trim()}'"); Assert.Fail($"The state map is invalid: unknown button value '{parts[0]}'");
if (!Enum.TryParse(parts[1], ignoreCase: true, out SButtonState state)) if (!Enum.TryParse(parts[1], ignoreCase: true, out SButtonState state))
Assert.Fail($"The state map is invalid: unknown state value '{parts[1].Trim()}'"); Assert.Fail($"The state map is invalid: unknown state value '{parts[1]}'");
// get state // get state
if (curButton == button) if (curButton == button)

View File

@ -121,10 +121,10 @@ namespace StardewModdingAPI.Web.Framework.Clients.Nexus
HtmlNode? node = doc.DocumentNode.SelectSingleNode("//div[contains(@class, 'site-notice')][contains(@class, 'warning')]"); HtmlNode? node = doc.DocumentNode.SelectSingleNode("//div[contains(@class, 'site-notice')][contains(@class, 'warning')]");
if (node != null) if (node != null)
{ {
string[] errorParts = node.InnerText.Trim().Split(new[] { '\n' }, 2, System.StringSplitOptions.RemoveEmptyEntries); string[] errorParts = node.InnerText.Trim().Split('\n', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
string errorCode = errorParts[0]; string errorCode = errorParts[0];
string? errorText = errorParts.Length > 1 ? errorParts[1] : null; string? errorText = errorParts.Length > 1 ? errorParts[1] : null;
switch (errorCode.Trim().ToLower()) switch (errorCode.ToLower())
{ {
case "not found": case "not found":
return null; return null;

View File

@ -223,7 +223,7 @@ namespace StardewModdingAPI.Framework.Logging
// show update alert // show update alert
if (File.Exists(Constants.UpdateMarker)) if (File.Exists(Constants.UpdateMarker))
{ {
string[] rawUpdateFound = File.ReadAllText(Constants.UpdateMarker).Split(new[] { '|' }, 2); string[] rawUpdateFound = File.ReadAllText(Constants.UpdateMarker).Split('|', 2);
if (SemanticVersion.TryParse(rawUpdateFound[0], out ISemanticVersion? updateFound)) if (SemanticVersion.TryParse(rawUpdateFound[0], out ISemanticVersion? updateFound))
{ {
if (Constants.ApiVersion.IsPrerelease() && updateFound.IsNewerThan(Constants.ApiVersion)) if (Constants.ApiVersion.IsPrerelease() && updateFound.IsNewerThan(Constants.ApiVersion))

View File

@ -58,7 +58,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
MethodReference? methodRef = RewriteHelper.AsMethodReference(instruction); MethodReference? methodRef = RewriteHelper.AsMethodReference(instruction);
if (methodRef != null && methodRef.DeclaringType.FullName == this.FullTypeName && this.MethodNames.Contains(methodRef.Name)) if (methodRef != null && methodRef.DeclaringType.FullName == this.FullTypeName && this.MethodNames.Contains(methodRef.Name))
{ {
string eventName = methodRef.Name.Split(new[] { '_' }, 2)[1]; string eventName = methodRef.Name.Split('_', 2)[1];
this.MethodNames.Remove($"add_{eventName}"); this.MethodNames.Remove($"add_{eventName}");
this.MethodNames.Remove($"remove_{eventName}"); this.MethodNames.Remove($"remove_{eventName}");

View File

@ -1384,7 +1384,7 @@ namespace StardewModdingAPI.Framework
} }
// check min length for specific types // check min length for specific types
switch (fields[SObject.objectInfoTypeIndex].Split(new[] { ' ' }, 2)[0]) switch (fields[SObject.objectInfoTypeIndex].Split(' ', 2)[0])
{ {
case "Cooking": case "Cooking":
if (fields.Length < SObject.objectInfoBuffDurationIndex + 1) if (fields.Length < SObject.objectInfoBuffDurationIndex + 1)

View File

@ -54,12 +54,12 @@ namespace StardewModdingAPI.Utilities
} }
// parse buttons // parse buttons
string[] rawButtons = input.Split('+'); string[] rawButtons = input.Split('+', StringSplitOptions.TrimEntries);
SButton[] buttons = new SButton[rawButtons.Length]; SButton[] buttons = new SButton[rawButtons.Length];
List<string> rawErrors = new List<string>(); List<string> rawErrors = new List<string>();
for (int i = 0; i < buttons.Length; i++) for (int i = 0; i < buttons.Length; i++)
{ {
string rawButton = rawButtons[i].Trim(); string rawButton = rawButtons[i];
if (string.IsNullOrWhiteSpace(rawButton)) if (string.IsNullOrWhiteSpace(rawButton))
rawErrors.Add("Invalid empty button value"); rawErrors.Add("Invalid empty button value");
else if (!Enum.TryParse(rawButton, ignoreCase: true, out SButton button)) else if (!Enum.TryParse(rawButton, ignoreCase: true, out SButton button))