optimize StartsWith,EndsWith
This commit is contained in:
parent
f31657640f
commit
fcce11249b
58
include/sled/apply.h
Normal file
58
include/sled/apply.h
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef SLED_APPLY_H
|
||||||
|
#define SLED_APPLY_H
|
||||||
|
#include <functional>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
|
namespace sled {
|
||||||
|
namespace detail {
|
||||||
|
template<int... Seq>
|
||||||
|
struct Sequence {};
|
||||||
|
|
||||||
|
template<int N, int... Seq>
|
||||||
|
struct MakeSeq : MakeSeq<N - 1, N - 1, Seq...> {};
|
||||||
|
|
||||||
|
template<int... Seq>
|
||||||
|
struct MakeSeq<0, Seq...> {
|
||||||
|
using type = Sequence<Seq...>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename ReturnT, typename Func, typename Tuple, int... Seq>
|
||||||
|
ReturnT
|
||||||
|
ApplyImpl(const Func &func, const Tuple &tuple, const Sequence<Seq...> &)
|
||||||
|
{
|
||||||
|
return std::bind(func, std::get<Seq>(tuple)...)();
|
||||||
|
}
|
||||||
|
|
||||||
|
struct VoidTag {};
|
||||||
|
|
||||||
|
}// namespace detail
|
||||||
|
|
||||||
|
template<typename ReturnT,
|
||||||
|
typename Func,
|
||||||
|
typename Tuple,
|
||||||
|
typename std::enable_if<!std::is_void<ReturnT>::value, ReturnT>::type
|
||||||
|
* = nullptr>
|
||||||
|
ReturnT
|
||||||
|
apply(const Func &func, const Tuple &tuple)
|
||||||
|
{
|
||||||
|
return detail::ApplyImpl<ReturnT>(
|
||||||
|
func, tuple,
|
||||||
|
typename detail::MakeSeq<std::tuple_size<Tuple>::value>::type());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename ReturnT = void,
|
||||||
|
typename Func,
|
||||||
|
typename Tuple,
|
||||||
|
typename std::enable_if<std::is_void<ReturnT>::value,
|
||||||
|
detail::VoidTag>::type * = nullptr>
|
||||||
|
void
|
||||||
|
apply(const Func &func, const Tuple &tuple)
|
||||||
|
{
|
||||||
|
detail::ApplyImpl<ReturnT>(
|
||||||
|
func, tuple,
|
||||||
|
typename detail::MakeSeq<std::tuple_size<Tuple>::value>::type());
|
||||||
|
}
|
||||||
|
|
||||||
|
}// namespace sled
|
||||||
|
#endif// SLED_APPLY_H
|
@ -50,22 +50,15 @@ Trim(const std::string &str, const std::string &chars)
|
|||||||
std::string
|
std::string
|
||||||
TrimLeft(const std::string &str, const std::string &chars)
|
TrimLeft(const std::string &str, const std::string &chars)
|
||||||
{
|
{
|
||||||
size_t start = 0;
|
size_t start = str.find_first_not_of(chars);
|
||||||
while (start < str.size()
|
return start == std::string::npos ? "" : str.substr(start);
|
||||||
&& chars.find_first_of(str[start]) != std::string::npos) {
|
|
||||||
++start;
|
|
||||||
}
|
|
||||||
return str.substr(start);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
TrimRight(const std::string &str, const std::string &chars)
|
TrimRight(const std::string &str, const std::string &chars)
|
||||||
{
|
{
|
||||||
size_t end = str.size();
|
size_t end = str.find_last_not_of(chars);
|
||||||
while (end > 0 && chars.find_first_of(str[end - 1]) != std::string::npos) {
|
return end == std::string::npos ? "" : str.substr(0, end + 1);
|
||||||
--end;
|
|
||||||
}
|
|
||||||
return str.substr(0, end);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
Loading…
Reference in New Issue
Block a user