naming_utils.cpp 8.9 KB

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