string_utils.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/io/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. }
  39. int src_len = 2;
  40. int len = p_text.length();
  41. if (len == 0) {
  42. return -1;
  43. }
  44. const char32_t *src = p_text.get_data();
  45. for (int i = p_from; i <= (len - src_len); i++) {
  46. bool found = true;
  47. for (int j = 0; j < src_len; j++) {
  48. int read_pos = i + j;
  49. ERR_FAIL_COND_V(read_pos >= len, -1);
  50. switch (j) {
  51. case 0:
  52. found = src[read_pos] == '%';
  53. break;
  54. case 1: {
  55. char32_t c = src[read_pos];
  56. found = src[read_pos] == 's' || (c >= '0' && c <= '5');
  57. break;
  58. }
  59. default:
  60. found = false;
  61. }
  62. if (!found) {
  63. break;
  64. }
  65. }
  66. if (found) {
  67. return i;
  68. }
  69. }
  70. return -1;
  71. }
  72. } // namespace
  73. String sformat(const String &p_text, const String &p1, const String &p2,
  74. const String &p3, const String &p4, const String &p5, const String &p6) {
  75. if (p_text.length() < 2) {
  76. return p_text;
  77. }
  78. String args[6] = { p1, p2, p3, p4, p5, p6 };
  79. String new_string;
  80. int findex = 0;
  81. int search_from = 0;
  82. int result = 0;
  83. while ((result = sfind(p_text, search_from)) >= 0) {
  84. char32_t c = p_text[result + 1];
  85. int req_index = (c == 's' ? findex++ : c - '0');
  86. new_string += p_text.substr(search_from, result - search_from);
  87. new_string += args[req_index];
  88. search_from = result + 2;
  89. }
  90. new_string += p_text.substr(search_from, p_text.length() - search_from);
  91. return new_string;
  92. }
  93. #ifdef TOOLS_ENABLED
  94. bool is_csharp_keyword(const String &p_name) {
  95. // Reserved keywords
  96. return p_name == "abstract" || p_name == "as" || p_name == "base" || p_name == "bool" ||
  97. p_name == "break" || p_name == "byte" || p_name == "case" || p_name == "catch" ||
  98. p_name == "char" || p_name == "checked" || p_name == "class" || p_name == "const" ||
  99. p_name == "continue" || p_name == "decimal" || p_name == "default" || p_name == "delegate" ||
  100. p_name == "do" || p_name == "double" || p_name == "else" || p_name == "enum" ||
  101. p_name == "event" || p_name == "explicit" || p_name == "extern" || p_name == "false" ||
  102. p_name == "finally" || p_name == "fixed" || p_name == "float" || p_name == "for" ||
  103. p_name == "foreach" || p_name == "goto" || p_name == "if" || p_name == "implicit" ||
  104. p_name == "in" || p_name == "int" || p_name == "interface" || p_name == "internal" ||
  105. p_name == "is" || p_name == "lock" || p_name == "long" || p_name == "namespace" ||
  106. p_name == "new" || p_name == "null" || p_name == "object" || p_name == "operator" ||
  107. p_name == "out" || p_name == "override" || p_name == "params" || p_name == "private" ||
  108. p_name == "protected" || p_name == "public" || p_name == "readonly" || p_name == "ref" ||
  109. p_name == "return" || p_name == "sbyte" || p_name == "sealed" || p_name == "short" ||
  110. p_name == "sizeof" || p_name == "stackalloc" || p_name == "static" || p_name == "string" ||
  111. p_name == "struct" || p_name == "switch" || p_name == "this" || p_name == "throw" ||
  112. p_name == "true" || p_name == "try" || p_name == "typeof" || p_name == "uint" || p_name == "ulong" ||
  113. p_name == "unchecked" || p_name == "unsafe" || p_name == "ushort" || p_name == "using" ||
  114. p_name == "virtual" || p_name == "volatile" || p_name == "void" || p_name == "while";
  115. }
  116. String escape_csharp_keyword(const String &p_name) {
  117. return is_csharp_keyword(p_name) ? "@" + p_name : p_name;
  118. }
  119. #endif
  120. Error read_all_file_utf8(const String &p_path, String &r_content) {
  121. Vector<uint8_t> sourcef;
  122. Error err;
  123. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  124. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'.");
  125. uint64_t len = f->get_length();
  126. sourcef.resize(len + 1);
  127. uint8_t *w = sourcef.ptrw();
  128. uint64_t r = f->get_buffer(w, len);
  129. ERR_FAIL_COND_V(r != len, ERR_CANT_OPEN);
  130. w[len] = 0;
  131. String source;
  132. if (source.parse_utf8((const char *)w) != OK) {
  133. ERR_FAIL_V(ERR_INVALID_DATA);
  134. }
  135. r_content = source;
  136. return OK;
  137. }
  138. // TODO: Move to variadic templates once we upgrade to C++11
  139. String str_format(const char *p_format, ...) {
  140. va_list list;
  141. va_start(list, p_format);
  142. String res = str_format(p_format, list);
  143. va_end(list);
  144. return res;
  145. }
  146. #if defined(MINGW_ENABLED)
  147. #define RSnprintf(m_buffer, m_count, m_format, m_args_copy) vsnprintf_s(m_buffer, m_count, _TRUNCATE, m_format, m_args_copy)
  148. #define RScprintf(m_format, m_args_copy) _vscprintf(m_format, m_args_copy)
  149. #else
  150. #define RSnprintf(m_buffer, m_count, m_format, m_args_copy) vsnprintf(m_buffer, m_count, m_format, m_args_copy)
  151. #define RScprintf(m_format, m_args_copy) vsnprintf(nullptr, 0, p_format, m_args_copy)
  152. #endif
  153. String str_format(const char *p_format, va_list p_list) {
  154. char *buffer = str_format_new(p_format, p_list);
  155. String res(buffer);
  156. memdelete_arr(buffer);
  157. return res;
  158. }
  159. char *str_format_new(const char *p_format, ...) {
  160. va_list list;
  161. va_start(list, p_format);
  162. char *res = str_format_new(p_format, list);
  163. va_end(list);
  164. return res;
  165. }
  166. char *str_format_new(const char *p_format, va_list p_list) {
  167. va_list list;
  168. va_copy(list, p_list);
  169. int len = RScprintf(p_format, list);
  170. va_end(list);
  171. len += 1; // for the trailing '/0'
  172. char *buffer(memnew_arr(char, len));
  173. va_copy(list, p_list);
  174. RSnprintf(buffer, len, p_format, list);
  175. va_end(list);
  176. return buffer;
  177. }