From 1ba76f57ba13ae59f518ecf9ba2319dd54b7a934 Mon Sep 17 00:00:00 2001 From: idb Date: Fri, 12 Dec 2008 11:50:12 +0000 Subject: Made the casting of stings to floats more robust and work more like SL. Added some more tests that casts previously failed on. Fixes Mantis #2789 --- OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs | 32 ++++++++++++---------- .../Shared/Tests/LSL_TypesTestLSLFloat.cs | 12 +++++++- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs b/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs index 65b1b5c..da40995 100644 --- a/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs +++ b/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs @@ -1363,7 +1363,7 @@ namespace OpenSim.Region.ScriptEngine.Shared public static explicit operator double(LSLString s) { - return Convert.ToDouble(s.m_string); + return new LSLFloat(s).value; } public static explicit operator LSLInteger(LSLString s) @@ -1401,7 +1401,7 @@ namespace OpenSim.Region.ScriptEngine.Shared public static implicit operator LSLFloat(LSLString s) { - return new LSLFloat(Convert.ToDouble(s.m_string)); + return new LSLFloat(s); } public static implicit operator list(LSLString s) @@ -1737,7 +1737,21 @@ namespace OpenSim.Region.ScriptEngine.Shared public LSLFloat(string s) { - this.value = double.Parse(s); + Regex r = new Regex("^ *(\\+|-)?([0-9]+\\.?[0-9]*|\\.[0-9]+)([eE](\\+|-)?[0-9]+)?"); + Match m = r.Match(s); + string v = m.Groups[0].Value; + + v = v.Trim(); + + if (v == String.Empty || v == null) + v = "0.0"; + else + if (!v.Contains(".") && !v.ToLower().Contains("e")) + v = v + ".0"; + else + if (v.EndsWith(".")) + v = v + "0"; + this.value = double.Parse(v, System.Globalization.NumberStyles.Float); } #endregion @@ -1783,17 +1797,7 @@ namespace OpenSim.Region.ScriptEngine.Shared static public explicit operator LSLFloat(string s) { - Regex r = new Regex("^[ ]*-?[0-9]*\\.?[0-9]*[eE]?-?[0-9]*"); - Match m = r.Match(s); - string v = m.Groups[0].Value; - - while (v.Length > 0 && v.Substring(0, 1) == " ") - v = v.Substring(1); - - if (v == String.Empty) - v = "0"; - - return new LSLFloat(double.Parse(v)); + return new LSLFloat(s); } public static implicit operator list(LSLFloat f) diff --git a/OpenSim/Region/ScriptEngine/Shared/Tests/LSL_TypesTestLSLFloat.cs b/OpenSim/Region/ScriptEngine/Shared/Tests/LSL_TypesTestLSLFloat.cs index 272d06c..d40a951 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Tests/LSL_TypesTestLSLFloat.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Tests/LSL_TypesTestLSLFloat.cs @@ -145,7 +145,17 @@ namespace OpenSim.Region.ScriptEngine.Shared.Tests m_stringDoubleSet.Add("-548.5", -548.5); m_stringDoubleSet.Add("-2.0005", -2.0005); m_stringDoubleSet.Add("-349485435.6805", -349485435.6805); - + // some oddball combinations and exponents + m_stringDoubleSet.Add("", 0.0); + m_stringDoubleSet.Add("1.0E+5", 100000.0); + m_stringDoubleSet.Add("-1.0E+5", -100000.0); + m_stringDoubleSet.Add("-1E+5", -100000.0); + m_stringDoubleSet.Add("-1.E+5", -100000.0); + m_stringDoubleSet.Add("-1.E+5.0", -100000.0); + m_stringDoubleSet.Add("1ef", 1.0); + m_stringDoubleSet.Add("e10", 0.0); + m_stringDoubleSet.Add("1.e0.0", 1.0); + m_doubleStringSet = new Dictionary(); m_doubleStringSet.Add(2.0, "2.000000"); m_doubleStringSet.Add(-2.0, "-2.000000"); -- cgit v1.1