Example #1
0
        private void AppendRawBytes(MutableString /*!*/ buffer, int count)
        {
            Debug.Assert(count > 0);

            int remaining = count;

            if (_bufferCount > 0)
            {
                int c = Math.Min(_bufferCount, count);
                buffer.Append(_buffer, _bufferStart, c);
                ConsumeBuffered(c);
                remaining -= c;
            }

            if (count == Int32.MaxValue)
            {
                const int chunk = 1024;

                int done = buffer.GetByteCount();
                int bytesRead;
                do
                {
                    buffer.Append(_stream, chunk);
                    bytesRead = buffer.GetByteCount() - done;
                    done     += bytesRead;
                } while (bytesRead == chunk);
            }
            else
            {
                buffer.Append(_stream, remaining);
            }
        }
Example #2
0
        public override int Read(byte[] /*!*/ buffer, int offset, int count)
        {
            int maxReadLen = _string.GetByteCount() - _position;

            if (count > maxReadLen)
            {
                count = maxReadLen;
            }
            for (int i = 0; i < count; i++)
            {
                buffer[offset + i] = _string.GetByte(_position++);
            }
            return(count);
        }
Example #3
0
        public MutableString ReadLine(MutableString /*!*/ separator, RubyEncoding /*!*/ encoding, bool preserveEndOfLines)
        {
            int b = ReadByteNormalizeEoln(preserveEndOfLines);

            if (b == -1)
            {
                return(null);
            }

            int           separatorOffset = 0;
            int           separatorLength = separator.GetByteCount();
            MutableString result          = MutableString.CreateBinary(encoding);

            do
            {
                result.Append((byte)b);

                if (b == separator.GetByte(separatorOffset))
                {
                    if (separatorOffset == separatorLength - 1)
                    {
                        break;
                    }
                    separatorOffset++;
                }
                else if (separatorOffset > 0)
                {
                    separatorOffset = 0;
                }

                b = ReadByteNormalizeEoln(preserveEndOfLines);
            } while (b != -1);

            return(result);
        }
Example #4
0
        public static int Write(RubyIO /*!*/ self, [NotNull] MutableString /*!*/ val)
        {
            int bytesWritten = val.IsEmpty ? 0 : self.WriteBytes(val, 0, val.GetByteCount());

            if (self.AutoFlush)
            {
                self.Flush();
            }
            return(bytesWritten);
        }
Example #5
0
        private static void FromHex(BinaryWriter /*!*/ writer, MutableString /*!*/ str, int nibbleCount, bool swap)
        {
            int maxCount = Math.Min(nibbleCount, str.GetByteCount());

            for (int i = 0, j = 0; i < (nibbleCount + 1) / 2; i++, j += 2)
            {
                int hiNibble = (j < maxCount) ? FromHexDigit(str.GetByte(j)) : 0;
                int loNibble = (j + 1 < maxCount) ? FromHexDigit(str.GetByte(j + 1)) : 0;
                Debug.Assert(hiNibble >= 0 && hiNibble < 16 && loNibble >= 0 && loNibble < 16);

                int c = (swap) ? (loNibble << 4) | hiNibble : (hiNibble << 4) | loNibble;
                writer.Write((byte)c);
            }
        }
Example #6
0
        private static string /*!*/ ForceEncoding(MutableString /*!*/ input, Encoding /*!*/ encoding, int start)
        {
            int byteCount = input.GetByteCount();

            if (start < 0)
            {
                start += byteCount;
            }
            if (start < 0)
            {
                return(null);
            }

            return((start <= byteCount) ? input.ToString(encoding, start, byteCount - start) : null);
        }
Example #7
0
        /// <summary>
        /// Searches the pattern for hexadecimal and octal character escapes that represent a non-ASCII character.
        /// </summary>
        private static bool HasEscapedNonAsciiBytes(MutableString /*!*/ pattern)
        {
            int i      = 0;
            int length = pattern.GetByteCount();

            while (i < length - 2)
            {
                int c = pattern.GetByte(i++);
                if (c == '\\')
                {
                    c = pattern.GetByte(i++);
                    if (c == 'x')
                    {
                        // hexa escape:
                        int d1 = Tokenizer.ToDigit(PeekByte(pattern, length, i++));
                        if (d1 < 16)
                        {
                            int d2 = Tokenizer.ToDigit(PeekByte(pattern, length, i++));
                            if (d2 < 16)
                            {
                                return(d1 * 16 + d2 >= 0x80);
                            }
                        }
                    }
                    else if (c >= '2' && c <= '7')
                    {
                        // a backreference (\1..\9) or an octal escape:
                        int d = Tokenizer.ToDigit(PeekByte(pattern, length, i++));
                        if (d < 8)
                        {
                            int value = Tokenizer.ToDigit(c) * 8 + d;
                            d = Tokenizer.ToDigit(PeekByte(pattern, length, i++));
                            if (d < 8)
                            {
                                value = value * 8 + d;
                            }
                            return(value >= 0x80);
                        }
                    }
                }
            }

            return(false);
        }
Example #8
0
 public int GetByteCount()
 {
     return(_string.GetByteCount());
 }
Example #9
0
        /// <summary>
        /// Reads <paramref name="count"/> bytes from the stream and appends them to the given <paramref name="buffer"/>.
        /// If <paramref name="count"/> is <c>Int32.MaxValue</c> the stream is read to the end.
        /// Unless <paramref name="preserveEndOfLines"/> is set the line endings in the appended data are normalized to "\n".
        /// </summary>
        public int AppendBytes(MutableString /*!*/ buffer, int count, bool preserveEndOfLines)
        {
            ContractUtils.RequiresNotNull(buffer, "buffer");
            ContractUtils.Requires(count >= 0, "count");

            if (count == 0)
            {
                return(0);
            }

            bool readAll = count == Int32.MaxValue;

            buffer.SwitchToBytes();
            int initialBufferSize = buffer.GetByteCount();

            if (preserveEndOfLines)
            {
                AppendRawBytes(buffer, count);
            }
            else
            {
                // allocate 3 more bytes at the end for a backstop and possible LF:
                byte[] bytes = Utils.EmptyBytes;

                int  done = initialBufferSize;
                bool eof;
                do
                {
                    AppendRawBytes(buffer, readAll ? 1024 : count);
                    int end       = buffer.GetByteCount();
                    int bytesRead = end - done;
                    if (bytesRead == 0)
                    {
                        break;
                    }

                    eof = bytesRead < count;

                    buffer.EnsureCapacity(end + 3);
                    int byteCount;
                    bytes = buffer.GetByteArray(out byteCount);

                    if (bytes[end - 1] == CR && PeekByte(0) == LF)
                    {
                        ReadByte();
                        bytes[end++] = LF;
                    }

                    // insert backstop:
                    bytes[end]     = CR;
                    bytes[end + 1] = LF;

                    int last = IndexOfCrLf(bytes, done);
                    count -= last - done;
                    done   = last;
                    while (last < end)
                    {
                        int next  = IndexOfCrLf(bytes, last + 2);
                        int chunk = next - last - 1;
                        Buffer.BlockCopy(bytes, last + 1, bytes, done, chunk);
                        done  += chunk;
                        count -= chunk;
                        last   = next;
                    }
                    buffer.Remove(done);
                } while (readAll || count > 0 && !eof);
            }

            if (readAll)
            {
                buffer.TrimExcess();
            }

            return(buffer.GetByteCount() - initialBufferSize);
        }