mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 09:46:54 +02:00
[Hosts]Don't parse commented lines with an address and host in the middle (#26415)
This commit is contained in:
committed by
GitHub
parent
9960d2d536
commit
f6f31c8c32
@@ -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);
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user