Simplify std::lower_bound with llvm::{bsearch,lower_bound}. NFC

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@364006 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Fangrui Song
2019-06-21 05:40:31 +00:00
parent c49d7cf3b1
commit 32e092ad46
26 changed files with 64 additions and 101 deletions
+3 -8
View File
@@ -95,14 +95,9 @@ unsigned SourceMgr::SrcBuffer::getLineNumber(const char *Ptr) const {
assert(PtrDiff >= 0 && static_cast<size_t>(PtrDiff) <= std::numeric_limits<T>::max());
T PtrOffset = static_cast<T>(PtrDiff);
// std::lower_bound returns the first EOL offset that's not-less-than
// PtrOffset, meaning the EOL that _ends the line_ that PtrOffset is on
// (including if PtrOffset refers to the EOL itself). If there's no such
// EOL, returns end().
auto EOL = std::lower_bound(Offsets->begin(), Offsets->end(), PtrOffset);
// Lines count from 1, so add 1 to the distance from the 0th line.
return (1 + (EOL - Offsets->begin()));
// llvm::lower_bound gives the number of EOL before PtrOffset. Add 1 to get
// the line number.
return llvm::lower_bound(*Offsets, PtrOffset) - Offsets->begin() + 1;
}
SourceMgr::SrcBuffer::SrcBuffer(SourceMgr::SrcBuffer &&Other)