StringUtil.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. if (tname.StartsWith("const"))
  30. tname = tname.Substring("const".Length);
  31. // strip the &
  32. if (tname.EndsWith("&"))
  33. tname = tname.Substring(0, tname.Length - 1);
  34. return tname.Trim();
  35. }
  36. public static string ExtractGenericParameter(this string str)
  37. {
  38. var open = str.IndexOf("<");
  39. var close = str.LastIndexOf(">");
  40. if (open < 0 || close < 0 || close <= open)
  41. return str;
  42. return str.Substring(open + 1, close - open - 1);
  43. }
  44. public static string DropClassOrStructPrefix(this string str)
  45. {
  46. if (str.StartsWith("class "))
  47. return str.Substring("class ".Length);
  48. if (str.StartsWith("struct "))
  49. return str.Substring("struct ".Length);
  50. return str;
  51. }
  52. public static string DropUrhoNamespace(this string str)
  53. {
  54. if (str.StartsWith("Urho3D::"))
  55. return str.Substring("Urho3D::".Length);
  56. return str;
  57. }
  58. public static string Remap (string source)
  59. {
  60. switch (source) {
  61. case "TraversingLink":
  62. return "TraversingLink";
  63. case "Conetwist":
  64. return "ConeTwist";
  65. case "Waitingforqueue":
  66. return "WaitingForQueue";
  67. case "Waitingforpath":
  68. return "WaitingForPath";
  69. case "Lookat":
  70. return "LookAt";
  71. case "Readwrite":
  72. return "ReadWrite";
  73. case "Notfocusable":
  74. return "NotFocusable";
  75. case "Resetfocus":
  76. return "ResetFocus";
  77. case "Premulalpha":
  78. return "PremultipliedAlpha";
  79. case "Subtractalpha":
  80. return "SubtractAlpha";
  81. case "Invdestalpha":
  82. return "InvDestAlpha";
  83. case "Notequal":
  84. return "NotEqual";
  85. case "Lessequal":
  86. return "LessEqual";
  87. case "Greaterequal":
  88. return "GreaterEqual";
  89. case "Bottomleft":
  90. return "BottomLeft";
  91. case "Topleft":
  92. return "TopLeft";
  93. case "Topright":
  94. return "Topright";
  95. case "Bottomright":
  96. return "BottomRight";
  97. case "Horizontalnvidia":
  98. return "HorizontalNvidia";
  99. case "Horizontalcross":
  100. return "HorizontalCross";
  101. case "Verticalcross":
  102. return "VerticalCross";
  103. }
  104. return source;
  105. }
  106. public static string RemapAcronyms(this string source)
  107. {
  108. if (string.IsNullOrEmpty(source))
  109. return source;
  110. var map = new Dictionary<string, string> { {"XML", "Xml"}, {"JSON", "Json"} };
  111. return map.Aggregate(source, (current, mapItem) => current.Replace(mapItem.Key, mapItem.Value));
  112. }
  113. public static string PascalCase (this string w)
  114. {
  115. return string.Join ("", w.Split ('_').Select (x => Remap (Capitalize (x))));
  116. }
  117. public static string Capitalize (this string word, bool restLower = true)
  118. {
  119. if (string.IsNullOrEmpty(word))
  120. return string.Empty;
  121. if (word.Length > 1)
  122. {
  123. if (char.IsDigit(word[0]))
  124. {
  125. string digits = "";
  126. foreach (char symbol in word)
  127. {
  128. if (char.IsDigit(symbol))
  129. digits += symbol;
  130. else break;
  131. }
  132. string result = "N" + digits;
  133. if (result.Length < word.Length)
  134. {
  135. result += Capitalize(word.Substring(digits.Length));
  136. }
  137. return result;
  138. }
  139. else
  140. {
  141. var r = word.Substring(1);
  142. return char.ToUpper(word[0]) + (restLower ? r.ToLower() : r);
  143. }
  144. }
  145. return char.ToUpper (word[0]).ToString ();
  146. }
  147. public static IEnumerable<string> GetMethodComments(CXXMethodDecl decl)
  148. {
  149. return ExtractTextComments(decl.DumpToString());
  150. }
  151. public static IEnumerable<string> GetTypeComments(EnumDecl decl)
  152. {
  153. return ExtractTextComments(decl.DumpToString()).Take(1);
  154. }
  155. public static IEnumerable<string> GetTypeComments(CXXRecordDecl decl)
  156. {
  157. return ExtractTextComments(decl.DumpToString()).Take(1);
  158. }
  159. static IEnumerable<string> ExtractTextComments(string dump)
  160. {
  161. //workaround since TextComment type is not surfaced in Clang.dll yet
  162. var dumpLines = dump.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries).Where(l => l.Contains("-TextComment "));
  163. foreach (var dumpLine in dumpLines)
  164. {
  165. int start = dumpLine.IndexOf("\"");
  166. int end = dumpLine.LastIndexOf("\"");
  167. if (start > 0 && end > 0)
  168. yield return dumpLine.Substring(start + 1, end - start - 1);
  169. }
  170. }
  171. }
  172. }