Skip to content

Commit

Permalink
[utils] Separate template declaration/definition
Browse files Browse the repository at this point in the history
For better readability.
  • Loading branch information
ricab committed Oct 27, 2023
1 parent ea907a2 commit 93bbdeb
Showing 1 changed file with 51 additions and 40 deletions.
91 changes: 51 additions & 40 deletions include/multipass/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,52 +101,18 @@ bool valid_mac_address(const std::string& mac);

// string helpers
bool has_only_digits(const std::string& value);

namespace detail
{
auto is_space = static_cast<int (*)(int)>(std::isspace);
}

template <typename Str, typename Filter>
Str&& trim_begin(Str&& s, Filter&& filter)
{
const auto it = std::find_if_not(s.begin(), s.end(), std::forward<Filter>(filter));
s.erase(s.begin(), it);
return std::forward<Str>(s);
}

Str&& trim_begin(Str&& s, Filter&& filter);
template <typename Str>
Str&& trim_begin(Str&& s)
{
return trim_begin(std::forward<Str>(s), detail::is_space);
}

Str&& trim_begin(Str&& s);
template <typename Str, typename Filter>
Str&& trim_end(Str&& s, Filter&& filter)
{
auto rev_it = std::find_if_not(s.rbegin(), s.rend(), std::forward<Filter>(filter));
s.erase(rev_it.base(), s.end());
return std::forward<Str>(s);
}

Str&& trim_end(Str&& s, Filter&& filter);
template <typename Str>
Str&& trim_end(Str&& s)
{
return trim_end(std::forward<Str>(s), detail::is_space);
}

Str&& trim_end(Str&& s);
template <typename Str, typename Filter>
Str&& trim(Str&& s, Filter&& filter)
{
return trim_begin(trim_end(std::forward<Str>(s), std::forward<Filter>(filter)));
}

Str&& trim(Str&& s, Filter&& filter);
template <typename Str>
Str&& trim(Str&& s)
{
return trim(std::forward<Str>(s), detail::is_space);
}

Str&& trim(Str&& s);
std::string& trim_newline(std::string& s);
std::string escape_char(const std::string& s, char c);
std::string escape_for_shell(const std::string& s);
Expand Down Expand Up @@ -274,6 +240,51 @@ class Utils : public Singleton<Utils>
};
} // namespace multipass

namespace multipass::utils::detail
{
inline constexpr auto is_space = static_cast<int (*)(int)>(std::isspace);
}

template <typename Str, typename Filter>
Str&& multipass::utils::trim_begin(Str&& s, Filter&& filter)

Check warning on line 249 in include/multipass/utils.h

View check run for this annotation

Codecov / codecov/patch

include/multipass/utils.h#L249

Added line #L249 was not covered by tests
{
const auto it = std::find_if_not(s.begin(), s.end(), std::forward<Filter>(filter));
s.erase(s.begin(), it);
return std::forward<Str>(s);

Check warning on line 253 in include/multipass/utils.h

View check run for this annotation

Codecov / codecov/patch

include/multipass/utils.h#L251-L253

Added lines #L251 - L253 were not covered by tests
}

template <typename Str>
Str&& multipass::utils::trim_begin(Str&& s)

Check warning on line 257 in include/multipass/utils.h

View check run for this annotation

Codecov / codecov/patch

include/multipass/utils.h#L257

Added line #L257 was not covered by tests
{
return trim_begin(std::forward<Str>(s), detail::is_space);

Check warning on line 259 in include/multipass/utils.h

View check run for this annotation

Codecov / codecov/patch

include/multipass/utils.h#L259

Added line #L259 was not covered by tests
}

template <typename Str, typename Filter>
Str&& multipass::utils::trim_end(Str&& s, Filter&& filter)
{
auto rev_it = std::find_if_not(s.rbegin(), s.rend(), std::forward<Filter>(filter));
s.erase(rev_it.base(), s.end());
return std::forward<Str>(s);
}

template <typename Str>
Str&& multipass::utils::trim_end(Str&& s)
{
return trim_end(std::forward<Str>(s), detail::is_space);
}

template <typename Str, typename Filter>
Str&& multipass::utils::trim(Str&& s, Filter&& filter)

Check warning on line 277 in include/multipass/utils.h

View check run for this annotation

Codecov / codecov/patch

include/multipass/utils.h#L277

Added line #L277 was not covered by tests
{
return trim_begin(trim_end(std::forward<Str>(s), std::forward<Filter>(filter)));

Check warning on line 279 in include/multipass/utils.h

View check run for this annotation

Codecov / codecov/patch

include/multipass/utils.h#L279

Added line #L279 was not covered by tests
}

template <typename Str>
Str&& multipass::utils::trim(Str&& s)

Check warning on line 283 in include/multipass/utils.h

View check run for this annotation

Codecov / codecov/patch

include/multipass/utils.h#L283

Added line #L283 was not covered by tests
{
return trim(std::forward<Str>(s), detail::is_space);

Check warning on line 285 in include/multipass/utils.h

View check run for this annotation

Codecov / codecov/patch

include/multipass/utils.h#L285

Added line #L285 was not covered by tests
}

template <typename OnTimeoutCallable, typename TryAction, typename... Args>
void multipass::utils::try_action_for(OnTimeoutCallable&& on_timeout, std::chrono::milliseconds timeout,
TryAction&& try_action, Args&&... args)
Expand Down

0 comments on commit 93bbdeb

Please sign in to comment.