7 #ifndef _FCITX_UTILS_STRINGUTILS_DETAIL_H_ 8 #define _FCITX_UTILS_STRINGUTILS_DETAIL_H_ 15 #include <initializer_list> 17 #include <string_view> 18 #include <type_traits> 20 #include <fcitx-utils/fcitxutils_export.h> 26 static const T &forward(
const T &t) {
return t; }
29 template <
typename T,
size_t TN>
31 static std::pair<const char *, std::size_t> forward(
const T (&t)[TN]) {
32 static_assert(TN > 0,
"No char array but only literal");
39 static std::pair<const char *, std::size_t> forward(T *t) {
46 static std::pair<const char *, std::size_t> forward(std::string_view t) {
47 return {t.data(), t.size()};
54 : piece_(p.first), size_(p.second) {}
57 typename = std::enable_if_t<std::is_same<T, char>::value>>
61 UniversalPiece(
const std::string &str)
62 : piece_(str.data()), size_(str.size()) {}
64 UniversalPiece(
char c) =
delete;
65 #define UNIVERSAL_PIECE_NUMERIC_CONVERSION(TYPE, FORMAT_STRING) \ 66 UniversalPiece(TYPE i) : piece_(buffer_) { \ 67 auto size = snprintf(buffer_, sizeof(buffer_), FORMAT_STRING, i); \ 68 assert(size >= 0 && static_cast<size_t>(size) + 1 <= sizeof(buffer_)); \ 71 UNIVERSAL_PIECE_NUMERIC_CONVERSION(
int,
"%d");
72 UNIVERSAL_PIECE_NUMERIC_CONVERSION(
unsigned int,
"%u");
73 UNIVERSAL_PIECE_NUMERIC_CONVERSION(
long,
"%ld");
74 UNIVERSAL_PIECE_NUMERIC_CONVERSION(
unsigned long,
"%lu");
75 UNIVERSAL_PIECE_NUMERIC_CONVERSION(
long long,
"%lld");
76 UNIVERSAL_PIECE_NUMERIC_CONVERSION(
unsigned long long,
"%llu");
77 UNIVERSAL_PIECE_NUMERIC_CONVERSION(
float,
"%f");
78 UNIVERSAL_PIECE_NUMERIC_CONVERSION(
double,
"%lf");
80 UniversalPiece(
const UniversalPiece &) =
delete;
82 const char *piece()
const {
return piece_; }
83 std::size_t size()
const {
return size_; }
85 std::pair<const char *, std::size_t> toPair()
const {
86 return {piece_, size_};
89 std::pair<const char *, std::size_t>
90 toPathPair(
const bool removePrefixSlash =
true)
const {
91 const auto *piece = piece_;
94 if (removePrefixSlash) {
95 while (size && piece[0] ==
'/') {
100 while (size && piece[size - 1] ==
'/') {
104 if (size_ && !removePrefixSlash && !size) {
105 return {piece_, size_};
109 return {piece, size};
113 static constexpr
int IntegerBufferSize = 30;
116 char buffer_[IntegerBufferSize];
119 FCITXUTILS_EXPORT std::string
120 concatPieces(std::initializer_list<std::pair<const char *, std::size_t>> list);
122 FCITXUTILS_EXPORT std::string concatPathPieces(
123 std::initializer_list<std::pair<const char *, std::size_t>> list);
127 #endif // _FCITX_UTILS_STRINGUTILS_DETAIL_H_ size_t length(Iter start, Iter end)
Return the number UTF-8 characters in the string iterator range.