| 324 | } |
| 325 | |
| 326 | Result<std::string> UriFromAbsolutePath(std::string_view path) { |
| 327 | if (path.empty()) { |
| 328 | return Status::Invalid( |
| 329 | "UriFromAbsolutePath expected an absolute path, got an empty string"); |
| 330 | } |
| 331 | std::string out; |
| 332 | #ifdef _WIN32 |
| 333 | // Turn "/" separators into "\", as Windows recognizes both but uriparser |
| 334 | // only the latter. |
| 335 | std::string fixed_path(path); |
| 336 | std::replace(fixed_path.begin(), fixed_path.end(), '/', '\\'); |
| 337 | out.resize(8 + 3 * fixed_path.length() + 1); |
| 338 | int r = uriWindowsFilenameToUriStringA(fixed_path.data(), out.data()); |
| 339 | // uriWindowsFilenameToUriStringA basically only fails if a null pointer is given. |
| 340 | ARROW_CHECK_EQ(r, 0) << "uriWindowsFilenameToUriStringA unexpectedly failed"; |
| 341 | #else |
| 342 | // uriUnixFilenameToUriStringA scans its argument as a NUL-terminated C string, |
| 343 | // but a std::string_view is not required to be NUL-terminated. Copy into a |
| 344 | // std::string first (as the Windows branch above already does) so the routine |
| 345 | // cannot read past the end of the view. |
| 346 | std::string fixed_path(path); |
| 347 | out.resize(7 + 3 * fixed_path.length() + 1); |
| 348 | int r = uriUnixFilenameToUriStringA(fixed_path.data(), out.data()); |
| 349 | // same as above (uriWindowsFilenameToUriStringA) |
| 350 | ARROW_CHECK_EQ(r, 0) << "uriUnixFilenameToUriStringA unexpectedly failed"; |
| 351 | #endif |
| 352 | out.resize(strlen(out.data())); |
| 353 | return out; |
| 354 | } |
| 355 | |
| 356 | } // namespace arrow::util |