Files
WireMock.Net-wiremock/src/WireMock.Net.Minimal/Util/StringUtils.cs
T
Stef Heyenrath a292f28dda Version 2.x (#1359)
* Version 2.x

* Setup .NET 9

* 12

* cleanup some #if for NETSTANDARD1_3

* cleanup + fix tests for net8

* openapi

* NO ConfigureAwait(false) + cleanup

* .

* #endif

* HashSet

* WireMock.Net.NUnit

* HttpContext

* Add WebSockets (#1423)

* Add WebSockets

* Add tests

* fix

* more tests

* Add tests

* ...

* remove IOwin

* -

* tests

* fluent

* ok

* match

* .

* byte[]

* x

* func

* func

* byte

* trans

* ...

* frameworks.........

* jmes

* xxx

* sc

* using var httpClient = new HttpClient();

* usings

* maxRetries

* up

* xunit v3

* ct

* ---

* ct

* ct2

* T Unit

* WireMock.Net.TUnitTests / 10

* t unit first

* --project

* no tunit

* t2

* --project

* --project

* ci -  --project

* publish ./test/wiremock-coverage.xml

* windows

* .

* log

* ...

* log

* goed

* BodyType

* .

* .

* --scenario

* ...

* pact

* ct

* .

* WireMock.Net.RestClient.AwesomeAssertions (#1427)

* WireMock.Net.RestClient.AwesomeAssertions

* ok

* atpath

* fix test

* sonar fixes

* ports

* proxy test

* FIX?

* ---

* await Task.Delay(100, _ct);

* ?

* --project

* Aspire: use IDistributedApplicationEventingSubscriber (#1428)

* broadcast

* ok

* more tsts

* .

* Collection

* up

* .

* 2

* remove nfluent

* <VersionPrefix>2.0.0-preview-02</VersionPrefix>

* ...

* .

* nuget icon

* .

* <PackageReference Include="JmesPath.Net" Version="1.1.0" />

* x

* 500

* .

* fix some warnings

* ws
2026-03-11 17:02:47 +01:00

94 lines
2.6 KiB
C#

// Copyright © WireMock.Net
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
using WireMock.Matchers;
namespace WireMock.Util;
internal static class StringUtils
{
private static readonly string[] ValidUriSchemes =
[
"ftp://",
"http://",
"https://"
];
private static readonly Func<string, (bool IsConverted, object ConvertedValue)>[] ConversionsFunctions =
[
s => bool.TryParse(s, out var result) ? (true, result) : (false, s),
s => int.TryParse(s, out var result) ? (true, result) : (false, s),
s => long.TryParse(s, out var result) ? (true, result) : (false, s),
s => double.TryParse(s, out var result) ? (true, result) : (false, s),
s => Guid.TryParseExact(s, "D", out var result) ? (true, result) : (false, s),
s => TimeSpan.TryParse(s, out var result) ? (true, result) : (false, s),
s => DateTime.TryParse(s, out var result) ? (true, result) : (false, s),
s =>
{
if (ValidUriSchemes.Any(u => s.StartsWith(u, StringComparison.OrdinalIgnoreCase)) &&
Uri.TryCreate(s, UriKind.RelativeOrAbsolute, out var uri))
{
return (true, uri);
}
return (false, s);
}
];
public static (bool IsConverted, object ConvertedValue) TryConvertToKnownType(string value)
{
foreach (var func in ConversionsFunctions)
{
var result = func(value);
if (result.IsConverted)
{
return result;
}
}
return (false, value);
}
public static MatchOperator ParseMatchOperator(string? value)
{
return value != null && Enum.TryParse<MatchOperator>(value, out var matchOperator)
? matchOperator
: MatchOperator.Or;
}
public static bool TryParseQuotedString(string? value, [NotNullWhen(true)] out string? result, out char quote)
{
result = null;
quote = '\0';
if (value == null || value.Length < 2)
{
return false;
}
quote = value[0]; // This can be single or a double quote
if (quote != '"' && quote != '\'')
{
return false;
}
if (value.Last() != quote)
{
return false;
}
try
{
result = Regex.Unescape(value.Substring(1, value.Length - 2));
return true;
}
catch
{
// Ignore Exception, just continue and return false.
}
return false;
}
}