Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMatthewLayton committed Sep 14, 2021
2 parents ae0f033 + fa6cd71 commit 5773ca6
Show file tree
Hide file tree
Showing 47 changed files with 1,845 additions and 426 deletions.
117 changes: 117 additions & 0 deletions OnixLabs.Core.UnitTests/StringExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2020-2021 ONIXLabs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Xunit;

namespace OnixLabs.Core.UnitTests
{
public sealed class StringExtensionTests
{
[Theory(DisplayName = "SubstringBefore should return the expected result (char)")]
[InlineData("First:Second", "First", ':')]
[InlineData("12345+678910", "12345", '+')]
public void SubstringBeforeShouldReturnTheExpectedResultC(string value, string expected, char delimiter)
{
// Arrange / Act
string actual = value.SubstringBefore(delimiter);

// Assert
Assert.Equal(expected, actual);
}

[Theory(DisplayName = "SubstringBefore should return the expected result (string)")]
[InlineData("First:Second", "First", ":")]
[InlineData("12345+678910", "12345", "+")]
public void SubstringBeforeShouldReturnTheExpectedResultS(string value, string expected, string delimiter)
{
// Arrange / Act
string actual = value.SubstringBefore(delimiter);

// Assert
Assert.Equal(expected, actual);
}

[Theory(DisplayName = "SubstringBeforeLast should return the expected result (char)")]
[InlineData("First:Second:Third", "First:Second", ':')]
[InlineData("12345+678910+12345", "12345+678910", '+')]
public void SubstringBeforeLastShouldReturnTheExpectedResultC(string value, string expected, char delimiter)
{
// Arrange / Act
string actual = value.SubstringBeforeLast(delimiter);

// Assert
Assert.Equal(expected, actual);
}

[Theory(DisplayName = "SubstringBeforeLast should return the expected result (string)")]
[InlineData("First:Second:Third", "First:Second", ":")]
[InlineData("12345+678910+12345", "12345+678910", "+")]
public void SubstringBeforeLastShouldReturnTheExpectedResultS(string value, string expected, string delimiter)
{
// Arrange / Act
string actual = value.SubstringBeforeLast(delimiter);

// Assert
Assert.Equal(expected, actual);
}

[Theory(DisplayName = "SubstringAfter should return the expected result (char)")]
[InlineData("First:Second", "Second", ':')]
[InlineData("12345+678910", "678910", '+')]
public void SubstringAfterShouldReturnTheExpectedResultC(string value, string expected, char delimiter)
{
// Arrange / Act
string actual = value.SubstringAfter(delimiter);

// Assert
Assert.Equal(expected, actual);
}

[Theory(DisplayName = "SubstringAfter should return the expected result (string)")]
[InlineData("First:Second", "Second", ":")]
[InlineData("12345+678910", "678910", "+")]
public void SubstringAfterShouldReturnTheExpectedResultS(string value, string expected, string delimiter)
{
// Arrange / Act
string actual = value.SubstringAfter(delimiter);

// Assert
Assert.Equal(expected, actual);
}

[Theory(DisplayName = "SubstringAfterLast should return the expected result (char)")]
[InlineData("First:Second:Third", "Third", ':')]
[InlineData("12345+678910+12345", "12345", '+')]
public void SubstringAfterLastShouldReturnTheExpectedResultC(string value, string expected, char delimiter)
{
// Arrange / Act
string actual = value.SubstringAfterLast(delimiter);

// Assert
Assert.Equal(expected, actual);
}

[Theory(DisplayName = "SubstringAfterLast should return the expected result (string)")]
[InlineData("First:Second:Third", "Third", ":")]
[InlineData("12345+678910+12345", "12345", "+")]
public void SubstringAfterLastShouldReturnTheExpectedResultS(string value, string expected, string delimiter)
{
// Arrange / Act
string actual = value.SubstringAfterLast(delimiter);

// Assert
Assert.Equal(expected, actual);
}
}
}
8 changes: 7 additions & 1 deletion OnixLabs.Core/OnixLabs.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@
<Title>OnixLabs.Core</Title>
<Authors>ONIXLabs</Authors>
<Description>ONIXLabs Core API for .NET</Description>
<AssemblyVersion>1.0.0</AssemblyVersion>
<AssemblyVersion>2.0.0</AssemblyVersion>
<NeutralLanguage>en</NeutralLanguage>
<Copyright>Copyright © ONIXLabs 2020-2021</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>2.0.0</PackageVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<Optimize>true</Optimize>
<DocumentationFile>bin\Debug\net5.0\OnixLabs.Core.xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DocumentationFile>bin\Release\net5.0\OnixLabs.Core.xml</DocumentationFile>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

</Project>
207 changes: 207 additions & 0 deletions OnixLabs.Core/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
// Copyright 2020-2021 ONIXLabs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.ComponentModel;

namespace OnixLabs.Core
{
/// <summary>
/// Provides extension methods for strings.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class StringExtensions
{
/// <summary>
/// The value of an index not found.
/// </summary>
private const int IndexNotFound = -1;

/// <summary>
/// Returns a substring before the first occurrence of the specified delimiter.
/// </summary>
/// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param>
/// <param name="delimiter">The delimiter to find within the original string.</param>
/// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param>
/// <returns>Returns a substring before the first occurrence of the specified delimiter.</returns>
public static string SubstringBefore(
this string value,
char delimiter,
string? defaultValue = null)
{
int index = value.IndexOf(delimiter);

return index switch
{
IndexNotFound => defaultValue ?? value,
_ => value[..index]
};
}

/// <summary>
/// Returns a substring before the first occurrence of the specified delimiter.
/// </summary>
/// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param>
/// <param name="delimiter">The delimiter to find within the original string.</param>
/// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param>
/// <param name="comparison">Specifies the string comparison rule for the search.</param>
/// <returns>Returns a substring before the first occurrence of the specified delimiter.</returns>
public static string SubstringBefore(
this string value,
string delimiter,
string? defaultValue = null,
StringComparison comparison = StringComparison.Ordinal)
{
int index = value.IndexOf(delimiter, comparison);

return index switch
{
IndexNotFound => defaultValue ?? value,
_ => value[..index]
};
}

/// <summary>
/// Returns a substring before the last occurrence of the specified delimiter.
/// </summary>
/// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param>
/// <param name="delimiter">The delimiter to find within the original string.</param>
/// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param>
/// <returns>Returns a substring before the last occurrence of the specified delimiter.</returns>
public static string SubstringBeforeLast(
this string value,
char delimiter,
string? defaultValue = null)
{
int index = value.LastIndexOf(delimiter);

return index switch
{
IndexNotFound => defaultValue ?? value,
_ => value[..index]
};
}

/// <summary>
/// Returns a substring before the last occurrence of the specified delimiter.
/// </summary>
/// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param>
/// <param name="delimiter">The delimiter to find within the original string.</param>
/// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param>
/// <param name="comparison">Specifies the string comparison rule for the search.</param>
/// <returns>Returns a substring before the last occurrence of the specified delimiter.</returns>
public static string SubstringBeforeLast(
this string value,
string delimiter,
string? defaultValue = null,
StringComparison comparison = StringComparison.Ordinal)
{
int index = value.LastIndexOf(delimiter, comparison);

return index switch
{
IndexNotFound => defaultValue ?? value,
_ => value[..index]
};
}

/// <summary>
/// Returns a substring after the first occurrence of the specified delimiter.
/// </summary>
/// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param>
/// <param name="delimiter">The delimiter to find within the original string.</param>
/// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param>
/// <returns>Returns a substring after the first occurrence of the specified delimiter.</returns>
public static string SubstringAfter(
this string value,
char delimiter,
string? defaultValue = null)
{
int index = value.IndexOf(delimiter);

return index switch
{
IndexNotFound => defaultValue ?? value,
_ => value[(index + 1)..value.Length]
};
}

/// <summary>
/// Returns a substring after the first occurrence of the specified delimiter.
/// </summary>
/// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param>
/// <param name="delimiter">The delimiter to find within the original string.</param>
/// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param>
/// <param name="comparison">Specifies the string comparison rule for the search.</param>
/// <returns>Returns a substring after the first occurrence of the specified delimiter.</returns>
public static string SubstringAfter(
this string value,
string delimiter,
string? defaultValue = null,
StringComparison comparison = StringComparison.Ordinal)
{
int index = value.IndexOf(delimiter, comparison);

return index switch
{
IndexNotFound => defaultValue ?? value,
_ => value[(index + delimiter.Length)..value.Length]
};
}

/// <summary>
/// Returns a substring after the last occurrence of the specified delimiter.
/// </summary>
/// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param>
/// <param name="delimiter">The delimiter to find within the original string.</param>
/// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param>
/// <returns>Returns a substring after the last occurrence of the specified delimiter.</returns>
public static string SubstringAfterLast(
this string value,
char delimiter,
string? defaultValue = null)
{
int index = value.LastIndexOf(delimiter);

return index switch
{
IndexNotFound => defaultValue ?? value,
_ => value[(index + 1)..value.Length]
};
}

/// <summary>
/// Returns a substring after the last occurrence of the specified delimiter.
/// </summary>
/// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param>
/// <param name="delimiter">The delimiter to find within the original string.</param>
/// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param>
/// <param name="comparison">Specifies the string comparison rule for the search.</param>
/// <returns>Returns a substring after the last occurrence of the specified delimiter.</returns>
public static string SubstringAfterLast(
this string value,
string delimiter,
string? defaultValue = null,
StringComparison comparison = StringComparison.Ordinal)
{
int index = value.LastIndexOf(delimiter, comparison);

return index switch
{
IndexNotFound => defaultValue ?? value,
_ => value[(index + delimiter.Length)..value.Length]
};
}
}
}
4 changes: 2 additions & 2 deletions OnixLabs.Core/Text/Base16.Equatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace OnixLabs.Core.Text
/// <returns>true if the object is equal to this instance; otherwise, false.</returns>
public bool Equals(Base16 other)
{
return other.value.SequenceEqual(value);
return other.Value.SequenceEqual(Value);
}

/// <summary>
Expand All @@ -70,7 +70,7 @@ public override bool Equals(object? obj)
/// <returns>A hash code for this instance.</returns>
public override int GetHashCode()
{
return HashCode.Combine(value);
return HashCode.Combine(Value);
}
}
}
6 changes: 3 additions & 3 deletions OnixLabs.Core/Text/Base16.To.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public readonly partial struct Base16
/// <returns>Returns a <see cref="byte"/> array that represents the current object.</returns>
public byte[] ToByteArray()
{
return value.Copy();
return Value.Copy();
}

/// <summary>
Expand All @@ -47,7 +47,7 @@ public string ToPlainTextString()
/// <returns>Returns a <see cref="string"/> that represents the current object in plain text.</returns>
public string ToPlainTextString(Encoding encoding)
{
return encoding.GetString(value);
return encoding.GetString(Value);
}

/// <summary>
Expand All @@ -56,7 +56,7 @@ public string ToPlainTextString(Encoding encoding)
/// <returns>A <see cref="string"/> that represents the current object.</returns>
public override string ToString()
{
return Convert.ToHexString(value).ToLower();
return Convert.ToHexString(Value).ToLower();
}
}
}
Loading

0 comments on commit 5773ca6

Please sign in to comment.