mirror of
https://github.com/kwsch/PKHeX
synced 2024-12-11 13:13:06 +00:00
Replace regex parse with string find
Only remaining uses of regex in the sln are wordfilter (required), and unescape for QR replies from web api. This is slightly faster -- cover all cases if the response json changes (shouldn't).
This commit is contained in:
parent
ad4aec3439
commit
4e12e54028
1 changed files with 12 additions and 7 deletions
|
@ -1,12 +1,9 @@
|
|||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace PKHeX.Core
|
||||
{
|
||||
public static class UpdateUtil
|
||||
{
|
||||
private static readonly Regex LatestGitTagRegex = new("\\\"tag_name\"\\s*\\:\\s*\\\"([0-9]+\\.[0-9]+\\.[0-9]+)\\\""); // Match `"tag_name": "18.12.02"`. Group 1 is `18.12.02`
|
||||
|
||||
/// <summary>
|
||||
/// Gets the latest version of PKHeX according to the Github API
|
||||
/// </summary>
|
||||
|
@ -18,12 +15,20 @@ namespace PKHeX.Core
|
|||
if (responseJson is null)
|
||||
return null;
|
||||
|
||||
// Using a regex to get the tag to avoid importing an entire JSON parsing library
|
||||
var tagMatch = LatestGitTagRegex.Match(responseJson);
|
||||
if (!tagMatch.Success)
|
||||
// Parse it manually; no need to parse the entire json to object.
|
||||
const string tag = "tag_name";
|
||||
var index = responseJson.IndexOf(tag, StringComparison.Ordinal);
|
||||
if (index == -1)
|
||||
return null;
|
||||
|
||||
var tagString = tagMatch.Groups[1].Value;
|
||||
var first = responseJson.IndexOf('"', index + tag.Length + 1) + 1;
|
||||
if (first == 0)
|
||||
return null;
|
||||
var second = responseJson.IndexOf('"', first);
|
||||
if (second == -1)
|
||||
return null;
|
||||
|
||||
var tagString = responseJson[first..second];
|
||||
return !Version.TryParse(tagString, out var latestVersion) ? null : latestVersion;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue