diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2018-07-24 23:53:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-24 23:53:37 +0200 |
commit | 540da3683f9aab041279e24c328a48b8fc4fcf73 (patch) | |
tree | 0e5c550a28111204e7be7320fd04bfd6c45cf69e | |
parent | 2e67fc57e6df5151506665108240d10ca366e56c (diff) | |
parent | e0df1221bf50934063b8e881e2d2b48fb8a643c0 (diff) |
Merge pull request #20411 from ibrahn/remove-substr-strlen
removed redundant strlen calculation from String::substr
-rw-r--r-- | core/ustring.cpp | 21 | ||||
-rw-r--r-- | core/ustring.h | 1 |
2 files changed, 16 insertions, 6 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp index bb08dd13c4..f552cb13ac 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -161,14 +161,21 @@ void String::copy_from(const CharType *p_cstr, int p_clip_to) { return; } - resize(len + 1); - set(len, 0); + copy_from_unchecked(p_cstr, len); +} - CharType *dst = &operator[](0); +// assumes the following have already been validated: +// p_char != NULL +// p_length > 0 +// p_length <= p_char strlen +void String::copy_from_unchecked(const CharType *p_char, int p_length) { + resize(p_length + 1); + set(p_length, 0); - for (int i = 0; i < len; i++) { + CharType *dst = &operator[](0); - dst[i] = p_cstr[i]; + for (int i = 0; i < p_length; i++) { + dst[i] = p_char[i]; } } @@ -2246,7 +2253,9 @@ String String::substr(int p_from, int p_chars) const { return String(*this); } - return String(&c_str()[p_from], p_chars); + String s = String(); + s.copy_from_unchecked(&c_str()[p_from], p_chars); + return s; } int String::find_last(const String &p_str) const { diff --git a/core/ustring.h b/core/ustring.h index b57e9629d9..001d111d64 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -65,6 +65,7 @@ class String : public Vector<CharType> { void copy_from(const char *p_cstr); void copy_from(const CharType *p_cstr, int p_clip_to = -1); void copy_from(const CharType &p_char); + void copy_from_unchecked(const CharType *p_char, int p_length); bool _base_is_subsequence_of(const String &p_string, bool case_insensitive) const; public: |