Esempio n. 1
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            try
            {
                long position = _stream.Position;
                if (position != _position - (_buffer.Count - _offset))
                {
                    _buffer.Clear();
                    _offset   = _buffer.Count;
                    _position = position;
                }
            }
            catch (NotSupportedException)
            {
            }
            int cached = Comparable.Min(count, _buffer.Count - _offset);

            if (cached > 0)
            {
                //buffer.MoveRange();
                buffer.Fill(i => _buffer[_offset + i], new Range(offset, cached));
            }
            int read = count - cached;

            if (read > 0)
            {
                read = _stream.Read(buffer, offset, read);
                if (read > _buffer.Capacity - _buffer.Count)
                {
                    _buffer.RemoveRange(0, Comparable.Min(read - (_buffer.Capacity - _buffer.Count), _buffer.Count));
                }
                _buffer.AddRange(buffer.Skip(offset + cached + Comparable.Max(0, read - (_buffer.Capacity - _buffer.Count))).Take(Comparable.Min(read, _buffer.Capacity - _buffer.Count)));
            }
            return(cached + read);
        }
Esempio n. 2
0
        public PwrList <T> DequeueRange(int count)
        {
            if (count < 0 || count > InnerList.Count)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            var list = new PwrList <T>(count);

            list.AddRange(InnerList.Enumerate(0, count));
            list.RemoveRange(0, count);
            return(list);
        }
Esempio n. 3
0
        public static IEnumerable <String> ReadLines(this TextReader reader, Func <IList <Char>, int> terminatorMatcher, Func <IList <Char>, string> terminatorConverter, int maxCount)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (terminatorMatcher == null)
            {
                throw new ArgumentNullException("matchTerm");
            }
            if (maxCount < 0)
            {
                throw new ArgumentOutOfRangeException("maxCount");
            }

            if (maxCount == 0)
            {
                yield break;
            }

            int matched = 0;
            var list    = new PwrList <Char>();
            var view    = new PwrFrameListView <Char>(list);
            var sb      = new StringBuilder();

            while (maxCount > 0)
            {
                int read     = 0;
                int accepted = 0;
                while (true)
                {
                    if (matched == list.Count)
                    {
                        if ((read = reader.Read()) < 0)
                        {
                            break;
                        }
                        else
                        {
                            list.PushLast((char)read);
                        }
                    }
                    view.FrameSize = matched + 1;
                    accepted       = terminatorMatcher(view);
                    if (accepted > 0)
                    {
                        break;
                    }
                    else if (accepted == 0)
                    {
                        matched++;
                    }
                    else
                    {
                        matched = 0;
                        sb.Append(list.PopFirst());
                    }
                }
                if (accepted > matched + 1)
                {
                    throw new InvalidOperationException("Accepted value is greater matched length.");
                }
                if (accepted > 0)
                {
                    if (terminatorConverter == null)
                    {
                        sb.Append(list.GetRange(0, accepted).ToArray());
                    }
                    else
                    {
                        view.FrameSize = accepted;
                        string term = terminatorConverter(view);
                        if (!string.IsNullOrEmpty(term))
                        {
                            sb.Append(term);
                        }
                    }
                    yield return(sb.ToString());

                    sb.Clear();
                    list.RemoveRange(0, accepted);
                    matched = 0;
                    maxCount--;
                }
                if (read < 0)
                {
                    break;
                }
            }
            if (maxCount > 0)
            {
                if (list.Count > 0)
                {
                    sb.AppendChars(list);
                }
                if (sb.Length > 0)
                {
                    yield return(sb.ToString());

                    sb.Clear();
                    list.Clear();
                }
            }
        }