| 40 | } |
| 41 | |
| 42 | std::string Slice::ToDebugString(size_t max_len) const { |
| 43 | size_t bytes_to_print = size_; |
| 44 | bool abbreviated = false; |
| 45 | if (max_len != 0 && bytes_to_print > max_len) { |
| 46 | bytes_to_print = max_len; |
| 47 | abbreviated = true; |
| 48 | } |
| 49 | |
| 50 | int size = 0; |
| 51 | for (int i = 0; i < bytes_to_print; i++) { |
| 52 | if (!isgraph(data_[i])) { |
| 53 | size += 4; |
| 54 | } else { |
| 55 | size++; |
| 56 | } |
| 57 | } |
| 58 | if (abbreviated) { |
| 59 | size += 20; // extra padding |
| 60 | } |
| 61 | |
| 62 | std::string ret; |
| 63 | ret.reserve(size); |
| 64 | for (int i = 0; i < bytes_to_print; i++) { |
| 65 | if (!isgraph(data_[i])) { |
| 66 | StringAppendF(&ret, "\\x%02x", data_[i] & 0xff); |
| 67 | } else { |
| 68 | ret.push_back(data_[i]); |
| 69 | } |
| 70 | } |
| 71 | if (abbreviated) { |
| 72 | StringAppendF(&ret, "...<%zd bytes total>", size_); |
| 73 | } |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | bool IsAllZeros(const Slice& s) { |
| 78 | // Walk a pointer through the slice instead of using s[i] |