package d2format
import (
"strings"
"oss.terrastruct.com/d2/d2ast"
)
func escapeSingleQuotedValue(s string ) string {
var b strings .Builder
for _ , r := range s {
switch r {
case '\'' :
b .WriteByte ('\'' )
case '\n' :
b .WriteByte ('\\' )
b .WriteByte ('n' )
continue
}
b .WriteRune (r )
}
return b .String ()
}
func escapeDoubledQuotedValue(s string , inKey bool ) string {
var b strings .Builder
for _ , r := range s {
switch r {
case '"' , '\\' :
b .WriteByte ('\\' )
case '\n' :
b .WriteByte ('\\' )
b .WriteByte ('n' )
continue
}
if !inKey && r == '$' {
b .WriteByte ('\\' )
}
b .WriteRune (r )
}
return b .String ()
}
func escapeUnquotedValue(s string , inKey bool ) string {
if len (s ) == 0 {
return `""`
}
if strings .EqualFold (s , "null" ) {
return `'null'`
}
var b strings .Builder
for i , r := range s {
switch r {
case '\'' , '"' , '|' :
if i == 0 {
b .WriteByte ('\\' )
}
case '\n' :
b .WriteByte ('\\' )
b .WriteByte ('n' )
continue
default :
if inKey {
switch r {
case '-' :
if i +1 < len (s ) && s [i +1 ] == '-' {
b .WriteByte ('\\' )
}
case '&' :
if i == 0 {
b .WriteByte ('\\' )
}
default :
if strings .ContainsRune (d2ast .UnquotedKeySpecials , r ) {
b .WriteByte ('\\' )
}
}
} else if strings .ContainsRune (d2ast .UnquotedValueSpecials , r ) {
b .WriteByte ('\\' )
}
}
b .WriteRune (r )
}
return b .String ()
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .