/// True if str is a number public static bool IsNumber(string str) { if (str == null) { throw new ArgumentNullException("str"); } using (var sr = new ParsingReader(str)) { if (sr.ReadNumber() == null) { return(false); } sr.SkipWhiteSpaceAndComments(); if (sr.Peek() != -1) { return(false); } } return(true); }
/// <summary> /// Try to get variable value /// </summary> /// <param name="key">Variable name</param> /// <param name="value">Where to store found variable value</param> /// <returns>true if variable is set</returns> public override bool TryGetValue(string key, out object value) { if (!IsCalculated(key)) { return(base.TryGetValue(key, out value)); } using (var sr = new ParsingReader(key)) { sr.SkipWhiteSpace(); value = sr.ReadNumber(); if (value != null) { sr.SkipWhiteSpace(); sr.ReadAndThrowIfNot(-1); return(true); } } value = EvalMulti(key); return(true); }
/// Returns null if str is not a number, or its value otherwise public static ValueType TryParseNumber(string stringToParse) { if (stringToParse == null) { return(null); } using (var sr = new ParsingReader(stringToParse)) { sr.SkipWhiteSpaceAndComments(); ValueType o = sr.ReadNumber(); if (o == null) { return(null); } sr.SkipWhiteSpaceAndComments(); if (sr.Peek() != -1) { return(null); } return(o); } }
/// Parse string to a number. 10 and hex numbers (like 0x222) are allowed. Suffixes like 20.3f are also allowed. public static ValueType ParseNumber(string str) { if (str == null) { throw new ArgumentNullException("str"); } using (var sr = new ParsingReader(str)) { sr.SkipWhiteSpaceAndComments(); ValueType o = sr.ReadNumber(); if (o == null) { throw new ParsingException("Invalid numeric expression at " + sr.ReadLine()); } sr.SkipWhiteSpaceAndComments(); if (sr.Peek() != -1) { throw new ParsingException("Invalid numeric expression, unexpected characters at " + sr.ReadLine()); } return(o); } }
/// <summary> /// Try to get variable value /// </summary> /// <param name="key">Variable name</param> /// <param name="value">Where to store found variable value</param> /// <returns>true if variable is set</returns> public override bool TryGetValue(string key, out object value) { if (!IsCalculated(key)) { return(base.TryGetValue(key, out value)); } using (var sr = new ParsingReader(key)) { sr.SkipWhiteSpace(); value = sr.ReadNumber(); if (value != null) { sr.SkipWhiteSpace(); sr.ReadAndThrowIfNot(-1); return(true); } } if (!string.IsNullOrEmpty(key)) { switch (key[0]) { case '%': EnvironmentVariableTarget target = getTarget(ref key); value = Environment.GetEnvironmentVariable(key, target); return(value != null); case '~': value = Find(key.Substring(1), true); return(true); } } value = EvalMulti(key); return(true); }