Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ private static string CreateExpanderTitle(string title)
return sb.ToString();
}

private static readonly Regex newLineRegex = new Regex(@"\r\n?|\n", RegexOptions.Compiled);

private static string CreateNodeInfo(OpenNodeAnnotationEventArgs e)
{
StringBuilder sb = new StringBuilder();
Expand All @@ -245,7 +247,7 @@ private static string CreateNodeInfo(OpenNodeAnnotationEventArgs e)
sb.AppendLine($"<p>{e.OriginalName}</p>");
}
sb.AppendLine($"<h2>{Resources.NodeDocumentationDescription}</h2>");
sb.AppendLine($"<p>{Regex.Replace(e.Description, @"\r\n?|\n", "<br>")}</p>");
sb.AppendLine($"<p>{newLineRegex.Replace(e.Description, "<br>")}</p>");

return sb.ToString();
}
Expand Down
2 changes: 1 addition & 1 deletion src/DynamoCore/Engine/MigrateFormulaToDS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class MigrateFormulaToDS : AstReplacer
/// recognize this "if" syntax.
/// </summary>
private const string ifCond = @"(if\s*)\(\s*(.*?)\s*\)$";
private static readonly Regex ifRgx = new Regex(ifCond, RegexOptions.IgnoreCase);
private static readonly Regex ifRgx = new Regex(ifCond, RegexOptions.IgnoreCase | RegexOptions.Compiled);


internal string ConvertFormulaToDS(string formula)
Expand Down
15 changes: 10 additions & 5 deletions src/DynamoCore/Graph/Nodes/CodeBlockUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ internal class CodeCompletionParser
// This pattern matches with identifier lists such as Autodesk.DesignScript.Geometry.Point
private static string identifierListPattern = string.Format("{0}([.]({0})+)*", variableNamePattern);


// Maintains a stack of symbols in a nested expression being typed
// where the symbols are nested based on brackets, braces or parentheses
// An expression like: x[y[z.foo(). will be stacked up as follows:
Expand Down Expand Up @@ -415,19 +416,23 @@ public static bool IsInsideCommentOrString(string text, int caretPos)

#region private methods

// This pattern is used to create a Regex to match expressions such as "a : Point" and add the Pair of ("a", "Point") to the dictionary
private static readonly string variablePattern = variableNamePattern + spacesOrNonePattern + colonPattern + spacesOrNonePattern + identifierListPattern;
private static readonly Regex variableRegex = new Regex(variablePattern, RegexOptions.Compiled);

private static readonly Regex whitespaceRegex = new Regex(@"\s", RegexOptions.Compiled);


private static Dictionary<string, string> FindVariableTypes(string code)
{
// Contains pairs of variable names and their types
Dictionary<string, string> variableTypes = new Dictionary<string, string>();

// This pattern is used to create a Regex to match expressions such as "a : Point" and add the Pair of ("a", "Point") to the dictionary
string pattern = variableNamePattern + spacesOrNonePattern + colonPattern + spacesOrNonePattern + identifierListPattern;

var varDeclarations = Regex.Matches(code, pattern);
var varDeclarations = variableRegex.Matches(code);
for (int i = 0; i < varDeclarations.Count; i++)
{
var match = varDeclarations[i].Value;
match = Regex.Replace(match, @"\s", "");
match = whitespaceRegex.Replace(match, "");
var groups = match.Split(':');

// Overwrite variable type for a redefinition
Expand Down
10 changes: 4 additions & 6 deletions src/DynamoCoreWpf/ViewModels/Core/NodeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,9 @@ void DebugSettings_PropertyChanged(object sender, PropertyChangedEventArgs e)
}
}

private const string guidRegexStr = @"([0-9a-f-]{32}).*?";
private static Regex guidRegex = new Regex(guidRegexStr, RegexOptions.None | RegexOptions.Compiled);

/// <summary>
/// Handler for the EngineController's AstBuilt event.
/// Formats a string of AST for preview on the node.
Expand All @@ -1095,13 +1098,8 @@ void EngineController_AstBuilt(object sender, CompiledEventArgs e)
foreach (var assocNode in e.AstNodes)
{
var pretty = assocNode.ToString();

//shorten the guids
var strRegex = @"([0-9a-f-]{32}).*?";
var myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = assocNode.ToString();

foreach (Match myMatch in myRegex.Matches(strTargetString))
foreach (Match myMatch in guidRegex.Matches(pretty))
{
if (myMatch.Success)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ public string Description
/// <value>
/// A string of space-delimited keywords</value>
private string _Keywords = "";

private static readonly Regex keyWordsRegex = new Regex(@"[ ]{2,}", RegexOptions.None | RegexOptions.Compiled);

public string Keywords
{
get { return _Keywords; }
Expand All @@ -315,9 +318,7 @@ public string Keywords
if (_Keywords != value)
{
value = value.Replace(',', ' ').ToLower();
var options = RegexOptions.None;
var regex = new Regex(@"[ ]{2,}", options);
value = regex.Replace(value, @" ");
value = keyWordsRegex.Replace(value, @" ");

_Keywords = value;
RaisePropertyChanged("Keywords");
Expand Down Expand Up @@ -2319,9 +2320,9 @@ private void Submit()
}

private static readonly Regex UserIsNotAMaintainerRegex =
new Regex("^The user sending the new package version, ([^,]+), is not a maintainer of the package (.*)$");
new Regex("^The user sending the new package version, ([^,]+), is not a maintainer of the package (.*)$", RegexOptions.Compiled);
private static readonly Regex PackageAlreadyExistsRegex =
new Regex("^A package with the given name and engine already exists\\.$");
new Regex("^A package with the given name and engine already exists\\.$", RegexOptions.Compiled);

/// <summary>
/// Inspects an error message to see if it matches a known Package Manager error message. If it does,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,12 @@ private NodeModelTypeId GetInfoFromTypeId(string typeId)
return new NodeModelTypeId(typeId);
}

private static readonly Regex variableInputPortRegex = new Regex(@"\d+$", RegexOptions.Compiled);

// Remove the digits at the end of the portname for variable input node
private string ParseVariableInputPortName(string portName)
{
string pattern = @"\d+$";
Regex rgx = new Regex(pattern);
return rgx.Replace(portName, string.Empty);
return variableInputPortRegex.Replace(portName, string.Empty);
}

// Get the host name from the enum list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2898,6 +2898,8 @@ internal static IEnumerable<string> FindIdentifiersForSelectedNodes(IEnumerable<
return selectedNodes.SelectMany(n => n.OutPorts.Select(p => n.GetAstIdentifierForOutputIndex(p.Index).Value));
}

private static readonly Regex outIdRegex = new Regex("_out[0-9]", RegexOptions.Compiled);

/// <summary>
/// Find all output identifiers for all custom nodes in the provided workspace.
/// </summary>
Expand All @@ -2912,7 +2914,7 @@ internal static IEnumerable<string> FindIdentifiersForCustomNodes(HomeWorkspaceM
}

// Remove the output identifier appended to the custom node outputs.
var rgx = new Regex("_out[0-9]");
var rgx = outIdRegex;

var customNodes = workspace.Nodes.Where(n => n is Function);
var idents = new List<string>();
Expand Down
71 changes: 47 additions & 24 deletions src/DynamoCoreWpf/Views/CodeBlocks/CodeBlockEditorUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Media;
Expand All @@ -17,6 +19,30 @@ namespace Dynamo.Wpf.Views
/// </summary>
public static class CodeHighlightingRuleFactory
{
// These Regex's must match with the grammars in the DS ATG for digits
// Refer to the 'number' and 'float' tokens in Start.atg
//*******************************************************************************
// number = digit {digit} .
// float = digit {digit} '.' digit {digit} [('E' | 'e') ['+'|'-'] digit {digit}].
//*******************************************************************************

[StringSyntax("regex")]
private const string digit = @"(-?\b\d+)";

[StringSyntax("regex")]
private const string floatingPoint = @"(\.[0-9]+)";

[StringSyntax("regex")]
private const string exponent = @"([eE][+-]?[0-9]+)";

private const string numberWithExponent = digit + floatingPoint + exponent;
private const string numberWithOptionalDecimal = digit + floatingPoint + "?";
private const string finalRegex = numberWithExponent + "|" + numberWithOptionalDecimal;

private static readonly Regex highlightRegex = new Regex(finalRegex, RegexOptions.Compiled);

private static readonly Dictionary<string, Regex> presetRegexes = new();

/// <summary>
/// Create hight lighting rule for number.
/// </summary>
Expand All @@ -30,22 +56,7 @@ public static HighlightingRule CreateNumberHighlightingRule()
{
Foreground = new CustomizedBrush(color)
};

// These Regex's must match with the grammars in the DS ATG for digits
// Refer to the 'number' and 'float' tokens in Start.atg
//*******************************************************************************
// number = digit {digit} .
// float = digit {digit} '.' digit {digit} [('E' | 'e') ['+'|'-'] digit {digit}].
//*******************************************************************************

string digit = @"(-?\b\d+)";
string floatingPoint = @"(\.[0-9]+)";
string numberWithOptionalDecimal = digit + floatingPoint + "?";

string exponent = @"([eE][+-]?[0-9]+)";
string numberWithExponent = digit + floatingPoint + exponent;

digitRule.Regex = new Regex(numberWithExponent + "|" + numberWithOptionalDecimal);
digitRule.Regex = highlightRegex;

return digitRule;
}
Expand All @@ -57,6 +68,9 @@ public static HighlightingRule CreateNumberHighlightingRule()
/// <returns></returns>
public static HighlightingRule CreateClassHighlightRule(EngineController engineController)
{
var wordList = engineController.CodeCompletionServices.GetClasses();
if (!wordList.Any()) return null;

Color color = (Color)ColorConverter.ConvertFromString("#b7d78c");
var classHighlightRule = new HighlightingRule
{
Expand All @@ -66,11 +80,14 @@ public static HighlightingRule CreateClassHighlightRule(EngineController engineC
}
};

var wordList = engineController.CodeCompletionServices.GetClasses();
if (!wordList.Any()) return null;
var words = string.Join("|", wordList);
if (!presetRegexes.TryGetValue(words, out var regex))
{
regex = new Regex(string.Format(@"\b({0})\b", words), RegexOptions.Compiled);
presetRegexes[words] = regex;
}

String regex = String.Format(@"\b({0})\b", String.Join("|", wordList));
classHighlightRule.Regex = new Regex(regex);
classHighlightRule.Regex = regex;

return classHighlightRule;
}
Expand All @@ -81,6 +98,9 @@ public static HighlightingRule CreateClassHighlightRule(EngineController engineC
/// <returns></returns>
public static HighlightingRule CreateMethodHighlightRule(EngineController engineController)
{
var wordList = engineController.CodeCompletionServices.GetGlobals();
if (!wordList.Any()) return null;

Color color = (Color)ColorConverter.ConvertFromString("#84d7ce");
var methodHighlightRule = new HighlightingRule
{
Expand All @@ -90,11 +110,14 @@ public static HighlightingRule CreateMethodHighlightRule(EngineController engine
}
};

var wordList = engineController.CodeCompletionServices.GetGlobals();
if (!wordList.Any()) return null;
var words = string.Join("|", wordList);
if (!presetRegexes.TryGetValue(words, out var regex))
{
regex = new Regex(string.Format(@"\b({0})\b", words), RegexOptions.Compiled);
presetRegexes[words] = regex;
}

String regex = String.Format(@"\b({0})\b", String.Join("|", wordList));
methodHighlightRule.Regex = new Regex(regex);
methodHighlightRule.Regex = regex;

return methodHighlightRule;
}
Expand Down
5 changes: 3 additions & 2 deletions src/DynamoCoreWpf/Views/Menu/PreferencesView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,11 +570,12 @@ private void OnRequestShowFileDialog(object sender, EventArgs e)
}
}

private static readonly Regex numberRegex = new Regex("[^0-9]+", RegexOptions.Compiled);

// Number input textbox validation
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
e.Handled = numberRegex.IsMatch(e.Text);
}

internal void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Dynamo.PackageManager.UI
/// </summary>
public partial class NumericUpDownControl : UserControl
{
private static readonly Regex numberMatchRegex = new Regex(@"^(?:0|[1-9]\d*)$", RegexOptions.Compiled);

#region Private properties
private Regex _numMatch;
private bool mouseClickSelection = true;
Expand Down Expand Up @@ -74,7 +76,7 @@ public NumericUpDownControl()
InitializeComponent();

// Allows positive whole numbers
_numMatch = new Regex(@"^(?:0|[1-9]\d*)$");
_numMatch = numberMatchRegex;
}

#region UI utility functions
Expand Down
37 changes: 23 additions & 14 deletions src/DynamoUtilities/PIIDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ public static Tuple<JObject,bool> RemovePIIData(JObject jsonObject)
static string ipPattern = @"((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
static string datePattern = @"\d{1,2}[/-]\d{1,2}[/-]\d{2,4}";

private static readonly Regex emailPatternRegex = new Regex(emailPattern, RegexOptions.Compiled);
private static readonly Regex websitePatternRegex = new Regex(websitePattern, RegexOptions.Compiled);
private static readonly Regex directoryPatternRegex = new Regex(directoryPattern, RegexOptions.Compiled);
private static readonly Regex creditCardPatternRegex = new Regex(creditCardPattern, RegexOptions.Compiled);
private static readonly Regex ssnPatternRegex = new Regex(ssnPattern, RegexOptions.Compiled);
private static readonly Regex ipPatternRegex = new Regex(ipPattern, RegexOptions.Compiled);
private static readonly Regex datePatternRegex = new Regex(datePattern, RegexOptions.Compiled);

public static JToken GetNodeById(JObject jsonWorkspace,string nodeId)
{
return jsonWorkspace["Nodes"].Where(t => t.Value<string>("Id") == nodeId).Select(t => t).FirstOrDefault();
Expand All @@ -102,13 +110,13 @@ public static JToken GetNoteValue(JObject jsonWorkspace, string nodeId)
return property.Value;
}

internal static bool ContainsEmail(string value) { return new Regex(emailPattern).Match(value).Success; }
internal static bool ContainsWebsite(string value) { return new Regex(websitePattern).Match(value).Success; }
internal static bool ContainsDirectory(string value) { return new Regex(directoryPattern).Match(value).Success; }
internal static bool ContainsCreditCard(string value) { return new Regex(creditCardPattern).Match(value).Success; }
internal static bool ContainsSSN(string value) { return new Regex(ssnPattern).Match(value).Success; }
internal static bool ContainsIpAddress(string value) { return new Regex(ipPattern).Match(value).Success; }
internal static bool ContainsDate(string value) { return new Regex(datePattern).Match(value).Success; }
internal static bool ContainsEmail(string value) { return emailPatternRegex.Match(value).Success; }
internal static bool ContainsWebsite(string value) { return websitePatternRegex.Match(value).Success; }
internal static bool ContainsDirectory(string value) { return directoryPatternRegex.Match(value).Success; }
internal static bool ContainsCreditCard(string value) { return creditCardPatternRegex.Match(value).Success; }
internal static bool ContainsSSN(string value) { return ssnPatternRegex.Match(value).Success; }
internal static bool ContainsIpAddress(string value) { return ipPatternRegex.Match(value).Success; }
internal static bool ContainsDate(string value) { return datePatternRegex.Match(value).Success; }

/// <summary>
/// Removes the PII data based on the information patterns
Expand All @@ -119,13 +127,14 @@ internal static string RemovePIIData(string data)
{
string result;

result = Regex.Replace(data, emailPattern, "");
result = Regex.Replace(result, directoryPattern, "");
result = Regex.Replace(result, ssnPattern, "");
result = Regex.Replace(result, datePattern, "");
result = Regex.Replace(result, ipPattern, "");
result = Regex.Replace(result, creditCardPattern, "");
result = Regex.Replace(result, websitePattern, "");
result = emailPatternRegex.Replace(data, "");

result = directoryPatternRegex.Replace(result, "");
result = ssnPatternRegex.Replace(result, "");
result = datePatternRegex.Replace(result, "");
result = ipPatternRegex.Replace(result, "");
result = creditCardPatternRegex.Replace(result, "");
result = websitePatternRegex.Replace(result, "");

return result;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Engine/ProtoCore/Utils/ParserUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -87,10 +87,10 @@ public static IEnumerable<Node> FindExprListNodes(CodeBlockNode node)
return ListCollector.Collect(node);
}

private static readonly Regex spaceNormalizationRegex = new Regex(@"([^\S]+)", RegexOptions.Compiled);

public static List<string> GetLHSatAssignment(string line, int equalIndex)
{
var spaceNormalizationRule = new Regex(@"([^\S]+)");

// the line could have multiple program statements separated by ';'
var programStatements = line.Split(';');

Expand All @@ -114,7 +114,7 @@ public static List<string> GetLHSatAssignment(string line, int equalIndex)
else if (identifier.Equals("return"))
continue;

identifier = spaceNormalizationRule.Replace(identifier, string.Empty);
identifier = spaceNormalizationRegex.Replace(identifier, string.Empty);

// Check if identifier contains ':' and extract the lhs of the ':'
identifier = identifier.Split(':')[0];
Expand Down
Loading
Loading