Skip to content

Commit

Permalink
Use regex
Browse files Browse the repository at this point in the history
Co-authored-by: Vyas Ramasubramani <[email protected]>
  • Loading branch information
madsbk and vyasr authored Oct 11, 2024
1 parent 5f48899 commit 76ad02f
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions cpp/include/kvikio/remote_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,13 @@ class S3Endpoint : public RemoteEndpoint {
*/
[[nodiscard]] static std::pair<std::string, std::string> parse_s3_url(std::string const& s3_url)
{
if (s3_url.empty()) { throw std::invalid_argument("The S3 url cannot be an empty string."); }
if (s3_url.size() < 5 || s3_url.substr(0, 5) != "s3://") {
throw std::invalid_argument("The S3 url must start with the S3 scheme (\"s3://\").");
// Regular expression to match s3://<bucket>/<object>
std::regex pattern{R"(s3://([^/]+)/(.+))"};
std::smatch matches;
if (std::regex_match(s3_url, matches, pattern)) {
return {matches[1].str(), matches[2].str()};
}
std::string p = s3_url.substr(5);
if (p.empty()) { throw std::invalid_argument("The S3 url cannot be an empty string."); }
size_t pos = p.find_first_of('/');
std::string bucket_name = p.substr(0, pos);
if (bucket_name.empty()) {
throw std::invalid_argument("The S3 url does not contain a bucket name.");
}
std::string object_name = (pos == std::string::npos) ? "" : p.substr(pos + 1);
if (object_name.empty()) {
throw std::invalid_argument("The S3 url does not contain an object name.");
}
return std::make_pair(std::move(bucket_name), std::move(object_name));
throw std::invalid_argument("Input string does not match the expected S3 URL format.");
}

/**
Expand Down

0 comments on commit 76ad02f

Please sign in to comment.