Skip to content

Commit

Permalink
Merge branch 'fmtlib:master' into zig-pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Nov 23, 2023
2 parents 1d6831f + dd6f657 commit 9332d74
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
15 changes: 12 additions & 3 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@
# endif
#endif

// GCC < 5 requires this-> in decltype
#ifndef FMT_DECLTYPE_THIS
# if FMT_GCC_VERSION && FMT_GCC_VERSION < 500
# define FMT_DECLTYPE_THIS this->
# else
# define FMT_DECLTYPE_THIS
# endif
#endif

// Enable minimal optimizations for more compact code in debug mode.
FMT_GCC_PRAGMA("GCC push_options")
#if !defined(__OPTIMIZE__) && !defined(__NVCOMPILER) && !defined(__LCC__) && \
Expand Down Expand Up @@ -1439,7 +1448,7 @@ template <typename Context> struct arg_mapper {
// Only map owning types because mapping views can be unsafe.
template <typename T, typename U = format_as_t<T>,
FMT_ENABLE_IF(std::is_arithmetic<U>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(const T& val) -> decltype(this->map(U())) {
FMT_CONSTEXPR FMT_INLINE auto map(const T& val) -> decltype(FMT_DECLTYPE_THIS map(U())) {
return map(format_as(val));
}

Expand All @@ -1463,13 +1472,13 @@ template <typename Context> struct arg_mapper {
!is_string<U>::value && !is_char<U>::value &&
!is_named_arg<U>::value &&
!std::is_arithmetic<format_as_t<U>>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(T& val) -> decltype(this->do_map(val)) {
FMT_CONSTEXPR FMT_INLINE auto map(T& val) -> decltype(FMT_DECLTYPE_THIS do_map(val)) {
return do_map(val);
}

template <typename T, FMT_ENABLE_IF(is_named_arg<T>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(const T& named_arg)
-> decltype(this->map(named_arg.value)) {
-> decltype(FMT_DECLTYPE_THIS map(named_arg.value)) {
return map(named_arg.value);
}

Expand Down
23 changes: 15 additions & 8 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -791,12 +791,18 @@ inline auto code_point_index(basic_string_view<Char> s, size_t n) -> size_t {

// Calculates the index of the nth code point in a UTF-8 string.
inline auto code_point_index(string_view s, size_t n) -> size_t {
const char* data = s.data();
size_t num_code_points = 0;
for (size_t i = 0, size = s.size(); i != size; ++i) {
if ((data[i] & 0xc0) != 0x80 && ++num_code_points > n) return i;
}
return s.size();
size_t result = s.size();
const char* begin = s.begin();
for_each_codepoint(
s, [begin, &n, &result](uint32_t, string_view sv) {
if (n != 0) {
--n;
return true;
}
result = to_unsigned(sv.begin() - begin);
return false;
});
return result;
}

inline auto code_point_index(basic_string_view<char8_type> s, size_t n)
Expand Down Expand Up @@ -1962,8 +1968,9 @@ auto write_escaped_char(OutputIt out, Char v) -> OutputIt {
*out++ = static_cast<Char>('\'');
if ((needs_escape(static_cast<uint32_t>(v)) && v != static_cast<Char>('"')) ||
v == static_cast<Char>('\'')) {
out = write_escaped_cp(
out, find_escape_result<Char>{v_array, v_array + 1, static_cast<uint32_t>(v)});
out = write_escaped_cp(out,
find_escape_result<Char>{v_array, v_array + 1,
static_cast<uint32_t>(v)});
} else {
*out++ = v;
}
Expand Down
1 change: 1 addition & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ TEST(format_test, precision) {

EXPECT_EQ("st", fmt::format("{0:.2}", "str"));
EXPECT_EQ("вожык", fmt::format("{0:.5}", "вожыкі"));
EXPECT_EQ("123456", fmt::format("{0:.6}", "123456\xad"));
}

TEST(format_test, runtime_precision) {
Expand Down

0 comments on commit 9332d74

Please sign in to comment.