Skip to content

Commit

Permalink
Extract RemoveWS
Browse files Browse the repository at this point in the history
  • Loading branch information
blowfishpro committed Sep 19, 2017
1 parent 6b817fc commit 180c2ee
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
11 changes: 8 additions & 3 deletions ModuleManager/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ModuleManager.Extensions
{
Expand All @@ -19,5 +17,12 @@ public static bool IsBracketBalanced(this string s)
}
return level == 0;
}

private static Regex whitespaceRegex = new Regex(@"\s+");

public static string RemoveWS(this string withWhite)
{
return whitespaceRegex.Replace(withWhite, "");
}
}
}
24 changes: 7 additions & 17 deletions ModuleManager/MMPatchLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private void PrePatchInit()
progress.PatchAdded();
if (name.Contains(":FOR["))
{
name = RemoveWS(name);
name = name.RemoveWS();

// check for FOR[] blocks that don't match loaded DLLs and add them to the pass list
try
Expand All @@ -216,7 +216,7 @@ private void PrePatchInit()
foreach (string subdir in Directory.GetDirectories(gameData))
{
string name = Path.GetFileName(subdir);
string cleanName = RemoveWS(name);
string cleanName = name.RemoveWS();
if (!mods.Contains(cleanName, StringComparer.OrdinalIgnoreCase))
{
mods.Add(cleanName);
Expand Down Expand Up @@ -947,7 +947,7 @@ private void PurgeUnused()
{
foreach (UrlDir.UrlConfig mod in GameDatabase.Instance.root.AllConfigs.ToArray())
{
string name = RemoveWS(mod.type);
string name = mod.type.RemoveWS();

if (CommandParser.Parse(name, out name) != Command.Insert)
mod.parent.configs.Remove(mod);
Expand Down Expand Up @@ -975,7 +975,7 @@ public IEnumerator ApplyPatch(string Stage, IEnumerable<UrlDir.UrlConfig> patche
{
try
{
string name = RemoveWS(mod.type);
string name = mod.type.RemoveWS();
Command cmd = CommandParser.Parse(name, out string tmp);

if (cmd == Command.Insert)
Expand Down Expand Up @@ -1386,7 +1386,7 @@ public static ConfigNode ModifyNode(NodeStack original, ConfigNode mod, PatchCon

foreach (ConfigNode subMod in mod.nodes)
{
subMod.name = RemoveWS(subMod.name);
subMod.name = subMod.name.RemoveWS();

if (!subMod.name.IsBracketBalanced())
{
Expand Down Expand Up @@ -2041,22 +2041,12 @@ private static string ProcessVariableSearch(string value, NodeStack nodeStack, P

#endregion Applying Patches

#region Sanity checking & Utility functions

public static string RemoveWS(string withWhite)
{
// Removes ALL whitespace of a string.
return new string(withWhite.ToCharArray().Where(c => !Char.IsWhiteSpace(c)).ToArray());
}

#endregion Sanity checking & Utility functions

#region Condition checking

// Split condiction while not getting lost in embeded brackets
public static List<string> SplitConstraints(string condition)
{
condition = RemoveWS(condition) + ",";
condition = condition.RemoveWS() + ",";
List<string> conditions = new List<string>();
int start = 0;
int level = 0;
Expand All @@ -2079,7 +2069,7 @@ public static List<string> SplitConstraints(string condition)

public static bool CheckConstraints(ConfigNode node, string constraints)
{
constraints = RemoveWS(constraints);
constraints = constraints.RemoveWS();

if (constraints.Length == 0)
return true;
Expand Down
7 changes: 7 additions & 0 deletions ModuleManagerTests/Extensions/StringExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ public void TestIsBracketBalanced__Unbalanced()
Assert.False("abc[def[ghi[jkl]mno[pqr]]stuvwx".IsBracketBalanced());
Assert.False("abcdef[ghi[jkl]mno[pqr]]stu]vwx".IsBracketBalanced());
}

[Fact]
public void TestIsBracketBalanced__BalancedButNegative()
{
Assert.False("abc]def[ghi".IsBracketBalanced());
}

[Fact]
public void TestRemoveWS()
{
Assert.Equal("abcdef", " abc \tdef\r\n\t ".RemoveWS());
}
}
}

0 comments on commit 180c2ee

Please sign in to comment.