FastLED 3.9.15
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 220 of file stdio.h.

220 {
221 int len = static_cast<int>(str.length());
222
223 // No width specified or content already wider
224 if (spec.width <= len) {
225 return str;
226 }
227
228 int padding = spec.width - len;
229 char pad_char = ' ';
230
231 // Zero-padding only for numeric types and right-align
232 if (spec.zero_pad && is_numeric && !spec.left_align) {
233 pad_char = '0';
234
235 // Handle sign for zero-padding: move sign to front
236 if (!str.empty() && (str[0] == '-' || str[0] == '+' || str[0] == ' ')) {
238 result += str[0]; // Sign first
239 for (int i = 0; i < padding; ++i) {
240 result += pad_char;
241 }
242 for (size_t i = 1; i < str.length(); ++i) {
243 result += str[i];
244 }
245 return result;
246 }
247
248 // Handle 0x prefix for zero-padding
249 if (str.length() >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
250 fl::string result;
251 result += str[0]; // '0'
252 result += str[1]; // 'x' or 'X'
253 for (int i = 0; i < padding; ++i) {
254 result += pad_char;
255 }
256 for (size_t i = 2; i < str.length(); ++i) {
257 result += str[i];
258 }
259 return result;
260 }
261 }
262
263 fl::string result;
264 if (spec.left_align) {
265 // Left-align: content then padding
266 result = str;
267 for (int i = 0; i < padding; ++i) {
268 result += pad_char;
269 }
270 } else {
271 // Right-align: padding then content
272 for (int i = 0; i < padding; ++i) {
273 result += pad_char;
274 }
275 result += str;
276 }
277
278 return result;
279}
fl::size length() const FL_NOEXCEPT
bool empty() const FL_NOEXCEPT
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31

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

Referenced by format_arg(), and format_arg().

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