Skip to content

Commit

Permalink
Filter newlines from effect display names
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Sep 15, 2023
1 parent fb6dbf1 commit 0d2dd91
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Security;
using System.Text.RegularExpressions;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Models;
Expand All @@ -16,6 +17,11 @@ partial class ID2D1ShaderGenerator
/// </summary>
private static partial class EffectDisplayName
{
/// <summary>
/// A <see cref="Regex"/> instance to find all newlines.
/// </summary>
private static readonly Regex NewLinesRegex = new("[\r\n\v]", RegexOptions.Compiled);

/// <summary>
/// Extracts the effect display name info for the current shader.
/// </summary>
Expand Down Expand Up @@ -53,8 +59,11 @@ private static bool TryGetDefinedEffectDisplayName(Compilation compilation, INam
if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, effectIdAttributeSymbol) &&
attributeData.ConstructorArguments is [{ Value: string { Length: > 0 } value }])
{
// Remove new lines (the values cannot span multiple lines in XML)
string singleLineValue = NewLinesRegex.Replace(value, string.Empty);

// Make sure to escape any invalid XML characters
string escapedValue = SecurityElement.Escape(value);
string escapedValue = SecurityElement.Escape(singleLineValue);

// Trim the display name as well
if (escapedValue.AsSpan().Trim() is { Length: > 0 } trimmedValue)
Expand Down

0 comments on commit 0d2dd91

Please sign in to comment.