Print string. * * @param str String to be printed. * @param width Width modifier. * @param precision Precision modifier. * @param flags Flags that modify the way the string is printed. * * @return Number of characters printed, negative value on failure. */
| 278 | * @return Number of characters printed, negative value on failure. |
| 279 | */ |
| 280 | static int print_str(char *str, int width, unsigned int precision, |
| 281 | uint32_t flags, printf_spec_t *ps) |
| 282 | { |
| 283 | if (str == NULL) |
| 284 | return printf_putstr(nullstr, ps); |
| 285 | |
| 286 | /* Print leading spaces. */ |
| 287 | size_t strw = str_length(str); |
| 288 | if ((precision == 0) || (precision > strw)) |
| 289 | precision = strw; |
| 290 | |
| 291 | /* Left padding */ |
| 292 | size_t counter = 0; |
| 293 | width -= precision; |
| 294 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
| 295 | while (width-- > 0) { |
| 296 | if (printf_putchar(' ', ps) == 1) |
| 297 | counter++; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /* Part of @a str fitting into the alloted space. */ |
| 302 | int retval; |
| 303 | size_t size = str_lsize(str, precision); |
| 304 | if ((retval = printf_putnchars(str, size, ps)) < 0) |
| 305 | return -counter; |
| 306 | |
| 307 | counter += retval; |
| 308 | |
| 309 | /* Right padding */ |
| 310 | while (width-- > 0) { |
| 311 | if (printf_putchar(' ', ps) == 1) |
| 312 | counter++; |
| 313 | } |
| 314 | |
| 315 | return ((int) counter); |
| 316 | |
| 317 | } |
| 318 | |
| 319 | /** Print wide string. |
| 320 | * |
no test coverage detected