Code/AutoRig/NameUtil.cs

Utility class that sanitizes names for AutoRig. It lowercases the input, replaces non-alphanumeric characters with underscores, collapses repeated underscores, trims leading/trailing underscores, and returns "part" when the result is empty.

File Access
namespace AutoRig;

/// <summary>Name sanitation shared by solvers and the exporter.</summary>
public static class NameUtil
{
    /// <summary>Lowercase, non-alphanumerics to single underscores, trimmed; "part" when empty.</summary>
    public static string Sanitize( string name )
    {
        ArgumentNullException.ThrowIfNull( name );
        var chars = name.Trim().ToLowerInvariant().ToCharArray();
        for ( var i = 0; i < chars.Length; i++ )
            if ( !char.IsLetterOrDigit( chars[i] ) )
                chars[i] = '_';
        var result = new string( chars );
        while ( result.Contains( "__", StringComparison.Ordinal ) )
            result = result.Replace( "__", "_" );
        result = result.Trim( '_' );
        return result.Length == 0 ? "part" : result;
    }
}