StringUtil.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Clang.Ast;
  5. namespace SharpieBinder
  6. {
  7. public static class StringUtil
  8. {
  9. public static string DropPrefix (this string w)
  10. {
  11. var j = w.IndexOf ("_", StringComparison.Ordinal);
  12. if (j < 0 || j == w.Length - 1)
  13. return w;
  14. return w.Substring(j + 1);
  15. }
  16. public static string SafeParamName(string paramName)
  17. {
  18. //some C#'s keywords which can be potentially used as arguments
  19. string[] csharpKeywords = { "object", "event", "string", "operator", "fixed", "ref", "case", "default", "lock", "unchecked" };
  20. if (csharpKeywords.Contains(paramName))
  21. return "@" + paramName;
  22. return paramName;
  23. }
  24. /// <summary>
  25. /// Removes the "const" and "&" from a typename string definition
  26. /// </summary>
  27. public static string DropConstAndReference(this string tname)
  28. {
  29. tname = DropConst(tname);
  30. // strip the &
  31. if (tname.EndsWith("&"))
  32. tname = tname.Substring(0, tname.Length - 1);
  33. return tname.Trim();
  34. }
  35. public static string DropConst(this string str)
  36. {
  37. return str.Replace("const ", "");
  38. }
  39. public static string ExtractGenericParameter(this string str)
  40. {
  41. var open = str.IndexOf("<");
  42. var close = str.LastIndexOf(">");
  43. if (open < 0 || close < 0 || close <= open)
  44. return str;
  45. return str.Substring(open + 1, close - open - 1);
  46. }
  47. public static string DropClassOrStructPrefix(this string str)
  48. {
  49. if (str.StartsWith("class "))
  50. return str.Substring("class ".Length);
  51. if (str.StartsWith("enum "))
  52. return str.Substring("enum ".Length);
  53. if (str.StartsWith("struct "))
  54. return str.Substring("struct ".Length);
  55. return str;
  56. }
  57. public static string DropUrhoNamespace(this string str)
  58. {
  59. if (str.StartsWith("Urho3D::"))
  60. return str.Substring("Urho3D::".Length);
  61. return str;
  62. }
  63. public static string Remap (string source)
  64. {
  65. switch (source) {
  66. case "TraversingLink":
  67. return "TraversingLink";
  68. case "Conetwist":
  69. return "ConeTwist";
  70. case "Waitingforqueue":
  71. return "WaitingForQueue";
  72. case "Waitingforpath":
  73. return "WaitingForPath";
  74. case "Lookat":
  75. return "LookAt";
  76. case "Readwrite":
  77. return "ReadWrite";
  78. case "Notfocusable":
  79. return "NotFocusable";
  80. case "Resetfocus":
  81. return "ResetFocus";
  82. case "Premulalpha":
  83. return "PremultipliedAlpha";
  84. case "Subtractalpha":
  85. return "SubtractAlpha";
  86. case "Invdestalpha":
  87. return "InvDestAlpha";
  88. case "Notequal":
  89. return "NotEqual";
  90. case "Lessequal":
  91. return "LessEqual";
  92. case "Greaterequal":
  93. return "GreaterEqual";
  94. case "Bottomleft":
  95. return "BottomLeft";
  96. case "Topleft":
  97. return "TopLeft";
  98. case "Topright":
  99. return "Topright";
  100. case "Bottomright":
  101. return "BottomRight";
  102. case "Horizontalnvidia":
  103. return "HorizontalNvidia";
  104. case "Horizontalcross":
  105. return "HorizontalCross";
  106. case "Verticalcross":
  107. return "VerticalCross";
  108. }
  109. return source;
  110. }
  111. public static string RemapEnumName(string type, string value)
  112. {
  113. if (value.StartsWith("MAX_"))
  114. return value.PascalCase();
  115. switch (type)
  116. {
  117. case "InterpolationMode":
  118. return value.PascalCase();
  119. case "PrimitiveType":
  120. return value.Replace("Prim_", "").PascalCase(); //there are more than one enum with this name
  121. case "Orientation2D":
  122. case "Orientation":
  123. return value.Replace("O_", "").PascalCase(); //there are more than one enum with this name
  124. case "EmitterTypeGravity":
  125. case "EmitterType2D":
  126. case "CrowdAgentTargetState":
  127. case "NavmeshPartitionType":
  128. return value.DropPrefix().DropPrefix().PascalCase();
  129. case "ShaderType":
  130. if (value.Length < 3)
  131. return value.ToUpper();
  132. goto default;
  133. default:
  134. return value.DropPrefix().PascalCase();
  135. }
  136. }
  137. public static string RemapAcronyms(this string source)
  138. {
  139. if (string.IsNullOrEmpty(source))
  140. return source;
  141. var map = new Dictionary<string, string> { {"XML", "Xml"}, {"JSON", "Json"} };
  142. return map.Aggregate(source, (current, mapItem) => current.Replace(mapItem.Key, mapItem.Value));
  143. }
  144. public static string PascalCase (this string w)
  145. {
  146. return string.Join ("", w.Split ('_').Select (x => Remap (Capitalize (x))));
  147. }
  148. public static string Capitalize (this string word, bool restLower = true)
  149. {
  150. if (string.IsNullOrEmpty(word))
  151. return string.Empty;
  152. if (word.Length > 1)
  153. {
  154. if (char.IsDigit(word[0]))
  155. {
  156. string digits = "";
  157. foreach (char symbol in word)
  158. {
  159. if (char.IsDigit(symbol))
  160. digits += symbol;
  161. else break;
  162. }
  163. string result = "N" + digits;
  164. if (result.Length < word.Length)
  165. {
  166. result += Capitalize(word.Substring(digits.Length));
  167. }
  168. return result;
  169. }
  170. else
  171. {
  172. var r = word.Substring(1);
  173. return char.ToUpper(word[0]) + (restLower ? r.ToLower() : r);
  174. }
  175. }
  176. return char.ToUpper (word[0]).ToString ();
  177. }
  178. public static IEnumerable<string> GetMethodComments(CXXMethodDecl decl)
  179. {
  180. return ExtractTextComments(decl.DumpToString());
  181. }
  182. public static IEnumerable<string> GetTypeComments(EnumDecl decl)
  183. {
  184. return ExtractTextComments(decl.DumpToString()).Take(1);
  185. }
  186. public static IEnumerable<string> GetTypeComments(CXXRecordDecl decl)
  187. {
  188. return ExtractTextComments(decl.DumpToString()).Take(1);
  189. }
  190. static IEnumerable<string> ExtractTextComments(string dump)
  191. {
  192. //workaround since TextComment type is not surfaced in Clang.dll yet
  193. var dumpLines = dump.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries).Where(l => l.Contains("-TextComment "));
  194. foreach (var dumpLine in dumpLines)
  195. {
  196. int start = dumpLine.IndexOf("\"");
  197. int end = dumpLine.LastIndexOf("\"");
  198. if (start > 0 && end > 0)
  199. yield return dumpLine.Substring(start + 1, end - start - 1);
  200. }
  201. }
  202. }
  203. }