mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-12 14:34:08 +02:00
Add StringUtils.SplitInTwo & use it in RequestHandlerBase
This commit is contained in:
parent
ff5e1da14d
commit
25680fa980
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user