Apply width/alignment to a formatted string
| 119 | |
| 120 | // Apply width/alignment to a formatted string |
| 121 | inline void apply_width_align(fl::string& result, const fl::string& value, const FormatSpec& spec) { |
| 122 | fl::size value_len = value.size(); |
| 123 | |
| 124 | if (spec.width <= 0 || static_cast<fl::size>(spec.width) <= value_len) { |
| 125 | result.append(value); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | fl::size padding = static_cast<fl::size>(spec.width) - value_len; |
| 130 | char fill = spec.fill; |
| 131 | |
| 132 | char align = spec.align; |
| 133 | if (align == '\0') { |
| 134 | align = '>'; // Default right-align for numbers |
| 135 | } |
| 136 | |
| 137 | if (align == '<') { |
| 138 | // Left align: value then padding |
| 139 | result.append(value); |
| 140 | for (fl::size i = 0; i < padding; ++i) { |
| 141 | result.append(fill); |
| 142 | } |
| 143 | } else if (align == '>') { |
| 144 | // Right align: padding then value |
| 145 | for (fl::size i = 0; i < padding; ++i) { |
| 146 | result.append(fill); |
| 147 | } |
| 148 | result.append(value); |
| 149 | } else if (align == '^') { |
| 150 | // Center align |
| 151 | fl::size left_pad = padding / 2; |
| 152 | fl::size right_pad = padding - left_pad; |
| 153 | for (fl::size i = 0; i < left_pad; ++i) { |
| 154 | result.append(fill); |
| 155 | } |
| 156 | result.append(value); |
| 157 | for (fl::size i = 0; i < right_pad; ++i) { |
| 158 | result.append(fill); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Format an integer value |
| 164 | template <typename T> |
no test coverage detected