@@ -47,6 +47,14 @@ constexpr auto parse_impl(ParseContext &ctx)
4747 boost::decimal::chars_format fmt = boost::decimal::chars_format::general;
4848 bool is_upper = false ;
4949 int padding_digits = 0 ;
50+ bool use_locale = false ;
51+
52+ // Check for the locale character
53+ if (*it == ' L' )
54+ {
55+ use_locale = true ;
56+ ++it;
57+ }
5058
5159 // Check for a sign character
5260 if (it != ctx.end ())
@@ -148,7 +156,7 @@ constexpr auto parse_impl(ParseContext &ctx)
148156 BOOST_DECIMAL_THROW_EXCEPTION (std::format_error (" Expected '}' in format string" )); // LCOV_EXCL_LINE
149157 }
150158
151- return std::make_tuple (ctx_precision, fmt, is_upper, padding_digits, sign_character, it);
159+ return std::make_tuple (ctx_precision, fmt, is_upper, padding_digits, sign_character, use_locale, it);
152160}
153161
154162} // Namespace boost::decimal::detail
@@ -163,12 +171,14 @@ struct formatter<T>
163171 int ctx_precision;
164172 int padding_digits;
165173 bool is_upper;
174+ bool use_locale;
166175
167176 constexpr formatter () : fmt(boost::decimal::chars_format::general),
168177 sign(boost::decimal::detail::format_sign_option::minus),
169178 ctx_precision(6 ),
170179 padding_digits(0 ),
171- is_upper(false )
180+ is_upper(false ),
181+ use_locale(false )
172182 {}
173183
174184 constexpr auto parse (format_parse_context &ctx)
@@ -180,8 +190,9 @@ struct formatter<T>
180190 is_upper = std::get<2 >(res);
181191 padding_digits = std::get<3 >(res);
182192 sign = std::get<4 >(res);
193+ use_locale = std::get<5 >(res);
183194
184- return std::get<5 >(res);
195+ return std::get<6 >(res);
185196 }
186197
187198 template <typename FormatContext>
@@ -254,6 +265,16 @@ struct formatter<T>
254265 s.insert (s.begin () + static_cast <std::size_t >(has_sign), static_cast <std::size_t >(padding_digits) - s.size (), ' 0' );
255266 }
256267
268+ if (use_locale)
269+ {
270+ // We need approximately 1/3 more space in order to insert the thousands separators,
271+ // but after we have done our processing we need to shrink the string back down
272+ const auto initial_length {s.length ()};
273+ s.resize (s.length () * 4 / 3 + 1 );
274+ const auto offset {static_cast <std::size_t >(convert_pointer_pair_to_local_locale (const_cast <char *>(s.data ()), s.data () + s.length ()))};
275+ s.resize (initial_length + offset);
276+ }
277+
257278 return std::format_to (ctx.out (), " {}" , s);
258279 }
259280};
0 commit comments