Skip to content

Commit

Permalink
Add PcreSubstituteException
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Sep 24, 2024
1 parent c4e177a commit c8e1bb2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/PCRE.NET.Tests/PcreNet/SubstituteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,12 @@ public void should_return_same_subject_instance_if_possible()

Assert.That(re.Substitute(subject, "bar"), Is.SameAs(subject));
}

[Test]
public void should_throw_on_invalid_replacement_syntax()
{
var re = new PcreRegex("a(b+)c");

Assert.Throws<PcreSubstituteException>(() => _ = re.Substitute("abc", "${4}"));
}
}
2 changes: 1 addition & 1 deletion src/PCRE.NET/Internal/InternalRegex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public string Substitute(ReadOnlySpan<char> subject,
return result.result_code switch
{
0 => (additionalOptions & PcreConstants.SUBSTITUTE_REPLACEMENT_ONLY) != 0 ? string.Empty : subjectAsString ?? subject.ToString(),
< 0 => throw new PcreMatchException((PcreErrorCode)result.result_code),
< 0 => throw new PcreSubstituteException((PcreErrorCode)result.result_code),
_ => new string(result.output, 0, (int)result.output_length)
};
}
Expand Down
40 changes: 33 additions & 7 deletions src/PCRE.NET/PcreException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class PcreException : Exception
public PcreErrorCode ErrorCode { get; }

/// <summary>
/// Creates a <see cref="PcreException"/>
/// Creates a <see cref="PcreException"/>.
/// </summary>
/// <param name="errorCode">The associated error code.</param>
public PcreException(PcreErrorCode errorCode)
Expand All @@ -25,7 +25,7 @@ public PcreException(PcreErrorCode errorCode)
}

/// <summary>
/// Creates a <see cref="PcreException"/>
/// Creates a <see cref="PcreException"/>.
/// </summary>
/// <param name="errorCode">The associated error code.</param>
/// <param name="message">The exception message.</param>
Expand All @@ -36,7 +36,7 @@ public PcreException(PcreErrorCode errorCode, string message)
}

/// <summary>
/// Creates a <see cref="PcreException"/>
/// Creates a <see cref="PcreException"/>.
/// </summary>
/// <param name="errorCode">The associated error code.</param>
/// <param name="message">The exception message.</param>
Expand All @@ -54,7 +54,7 @@ public PcreException(PcreErrorCode errorCode, string message, Exception? innerEx
public class PcrePatternException : PcreException
{
/// <summary>
/// Creates a <see cref="PcrePatternException"/>
/// Creates a <see cref="PcrePatternException"/>.
/// </summary>
/// <param name="errorCode">The associated error code.</param>
/// <param name="message">The exception message.</param>
Expand All @@ -70,7 +70,7 @@ public PcrePatternException(PcreErrorCode errorCode, string message)
public class PcreMatchException : PcreException
{
/// <summary>
/// Creates a <see cref="PcreMatchException"/>
/// Creates a <see cref="PcreMatchException"/>.
/// </summary>
/// <param name="errorCode">The associated error code.</param>
public PcreMatchException(PcreErrorCode errorCode)
Expand All @@ -79,7 +79,7 @@ public PcreMatchException(PcreErrorCode errorCode)
}

/// <summary>
/// Creates a <see cref="PcreMatchException"/>
/// Creates a <see cref="PcreMatchException"/>.
/// </summary>
/// <param name="errorCode">The associated error code.</param>
/// <param name="message">The exception message.</param>
Expand All @@ -96,7 +96,7 @@ protected PcreMatchException(PcreErrorCode errorCode, string message, Exception?
public class PcreCalloutException : PcreMatchException
{
/// <summary>
/// Creates a <see cref="PcreCalloutException"/>
/// Creates a <see cref="PcreCalloutException"/>.
/// </summary>
/// <param name="message">The exception message.</param>
/// <param name="innerException">The inner exception.</param>
Expand All @@ -105,3 +105,29 @@ public PcreCalloutException(string message, Exception? innerException)
{
}
}

/// <summary>
/// Represents an error that occured during pattern substitution.
/// </summary>
public class PcreSubstituteException : PcreException
{
/// <summary>
/// Creates a <see cref="PcreSubstituteException"/>.
/// </summary>
/// <param name="errorCode">The associated error code.</param>
public PcreSubstituteException(PcreErrorCode errorCode)
: base(errorCode)
{
}

/// <summary>
/// Creates a <see cref="PcreSubstituteException"/>.
/// </summary>
/// <param name="errorCode">The associated error code.</param>
/// <param name="message">The exception message.</param>
/// <param name="innerException">The inner exception.</param>
protected PcreSubstituteException(PcreErrorCode errorCode, string message, Exception? innerException)
: base(errorCode, message, innerException)
{
}
}

0 comments on commit c8e1bb2

Please sign in to comment.