Beispiel #1
0
        /// <summary> 读取从当前位置到终止字符之间的数据
        /// </summary>
        /// <param name="stop">终止字符</param>
        /// <param name="mode">读取模式</param>
        public string ReadToStop(Func <char, bool> stopCharMatch, ReadMode mode = ReadMode.SkipAll | ReadMode.RemoveStop | ReadMode.AllowToEnd)
        {
            ReadNext(mode | ReadMode.ReserveStop);

            int startIndex = Position;
            int stopIndex;
            var parse = 0 != (mode & ReadMode.ParseAll);

            do
            {
                if (parse && Current == '\\')
                {
                    var str = new QuickStringWriter();
                    str.Append(_rawCharArray, startIndex, Position - startIndex);
                    foreach (var s in ReadParseString(stopCharMatch, mode))
                    {
                        str.Append(s);
                    }
                    return(str.ToString());
                }
                if (stopCharMatch(Current))
                {
                    if (CheckAnd(mode, ReadMode.RemoveStop))
                    {
                        stopIndex = Position;
                    }
                    else
                    {
                        stopIndex = Position + 1;
                    }
                    if (CheckAnd(mode, ReadMode.ReserveStop) == false)
                    {
                        ReadNext();
                    }
                    if (CheckAnd(mode, ReadMode.RemoveStart))
                    {
                        startIndex++;
                    }
                    return(new string(_rawCharArray, startIndex, stopIndex - startIndex));
                }
            } while (ReadNext());

            if ((mode & ReadMode.AllowToEnd) == 0)
            {
                throw new NotSupportedException("已达字符串结尾");
            }
            else
            {
                return(new string(_rawCharArray, startIndex, Length - startIndex));
            }
        }
Beispiel #2
0
 /// <summary>
 /// 将对象转换为Json字符串
 /// </summary>
 /// <param name="obj"> </param>
 /// <param name="settings"> 序列化Json字符串时使用的设置参数 </param>
 public static string ToJsonString(this object obj, JsonBuilderSettings settings)
 {
     using (var buffer = new QuickStringWriter(4096))
     {
         var args = new JsonWriterArgs(WriterContainer, buffer, settings);
         if (obj == null || obj is DBNull)
         {
             WriterContainer.GetNullWriter().Write(null, args);
             return(buffer.ToString());
         }
         var writer = WriterContainer.GetWriter(obj.GetType());
         writer.Write(obj, args);
         return(buffer.ToString());
     }
 }
Beispiel #3
0
 /// <summary> 将对象转换为Json字符串
 /// </summary>
 public string ToJsonString(object obj)
 {
     if (obj == null || obj is DBNull)
     {
         return("null");
     }
     using (Buffer = new QuickStringWriter(4096))
     {
         if (CheckLoopRef)
         {
             _loopObject = new ArrayList(32);
         }
         AppendObject(obj);
         var json = Buffer.ToString();
         _loopObject = null;
         return(json);
     }
 }
Beispiel #4
0
        public static string EscapeString(string str)
        {
            var size = str.Length * 2;

            if (size > ushort.MaxValue)
            {
                size = ushort.MaxValue;
            }
            QuickStringWriter buffer = null;

            try
            {
                unsafe
                {
                    var length = str.Length;
                    fixed(char *fp = str)
                    {
                        char *p    = fp;
                        char *end  = fp + length;
                        char *flag = fp;

                        while (p < end)
                        {
                            char c = *p;
                            switch (c)
                            {
                            case '\\':
                            case '"':
                                if (buffer == null)
                                {
                                    buffer = new QuickStringWriter((ushort)size).Append('"');
                                }
                                buffer.Append(flag, 0, (int)(p - flag));
                                buffer.Append('\\');
                                flag = p;
                                break;

                            case '\n':
                                if (buffer == null)
                                {
                                    buffer = new QuickStringWriter((ushort)size);
                                }
                                buffer.Append(flag, 0, (int)(p - flag));
                                buffer.Append('\\');
                                buffer.Append('n');
                                flag = p + 1;
                                break;

                            case '\r':
                                if (buffer == null)
                                {
                                    buffer = new QuickStringWriter((ushort)size);
                                }
                                buffer.Append(flag, 0, (int)(p - flag));
                                buffer.Append('\\');
                                buffer.Append('r');
                                flag = p + 1;
                                break;

                            case '\t':
                                if (buffer == null)
                                {
                                    buffer = new QuickStringWriter((ushort)size);
                                }
                                buffer.Append(flag, 0, (int)(p - flag));
                                buffer.Append('\\');
                                buffer.Append('t');
                                flag = p + 1;
                                break;

                            case '\f':
                                if (buffer == null)
                                {
                                    buffer = new QuickStringWriter((ushort)size);
                                }
                                buffer.Append(flag, 0, (int)(p - flag));
                                buffer.Append('\\');
                                buffer.Append('f');
                                flag = p + 1;
                                break;

                            case '\0':
                                if (buffer == null)
                                {
                                    buffer = new QuickStringWriter((ushort)size);
                                }
                                buffer.Append(flag, 0, (int)(p - flag));
                                buffer.Append('\\');
                                buffer.Append('0');
                                flag = p + 1;
                                break;

                            case '\a':
                                if (buffer == null)
                                {
                                    buffer = new QuickStringWriter((ushort)size);
                                }
                                buffer.Append(flag, 0, (int)(p - flag));
                                buffer.Append('\\');
                                buffer.Append('a');
                                flag = p + 1;
                                break;

                            case '\b':
                                if (buffer == null)
                                {
                                    buffer = new QuickStringWriter((ushort)size);
                                }
                                buffer.Append(flag, 0, (int)(p - flag));
                                buffer.Append('\\');
                                buffer.Append('b');
                                flag = p + 1;
                                break;

                            case '\v':
                                if (buffer == null)
                                {
                                    buffer = new QuickStringWriter((ushort)size);
                                }
                                buffer.Append(flag, 0, (int)(p - flag));
                                buffer.Append('\\');
                                buffer.Append('v');
                                flag = p + 1;
                                break;

                            default:
                                break;
                            }
                            p++;
                        }
                        if (flag == fp)
                        {
                            if (buffer == null)
                            {
                                return(str);
                            }
                            buffer.Append(fp, 0, length);
                        }
                        else if (p > flag)
                        {
                            if (buffer == null)
                            {
                                buffer = new QuickStringWriter((ushort)size).Append('"');
                            }
                            buffer.Append(flag, 0, (int)(p - flag));
                        }
                    }
                }
                return(buffer.ToString());
            }
            finally
            {
                if (buffer != null)
                {
                    buffer.Dispose();
                }
            }
        }