Fix and expose String::strip_escapes(), use it in LineEdit paste

Supersedes #27736.
This commit is contained in:
Rémi Verschelde 2019-05-31 15:27:53 +02:00
parent 29645c8147
commit af2c742f53
7 changed files with 21 additions and 23 deletions

View File

@ -3102,29 +3102,16 @@ String String::strip_edges(bool left, bool right) const {
String String::strip_escapes() const { String String::strip_escapes() const {
int len = length(); String new_string;
int beg = 0, end = len;
for (int i = 0; i < length(); i++) { for (int i = 0; i < length(); i++) {
if (operator[](i) <= 31) // Escape characters on first page of the ASCII table, before 32 (Space).
beg++; if (operator[](i) < 32)
else continue;
break; new_string += operator[](i);
} }
for (int i = (int)(length() - 1); i >= 0; i--) { return new_string;
if (operator[](i) <= 31)
end--;
else
break;
}
if (beg == 0 && end == len)
return *this;
return substr(beg, end - beg);
} }
String String::lstrip(const String &p_chars) const { String String::lstrip(const String &p_chars) const {

View File

@ -265,6 +265,7 @@ struct _VariantCall {
VCALL_LOCALMEM1R(String, right); VCALL_LOCALMEM1R(String, right);
VCALL_LOCALMEM0R(String, dedent); VCALL_LOCALMEM0R(String, dedent);
VCALL_LOCALMEM2R(String, strip_edges); VCALL_LOCALMEM2R(String, strip_edges);
VCALL_LOCALMEM0R(String, strip_escapes);
VCALL_LOCALMEM1R(String, lstrip); VCALL_LOCALMEM1R(String, lstrip);
VCALL_LOCALMEM1R(String, rstrip); VCALL_LOCALMEM1R(String, rstrip);
VCALL_LOCALMEM0R(String, get_extension); VCALL_LOCALMEM0R(String, get_extension);
@ -1526,6 +1527,7 @@ void register_variant_methods() {
ADDFUNC1R(STRING, STRING, String, left, INT, "position", varray()); ADDFUNC1R(STRING, STRING, String, left, INT, "position", varray());
ADDFUNC1R(STRING, STRING, String, right, INT, "position", varray()); ADDFUNC1R(STRING, STRING, String, right, INT, "position", varray());
ADDFUNC2R(STRING, STRING, String, strip_edges, BOOL, "left", BOOL, "right", varray(true, true)); ADDFUNC2R(STRING, STRING, String, strip_edges, BOOL, "left", BOOL, "right", varray(true, true));
ADDFUNC0R(STRING, STRING, String, strip_escapes, varray());
ADDFUNC1R(STRING, STRING, String, lstrip, STRING, "chars", varray()); ADDFUNC1R(STRING, STRING, String, lstrip, STRING, "chars", varray());
ADDFUNC1R(STRING, STRING, String, rstrip, STRING, "chars", varray()); ADDFUNC1R(STRING, STRING, String, rstrip, STRING, "chars", varray());
ADDFUNC0R(STRING, STRING, String, get_extension, varray()); ADDFUNC0R(STRING, STRING, String, get_extension, varray());

View File

@ -166,6 +166,7 @@
</constant> </constant>
<constant name="MENU_PASTE" value="2" enum="MenuItems"> <constant name="MENU_PASTE" value="2" enum="MenuItems">
Pastes the clipboard text over the selected text (or at the cursor's position). Pastes the clipboard text over the selected text (or at the cursor's position).
Non-printable escape characters are automatically stripped from the OS clipboard via [method String.strip_escapes].
</constant> </constant>
<constant name="MENU_CLEAR" value="3" enum="MenuItems"> <constant name="MENU_CLEAR" value="3" enum="MenuItems">
Erases the whole [LineEdit] text. Erases the whole [LineEdit] text.

View File

@ -741,7 +741,14 @@
<argument index="1" name="right" type="bool" default="True"> <argument index="1" name="right" type="bool" default="True">
</argument> </argument>
<description> <description>
Returns a copy of the string stripped of any non-printable character at the beginning and the end. The optional arguments are used to toggle stripping on the left and right edges respectively. Returns a copy of the string stripped of any non-printable character (including tabulations, spaces and line breaks) at the beginning and the end. The optional arguments are used to toggle stripping on the left and right edges respectively.
</description>
</method>
<method name="strip_escapes">
<return type="String">
</return>
<description>
Returns a copy of the string stripped of any escape character. These include all non-printable control characters of the first page of the ASCII table (&lt; 32), such as tabulation ([code]\t[/code] in C) and newline ([code]\n[/code] and [code]\r[/code]) characters, but not spaces.
</description> </description>
</method> </method>
<method name="substr"> <method name="substr">

View File

@ -375,7 +375,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
while (I) { while (I) {
I->get() = unescape_cmdline(I->get().strip_escapes()); I->get() = unescape_cmdline(I->get().strip_edges());
I = I->next(); I = I->next();
} }

View File

@ -2378,7 +2378,7 @@ void OS_X11::process_xevents() {
Vector<String> files = String((char *)p.data).split("\n", false); Vector<String> files = String((char *)p.data).split("\n", false);
for (int i = 0; i < files.size(); i++) { for (int i = 0; i < files.size(); i++) {
files.write[i] = files[i].replace("file://", "").http_unescape().strip_escapes(); files.write[i] = files[i].replace("file://", "").http_unescape().strip_edges();
} }
main_loop->drop_files(files); main_loop->drop_files(files);

View File

@ -923,7 +923,8 @@ void LineEdit::cut_text() {
void LineEdit::paste_text() { void LineEdit::paste_text() {
String paste_buffer = OS::get_singleton()->get_clipboard(); // Strip escape characters like \n and \t as they can't be displayed on LineEdit.
String paste_buffer = OS::get_singleton()->get_clipboard().strip_escapes();
if (paste_buffer != "") { if (paste_buffer != "") {