Skip to content

[WIP] Introduce a utility function to transform a path into a URI #1706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/core/uri/include/sourcemeta/core/uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// NOLINTEND(misc-include-cleaner)

#include <cstdint> // std::uint32_t
#include <filesystem> // std::filesystem
#include <istream> // std::istream
#include <memory> // std::unique_ptr
#include <optional> // std::optional
Expand Down Expand Up @@ -437,6 +438,10 @@ class SOURCEMETA_CORE_URI_EXPORT URI {
#endif
};

// @ingroup uri
// TODO: test & document
auto to_uri(const std::filesystem::path &path) -> URI;

} // namespace sourcemeta::core

#endif
26 changes: 25 additions & 1 deletion src/core/uri/uri.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <cstdint> // std::uint32_t
#include <istream> // std::istream
#include <optional> // std::optional
#include <sstream> // std::ostringstream
#include <sstream> // std::ostringstream, std::istringstream
#include <stdexcept> // std::length_error, std::runtime_error
#include <string> // std::stoul, std::string, std::tolower
#include <tuple> // std::tie
Expand Down Expand Up @@ -634,4 +634,28 @@ auto URI::operator<(const URI &other) const noexcept -> bool {
other.path_, other.query_, other.fragment_);
}

auto to_uri(const std::filesystem::path &path) -> URI {
// URI representation of paths is only valid for absolute paths
assert(path.is_absolute());

// Get the path always with forward slashes
// https://en.cppreference.com/w/cpp/filesystem/path/generic_string.html
const auto unix_path{path.generic_string()};
std::ostringstream result;
if (unix_path.empty() || unix_path.front() != '/') {
result << '/';
}

result << unix_path;

// TODO: This whole dance is because we don't
// allow setting a scheme on an existing URI
URI temp{result.str()};
temp.canonicalize();
std::ostringstream final_result;
final_result << "file://";
final_result << temp.recompose();
return URI{final_result.str()};
}

} // namespace sourcemeta::core
1 change: 1 addition & 0 deletions test/uri/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sourcemeta_googletest(NAMESPACE sourcemeta PROJECT core NAME uri
uri_fragment_test.cc
uri_host_test.cc
uri_path_test.cc
uri_path_to_uri_test.cc
uri_parse_test.cc
uri_port_test.cc
uri_scheme_test.cc
Expand Down
16 changes: 16 additions & 0 deletions test/uri/uri_path_to_uri_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <gtest/gtest.h>

#include <sourcemeta/core/uri.h>

TEST(URI_path_to_uri, unix_absolute) {
const std::filesystem::path example{"/foo/bar/baz"};
const auto uri{sourcemeta::core::to_uri(example)};
EXPECT_EQ(uri.recompose(), "file:///foo/bar/baz");
}

TEST(URI_path_to_uri, unix_absolute_other_characters) {
const std::filesystem::path example{"/foo bar/bar?"};
const auto uri{sourcemeta::core::to_uri(example)};
// TODO: Escape
EXPECT_EQ(uri.recompose(), "file:///foo bar/bar?");
}
Loading