diff --git a/Core/Handling/RequestHandlerBase.cs b/Core/Handling/RequestHandlerBase.cs index e3896615..4bc966a2 100644 --- a/Core/Handling/RequestHandlerBase.cs +++ b/Core/Handling/RequestHandlerBase.cs @@ -7,6 +7,7 @@ using CefSharp.Handler; using TweetDuck.Core.Handling.General; using TweetDuck.Core.Utils; +using TweetLib.Core.Utils; namespace TweetDuck.Core.Handling{ class RequestHandlerBase : DefaultRequestHandler{ @@ -21,21 +22,13 @@ public static void LoadResourceRewriteRules(string rules){ TweetDeckHashes.Clear(); foreach(string rule in rules.Replace(" ", "").ToLower().Split(',')){ - string[] split = rule.Split('='); + var (key, hash) = StringUtils.SplitInTwo(rule, '=') ?? throw new ArgumentException("A rule must have one '=' character: " + rule); - if (split.Length == 2){ - string key = split[0]; - string hash = split[1]; - - if (hash.All(chr => char.IsDigit(chr) || (chr >= 'a' && chr <= 'f'))){ - TweetDeckHashes.Add(key, hash); - } - else{ - throw new ArgumentException("Invalid hash characters: "+rule); - } + if (hash.All(chr => char.IsDigit(chr) || (chr >= 'a' && chr <= 'f'))){ + TweetDeckHashes.Add(key, hash); } else{ - throw new ArgumentException("A rule must have exactly one '=' character: "+rule); + throw new ArgumentException("Invalid hash characters: " + rule); } } } diff --git a/lib/TweetLib.Core/Utils/StringUtils.cs b/lib/TweetLib.Core/Utils/StringUtils.cs index 71afde4d..58710d4f 100644 --- a/lib/TweetLib.Core/Utils/StringUtils.cs +++ b/lib/TweetLib.Core/Utils/StringUtils.cs @@ -6,6 +6,16 @@ namespace TweetLib.Core.Utils{ public static class StringUtils{ public static readonly string[] EmptyArray = new string[0]; + public static (string before, string after)? SplitInTwo(string str, char search, int startIndex = 0){ + int index = str.IndexOf(search, startIndex); + + if (index == -1){ + return null; + } + + return (str.Substring(0, index), str.Substring(index + 1)); + } + public static string ExtractBefore(string str, char search, int startIndex = 0){ int index = str.IndexOf(search, startIndex); return index == -1 ? str : str.Substring(0, index);