Tidy up the code around the memchr(3) calls.
Change-Id: Id06cae00beaebeaa74f59d68a9abc5e39e37ced6
Reviewed-on: https://code-review.googlesource.com/c/re2/+/56650
Reviewed-by: Paul Wankadia <junyer@google.com>
diff --git a/re2/bitstate.cc b/re2/bitstate.cc
index 865b58e..0f7ab47 100644
--- a/re2/bitstate.cc
+++ b/re2/bitstate.cc
@@ -332,14 +332,14 @@
// This looks like it's quadratic in the size of the text,
// but we are not clearing visited_ between calls to TrySearch,
// so no work is duplicated and it ends up still being linear.
- for (const char* p = text.data(); p <= text.data() + text.size(); p++) {
+ const char* etext = text.data() + text.size();
+ for (const char* p = text.data(); p <= etext; p++) {
// Try to use memchr to find the first byte quickly.
- int fb = prog_->first_byte();
- if (fb >= 0 && p < text.data() + text.size() && (p[0] & 0xFF) != fb) {
- p = reinterpret_cast<const char*>(
- memchr(p, fb, text.data() + text.size() - p));
+ int first_byte = prog_->first_byte();
+ if (first_byte >= 0 && p < etext && (p[0] & 0xFF) != first_byte) {
+ p = reinterpret_cast<const char*>(memchr(p, first_byte, etext - p));
if (p == NULL)
- p = text.data() + text.size();
+ p = etext;
}
cap_[0] = p;
diff --git a/re2/dfa.cc b/re2/dfa.cc
index 9e2276e..6581aec 100644
--- a/re2/dfa.cc
+++ b/re2/dfa.cc
@@ -1337,7 +1337,6 @@
swap(p, ep);
}
- int first_byte = prog_->first_byte();
const uint8_t* bytemap = prog_->bytemap();
const uint8_t* lastmatch = NULL; // most recent matching position in text
bool matched = false;
@@ -1374,7 +1373,9 @@
// so use optimized assembly in memchr to skip ahead.
// If first_byte isn't found, we can skip to the end
// of the string.
- if ((p = BytePtr(memchr(p, first_byte, ep - p))) == NULL) {
+ int first_byte = prog_->first_byte();
+ p = BytePtr(memchr(p, first_byte, ep - p));
+ if (p == NULL) {
p = ep;
break;
}
diff --git a/re2/nfa.cc b/re2/nfa.cc
index 6ef918f..8e0ace9 100644
--- a/re2/nfa.cc
+++ b/re2/nfa.cc
@@ -574,13 +574,12 @@
// If there's a required first byte for an unanchored search
// and we're not in the middle of any possible matches,
// use memchr to search for the byte quickly.
- int fb = prog_->first_byte();
+ int first_byte = prog_->first_byte();
if (!anchored && runq->size() == 0 &&
- fb >= 0 && p < etext_ && (p[0] & 0xFF) != fb) {
- p = reinterpret_cast<const char*>(memchr(p, fb, etext_ - p));
- if (p == NULL) {
+ first_byte >= 0 && p < etext_ && (p[0] & 0xFF) != first_byte) {
+ p = reinterpret_cast<const char*>(memchr(p, first_byte, etext_ - p));
+ if (p == NULL)
p = etext_;
- }
}
Thread* t = AllocThread();