private static Value ParseJsonString(VmGlobals globals, char[] rawValue, int length, Index i) { i.Value++; // opening quote StringBuilder sb = new StringBuilder(); while (i.Value < length && rawValue[i.Value] != '"') { char c = rawValue[i.Value++]; if (c == '\\') { switch (rawValue[i.Value++]) { case '\\': c = '\\'; break; case '"': c = '"'; break; case '\'': c = '\''; break; case 'n': c = '\n'; break; case 't': c = '\t'; break; case 'r': c = '\r'; break; case '0': c = '\0'; break; default: throw new JsonParserException(); } } sb.Append(c); } if (i.Value >= length) { throw new JsonParserException(); } i.Value++; // closing quote return(CrayonWrapper.buildString(globals, sb.ToString())); }
private Value ConvertNativeArg(object na) { if (na == null) { return(vm.globalNull); } if (na is Value) { return((Value)na); // allow mixed native/Value } if (na is bool) { return((bool)na ? vm.globalTrue : vm.globalFalse); } if (na is string) { return(CrayonWrapper.buildString(vm.globals, (string)na)); } if (na is int) { return(CrayonWrapper.buildInteger(vm.globals, (int)na)); } if (na is double) { return(CrayonWrapper.buildFloat(vm.globals, (double)na)); } if (na is float) { return(CrayonWrapper.buildFloat(vm.globals, (float)na)); } if (na is object[]) { List <Value> list = new List <Value>(((object[])na).Select(a => ConvertNativeArg(a))); return(CrayonWrapper.buildList(list)); } throw new NotImplementedException("Unsupported type for native arg: " + na.GetType()); }
private static Value ParseJsonString(VmGlobals globals, char[] rawValue, int length, Index i) { i.Value++; // opening quote StringBuilder sb = new StringBuilder(); while (i.Value < length && rawValue[i.Value] != '"') { char c = rawValue[i.Value++]; if (c == '\\') { switch (rawValue[i.Value++]) { case '\\': sb.Append('\\'); break; case '"': sb.Append('"'); break; case '\'': sb.Append('\''); break; case 'n': sb.Append('\n'); break; case 't': sb.Append('\t'); break; case 'r': sb.Append('\r'); break; case '0': sb.Append('\0'); break; case 'u': if (i.Value + 4 < length) { char u1 = rawValue[i.Value]; char u2 = rawValue[i.Value + 1]; char u3 = rawValue[i.Value + 2]; char u4 = rawValue[i.Value + 3]; i.Value += 4; string hexValue = "" + u1 + u2 + u3 + u4; int value; if (!int.TryParse(hexValue, System.Globalization.NumberStyles.HexNumber, null, out value)) { throw new JsonParserException(); } sb.Append((char)value); } else { throw new JsonParserException(); } break; default: throw new JsonParserException(); } } else { sb.Append(c); } } if (i.Value >= length) { throw new JsonParserException(); } i.Value++; // closing quote return(CrayonWrapper.buildString(globals, sb.ToString())); }