\brief Encode path \param buffer_ Buffer to encode \param quotes_ Whether to encode quotes
| 113 | /// \param buffer_ Buffer to encode |
| 114 | /// \param quotes_ Whether to encode quotes |
| 115 | std::string encodePath (std::string_view const buffer_, bool const quotes_ = false) |
| 116 | { |
| 117 | // check if the buffer has \n |
| 118 | bool const lf = std::memchr (buffer_.data (), '\n', buffer_.size ()); |
| 119 | |
| 120 | auto end = std::end (buffer_); |
| 121 | |
| 122 | std::size_t numQuotes = 0; |
| 123 | if (quotes_) |
| 124 | { |
| 125 | // check for \" that needs to be encoded |
| 126 | auto p = buffer_.data (); |
| 127 | do |
| 128 | { |
| 129 | p = static_cast<char const *> (std::memchr (p, '"', end - p)); |
| 130 | if (p) |
| 131 | { |
| 132 | ++p; |
| 133 | ++numQuotes; |
| 134 | } |
| 135 | } while (p); |
| 136 | } |
| 137 | |
| 138 | // if nothing needs escaping, return it as-is |
| 139 | if (!lf && !numQuotes) |
| 140 | return std::string (buffer_); |
| 141 | |
| 142 | // reserve output buffer |
| 143 | std::string path (buffer_.size () + numQuotes, '\0'); |
| 144 | auto in = buffer_.data (); |
| 145 | auto out = path.data (); |
| 146 | |
| 147 | // encode into the output buffer |
| 148 | while (in < end) |
| 149 | { |
| 150 | if (*in == '\n') |
| 151 | { |
| 152 | // encoded \n is \0 |
| 153 | *out++ = '\0'; |
| 154 | } |
| 155 | else if (quotes_ && *in == '"') |
| 156 | { |
| 157 | // encoded \" is \"\" |
| 158 | *out++ = '"'; |
| 159 | *out++ = '"'; |
| 160 | } |
| 161 | else |
| 162 | *out++ = *in; |
| 163 | ++in; |
| 164 | } |
| 165 | |
| 166 | return path; |
| 167 | } |
| 168 | |
| 169 | /// \brief Get parent directory name of a path |
| 170 | /// \param path_ Path to get parent of |
no test coverage detected