- Timestamp:
- Sep 4, 2018, 1:00:48 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/JSON/JSONNumber.cs
r324 r325 2 2 using System.Text; 3 3 4 namespace AllocsFixes.JSON 5 { 6 public class JSONNumber : JSONValue 7 { 8 private double value; 4 namespace AllocsFixes.JSON { 5 public class JSONNumber : JSONValue { 6 private readonly double value; 9 7 10 public JSONNumber (double value) 11 { 8 public JSONNumber (double value) { 12 9 this.value = value; 13 10 } 14 11 15 public double GetDouble () 16 { 12 public double GetDouble () { 17 13 return value; 18 14 } 19 15 20 public int GetInt () 21 { 22 return (int)Math.Round(value); 16 public int GetInt () { 17 return (int) Math.Round (value); 23 18 } 24 19 25 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) 26 { 20 public override void ToString (StringBuilder stringBuilder, bool prettyPrint = false, int currentLevel = 0) { 27 21 stringBuilder.Append (value.ToCultureInvariantString ()); 28 22 } 29 23 30 public static JSONNumber Parse (string json, ref int offset) 31 { 24 public static JSONNumber Parse (string json, ref int offset) { 32 25 //Log.Out ("ParseNumber enter (" + offset + ")"); 33 26 StringBuilder sbNum = new StringBuilder (); … … 37 30 while (offset < json.Length) { 38 31 if (json [offset] >= '0' && json [offset] <= '9') { 39 if (hasExp) 32 if (hasExp) { 40 33 sbExp.Append (json [offset]); 41 else34 } else { 42 35 sbNum.Append (json [offset]); 36 } 43 37 } else if (json [offset] == '.') { 44 38 if (hasExp) { 45 39 throw new MalformedJSONException ("Decimal separator in exponent"); 40 } 41 42 if (hasDec) { 43 throw new MalformedJSONException ("Multiple decimal separators in number found"); 44 } 45 46 if (sbNum.Length == 0) { 47 throw new MalformedJSONException ("No leading digits before decimal separator found"); 48 } 49 50 sbNum.Append ('.'); 51 hasDec = true; 52 } else if (json [offset] == '-') { 53 if (hasExp) { 54 if (sbExp.Length > 0) { 55 throw new MalformedJSONException ("Negative sign in exponent after digits"); 56 } 57 58 sbExp.Append (json [offset]); 46 59 } else { 47 if (hasDec) 48 throw new MalformedJSONException ("Multiple decimal separators in number found"); 49 else if (sbNum.Length == 0) { 50 throw new MalformedJSONException ("No leading digits before decimal separator found"); 51 } else { 52 sbNum.Append ('.'); 53 hasDec = true; 60 if (sbNum.Length > 0) { 61 throw new MalformedJSONException ("Negative sign in mantissa after digits"); 54 62 } 55 } 56 } else if (json [offset] == '-') { 57 if (hasExp) { 58 if (sbExp.Length > 0) 59 throw new MalformedJSONException ("Negative sign in exponent after digits"); 60 else 61 sbExp.Append (json [offset]); 62 } else { 63 if (sbNum.Length > 0) 64 throw new MalformedJSONException ("Negative sign in mantissa after digits"); 65 else 66 sbNum.Append (json [offset]); 63 64 sbNum.Append (json [offset]); 67 65 } 68 66 } else if (json [offset] == 'e' || json [offset] == 'E') { 69 if (hasExp) 67 if (hasExp) { 70 68 throw new MalformedJSONException ("Multiple exponential markers in number found"); 71 else if (sbNum.Length == 0) { 69 } 70 71 if (sbNum.Length == 0) { 72 72 throw new MalformedJSONException ("No leading digits before exponential marker found"); 73 } else {74 sbExp = new StringBuilder ();75 hasExp = true;76 73 } 74 75 sbExp = new StringBuilder (); 76 hasExp = true; 77 77 } else if (json [offset] == '+') { 78 78 if (hasExp) { 79 if (sbExp.Length > 0) 79 if (sbExp.Length > 0) { 80 80 throw new MalformedJSONException ("Positive sign in exponent after digits"); 81 else 82 sbExp.Append (json [offset]); 81 } 82 83 sbExp.Append (json [offset]); 83 84 } else { 84 85 throw new MalformedJSONException ("Positive sign in mantissa found"); … … 86 87 } else { 87 88 double number; 88 if (!StringParsers.TryParseDouble (sbNum.ToString (), out number)) {89 throw new MalformedJSONException ("Mantissa is not a valid decimal (\"" + sbNum .ToString ()+ "\")");89 if (!StringParsers.TryParseDouble (sbNum.ToString (), out number)) { 90 throw new MalformedJSONException ("Mantissa is not a valid decimal (\"" + sbNum + "\")"); 90 91 } 91 92 … … 93 94 int exp; 94 95 if (!int.TryParse (sbExp.ToString (), out exp)) { 95 throw new MalformedJSONException ("Exponent is not a valid integer (\"" + sbExp .ToString ()+ "\")");96 throw new MalformedJSONException ("Exponent is not a valid integer (\"" + sbExp + "\")"); 96 97 } 97 98 … … 102 103 return new JSONNumber (number); 103 104 } 105 104 106 offset++; 105 107 } 108 106 109 throw new MalformedJSONException ("End of JSON reached before parsing number finished"); 107 110 } 108 109 111 } 110 112 } 111
Note:
See TracChangeset
for help on using the changeset viewer.