// determines whether given index is inside brackets, or is after OpenBracket internal static bool IndexInsideBrackets(FindTextArgs args) { int pos = args.StartIndex; args.StartIndex = 0; while (args.StartIndex < pos) { // find open bracket args.StartIndex = args.Text.IndexOf(args.OpenBracket, args.StartIndex); if (args.StartIndex == -1) { return(false); } // missing close bracket if (!FindMatchingBrackets(args, false)) { return(true); } // pos is inside brackets if (args.StartIndex < pos && args.EndIndex > pos) { return(true); } args.StartIndex = args.EndIndex; } return(false); }
// adjusts StartIndex to the next char after end of string. Returns true if string is correct. private static bool SkipString(FindTextArgs args) { if (args.Text[args.StartIndex] == '"') { args.StartIndex++; } else { return(true); } while (args.StartIndex < args.Text.Length) { if (args.Text[args.StartIndex] == '"') { if (args.Text[args.StartIndex - 1] != '\\') { args.StartIndex++; return(true); } } args.StartIndex++; } return(false); }
/// <summary> /// Gets first expression found in the text. /// </summary> /// <param name="args">Object with find arguments.</param> /// <param name="skipStrings">Indicates whether to skip strings.</param> /// <returns>The expression if found; otherwise, returns an empty string.</returns> public static string GetExpression(FindTextArgs args, bool skipStrings) { if (args.StartIndex < args.Text.Length) { if (FindMatchingBrackets(args, skipStrings)) { return(args.FoundText); } } return(""); }
// find matching open and close brackets starting from StartIndex. Takes strings into account. // Returns true if matching brackets found. Also returns FoundText with text inside brackets, // StartIndex pointing to the OpenBracket and EndIndex pointing to the next char after CloseBracket. private static bool FindMatchingBrackets(FindTextArgs args, bool skipLeadingStrings) { if (!skipLeadingStrings) { args.StartIndex = args.Text.IndexOf(args.OpenBracket, args.StartIndex); if (args.StartIndex == -1) { return(false); } } int saveStartIndex = 0; int brCount = 0; while (args.StartIndex < args.Text.Length) { if (!SkipString(args)) { return(false); } if (args.StartIndex + args.OpenBracket.Length > args.Text.Length) { return(false); } if (args.Text.SubstringCompare(args.StartIndex, args.OpenBracket)) { if (brCount == 0) { saveStartIndex = args.StartIndex; } brCount++; } else if (args.Text.SubstringCompare(args.StartIndex, args.CloseBracket)) { brCount--; if (brCount == 0) { args.EndIndex = args.StartIndex + args.CloseBracket.Length; args.StartIndex = saveStartIndex; args.FoundText = args.Text.Substring(args.StartIndex + args.OpenBracket.Length, args.EndIndex - args.StartIndex - args.OpenBracket.Length - args.CloseBracket.Length); return(true); } } args.StartIndex++; } return(false); }
/// <summary> /// Returns expressions found in the text. /// </summary> /// <param name="text">Text that may contain expressions.</param> /// <param name="openBracket">The char sequence used to find the start of expression.</param> /// <param name="closeBracket">The char sequence used to find the end of expression.</param> /// <returns>Array of expressions if found; otherwise return an empty array.</returns> public static string[] GetExpressions(string text, string openBracket, string closeBracket) { List <string> expressions = new List <string>(); FindTextArgs args = new FindTextArgs(); args.Text = new FastString(text); args.OpenBracket = openBracket; args.CloseBracket = closeBracket; while (args.StartIndex < args.Text.Length) //text.Length { if (!FindMatchingBrackets(args, false)) { break; } expressions.Add(args.FoundText); args.StartIndex = args.EndIndex; } return(expressions.ToArray()); }
private string ReplaceDataItems(string expression) { FindTextArgs args = new FindTextArgs(); args.Text = new FastString(expression); args.OpenBracket = "["; args.CloseBracket = "]"; while (args.StartIndex < args.Text.Length) { expression = CodeUtils.GetExpression(args, true); if (expression == "") { break; } if (DataHelper.IsValidColumn(Report.Dictionary, expression)) { Type type = DataHelper.GetColumnType(Report.Dictionary, expression); expression = Report.CodeHelper.ReplaceColumnName(expression, type); } else if (DataHelper.IsValidParameter(Report.Dictionary, expression)) { expression = Report.CodeHelper.ReplaceParameterName(DataHelper.GetParameter(Report.Dictionary, expression)); } else if (DataHelper.IsValidTotal(Report.Dictionary, expression)) { expression = Report.CodeHelper.ReplaceTotalName(expression); } else { expression = "[" + ReplaceDataItems(expression) + "]"; } args.Text = args.Text.Remove(args.StartIndex, args.EndIndex - args.StartIndex); args.Text = args.Text.Insert(args.StartIndex, expression); args.StartIndex += expression.Length; } return(args.Text.ToString()); }