From 3bf2b79d888bf1d1dcab1847e86948745358124f Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 22 Dec 2022 21:10:04 +0100 Subject: [PATCH] Added set_length helper method to String. --- core/string/ustring.h | 1 + core/variant/variant_call.cpp | 2 ++ doc/classes/String.xml | 13 +++++++++++++ 3 files changed, 16 insertions(+) diff --git a/core/string/ustring.h b/core/string/ustring.h index b1c0dadc7..fa312b62d 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -229,6 +229,7 @@ public: return err; } + _FORCE_INLINE_ Error set_length(int p_length) { return resize(p_length + 1); } _FORCE_INLINE_ const CharType &operator[](int p_index) const { return _cowdata.get(p_index); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index dea70089a..84e75478e 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -297,6 +297,7 @@ struct _VariantCall { VCALL_LOCALMEM0(String, clear); VCALL_LOCALMEM0R(String, size); VCALL_LOCALMEM1R(String, resize); + VCALL_LOCALMEM1R(String, set_length); VCALL_LOCALMEM1R(String, casecmp_to); VCALL_LOCALMEM1R(String, nocasecmp_to); VCALL_LOCALMEM1R(String, naturalnocasecmp_to); @@ -2452,6 +2453,7 @@ void register_variant_methods() { ADDFUNC0(STRING, NIL, String, clear, varray()); ADDFUNC0R(STRING, INT, String, size, varray()); ADDFUNC1R(STRING, INT, String, resize, INT, "size", varray()); + ADDFUNC1R(STRING, INT, String, set_length, INT, "length", varray()); ADDFUNC1R(STRING, INT, String, casecmp_to, STRING, "to", varray()); ADDFUNC1R(STRING, INT, String, nocasecmp_to, STRING, "to", varray()); ADDFUNC1R(STRING, INT, String, naturalnocasecmp_to, STRING, "to", varray()); diff --git a/doc/classes/String.xml b/doc/classes/String.xml index 48900ecca..ace4eb5bd 100644 --- a/doc/classes/String.xml +++ b/doc/classes/String.xml @@ -711,6 +711,7 @@ Returns the string's amount of characters. + This does not include the null terminator (\0), use [method size] if you need that in your calculations aswell. @@ -898,6 +899,8 @@ + Resizes a string to the given size. Normally you should use [method set_length]. + This includes the null terminator, which means the last character ([code]size - 1[/code]) will be set to null (\0). @@ -958,6 +961,14 @@ [b]Note:[/b] The [code]chars[/code] is not a suffix. See [method trim_suffix] method that will remove a single suffix string rather than a set of characters. + + + + + Sets how many characters a string will be able to store. + The null terminator (/0) will be automatically added as the [code]length + 1[/code]-th character. Use [method resize] if want to calculate with the null terminator directly. + + @@ -1004,6 +1015,8 @@ + Returns the String's size. Note that size includes the null terminator! + Normally [method length] is the method you want use.