Skip to content

Commit

Permalink
Use HashSet to display each wrong letter only once
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmairegger committed Apr 9, 2019
1 parent 166b8ae commit 59e014c
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions 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<T> : PropertyValidator
{
private readonly Charsets _charset;
Expand Down Expand Up @@ -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<char>();
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()));
}
}

Expand Down

0 comments on commit 59e014c

Please sign in to comment.