A practical, real-world parser that handles multiple key-value pairs and URL-encoded characters using a custom callback.
ID: 1399
using uCalcSoftware;
var uc = new uCalc();
static void URLDecode(uCalc.Callback cb) {
// In a real application, this would be a full URL decoding implementation.
// For this example, we'll just handle spaces (%20) and plus signs (+).
uCalc.String s = cb.ArgStr(1);
s.Replace("%20", " ").Replace("+", " ");
cb.ReturnStr(s.Text);
}
// 1. Define the custom URLDecode function in the uCalc engine
uc.DefineFunction("URLDecode(s As String) As String", URLDecode);
// 2. Create the transformer and configure its tokenizer
using (var t = new uCalc.Transformer(uc)) {
// Treat '&' as a statement separator to process each pair individually
t.Tokens.Add("&", TokenType.StatementSep);
// 3. Define the rule to capture key-value pairs and decode the value
t.FromTo("{@Alphanumeric:key}={value}", "- {key}: '{@Eval: URLDecode(value)}'");
// 4. Process a real-world query string
var queryString = "name=John%20Doe&role=user+admin&id=123";
// Use Filter() to get a clean, newline-separated list of the results
Console.WriteLine(t.Transform(queryString).Matches);
}
- name: 'John Doe'
- role: 'user admin'
- id: '123' using uCalcSoftware; var uc = new uCalc(); static void URLDecode(uCalc.Callback cb) { // In a real application, this would be a full URL decoding implementation. // For this example, we'll just handle spaces (%20) and plus signs (+). uCalc.String s = cb.ArgStr(1); s.Replace("%20", " ").Replace("+", " "); cb.ReturnStr(s.Text); } // 1. Define the custom URLDecode function in the uCalc engine uc.DefineFunction("URLDecode(s As String) As String", URLDecode); // 2. Create the transformer and configure its tokenizer using (var t = new uCalc.Transformer(uc)) { // Treat '&' as a statement separator to process each pair individually t.Tokens.Add("&", TokenType.StatementSep); // 3. Define the rule to capture key-value pairs and decode the value t.FromTo("{@Alphanumeric:key}={value}", "- {key}: '{@Eval: URLDecode(value)}'"); // 4. Process a real-world query string var queryString = "name=John%20Doe&role=user+admin&id=123"; // Use Filter() to get a clean, newline-separated list of the results Console.WriteLine(t.Transform(queryString).Matches); }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call URLDecode(uCalcBase::Callback cb) {
// In a real application, this would be a full URL decoding implementation.
// For this example, we'll just handle spaces (%20) and plus signs (+).
uCalc::String s = cb.ArgStr(1);
s.Replace("%20", " ").Replace("+", " ");
cb.ReturnStr(s.Text());
}
int main() {
uCalc uc;
// 1. Define the custom URLDecode function in the uCalc engine
uc.DefineFunction("URLDecode(s As String) As String", URLDecode);
// 2. Create the transformer and configure its tokenizer
{
uCalc::Transformer t(uc);
t.Owned(); // Causes t to be released when it goes out of scope
// Treat '&' as a statement separator to process each pair individually
t.Tokens().Add("&", TokenType::StatementSep);
// 3. Define the rule to capture key-value pairs and decode the value
t.FromTo("{@Alphanumeric:key}={value}", "- {key}: '{@Eval: URLDecode(value)}'");
// 4. Process a real-world query string
auto queryString = "name=John%20Doe&role=user+admin&id=123";
// Use Filter() to get a clean, newline-separated list of the results
cout << t.Transform(queryString).Matches() << endl;
}
}
- name: 'John Doe'
- role: 'user admin'
- id: '123' #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call URLDecode(uCalcBase::Callback cb) { // In a real application, this would be a full URL decoding implementation. // For this example, we'll just handle spaces (%20) and plus signs (+). uCalc::String s = cb.ArgStr(1); s.Replace("%20", " ").Replace("+", " "); cb.ReturnStr(s.Text()); } int main() { uCalc uc; // 1. Define the custom URLDecode function in the uCalc engine uc.DefineFunction("URLDecode(s As String) As String", URLDecode); // 2. Create the transformer and configure its tokenizer { uCalc::Transformer t(uc); t.Owned(); // Causes t to be released when it goes out of scope // Treat '&' as a statement separator to process each pair individually t.Tokens().Add("&", TokenType::StatementSep); // 3. Define the rule to capture key-value pairs and decode the value t.FromTo("{@Alphanumeric:key}={value}", "- {key}: '{@Eval: URLDecode(value)}'"); // 4. Process a real-world query string auto queryString = "name=John%20Doe&role=user+admin&id=123"; // Use Filter() to get a clean, newline-separated list of the results cout << t.Transform(queryString).Matches() << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub URLDecode(ByVal cb As uCalc.Callback)
'// In a real application, this would be a full URL decoding implementation.
'// For this example, we'll just handle spaces (%20) and plus signs (+).
Dim s As uCalc.String = cb.ArgStr(1)
s.Replace("%20", " ").Replace("+", " ")
cb.ReturnStr(s.Text)
End Sub
Public Sub Main()
Dim uc As New uCalc()
'// 1. Define the custom URLDecode function in the uCalc engine
uc.DefineFunction("URLDecode(s As String) As String", AddressOf URLDecode)
'// 2. Create the transformer and configure its tokenizer
Using t As New uCalc.Transformer(uc)
'// Treat '&' as a statement separator to process each pair individually
t.Tokens.Add("&", TokenType.StatementSep)
'// 3. Define the rule to capture key-value pairs and decode the value
t.FromTo("{@Alphanumeric:key}={value}", "- {key}: '{@Eval: URLDecode(value)}'")
'// 4. Process a real-world query string
Dim queryString = "name=John%20Doe&role=user+admin&id=123"
'// Use Filter() to get a clean, newline-separated list of the results
Console.WriteLine(t.Transform(queryString).Matches)
End Using
End Sub
End Module
- name: 'John Doe'
- role: 'user admin'
- id: '123' Imports System Imports uCalcSoftware Public Module Program Public Sub URLDecode(ByVal cb As uCalc.Callback) '// In a real application, this would be a full URL decoding implementation. '// For this example, we'll just handle spaces (%20) and plus signs (+). Dim s As uCalc.String = cb.ArgStr(1) s.Replace("%20", " ").Replace("+", " ") cb.ReturnStr(s.Text) End Sub Public Sub Main() Dim uc As New uCalc() '// 1. Define the custom URLDecode function in the uCalc engine uc.DefineFunction("URLDecode(s As String) As String", AddressOf URLDecode) '// 2. Create the transformer and configure its tokenizer Using t As New uCalc.Transformer(uc) '// Treat '&' as a statement separator to process each pair individually t.Tokens.Add("&", TokenType.StatementSep) '// 3. Define the rule to capture key-value pairs and decode the value t.FromTo("{@Alphanumeric:key}={value}", "- {key}: '{@Eval: URLDecode(value)}'") '// 4. Process a real-world query string Dim queryString = "name=John%20Doe&role=user+admin&id=123" '// Use Filter() to get a clean, newline-separated list of the results Console.WriteLine(t.Transform(queryString).Matches) End Using End Sub End Module