Shows how importing a comment token definition can prevent find-and-replace rules from incorrectly modifying text inside comments.

ID: 1004

See: Add(Item)
				
					using uCalcSoftware;

var uc = new uCalc();
// Create a transformer with a custom comment token definition
var t_source = new uCalc.Transformer();
var commentToken = t_source.Tokens.Add("""
/\*([\s\S]*?)\*/
""", TokenType.Whitespace);
commentToken.Description = "C-style block comment";

// Create a second transformer that will do replacements
var t_replacer = new uCalc.Transformer();
t_replacer.FromTo("secret", "REDACTED");

var sourceText = "a secret /* contains another secret */ value";

Console.WriteLine("--- Before Importing Comment Token ---");
// Without knowing about comments, t_replacer incorrectly modifies the text inside the comment.
Console.WriteLine(t_replacer.Transform(sourceText));
Console.WriteLine("");

Console.WriteLine("--- After Importing Comment Token ---");
// Import the comment token definition. Now, the comment block is treated as a single whitespace token.
t_replacer.Tokens.Add(commentToken);
// The transformer must be reset with the original text for the new token to apply.
t_replacer.Text = sourceText;
Console.WriteLine(t_replacer.Transform());
				
			
--- Before Importing Comment Token ---
a REDACTED /* contains another REDACTED */ value

--- After Importing Comment Token ---
a REDACTED /* contains another secret */ value
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Create a transformer with a custom comment token definition
   uCalc::Transformer t_source;
   auto commentToken = t_source.Tokens().Add(R"(/\*([\s\S]*?)\*/)", TokenType::Whitespace);
   commentToken.Description("C-style block comment");

   // Create a second transformer that will do replacements
   uCalc::Transformer t_replacer;
   t_replacer.FromTo("secret", "REDACTED");

   auto sourceText = "a secret /* contains another secret */ value";

   cout << "--- Before Importing Comment Token ---" << endl;
   // Without knowing about comments, t_replacer incorrectly modifies the text inside the comment.
   cout << t_replacer.Transform(sourceText) << endl;
   cout << "" << endl;

   cout << "--- After Importing Comment Token ---" << endl;
   // Import the comment token definition. Now, the comment block is treated as a single whitespace token.
   t_replacer.Tokens().Add(commentToken);
   // The transformer must be reset with the original text for the new token to apply.
   t_replacer.Text(sourceText);
   cout << t_replacer.Transform() << endl;
}
				
			
--- Before Importing Comment Token ---
a REDACTED /* contains another REDACTED */ value

--- After Importing Comment Token ---
a REDACTED /* contains another secret */ value
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Create a transformer with a custom comment token definition
      Dim t_source As New uCalc.Transformer()
      Dim commentToken = t_source.Tokens.Add("/\*([\s\S]*?)\*/", TokenType.Whitespace)
      commentToken.Description = "C-style block comment"
      
      '// Create a second transformer that will do replacements
      Dim t_replacer As New uCalc.Transformer()
      t_replacer.FromTo("secret", "REDACTED")
      
      Dim sourceText = "a secret /* contains another secret */ value"
      
      Console.WriteLine("--- Before Importing Comment Token ---")
      '// Without knowing about comments, t_replacer incorrectly modifies the text inside the comment.
      Console.WriteLine(t_replacer.Transform(sourceText))
      Console.WriteLine("")
      
      Console.WriteLine("--- After Importing Comment Token ---")
      '// Import the comment token definition. Now, the comment block is treated as a single whitespace token.
      t_replacer.Tokens.Add(commentToken)
      '// The transformer must be reset with the original text for the new token to apply.
      t_replacer.Text = sourceText
      Console.WriteLine(t_replacer.Transform())
   End Sub
End Module
				
			
--- Before Importing Comment Token ---
a REDACTED /* contains another REDACTED */ value

--- After Importing Comment Token ---
a REDACTED /* contains another secret */ value