================ @@ -72,12 +74,39 @@ static std::optional<llvm::StringRef> IsWord(llvm::StringRef expr, static bool IsNumberBodyChar(char ch) { return IsDigit(ch) || IsLetter(ch); } -static std::optional<llvm::StringRef> IsNumber(llvm::StringRef expr, - llvm::StringRef &remainder) { - if (IsDigit(remainder[0])) { - llvm::StringRef number = remainder.take_while(IsNumberBodyChar); - remainder = remainder.drop_front(number.size()); - return number; +static std::optional<llvm::StringRef> IsNumber(llvm::StringRef &remainder, + bool &isFloat) { + llvm::StringRef::iterator cur_pos = remainder.begin(); + if (*cur_pos == '.') { + auto next_pos = cur_pos + 1; + if (next_pos == remainder.end() || !IsDigit(*next_pos)) + return std::nullopt; + } + if (IsDigit(*(cur_pos)) || *(cur_pos) == '.') { + while (IsNumberBodyChar(*cur_pos)) + cur_pos++; + + if (*cur_pos == '.') { + isFloat = true; + cur_pos++; + while (IsNumberBodyChar(*cur_pos)) + cur_pos++; ---------------- labath wrote:
Yeah, it doesn't have to be. However, the check you've added here is hardly sufficient. Even simply incrementing the end iterator is UB so a comparison like `cur_pos > remainder.end()` doesn't make sense. A sufficiently smart compiler might even optimize that away. You pretty much need check for EOF every time you increment the iterator. StringRef functions can be unwieldy if you try to implement them like you would do it with iterators/indexes. However, there is often a better (and shorter) way to express that with the high-level methods. So, something like "sequence of numbers, maybe followed by a dot, followed by another sequence of numbers, where one of the sequences is not empty" might be better expressed as "sequence of numbers and dots, which contains at most one dot and at least one number", which you can write as: ``` StringRef number = input.take_while([](char c){ return IsNumberBodyChar(c) || c == '.'; } ); size_t dots = number.count('.'); if (dots > 1|| dots == number.size()) return bad_number; ``` https://github.com/llvm/llvm-project/pull/152308 _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits