using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
#pragma warning disable CS1591 // Missing documentation (these methods are mostly hidden from the user)
namespace Iot.Device
{
///
/// .NET Core compatibility helper functions (methods that do not exist in .NET Framework)
///
#if BUILDING_IOT_DEVICE_BINDINGS
internal
#else
public
#endif
static class FrameworkCompatibilityExtensions
{
public static bool StartsWith(this Span span, string value)
{
return span.StartsWith(new ReadOnlySpan(value.ToCharArray()));
}
public static bool StartsWith(this Span span, string value, StringComparison comparison)
{
return span.ToString().StartsWith(value, comparison);
}
public static bool StartsWith(this ReadOnlySpan span, string value, StringComparison comparison)
{
return span.ToString().StartsWith(value, comparison);
}
public static int CompareTo(this ReadOnlySpan span, string value, StringComparison comparison)
{
return string.Compare(span.ToString(), value, comparison);
}
public static string GetString(this Encoding encoding, Span input)
{
return encoding.GetString(input.ToArray());
}
public static void NextBytes(this Random random, Span buffer)
{
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte)random.Next();
}
}
public static void Write(this Stream stream, Span data)
{
stream.Write(data.ToArray(), 0, data.Length);
}
public static int Read(this Stream stream, Span data)
{
byte[] rawData = new byte[data.Length];
int result = stream.Read(rawData, 0, rawData.Length);
rawData.CopyTo(data);
return result;
}
///
/// Attempts to add the specified key and value to the dictionary.
///
/// Type of key
/// Type of value
/// Dictionary
/// Key to add
/// Value to add
/// True if the value was added, false otherwise. No exception is thrown
public static bool TryAdd(this Dictionary dictionary, TKey key, TValue value)
{
if (dictionary.ContainsKey(key))
{
return false;
}
dictionary.Add(key, value);
return true;
}
}
}