uCalc API Version: 2.1.3-preview.2 Released: 6/16/2026
Warning
uCalc API Preview Release Notice:The documentation describes the intended behavior of the API. The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.
Clone
Method
Product:
Class:
Creates an identical, yet completely independent, copy of the Transformer object, including all its rules and settings.
Syntax
Parameters
Return
Transformer
A new, independent Transformer instance that is a complete replica of the original.
Remarks
The Clone method creates a deep, independent copy of a Transformer object. This is a highly efficient operation, as it duplicates the transformer's internal state directly in memory, which is significantly faster than creating a new transformer and redefining all its rules and tokens from scratch.
What is Cloned?
A clone is a complete replica of the original Transformer's state at the moment of cloning, including:
- All Rules: Every
FromToandPatternrule is copied. - Tokens: The entire collection of token definitions is duplicated.
- Text: The current source string held by the transformer is copied.
- Settings: All default rule settings (
CaseSensitive,WhitespaceSensitive, etc.) are preserved.
Once created, the clone is completely independent; modifying the clone will not affect the original, and vice-versa.
🎯 Primary Use Cases
Performance Optimization: Parsing a complex set of rules and token definitions can be computationally expensive. If you need to use the same configuration repeatedly, you should configure it once, and then
ClonetheTransformerobject as needed. This avoids the high cost of repeated setup.Templating:
Cloneis perfect for creating a "template" pattern. You can create a baseTransformerwith a standard set of rules (e.g., for parsing a specific language or format). Then, for different tasks, you canClonethis base and add a few specialized rules to the clone, promoting modularity and reuse.Isolation & Sandboxing: Create a clone to experiment with temporary rules or transformations without altering the state of the original, master
Transformer.
Transformer.Clone() vs. uCalc.Clone()
It is crucial to understand the difference between cloning a Transformer and cloning the entire uCalc engine:
| Method | What It Copies | Parent uCalc Instance |
|---|---|---|
Transformer.Clone() (This Method) | A single Transformer object. | The clone belongs to the same parent uCalc instance as the original. |
uCalc.Clone() | The entire uCalc engine, including all variables, functions, and every Transformer within it. | The clone creates a new, isolated parent uCalc instance. |
Memory Management
A cloned Transformer object holds resources and must be released when no longer needed to prevent memory leaks. This can be done explicitly with Release() or automatically using language-specific scoping constructs like using in C# or Owned() in C++.
Examples
A succinct example demonstrating that a clone is independent and that modifying it does not affect the original.
using uCalcSoftware;
var uc = new uCalc();
// 1. Create and configure the original transformer
var t1 = new uCalc.Transformer();
t1.FromTo("A", "B");
Console.WriteLine($"Original Transform: {t1.Transform("A C A")}");
// 2. Clone it
var t2 = t1.Clone();
// 3. Modify the clone. This does not affect the original.
t2.FromTo("C", "D");
Console.WriteLine($"Cloned Transform: {t2.Transform("A C A")}");
// 4. Verify original is unchanged by re-running its transform
Console.WriteLine($"Original is Unchanged: {t1.Transform("A C A")}");
t2.Release();
t1.Release();
Original Transform: B C B
Cloned Transform: B D B
Original is Unchanged: B C B using uCalcSoftware; var uc = new uCalc(); // 1. Create and configure the original transformer var t1 = new uCalc.Transformer(); t1.FromTo("A", "B"); Console.WriteLine($"Original Transform: {t1.Transform("A C A")}"); // 2. Clone it var t2 = t1.Clone(); // 3. Modify the clone. This does not affect the original. t2.FromTo("C", "D"); Console.WriteLine($"Cloned Transform: {t2.Transform("A C A")}"); // 4. Verify original is unchanged by re-running its transform Console.WriteLine($"Original is Unchanged: {t1.Transform("A C A")}"); t2.Release(); t1.Release();
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// 1. Create and configure the original transformer
uCalc::Transformer t1;
t1.FromTo("A", "B");
cout << "Original Transform: " << t1.Transform("A C A") << endl;
// 2. Clone it
auto t2 = t1.Clone();
// 3. Modify the clone. This does not affect the original.
t2.FromTo("C", "D");
cout << "Cloned Transform: " << t2.Transform("A C A") << endl;
// 4. Verify original is unchanged by re-running its transform
cout << "Original is Unchanged: " << t1.Transform("A C A") << endl;
t2.Release();
t1.Release();
}
Original Transform: B C B
Cloned Transform: B D B
Original is Unchanged: B C B #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // 1. Create and configure the original transformer uCalc::Transformer t1; t1.FromTo("A", "B"); cout << "Original Transform: " << t1.Transform("A C A") << endl; // 2. Clone it auto t2 = t1.Clone(); // 3. Modify the clone. This does not affect the original. t2.FromTo("C", "D"); cout << "Cloned Transform: " << t2.Transform("A C A") << endl; // 4. Verify original is unchanged by re-running its transform cout << "Original is Unchanged: " << t1.Transform("A C A") << endl; t2.Release(); t1.Release(); }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// 1. Create and configure the original transformer
Dim t1 As New uCalc.Transformer()
t1.FromTo("A", "B")
Console.WriteLine($"Original Transform: {t1.Transform("A C A")}")
'// 2. Clone it
Dim t2 = t1.Clone()
'// 3. Modify the clone. This does not affect the original.
t2.FromTo("C", "D")
Console.WriteLine($"Cloned Transform: {t2.Transform("A C A")}")
'// 4. Verify original is unchanged by re-running its transform
Console.WriteLine($"Original is Unchanged: {t1.Transform("A C A")}")
t2.Release()
t1.Release()
End Sub
End Module
Original Transform: B C B
Cloned Transform: B D B
Original is Unchanged: B C B Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// 1. Create and configure the original transformer Dim t1 As New uCalc.Transformer() t1.FromTo("A", "B") Console.WriteLine($"Original Transform: {t1.Transform("A C A")}") '// 2. Clone it Dim t2 = t1.Clone() '// 3. Modify the clone. This does not affect the original. t2.FromTo("C", "D") Console.WriteLine($"Cloned Transform: {t2.Transform("A C A")}") '// 4. Verify original is unchanged by re-running its transform Console.WriteLine($"Original is Unchanged: {t1.Transform("A C A")}") t2.Release() t1.Release() End Sub End Module
A practical example using Clone() to create a specialized parser from a base template, demonstrating rule inheritance.
using uCalcSoftware;
var uc = new uCalc();
// 1. Create a "base" HTML transformer template
var baseHtmlParser = new uCalc.Transformer();
baseHtmlParser.Description = "Base HTML Parser";
// Rule to skip over comments
baseHtmlParser.SkipOver("