[PATCH] D24778: Add `StringRef::consumeInteger`

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 20 12:50:25 PDT 2016


zturner created this revision.
zturner added reviewers: beanz, mehdi_amini.
zturner added a subscriber: llvm-commits.

`StringRef::getAsInteger()` is our answer to the C-apis `strtoll`, `strtoull`, etc.  Those functions have a signature that looks like this:

```
unsigned long long int strtoull (const char* str, char** endptr, int radix)
```

The second argument is (optionally) used to return the location in the input string where the returned number stopped.  So, for example if you had a string such as `"23908234 Integers", then you could do the following:

```
const char * str = "23908234 Integers";
unsigned result = strtoul(str, &str, 10);
// str == " Integers";
```

However, regardless of the second argument, these functions only assume that the input string //begins// with a number, whereas our `getAsInteger` functions consider it a failure unless the entire string is a number.

LLDB depends on the ability to extract integers from the beginning of strings quite commonly, and there is no good solution to this in LLVM presently.  So I'm introducing the `consumeInteger()` function which consumes a number from the beginning of the string, failing only if the string does not BEGIN with a valid number of the specified radix, and upon success modifying the original `StringRef` to chop off the consumed portion.  This is similar in spirit to how the `consume_front()` and `consume_back()` functions work.

`getAsInteger()` is updated to internally use the `consume` functions so that the logic is not duplicated, and unit tests are added for `consume` behavior.

https://reviews.llvm.org/D24778

Files:
  include/llvm/ADT/StringRef.h
  lib/Support/StringRef.cpp
  unittests/ADT/StringRefTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24778.71968.patch
Type: text/x-patch
Size: 12372 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160920/e04db9bd/attachment.bin>


More information about the llvm-commits mailing list