Commit 410e03e7 authored by Kjell Ahlstedt's avatar Kjell Ahlstedt

Merge branch 'wip/dboles/ustring_format_fold_and_test' into 'master'

ustring: Simplify format() using C++17 fold expression, test output, Stringify cleanups

See merge request !4
parents 9a5acb61 25846ce1
......@@ -740,12 +740,6 @@ private:
class FormatStream;
template<class T>
static inline void format_private(FormatStream& buf, const T& arg);
template<class T1, class... Ts>
static inline void format_private(FormatStream& buf, const T1& a1, const Ts&... args);
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
std::string string_;
......@@ -1059,30 +1053,13 @@ ustring::raw() const
return string_;
}
template <class T>
inline // static
void
ustring::format_private(FormatStream& buf, const T& arg)
{
buf.stream(arg);
}
template <class T1, class... Ts>
inline // static
void
ustring::format_private(FormatStream& buf, const T1& a1, const Ts&... args)
{
buf.stream(a1);
return format_private(buf, args...);
}
template <class... Ts>
inline // static
ustring
ustring::format(const Ts&... args)
{
ustring::FormatStream buf;
format_private(buf, args...);
(buf.stream(args), ...);
return buf.to_string();
}
......@@ -1092,19 +1069,16 @@ template <class T>
class ustring::Stringify
{
private:
ustring string_;
const ustring string_;
public:
explicit inline Stringify(const T& arg) : string_(ustring::format(arg)) {}
// TODO: Why is this here? See the template specialization:
explicit inline Stringify(const char* arg) : string_(arg) {}
// noncopyable
Stringify(const ustring::Stringify<T>&) = delete;
Stringify<T>& operator=(const ustring::Stringify<T>&) = delete;
inline const ustring* ptr() const { return &string_; }
inline const ustring& ref() const { return string_; }
};
/// A template specialization for Stringify<ustring>:
......@@ -1121,7 +1095,7 @@ public:
Stringify(const ustring::Stringify<ustring>&) = delete;
Stringify<ustring>& operator=(const ustring::Stringify<ustring>&) = delete;
inline const ustring* ptr() const { return &string_; }
inline const ustring& ref() const { return string_; }
};
/** A template specialization for Stringify<const char*>,
......@@ -1140,7 +1114,7 @@ public:
Stringify(const ustring::Stringify<const char*>&) = delete;
Stringify<ustring>& operator=(const ustring::Stringify<const char*>&) = delete;
inline const ustring* ptr() const { return &string_; }
inline const ustring& ref() const { return string_; }
};
/** A template specialization for Stringify<char[N]> (for string literals),
......@@ -1159,7 +1133,7 @@ public:
Stringify(const ustring::Stringify<char[N]>&) = delete;
Stringify<ustring>& operator=(const ustring::Stringify<char[N]>&) = delete;
inline const ustring* ptr() const { return &string_; }
inline const ustring& ref() const { return string_; }
};
/** A template specialization for Stringify<const char[N]> (for string literals),
......@@ -1179,7 +1153,7 @@ public:
Stringify(const ustring::Stringify<const char[N]>&) = delete;
Stringify<ustring>& operator=(const ustring::Stringify<const char[N]>&) = delete;
inline const ustring* ptr() const { return &string_; }
inline const ustring& ref() const { return string_; }
};
inline // static
......@@ -1197,7 +1171,7 @@ inline // static
static_assert(sizeof...(Ts) <= 9,
"ustring::compose only supports up to 9 placeholders.");
return compose_private(fmt, { Stringify<Ts>(args).ptr()... });
return compose_private(fmt, {&Stringify<Ts>(args).ref()...});
}
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
......
#include <glibmm.h>
#include <iomanip>
#include <iostream>
int
main(int, char**)
{
// Don't use the user's preferred locale. The decimal delimiter may be ','
// instead of the expected '.'.
Glib::set_init_to_users_preferred_locale(false);
Glib::init();
char carr[10] = "Užduotys";
......@@ -27,5 +32,26 @@ main(int, char**)
// This threw an exception before we added a ustring::FormatStream::stream(char*) overload.
Glib::ustring::format(cptr);
// Test substitution of various types and I/O manipulators
Glib::ustring expected("The meaning of life is 42, or with 2 decimal places, 42.00.");
auto the = "The";
std::string meaning("meaning");
Glib::ustring life("life");
auto number = 42.0;
auto places = 2;
auto actual = Glib::ustring::format(the, ' ', meaning, " of ", life, " is ",
number,
", or with ", places, " decimal places, ",
std::fixed, std::setprecision(places), number,
'.');
if (actual != expected)
{
std::cerr << "expected (" << expected.size() << "):\n" << expected << "\n\n"
<< "actual (" << actual .size() << "):\n" << actual << "\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment