summaryrefslogtreecommitdiff
path: root/src/ranges.hh
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2017-12-04 14:27:54 +0800
committerMaxime Coste <mawww@kakoune.org>2017-12-04 15:19:57 +0800
commit274367116a57c5788cc4b30861dcd94f54fba60e (patch)
tree0a0f611802cfc3ea50f2590640256f85467742c1 /src/ranges.hh
parent7d32b3fc3689219d47fc236eefa4749f2e7c3c61 (diff)
Replace uses of getpwuid which is incompatible with static linking
Introduce a get_user_name function which parses '/etc/passwd' to find the username associated with a user id.
Diffstat (limited to 'src/ranges.hh')
-rw-r--r--src/ranges.hh17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/ranges.hh b/src/ranges.hh
index fa23f180..599396dc 100644
--- a/src/ranges.hh
+++ b/src/ranges.hh
@@ -6,6 +6,8 @@
#include <iterator>
#include <numeric>
+#include "constexpr_utils.hh"
+
namespace Kakoune
{
@@ -337,6 +339,21 @@ auto gather()
});
}
+template<typename ExceptionType, size_t... Indexes>
+auto elements()
+{
+ return make_view_factory([] (auto&& range) {
+ using std::begin; using std::end;
+ auto elem = [it = begin(range), end = end(range), i = 0u](size_t index) mutable {
+ for (; i < index; ++i, ++it)
+ if (it == end) throw ExceptionType{i};
+ return *it;
+ };
+ // Note that initializer lists elements are guaranteed to be sequenced
+ return Array<std::decay_t<decltype(*begin(range))>, sizeof...(Indexes)>{{elem(Indexes)...}};
+ });
+}
+
}
#endif // ranges_hh_INCLUDED