string_utils.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*************************************************************************/
  2. /* string_utils.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. namespace {
  33. int sfind(const String &p_text, int p_from) {
  34. if (p_from < 0)
  35. return -1;
  36. int src_len = 2;
  37. int len = p_text.length();
  38. if (src_len == 0 || len == 0)
  39. return -1;
  40. const CharType *src = p_text.c_str();
  41. for (int i = p_from; i <= (len - src_len); i++) {
  42. bool found = true;
  43. for (int j = 0; j < src_len; j++) {
  44. int read_pos = i + j;
  45. if (read_pos >= len) {
  46. ERR_PRINT("read_pos >= len");
  47. return -1;
  48. };
  49. switch (j) {
  50. case 0:
  51. found = src[read_pos] == '%';
  52. break;
  53. case 1: {
  54. CharType c = src[read_pos];
  55. found = src[read_pos] == 's' || (c >= '0' || c <= '4');
  56. break;
  57. }
  58. default:
  59. found = false;
  60. }
  61. if (!found) {
  62. break;
  63. }
  64. }
  65. if (found)
  66. return i;
  67. }
  68. return -1;
  69. }
  70. } // namespace
  71. String sformat(const String &p_text, const Variant &p1, const Variant &p2, const Variant &p3, const Variant &p4, const Variant &p5) {
  72. if (p_text.length() < 2)
  73. return p_text;
  74. Array args;
  75. if (p1.get_type() != Variant::NIL) {
  76. args.push_back(p1);
  77. if (p2.get_type() != Variant::NIL) {
  78. args.push_back(p2);
  79. if (p3.get_type() != Variant::NIL) {
  80. args.push_back(p3);
  81. if (p4.get_type() != Variant::NIL) {
  82. args.push_back(p4);
  83. if (p5.get_type() != Variant::NIL) {
  84. args.push_back(p5);
  85. }
  86. }
  87. }
  88. }
  89. }
  90. String new_string;
  91. int findex = 0;
  92. int search_from = 0;
  93. int result = 0;
  94. while ((result = sfind(p_text, search_from)) >= 0) {
  95. CharType c = p_text[result + 1];
  96. int req_index = (c == 's' ? findex++ : c - '0');
  97. new_string += p_text.substr(search_from, result - search_from);
  98. new_string += args[req_index].operator String();
  99. search_from = result + 2;
  100. }
  101. new_string += p_text.substr(search_from, p_text.length() - search_from);
  102. return new_string;
  103. }
  104. #ifdef TOOLS_ENABLED
  105. bool is_csharp_keyword(const String &p_name) {
  106. // Reserved keywords
  107. return p_name == "abstract" || p_name == "as" || p_name == "base" || p_name == "bool" ||
  108. p_name == "break" || p_name == "byte" || p_name == "case" || p_name == "catch" ||
  109. p_name == "char" || p_name == "checked" || p_name == "class" || p_name == "const" ||
  110. p_name == "continue" || p_name == "decimal" || p_name == "default" || p_name == "delegate" ||
  111. p_name == "do" || p_name == "double" || p_name == "else" || p_name == "enum" ||
  112. p_name == "event" || p_name == "explicit" || p_name == "extern" || p_name == "false" ||
  113. p_name == "finally" || p_name == "fixed" || p_name == "float" || p_name == "for" ||
  114. p_name == "forech" || p_name == "goto" || p_name == "if" || p_name == "implicit" ||
  115. p_name == "in" || p_name == "int" || p_name == "interface" || p_name == "internal" ||
  116. p_name == "is" || p_name == "lock" || p_name == "long" || p_name == "namespace" ||
  117. p_name == "new" || p_name == "null" || p_name == "object" || p_name == "operator" ||
  118. p_name == "out" || p_name == "override" || p_name == "params" || p_name == "private" ||
  119. p_name == "protected" || p_name == "public" || p_name == "readonly" || p_name == "ref" ||
  120. p_name == "return" || p_name == "sbyte" || p_name == "sealed" || p_name == "short" ||
  121. p_name == "sizeof" || p_name == "stackalloc" || p_name == "static" || p_name == "string" ||
  122. p_name == "struct" || p_name == "switch" || p_name == "this" || p_name == "throw" ||
  123. p_name == "true" || p_name == "try" || p_name == "typeof" || p_name == "uint" || p_name == "ulong" ||
  124. p_name == "unchecked" || p_name == "unsafe" || p_name == "ushort" || p_name == "using" ||
  125. p_name == "virtual" || p_name == "volatile" || p_name == "void" || p_name == "while";
  126. }
  127. String escape_csharp_keyword(const String &p_name) {
  128. return is_csharp_keyword(p_name) ? "@" + p_name : p_name;
  129. }
  130. #endif
  131. Error read_all_file_utf8(const String &p_path, String &r_content) {
  132. PoolVector<uint8_t> sourcef;
  133. Error err;
  134. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  135. ERR_FAIL_COND_V(err != OK, err);
  136. int len = f->get_len();
  137. sourcef.resize(len + 1);
  138. PoolVector<uint8_t>::Write w = sourcef.write();
  139. int r = f->get_buffer(w.ptr(), len);
  140. f->close();
  141. memdelete(f);
  142. ERR_FAIL_COND_V(r != len, ERR_CANT_OPEN);
  143. w[len] = 0;
  144. String source;
  145. if (source.parse_utf8((const char *)w.ptr())) {
  146. ERR_FAIL_V(ERR_INVALID_DATA);
  147. }
  148. r_content = source;
  149. return OK;
  150. }