crozone / FormatWith

String extensions for named parameterized string formatting.
MIT License
73 stars 13 forks source link

custom braces as string, not char - {{ }} << >> #24

Open Planche95 opened 3 years ago

Planche95 commented 3 years ago

Hi, is it possible to use string as braces not char? Like {{ }} << >>? Using only one character is risky, you can end up with unwanted parameters.

crozone commented 3 years ago

Passing strings as the brace parameter is not supported. This is because the library is hardcoded to assume that a pair of either brace character is an escaped brace, and strings do not conform nicely to this pattern.

For example, if the brace characters are '{' and '}', they are escaped using "{{" "}}". There is no ambiguity because all literal braces should be escaped before the string is parsed. An input such as "{{{param}}}" will be correctly parsed as "{" + param + "}".

I'm interested in the scenario where multi-character braces is required. If we were to allow "{{" and "}}" as the braces, how should they be escaped without introducing ambiguities?

Planche95 commented 3 years ago

Hi sorry for the late response.

I was looking at different libraries that will give me the possibility to parametrize jsons. I use to use handlebars for it https://github.com/Handlebars-Net/Handlebars.Net, but it doesn't have build in functionality to get all parameter names from file. In handlebars, default braces are "{{", "}}", and if you want to escape them you just add "\" before them, like "{{".

In my case where I have json with random strings in it it's hard to choose only one brace, for example:

{
  "random" : "{asd<f(v)vvbm>lkk}",
  "random2" : "asda)>}xc"
}

I feel there is too much probability that "one char" open and close braces will be generated in my random strings and then I will end up with not wanted parameters. If I would use string as braces it would lower this probality. I'm not saying one brace is bad, but how should I handle this situation then?