FastLED 3.10.6
Loading...
Searching...
No Matches

◆ apply_width()

fl::string fl::printf_detail::apply_width ( const fl::string & str,
const FormatSpec & spec,
bool is_numeric = false )
inline

Definition at line 247 of file stdio.h.

247 {
248 int len = static_cast<int>(str.length());
249
250 // No width specified or content already wider
251 if (spec.width <= len) {
252 return str;
253 }
254
255 int padding = spec.width - len;
256 char pad_char = ' ';
257
258 // Zero-padding only for numeric types and right-align
259 if (spec.zero_pad && is_numeric && !spec.left_align) {
260 pad_char = '0';
261
262 // Handle sign for zero-padding: move sign to front
263 if (!str.empty() && (str[0] == '-' || str[0] == '+' || str[0] == ' ')) {
265 result += str[0]; // Sign first
266 for (int i = 0; i < padding; ++i) {
267 result += pad_char;
268 }
269 for (size_t i = 1; i < str.length(); ++i) {
270 result += str[i];
271 }
272 return result;
273 }
274
275 // Handle 0x prefix for zero-padding
276 if (str.length() >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
277 fl::string result;
278 result += str[0]; // '0'
279 result += str[1]; // 'x' or 'X'
280 for (int i = 0; i < padding; ++i) {
281 result += pad_char;
282 }
283 for (size_t i = 2; i < str.length(); ++i) {
284 result += str[i];
285 }
286 return result;
287 }
288 }
289
290 fl::string result;
291 if (spec.left_align) {
292 // Left-align: content then padding
293 result = str;
294 for (int i = 0; i < padding; ++i) {
295 result += pad_char;
296 }
297 } else {
298 // Right-align: padding then content
299 for (int i = 0; i < padding; ++i) {
300 result += pad_char;
301 }
302 result += str;
303 }
304
305 return result;
306}
fl::size length() const FL_NO_EXCEPT
bool empty() const FL_NO_EXCEPT
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31

References fl::basic_string::empty(), fl::FL_NO_EXCEPT, fl::printf_detail::FormatSpec::left_align, fl::basic_string::length(), fl::printf_detail::FormatSpec::width, and fl::printf_detail::FormatSpec::zero_pad.

Referenced by fl::printf_detail::anonymous_namespace{stdio.cpp.hpp}::emit_padded(), format_arg(), and format_arg().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: