This large function is defined inline so that in a fairly common case where one of the arguments is a literal, the compiler can elide a lot of the following comparisons.
| 222 | // one of the arguments is a literal, the compiler can elide a lot of the |
| 223 | // following comparisons. |
| 224 | inline bool operator==(const StringPiece& x, const StringPiece& y) { |
| 225 | StringPiece::size_type len = x.size(); |
| 226 | if (len != y.size()) { |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | const char* p1 = x.data(); |
| 231 | const char* p2 = y.data(); |
| 232 | if (p1 == p2) { |
| 233 | return true; |
| 234 | } |
| 235 | if (len == 0) { |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | // Test last byte in case strings share large common prefix |
| 240 | if (p1[len-1] != p2[len-1]) return false; |
| 241 | if (len == 1) return true; |
| 242 | |
| 243 | // At this point we can, but don't have to, ignore the last byte. We use |
| 244 | // this observation to fold the odd-length case into the even-length case. |
| 245 | len &= ~1; |
| 246 | |
| 247 | return memcmp(p1, p2, len) == 0; |
| 248 | } |
| 249 | |
| 250 | inline bool operator==(const StringPiece& x, const char* y) { |
| 251 | if (y == nullptr) { |