comparison include/core/strings.h @ 114:ab191e28e69d

*: add initial torrent stuff WOAH! these checkboxes are a pain in my fucking ass
author Paper <mrpapersonic@gmail.com>
date Tue, 07 Nov 2023 08:03:42 -0500
parents 6d8da6e64d61
children 254b1d2b7096
comparison
equal deleted inserted replaced
113:32afe0e940bf 114:ab191e28e69d
2 #define __core__strings_h 2 #define __core__strings_h
3 3
4 #include <string> 4 #include <string>
5 #include <vector> 5 #include <vector>
6 #include <array> 6 #include <array>
7 #include <cstdint>
7 8
8 class QString; 9 class QString;
9 10
10 namespace Strings { 11 namespace Strings {
11
12 template<unsigned...>struct seq{using type=seq;};
13 template<unsigned N, unsigned... Is>
14 struct gen_seq_x : gen_seq_x<N-1, N-1, Is...>{};
15 template<unsigned... Is>
16 struct gen_seq_x<0, Is...> : seq<Is...>{};
17 template<unsigned N>
18 using gen_seq=typename gen_seq_x<N>::type;
19
20 template<size_t S>
21 using size=std::integral_constant<size_t, S>;
22
23 template<class T, size_t N>
24 constexpr size<N> length( T const(&)[N] ) { return {}; }
25 template<class T, size_t N>
26 constexpr size<N> length( std::array<T, N> const& ) { return {}; }
27
28 template<class T>
29 using length_t = decltype(length(std::declval<T>()));
30
31 constexpr size_t string_size() { return 0; }
32 template<class...Ts>
33 constexpr size_t string_size( size_t i, Ts... ts ) {
34 return (i?i-1:0) + string_size(ts...);
35 }
36 template<class...Ts>
37 using string_length=size< string_size( length_t<Ts>{}... )>;
38
39 template<class...Ts>
40 using combined_string = std::array<char, string_length<Ts...>{}+1>;
41
42 template<class Lhs, class Rhs, unsigned...I1, unsigned...I2>
43 constexpr const combined_string<Lhs,Rhs>
44 concat_impl( Lhs const& lhs, Rhs const& rhs, seq<I1...>, seq<I2...>)
45 {
46 return {{ lhs[I1]..., rhs[I2]..., '\0' }};
47 }
48
49 template<class Lhs, class Rhs>
50 constexpr const combined_string<Lhs,Rhs>
51 concat(Lhs const& lhs, Rhs const& rhs)
52 {
53 return concat_impl(lhs, rhs, gen_seq<string_length<Lhs>{}>{}, gen_seq<string_length<Rhs>{}>{});
54 }
55
56 template<class T0, class T1, class... Ts>
57 constexpr const combined_string<T0, T1, Ts...>
58 concat(T0 const&t0, T1 const&t1, Ts const&...ts)
59 {
60 return concat(t0, concat(t1, ts...));
61 }
62
63 template<class T>
64 constexpr const combined_string<T>
65 concat(T const&t) {
66 return concat(t, "");
67 }
68 constexpr const combined_string<>
69 concat() {
70 return concat("");
71 }
72 12
73 /* Implode function: takes a vector of strings and turns it 13 /* Implode function: takes a vector of strings and turns it
74 into a string, separated by delimiters. */ 14 into a string, separated by delimiters. */
75 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter); 15 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter);
76 16
96 QString ToQString(const std::wstring& wstring); 36 QString ToQString(const std::wstring& wstring);
97 37
98 /* arithmetic :) */ 38 /* arithmetic :) */
99 int ToInt(const std::string& str, int def = 0); 39 int ToInt(const std::string& str, int def = 0);
100 40
41 uint64_t HumanReadableSizeToBytes(const std::string& str);
42
43 std::string RemoveLeadingChars(std::string s, const char c);
44 std::string RemoveTrailingChars(std::string s, const char c);
45
101 bool BeginningMatchesSubstring(const std::string& str, const std::string& sub); 46 bool BeginningMatchesSubstring(const std::string& str, const std::string& sub);
102 47
103 }; // namespace Strings 48 }; // namespace Strings
104 49
105 #endif // __core__strings_h 50 #endif // __core__strings_h