1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-06-01 02:34:04 +02:00

Add StringUtils.SplitInTwo & use it in RequestHandlerBase

This commit is contained in:
chylex 2019-07-12 22:17:29 +02:00
parent ff5e1da14d
commit 25680fa980
2 changed files with 15 additions and 12 deletions
Core/Handling
lib/TweetLib.Core/Utils

View File

@ -7,6 +7,7 @@
using CefSharp.Handler; using CefSharp.Handler;
using TweetDuck.Core.Handling.General; using TweetDuck.Core.Handling.General;
using TweetDuck.Core.Utils; using TweetDuck.Core.Utils;
using TweetLib.Core.Utils;
namespace TweetDuck.Core.Handling{ namespace TweetDuck.Core.Handling{
class RequestHandlerBase : DefaultRequestHandler{ class RequestHandlerBase : DefaultRequestHandler{
@ -21,21 +22,13 @@ public static void LoadResourceRewriteRules(string rules){
TweetDeckHashes.Clear(); TweetDeckHashes.Clear();
foreach(string rule in rules.Replace(" ", "").ToLower().Split(',')){ 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){ if (hash.All(chr => char.IsDigit(chr) || (chr >= 'a' && chr <= 'f'))){
string key = split[0]; TweetDeckHashes.Add(key, hash);
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);
}
} }
else{ else{
throw new ArgumentException("A rule must have exactly one '=' character: "+rule); throw new ArgumentException("Invalid hash characters: " + rule);
} }
} }
} }

View File

@ -6,6 +6,16 @@ namespace TweetLib.Core.Utils{
public static class StringUtils{ public static class StringUtils{
public static readonly string[] EmptyArray = new string[0]; 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){ public static string ExtractBefore(string str, char search, int startIndex = 0){
int index = str.IndexOf(search, startIndex); int index = str.IndexOf(search, startIndex);
return index == -1 ? str : str.Substring(0, index); return index == -1 ? str : str.Substring(0, index);