Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements #246

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.188">
<PackageReference Include="Meziantou.Analyzer" Version="2.0.189">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,7 @@ public void AppendFormat<T1, T2, T3, T4, T5>(
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetValidArgumentIndex(ReadOnlySpan<char> placeholder, int allowedRange)
{
#pragma warning disable MA0011
if (!int.TryParse(placeholder[1..^1], out var argIndex))
#pragma warning restore MA0011
if (!int.TryParse(placeholder[1..^1], null, out var argIndex))
{
throw new FormatException("Invalid argument index in format string: " + placeholder.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static string Concat<T>(params T[] values)

using var sb = new ValueStringBuilder(stackalloc char[128]);
sb.AppendJoin(string.Empty, values);

return sb.ToString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ ref Unsafe.As<char, byte>(ref sourceRef),
/// Finds the smallest power of 2 which is greater than or equal to <paramref name="minimum"/>.
/// </summary>
/// <param name="minimum">The value the result should be greater than or equal to.</param>
/// <returns>The smallest power of 2 >= <paramref name="minimum"/>.</returns>
/// <returns>The smallest power of 2 &gt;= <paramref name="minimum"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int FindSmallestPowerOf2Above(int minimum)
{
return 1 << (int)Math.Ceiling(Math.Log2(minimum));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public ref partial struct ValueStringBuilder
/// Creates an enumerator over the characters in the builder.
/// </summary>
/// <returns>An enumerator over the characters in the builder.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Enumerator GetEnumerator() => new(buffer[..bufferPosition]);

/// <summary>Enumerates the elements of a <see cref="Span{T}"/>.</summary>
Expand Down
4 changes: 4 additions & 0 deletions src/LinkDotNet.StringBuilder/ValueStringBuilder.Pad.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.CompilerServices;

namespace LinkDotNet.StringBuilder;

public ref partial struct ValueStringBuilder
Expand All @@ -7,6 +9,7 @@ public ref partial struct ValueStringBuilder
/// </summary>
/// <param name="totalWidth">Total width of the string after padding.</param>
/// <param name="paddingChar">Character to pad the string with.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PadLeft(int totalWidth, char paddingChar)
{
if (totalWidth <= bufferPosition)
Expand All @@ -27,6 +30,7 @@ public void PadLeft(int totalWidth, char paddingChar)
/// </summary>
/// <param name="totalWidth">Total width of the string after padding.</param>
/// <param name="paddingChar">Character to pad the string with.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PadRight(int totalWidth, char paddingChar)
{
if (totalWidth <= bufferPosition)
Expand Down
22 changes: 18 additions & 4 deletions src/LinkDotNet.StringBuilder/ValueStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,49 @@ public ValueStringBuilder(int initialCapacity)
/// <value>
/// The current length of the represented string.
/// </value>
public readonly int Length => bufferPosition;
public readonly int Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => bufferPosition;
}

/// <summary>
/// Gets the current maximum capacity before the span must be resized.
/// </summary>
/// <value>
/// The current maximum capacity before the span must be resized.
/// </value>
public readonly int Capacity => buffer.Length;
public readonly int Capacity
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => buffer.Length;
}

/// <summary>
/// Returns the character at the given index or throws an <see cref="IndexOutOfRangeException"/> if the index is bigger than the string.
/// </summary>
/// <param name="index">Character position to be retrieved.</param>
public readonly ref char this[int index] => ref buffer[index];
public readonly ref char this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref buffer[index];
}

/// <summary>
/// Defines the implicit conversion of a <see cref="string"/> to <see cref="ValueStringBuilder"/>.
/// </summary>
/// <param name="fromString">The string as initial buffer.</param>
#pragma warning disable CA2225
public static implicit operator ValueStringBuilder(string fromString) => new(fromString.AsSpan());
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator ValueStringBuilder(string fromString) => new(fromString);
#pragma warning restore CA2225

/// <summary>
/// Defines the implicit conversion of a <see cref="ReadOnlySpan{Char}"/> to <see cref="ValueStringBuilder"/>.
/// </summary>
/// <param name="fromString">The string as initial buffer.</param>
#pragma warning disable CA2225
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator ValueStringBuilder(ReadOnlySpan<char> fromString) => new(fromString);
#pragma warning restore CA2225

Expand Down
6 changes: 5 additions & 1 deletion src/LinkDotNet.StringBuilder/ValueStringBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.CompilerServices;

namespace LinkDotNet.StringBuilder;

/// <summary>
Expand All @@ -12,9 +14,10 @@ public static class ValueStringBuilderExtensions
/// <returns>A new <see cref="System.Text.StringBuilder"/> instance with the string represented
/// by this <paramref name="builder"/>.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static System.Text.StringBuilder ToStringBuilder(this ValueStringBuilder builder)
{
var stringBuilder = new System.Text.StringBuilder();
var stringBuilder = new System.Text.StringBuilder(builder.Length);
stringBuilder.Append(builder.AsSpan());
return stringBuilder;
}
Expand All @@ -25,6 +28,7 @@ public static System.Text.StringBuilder ToStringBuilder(this ValueStringBuilder
/// <param name="builder">The builder from which the new instance is derived.</param>
/// <returns>A new <see cref="ValueStringBuilder"/> instance with the string represented by this builder.</returns>
/// <exception cref="ArgumentNullException">Throws if <paramref name="builder"/> is null.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ValueStringBuilder ToValueStringBuilder(this System.Text.StringBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="xunit.v3" Version="1.0.1" />
<PackageReference Include="xunit.v3" Version="2.0.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace LinkDotNet.StringBuilder.UnitTests;

public class ValueStringBuilderAppendTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace LinkDotNet.StringBuilder.UnitTests;

public class ValueStringBuilderAppendFormatTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;

namespace LinkDotNet.StringBuilder.UnitTests;

public class ValueStringBuilderAppendJoinTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace LinkDotNet.StringBuilder.UnitTests;

public class ValueStringBuilderInsertTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Linq;

namespace LinkDotNet.StringBuilder.UnitTests;

public class ValueStringBuilderReplaceTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace LinkDotNet.StringBuilder.UnitTests;

public class ValueStringBuilderTrimTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace LinkDotNet.StringBuilder.UnitTests;

public class ValueStringBuilderExtensionsTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace LinkDotNet.StringBuilder.UnitTests;
namespace LinkDotNet.StringBuilder.UnitTests;

public class ValueStringBuilderTests
{
Expand Down