From 0b8a785539ce7823855944aeff33aad3773aad6a Mon Sep 17 00:00:00 2001 From: Tomasz Chabora Date: Fri, 3 May 2019 14:21:04 +0200 Subject: [PATCH] Make second parameter of substr optional --- core/ustring.cpp | 3 +++ core/ustring.h | 2 +- core/variant_call.cpp | 2 +- doc/classes/String.xml | 4 ++-- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/core/ustring.cpp b/core/ustring.cpp index 78feddb229..902976751f 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -2292,6 +2292,9 @@ String String::insert(int p_at_pos, const String &p_string) const { } String String::substr(int p_from, int p_chars) const { + if (p_chars == -1) + p_chars = length() - p_from; + if (empty() || p_from < 0 || p_from >= length() || p_chars <= 0) return ""; diff --git a/core/ustring.h b/core/ustring.h index e2e62874d6..9ead36e383 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -201,7 +201,7 @@ public: } /* complex helpers */ - String substr(int p_from, int p_chars) const; + String substr(int p_from, int p_chars = -1) const; int find(const String &p_str, int p_from = 0) const; ///< return <0 if failed int find(const char *p_str, int p_from = 0) const; ///< return <0 if failed int find_char(const CharType &p_char, int p_from = 0) const; ///< return <0 if failed diff --git a/core/variant_call.cpp b/core/variant_call.cpp index f9f73b4e51..df45262819 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -1494,7 +1494,7 @@ void register_variant_methods() { ADDFUNC1R(STRING, INT, String, casecmp_to, STRING, "to", varray()); ADDFUNC1R(STRING, INT, String, nocasecmp_to, STRING, "to", varray()); ADDFUNC0R(STRING, INT, String, length, varray()); - ADDFUNC2R(STRING, STRING, String, substr, INT, "from", INT, "len", varray()); + ADDFUNC2R(STRING, STRING, String, substr, INT, "from", INT, "len", varray(-1)); ADDFUNC2R(STRING, INT, String, find, STRING, "what", INT, "from", varray(0)); diff --git a/doc/classes/String.xml b/doc/classes/String.xml index 526a9427dc..7a30801a22 100644 --- a/doc/classes/String.xml +++ b/doc/classes/String.xml @@ -748,10 +748,10 @@ - + - Returns part of the string from the position [code]from[/code] with length [code]len[/code]. + Returns part of the string from the position [code]from[/code] with length [code]len[/code]. Argument [code]len[/code] is optional and using -1 will return remaining characters from given position.