This is an archive of the discontinued LLVM Phabricator instance.

[StringRef] Make getAsInteger a tad more robust.
Needs ReviewPublic

Authored by zturner on Jun 16 2017, 11:09 AM.

Details

Summary
Previously if the auto sense radix was specified as part of
the string and you *also* explicitly specified the radix value
to the function, we would fail.

This patch allows the radix prefix to be properly consumed even
in non-auto-sense mode.

In doing so, I learned that there was zero test coverage for
getAsInteger with an explicitly specified radix, so this patch
also rectifies that since I needed to add tests to make sure
this patch works.

Diff Detail

Event Timeline

zturner created this revision.Jun 16 2017, 11:09 AM
amccarth added inline comments.
llvm/lib/Support/StringRef.cpp
442

You could avoid the duplication of the prefix logic by always calling GetAutoSenseRadix:

const auto SensedRadix = GetAutoSenseRadix(Str);
if (Radix == 0)
  Radix = SensedRadix ;
assert(Radix == SensedRadix);
zturner added inline comments.Jun 16 2017, 12:50 PM
llvm/lib/Support/StringRef.cpp
442
int N;
StringRef("012345").getAsInteger(10, N);

should work, but auto-sense will detect this as base 8.