public void Append_Simple_OtherLanguages() { var fr = new FastReplacer("/*", "*/"); fr.Append("abc"); fr.Append(@" čćšđž;'[]\"); Assert.AreEqual(@"abc čćšđž;'[]\", fr.ToString()); }
public void Append_Simple() { var fr = new FastReplacer("/*", "*/"); fr.Append("a"); fr.Append("b"); fr.Append("c"); Assert.AreEqual("abc", fr.ToString()); }
public void Combination1() { var fr = new FastReplacer("/*", "*/"); fr.Append("1"); fr.Append("2/*a*/3"); fr.Append("4"); Assert.AreEqual("12/*a*/34", fr.ToString()); fr.InsertBefore("/*a*/", "x/*a*/x"); Assert.AreEqual("12x/*a*/x/*a*/34", fr.ToString()); fr.InsertBefore("/*a*/", "-"); Assert.AreEqual("12x-/*a*/x-/*a*/34", fr.ToString()); }
private static void FailUsingToken(string token) { ShouldFail(delegate { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*/"); fr.Contains(token); }, "Expected exception was not thrown while using token \"" + token + "\" in function Contains."); ShouldFail(delegate { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*/"); fr.Replace(token, "x"); }, "Expected exception was not thrown while using token \"" + token + "\" in function Replace."); ShouldFail(delegate { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*/"); fr.InsertBefore(token, "x"); }, "Expected exception was not thrown while using token \"" + token + "\" in function InsertBefore."); ShouldFail(delegate { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*/"); fr.InsertAfter(token, "x"); }, "Expected exception was not thrown while using token \"" + token + "\" in function InsertAfter."); }
private string AddFrontmatter(BlogMLPost post, string markdown) { var frontMatter = Settings.Instance.FrontMatter; if (string.IsNullOrWhiteSpace(frontMatter)) { return(markdown); } var fr = new FastReplacer("<$", "$>"); var title = post.Title.ToHtmlDecodedString().Replace("\"", ""); fr.Append(frontMatter); fr.Replace("<$GWB_TITLE$>", $"\"{title}\""); var description = post.HasExcerpt ? post.Excerpt.Text.ToHtmlDecodedString() : title; fr.Replace("<$GWB_DESCRIPTION$>", $"\"{description}\""); fr.Replace("<$GWB_AUTHOR$>", "Utkarsh Shigihalli"); var categories = post.Categories.Cast <BlogMLCategoryReference>().Select(x => $"\"{x.Ref}\""); fr.Replace("<$GWB_CATEGORIES$>", $"{string.Join($"{Environment.NewLine}- ", categories)}"); fr.Replace("<$GWB_DATE$>", post.DateCreated.ToString("yyyy-MM-dd HH:mm:ss")); return($"{fr}{Environment.NewLine}{markdown}"); }
private static void FailAppend(string text) { ShouldFail(delegate { var fr = new FastReplacer("/*", "*/"); fr.Append(text); }, "Expected exception was not thrown while appending \"" + text + "\"."); }
public void PossiblyUnintuitiveBehaviour_NotAComment() { // Behavior is different from standard C# or SQL comments: // Tokens are recognized even if they are inside a string (token is not always a comment). var fr = new FastReplacer("/*", "*/"); fr.Append("PRINT 'Token starts with /* sequence'"); }
public void Replace_Simple() { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*/"); fr.Replace("/*a*/", "123"); Assert.AreEqual("123", fr.ToString()); }
public void CaseInsensitive_OtherLanguages() { var fr = new FastReplacer("{", "}", false); fr.Append("{č}"); Assert.IsTrue(fr.Contains("{č}")); Assert.IsTrue(fr.Contains("{Č}")); Assert.IsTrue(fr.Replace("{Č}", "x")); Assert.AreEqual("x", fr.ToString()); }
public void Replace_RemoveWhenReplaced() { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*/"); Assert.AreEqual(true, fr.Replace("/*a*/", "123")); Assert.AreEqual(false, fr.Replace("/*a*/", "456")); Assert.AreEqual("123", fr.ToString()); }
public void CaseSensitive() { var fr = new FastReplacer("{", "}"); fr.Append("{a}"); Assert.IsTrue(fr.Contains("{a}")); Assert.IsFalse(fr.Contains("{A}")); Assert.IsFalse(fr.Replace("{A}", "x")); Assert.AreEqual("{a}", fr.ToString()); }
public void InsertAfterReverse_Simple() { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*/"); fr.InsertAfter("/*a*/", "1"); fr.InsertAfter("/*a*/", "2"); fr.InsertAfter("/*a*/", "3", reverse: true); fr.InsertAfter("/*a*/", "4", reverse: true); Assert.AreEqual("/*a*/4312", fr.ToString()); }
public void InsertAfterReverse_ComplexAndOrder() { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*//*b*/"); fr.InsertAfter("/*b*/", "1"); fr.InsertAfter("/*a*/", "2", true); fr.InsertAfter("/*b*/", "3"); fr.InsertAfter("/*a*/", "4", true); Assert.AreEqual("/*a*/42/*b*/13", fr.ToString()); }
public void Combination_OrderAdvanced() { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*/"); fr.InsertAfter("/*a*/", "1"); fr.InsertAfter("/*a*/", "2"); fr.Replace("/*a*/", "/*a*/"); fr.InsertAfter("/*a*/", "3"); Assert.AreEqual("/*a*/312", fr.ToString()); }
public void InsertBefore_ComplexAndOrder() { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*//*b*/"); fr.InsertBefore("/*b*/", "1"); fr.InsertBefore("/*a*/", "2"); fr.InsertBefore("/*b*/", "3"); fr.InsertBefore("/*a*/", "4"); Assert.AreEqual("24/*a*/13/*b*/", fr.ToString()); }
private static string DoReplacingWithFastReplacer(string initial, int replaceCount) { var fr = new FastReplacer("/*", "*/"); fr.Append(initial); for (int i = 1; i <= replaceCount; i++) { string token = "/*" + i + "*/"; string newText = "(/*" + (2 * i) + "*//*" + (2 * i + 1) + "*/)"; fr.Replace(token, newText); } return(fr.ToString()); }
public void Combination_TokenReuse() { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*/"); fr.Replace("/*a*/", "/*a*/"); Assert.AreEqual("/*a*/", fr.ToString()); fr.InsertAfter("/*a*/", "/*a*/"); Assert.AreEqual("/*a*//*a*/", fr.ToString()); fr.InsertBefore("/*a*/", "/*a*/"); Assert.AreEqual("/*a*//*a*//*a*//*a*/", fr.ToString()); }
public void Combination2() { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*/"); fr.InsertAfter("/*a*/", "/*c*/"); fr.Replace("/*a*/", "/*b*/"); fr.InsertAfter("/*b*/", "/*d*/"); fr.Replace("/*a*/", "a"); fr.Replace("/*b*/", "b"); fr.Replace("/*c*/", "c"); fr.Replace("/*d*/", "d"); Assert.AreEqual("bdc", fr.ToString()); }
/// <summary> /// Replace the variables <XXX> in the string /// </summary> private string ReplaceVariablesIn(string input) { if (input.ContainsFast("<")) { var fr = new FastReplacer("<", ">", false); fr.Append(input.Replace("<<", $"{(char) 2}").Replace(">>", $"{(char) 3}")); // special replacement fr.Replace(@"<ROOT>", input.StartsWith(":") ? Regex.Escape(_sourceDirectory) : _sourceDirectory); foreach (var variableRule in DeployVarList) { fr.Replace(variableRule.VariableName, variableRule.Path); } return(fr.ToString().Replace($"{(char) 2}", "<").Replace($"{(char) 3}", ">")); } return(input); }
public void PossiblyUnintuitiveBehaviour_IgnoreTokenIfNotFromSingleText() { // Behavior is different from standard String.Replace function: // Token is ignored if it is composed by concatenation of two different strings. var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*//*b*/"); fr.Replace("/*a*/", "/"); fr.Replace("/*b*/", "*c*/"); Assert.AreEqual("/*c*/", fr.ToString()); Assert.IsFalse(fr.Contains("/*c*/")); bool replaced = fr.Replace("/*c*/", "-"); Assert.IsFalse(replaced); Assert.AreEqual("/*c*/", fr.ToString()); }
public void Combination_Order() { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*//*b*/"); fr.InsertBefore("/*b*/", "1"); fr.InsertBefore("/*b*/", "2"); fr.InsertAfter("/*a*/", "3"); fr.InsertAfter("/*a*/", "4"); fr.InsertBefore("/*b*/", "5"); fr.InsertAfter("/*a*/", "6"); Assert.AreEqual("/*a*/346125/*b*/", fr.ToString()); fr.Replace("/*a*/", "7"); fr.Replace("/*b*/", "8"); Assert.AreEqual("73461258", fr.ToString()); }
public void Contains_Test() { string a = "/*a*/"; string b = "/*b*/"; string c = "/*c*/"; var fr = new FastReplacer("/*", "*/"); Assert.AreEqual(false, fr.Contains(a)); fr.Append(a); Assert.AreEqual(true, fr.Contains(a)); fr.Replace(a, b); Assert.AreEqual(false, fr.Contains(a)); Assert.AreEqual(true, fr.Contains(b)); fr.Replace(b, b); Assert.AreEqual(true, fr.Contains(b)); fr.Replace(b, c); Assert.AreEqual(false, fr.Contains(b)); Assert.AreEqual(c, fr.ToString()); }
public void ReturnValue() { var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*/"); Assert.AreEqual(true, fr.InsertAfter("/*a*/", "1")); Assert.AreEqual(true, fr.InsertBefore("/*a*/", "2")); Assert.AreEqual(true, fr.Replace("/*a*/", "/*b*/")); Assert.AreEqual(false, fr.InsertAfter("/*a*/", "1")); Assert.AreEqual(false, fr.InsertBefore("/*a*/", "2")); Assert.AreEqual(false, fr.Replace("/*a*/", "/*c*/")); Assert.AreEqual("2/*b*/1", fr.ToString()); Assert.AreEqual(true, fr.Replace("/*b*/", "/*a*/")); Assert.AreEqual(false, fr.Replace("/*x*/", "x1")); Assert.AreEqual(false, fr.Replace("/*b*/", "x4")); Assert.AreEqual(false, fr.Replace("/*c*/", "x5")); Assert.AreEqual(true, fr.Replace("/*a*/", "x6")); Assert.AreEqual("2x61", fr.ToString()); }
public void IgnoreTokenMarksInMultilineText() { var fr = new FastReplacer("/*", "*/"); fr.Append(@" /*a */ /* /*b*/ */"); Assert.IsTrue(fr.Contains("/*b*/")); Assert.IsFalse(fr.Contains("/*a*/")); fr.Replace("/*b*/", "123"); Assert.AreEqual(@" /*a */ /* 123 */", fr.ToString()); }
public void InsertCode(string code) { _code.Append(code); }
public void PossiblyUnintuitiveBehaviour_IgnoreTokenIfNotFromSingleText() { // Behaviour is different from standard String.Replace function: // Token is ignored if it is composed by concatenation of two different strings. var fr = new FastReplacer("/*", "*/"); fr.Append("/*a*//*b*/"); fr.Replace("/*a*/", "/"); fr.Replace("/*b*/", "*c*/"); Assert.AreEqual("/*c*/", fr.ToString()); Assert.IsFalse(fr.Contains("/*c*/")); bool replaced = fr.Replace("/*c*/", "-"); Assert.IsFalse(replaced); Assert.AreEqual("/*c*/", fr.ToString()); }
public void InsertCode(string code) { _code.Append(code + Environment.NewLine); }
private static string DoReplacingWithFastReplacer(string initial, int replaceCount) { var fr = new FastReplacer("/*", "*/"); fr.Append(initial); for (int i = 1; i <= replaceCount; i++) { string token = "/*" + i + "*/"; string newText = "(/*" + (2 * i) + "*//*" + (2 * i + 1) + "*/)"; fr.Replace(token, newText); } return fr.ToString(); }
public void PossiblyUnintuitiveBehaviour_NotAComment() { // Behaviour is different from standard C# or SQL comments: // Tokens are recognized even if they are inside a string (token is not always a comment). var fr = new FastReplacer("/*", "*/"); fr.Append("PRINT 'Token starts with /* sequence'"); }