Skip to content

Commit 309a404

Browse files
committed
added splitURI() function
1 parent 3b75a2f commit 309a404

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/ReadoutUtils.cxx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

src/ReadoutUtils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include <stdint.h>
1818
#include <string>
1919
#include <vector>
20+
#include <string_view>
21+
#include <utility>
22+
2023

2124
#include "RAWDataHeader.h"
2225

@@ -85,6 +88,9 @@ int numaGetNodeFromAddress(void *ptr, int &node);
8588
// function to set a name for current thread
8689
void setThreadName(const char*name);
8790

91+
// function to split string into URI / path
92+
std::pair<std::string, std::string> splitURI(const std::string& uri);
93+
8894
// end of _READOUTUTILS_H
8995
#endif
9096

0 commit comments

Comments
 (0)