/// <summary> /// Returns the position of the first found occurrence of a specified value in a string /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="searchString">The string to search for</param> /// <param name="start">The starting position to start the search.</param> /// <returns></returns> public int IndexOf(LString target, string searchString, int start = 0) { if (string.IsNullOrEmpty(target.Value)) { return(-1); } if (string.IsNullOrEmpty(searchString)) { return(-1); } return(target.Value.IndexOf(searchString, start)); }
/// <summary> /// Returns the character at the specified index /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="ndx">The index of the character to get</param> /// <returns></returns> public string CharAt(LString target, int ndx) { if (ndx < 0) { return(string.Empty); } if (ndx >= target.Value.Length) { return(string.Empty); } return(target.Value[ndx].ToString()); }
/// <summary> /// Extracts the characters from a string, beginning at a specified start position, and through the specified number of character /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="from">Index where to start extraction</param> /// <param name="length">The number of characters to extract. If omitted, it extracts the rest of the string</param> /// <returns></returns> public string Substr(LString target, int from, int length = -1) { if (from < 0) { from = 0; } // Upto end of string. if (length == -1) { return(target.Value.Substring(from)); } return(target.Value.Substring(from, length)); }
/// <summary> /// Extracts the characters from a string, between two specified indices /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="from">Index where to start extraction</param> /// <param name="to">The index where to stop the extraction. If omitted, it extracts the rest of the string</param> /// <returns></returns> public string Substring(LString target, int from, int to = -1) { if (from < 0) { from = 0; } // Upto end of string. if (to == -1) { return(target.Value.Substring(from)); } // Compute length for c# string method. int length = (to - from) + 1; return(target.Value.Substring(from, length)); }
/// <summary> /// Returns the position of the last found occurrence of a specified value in a string /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="searchString">The text to search for</param> /// <param name="start">The position to start search</param> /// <returns></returns> public int LastIndexOf(LString target, string searchString, int start) { if (string.IsNullOrEmpty(target.Value)) { return(-1); } if (string.IsNullOrEmpty(searchString)) { return(-1); } if (start == -1) { return(target.Value.LastIndexOf(searchString)); } var result = target.Value.LastIndexOf(searchString, start); return(result); }
/// <summary> /// Joins two or more strings, and returns a copy of the joined strings /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="strings">The list of strings to join</param> /// <returns></returns> public string Concat(LString target, object[] strings) { var result = new StringBuilder(); result.Append(target.Value); foreach (object str in strings) result.Append(str); return result.ToString(); }
/// <summary> /// Returns the character at the specified index /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="ndx">The index of the character to get</param> /// <returns></returns> public string CharAt(LString target, int ndx) { if (ndx < 0) return string.Empty; if (ndx >= target.Value.Length) return string.Empty; return target.Value[ndx].ToString(); }
/// <summary> /// Converts a string to uppercase letters /// </summary> /// <param name="target">The target value to apply this method on</param> /// <returns></returns> public string ToUpperCase(LString target) { return target.Value.ToUpper(); }
/// <summary> /// Extracts the characters from a string, between two specified indices /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="from">Index where to start extraction</param> /// <param name="to">The index where to stop the extraction. If omitted, it extracts the rest of the string</param> /// <returns></returns> public string Substring(LString target, int from, int to = -1) { if (from < 0) from = 0; // Upto end of string. if (to == -1) return target.Value.Substring(from); // Compute length for c# string method. int length = (to - from) + 1; return target.Value.Substring(from, length); }
/// <summary> /// Converts a string to lowercase letters /// </summary> /// <param name="target">The target value to apply this method on</param> /// <returns></returns> public string ToLowerCase(LString target) { return(target.Value.ToLower()); }
/// <summary> /// Gets the length of the string /// </summary> /// <param name="target">The target value to apply this method on</param> /// <returns></returns> public int Length(LString target) { return target.Value.Length; }
/// <summary> /// Returns the position of the last found occurrence of a specified value in a string /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="searchString">The text to search for</param> /// <param name="start">The position to start search</param> /// <returns></returns> public int LastIndexOf(LString target, string searchString, int start) { if (string.IsNullOrEmpty(target.Value)) return -1; if (string.IsNullOrEmpty(searchString)) return -1; if (start == -1) return target.Value.LastIndexOf(searchString); var result = target.Value.LastIndexOf(searchString, start); return result; }
/// <summary> /// Returns the position of the first found occurrence of a specified value in a string /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="searchString">The string to search for</param> /// <param name="start">The starting position to start the search.</param> /// <returns></returns> public int IndexOf(LString target, string searchString, int start = 0) { if (string.IsNullOrEmpty(target.Value)) return -1; if (string.IsNullOrEmpty(searchString)) return -1; return target.Value.IndexOf(searchString, start); }
private LString IncrementString(LString sourceVal) { // Check 1: Can only do += on strings. if (Op != Operator.PlusEqual) throw new LangException("Syntax Error", "string operation with " + Op.ToString() + " not supported", Ref.ScriptName, Ref.Line, Ref.CharPos); this.DataType = typeof(string); var val = this.Expression.Evaluate() as LObject; // Check 2: Check for null if (val == LObjects.Null) return sourceVal; // Check 3: Limit size if string Ctx.Limits.CheckStringLength(this, sourceVal.Value, val.GetValue().ToString()); // Finally do the appending. string appended = sourceVal.Value + val.GetValue().ToString(); sourceVal.Value = appended; this.Value = appended; this.Ctx.Memory.SetValue(this.Name, sourceVal); return sourceVal; }
/// <summary> /// Evaluate * / + - % /// </summary> /// <returns></returns> public static object EvalBinary(BinaryExpr expr) { // Validate object result = 0; var node = expr; var op = expr.Op; var left = (LObject)expr.Left.Evaluate(); var right = (LObject)expr.Right.Evaluate(); // Case 1: Both numbers if (IsTypeMatch(LTypes.Number, left, right)) { result = EvalHelper.CalcNumbers(node, (LNumber)left, (LNumber)right, op); } // Case 2: Both times else if (IsTypeMatch(LTypes.Time, left, right)) { result = EvalHelper.CalcTimes(node, (LTime)left, (LTime)right, op); } // Case 3: Both dates else if (IsTypeMatch(LTypes.Date, left, right)) { result = EvalHelper.CalcDates(node, (LDate)left, (LDate)right, op); } // Case 4: Both strings. else if (IsTypeMatch(LTypes.String, left, right)) { var strleft = ((LString)left).Value; var strright = ((LString)right).Value; // Check string limit. Ctx.Limits.CheckStringLength(node, strleft, strright); result = new LString(strleft + strright); } // MIXED TYPES // TODO: Needs to be improved with new code for types. // Case 5 : Double and Bool else if (left.Type == LTypes.Number && right.Type == LTypes.Bool) { var r = ((LBool)right).Value; var rval = r ? 1 : 0; result = EvalHelper.CalcNumbers(node, (LNumber)left, new LNumber(rval), op); } // Bool Double else if (left.Type == LTypes.Bool && right.Type == LTypes.Number) { var l = ((LBool)left).Value; var lval = l ? 1 : 0; result = EvalHelper.CalcNumbers(node, new LNumber(lval), (LNumber)right, op); } // Append as strings. else if (left.Type == LTypes.String && right.Type == LTypes.Bool) { var st1 = ((LString)left).Value + ((LBool)right).Value.ToString().ToLower(); result = new LString(st1); } // Append as strings. else if (left.Type == LTypes.Bool && right.Type == LTypes.String) { var st2 = ((LBool)left).Value.ToString().ToLower() + ((LString)right).Value; result = new LString(st2); } // TODO: Need to handle LUnit and LVersion better //else if (left.Type == LTypes.Unit && right.Type == LTypes.Unit) else if (left.Type.Name == "LUnit" && right.Type.Name == "LUnit") { result = EvalHelper.CalcUnits(node, (LUnit)((LClass)left).Value, (LUnit)((LClass)right).Value, op, Ctx.Units); } else { var st3 = left.GetValue().ToString() + right.GetValue().ToString(); result = new LString(st3); } return result; }
/// <summary> /// Evaluates a math expression of 2 time spans. /// </summary> /// <param name="node">The AST node the evaluation is a part of.</param> /// <param name="lhs">The time on the left hand side</param> /// <param name="rhs">The time on the right hand side</param> /// <param name="op">The math operator.</param> /// <returns></returns> public static LBool CompareStrings(AstNode node, LString lhs, LString rhs, Operator op) { var left = lhs.Value; var right = rhs.Value; var result = false; if (op == Operator.EqualEqual) { result = left == right; return new LBool(result); } else if (op == Operator.NotEqual) { result = left != right; return new LBool(result); } int compareResult = string.Compare(left, right, StringComparison.InvariantCultureIgnoreCase); if (op == Operator.LessThan) result = compareResult == -1; else if (op == Operator.LessThanEqual) result = compareResult != 1; else if (op == Operator.MoreThan) result = compareResult == 1; else if (op == Operator.MoreThanEqual) result = compareResult != -1; return new LBool(result); }
/// <summary> /// Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="substring">Required. A substring or a regular expression.</param> /// <param name="newString">Required. The string to replace the found value in parameter 1</param> /// <returns></returns> public string Replace(LString target, string substring, string newString) { return target.Value.Replace(substring, newString); }
/// <summary> /// Searches for a match between a regular expression and a string, and returns the position of the match /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="regExp">Required. A regular expression.</param> /// <returns></returns> public int Search(LString target, string regExp) { Match match = Regex.Match(target.Value, regExp); if (!match.Success) return -1; return match.Index; }
/// <summary> /// Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="substring">Required. A substring or a regular expression.</param> /// <param name="newString">Required. The string to replace the found value in parameter 1</param> /// <returns></returns> public string Replace(LString target, string substring, string newString) { return(target.Value.Replace(substring, newString)); }
/// <summary> /// Extracts the characters from a string, beginning at a specified start position, and through the specified number of character /// </summary> /// <param name="target">The target value to apply this method on</param> /// <param name="from">Index where to start extraction</param> /// <param name="length">The number of characters to extract. If omitted, it extracts the rest of the string</param> /// <returns></returns> public string Substr(LString target, int from, int length = -1) { if (from < 0) from = 0; // Upto end of string. if (length == -1) return target.Value.Substring(from); return target.Value.Substring(from, length); }
/// <summary> /// Gets the length of the string /// </summary> /// <param name="target">The target value to apply this method on</param> /// <returns></returns> public int Length(LString target) { return(target.Value.Length); }
public static LString IncrementString(UnaryExpr expr, LString sourceVal, IAstVisitor visitor) { // Check 1: Can only do += on strings. if (expr.Op != Operator.PlusEqual) throw new LangException("Syntax Error", "string operation with " + expr.Op.ToString() + " not supported", expr.Ref.ScriptName, expr.Ref.Line, expr.Ref.CharPos); //expr.DataType = typeof(string); var val = expr.Expression.Evaluate(visitor) as LObject; // Check 2: Check for null if (val == LObjects.Null) return sourceVal; // Check 3: Limit size if string Ctx.Limits.CheckStringLength(expr, sourceVal.Value, val.GetValue().ToString()); // Finally do the appending. var appended = sourceVal.Value + val.GetValue().ToString(); sourceVal.Value = appended; expr.Ctx.Memory.SetValue(expr.Name, sourceVal); return sourceVal; }