<commit_before>﻿//-----------------------------------------------------------------------
// <copyright file="StreamExtensions.cs" company="falldave">
//
// Written in 2018 by <NAME>
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see
// [http://creativecommons.org/publicdomain/zero/1.0/].
//
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;

namespace FallDave.Trifles
{
    /// <summary>
    /// Extension methods for Streams and other stream-like I/O objects.
    /// </summary>
    public static partial class StreamExtensions
    {
        private static Action<byte[], int, int> WriteActionOf(Stream stream)
        {
            return (buffer, offset, count) => stream.Write(buffer, offset, count);
        }

        private static Action<byte[], int, int> WriteActionOf(BinaryWriter writer)
        {
            return (buffer, index, count) => writer.Write(buffer, index, count);
        }

        #region Write(... IEnumerable<byte> source ...)

        // This value must be at least 1.
        private const int DefaultWriteBufferSize = 8192;

        // Copies elements from an IList<byte> to a byte array. Special cases exist for source of
        // byte[] or ImmutableArray<byte>.

        private static void CopyToNoCheck(IList<byte> source, int sourceIndex, byte[] destination, int destinationIndex, int length)
        {
            if (source is byte[])
            {
                Array.Copy((byte[])source, sourceIndex, destination, destinationIndex, length);
            }
            else if (source is ImmutableArray<byte>)
            {
                ((ImmutableArray<byte>)source).CopyTo(sourceIndex, destination, destinationIndex, length);
            }
            else
            {
                for (int i = 0; i < length; ++i)<commit_msg>ITriflesByteReader-based implementations for ReadFully and ReadViaArray<commit_after>﻿//-----------------------------------------------------------------------
// <copyright file="StreamExtensions.cs" company="falldave">
//
// Written in 2018 by <NAME>
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see
// [http://creativecommons.org/publicdomain/zero/1.0/].
//
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;

namespace FallDave.Trifles
{
    /// <summary>
    /// Extension methods for Streams and other stream-like I/O objects.
    /// </summary>
    public static partial class StreamExtensions
    {
        private static Action<byte[], int, int> WriteActionOf(Stream stream)
        {
            return (buffer, offset, count) => stream.Write(buffer, offset, count);
        }

        private static Action<byte[], int, int> WriteActionOf(BinaryWriter writer)
        {
            return (buffer, index, count) => writer.Write(buffer, index, count);
        }

        #region ToTriflesByteReader

        /// <summary>
        /// Returns this source as an <see cref="ITriflesByteReader"/>.
        /// </summary>
        /// <param name="source">A source to convert.</param>
        /// <returns><paramref name="source"/> as an <see cref="ITriflesByteReader"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is <c>null</c>.</exception>
        public static ITriflesByteReader ToTriflesByteReader(this ITriflesByteReader source)
        {
            return TriflesByteReaderImpl.ToTriflesByteReader(source);
        }

        /// <summary>
        /// Creates a new <see cref="ITriflesByteReader"/> backed by this source.
        /// </summary>
        /// <param name="source">A source to convert.</param>
        /// <returns>
        /// A new <see cref="ITriflesByteReader"/> whose <see cref="ITriflesByteReader.Read(byte[],
        /// int, int)"/> method is implemented directly by the source's <see
        /// cref="Stream.Read(byte[], int, int)"/> method.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="source"/> does not support reading.
        /// </exception>
        public static ITriflesByteReader ToTriflesByteReader(this Stream source)
        {
            return TriflesByteReaderImpl.ToTriflesByteReader(source);
        }

        /// <summary>
        /// Creates a new <see cref="ITriflesByteReader"/> backed by this source.
        /// </summary>
        /// <param name="source">A source to convert.</param>
        /// <returns>
        /// A new <see cref="ITriflesByteReader"/> whose <see cref="ITriflesByteReader.Read(byte[],
        /// int, int)"/> method is implemented directly by the source's <see
        /// cref="BinaryReader.Read(byte[], int, int)"/> method.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is <c>null</c>.</exception>
        public static ITriflesByteReader ToTriflesByteReader(this BinaryReader source)
        {
            return TriflesByteReaderImpl.ToTriflesByteReader(source);
        }

        #endregion ToTriflesByteReader

        #region Write(... IEnumerable<byte> source ...)

        // This value must be at least 1.
        private const int DefaultWriteBufferSize = 8192;

        // Copies elements from an IList<byte> to a byte array. Special cases exist for source of
        // byte[] or ImmutableArray<byte>.

        private static void CopyToNoCheck(IList<byte> source, int sourceIndex, byte[] destination, int destinationIndex, int length)
        {
            if (source is byte[])
            {
                Array.Copy((byte[])source, sourceIndex, destination, destinationIndex, length);
            }
            else if (source is ImmutableArray<byte>)
            {
                ((ImmutableArray<byte>)source).CopyTo(sourceIndex, destination, destinationIndex, length);
            }
            else
            {
                for (int i = 0; i < length; ++i)
{'fraction_non_alphanumeric': 0.15944795184260754, 'fraction_numerical': 0.003817354279841433, 'mean_word_length': 2.1034168564920273, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5546370', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1697, 'n_words': 536}