Skip to content

Commit

Permalink
Allow QueryStringBuilder to be initialized from a string or Uri
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryptoc1 committed Jul 17, 2024
1 parent 36c9652 commit 8cefe11
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/Http/QueryStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,44 @@ namespace ESCd.Extensions.Http;

/// <summary> Provides a "fluent builder" syntax for constructing query string parameters. </summary>
/// <remarks> This class cannot be inherited. </remarks>
/// <param name="capacity"> The suggested starting size of the instance. </param>
public sealed class QueryStringBuilder( int capacity )
public sealed class QueryStringBuilder
{
private readonly StringBuilder builder = new( "?", Math.Max( 1, capacity ) );
private readonly StringBuilder builder;

/// <summary> Get or set the suggested size of the instance. </summary>
public int Capacity { get => builder.Capacity; set => builder.Capacity = value; }

/// <summary> Create an empty query string. </summary>
public QueryStringBuilder( ) : this( default )
public QueryStringBuilder( )
{
builder = new( "?" );
}

/// <summary> Create an empty query string. </summary>
/// <param name="capacity"> The suggested starting size of the instance. </param>
public QueryStringBuilder( int capacity )
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero( capacity );
builder = new( "?", Math.Max( 1, capacity ) );
}

/// <summary> Create a query string from the given <paramref name="url"/>. </summary>
public QueryStringBuilder( Uri url )
{
ArgumentNullException.ThrowIfNull( url );
builder = new( url.Query ?? "?" );
}

/// <summary> Create a query string from the given <paramref name="value"/>. </summary>
public QueryStringBuilder( string value )
{
ArgumentException.ThrowIfNullOrWhiteSpace( value );
if( !value.StartsWith( '?' ) )
{
throw new ArgumentException( "A QueryString must start with a leading '?'.", nameof( value ) );
}

builder = new( value );
}

/// <summary> Appends a parameter with the given <paramref name="name"/> and <paramref name="value"/>. </summary>
Expand Down

0 comments on commit 8cefe11

Please sign in to comment.