How to escape special char when use glib.string.escape() - gtk

Due to the document of glib.string.escape()
Escapes the special characters '\b', '\f', '\n', '\r', '\t', '\v', '\' and '"' in the string source by inserting a '\' before them.
Additionally all characters in the range 0x01-0x1F (everything below SPACE) and in the range 0x7F-0xFF (all non-ASCII chars) are replaced with a '\' followed by their octal representation. Characters supplied in exceptions are not escaped.
Now I want not eacape "0x7F-0xFF" characters. How to write the exceptions part?
my example code no work.
shellcmd = "bash -c \""+file.get_string(title,"List").escape("0x7F-0xFF")+"\"";
print("shellcmd: %s\n", shellcmd);
Process.spawn_command_line_sync (shellcmd,
out ls_stdout, out ls_stderr, out ls_status);
if(ls_status!=0){ list = ls_stderr.split("\n"); }
else{ list = ls_stdout.split("\n"); }
this works.
shellcmd = "bash -c \""+file.get_string(title,"Check").replace("\"","\\\"")+"\"";

You actually have to put the characters 0x7f to 0xff in the exceptions argument. So something like:
shellcmd = "bash -c \""+file.get_string(title,"List").escape("\x7F\x80\x81\x82…\xfe\xff")+"\"";
You would need to list them all manually.
Looking more generally at your code, you seem to be constructing a command to run. This is a very bad idea and you should never do it. It is wide open to code injection. Use Process.spawn_sync() and pass it an argument vector instead.

Related

Running a PowerShell script file with path containing spaces from Jenkins Pipeline without using backtick

I want to run the following PowerShell script file from Jenkins Pipeline:
".\Folder With Spaces\script.ps1"
I have been able to do it with the following step definition:
powershell(script: '.\\Folder` With` Spaces\\script.ps1')
So I have to remember to:
escape the backslash with a double backslash (Groovy syntax)
escape the space with backtick (PowerShell syntax)
I would prefer to avoid at least some of this. Is it possible to avoid using the backtick escaping, for example? (Putting it between "" does not seem to work, for some reason.)
I found that it's possible to use the ampersand, or invoke, operator, like this:
powershell(script: "& '.\\Folder With Spaces\\script.ps1'")
That gets rid of the backtick escaping, and should make life a tiny bit easier.
To avoid escaping the backslashes you could use slashy strings or dollar slashy strings as follows. However you cannot use a backslash as the very last character in slashy strings as it would escape the /. Of course slashes as well would have to be escaped when using slashy strings.
String slashy = /String with \ /
echo slashy
assert slashy == 'String with \\ '
// won't work
// String slashy = /String with \/
String dollarSlashy = $/String with / and \/$
echo dollarSlashy
assert dollarSlashy == 'String with / and \\'
And of course you'll lose the possibility to include newlines \n and other special characters in the string using the \. However as both slashy and dollar slashy strings have multi line support at least newlines can be included like:
String slashyWithNewline = /String with \/ and \
with newline/
echo slashyWithNewline
assert slashyWithNewline == 'String with / and \\ \nwith newline'
String dollarSlashyWithNewline = $/String with / and \
with newline/$
echo dollarSlashyWithNewline
assert dollarSlashyWithNewline == 'String with / and \\ \nwith newline'
If you combine that with your very own answer you won't need both of the escaping.

How to auto-escape a special char in VS Code Snippets?

I want to write a snippet for Debugging in TYPO3.
This is my Snippet-Code in php.json file:
"TYPO3 Extbase DebuggerUtility": {
"prefix": "ee",
"body": [
"\\TYPO3\\CMS\\Extbase\\Utility\\DebuggerUtility::var_dump($1,'$1');",
"$0"
],
"description": "TYPO3 Extbase DebuggerUtility"
},
If I want to debug something liket this : $this->settings['key'] I get this code:
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->settings['key'],'$this->settings['key']');
But it should looks like this
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->settings['key'],'$this->settings[\'key\']');
With escaped ' in the second part of that snippet.
EDIT
Thank you, but I think you missunderstood the question.
I don't want to escape a static character. I want to use the snippet and when I type the first $1-content it should be $this->settings['someKey'] but the second $1 (which is near the same) should automatically escape the ' chars I write, that I don't do this manually by hand.
So if i type '
first $1: ' second $1: \' that my Debug looks like this
Debug:
$this->settings['someKey']
contentOfsomeKey
I I don't escape the ' signs inside the "title of the debug" it breaks the string because ' wraps the debug-title.
In other words: I want to escape the content of the second $1 variable not the variable or the '-wrap in the snippet.
I hope I could clarify my issue.
If you want escape characters \ in your output you need to insert escaped escape characters: \\ this should result in single escape characters.
You might need an additional escape character if the following character needs an additional escaping: one backslash before quote \' = \\+ \' = \\\'
`

Replace emdash with double dash

I want to replace ― back into --
I tried with the utf8 encodings but that doesn't work
string = "blablabla -- blablabla ―"
I want to replace the long dash (if there is one) with double hyphens. I tried it the simple way but that didn't work:
string= string.replace ("―", "--")
I also tried to encode it with utf8 and use the codes of the special characters
stringutf8= string.encode("utf-8")
emdash= u"\u2014"
hyphen= u"\u002D"
if emdash in stringutf8:
stringutf8.replace(emdash, 2*hyphen)
Any suggestions?
I am working with text files in which sometimes apparently the two hyphens are replaced automatically with a long dash...
thanks a lot!
You are dealing with strings here. Strings are lists of characters. Replace the character, leave the encoding out of the equation.
string = 'blablabla -- blablabla \u2014'
emdash = '\u2014'
hyphen = '\u002D'
string2 = string.replace(emdash, 2*hyphen)

Sed replacing Special Characters in a string

I am having difficulties replacing a string containing special characters using sed. My old and new string are shown below
oldStr = "# td=(nstates=20) cam-b3lyp/6-31g geom=connectivity"
newStr = "# opt b3lyp/6-31g geom=connectivity"
My sed command is the following
sed -i 's/\# td\=\(nstates\=20\) cam\-b3lyp\/6\-31g geom\=connectivity/\# opt b3lyp\/6\-31g geom\=connectivity/g' myfile.txt
I dont get any errors, however there is no match. Any ideas on how to fix my patterns.
Thanks
try s|# td=(nstates=20) cam-b3lyp/6-31g geom=connectivity|# opt b3lyp/6-31g geom=connectivity|g'
you can use next to anything after s instead of /, as your expression contains slashes I used | instead. -, = and # don't have to be escaped (minus only in character sets [...]), escaped parens indicate a group, nonescaped parens are literals.

How do I encode Unicode character codes in a PowerShell string literal?

How can I encode the Unicode character U+0048 (H), say, in a PowerShell string?
In C# I would just do this: "\u0048", but that doesn't appear to work in PowerShell.
Replace '\u' with '0x' and cast it to System.Char:
PS > [char]0x0048
H
You can also use the "$()" syntax to embed a Unicode character into a string:
PS > "Acme$([char]0x2122) Company"
AcmeT Company
Where T is PowerShell's representation of the character for non-registered trademarks.
Note: this method works only for characters in Plane 0, the BMP (Basic Multilingual Plane), chars < U+10000.
According to the documentation, PowerShell Core 6.0 adds support with this escape sequence:
PS> "`u{0048}"
H
see https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-6#unicode-character-ux
Maybe this isn't the PowerShell way, but this is what I do. I find it to be cleaner.
[regex]::Unescape("\u0048") # Prints H
[regex]::Unescape("\u0048ello") # Prints Hello
For those of us still on 5.1 and wanting to use the higher-order Unicode charset (for which none of these answers work) I made this function so you can simply build strings like so:
'this is my favourite park ',0x1F3DE,'. It is pretty sweet ',0x1F60A | Unicode
#takes in a stream of strings and integers,
#where integers are unicode codepoints,
#and concatenates these into valid UTF16
Function Unicode {
Begin {
$output=[System.Text.StringBuilder]::new()
}
Process {
$output.Append($(
if ($_ -is [int]) { [char]::ConvertFromUtf32($_) }
else { [string]$_ }
)) | Out-Null
}
End { $output.ToString() }
}
Note that getting these to display in your console is a whole other problem, but if you're outputting to an Outlook email or a Gridview (below) it will just work (as utf16 is native for .NET interfaces).
This also means you can also output plain control (not necessarily unicode) characters pretty easily if you're more comfortable with decimal since you dont actually need to use the 0x (hex) syntax to make the integers. 'hello',32,'there' | Unicode would put a non-breaking space betwixt the two words, the same as if you did 0x20 instead.
Another way using PowerShell.
$Heart = $([char]0x2665)
$Diamond = $([char]0x2666)
$Club = $([char]0x2663)
$Spade = $([char]0x2660)
Write-Host $Heart -BackgroundColor Yellow -ForegroundColor Magenta
Use the command help Write-Host -Full to read all about it.
To make it work for characters outside the BMP you need to use Char.ConvertFromUtf32()
'this is my favourite park ' + [char]::ConvertFromUtf32(0x1F3DE) +
'. It is pretty sweet ' + [char]::ConvertFromUtf32(0x1F60A)
Note that some characters like 🌎 might need a "double rune" to be printed:
PS> "C:\foo\bar\$([char]0xd83c)$([char]0xdf0e)something.txt"
Will print:
C:\foo\bar\🌎something.txt
You can find these "runes" here, in the "unicode escape" row:
https://dencode.com/string