Skip to content

Commit

Permalink
Merge pull request #29347 from akien-mga/string-strip-escapes
Browse files Browse the repository at this point in the history
Fix and expose String::strip_escapes(), use it in LineEdit paste
  • Loading branch information
mhilbrunner authored May 31, 2019
2 parents 29645c8 + af2c742 commit 95f2567
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 23 deletions.
25 changes: 6 additions & 19 deletions core/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3102,29 +3102,16 @@ String String::strip_edges(bool left, bool right) const {

String String::strip_escapes() const {

int len = length();
int beg = 0, end = len;

String new_string;
for (int i = 0; i < length(); i++) {

if (operator[](i) <= 31)
beg++;
else
break;
}

for (int i = (int)(length() - 1); i >= 0; i--) {

if (operator[](i) <= 31)
end--;
else
break;
// Escape characters on first page of the ASCII table, before 32 (Space).
if (operator[](i) < 32)
continue;
new_string += operator[](i);
}

if (beg == 0 && end == len)
return *this;

return substr(beg, end - beg);
return new_string;
}

String String::lstrip(const String &p_chars) const {
Expand Down
2 changes: 2 additions & 0 deletions core/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ struct _VariantCall {
VCALL_LOCALMEM1R(String, right);
VCALL_LOCALMEM0R(String, dedent);
VCALL_LOCALMEM2R(String, strip_edges);
VCALL_LOCALMEM0R(String, strip_escapes);
VCALL_LOCALMEM1R(String, lstrip);
VCALL_LOCALMEM1R(String, rstrip);
VCALL_LOCALMEM0R(String, get_extension);
Expand Down Expand Up @@ -1526,6 +1527,7 @@ void register_variant_methods() {
ADDFUNC1R(STRING, STRING, String, left, INT, "position", varray());
ADDFUNC1R(STRING, STRING, String, right, INT, "position", varray());
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, rstrip, STRING, "chars", varray());
ADDFUNC0R(STRING, STRING, String, get_extension, varray());
Expand Down
1 change: 1 addition & 0 deletions doc/classes/LineEdit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
</constant>
<constant name="MENU_PASTE" value="2" enum="MenuItems">
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 name="MENU_CLEAR" value="3" enum="MenuItems">
Erases the whole [LineEdit] text.
Expand Down
9 changes: 8 additions & 1 deletion doc/classes/String.xml
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,14 @@
<argument index="1" name="right" type="bool" default="True">
</argument>
<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>
</method>
<method name="substr">
Expand Down
2 changes: 1 addition & 1 deletion main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph

while (I) {

I->get() = unescape_cmdline(I->get().strip_escapes());
I->get() = unescape_cmdline(I->get().strip_edges());
I = I->next();
}

Expand Down
2 changes: 1 addition & 1 deletion platform/x11/os_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2378,7 +2378,7 @@ void OS_X11::process_xevents() {

Vector<String> files = String((char *)p.data).split("\n", false);
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);

Expand Down
3 changes: 2 additions & 1 deletion scene/gui/line_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,8 @@ void LineEdit::cut_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 != "") {

Expand Down

0 comments on commit 95f2567

Please sign in to comment.