naming_utils.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**************************************************************************/
  2. /* naming_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 "naming_utils.h"
  31. #include "core/string/ucaps.h"
  32. #include "core/templates/hash_map.h"
  33. HashMap<String, String> _create_hashmap_from_vector(Vector<Pair<String, String>> vector) {
  34. HashMap<String, String> hashmap = HashMap<String, String>(vector.size());
  35. for (const Pair<String, String> &pair : vector) {
  36. hashmap.insert(pair.first, pair.second);
  37. }
  38. return hashmap;
  39. }
  40. // Hardcoded collection of PascalCase name conversions.
  41. const HashMap<String, String> pascal_case_name_overrides = _create_hashmap_from_vector({
  42. { "BitMap", "Bitmap" },
  43. { "JSONRPC", "JsonRpc" },
  44. { "Object", "GodotObject" },
  45. { "OpenXRIPBinding", "OpenXRIPBinding" },
  46. { "SkeletonModification2DCCDIK", "SkeletonModification2DCcdik" },
  47. { "SkeletonModification2DFABRIK", "SkeletonModification2DFabrik" },
  48. { "SkeletonModification3DCCDIK", "SkeletonModification3DCcdik" },
  49. { "SkeletonModification3DFABRIK", "SkeletonModification3DFabrik" },
  50. { "System", "System_" },
  51. { "Thread", "GodotThread" },
  52. });
  53. // Hardcoded collection of PascalCase part conversions.
  54. const HashMap<String, String> pascal_case_part_overrides = _create_hashmap_from_vector({
  55. { "AA", "AA" }, // Anti Aliasing
  56. { "AO", "AO" }, // Ambient Occlusion
  57. { "FILENAME", "FileName" },
  58. { "FADEIN", "FadeIn" },
  59. { "FADEOUT", "FadeOut" },
  60. { "FX", "FX" },
  61. { "GI", "GI" }, // Global Illumination
  62. { "GZIP", "GZip" },
  63. { "HBOX", "HBox" }, // Horizontal Box
  64. { "ID", "Id" },
  65. { "IO", "IO" }, // Input/Output
  66. { "IP", "IP" }, // Internet Protocol
  67. { "IV", "IV" }, // Initialization Vector
  68. { "MACOS", "MacOS" },
  69. { "NODEPATH", "NodePath" },
  70. { "SPIRV", "SpirV" },
  71. { "STDIN", "StdIn" },
  72. { "STDOUT", "StdOut" },
  73. { "USERNAME", "UserName" },
  74. { "UV", "UV" },
  75. { "UV2", "UV2" },
  76. { "VBOX", "VBox" }, // Vertical Box
  77. { "WHITESPACE", "WhiteSpace" },
  78. { "WM", "WM" },
  79. { "XR", "XR" },
  80. { "XRAPI", "XRApi" },
  81. });
  82. String _get_pascal_case_part_override(String p_part, bool p_input_is_upper = true) {
  83. if (!p_input_is_upper) {
  84. for (int i = 0; i < p_part.length(); i++) {
  85. p_part[i] = _find_upper(p_part[i]);
  86. }
  87. }
  88. if (pascal_case_part_overrides.has(p_part)) {
  89. return pascal_case_part_overrides.get(p_part);
  90. }
  91. return String();
  92. }
  93. Vector<String> _split_pascal_case(const String &p_identifier) {
  94. Vector<String> parts;
  95. int current_part_start = 0;
  96. bool prev_was_upper = is_ascii_upper_case(p_identifier[0]);
  97. for (int i = 1; i < p_identifier.length(); i++) {
  98. if (prev_was_upper) {
  99. if (is_digit(p_identifier[i]) || is_ascii_lower_case(p_identifier[i])) {
  100. if (!is_digit(p_identifier[i])) {
  101. // These conditions only apply when the separator is not a digit.
  102. if (i - current_part_start == 1) {
  103. // Upper character was only the beginning of a word.
  104. prev_was_upper = false;
  105. continue;
  106. }
  107. if (i != p_identifier.length()) {
  108. // If this is not the last character, the last uppercase
  109. // character is the start of the next word.
  110. i--;
  111. }
  112. }
  113. if (i - current_part_start > 0) {
  114. parts.append(p_identifier.substr(current_part_start, i - current_part_start));
  115. current_part_start = i;
  116. prev_was_upper = false;
  117. }
  118. }
  119. } else {
  120. if (is_digit(p_identifier[i]) || is_ascii_upper_case(p_identifier[i])) {
  121. parts.append(p_identifier.substr(current_part_start, i - current_part_start));
  122. current_part_start = i;
  123. prev_was_upper = true;
  124. }
  125. }
  126. }
  127. // Add the rest of the identifier as the last part.
  128. if (current_part_start != p_identifier.length()) {
  129. parts.append(p_identifier.substr(current_part_start));
  130. }
  131. return parts;
  132. }
  133. String pascal_to_pascal_case(const String &p_identifier) {
  134. if (p_identifier.length() == 0) {
  135. return p_identifier;
  136. }
  137. if (p_identifier.length() <= 2) {
  138. return p_identifier.to_upper();
  139. }
  140. if (pascal_case_name_overrides.has(p_identifier)) {
  141. // Use hardcoded value for the identifier.
  142. return pascal_case_name_overrides.get(p_identifier);
  143. }
  144. Vector<String> parts = _split_pascal_case(p_identifier);
  145. String ret;
  146. for (String &part : parts) {
  147. String part_override = _get_pascal_case_part_override(part);
  148. if (!part_override.is_empty()) {
  149. // Use hardcoded value for part.
  150. ret += part_override;
  151. continue;
  152. }
  153. if (part.length() <= 2 && part.to_upper() == part) {
  154. // Acronym of length 1 or 2.
  155. for (int j = 0; j < part.length(); j++) {
  156. part[j] = _find_upper(part[j]);
  157. }
  158. ret += part;
  159. continue;
  160. }
  161. part[0] = _find_upper(part[0]);
  162. for (int i = 1; i < part.length(); i++) {
  163. if (is_digit(part[i - 1])) {
  164. // Use uppercase after digits.
  165. part[i] = _find_upper(part[i]);
  166. continue;
  167. }
  168. part[i] = _find_lower(part[i]);
  169. }
  170. ret += part;
  171. }
  172. return ret;
  173. }
  174. String snake_to_pascal_case(const String &p_identifier, bool p_input_is_upper) {
  175. String ret;
  176. Vector<String> parts = p_identifier.split("_", true);
  177. for (int i = 0; i < parts.size(); i++) {
  178. String part = parts[i];
  179. String part_override = _get_pascal_case_part_override(part, p_input_is_upper);
  180. if (!part_override.is_empty()) {
  181. // Use hardcoded value for part.
  182. ret += part_override;
  183. continue;
  184. }
  185. if (!part.is_empty()) {
  186. part[0] = _find_upper(part[0]);
  187. for (int j = 1; j < part.length(); j++) {
  188. if (is_digit(part[j - 1])) {
  189. // Use uppercase after digits.
  190. part[j] = _find_upper(part[j]);
  191. continue;
  192. }
  193. if (p_input_is_upper) {
  194. part[j] = _find_lower(part[j]);
  195. }
  196. }
  197. ret += part;
  198. } else {
  199. if (i == 0 || i == (parts.size() - 1)) {
  200. // Preserve underscores at the beginning and end
  201. ret += "_";
  202. } else {
  203. // Preserve contiguous underscores
  204. if (parts[i - 1].length()) {
  205. ret += "__";
  206. } else {
  207. ret += "_";
  208. }
  209. }
  210. }
  211. }
  212. return ret;
  213. }
  214. String snake_to_camel_case(const String &p_identifier, bool p_input_is_upper) {
  215. String ret;
  216. Vector<String> parts = p_identifier.split("_", true);
  217. for (int i = 0; i < parts.size(); i++) {
  218. String part = parts[i];
  219. String part_override = _get_pascal_case_part_override(part, p_input_is_upper);
  220. if (!part_override.is_empty()) {
  221. // Use hardcoded value for part.
  222. if (i == 0) {
  223. part_override[0] = _find_lower(part_override[0]);
  224. }
  225. ret += part_override;
  226. continue;
  227. }
  228. if (!part.is_empty()) {
  229. if (i == 0) {
  230. part[0] = _find_lower(part[0]);
  231. } else {
  232. part[0] = _find_upper(part[0]);
  233. }
  234. for (int j = 1; j < part.length(); j++) {
  235. if (is_digit(part[j - 1])) {
  236. // Use uppercase after digits.
  237. part[j] = _find_upper(part[j]);
  238. continue;
  239. }
  240. if (p_input_is_upper) {
  241. part[j] = _find_lower(part[j]);
  242. }
  243. }
  244. ret += part;
  245. } else {
  246. if (i == 0 || i == (parts.size() - 1)) {
  247. // Preserve underscores at the beginning and end
  248. ret += "_";
  249. } else {
  250. // Preserve contiguous underscores
  251. if (parts[i - 1].length()) {
  252. ret += "__";
  253. } else {
  254. ret += "_";
  255. }
  256. }
  257. }
  258. }
  259. return ret;
  260. }