8000 StringBuilderExt - Change Append2DigitsZeroPadded to array-lookup by snakefoot · Pull Request #5890 · NLog/NLog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

StringBuilderExt - Change Append2DigitsZeroPadded to array-lookup #5890

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

Merged
merged 1 commit into from
Jun 21, 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
50 changes: 38 additions & 12 deletions src/NLog/Internal/StringBuilderExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//

using System;
using System.Globalization;
using System.IO;
using System.Text;
using NLog.MessageTemplates;

namespace NLog.Internal
{
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using NLog.MessageTemplates;

/// <summary>
/// Helpers for <see cref="StringBuilder"/>, which is used in e.g. layout renderers.
/// </summary>
Expand Down Expand Up @@ -75,6 +76,7 @@ public static void AppendFormattedValue(this StringBuilder builder, object value
/// <param name="value">value to append</param>
public static void AppendInvariant(this StringBuilder builder, int value)
{
#if NETFRAMEWORK
// Deal with negative numbers
if (value < 0)
{
Expand All @@ -86,6 +88,9 @@ public static void AppendInvariant(this StringBuilder builder, int value)
{
AppendInvariant(builder, (uint)value);
}
#else
builder.Append(value);
#endif
}

/// <summary>
Expand All @@ -97,6 +102,7 @@ public static void AppendInvariant(this StringBuilder builder, int value)
/// <param name="value">value to append</param>
public static void AppendInvariant(this StringBuilder builder, uint value)
{
#if NETFRAMEWORK
if (value == 0)
{
builder.Append('0');
Expand All @@ -105,6 +111,9 @@ public static void AppendInvariant(this StringBuilder builder, uint value)

int digitCount = CalculateDigitCount(value);
ApppendValueWithDigitCount(builder, value, digitCount);
#else
builder.Append(value);
#endif
}

private static int CalculateDigitCount(uint value)
Expand Down Expand Up @@ -386,9 +395,17 @@ public static bool EqualTo(this StringBuilder builder, string other)
/// <param name="number">the number</param>
internal static void Append2DigitsZeroPadded(this StringBuilder builder, int number)
{
builder.Append((char)((number / 10) + '0'));
builder.Append((char)((number % 10) + '0'));
if (number < 0 || number >= _zeroPaddedDigits.Length)
{
builder.Append((char)((number / 10) + '0'));
builder.Append((char)((number % 10) + '0'));
}
else
{
builder.Append(_zeroPaddedDigits[number]);
}
}
private static readonly string[] _zeroPaddedDigits = Enumerable.Range(0, 60).Select(i => i.ToString("D2")).ToArray();

/// <summary>
/// Append a number and pad with 0 to 4 digits
Expand All @@ -397,10 +414,19 @@ internal static void Append2DigitsZeroPadded(this StringBuilder builder, int num
/// <param name="number">the number</param>
internal static void Append4DigitsZeroPadded(this StringBuilder builder, int number)
{
builder.Append((char)(((number / 1000) % 10) + '0'));
builder.Append((char)(((number / 100) % 10) + '0'));
builder.Append((char)(((number / 10) % 10) + '0'));
builder.Append((char)(((number / 1) % 10) + '0'));
#if !NETFRAMEWORK
if (number > 999 && number < 10000)
{
builder.Append(number);
}
else
#endif
{
builder.Append((char)(((number / 1000) % 10) + '0'));
builder.Append((char)(((number / 100) % 10) + '0'));
builder.Append((char)(((number / 10) % 10) + '0'));
builder.Append((char)((number % 10) + '0'));
}
}

/// <summary>
Expand Down
11 changes: 4 additions & 7 deletions src/NLog/LayoutRenderers/LevelLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,11 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)

private static string GetUpperCaseString(LogLevel level)
{
try
{
return _upperCaseMapper[level.Ordinal];
}
catch (IndexOutOfRangeException)
{
var ordinal = level.Ordinal;
if (ordinal < 0 || ordinal >= _upperCaseMapper.Length)
return level.ToString().ToUpperInvariant();
}
else
return _upperCaseMapper[ordinal];
}

private string GetFullNameString(LogLevel level)
Expand Down
Loading
0