From 59e014c8d1768e074eefcb53a74925b9544135c5 Mon Sep 17 00:00:00 2001 From: Michael Mairegger Date: Tue, 9 Apr 2019 08:29:08 +0200 Subject: [PATCH] Use HashSet to display each wrong letter only once --- Validators/LatinValidators.cs | 43 +++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/Validators/LatinValidators.cs b/Validators/LatinValidators.cs index 27aa6647..4dd76714 100644 --- a/Validators/LatinValidators.cs +++ b/Validators/LatinValidators.cs @@ -1,11 +1,13 @@ using System; -using System.Text; +using System.Collections.Generic; +using System.Linq; using System.Text.RegularExpressions; using FluentValidation; using FluentValidation.Validators; namespace FatturaElettronica.Validators { + public abstract class LatinBaseValidator : PropertyValidator { private readonly Charsets _charset; @@ -36,29 +38,32 @@ protected override void PrepareMessageFormatterForValidationError(PropertyValida { base.PrepareMessageFormatterForValidationError(context); - var sb = new StringBuilder(); - foreach (var c in context.PropertyValue.ToString()) + var invalidLetters = new HashSet(); + if (context.PropertyValue != null) { - int upperLimit; - switch (_charset) + foreach (var letter in context.PropertyValue.ToString()) { - case Charsets.BasicLatin: - upperLimit = 0x7F; - break; - case Charsets.Latin1Supplement: - upperLimit = 0xFF; - break; - default: - throw new ArgumentOutOfRangeException(); - } + int upperLimit; + switch (_charset) + { + case Charsets.BasicLatin: + upperLimit = 0x7F; + break; + case Charsets.Latin1Supplement: + upperLimit = 0xFF; + break; + default: + throw new ArgumentOutOfRangeException(); + } - if (c > upperLimit) - { - sb.Append(c); + if (letter > upperLimit) + { + invalidLetters.Add(letter); + } } } - - context.MessageFormatter.AppendArgument("NonLatinCode", sb.ToString()); + + context.MessageFormatter.AppendArgument("NonLatinCode", new string(invalidLetters.ToArray())); } }