Пример #1
0
 /// <summary>
 ///     格式化ajax字符串
 /// </summary>
 /// <param name="jsStream">JS字符流</param>
 /// <param name="formatStream">格式化JSON字符流</param>
 internal static unsafe void FormatJavascript(CharStreamPlus jsStream, UnmanagedStreamPlus formatStream)
 {
     var length = FormatLength(jsStream);
     if (length == 0)
     {
         if (jsStream.Length != 0) FormatQuote(jsStream, formatStream);
         return;
     }
     length += jsStream.Length;
     formatStream.PrepLength(length <<= 1);
     for (char* start = jsStream.Char, write = (char*) (formatStream.CurrentData), end = start + jsStream.Length;
         start != end;
         ++start)
     {
         if (*start == Quote) *write++ = '"';
         else if (*start == '\n')
         {
             *(int*) write = ('n' << 16) + '\\';
             write += 2;
         }
         else if (*start == '\r')
         {
             *(int*) write = ('r' << 16) + '\\';
             write += 2;
         }
         else
         {
             if (*start == '"' || *start == '\\') *write++ = '\\';
             *write++ = *start;
         }
     }
     formatStream.Unsafer.AddLength(length);
 }
Пример #2
0
 public static unsafe void ToString(ulong value, CharStreamPlus jsonStream, bool isMaxToString = true)
 {
     if (value <= MaxValue || !isMaxToString)
     {
         char* chars = stackalloc char[20];
         var index = ToString(value, chars);
         jsonStream.WriteBase(chars + index.Key, index.Value);
     }
     else
     {
         var unsafeStraem = jsonStream.Unsafer;
         jsonStream.PrepLength(22);
         unsafeStraem.Write(Quote);
         NumberPlus.ToString(value, jsonStream);
         unsafeStraem.Write(Quote);
     }
 }
Пример #3
0
 public static unsafe void WriteObject(CharStreamPlus jsonStream)
 {
     jsonStream.PrepLength(2);
     var data = (byte*) jsonStream.CurrentChar;
     *(char*) data = '{';
     *(char*) (data + sizeof (char)) = '}';
     jsonStream.Unsafer.AddLength(2);
 }
Пример #4
0
 /// <summary>
 /// 数值转字符串
 /// </summary>
 /// <param name="value">数值</param>
 /// <param name="charStream">字符流</param>
 internal static void ToString(sbyte value, CharStreamPlus charStream)
 {
     charStream.PrepLength(4);
     charStream.Unsafer.AddLength(ToString(value, charStream.Char + charStream.Length));
 }
Пример #5
0
 /// <summary>
 /// 数值转字符串
 /// </summary>
 /// <param name="value">数值</param>
 /// <param name="charStream">字符流</param>
 internal static void ToString(ulong value, CharStreamPlus charStream)
 {
     char* chars = stackalloc char[20];
     KeyValueStruct<int, int> index = ToString(value, chars);
     charStream.WriteBase(chars + index.Key, index.Value);
 }
Пример #6
0
 /// <summary>
 ///     格式化ajax字符串
 /// </summary>
 /// <param name="jsStream">js字符流</param>
 /// <returns>格式化后ajax字符串</returns>
 private static unsafe string FormatQuote(CharStreamPlus jsStream)
 {
     for (char* start = jsStream.Char, end = start + jsStream.Length; start != end; ++start)
     {
         if (*start == Quote) *start = '"';
     }
     return new string(jsStream.Char, 0, jsStream.Length);
 }
Пример #7
0
 /// <summary>
 ///     字节流转换成JSON字符串
 /// </summary>
 /// <param name="jsonStream">JSON输出流</param>
 /// <param name="buffer">字节流数组</param>
 public static unsafe void ToJson(CharStreamPlus jsonStream, SubArrayStruct<byte> buffer)
 {
     if (buffer.Array == null) AjaxPlus.WriteNull(jsonStream);
     else if (buffer.Count == 0) AjaxPlus.WriteArray(jsonStream);
     else
     {
         fixed (byte* bufferFixed = buffer.Array)
         {
             var start = bufferFixed + buffer.StartIndex;
             ToJson(jsonStream, start, start + buffer.Count);
         }
     }
 }
Пример #8
0
 public static void ToString(DateTime time, CharStreamPlus jsonStream)
 {
     if (time != DateTime.MinValue) ToStringNotNull(time, jsonStream);
     else WriteNull(jsonStream);
 }
Пример #9
0
 public static unsafe void ToString(Guid value, CharStreamPlus jsonStream)
 {
     var guid = new GuidStruct {Value = value};
     jsonStream.PrepLength(38);
     var data = (byte*) jsonStream.CurrentChar;
     int high = guid.Byte3 >> 4, low = guid.Byte3 & 15;
     *(char*) data = Quote;
     *(char*) (data + sizeof (char)) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*2) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     high = guid.Byte2 >> 4;
     low = guid.Byte2 & 15;
     *(char*) (data + sizeof (char)*3) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*4) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     high = guid.Byte1 >> 4;
     low = guid.Byte1 & 15;
     *(char*) (data + sizeof (char)*5) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*6) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     high = guid.Byte0 >> 4;
     low = guid.Byte0 & 15;
     *(char*) (data + sizeof (char)*7) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*8) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*9) = '-';
     high = guid.Byte5 >> 4;
     low = guid.Byte5 & 15;
     *(char*) (data + sizeof (char)*10) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*11) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     high = guid.Byte4 >> 4;
     low = guid.Byte4 & 15;
     *(char*) (data + sizeof (char)*12) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*13) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*14) = '-';
     high = guid.Byte7 >> 4;
     low = guid.Byte7 & 15;
     *(char*) (data + sizeof (char)*15) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*16) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     high = guid.Byte6 >> 4;
     low = guid.Byte6 & 15;
     *(char*) (data + sizeof (char)*17) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*18) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*19) = '-';
     high = guid.Byte8 >> 4;
     low = guid.Byte8 & 15;
     *(char*) (data + sizeof (char)*20) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*21) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     high = guid.Byte9 >> 4;
     low = guid.Byte9 & 15;
     *(char*) (data + sizeof (char)*22) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*23) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*24) = '-';
     high = guid.Byte10 >> 4;
     low = guid.Byte10 & 15;
     *(char*) (data + sizeof (char)*25) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*26) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     high = guid.Byte11 >> 4;
     low = guid.Byte11 & 15;
     *(char*) (data + sizeof (char)*27) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*28) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     high = guid.Byte12 >> 4;
     low = guid.Byte12 & 15;
     *(char*) (data + sizeof (char)*29) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*30) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     high = guid.Byte13 >> 4;
     low = guid.Byte13 & 15;
     *(char*) (data + sizeof (char)*31) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*32) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     high = guid.Byte14 >> 4;
     low = guid.Byte14 & 15;
     *(char*) (data + sizeof (char)*33) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*34) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     high = guid.Byte15 >> 4;
     low = guid.Byte15 & 15;
     *(char*) (data + sizeof (char)*35) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*36) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1)));
     *(char*) (data + sizeof (char)*37) = Quote;
     jsonStream.Unsafer.AddLength(38);
 }
Пример #10
0
 public static void ToString(bool value, CharStreamPlus jsonStream)
 {
     jsonStream.WriteBase(value ? '1' : '0');
 }
Пример #11
0
 public static void ToString(decimal value, CharStreamPlus jsonStream)
 {
     jsonStream.WriteNotNull(value.ToString(CultureInfo.InvariantCulture));
 }
Пример #12
0
 public static unsafe void ToString(byte value, CharStreamPlus jsonStream)
 {
     if (value == 0) jsonStream.WriteBase('0');
     else
     {
         jsonStream.PrepLength(4);
         var data = (byte*) jsonStream.CurrentChar;
         var nextValue = value >> 4;
         *(char*) data = '0';
         *(char*) (data + sizeof (char)) = 'x';
         *(char*) (data + sizeof (char)*2) =
             (char) (nextValue < 10 ? nextValue + '0' : (nextValue + ('0' + 'A' - '9' - 1)));
         *(char*) (data + sizeof (char)*3) =
             (char) ((value &= 15) < 10 ? value + '0' : (value + ('0' + 'A' - '9' - 1)));
         jsonStream.Unsafer.AddLength(4);
     }
 }
Пример #13
0
 public static unsafe void ToString(sbyte value, CharStreamPlus jsonStream)
 {
     if (value == 0) jsonStream.WriteBase('0');
     else
     {
         char* chars = stackalloc char[6];
         if (value < 0)
         {
             if (value == -128)
             {
                 jsonStream.PrepLength(4);
                 var unsafeStream = jsonStream.Unsafer;
                 unsafeStream.Write('-');
                 unsafeStream.Write('1');
                 unsafeStream.Write('2');
                 unsafeStream.Write('8');
             }
             else
             {
                 ToString((sbyte) -value, chars);
                 jsonStream.WriteBase(chars + 1, 5);
             }
         }
         else
         {
             ToString(value, chars);
             jsonStream.WriteBase(chars + 2, 4);
         }
     }
 }
Пример #14
0
 public static unsafe void ToString(ushort value, CharStreamPlus jsonStream)
 {
     char* chars = stackalloc char[8];
     var index = ToString(value, chars);
     jsonStream.WriteBase(chars + index.Key, index.Value);
 }
Пример #15
0
 /// <summary>
 ///     格式化ajax字符串
 /// </summary>
 /// <param name="jsStream">js字符流</param>
 /// <param name="length">格式化后ajax字符串长度</param>
 /// <returns>格式化后ajax字符串</returns>
 private static unsafe string Format(CharStreamPlus jsStream, int length)
 {
     char* start = jsStream.Char, write = start + length;
     for (var read = start + jsStream.Length; read != write;)
     {
         if (*--read == Quote) *--write = '"';
         else if (*read == '\n') *(int*) (write -= 2) = ('n' << 16) + '\\';
         else if (*read == '\r') *(int*) (write -= 2) = ('r' << 16) + '\\';
         else
         {
             *--write = *read;
             if (*read == '"' || *read == '\\') *--write = '\\';
         }
     }
     while (write != start) if (*--write == Quote) *write = '"';
     return new string(jsStream.Char, 0, length);
 }
Пример #16
0
 public static void ToString(char value, CharStreamPlus jsonStream)
 {
     jsonStream.PrepLength(3);
     var unsafeStream = jsonStream.Unsafer;
     unsafeStream.Write(Quote);
     unsafeStream.Write(value);
     unsafeStream.Write(Quote);
 }
Пример #17
0
 /// <summary>
 ///     ajax字符串长度
 /// </summary>
 /// <param name="jsStream">js字符流</param>
 /// <returns>ajax字符串长度</returns>
 private static unsafe int FormatLength(CharStreamPlus jsStream)
 {
     var length = 0;
     for (char* start = jsStream.Char, end = start + jsStream.Length; start != end; ++start)
     {
         if (*start == '\n' || *start == '\r' || *start == '"' || *start == '\\') ++length;
     }
     return length;
 }
Пример #18
0
 public static void ToString(string value, CharStreamPlus jsonStream)
 {
     jsonStream.PrepLength(value.Length + 2);
     jsonStream.Unsafer.Write(Quote);
     jsonStream.WriteBase(value);
     jsonStream.Unsafer.Write(Quote);
 }
Пример #19
0
 /// <summary>
 ///     格式化ajax字符串
 /// </summary>
 /// <param name="jsStream">js字符流</param>
 /// <param name="formatStream">格式化JSON字符流</param>
 private static unsafe void FormatQuote(CharStreamPlus jsStream, UnmanagedStreamPlus formatStream)
 {
     var length = jsStream.Length;
     char* start = jsStream.Char, end = start + length;
     formatStream.PrepLength(length <<= 1);
     for (var write = (char*) (formatStream.CurrentData); start != end; ++start)
         *write++ = *start == Quote ? '"' : *start;
     formatStream.Unsafer.AddLength(length);
 }
Пример #20
0
 public static void ToString(SubStringStruct value, CharStreamPlus jsonStream)
 {
     if (value.Value == null) WriteNull(jsonStream);
     else
     {
         jsonStream.PrepLength(value.Length + 2);
         jsonStream.Unsafer.Write(Quote);
         jsonStream.WriteBase(value);
         jsonStream.Unsafer.Write(Quote);
     }
 }
Пример #21
0
 /// <summary>
 ///     字节流转换成JSON字符串
 /// </summary>
 /// <param name="jsonStream">JSON输出流,不能为null</param>
 /// <param name="start">起始位置,不能为null</param>
 /// <param name="end">结束位置,长度必须大于0</param>
 public static unsafe void ToJson(CharStreamPlus jsonStream, byte* start, byte* end)
 {
     jsonStream.WriteBase('[');
     for (AjaxPlus.ToString(*start, jsonStream); ++start != end; AjaxPlus.ToString(*start, jsonStream))
         jsonStream.WriteBase(',');
     jsonStream.WriteBase(']');
 }
Пример #22
0
 public static unsafe void ToStringNotNull(DateTime time, CharStreamPlus jsonStream)
 {
     char* chars = stackalloc char[20];
     var index = ToString((time.Ticks - JavascriptMinTimeTicks)/DatePlus.MillisecondTicks, chars);
     jsonStream.PrepLength(index.Value + DateStart.Length + 1);
     var unsafeStraem = jsonStream.Unsafer;
     fixed (char* dataFixed = DateStart) unsafeStraem.Write(dataFixed, DateStart.Length);
     unsafeStraem.Write(chars + index.Key, index.Value);
     unsafeStraem.Write(DateEnd);
 }
Пример #23
0
 /// <summary>
 /// 数值转字符串
 /// </summary>
 /// <param name="value">数值</param>
 /// <param name="charStream">字符流</param>
 internal static void ToString(short value, CharStreamPlus charStream)
 {
     charStream.PrepLength(6);
     charStream.Unsafer.AddLength(ToString(value, charStream.CurrentChar));
 }
Пример #24
0
 public static unsafe void WriteNull(CharStreamPlus jsonStream)
 {
     jsonStream.PrepLength(4);
     var data = (byte*) jsonStream.CurrentChar;
     *(char*) data = 'n';
     *(char*) (data + sizeof (char)) = 'u';
     *(char*) (data + sizeof (char)*2) = 'l';
     *(char*) (data + sizeof (char)*3) = 'l';
     jsonStream.Unsafer.AddLength(4);
 }
Пример #25
0
 /// <summary>
 /// 数值转字符串
 /// </summary>
 /// <param name="value">数值</param>
 /// <param name="charStream">字符流</param>
 internal static void ToString(long value, CharStreamPlus charStream)
 {
     char* chars = stackalloc char[22];
     var index = ToString(value, chars);
     charStream.WriteBase(chars + index.Key, index.Value);
 }
Пример #26
0
 /// <summary>
 ///     格式化ajax字符串
 /// </summary>
 /// <param name="jsStream">js字符流</param>
 /// <returns>格式化后ajax字符串</returns>
 public static unsafe string FormatJavascript(CharStreamPlus jsStream)
 {
     var length = FormatLength(jsStream);
     if (length == 0) return jsStream.Length == 0 ? string.Empty : FormatQuote(jsStream);
     if ((length += jsStream.Length) <= jsStream.DataLength >> 1) return Format(jsStream, length);
     var value = StringPlus.FastAllocateString(length);
     fixed (char* valueFixed = value)
     {
         for (char* start = jsStream.Char, write = valueFixed, end = start + jsStream.Length;
             start != end;
             ++start)
         {
             if (*start == Quote) *write++ = '"';
             else if (*start == '\n')
             {
                 *(int*) write = '\\' + ('n' << 16);
                 write += 2;
             }
             else if (*start == '\r')
             {
                 *(int*) write = '\\' + ('r' << 16);
                 write += 2;
             }
             else if (*start == '"' || *start == '\\')
             {
                 *write++ = '\\';
                 *write++ = *start;
             }
             else *write++ = *start;
         }
     }
     return value;
 }