[Hosts]Don't parse commented lines with an address and host in the middle (#26415)

This commit is contained in:
Davide Giacometti
2023-06-06 17:16:06 +02:00
committed by GitHub
parent 9960d2d536
commit f6f31c8c32
2 changed files with 6 additions and 11 deletions

View File

@@ -74,6 +74,7 @@ namespace Hosts.Tests
[DataRow("\t\thost\t\t10.1.1.1")]
[DataRow(" host 10.1.1.1")]
[DataRow("host 10.1.1.1")]
[DataRow("# comment 10.1.1.1 host # comment")]
public void Not_Valid_Entry(string line)
{
var entry = new Entry(0, line);

View File

@@ -12,6 +12,8 @@ namespace Hosts.Models
{
public partial class Entry : ObservableObject
{
private static readonly char[] _spaceCharacters = new char[] { ' ', '\t' };
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Valid))]
private string _address;
@@ -99,7 +101,7 @@ namespace Hosts.Models
var addressHost = lineSplit[0];
var addressHostSplit = addressHost.Split(' ', '\t');
var addressHostSplit = addressHost.Split(_spaceCharacters, StringSplitOptions.RemoveEmptyEntries);
var hostsBuilder = new StringBuilder();
var commentBuilder = new StringBuilder();
@@ -107,17 +109,9 @@ namespace Hosts.Models
{
var element = addressHostSplit[i].Trim();
if (string.IsNullOrWhiteSpace(element))
if (i == 0 && IPAddress.TryParse(element, out var _) && (element.Contains(':') || element.Contains('.')))
{
continue;
}
if (Address == null)
{
if (IPAddress.TryParse(element, out var _) && (element.Contains(':') || element.Contains('.')))
{
Address = element;
}
Address = element;
}
else
{