Skip to content

Commit

Permalink
R# Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsmith1968 committed Jan 11, 2025
1 parent 4dc4c5f commit 9d5c1d2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/DNX.Extensions/Arrays/ArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static T[] ShiftLeft<T>(this T[] input)
{
if (input == null)
{
return new T[0];
return [];
}

var shiftedArray = new T[input.Length];
Expand Down
6 changes: 4 additions & 2 deletions src/DNX.Extensions/Arrays/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ public static string GetAsciiString(this byte[] input)
/// Converts A byte array into an hex string
/// </summary>
/// <param name="input">The byte[] to turn into the string</param>
public static string ToHexString(this byte[] input)
/// <param name="format">The format to use for each byte</param>
public static string ToHexString(this byte[] input, string format = "X2")
{
var hex = new StringBuilder(input.Length * 2);
var byteFormat = "{0:" + format + "}";
foreach (var b in input)
hex.AppendFormat("{0:x2}", b);
hex.AppendFormat(byteFormat, b);
return hex.ToString();
}
}
4 changes: 2 additions & 2 deletions src/DNX.Extensions/Reflection/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class ReflectionExtensions
/// <param name="flags">The flags.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>System.Object.</returns>
public static object GetPropertyValueByName<T>(this T instance, string propertyName, BindingFlags flags, object defaultValue = default)
public static object GetPropertyValueByName<T>(this T instance, string propertyName, BindingFlags flags, object defaultValue = null)
{
var pi = typeof(T).GetProperty(propertyName, flags);
if (pi == null)
Expand All @@ -41,7 +41,7 @@ public static object GetPropertyValueByName<T>(this T instance, string propertyN
/// <param name="propertyName">Name of the property.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>System.Object.</returns>
public static object GetPrivatePropertyValue<T>(this T instance, string propertyName, object defaultValue = default)
public static object GetPrivatePropertyValue<T>(this T instance, string propertyName, object defaultValue = null)
{
return instance.GetPropertyValueByName(propertyName, BindingFlags.Instance | BindingFlags.NonPublic, defaultValue);
}
Expand Down
19 changes: 15 additions & 4 deletions src/DNX.Extensions/Strings/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public static string AsFarAs(this string text, string endText, StringComparison
return null;
}

var endTextLength = endText?.Length ?? 0;
var endTextLength = endText.Length;

var endIndex = text.IndexOf(endText, comparison);

Expand All @@ -415,7 +415,7 @@ public static string AsFarAsLast(this string text, string endText, StringCompari
return null;
}

var endTextLength = endText?.Length ?? 0;
var endTextLength = endText.Length;

var endIndex = text.LastIndexOf(endText, comparison);

Expand Down Expand Up @@ -841,19 +841,30 @@ public static string Wordify(this string text, IList<string> preservedWords)
}

/// <summary>
/// Truncates a string to the given length if it is longer than the given length, otherwise returns the original string.
/// Truncates a string to the given length of the leftmost characters if it is longer than the given length, otherwise returns the original string.
/// </summary>
public static string Truncate(this string input, int length)
public static string Left(this string input, int length)
{
return input != null && input.Length > length
? input.Substring(0, length)
: input;
}

/// <summary>
/// Truncates a string to the given length of the rightmost characters if it is longer than the given length, otherwise returns the original string.
/// </summary>
public static string Right(this string input, int length)
{
return input != null && input.Length > length
? input.Substring(input.Length - length, length)
: input;
}

/// <summary>
/// Converts an ASCII encoded string to Hexadecimal
/// </summary>
/// <param name="input">ASCII Encoded Input</param>
/// <param name="format">The format to use to generate the Hex value</param>
/// <returns>Input as Hexadecimal</returns>
public static string ToHexString(this string input, string format = "X2")
{
Expand Down
26 changes: 23 additions & 3 deletions tests/DNX.Extensions.Tests/Strings/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,18 +1138,38 @@ public void Wordify_with_reserved_words_Tests(string text, string reservedWordsT
}
}

public class Truncate
public class Left
{
[Theory]
[InlineData("Some text", 5, "Some ")]
[InlineData("Some text", 9, "Some text")]
[InlineData("Some text", 20, "Some text")]
[InlineData("Some text", 0, "")]
[InlineData("", 0, "")]
[InlineData("", 5, "")]
[InlineData(null, 2, null)]
public void Test_Truncate(string text, int length, string expectedResult)
public void Test_Left(string text, int length, string expectedResult)
{
var result = text.Truncate(length);
var result = text.Left(length);

// Assert
result.Should().Be(expectedResult);
}
}

public class Right
{
[Theory]
[InlineData("Some text", 5, " text")]
[InlineData("Some text", 9, "Some text")]
[InlineData("Some text", 20, "Some text")]
[InlineData("Some text", 0, "")]
[InlineData("", 0, "")]
[InlineData("", 5, "")]
[InlineData(null, 2, null)]
public void Test_Right(string text, int length, string expectedResult)
{
var result = text.Right(length);

// Assert
result.Should().Be(expectedResult);
Expand Down

0 comments on commit 9d5c1d2

Please sign in to comment.