MCPcopy Create free account
hub / github.com/FastLED/FastLED / apply_width

Function apply_width

src/fl/stl/stdio.h:220–279  ·  view source on GitHub ↗

Apply width and padding to a string based on format spec

Source from the content-addressed store, hash-verified

218
219// Apply width and padding to a string based on format spec
220inline fl::string apply_width(const fl::string& str, const FormatSpec& spec, bool is_numeric = false) FL_NOEXCEPT {
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] == ' ')) {
237 fl::string result;
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

Callers 1

format_argFunction · 0.85

Calls 2

lengthMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected