Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
bytecode77 committed Sep 30, 2023
2 parents f1bee46 + e8219ef commit 7ff125a
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 20 deletions.
6 changes: 3 additions & 3 deletions BytecodeApi.Wpf/BytecodeApi.Wpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<GeneratePackageOnBuild Condition="'$(Configuration)' == 'Release'">True</GeneratePackageOnBuild>
</PropertyGroup>
<PropertyGroup>
<Version>3.0.1</Version>
<AssemblyVersion>3.0.1</AssemblyVersion>
<FileVersion>3.0.1</FileVersion>
<Version>3.0.2</Version>
<AssemblyVersion>3.0.2</AssemblyVersion>
<FileVersion>3.0.2</FileVersion>
</PropertyGroup>
<PropertyGroup>
<Product>BytecodeApi.Wpf</Product>
Expand Down
2 changes: 2 additions & 0 deletions BytecodeApi.Wpf/Converters/DateOnlyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public DateOnlyConverter(DateOnlyConverterMethod method)
DateOnlyConverterMethod.ShortDate => value.Value.ToShortDateString(),
DateOnlyConverterMethod.LongDate => value.Value.ToLongDateString(),
DateOnlyConverterMethod.Year => value.Value.Year.ToString(),
DateOnlyConverterMethod.Quarter => ((value.Value.Month - 1) / 3 + 1).ToString(),
DateOnlyConverterMethod.Month => value.Value.Month.ToString(),
DateOnlyConverterMethod.Day => value.Value.Day.ToString(),
DateOnlyConverterMethod.Format => value.Value.ToStringInvariant(parameter ?? ""),
Expand Down Expand Up @@ -72,6 +73,7 @@ public DateOnlyConverter(DateOnlyConverterMethod method)
DateOnlyConverterMethod.ShortDate or
DateOnlyConverterMethod.LongDate or
DateOnlyConverterMethod.Year or
DateOnlyConverterMethod.Quarter or
DateOnlyConverterMethod.Month or
DateOnlyConverterMethod.Day => DependencyProperty.UnsetValue,
DateOnlyConverterMethod.Format => (value as string)?.ToDateOnly(parameter ?? ""),
Expand Down
4 changes: 4 additions & 0 deletions BytecodeApi.Wpf/Converters/DateOnlyConverterMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public enum DateOnlyConverterMethod
/// </summary>
Year,
/// <summary>
/// Returns the quarter (a number between 1 and 4) of the <see cref="DateOnly" />? value as a <see cref="string" />.
/// </summary>
Quarter,
/// <summary>
/// Returns the month component of the <see cref="DateOnly" />? value as a <see cref="string" />.
/// </summary>
Month,
Expand Down
2 changes: 2 additions & 0 deletions BytecodeApi.Wpf/Converters/DateTimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public DateTimeConverter(DateTimeConverterMethod method)
DateTimeConverterMethod.LongDate => value.Value.ToLongDateString(),
DateTimeConverterMethod.LongTime => value.Value.ToLongTimeString(),
DateTimeConverterMethod.Year => value.Value.Year.ToString(),
DateTimeConverterMethod.Quarter => ((value.Value.Month - 1) / 3 + 1).ToString(),
DateTimeConverterMethod.Month => value.Value.Month.ToString(),
DateTimeConverterMethod.Day => value.Value.Day.ToString(),
DateTimeConverterMethod.Hour => value.Value.Hour.ToString(),
Expand Down Expand Up @@ -81,6 +82,7 @@ DateTimeConverterMethod.ShortTime or
DateTimeConverterMethod.LongDate or
DateTimeConverterMethod.LongTime => DateTime.TryParse(value as string, CultureInfo.CurrentCulture, out DateTime dateTime) ? dateTime : null,
DateTimeConverterMethod.Year or
DateTimeConverterMethod.Quarter or
DateTimeConverterMethod.Month or
DateTimeConverterMethod.Day or
DateTimeConverterMethod.Hour or
Expand Down
4 changes: 4 additions & 0 deletions BytecodeApi.Wpf/Converters/DateTimeConverterMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public enum DateTimeConverterMethod
/// </summary>
Year,
/// <summary>
/// Returns the quarter (a number between 1 and 4) of the <see cref="DateTime" />? value as a <see cref="string" />.
/// </summary>
Quarter,
/// <summary>
/// Returns the month component of the <see cref="DateTime" />? value as a <see cref="string" />.
/// </summary>
Month,
Expand Down
6 changes: 3 additions & 3 deletions BytecodeApi/BytecodeApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<GeneratePackageOnBuild Condition="'$(Configuration)' == 'Release'">True</GeneratePackageOnBuild>
</PropertyGroup>
<PropertyGroup>
<Version>3.0.2</Version>
<AssemblyVersion>3.0.2</AssemblyVersion>
<FileVersion>3.0.2</FileVersion>
<Version>3.0.3</Version>
<AssemblyVersion>3.0.3</AssemblyVersion>
<FileVersion>3.0.3</FileVersion>
</PropertyGroup>
<PropertyGroup>
<Product>BytecodeApi</Product>
Expand Down
17 changes: 17 additions & 0 deletions BytecodeApi/Extensions/RandomExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,21 @@ public static T NextObject<T>(this Random random, IList<T> list)

return list[random.Next(list.Count)];
}
/// <summary>
/// Returns a random value of the specified <see langword="enum" /> type.
/// </summary>
/// <typeparam name="T">The type of the <see cref="Enum" /> to be returned.</typeparam>
/// <param name="random">The <see cref="Random" /> object to be used for random number generation.</param>
/// <returns>
/// A random value of the specified <see langword="enum" /> type.
/// </returns>
public static T NextEnumValue<T>(this Random random) where T : struct, Enum
{
Check.ArgumentNull(random);

T[] values = EnumEx.GetValues<T>();
if (values.None()) throw Throw.Argument(nameof(T), "Enum does not have values.");

return random.NextObject(values);
}
}
17 changes: 17 additions & 0 deletions BytecodeApi/Extensions/RandomNumberGeneratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,21 @@ public static T GetObject<T>(this RandomNumberGenerator randomNumberGenerator, I

return list[randomNumberGenerator.GetInt32(list.Count)];
}
/// <summary>
/// Returns a random value of the specified <see langword="enum" /> type.
/// </summary>
/// <typeparam name="T">The type of the <see cref="Enum" /> to be returned.</typeparam>
/// <param name="randomNumberGenerator">The <see cref="RandomNumberGenerator" /> object to be used for random number generation.</param>
/// <returns>
/// A random value of the specified <see langword="enum" /> type.
/// </returns>
public static T GetEnumValue<T>(this RandomNumberGenerator randomNumberGenerator) where T : struct, Enum
{
Check.ArgumentNull(randomNumberGenerator);

T[] values = EnumEx.GetValues<T>();
if (values.None()) throw Throw.Argument(nameof(T), "Enum does not have values.");

return randomNumberGenerator.GetObject(values);
}
}
9 changes: 8 additions & 1 deletion BytecodeApi/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Text.RegularExpressions;

namespace BytecodeApi.Extensions;

Expand Down Expand Up @@ -111,7 +112,13 @@ public static string ToCSharpName(this Type type, TypeNaming namingConvention)

while (t.HasElementType)
{
suffix = new[] { "[]", "*", "&" }.First(name.EndsWith) + suffix;
Match match = Regex.Match(name, @"\[,*\]|\*|&", RegexOptions.RightToLeft);
if (!match.Success)
{
throw new InvalidOperationException("Could not parse element type.");
}

suffix = match.Value + suffix;

if (t.GetElementType() is Type elementType)
{
Expand Down
52 changes: 50 additions & 2 deletions BytecodeApi/Extensions/RegistryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static long GetInt64Value(this RegistryKey key, string? name, long defaul
return key.GetInt64Value(name) ?? defaultValue;
}
/// <summary>
/// Retrieves a <see cref="string" /> value from this <see cref="RegistryKey" /> that is represented as a REG_SZ value. Returns <see langword="null" />, if the value does not exist in the registry or is not a REG_SZ value.
/// Retrieves a <see cref="string" /> value from this <see cref="RegistryKey" /> that is represented as a REG_SZ or REG_EXPAND_SZ value. Returns <see langword="null" />, if the value does not exist in the registry or is not a REG_SZ or REG_EXPAND_SZ value.
/// </summary>
/// <param name="key">The <see cref="RegistryKey" /> to read the value from.</param>
/// <param name="name">A <see cref="string" /> value specifying the name of the value to read.</param>
Expand All @@ -136,7 +136,7 @@ public static long GetInt64Value(this RegistryKey key, string? name, long defaul
return key.GetValue(name) as string;
}
/// <summary>
/// Retrieves a <see cref="string" /> value from this <see cref="RegistryKey" /> that is represented as a REG_SZ value. Returns a default value, if the value does not exist in the registry or is not a REG_SZ value.
/// Retrieves a <see cref="string" /> value from this <see cref="RegistryKey" /> that is represented as a REG_SZ or REG_EXPAND_SZ value. Returns a default value, if the value does not exist in the registry or is not a REG_SZ or REG_EXPAND_SZ value.
/// </summary>
/// <param name="key">The <see cref="RegistryKey" /> to read the value from.</param>
/// <param name="name">A <see cref="string" /> value specifying the name of the value to read.</param>
Expand All @@ -150,6 +150,35 @@ public static string GetStringValue(this RegistryKey key, string? name, string d
return key.GetStringValue(name) ?? defaultValue;
}
/// <summary>
/// Retrieves the original, unexpanded <see cref="string" /> value from this <see cref="RegistryKey" /> that is represented as a REG_SZ or REG_EXPAND_SZ value. Returns <see langword="null" />, if the value does not exist in the registry or is not a REG_SZ or REG_EXPAND_SZ value.
/// </summary>
/// <param name="key">The <see cref="RegistryKey" /> to read the value from.</param>
/// <param name="name">A <see cref="string" /> value specifying the name of the value to read.</param>
/// <returns>
/// The converted value, if it exists and conversion is possible;
/// otherwise, <see langword="null" />.
/// </returns>
public static string? GetExpandStringValue(this RegistryKey key, string? name)
{
Check.ArgumentNull(key);

return key.GetValue(name, null, RegistryValueOptions.DoNotExpandEnvironmentNames) as string;
}
/// <summary>
/// Retrieves the original, unexpanded <see cref="string" /> value from this <see cref="RegistryKey" /> that is represented as a REG_SZ or REG_EXPAND_SZ value. Returns a default value, if the value does not exist in the registry or is not a REG_SZ value.
/// </summary>
/// <param name="key">The <see cref="RegistryKey" /> to read the value from.</param>
/// <param name="name">A <see cref="string" /> value specifying the name of the value to read.</param>
/// <param name="defaultValue">The value that is used if retrieving or conversion failed.</param>
/// <returns>
/// The converted value, if it exists and conversion is possible;
/// otherwise, <paramref name="defaultValue" />.
/// </returns>
public static string GetExpandStringValue(this RegistryKey key, string? name, string defaultValue)
{
return key.GetExpandStringValue(name) ?? defaultValue;
}
/// <summary>
/// Retrieves a <see cref="DateTime" /> value from this <see cref="RegistryKey" /> that is represented as a REG_SZ value. Returns <see langword="null" />, if the value does not exist in the registry, is not a REG_SZ value, or does not match the format.
/// <para><see cref="StringToDateTimeConverter" /> is used to convert the REG_SZ value.</para>
/// </summary>
Expand Down Expand Up @@ -341,6 +370,25 @@ public static void SetStringValue(this RegistryKey key, string? name, string? va
}
}
/// <summary>
/// Writes a <see cref="string" /> value to this <see cref="RegistryKey" /> that is represented as a REG_EXPAND_SZ value. If <see langword="null" /> is provided, the value will be deleted.
/// </summary>
/// <param name="key">The <see cref="RegistryKey" /> to write the value to.</param>
/// <param name="name">A <see cref="string" /> value specifying the name of the value to write to.</param>
/// <param name="value">The <see cref="string" /> value to be written. If <see langword="null" /> is provided, the value will be deleted.</param>
public static void SetExpandStringValue(this RegistryKey key, string? name, string? value)
{
Check.ArgumentNull(key);

if (value == null)
{
key.DeleteValue(name ?? "", false);
}
else
{
key.SetValue(name, value, RegistryValueKind.ExpandString);
}
}
/// <summary>
/// Writes a <see cref="DateTime" /> value to this <see cref="RegistryKey" /> that is represented as a REG_SZ value. If <see langword="null" /> is provided, the value will be deleted.
/// <para><see cref="DateTimeToStringConverter" /> is used to convert <paramref name="value" /> to a REG_SZ value.</para>
/// </summary>
Expand Down
30 changes: 19 additions & 11 deletions BytecodeApi/IO/CommandLine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using BytecodeApi.Extensions;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;

Expand All @@ -21,19 +22,26 @@ public static string[] GetArguments(string commandLine)
{
Check.ArgumentNull(commandLine);

nint argumentsPtr = Native.CommandLineToArgvW(commandLine, out int count);
if (argumentsPtr == 0)
if (commandLine.IsNullOrWhiteSpace())
{
throw Throw.Win32();
return new string[0];
}

try
{
return Create.Array(count, i => Marshal.PtrToStringUni(Marshal.ReadIntPtr(argumentsPtr, i * nint.Size)) ?? "");
}
finally
else
{
Native.LocalFree(argumentsPtr);
nint argumentsPtr = Native.CommandLineToArgvW(commandLine, out int count);
if (argumentsPtr == 0)
{
throw Throw.Win32();
}

try
{
return Create.Array(count, i => Marshal.PtrToStringUni(Marshal.ReadIntPtr(argumentsPtr, i * nint.Size)) ?? "");
}
finally
{
Native.LocalFree(argumentsPtr);
}
}
}
/// <summary>
Expand Down

0 comments on commit 7ff125a

Please sign in to comment.