mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Nodes - Added string contains node
Nodes - Added string regex match node
This commit is contained in:
parent
d2e0607622
commit
c45e879eef
32
src/Artemis.VisualScripting/Nodes/Text/StringContainsNode.cs
Normal file
32
src/Artemis.VisualScripting/Nodes/Text/StringContainsNode.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using Artemis.Core;
|
||||
|
||||
namespace Artemis.VisualScripting.Nodes.Text;
|
||||
|
||||
[Node("Contains", "Checks whether the first input is contained in the second input.", "Text", InputType = typeof(string), OutputType = typeof(bool))]
|
||||
public class StringContainsNode : Node
|
||||
{
|
||||
public StringContainsNode()
|
||||
: base("Contains", "Checks whether the first input is contained in the second input.")
|
||||
{
|
||||
Input1 = CreateInputPin<string>();
|
||||
Input2 = CreateInputPin<string>();
|
||||
Result = CreateOutputPin<bool>();
|
||||
}
|
||||
|
||||
public InputPin<string> Input1 { get; }
|
||||
public InputPin<string> Input2 { get; }
|
||||
|
||||
public OutputPin<bool> Result { get; }
|
||||
|
||||
public override void Evaluate()
|
||||
{
|
||||
if (Input1.Value == null && Input2.Value == null)
|
||||
Result.Value = false;
|
||||
else if (Input1.Value == null && Input2.Value != null)
|
||||
Result.Value = false;
|
||||
else if (Input1.Value != null && Input2.Value == null)
|
||||
Result.Value = true;
|
||||
else if (Input1.Value != null && Input2.Value != null)
|
||||
Result.Value = Input1.Value.Contains(Input2.Value, StringComparison.InvariantCultureIgnoreCase);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Artemis.Core;
|
||||
|
||||
namespace Artemis.VisualScripting.Nodes.Text;
|
||||
|
||||
[Node("Regex Match", "Checks provided regex pattern matches the input.", "Text", InputType = typeof(string), OutputType = typeof(bool))]
|
||||
public class StringRegexMatchNode : Node
|
||||
{
|
||||
private string? _lastPattern;
|
||||
private Regex? _regex;
|
||||
private bool _broken;
|
||||
|
||||
public StringRegexMatchNode() : base("Regex Match", "Checks provided regex pattern matches the input.")
|
||||
{
|
||||
Pattern = CreateInputPin<string>("Pattern");
|
||||
Input = CreateInputPin<string>("Input");
|
||||
Result = CreateOutputPin<bool>();
|
||||
}
|
||||
|
||||
public InputPin<string> Pattern { get; }
|
||||
public InputPin<string> Input { get; }
|
||||
public OutputPin<bool> Result { get; }
|
||||
|
||||
public override void Evaluate()
|
||||
{
|
||||
if (Input.Value == null || Pattern.Value == null)
|
||||
return;
|
||||
if (_broken && _lastPattern == Pattern.Value)
|
||||
return;
|
||||
|
||||
if (_regex == null || _lastPattern != Pattern.Value)
|
||||
{
|
||||
try
|
||||
{
|
||||
_regex = new Regex(Pattern.Value, RegexOptions.Compiled);
|
||||
_broken = false;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
_broken = true;
|
||||
return;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_lastPattern = Pattern.Value;
|
||||
}
|
||||
}
|
||||
|
||||
Result.Value = _regex.IsMatch(Input.Value);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user