<commit_before>﻿using NBitcoin.DataEncoders;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Numerics;
using System.Text;
using System.Threading;
using NBitcoin.Protocol;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
#if !NOSOCKET
using System.Net.Sockets;
#endif
#if WINDOWS_UWP
using System.Net.Sockets;
using Windows.Networking;
using Windows.Networking.Connectivity;
#endif

namespace NBitcoin
{
	public static class Extensions
	{
		public static Block GetBlock(this IBlockRepository repository, uint256 blockId)
		{
			try
			{
				return repository.GetBlockAsync(blockId).Result;
			}
			catch(AggregateException aex)
			{
				ExceptionDispatchInfo.Capture(aex.InnerException).Throw();
				return null; //Can't happen
			}
		}



		public static T ToNetwork<T>(this T base58, Network network) where T : Base58Data
		{
			if(network == null)
				throw new ArgumentNullException("network");
			if(base58.Network == network)
				return base58;
			if(base58 == null)
				throw new ArgumentNullException("base58");
			var inner = base58.ToBytes();
			if(base58.Type != Base58Type.COLORED_ADDRESS)
			{
				byte[] version = network.GetVersionBytes(base58.Type);
				var newBase58 = Encoders.Base58Check.EncodeData(version.Concat(inner).ToArray());
				return Network.CreateFromBase58Data<T>(newBase58, network);
			}
			else
			{
				var colored = BitcoinColoredAddress.GetWrappedBase58(base58.ToWif(), base58.Network);
				var address = Network.CreateFromBase58Data<BitcoinAddress>(colored).ToNetwork(network);
				return (T)(object)address.ToColoredAddress();
			}
		}

		public static byte[] ReadBytes(this Stream stream, int bytesToRead)
		{
			var buffer = new byte[bytesToRead];
			int num = 0;
			int num2;
			do
			{
				num += (num2 = stream.Read(buffer, num, bytesToRead - num));
			} while(num2 > 0 && num < bytesToRead);
			return buffer;
		}

		public static int ReadBytes(this Stream stream, int count, out byte[] result)
		{
			result = new byte[count];
			return stream.Read(result, 0, count);
		}
		public static IEnumerable<T> Resize<T>(this List<T> list, int count)
		{
			if(list.Count == count)
				return new T[0];

			List<T> removed = new List<T>();

			for(int i = list.Count - 1; i + 1 > count; i--)
			{
				removed.Add(list[i]);
				list.RemoveAt(i);
			}

			while(list.Count < count)
			{
				list.Add(default(T));
			}
			return removed;
		}
		public static IEnumerable<List<T>> Partition<T>(this IEnumerable<T> source, int max)
		{
			return Partition(source, () => max);
		}
		public static IEnumerable<List<T>> Partition<T>(this IEnumerable<T> source, Func<int> max)
		{
			var partitionSize = max();
			List<T> toReturn = new List<T>(partitionSize);
			foreach(var item in source)
			{
				toReturn.Add(item);
				if(toReturn.Count == partitionSize)
				{
					yield return toReturn;
					partitionSize = max();
					toReturn = new List<T>(partitionSize);
				}
			}
			if(toReturn.Any())
			{
				yield return toReturn;
			}
		}

#if !(PORTABLE || NETCORE)
		public static int ReadEx(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellation = default(CancellationToken))
		{
			if(stream == null) throw new ArgumentNullException("stream");
			if(buffer == null) throw new ArgumentNullException("buffer");
			if(offset < 0 || offset > buffer.Length) throw new ArgumentOutOfRangeException("offset");
			if(count <= 0 || count > buffer.Length) throw new ArgumentOutOfRangeException("count"); //Disallow 0 as a debugging aid.
			if(offset > buffer.Length - count) throw new ArgumentOutOfRangeException("count");

			int totalReadCount = 0;

			while(totalReadCount < count)
			{
				cancellation.ThrowIfCancellationRequested();

				int currentReadCount;

				//Big performance problem with BeginRead for other stream types than NetworkStream.
				//Only take the slow path if cancellation is possible.
				if(stream is NetworkStream && cancellation.CanBeCanceled)
				{
					var ar = stream.BeginRead(buffer, offset + totalReadCount, count - totalReadCount, null, null);
					if(!ar.CompletedSynchronously)
					{
						WaitHandle.WaitAny(new WaitHandle[] { ar.AsyncWaitHandle, cancellation.WaitHandle }, -1);
					}
					currentReadCount = stream.EndRead(ar);
				}
				else
				{
					//IO interruption not supported in this path.
					currentReadCount = stream.Read(buffer, offset + totalReadCount, count - totalReadCount);
				}

				if(currentReadCount == 0)
					return 0;

				totalReadCount += currentReadCount;
			}

			return totalReadCount;
		}
#else

		public static int ReadEx(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellation = default(CancellationToken))
		{
			if(stream == null) throw new ArgumentNullException("stream");
			if(buffer == null) throw new ArgumentNullException("buffer");
			if(offset < 0 || offset > buffer.Length) throw new ArgumentOutOfRangeException("offset");
			if(count <= 0 || count > buffer.Length) throw new ArgumentOutOfRangeException("count"); //Disallow 0 as a debugging aid.
			if(offset > buffer.Length - count) throw new ArgumentOutOfRangeException("count");

			//IO interruption not supported on these platforms.

			int totalReadCount = 0;

			while(totalReadCount < count)
			{
				cancellation.ThrowIfCancellationRequested();
				int currentReadCount = 0;
				if(stream is NetworkStream && cancellation.CanBeCanceled)
				{
					currentReadCount = stream.ReadAsync(buffer, offset + totalReadCount, count - totalReadCount, cancellation).GetAwaiter().GetResult();
				}
				else
				{
					currentReadCount = stream.Read(buffer, offset + totalReadCount, count - totalReadCount);
				}
				if(currentReadCount == 0)
					return 0;
				totalReadCount += currentReadCount;
			}

			return totalReadCount;
		}
#endif
		public static void AddOrReplace<TKey, TValue>(this IDictionary<TKey, TValue> dico, TKey key, TValue value)
		{
			if(dico.ContainsKey(key))
			{
				dico.Remove(key);
				dico.Add(key, value);
			}
			else
			{
				dico.Add(key, value);
			}
		}

		public static TValue TryGet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
		{
			TValue value;
			dictionary.TryGetValue(key, out value);<commit_msg>fix netstandard 1.1 build problem<commit_after>﻿using NBitcoin.DataEncoders;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Numerics;
using System.Text;
using System.Threading;
using NBitcoin.Protocol;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
#if !NOSOCKET
using System.Net.Sockets;
#endif
#if WINDOWS_UWP
using System.Net.Sockets;
using Windows.Networking;
using Windows.Networking.Connectivity;
#endif

namespace NBitcoin
{
	public static class Extensions
	{
		public static Block GetBlock(this IBlockRepository repository, uint256 blockId)
		{
			try
			{
				return repository.GetBlockAsync(blockId).Result;
			}
			catch(AggregateException aex)
			{
				ExceptionDispatchInfo.Capture(aex.InnerException).Throw();
				return null; //Can't happen
			}
		}



		public static T ToNetwork<T>(this T base58, Network network) where T : Base58Data
		{
			if(network == null)
				throw new ArgumentNullException("network");
			if(base58.Network == network)
				return base58;
			if(base58 == null)
				throw new ArgumentNullException("base58");
			var inner = base58.ToBytes();
			if(base58.Type != Base58Type.COLORED_ADDRESS)
			{
				byte[] version = network.GetVersionBytes(base58.Type);
				var newBase58 = Encoders.Base58Check.EncodeData(version.Concat(inner).ToArray());
				return Network.CreateFromBase58Data<T>(newBase58, network);
			}
			else
			{
				var colored = BitcoinColoredAddress.GetWrappedBase58(base58.ToWif(), base58.Network);
				var address = Network.CreateFromBase58Data<BitcoinAddress>(colored).ToNetwork(network);
				return (T)(object)address.ToColoredAddress();
			}
		}

		public static byte[] ReadBytes(this Stream stream, int bytesToRead)
		{
			var buffer = new byte[bytesToRead];
			int num = 0;
			int num2;
			do
			{
				num += (num2 = stream.Read(buffer, num, bytesToRead - num));
			} while(num2 > 0 && num < bytesToRead);
			return buffer;
		}

		public static int ReadBytes(this Stream stream, int count, out byte[] result)
		{
			result = new byte[count];
			return stream.Read(result, 0, count);
		}
		public static IEnumerable<T> Resize<T>(this List<T> list, int count)
		{
			if(list.Count == count)
				return new T[0];

			List<T> removed = new List<T>();

			for(int i = list.Count - 1; i + 1 > count; i--)
			{
				removed.Add(list[i]);
				list.RemoveAt(i);
			}

			while(list.Count < count)
			{
				list.Add(default(T));
			}
			return removed;
		}
		public static IEnumerable<List<T>> Partition<T>(this IEnumerable<T> source, int max)
		{
			return Partition(source, () => max);
		}
		public static IEnumerable<List<T>> Partition<T>(this IEnumerable<T> source, Func<int> max)
		{
			var partitionSize = max();
			List<T> toReturn = new List<T>(partitionSize);
			foreach(var item in source)
			{
				toReturn.Add(item);
				if(toReturn.Count == partitionSize)
				{
					yield return toReturn;
					partitionSize = max();
					toReturn = new List<T>(partitionSize);
				}
			}
			if(toReturn.Any())
			{
				yield return toReturn;
			}
		}

#if !(PORTABLE || NETCORE)
		public static int ReadEx(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellation = default(CancellationToken))
		{
			if(stream == null) throw new ArgumentNullException("stream");
			if(buffer == null) throw new ArgumentNullException("buffer");
			if(offset < 0 || offset > buffer.Length) throw new ArgumentOutOfRangeException("offset");
			if(count <= 0 || count > buffer.Length) throw new ArgumentOutOfRangeException("count"); //Disallow 0 as a debugging aid.
			if(offset > buffer.Length - count) throw new ArgumentOutOfRangeException("count");

			int totalReadCount = 0;

			while(totalReadCount < count)
			{
				cancellation.ThrowIfCancellationRequested();

				int currentReadCount;

				//Big performance problem with BeginRead for other stream types than NetworkStream.
				//Only take the slow path if cancellation is possible.
				if(stream is NetworkStream && cancellation.CanBeCanceled)
				{
					var ar = stream.BeginRead(buffer, offset + totalReadCount, count - totalReadCount, null, null);
					if(!ar.CompletedSynchronously)
					{
						WaitHandle.WaitAny(new WaitHandle[] { ar.AsyncWaitHandle, cancellation.WaitHandle }, -1);
					}
					currentReadCount = stream.EndRead(ar);
				}
				else
				{
					//IO interruption not supported in this path.
					currentReadCount = stream.Read(buffer, offset + totalReadCount, count - totalReadCount);
				}

				if(currentReadCount == 0)
					return 0;

				totalReadCount += currentReadCount;
			}

			return totalReadCount;
		}
#else

		public static int ReadEx(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellation = default(CancellationToken))
		{
			if(stream == null) throw new ArgumentNullException("stream");
			if(buffer == null) throw new ArgumentNullException("buffer");
			if(offset < 0 || offset > buffer.Length) throw new ArgumentOutOfRangeException("offset");
			if(count <= 0 || count > buffer.Length) throw new ArgumentOutOfRangeException("count"); //Disallow 0 as a debugging aid.
			if(offset > buffer.Length - count) throw new ArgumentOutOfRangeException("count");

			//IO interruption not supported on these platforms.

			int totalReadCount = 0;

			while(totalReadCount < count)
			{
				cancellation.ThrowIfCancellationRequested();
				int currentReadCount = 0;
#if !NOSOCKET
				if(stream is NetworkStream && cancellation.CanBeCanceled)
				{
					currentReadCount = stream.ReadAsync(buffer, offset + totalReadCount, count - totalReadCount, cancellation).GetAwaiter().GetResult();
				}
				else
#endif
				{
					currentReadCount = stream.Read(buffer, offset + totalReadCount, count - totalReadCount);
				}
				if(currentReadCount == 0)
					return 0;
				totalReadCount += currentReadCount;
			}

			return totalReadCount;
		}
#endif
		public static void AddOrReplace<TKey, TValue>(this IDictionary<TKey, TValue> dico, TKey key, TValue value)
		{
			if(dico.ContainsKey(key))
			{
				dico.Remove(key);
				dico.Add(key, value);
			}
			else
			{
				dico.Add(key, value);
			}
		}

		public static TValue TryGet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
		{
			TValue value;
			dictionary.TryGetValue(key, out value);
{'fraction_non_alphanumeric': 0.11380700229194657, 'fraction_numerical': 0.009958112700545325, 'mean_word_length': 3.794998105342933, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 69, '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': '2800231', 'n_tokens_mistral': 4738, 'n_tokens_neox': 3877, 'n_words': 990}