diff --git a/Core/Text/Utils/StringFinder.cs b/Core/Text/Utils/StringFinder.cs index 37dffce..cde3dbf 100644 --- a/Core/Text/Utils/StringFinder.cs +++ b/Core/Text/Utils/StringFinder.cs @@ -50,6 +50,15 @@ public StringFinder(string s) Value = Previous = string.Empty; } + /// + /// Initializes a new instance of the StringFinder class. + /// + /// The source string. + public StringFinder(StringBuilder s) + : this(s?.ToString()) + { + } + /// /// Gets the source string. /// @@ -85,6 +94,34 @@ public StringFinder(string s) /// public int RestLength => Rest.Length; + /// + /// Gets the sub-string before a search string. + /// + /// The search string. + /// The zero-based offset. + /// The sub-string. + public string PreviewBefore(string q, out int offset) + { + if (string.IsNullOrEmpty(q)) + { + offset = Offset; + return string.Empty; + } + + var s = Rest; + var index = s.IndexOf(q); + if (index < 0) + { + Value = string.Empty; + offset = Source.Length; + return string.Empty; + } + + var value = Rest.Substring(0, index); + offset = Offset + index; + return value; + } + /// /// Gets the sub-string before a search string. The search string is in the rest string. /// diff --git a/UnitTest/Text/StringUnitTest.cs b/UnitTest/Text/StringUnitTest.cs index 64fd43a..c79b6e1 100644 --- a/UnitTest/Text/StringUnitTest.cs +++ b/UnitTest/Text/StringUnitTest.cs @@ -47,6 +47,7 @@ public void TestStringFinder() "." }); Assert.IsFalse(finder.IsEnd); + Assert.AreEqual(TestString.Length - 1, finder.Offset); Assert.AreEqual("\"", finder.Before(1)); Assert.AreEqual("\"", finder.Value); Assert.IsTrue(finder.IsEnd);