Skip to content

Commit 0c1fb1b

Browse files
committed
Improved Guid.ToUppercaseAsciiLetters() extension
1 parent 6961808 commit 0c1fb1b

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Microsoft.Toolkit.Uwp.UI.Media/Extensions/System/GuidExtensions.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System;
66
using System.Diagnostics.Contracts;
7-
using System.Linq;
87

98
namespace Microsoft.Toolkit.Uwp.UI.Media
109
{
@@ -19,12 +18,19 @@ internal static class GuidExtensions
1918
/// <param name="guid">The input <see cref="Guid"/> to process</param>
2019
/// <returns>A <see cref="string"/> representation of <paramref name="guid"/> only made up of letters in the [A-Z] range</returns>
2120
[Pure]
22-
public static string ToUppercaseAsciiLetters(this Guid guid)
21+
public static string ToUppercaseAsciiLetters(in this Guid guid)
2322
{
24-
return new string((
25-
from c in guid.ToString("N")
26-
let l = char.IsDigit(c) ? (char)('G' + c - '0') : c
27-
select l).ToArray());
23+
// Composition IDs must only be composed of characters in the [A-Z0-9_] set,
24+
// and also have the restriction that the initial character cannot be a digit.
25+
// Because of this, we need to prepend an underscore to a serialized guid to
26+
// avoid cases where the first character is a digit. Additionally, we're forced
27+
// to use ToUpper() here because ToString("N") currently returns a lowercase
28+
// hexadecimal string. Note: this extension might be improved once we move to
29+
// .NET 5 in the WinUI 3 release, by using string.Create<TState>(...) to only
30+
// have a single string allocation, and then using Guid.TryFormat(...) to
31+
// serialize the guid in place over the Span<char> starting from the second
32+
// character. For now, this implementation is fine on UWP and still fast enough.
33+
return $"_{guid.ToString("N").ToUpper()}";
2834
}
2935
}
3036
}

0 commit comments

Comments
 (0)