Concourse templating: How to escape double curly-braces (parameters), to use as literals? - concourse

How do we escape double curly braces and use them as literals instead of fly interpreting them as parameters?
I wan to be able to do something like:
jobs:
- task: greet-him
params:
STRING: 'Hello {{firstname}} {{lastname}}!'
With the STRING param as a literal string.

Related

Convert a string JSON to a dictionary in Swift when curly quotes are used

I have a string JSON, but it has fancy curly quotes in it which makes NSJSONSerialization fail.
let str = "{“title”:\"this is a “test” example\"}"
try! JSONSerialization.jsonObject(with: str.data(using: .utf8)!) // Error
The quotes around title are curly double quotes and apparently JSONSerialization can not handle it and fails. A naive approach would be to simple replace all instances of the curly quote with a non-curly one. The problem with that approach is that it will change the curly quotes around test which shouldn't be changed! The quotes around title are OK to be changed but the ones around test should not.
What can I do to get around this issue?
To fix this, you talk to whoever created the string, which does not contain JSON at the moment, and convince them to create a string that does contain JSON.
For JSON the rule is: If your parser can't parse it, then it's broken and you don't touch it.
The problem isn't that JSONSerialization cannot handle it. The problem is that JSONSerialization absolutely must not under any circumstances handle it. Because it's not JSON.
If curly quotes are only used for the keys, this regex will do the job:
let str = "{“title”:\"this is a “test” example\"}"
let strFixed = str.replacingOccurrences(
of: #"“(.[^”]*)”:\"(.[^\"]*)\""#,
with: "\"$1\":\"$2\"",
options: .regularExpression
)
// It's strongly recommended to use try/catch instead of force unwrapping.
let json = try! JSONSerialization.jsonObject(with: strFixed.data(using: .utf8)!)
If we print json, we get the correct result:
{
title = "this is a \U201ctest\U201d example";
}
Explanation
“(.[^”]*)”:\"(.[^\"]*)\"
------------------------
“(.[^”]*)” match everything between curly braces,
except the closing curling brace character
: separator between keys and values
\"(.[^\"]*)\" match everything between double quotes,
except the double quote character
\"$1\":\"$2\"
-------------
\"$1\" place the first captured group between double quotes
: separator between keys and values
\"$2\" place the second captured group between double quotes

How to apply whitespaces in pound signed string in Swift?

Swift 5 came up with an extra new approach to declare string literals, which is using # sign (before opening and closing quotes) to declare a string without worrying about backslashes and quote marks.
(Reference: https://github.com/apple/swift-evolution/blob/master/proposals/0200-raw-string-escaping.md)
For example:
let string1 = #"\ Hello World"#
print(string1) // => \ Hello World
let string2 = "Hello World #"
print(string2) // => Hello World #
let string3 = ##"#\#\ Hello World #\#\"##
print(string3) // => #\#\ Hello World #\#\
However, when attempting to use pound signs for declaring a string that contains a whitespace, it won't work. Let's consider the tab:
let tabString = #"Hello World\t#"#
print(tabString) // => Hello World\t#
My expected result is Hello World # but not Hello World\t#.
How to resolve such an issue when using # declaration?
Updated Answer:
At this point, you could type it as \#t.
Thanks for #dan for commenting this.
Old Answer:
Logically, the result of Hello World\t\ seems to be logical, because the purpose of using # for declaring a string is to "literally" deal with backslashes as backslashes, means that "\t" would be displayed as "\t" but not " ".
As a workaround, what you could do here is to interpolate the whitespace in your string as a string declared without using the pound sign:
let tabString = #"Hello World \#("\t")#"#
print(tabString) // => Hello World #
Keep in mind that when interpolating inside a #""# declared string, you should add # after the backslash (\#("\t") NOT \("\t")).

PowerShell: Escape characters in Replace and Contains operations

I'm trying to replace all occurrences of $(foo)\bar with $(foo)\baz\bar literal string, I don't need to expand anything and I don't need to use regex.
This is my code:
if($myString.contains("$(foo)\bar\"))
{
$myString = $myString -replace "$(foo)\bar\","$(foo)\baz\bar\"
...
}
It doesn't work, I'm totally lost how, when and where to escape characters like $, (, \ . Should I use single or double quotes in strings?
This is my attempt:
if($myString.contains("`$(foo)\\bar\\"))
{
$myString = $myString -replace '\$(foo)\\bar\\','\$(foo)\\baz\\bar\\'
...
}
The -replace command always works with regex. You could either escape the string correctly or just use the .replace-method of the string object. The method does not use regex, so no escaping is needed:
if($myString.contains("`$(foo)\\bar\\"))
{
$myString = $myString.replace( '$(foo)\bar\','$(foo)\baz\bar\')
...
}

Does TypeScript has variable names escaping feature like backticks in Scala for literal identifiers?

Does TypeScript has variable names escaping feature like backticks in Scala for literal identifiers:
`0029-otherwise-illegal-scala-literal`
See Scala explanation in Need clarification on Scala literal identifiers (backticks)
You can find the spec at https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#8.2
Section 2.2.2 tells you
The PropertyName production from the ECMAScript grammar is reproduced
below:
  PropertyName:    LiteralPropertyName    ComputedPropertyName
  LiteralPropertyName:    IdentifierName    StringLiteral
   NumericLiteral
  ComputedPropertyName:    [ AssignmentExpression ]
A property name can be any identifier (including a reserved word), a
string literal, a numeric literal, or a computed property name. String
literals may be used to give properties names that are not valid
identifiers, such as names containing blanks. Numeric literal property
names are equivalent to string literal property names with the string
representation of the numeric literal, as defined in the ECMAScript
specification.
This includes string literals.
You can declare a property as a string literal:
class MyClass {
"return" = 1;
}
you can access it with square brackets
let myinstance = new MyClass()
let one = myinstance["return"]

Can you use string/character literals within Swift string interpolation?

Is it possible to use a String/Character literal within string interpolation in Swift?
The language reference says:
The expression you write inside parentheses within an interpolated string cannot contain an unescaped double quote (") ...
That's a little fuzzy to me, since it seems to deliberately leave the loophole of escaped double quotes.
If I try:
println( "Output: \(repeat("H",20))" );
func repeat( char:Character, times:Int ) -> String {
var output:String = "";
for index in 1...times {
output += char;
}
return output;
}
I get "Expected ',' separator".
Similarly, if I do the same thing, but escape the quotes, still no dice:
println( "Output: \(repeat(\"H\",20))" );
I suspect it's just not possible, and to be honest, no big deal -- I haven't found any example that I can't easily solve by doing a little work before the string interpolation, I guess I'm just looking for confirmation that it's just not possible.
It can be done starting in Swift 2.1:
http://www.russbishop.net/swift-2-1
Prior to that, it wasn't possible.
I hope it's:
let strfunc = repeat("H",20) as string
println( "Output: \(strfunc)" );