<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Feb 15, 2011, at 12:51 PM, John Thompson wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Hi,</div>
<div> </div>
<div>Given a QualType that has a class, struct, or enum name in there somewhere, I'd like to get a string that just contains the leaf type name, and a string that contains just the qualified type name.</div>
<div> </div>
<div>For example, given a QualType whose getAsString() call returns "class namespace::classname *&", I'd like to get a string like "classname" and a string like "namespace::classname".</div>

<div> </div>
<div>At present I'm doing it very naively like this:</div>
<div> </div>
<div><font face="courier new,monospace">//...<br>std::string baseName = LuaBindingsConsumer::StripToBaseName(type.GetAsString());<br>std::string qualifiedBaseName = LuaBindingsConsumer::StripToQualifiedName(type.GetAsString());<br>
//...</font></div>
<div><br><font face="courier new,monospace">  // Strip a type string of everything but the base type name.<br>std::string LuaBindingsConsumer::StripToBaseName(std::string typeName) {<br>  typeName = StripToQualifiedName(typeName);<br>
  size_t index;<br>  while ((index = typeName.find_first_of("::")) < typeName.length())<br>    typeName = typeName.substr(index + 2);<br>  return typeName;<br>}</font></div>
<div><font face="courier new,monospace">  // Strip a type string of everything but the qualified base type name.<br>std::string LuaBindingsConsumer::StripToQualifiedName(std::string typeName) {<br>  size_t index;<br>  if (typeName.compare(0, 6, "class ") == 0)<br>
    typeName = typeName.substr(6);<br>  else if (typeName.compare(0, 7, "struct ") == 0)<br>    typeName = typeName.substr(7);<br>  else if (typeName.compare(0, 5, "enum ") == 0)<br>    typeName = typeName.substr(5);<br>
  while ((index = typeName.find_first_of('*')) < typeName.length())<br>    typeName = typeName.substr(0, index);<br>  while ((index = typeName.find_first_of('&')) < typeName.length())<br>    typeName = typeName.substr(0, index);<br>
  while ((index = typeName.find_first_of('[')) < typeName.length())<br>    typeName = typeName.substr(0, index);<br>  while ((index = typeName.find_first_of('{')) < typeName.length())<br>    typeName = typeName.substr(0, index);<br>
  while ((index = typeName.find_first_of(' ')) < typeName.length())<br>    typeName = typeName.substr(0, index);<br>  while ((index = typeName.find_first_of('\t')) < typeName.length())<br>    typeName = typeName.substr(0, index);<br>
  return typeName;<br>}<br><br clear="all"></font>but there must be a better way.</div>
</blockquote><br></div><div>There's no reason to view this as a string-manipulation problem. Just look at the structure of the type and strip off all of the information you don't care about. For example, match PointerType nodes and pull out the pointee, match ArrayType nodes and pull out the element type, etc.</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">  </span>- Doug</div><br></body></html>