Files
sbox-public/engine/Sandbox.Engine/Systems/UI/Parser/StyleParser.StripComments.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

75 lines
1.4 KiB
C#

using System.Text;
namespace Sandbox.UI;
internal static partial class StyleParser
{
public static string StripComments( string v )
{
if ( string.IsNullOrWhiteSpace( v ) )
return v;
StringBuilder builder = new();
var p = new Parse( v );
int lastSafe = 0;
bool commentCanFollow = true;
while ( !p.IsEnd )
{
if ( commentCanFollow && p.Is( '/' ) )
{
p.Pointer++;
if ( p.IsEnd )
throw new System.Exception( "Parse error (file ends in /)" );
if ( p.Is( '/' ) )
{
p.Pointer++;
builder.Append( v.Substring( lastSafe, (p.Pointer - 2) - lastSafe ) );
p = p.JumpToEndOfLine( false );
lastSafe = Math.Min( p.Pointer, p.Length );
}
if ( p.Is( '*' ) )/* */
{
p.Pointer++;
if ( p.IsEnd )
throw new System.Exception( "Parse error (file ends in *)" );
builder.Append( v.Substring( lastSafe, (p.Pointer - 2) - lastSafe ) );
while ( true )
{
if ( p.IsEnd )
throw new System.Exception( "Unterminated Multiline Comment" );
if ( p.Is( '*' ) && p.Next == '/' )
{
p.Pointer += 2;
lastSafe = Math.Min( p.Pointer, p.Length );
break;
}
p.Pointer++;
}
}
}
// don't allow comments after :, because it might be a url
commentCanFollow = !p.Is( ':' );
p.Pointer++;
}
builder.Append( v.Substring( lastSafe, Math.Min( v.Length - lastSafe, p.Pointer - lastSafe ) ) );
return builder.ToString();
}
}