mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-10-19 11:02:59 +02:00
[StringExtras] Add llvm::to_integer.
This is a very thin wrapper around StringRef::getAsInteger. It serves three purposes. 1) It allows a cleaner syntax when you have something other than a StringRef - for example, a std::string or an llvm::SmallString. Previously, in this case you would have to write something like: StringRef(MyStr).getAsInteger(0, Result) by explicitly constructing a temporary StringRef. This can be done implicitly however with the new function by just writing: to_integer(MyStr, ...). 2) Correcting the travesty that is getAsInteger's return value. This function returns true on success, and false on failure. While this may cause confusion with people familiar with the getAsInteger API, there seems to be widespread agreement that the return semantics of getAsInteger was a mistake. 3) It allows the Radix to be deduced as a default argument by putting it last in the parameter list. Most uses of getAsInteger pass 0 for the first argument. With this syntax it can just be omitted. llvm-svn: 303011
This commit is contained in:
parent
75f3dd7e94
commit
ccd91ee2f4
@ -106,6 +106,13 @@ static inline std::string fromHex(StringRef Input) {
|
||||
return Output;
|
||||
}
|
||||
|
||||
/// \brief Convert the string \p S to an integer of the specified type using
|
||||
/// the radix \p Base. If \p Base is 0, auto-detects the radix.
|
||||
/// Returns true if the number was successfully converted, false otherwise.
|
||||
template <typename N> bool to_integer(StringRef S, N &Num, unsigned Base = 0) {
|
||||
return !S.getAsInteger(Base, Num);
|
||||
}
|
||||
|
||||
static inline std::string utostr(uint64_t X, bool isNeg = false) {
|
||||
char Buffer[21];
|
||||
char *BufPtr = std::end(Buffer);
|
||||
|
Loading…
Reference in New Issue
Block a user