Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/podio/utilities/MiscHelpers.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 #ifndef PODIO_UTILITIES_MISCHELPERS_H
0002 #define PODIO_UTILITIES_MISCHELPERS_H
0003 
0004 #include <algorithm>
0005 #include <ranges>
0006 #include <string>
0007 #include <string_view>
0008 #include <vector>
0009 
0010 namespace podio::utils {
0011 
0012 ///  Sort the input vector of strings alphabetically, case insensitive.
0013 ///
0014 ///  @param strings The strings that should be sorted alphabetically
0015 ///
0016 ///  @returns A vector of strings sorted alphabetically, case insensitive
0017 inline std::vector<std::string> sortAlphabeticaly(std::vector<std::string> strings) {
0018   // Obviously there is no tolower(std::string) in c++, so this is slightly more
0019   // involved and we make use of the fact that lexicographical_compare works on
0020   // ranges and the fact that we can feed it a dedicated comparison function,
0021   // where we convert the strings to lower case char-by-char. The alternative is
0022   // to make string copies inside the first lambda, transform them to lowercase
0023   // and then use operator< of std::string, which would be effectively
0024   // hand-writing what is happening below.
0025   std::ranges::sort(strings, [](const auto& lhs, const auto& rhs) {
0026     return std::lexicographical_compare(
0027         lhs.begin(), lhs.end(), rhs.begin(), rhs.end(),
0028         [](const auto& cl, const auto& cr) { return std::tolower(cl) < std::tolower(cr); });
0029   });
0030   return strings;
0031 }
0032 
0033 /// Split a string (view) at the delimiter and return a range of views
0034 ///
0035 /// @param str The string to split
0036 /// @param delim The delimeter at which to split
0037 ///
0038 /// @returns A range of views into the original view
0039 inline auto splitString(const std::string_view str, const char delim) {
0040   namespace rv = std::ranges::views;
0041 
0042   return str | rv::split(delim) |
0043 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 12
0044       // gcc 11 is missing a string_view constructor that takes the range
0045       // iterators directly, so we construct from a char* and a size
0046       rv::transform(
0047              [](auto&& subrange) { return std::string_view(&*subrange.begin(), std::ranges::distance(subrange)); });
0048 #else
0049       rv::transform([](auto&& subrange) { return std::string_view(subrange.begin(), subrange.end()); });
0050 #endif
0051 }
0052 
0053 } // namespace podio::utils
0054 
0055 #endif // PODIO_UTILITIES_MISCHELPERS_H