-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
786 additions
and
824 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
using System.Collections.Immutable; | ||
|
||
namespace LightResults.Common; | ||
|
||
internal static class StringHelper | ||
{ | ||
public static string GetResultString(string typeName, string successString, string informationString) | ||
{ | ||
const string preResultStr = " { IsSuccess = "; | ||
const string postResultStr = " }"; | ||
#if NET6_0_OR_GREATER | ||
var stringLength = typeName.Length + preResultStr.Length + successString.Length + informationString.Length + postResultStr.Length; | ||
|
||
var str = string.Create(stringLength, (typeName, successString, informationString), (span, state) => { span.TryWrite($"{state.typeName}{preResultStr}{state.successString}{state.informationString}{postResultStr}", out _); }); | ||
|
||
return str; | ||
#else | ||
return $"{typeName}{preResultStr}{successString}{informationString}{postResultStr}"; | ||
#endif | ||
} | ||
|
||
public static string GetResultValueString<T>(T value) | ||
{ | ||
var valueString = value?.ToString() ?? ""; | ||
|
||
const string preValueStr = ", Value = "; | ||
const string charStr = "'"; | ||
const string stringStr = "\""; | ||
|
||
if (value is bool || value is sbyte || value is byte || value is short || value is ushort || value is int || value is uint || value is long || value is ulong || | ||
#if NET7_0_OR_GREATER | ||
value is Int128 || value is UInt128 || | ||
#endif | ||
value is decimal || value is float || value is double) | ||
{ | ||
#if NET6_0_OR_GREATER | ||
var stringLength = preValueStr.Length + valueString.Length; | ||
|
||
var str = string.Create(stringLength, valueString, (span, state) => { span.TryWrite($"{preValueStr}{state}", out _); }); | ||
|
||
return str; | ||
#else | ||
return $"{preValueStr}{valueString}"; | ||
#endif | ||
} | ||
|
||
if (value is char) | ||
{ | ||
#if NET6_0_OR_GREATER | ||
var stringLength = preValueStr.Length + charStr.Length + valueString.Length + charStr.Length; | ||
|
||
var str = string.Create(stringLength, valueString, (span, state) => { span.TryWrite($"{preValueStr}{charStr}{state}{charStr}", out _); }); | ||
|
||
return str; | ||
#else | ||
return $"{preValueStr}{charStr}{valueString}{charStr}"; | ||
#endif | ||
} | ||
|
||
if (value is string) | ||
{ | ||
#if NET6_0_OR_GREATER | ||
var stringLength = preValueStr.Length + stringStr.Length + valueString.Length + stringStr.Length; | ||
|
||
var str = string.Create(stringLength, valueString, (span, state) => { span.TryWrite($"{preValueStr}{stringStr}{state}{stringStr}", out _); }); | ||
|
||
return str; | ||
#else | ||
return $"{preValueStr}{stringStr}{valueString}{stringStr}"; | ||
#endif | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
public static string GetResultErrorString(ImmutableArray<IError> errors) | ||
{ | ||
if (errors[0].Message.Length <= 0) | ||
return ""; | ||
|
||
var errorMessage = errors[0].Message; | ||
|
||
const string preErrorStr = ", Error = \""; | ||
const string postErrorStr = "\""; | ||
#if NET6_0_OR_GREATER | ||
var stringLength = preErrorStr.Length + errorMessage.Length + postErrorStr.Length; | ||
|
||
var str = string.Create(stringLength, errorMessage, (span, state) => { span.TryWrite($"{preErrorStr}{state}{postErrorStr}", out _); }); | ||
|
||
return str; | ||
#else | ||
return $"{preErrorStr}{errorMessage}{postErrorStr}"; | ||
#endif | ||
} | ||
|
||
public static string GetErrorString(IError error) | ||
{ | ||
var errorType = error.GetType().Name; | ||
|
||
if (error.Message.Length <= 0) | ||
return errorType; | ||
|
||
var errorMessage = error.Message; | ||
|
||
const string preErrorStr = " { Message = \""; | ||
const string postErrorStr = "\" }"; | ||
#if NET6_0_OR_GREATER | ||
var stringLength = errorType.Length + preErrorStr.Length + errorMessage.Length + postErrorStr.Length; | ||
|
||
var str = string.Create(stringLength, (errorType, errorMessage), (span, state) => { span.TryWrite($"{state.errorType}{preErrorStr}{state.errorMessage}{postErrorStr}", out _); }); | ||
|
||
return str; | ||
#else | ||
return $"{errorType}{preErrorStr}{errorMessage}{postErrorStr}"; | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
// ReSharper disable CheckNamespace | ||
|
||
namespace System.Runtime.CompilerServices | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
[DebuggerNonUserCode] | ||
internal static class IsExternalInit; | ||
|
||
[ExcludeFromCodeCoverage] | ||
[DebuggerNonUserCode] | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Field | AttributeTargets.Property, Inherited = false)] | ||
internal sealed class RequiredMemberAttribute : Attribute; | ||
|
||
[ExcludeFromCodeCoverage] | ||
[DebuggerNonUserCode] | ||
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)] | ||
internal sealed class CompilerFeatureRequiredAttribute(string featureName) : Attribute | ||
{ | ||
public const string RefStructs = nameof(RefStructs); | ||
public const string RequiredMembers = nameof(RequiredMembers); | ||
public string FeatureName { get; } = featureName; | ||
|
||
public bool IsOptional { get; init; } | ||
} | ||
} | ||
|
||
namespace System.Diagnostics.CodeAnalysis | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
[DebuggerNonUserCode] | ||
[AttributeUsage(AttributeTargets.Constructor)] | ||
internal sealed class SetsRequiredMembersAttribute : Attribute; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.