string_utils.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**************************************************************************/
  2. /* string_utils.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "string_utils.h"
  31. #include "core/os/file_access.h"
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. namespace {
  35. int sfind(const String &p_text, int p_from) {
  36. if (p_from < 0)
  37. return -1;
  38. int src_len = 2;
  39. int len = p_text.length();
  40. if (len == 0)
  41. return -1;
  42. const CharType *src = p_text.c_str();
  43. for (int i = p_from; i <= (len - src_len); i++) {
  44. bool found = true;
  45. for (int j = 0; j < src_len; j++) {
  46. int read_pos = i + j;
  47. ERR_FAIL_COND_V(read_pos >= len, -1);
  48. switch (j) {
  49. case 0:
  50. found = src[read_pos] == '%';
  51. break;
  52. case 1: {
  53. CharType c = src[read_pos];
  54. found = src[read_pos] == 's' || (c >= '0' && c <= '4');
  55. break;
  56. }
  57. default:
  58. found = false;
  59. }
  60. if (!found) {
  61. break;
  62. }
  63. }
  64. if (found)
  65. return i;
  66. }
  67. return -1;
  68. }
  69. } // namespace
  70. String sformat(const String &p_text, const Variant &p1, const Variant &p2, const Variant &p3, const Variant &p4, const Variant &p5) {
  71. if (p_text.length() < 2)
  72. return p_text;
  73. Array args;
  74. if (p1.get_type() != Variant::NIL) {
  75. args.push_back(p1);
  76. if (p2.get_type() != Variant::NIL) {
  77. args.push_back(p2);
  78. if (p3.get_type() != Variant::NIL) {
  79. args.push_back(p3);
  80. if (p4.get_type() != Variant::NIL) {
  81. args.push_back(p4);
  82. if (p5.get_type() != Variant::NIL) {
  83. args.push_back(p5);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. String new_string;
  90. int findex = 0;
  91. int search_from = 0;
  92. int result = 0;
  93. while ((result = sfind(p_text, search_from)) >= 0) {
  94. CharType c = p_text[result + 1];
  95. int req_index = (c == 's' ? findex++ : c - '0');
  96. new_string += p_text.substr(search_from, result - search_from);
  97. new_string += args[req_index].operator String();
  98. search_from = result + 2;
  99. }
  100. new_string += p_text.substr(search_from, p_text.length() - search_from);
  101. return new_string;
  102. }
  103. #ifdef TOOLS_ENABLED
  104. bool is_csharp_keyword(const String &p_name) {
  105. // Reserved keywords
  106. return p_name == "abstract" || p_name == "as" || p_name == "base" || p_name == "bool" ||
  107. p_name == "break" || p_name == "byte" || p_name == "case" || p_name == "catch" ||
  108. p_name == "char" || p_name == "checked" || p_name == "class" || p_name == "const" ||
  109. p_name == "continue" || p_name == "decimal" || p_name == "default" || p_name == "delegate" ||
  110. p_name == "do" || p_name == "double" || p_name == "else" || p_name == "enum" ||
  111. p_name == "event" || p_name == "explicit" || p_name == "extern" || p_name == "false" ||
  112. p_name == "finally" || p_name == "fixed" || p_name == "float" || p_name == "for" ||
  113. p_name == "foreach" || p_name == "goto" || p_name == "if" || p_name == "implicit" ||
  114. p_name == "in" || p_name == "int" || p_name == "interface" || p_name == "internal" ||
  115. p_name == "is" || p_name == "lock" || p_name == "long" || p_name == "namespace" ||
  116. p_name == "new" || p_name == "null" || p_name == "object" || p_name == "operator" ||
  117. p_name == "out" || p_name == "override" || p_name == "params" || p_name == "private" ||
  118. p_name == "protected" || p_name == "public" || p_name == "readonly" || p_name == "ref" ||
  119. p_name == "return" || p_name == "sbyte" || p_name == "sealed" || p_name == "short" ||
  120. p_name == "sizeof" || p_name == "stackalloc" || p_name == "static" || p_name == "string" ||
  121. p_name == "struct" || p_name == "switch" || p_name == "this" || p_name == "throw" ||
  122. p_name == "true" || p_name == "try" || p_name == "typeof" || p_name == "uint" || p_name == "ulong" ||
  123. p_name == "unchecked" || p_name == "unsafe" || p_name == "ushort" || p_name == "using" ||
  124. p_name == "virtual" || p_name == "volatile" || p_name == "void" || p_name == "while";
  125. }
  126. String escape_csharp_keyword(const String &p_name) {
  127. return is_csharp_keyword(p_name) ? "@" + p_name : p_name;
  128. }
  129. #endif
  130. Error read_all_file_utf8(const String &p_path, String &r_content) {
  131. PoolVector<uint8_t> sourcef;
  132. Error err;
  133. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  134. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'.");
  135. uint64_t len = f->get_len();
  136. sourcef.resize(len + 1);
  137. PoolVector<uint8_t>::Write w = sourcef.write();
  138. uint64_t r = f->get_buffer(w.ptr(), len);
  139. f->close();
  140. memdelete(f);
  141. ERR_FAIL_COND_V(r != len, ERR_CANT_OPEN);
  142. w[len] = 0;
  143. String source;
  144. if (source.parse_utf8((const char *)w.ptr())) {
  145. ERR_FAIL_V(ERR_INVALID_DATA);
  146. }
  147. r_content = source;
  148. return OK;
  149. }
  150. // TODO: Move to variadic templates once we upgrade to C++11
  151. String str_format(const char *p_format, ...) {
  152. va_list list;
  153. va_start(list, p_format);
  154. String res = str_format(p_format, list);
  155. va_end(list);
  156. return res;
  157. }
  158. // va_copy was defined in the C99, but not in C++ standards before C++11.
  159. // When you compile C++ without --std=c++<XX> option, compilers still define
  160. // va_copy, otherwise you have to use the internal version (__va_copy).
  161. #if !defined(va_copy)
  162. #if defined(__GNUC__)
  163. #define va_copy(d, s) __va_copy((d), (s))
  164. #else
  165. #define va_copy(d, s) ((d) = (s))
  166. #endif
  167. #endif
  168. #if defined(MINGW_ENABLED)
  169. #define gd_vsnprintf(m_buffer, m_count, m_format, m_args_copy) vsnprintf_s(m_buffer, m_count, _TRUNCATE, m_format, m_args_copy)
  170. #define gd_vscprintf(m_format, m_args_copy) _vscprintf(m_format, m_args_copy)
  171. #else
  172. #define gd_vsnprintf(m_buffer, m_count, m_format, m_args_copy) vsnprintf(m_buffer, m_count, m_format, m_args_copy)
  173. #define gd_vscprintf(m_format, m_args_copy) vsnprintf(NULL, 0, p_format, m_args_copy)
  174. #endif
  175. String str_format(const char *p_format, va_list p_list) {
  176. char *buffer = str_format_new(p_format, p_list);
  177. String res(buffer);
  178. memdelete_arr(buffer);
  179. return res;
  180. }
  181. char *str_format_new(const char *p_format, ...) {
  182. va_list list;
  183. va_start(list, p_format);
  184. char *res = str_format_new(p_format, list);
  185. va_end(list);
  186. return res;
  187. }
  188. char *str_format_new(const char *p_format, va_list p_list) {
  189. va_list list;
  190. va_copy(list, p_list);
  191. int len = gd_vscprintf(p_format, list);
  192. va_end(list);
  193. len += 1; // for the trailing '/0'
  194. char *buffer(memnew_arr(char, len));
  195. va_copy(list, p_list);
  196. gd_vsnprintf(buffer, len, p_format, list);
  197. va_end(list);
  198. return buffer;
  199. }