gtk - 使用 glib.string.escape() 时如何转义特殊字符

标签 gtk glib vala

由于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.

现在我想要 eacape“0x7F-0xFF”字符。如何编写异常(exception)部分?

我的示例代码不起作用。

            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"); }

这有效。

shellcmd = "bash -c \""+file.get_string(title,"Check").replace("\"","\\\"")+"\"";

最佳答案

实际上,您必须将字符 0x7f 放入 exceptions 参数中的 0xff 中。所以类似:

shellcmd = "bash -c \""+file.get_string(title,"List").escape("\x7F\x80\x81\x82…\xfe\xff")+"\"";

您需要手动列出它们。

更广泛地查看您的代码,您似乎正在构建一个要运行的命令。这是一个非常糟糕的主意,你绝对不应该这样做。它对code injection敞开大门。 。使用 Process.spawn_sync() 并向其传递一个参数向量。

关于gtk - 使用 glib.string.escape() 时如何转义特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59971175/

相关文章:

c - 需要帮助使用 C 语言通过 GTK 创建 TicTacToe

python - 如何真正将窗口置于所有窗口之上——Linux PyGTK

python - 如何在 Python 中使用 GLib.Array?

objective-c - 适用于 mac os 10.6 雪豹的 glib

opencv - 在 Gtk Widget 中显示 OpenCV 图像时出现内存泄漏

vala - 如何以正确的方式摆脱vala编译警告 "copying delegates is discouraged"?

Gtk# 和 TreeView : how to get "selected" item?

python - 使用一体式 pygtk 安装程序 Windows 7 不起作用?

vala:两个整数的商总是一个整数。为什么?

gtk3 - 瓦拉。你如何删除一个 GTK 容器的所有 child ?