2
0

StringUtil.cs 4.5 KB

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