Returns true when following conditions are met: - is a suffix of . - No other unescaped occurrence of inside (apart from being a suffix). Returns false otherwise. If is non-NULL, returns an error message in . If is non-NULL, returns the offset in that corresponds to the location of the error.
| 51 | // <error>. If <error_offset> is non-NULL, returns the offset in <source> that |
| 52 | // corresponds to the location of the error. |
| 53 | bool CheckForClosingString(absl::string_view source, |
| 54 | absl::string_view closing_str, std::string* error) { |
| 55 | if (closing_str.empty()) return true; |
| 56 | |
| 57 | const char* p = source.data(); |
| 58 | const char* end = p + source.size(); |
| 59 | |
| 60 | bool is_closed = false; |
| 61 | while (p + closing_str.length() <= end) { |
| 62 | if (*p != '\\') { |
| 63 | size_t cur_pos = p - source.data(); |
| 64 | bool is_closing = |
| 65 | absl::StartsWith(absl::ClippedSubstr(source, cur_pos), closing_str); |
| 66 | if (is_closing && p + closing_str.length() < end) { |
| 67 | if (error) { |
| 68 | *error = |
| 69 | absl::StrCat("String cannot contain unescaped ", closing_str); |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | is_closed = is_closing && (p + closing_str.length() == end); |
| 74 | } else { |
| 75 | p++; // Read past the escaped character. |
| 76 | } |
| 77 | p++; |
| 78 | } |
| 79 | |
| 80 | if (!is_closed) { |
| 81 | if (error) { |
| 82 | *error = absl::StrCat("String must end with ", closing_str); |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | // ---------------------------------------------------------------------- |
| 91 | // CUnescapeInternal() |
no test coverage detected