/// <summary> /// Validates the format of a string is acceptable for assigning to <see cref="ValueString" /> when <see cref="Type" /> /// is equal to <see cref="LSLType.Float" />. /// </summary> /// <param name="value">The value string to validate.</param> /// <param name="valueString">The re-formated version of <paramref name="valueString" /> if the parse was successful.</param> /// <param name="errMessage">An error message describing why the parse failed if this function returns <c>false</c>.</param> /// <returns><c>true</c> if <paramref name="valueString" /> can successfully be parsed for <see cref="LSLType.Float" />.</returns> /// <exception cref="ArgumentNullException"><paramref name="value" /> is <c>null</c>.</exception> public static bool TryParseFloatValueString(string value, out string valueString, out string errMessage) { if (value == null) { throw new ArgumentNullException("value", "value cannot be null."); } valueString = null; string stripSpecifiers = value.TrimEnd('f', 'F'); try { valueString = LSLFormatTools.FormatFloatString(stripSpecifiers); } catch (FormatException) { errMessage = string.Format("Float Constant ValueString: Given string '{0}' is not a valid LSL float value.", value); return(false); } errMessage = null; return(true); }
/// <summary> /// Initializes a new instance of the <see cref="LSLListFloatExpr" /> class. /// </summary> /// <param name="val">The value.</param> /// <param name="hex">if set to <c>true</c> val is parsed from hexadecimal notation.</param> public LSLListFloatExpr(string val, bool hex = false) { if (hex) { Value = Convert.ToInt32(val, 16); } else { var v = LSLFormatTools.FormatFloatString(val.TrimEnd('f', 'F')); Value = float.Parse(v); } }
/// <summary> /// Validates the format of a string is acceptable for assigning to <see cref="ValueString" /> when <see cref="Type" /> /// is equal to <see cref="LSLType.Vector" />. /// </summary> /// <param name="value">The value string to validate.</param> /// <param name="valueString">The re-formated version of <paramref name="valueString" /> if the parse was successful.</param> /// <param name="errMessage">An error message describing why the parse failed if this function returns <c>false</c>.</param> /// <returns><c>true</c> if <paramref name="valueString" /> can successfully be parsed for <see cref="LSLType.Vector" />.</returns> /// <exception cref="ArgumentNullException"><paramref name="value" /> is <c>null</c>.</exception> public static bool TryParseVectorValueString(string value, out string valueString, out string errMessage) { if (value == null) { throw new ArgumentNullException("value", "value cannot be null."); } valueString = null; string s = value.Trim(' '); if (string.IsNullOrWhiteSpace(s)) { errMessage = "Vector Constant ValueString Invalid: May not be null or whitespace."; return(false); } char firstChar = s[0]; char lastChar = s[s.Length - 1]; if ((firstChar == '<' || lastChar == '>') && (firstChar != '<' || lastChar != '>')) { errMessage = "Vector Constant ValueString '{0}' Invalid: If vector quotes are used for a Vector value string, both '<' and '>' must be present."; return(false); } if (firstChar == '<') { s = s.Substring(1, s.Length - 2); } var match = VectorValidationRegex.Match(s); if (!match.Success) { errMessage = string.Format("Vector Constant ValueString: '{0}' could not be parsed and formated.", value); return(false); } valueString = string.Join(", ", s.Split(',').Select(x => LSLFormatTools.FormatFloatString(x.TrimEnd('f', 'F')))); errMessage = null; return(true); }
/// <summary> /// Determines whether the float literal node is a literal value that overflows/underflows a 32 bit float. /// </summary> /// <param name="node">The float literal node to test.</param> /// <returns><see cref="LSLLiteralOverflowType"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="node"/> is <c>null</c>.</exception> public static LSLLiteralOverflowType CheckForOverflow(this ILSLFloatLiteralNode node) { if (node == null) { throw new ArgumentNullException("node"); } var formatedFloatString = LSLFormatTools.FormatFloatString(node.RawText.TrimEnd('f', 'F')); bool nonZero = formatedFloatString.TakeWhile(c => c != 'e').Any(c => c != '0' && c != '.' && c != 'f'); if (!nonZero) { return(LSLLiteralOverflowType.None); } double val; try { val = double.Parse(formatedFloatString); } catch (OverflowException) { return(LSLLiteralOverflowType.Overflow); } if (val > 3.402823466E+38) { return(LSLLiteralOverflowType.Overflow); } if (val < 1.401298464E-45) { return(LSLLiteralOverflowType.Underflow); } return(LSLLiteralOverflowType.None); }