延迟模板字符串插值

编程入门 行业动态 更新时间:2024-10-23 05:02:12
本文介绍了延迟模板字符串插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在C#中,有这样的字符串插值支持:

In C# there is a string interpolation support like this:

$"Constant with {Value}"

它将使用范围内变量 Value 格式化该字符串.

which will format this string using in-scope variable Value.

但是以下内容无法在当前的C#语法中进行编译.

But the following won't compile in current C# syntax.

说,我有一个静态的 Dictionary< string,string> 模板:

Say, I have a static Dictionary<string, string> of templates:

templates = new Dictionary<string, string> { { "Key1", $"{Value1}" }, { "Key2", $"Constant with {Value2}" } }

然后在每次运行此方法时,我都要填写占位符:

And then on every run of this method I want to fill in the placeholders:

public IDictionary<string, string> FillTemplate(IDictionary<string, string> placeholderValues) { return templates.ToDictionary( t => t.Key, t => string.FormatByNames(t.Value, placeholderValues)); }

在不执行那些占位符的Regex解析然后对该Regex进行替换回调的情况下是否可以实现?哪种方法最适合作为热路径?

Is it achievable without implementing Regex parsing of those placeholders and then a replace callback on that Regex? What are the most performant options that can suit this method as being a hot path?

例如,可以在Python中轻松实现:

For example, it is easily achievable in Python:

>>> templates = { "Key1": "{Value1}", "Key2": "Constant with {Value2}" } >>> values = { "Value1": "1", "Value2": "example 2" } >>> result = dict(((k, v.format(**values)) for k, v in templates.items())) >>> result {'Key2': 'Constant with example 2', 'Key1': '1'} >>> values2 = { "Value1": "another", "Value2": "different" } >>> result2 = dict(((k, v.format(**values2)) for k, v in templates.items())) >>> result2 {'Key2': 'Constant with different', 'Key1': 'another'}

推荐答案

使用基于正则表达式进行替换的扩展方法,对于每个调用多个 Replace 调用,我获得了更快的速度值.

Using an extension method that does a substitution based on regular expressions, I get a good speed up over using multiple Replace calls for each value.

这是我的扩展方法,用于扩展大括号包围的变量:

Here is my extension method for expanding brace surrounded variables:

public static class ExpandExt { static Regex varPattern = new Regex(@"{(?<var>\w+)}", RegexOptions.Compiled); public static string Expand(this string src, Dictionary<string, string> vals) => varPattern.Replace(src, m => vals.TryGetValue(m.Groups[1].Value, out var v) ? v : m.Value); }

这是使用它的示例代码:

And here is the sample code using it:

var ans = templates.ToDictionary(kv => kv.Key, kv => kv.Value.Expand(values));

在18个条目上使用 values 进行了10,000次以上的重复扩展,通常只有一次替换,与多个 String.Replace 调用相比,我得到的速度快3倍.

Over 10,000 repeating expansions with values at 18 entries and typically only one replacement, I get 3x faster than multiple String.Replace calls.

更多推荐

延迟模板字符串插值

本文发布于:2023-11-08 14:06:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569586.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   模板   插值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!