Skip to content

Commit

Permalink
refactor: use file scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kthompson committed Feb 22, 2024
1 parent 56f4baa commit 89420df
Show file tree
Hide file tree
Showing 36 changed files with 1,698 additions and 1,734 deletions.
95 changes: 47 additions & 48 deletions src/Glob/AST/CharacterSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,71 @@
using System.Linq;
using System.Text;

namespace GlobExpressions.AST
namespace GlobExpressions.AST;

internal sealed class CharacterSet : SubSegment
{
internal sealed class CharacterSet : SubSegment
{
public bool Inverted { get; }
public string Characters { get; }
public string ExpandedCharacters { get; }
public bool Inverted { get; }
public string Characters { get; }
public string ExpandedCharacters { get; }

public CharacterSet(string characters, bool inverted)
: base(GlobNodeType.CharacterSet)
{
public CharacterSet(string characters, bool inverted)
: base(GlobNodeType.CharacterSet)
{
Characters = characters;
Inverted = inverted;
this.ExpandedCharacters = CalculateExpandedForm(characters);
}

public bool Matches(char c, bool caseSensitive) => Contains(c, caseSensitive) != this.Inverted;
public bool Matches(char c, bool caseSensitive) => Contains(c, caseSensitive) != this.Inverted;

private bool Contains(char c, bool caseSensitive) => ExpandedCharacters.IndexOf(c.ToString(), caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase) >= 0;
private bool Contains(char c, bool caseSensitive) => ExpandedCharacters.IndexOf(c.ToString(), caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase) >= 0;

private string CalculateExpandedForm(string chars)
private string CalculateExpandedForm(string chars)
{
var sb = new StringBuilder();
var i = 0;
var len = chars.Length;

// if first character is special, add it
if (chars.StartsWith("-") || chars.StartsWith("[") || chars.StartsWith("]"))
{
var sb = new StringBuilder();
var i = 0;
var len = chars.Length;
sb.Append(chars[0]);
i++;
}

// if first character is special, add it
if (chars.StartsWith("-") || chars.StartsWith("[") || chars.StartsWith("]"))
{
sb.Append(chars[0]);
i++;
}
while (true)
{
if (i >= len)
break;

while (true)
if (chars[i] == '-')
{
if (i >= len)
break;

if (chars[i] == '-')
if (i == len - 1)
{
if (i == len - 1)
{
// - is last character so just add it
sb.Append('-');
}
else
{
for (var c = chars[i - 1] + 1; c <= chars[i + 1]; c++)
{
sb.Append((char)c);
}
i++; // skip trailing range
}
}
else if (chars[i] == '/')
{
i++; // skip
// - is last character so just add it
sb.Append('-');
}
else
{
sb.Append(chars[i]);
for (var c = chars[i - 1] + 1; c <= chars[i + 1]; c++)
{
sb.Append((char)c);
}
i++; // skip trailing range
}
i++;
}

return sb.ToString();
else if (chars[i] == '/')
{
i++; // skip
}
else
{
sb.Append(chars[i]);
}
i++;
}

return sb.ToString();
}
}
}
19 changes: 9 additions & 10 deletions src/Glob/AST/CharacterWildcard.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
namespace GlobExpressions.AST
namespace GlobExpressions.AST;

internal sealed class CharacterWildcard : SubSegment
{
internal sealed class CharacterWildcard : SubSegment
{
public static readonly CharacterWildcard Default = new CharacterWildcard();
public static readonly CharacterWildcard Default = new CharacterWildcard();

private CharacterWildcard()
: base(GlobNodeType.CharacterWildcard)
{
private CharacterWildcard()
: base(GlobNodeType.CharacterWildcard)
{
}

public override string ToString() => "?";
}
}
public override string ToString() => "?";
}
21 changes: 10 additions & 11 deletions src/Glob/AST/DirectorySegment.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using System.Collections.Generic;
using System.Linq;

namespace GlobExpressions.AST
namespace GlobExpressions.AST;

internal sealed class DirectorySegment : Segment
{
internal sealed class DirectorySegment : Segment
{
public SubSegment[] SubSegments { get; }
public SubSegment[] SubSegments { get; }

public DirectorySegment(IEnumerable<SubSegment> subSegments)
: base(GlobNodeType.DirectorySegment)
{
public DirectorySegment(IEnumerable<SubSegment> subSegments)
: base(GlobNodeType.DirectorySegment)
{
SubSegments = subSegments.ToArray();
}

public override string ToString()
{
public override string ToString()
{
return string.Join("", SubSegments.Select(x => x.ToString()));
}
}
}
}
21 changes: 10 additions & 11 deletions src/Glob/AST/DirectoryWildcard.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
namespace GlobExpressions.AST
namespace GlobExpressions.AST;

internal sealed class DirectoryWildcard : Segment
{
internal sealed class DirectoryWildcard : Segment
{
public static readonly DirectoryWildcard Default = new DirectoryWildcard();
public static readonly DirectoryWildcard Default = new DirectoryWildcard();

private DirectoryWildcard()
: base(GlobNodeType.DirectoryWildcard)
{
private DirectoryWildcard()
: base(GlobNodeType.DirectoryWildcard)
{
}

public override string ToString()
{
public override string ToString()
{
return "**";
}
}
}
}
13 changes: 6 additions & 7 deletions src/Glob/AST/GlobNode.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace GlobExpressions.AST
namespace GlobExpressions.AST;

internal abstract class GlobNode
{
internal abstract class GlobNode
protected GlobNode(GlobNodeType type)
{
protected GlobNode(GlobNodeType type)
{
this.Type = type;
}

public GlobNodeType Type { get; }
}
}
public GlobNodeType Type { get; }
}
29 changes: 14 additions & 15 deletions src/Glob/AST/GlobNodeType.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
namespace GlobExpressions.AST
namespace GlobExpressions.AST;

internal enum GlobNodeType
{
internal enum GlobNodeType
{
Tree,
Tree,

// Segments
Root,
// Segments
Root,

DirectoryWildcard,
DirectorySegment,
DirectoryWildcard,
DirectorySegment,

// SubSegments
CharacterSet,
// SubSegments
CharacterSet,

Identifier,
LiteralSet,
StringWildcard,
CharacterWildcard
}
Identifier,
LiteralSet,
StringWildcard,
CharacterWildcard
}
29 changes: 14 additions & 15 deletions src/Glob/AST/Identifier.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
namespace GlobExpressions.AST
namespace GlobExpressions.AST;

internal sealed class Identifier : SubSegment
{
internal sealed class Identifier : SubSegment
{
public string Value { get; }
public string Value { get; }

public Identifier(string value)
: base(GlobNodeType.Identifier)
{
Value = value;
}
public Identifier(string value)
: base(GlobNodeType.Identifier)
{
Value = value;
}

public override string ToString() => Value;
public override string ToString() => Value;

public static implicit operator Identifier(string value)
{
return new Identifier(value);
}
public static implicit operator Identifier(string value)
{
return new Identifier(value);
}
}
}
25 changes: 12 additions & 13 deletions src/Glob/AST/LiteralSet.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
using System.Collections.Generic;
using System.Linq;

namespace GlobExpressions.AST
namespace GlobExpressions.AST;

internal sealed class LiteralSet : SubSegment
{
internal sealed class LiteralSet : SubSegment
{
public Identifier[] Literals { get; }
public Identifier[] Literals { get; }

public LiteralSet(params Identifier[] literals)
: base(GlobNodeType.LiteralSet)
{
public LiteralSet(params Identifier[] literals)
: base(GlobNodeType.LiteralSet)
{
Literals = literals.ToArray();
}

public LiteralSet(IEnumerable<Identifier> literals)
: base(GlobNodeType.LiteralSet)
{
public LiteralSet(IEnumerable<Identifier> literals)
: base(GlobNodeType.LiteralSet)
{
Literals = literals.ToArray();
}

public override string ToString() => $"{{{string.Join(",", Literals.Select(lit => lit.ToString()))}}}";
}
}
public override string ToString() => $"{{{string.Join(",", Literals.Select(lit => lit.ToString()))}}}";
}
19 changes: 9 additions & 10 deletions src/Glob/AST/Root.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
namespace GlobExpressions.AST
namespace GlobExpressions.AST;

internal sealed class Root : Segment
{
internal sealed class Root : Segment
{
public string Text { get; }
public string Text { get; }

public Root(string text = "")
: base(GlobNodeType.Root)
{
public Root(string text = "")
: base(GlobNodeType.Root)
{
Text = text;
}

public override string ToString() => Text;
}
}
public override string ToString() => Text;
}
13 changes: 6 additions & 7 deletions src/Glob/AST/Segment.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
namespace GlobExpressions.AST
namespace GlobExpressions.AST;

internal abstract class Segment : GlobNode
{
internal abstract class Segment : GlobNode
protected Segment(GlobNodeType type)
: base(type)
{
protected Segment(GlobNodeType type)
: base(type)
{
}
}
}
}
Loading

0 comments on commit 89420df

Please sign in to comment.