diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2019-04-29 16:56:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-29 16:56:25 +0200 |
commit | 554c0ea90b4bc3b94d2c9a357d6f9c1db93abfe7 (patch) | |
tree | 90076d464a839ebdd1cb4485239c5eb3aab4bb34 /modules/mono/utils/string_utils.cpp | |
parent | a3617f6ca82f4c8121f75d9d2c342ef5431dd214 (diff) | |
parent | 3380565e4be4e3decbd0981c3f6a2e54f982c2f2 (diff) |
Merge pull request #28423 from neikeq/dont-forget-to-think-a-name-for-this-branch
C#: Deprecate accessor methods and generate correct int and float types
Diffstat (limited to 'modules/mono/utils/string_utils.cpp')
-rw-r--r-- | modules/mono/utils/string_utils.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/modules/mono/utils/string_utils.cpp b/modules/mono/utils/string_utils.cpp index c390f8b9c2..0ef66577fe 100644 --- a/modules/mono/utils/string_utils.cpp +++ b/modules/mono/utils/string_utils.cpp @@ -32,6 +32,8 @@ #include "core/os/file_access.h" +#include <stdio.h> + namespace { int sfind(const String &p_text, int p_from) { @@ -184,3 +186,50 @@ Error read_all_file_utf8(const String &p_path, String &r_content) { r_content = source; return OK; } + +// TODO: Move to variadic templates once we upgrade to C++11 + +String str_format(const char *p_format, ...) { + va_list list; + + va_start(list, p_format); + String res = str_format(p_format, list); + va_end(list); + + return res; +} +// va_copy was defined in the C99, but not in C++ standards before C++11. +// When you compile C++ without --std=c++<XX> option, compilers still define +// va_copy, otherwise you have to use the internal version (__va_copy). +#if !defined(va_copy) +#if defined(__GNUC__) +#define va_copy(d, s) __va_copy((d), (s)) +#else +#define va_copy(d, s) ((d) = (s)) +#endif +#endif + +#if defined(MINGW_ENABLED) || defined(_MSC_VER) +#define vsnprintf vsnprintf_s +#endif + +String str_format(const char *p_format, va_list p_list) { + va_list list; + + va_copy(list, p_list); + int len = vsnprintf(NULL, 0, p_format, list); + va_end(list); + + len += 1; // for the trailing '/0' + + char *buffer(memnew_arr(char, len)); + + va_copy(list, p_list); + vsnprintf(buffer, len, p_format, list); + va_end(list); + + String res(buffer); + memdelete_arr(buffer); + + return res; +} |