| 35 | } |
| 36 | |
| 37 | Status TruncateUp(const string& str, int32_t max_length, string* result) { |
| 38 | DCHECK(result != nullptr); |
| 39 | if (str.length() <= max_length) { |
| 40 | *result = str; |
| 41 | return Status::OK(); |
| 42 | } |
| 43 | |
| 44 | *result = str.substr(0, max_length); |
| 45 | int i = max_length - 1; |
| 46 | while (i > 0 && static_cast<int32_t>((*result)[i]) == -1) { |
| 47 | (*result)[i] += 1; |
| 48 | --i; |
| 49 | } |
| 50 | // We convert it to unsigned because signed overflow results in undefined behavior. |
| 51 | unsigned char uch = static_cast<unsigned char>((*result)[i]); |
| 52 | uch += 1; |
| 53 | (*result)[i] = uch; |
| 54 | if (i == 0 && (*result)[i] == 0) { |
| 55 | return Status("TruncateUp() couldn't increase string."); |
| 56 | } |
| 57 | result->resize(i + 1); |
| 58 | return Status::OK(); |
| 59 | } |
| 60 | |
| 61 | bool CommaSeparatedContains(const std::string& cs_list, const std::string& item) { |
| 62 | size_t pos = 0; |