Skip to content

Commit

Permalink
Check if empty() instead of comparing size() with 0.
Browse files Browse the repository at this point in the history
Change-Id: Ie5d85102b82dcf1eee18a455eb5b8ac671b54277
Reviewed-on: https://code-review.googlesource.com/c/re2/+/48850
Reviewed-by: Paul Wankadia <[email protected]>
  • Loading branch information
junyer committed Nov 25, 2019
1 parent af34559 commit 4f6e130
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
62 changes: 31 additions & 31 deletions re2/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1323,14 +1323,14 @@ bool Regexp::ParseState::MaybeConcatString(int r, ParseFlags flags) {
// Parses a decimal integer, storing it in *np.
// Sets *s to span the remainder of the string.
static bool ParseInteger(StringPiece* s, int* np) {
if (s->size() == 0 || !isdigit((*s)[0] & 0xFF))
if (s->empty() || !isdigit((*s)[0] & 0xFF))
return false;
// Disallow leading zeros.
if (s->size() >= 2 && (*s)[0] == '0' && isdigit((*s)[1] & 0xFF))
return false;
int n = 0;
int c;
while (s->size() > 0 && isdigit(c = (*s)[0] & 0xFF)) {
while (!s->empty() && isdigit(c = (*s)[0] & 0xFF)) {
// Avoid overflow.
if (n >= 100000000)
return false;
Expand All @@ -1352,16 +1352,16 @@ static bool ParseInteger(StringPiece* s, int* np) {
// s must NOT be edited unless MaybeParseRepetition returns true.
static bool MaybeParseRepetition(StringPiece* sp, int* lo, int* hi) {
StringPiece s = *sp;
if (s.size() == 0 || s[0] != '{')
if (s.empty() || s[0] != '{')
return false;
s.remove_prefix(1); // '{'
if (!ParseInteger(&s, lo))
return false;
if (s.size() == 0)
if (s.empty())
return false;
if (s[0] == ',') {
s.remove_prefix(1); // ','
if (s.size() == 0)
if (s.empty())
return false;
if (s[0] == '}') {
// {2,} means at least 2
Expand All @@ -1375,7 +1375,7 @@ static bool MaybeParseRepetition(StringPiece* sp, int* lo, int* hi) {
// {2} means exactly two
*hi = *lo;
}
if (s.size() == 0 || s[0] != '}')
if (s.empty() || s[0] != '}')
return false;
s.remove_prefix(1); // '}'
*sp = s;
Expand Down Expand Up @@ -1416,7 +1416,7 @@ static int StringPieceToRune(Rune *r, StringPiece *sp, RegexpStatus* status) {
static bool IsValidUTF8(const StringPiece& s, RegexpStatus* status) {
StringPiece t = s;
Rune r;
while (t.size() > 0) {
while (!t.empty()) {
if (StringPieceToRune(&r, &t, status) < 0)
return false;
}
Expand Down Expand Up @@ -1448,13 +1448,13 @@ static int UnHex(int c) {
static bool ParseEscape(StringPiece* s, Rune* rp,
RegexpStatus* status, int rune_max) {
const char* begin = s->data();
if (s->size() < 1 || (*s)[0] != '\\') {
if (s->empty() || (*s)[0] != '\\') {
// Should not happen - caller always checks.
status->set_code(kRegexpInternalError);
status->set_error_arg(StringPiece());
return false;
}
if (s->size() < 2) {
if (s->size() == 1) {
status->set_code(kRegexpTrailingBackslash);
status->set_error_arg(StringPiece());
return false;
Expand Down Expand Up @@ -1485,16 +1485,16 @@ static bool ParseEscape(StringPiece* s, Rune* rp,
case '6':
case '7':
// Single non-zero octal digit is a backreference; not supported.
if (s->size() == 0 || (*s)[0] < '0' || (*s)[0] > '7')
if (s->empty() || (*s)[0] < '0' || (*s)[0] > '7')
goto BadEscape;
FALLTHROUGH_INTENDED;
case '0':
// consume up to three octal digits; already have one.
code = c - '0';
if (s->size() > 0 && '0' <= (c = (*s)[0]) && c <= '7') {
if (!s->empty() && '0' <= (c = (*s)[0]) && c <= '7') {
code = code * 8 + c - '0';
s->remove_prefix(1); // digit
if (s->size() > 0) {
if (!s->empty()) {
c = (*s)[0];
if ('0' <= c && c <= '7') {
code = code * 8 + c - '0';
Expand All @@ -1509,7 +1509,7 @@ static bool ParseEscape(StringPiece* s, Rune* rp,

// Hexadecimal escapes
case 'x':
if (s->size() == 0)
if (s->empty())
goto BadEscape;
if (StringPieceToRune(&c, s, status) < 0)
return false;
Expand All @@ -1529,7 +1529,7 @@ static bool ParseEscape(StringPiece* s, Rune* rp,
code = code * 16 + UnHex(c);
if (code > rune_max)
goto BadEscape;
if (s->size() == 0)
if (s->empty())
goto BadEscape;
if (StringPieceToRune(&c, s, status) < 0)
return false;
Expand All @@ -1540,7 +1540,7 @@ static bool ParseEscape(StringPiece* s, Rune* rp,
return true;
}
// Easy case: two hex digits.
if (s->size() == 0)
if (s->empty())
goto BadEscape;
if (StringPieceToRune(&c1, s, status) < 0)
return false;
Expand Down Expand Up @@ -1771,7 +1771,7 @@ ParseStatus ParseUnicodeGroup(StringPiece* s, Regexp::ParseFlags parse_flags,
// Chop seq where s now begins.
seq = StringPiece(seq.data(), static_cast<size_t>(s->data() - seq.data()));

if (name.size() > 0 && name[0] == '^') {
if (!name.empty() && name[0] == '^') {
sign = -sign;
name.remove_prefix(1); // '^'
}
Expand Down Expand Up @@ -1858,15 +1858,15 @@ static ParseStatus ParseCCName(StringPiece* s, Regexp::ParseFlags parse_flags,
bool Regexp::ParseState::ParseCCCharacter(StringPiece* s, Rune *rp,
const StringPiece& whole_class,
RegexpStatus* status) {
if (s->size() == 0) {
if (s->empty()) {
status->set_code(kRegexpMissingBracket);
status->set_error_arg(whole_class);
return false;
}

// Allow regular escape sequences even though
// many need not be escaped in this context.
if (s->size() >= 1 && (*s)[0] == '\\')
if ((*s)[0] == '\\')
return ParseEscape(s, rp, status, rune_max_);

// Otherwise take the next rune.
Expand Down Expand Up @@ -1908,7 +1908,7 @@ bool Regexp::ParseState::ParseCharClass(StringPiece* s,
Regexp** out_re,
RegexpStatus* status) {
StringPiece whole_class = *s;
if (s->size() == 0 || (*s)[0] != '[') {
if (s->empty() || (*s)[0] != '[') {
// Caller checked this.
status->set_code(kRegexpInternalError);
status->set_error_arg(StringPiece());
Expand All @@ -1918,7 +1918,7 @@ bool Regexp::ParseState::ParseCharClass(StringPiece* s,
Regexp* re = new Regexp(kRegexpCharClass, flags_ & ~FoldCase);
re->ccb_ = new CharClassBuilder;
s->remove_prefix(1); // '['
if (s->size() > 0 && (*s)[0] == '^') {
if (!s->empty() && (*s)[0] == '^') {
s->remove_prefix(1); // '^'
negated = true;
if (!(flags_ & ClassNL) || (flags_ & NeverNL)) {
Expand All @@ -1928,7 +1928,7 @@ bool Regexp::ParseState::ParseCharClass(StringPiece* s,
}
}
bool first = true; // ] is okay as first char in class
while (s->size() > 0 && ((*s)[0] != ']' || first)) {
while (!s->empty() && ((*s)[0] != ']' || first)) {
// - is only okay unescaped as first or last in class.
// Except that Perl allows - anywhere.
if ((*s)[0] == '-' && !first && !(flags_&PerlX) &&
Expand Down Expand Up @@ -1996,7 +1996,7 @@ bool Regexp::ParseState::ParseCharClass(StringPiece* s,
// in the flags.
re->ccb_->AddRangeFlags(rr.lo, rr.hi, flags_ | Regexp::ClassNL);
}
if (s->size() == 0) {
if (s->empty()) {
status->set_code(kRegexpMissingBracket);
status->set_error_arg(whole_class);
re->Decref();
Expand All @@ -2016,7 +2016,7 @@ bool Regexp::ParseState::ParseCharClass(StringPiece* s,
// Python rejects names starting with digits.
// We don't enforce either of those.
static bool IsValidCaptureName(const StringPiece& name) {
if (name.size() == 0)
if (name.empty())
return false;
for (size_t i = 0; i < name.size(); i++) {
int c = name[i];
Expand Down Expand Up @@ -2099,7 +2099,7 @@ bool Regexp::ParseState::ParsePerlFlags(StringPiece* s) {
int nflags = flags_;
Rune c;
for (bool done = false; !done; ) {
if (t.size() == 0)
if (t.empty())
goto BadPerlOp;
if (StringPieceToRune(&c, &t, status_) < 0)
return false;
Expand Down Expand Up @@ -2217,7 +2217,7 @@ Regexp* Regexp::Parse(const StringPiece& s, ParseFlags global_flags,

if (global_flags & Literal) {
// Special parse loop for literal string.
while (t.size() > 0) {
while (!t.empty()) {
Rune r;
if (StringPieceToRune(&r, &t, status) < 0)
return NULL;
Expand All @@ -2228,7 +2228,7 @@ Regexp* Regexp::Parse(const StringPiece& s, ParseFlags global_flags,
}

StringPiece lastunary = StringPiece();
while (t.size() > 0) {
while (!t.empty()) {
StringPiece isunary = StringPiece();
switch (t[0]) {
default: {
Expand Down Expand Up @@ -2312,11 +2312,11 @@ Regexp* Regexp::Parse(const StringPiece& s, ParseFlags global_flags,
bool nongreedy = false;
t.remove_prefix(1); // '*' or '+' or '?'
if (ps.flags() & PerlX) {
if (t.size() > 0 && t[0] == '?') {
if (!t.empty() && t[0] == '?') {
nongreedy = true;
t.remove_prefix(1); // '?'
}
if (lastunary.size() > 0) {
if (!lastunary.empty()) {
// In Perl it is not allowed to stack repetition operators:
// a** is a syntax error, not a double-star.
// (and a++ means something else entirely, which we don't support!)
Expand Down Expand Up @@ -2347,11 +2347,11 @@ Regexp* Regexp::Parse(const StringPiece& s, ParseFlags global_flags,
}
bool nongreedy = false;
if (ps.flags() & PerlX) {
if (t.size() > 0 && t[0] == '?') {
if (!t.empty() && t[0] == '?') {
nongreedy = true;
t.remove_prefix(1); // '?'
}
if (lastunary.size() > 0) {
if (!lastunary.empty()) {
// Not allowed to stack repetition operators.
status->set_code(kRegexpRepeatOp);
status->set_error_arg(StringPiece(
Expand Down Expand Up @@ -2405,7 +2405,7 @@ Regexp* Regexp::Parse(const StringPiece& s, ParseFlags global_flags,

if (t[1] == 'Q') { // \Q ... \E: the ... is always literals
t.remove_prefix(2); // '\\', 'Q'
while (t.size() > 0) {
while (!t.empty()) {
if (t.size() >= 2 && t[0] == '\\' && t[1] == 'E') {
t.remove_prefix(2); // '\\', 'E'
break;
Expand Down
4 changes: 2 additions & 2 deletions re2/re2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ int RE2::GlobalReplace(std::string* str,
break;
if (p < vec[0].data())
out.append(p, vec[0].data() - p);
if (vec[0].data() == lastend && vec[0].size() == 0) {
if (vec[0].data() == lastend && vec[0].empty()) {
// Disallow empty match at end of last match: skip ahead.
//
// fullrune() takes int, not ptrdiff_t. However, it just looks
Expand Down Expand Up @@ -934,7 +934,7 @@ bool RE2::Rewrite(std::string* out,
return false;
}
StringPiece snip = vec[n];
if (snip.size() > 0)
if (!snip.empty())
out->append(snip.data(), snip.size());
} else if (c == '\\') {
out->push_back('\\');
Expand Down
2 changes: 1 addition & 1 deletion re2/testing/regexp_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ std::vector<std::string> Explode(const StringPiece& s) {
std::vector<std::string> Split(const StringPiece& sep, const StringPiece& s) {
std::vector<std::string> v;

if (sep.size() == 0)
if (sep.empty())
return Explode(s);

const char *p = s.data();
Expand Down
2 changes: 1 addition & 1 deletion re2/testing/tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ static Prog::Anchor anchors[] = {

bool Tester::TestInput(const StringPiece& text) {
bool okay = TestInputInContext(text, text);
if (text.size() > 0) {
if (!text.empty()) {
StringPiece sp;
sp = text;
sp.remove_prefix(1);
Expand Down

0 comments on commit 4f6e130

Please sign in to comment.