fish-station/Content.Shared/_Sunrise/Helpers/StringExtensions.cs
ThereDrD b38dc9bcce
Обновление консоли станционного учета (#2800)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-08-16 06:13:21 +03:00

39 lines
1.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Text;
using System.Text.RegularExpressions;
namespace Content.Shared._Sunrise.Helpers;
public static class StringExtensions
{
private static readonly Regex AllowedCharsRegex = new Regex(
@"[^a-zA-Zа-яА-ЯёЁ0-9\s.,!?;:\-_\(\)\[\]{}""'/\\@#%\^&\*\+=<>]",
RegexOptions.Compiled | RegexOptions.CultureInvariant
);
/// <summary>
/// Санитизация пользовательского ввода:
/// - Убирает лишние пробелы по краям
/// - Обрезает до maxLength (если задан)
/// - Убирает недопустимые символы
/// - Нормализует Unicode
/// </summary>
/// <remarks>
/// Рекомендуется для UI, содержащих LineEdit или подобные возможности передать текст на сервер.
/// </remarks>
public static string SanitizeInput(this string? input, int? maxLength = null)
{
if (string.IsNullOrEmpty(input))
return string.Empty;
input = input.Trim();
if (maxLength.HasValue && input.Length > maxLength.Value)
input = input.Substring(0, maxLength.Value);
input = input.Normalize(NormalizationForm.FormC);
input = AllowedCharsRegex.Replace(input, string.Empty);
return input;
}
}