Skip to content

Latest commit

 

History

History

Gapotchenko.FX.Text

Gapotchenko.FX.Text

License NuGet

The module provides primitives for string and text manipulation.

StringEditor

StringEditor class provided by Gapotchenko.FX.Text module allows to perform an iterative random-access editing of a string.

It is primarily designed to work in conjunction with Regex class from System.Text.RegularExpressions namespace to efficiently handle an advanced set of tasks when functionality provided by conventional methods like Regex.Replace is just not enough.

More on StringEditor class

Regular Expression Trampolines

Regex trampolines are special functions that allow to gradually convert the conventional string manipulation code into one that uses regex semantics.

Let's take a look on example:

if (name.Equals("[mscorlib]System.Object", StringComparison.Ordinal))
    Console.WriteLine("The name represents a system object.");

The given code is later changed in order to cover the new requirements:

if (name.Equals("[mscorlib]System.Object", StringComparison.Ordinal) ||
    name.Equals("[netstandard]System.Object", StringComparison.Ordinal) ||
    name.Equals("[System.Runtime]System.Object", StringComparison.Ordinal))
{
    Console.WriteLine("The name represents a system object.");
}

It does the job but such mechanical changes may put a toll on code maintainability when they accumulate. You can also spot some amount of code duplication here.

Another approach would be to use Regex class which is readily available in .NET. But that might destroy the expressiveness of string manipulation functions like Equals. If a string function takes a StringComparison parameter then it may become a significant challenge to reliably refactor it to Regex implementation.

That's why Gapotchenko.FX.Text module provides a set of so called regex trampoline functions. They look exactly like Equals, StartsWith, EndsWith, IndexOf but work with regex patterns instead of raw strings. They also end with Regex suffix in their names, so Equals becomes EqualsRegex, StartsWith correspondingly becomes StartsWithRegex, and so on.

And this is how a regex trampoline can be used for the given sample in order to meet the new requirements by a single line change:

using Gapotchenko.FX.Text.RegularExpressions;

if (name.EqualsRegex(@"\[(mscorlib|netstandard|System\.Runtime)]System\.Object", StringComparison.Ordinal))
    Console.WriteLine("The name represents a system object.");

An immediate improvement in expressiveness without duplication.

StringBuilder Polyfills

AppendJoin

StringBuilder.AppendJoin is a method that appeared in later versions of .NET platform. Gapochenko.FX provides a corresponding polyfill that can be used in code targeting older .NET versions.

The benefit of this method is that it combines string.Join and StringBuilder.Append operations into one method using the underlying efficiency of StringBuilder.

Usage

Gapotchenko.FX.Text module is available as a NuGet package:

PM> Install-Package Gapotchenko.FX.Text

Other Modules

Let's continue with a look at some other modules provided by Gapotchenko.FX:

Or look at the full list of modules.