[lldb-dev] llvm::to_integer

Zachary Turner via lldb-dev lldb-dev at lists.llvm.org
Mon May 15 10:23:03 PDT 2017


In the past I've advocated for use of StringRef::getAsInteger().  Sometimes
this leads to awkward code, for example if you have a std::string you have
to write StringRef(Str).getAsInteger().

Secondly, the return value is extremely confusing as it returns true on
failure and false on success.

I recently committed a function to llvm/ADT/StringExtras.h called
to_integer which solves both of these problems.  If you have a std::string
S, you can call llvm::to_integer(S) and it just works.  It also inverts the
conditional so it now returns true on success and false on failure, which
should be more intuitive.

As a side benefit, the radix argument is also last instead of first, so it
can take a default value.  So you no longer have to pass 0 for the first
argument to auto-detect.

So I recommend using this function going forward.  At some point in the
future, I may even consider deleting getAsInteger() and all callers with
this function.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-dev/attachments/20170515/214bbea4/attachment.html>


More information about the lldb-dev mailing list