Reduce locale dependency

This commit is contained in:
Victor Zverovich
2022-09-02 09:36:41 -07:00
parent 4191477b98
commit 56c72a671c
4 changed files with 22 additions and 10 deletions

View File

@@ -31,7 +31,7 @@
#include "locale.h"
FMT_BEGIN_NAMESPACE
template <typename Char> std::locale::id num_format_facet<Char>::id;
template <typename Locale> typename Locale::id num_format_facet<Locale>::id;
namespace detail {
@@ -131,8 +131,9 @@ FMT_FUNC auto write_int(unsigned long long value, locale_ref loc)
auto out = std::ostreambuf_iterator<Char>(&buf);
// We cannot use the num_put<char> facet because it may produce output in
// a wrong encoding.
using facet_t = conditional_t<std::is_same<Char, char>::value,
num_format_facet<>, std::num_put<Char>>;
using facet_t =
conditional_t<std::is_same<Char, char>::value,
num_format_facet<std::locale>, std::num_put<Char>>;
if (std::has_facet<facet_t>(locale)) {
std::use_facet<facet_t>(locale).put(out, ios, ' ', value);
return buf.str();

View File

@@ -8,17 +8,28 @@
#ifndef FMT_LOCALE_H_
#define FMT_LOCALE_H_
#include <locale> // std::num_put
#include <ios> // std::ios_base
#include <iterator> // std::ostreambuf_iterator
#include "core.h"
FMT_BEGIN_NAMESPACE
// A locale facet that formats numeric values in UTF-8.
template <typename Char = char>
class num_format_facet : public std::num_put<Char> {
template <typename Locale> class num_format_facet : public Locale::facet {
public:
static FMT_API std::locale::id id;
static FMT_API typename Locale::id id;
using iter_type = std::ostreambuf_iterator<char>;
auto put(iter_type out, std::ios_base& str, char fill,
unsigned long long val) const -> iter_type {
return do_put(out, str, fill, val);
}
protected:
virtual auto do_put(iter_type out, std::ios_base& str, char fill,
unsigned long long val) const -> iter_type = 0;
};
FMT_END_NAMESPACE