Allow string::front() and back() and update the codebase to use back().
I searched for "length() - 1]", which won't find all potential sites to use
back(), but should hit a reasonable number. These were all converted.
I don't consider str.front() to be vastly more readable than str[0], so I made
no effort to seek these out and change them. I did change code to use front()
when it would make for better parallel structure with a place I was making use
back(), e.g.:
if (str[0] == 'x' && str[str.length() - 1] == 'y') {
...was transformed to:
if (str.front() == 'x' && str.back() == 'y') {
...and not:
if (str[0] == 'x' && str.back() == 'y') {
I also added front() and back() methods to StringPiece so people wouldn't need to distinguish between string and StringPiece for these purposes.
BUG=none
TEST=none
Review URL: https://codereview.chromium.org/1663253004
Cr-Commit-Position: refs/heads/master@{#373672}
diff --git a/base/strings/string_piece.h b/base/strings/string_piece.h
index 92634b9..20784d7 100644
--- a/base/strings/string_piece.h
+++ b/base/strings/string_piece.h
@@ -224,6 +224,8 @@
}
value_type operator[](size_type i) const { return ptr_[i]; }
+ value_type front() const { return ptr_[0]; }
+ value_type back() const { return ptr_[length_ - 1]; }
void remove_prefix(size_type n) {
ptr_ += n;