@@ -322,3 +322,51 @@ void setThreadName(const char* name) {
322322 (void )name;
323323 #endif
324324}
325+
326+
327+ // @brief Splits a URI string into its scheme and the rest of the URI.
328+ //
329+ // This function parses a given URI and separates it into two parts:
330+ // 1. The scheme (including the colon and double slashes if present)
331+ // 2. The rest of the URI
332+ //
333+ // The function handles the following cases:
334+ // - URIs with "://" (e.g., "http://", "consul-ini://")
335+ // - URIs with only ":" (e.g., "file:")
336+ // - File URIs with varying numbers of slashes (e.g., "file:", "file:/", "file:///")
337+ // - URIs without a scheme
338+ //
339+ // @param uri The input URI string to be split.
340+ // @return A std::pair where:
341+ // - first: The scheme part of the URI (including ":" or "://")
342+ // - second: The rest of the URI after the scheme
343+ // If no scheme is found, first will be empty and second will contain the entire input string.
344+ //
345+ // @note The function does not validate the URI format beyond identifying the scheme.
346+ // It assumes the input is a well-formed URI string.
347+ //
348+ // @example
349+ // auto [scheme, rest] = splitURI("http://example.com");
350+ // // scheme = "http://", rest = "example.com"
351+ //
352+ // auto [scheme, rest] = splitURI("file:///path/to/file");
353+ // // scheme = "file://", rest = "/path/to/file"
354+ //
355+ // This code was generated with the assistance of GitLab Duo Chat, an AI-powered coding assistant.
356+
357+ std::pair<std::string, std::string> splitURI (const std::string& uri) {
358+ constexpr std::string_view double_slash = " ://" ;
359+ auto double_slash_pos = uri.find (double_slash);
360+ if (double_slash_pos != std::string::npos) {
361+ std::string scheme = uri.substr (0 , double_slash_pos + double_slash.length ());
362+ std::string rest = uri.substr (double_slash_pos + double_slash.length ());
363+ return {std::move (scheme), std::move (rest)};
364+ }
365+ auto single_colon_pos = uri.find (' :' );
366+ if (single_colon_pos != std::string::npos) {
367+ std::string scheme = uri.substr (0 , single_colon_pos + 1 );
368+ std::string rest = uri.substr (single_colon_pos + 1 );
369+ return {std::move (scheme), std::move (rest)};
370+ }
371+ return {" " , uri}; // No scheme found, return empty scheme and the whole string as rest
372+ }
0 commit comments