| Directory: | ./ |
|---|---|
| File: | s21_sprintf.c |
| Date: | 2025-11-01 23:04:41 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 54 | 54 | 100.0% |
| Branches: | 38 | 38 | 100.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "s21_sprintf.h" | ||
| 2 | |||
| 3 | 2131 | int s21_sprintf(char *str, const char *format, ...) { | |
| 4 | va_list args; | ||
| 5 | 2131 | va_start(args, format); | |
| 6 | 2131 | int chars_written = parse_format_string(str, format, args); | |
| 7 | 2131 | va_end(args); | |
| 8 | 2131 | return chars_written; | |
| 9 | } | ||
| 10 | |||
| 11 | 2131 | int parse_format_string(char *str, const char *format, va_list args) { | |
| 12 | 2131 | const char *str_start = str; | |
| 13 |
2/2✓ Branch 0 taken 4183 times.
✓ Branch 1 taken 2131 times.
|
6314 | while (*format) { |
| 14 |
2/2✓ Branch 0 taken 2221 times.
✓ Branch 1 taken 1962 times.
|
4183 | if (*format == '%') { |
| 15 | 2221 | format++; | |
| 16 | 2221 | parse_format_specifier(&str, &format, args, str_start); | |
| 17 | } else { | ||
| 18 | 1962 | write_string(&str, &format); | |
| 19 | } | ||
| 20 | } | ||
| 21 | 2131 | *str = '\0'; | |
| 22 | 2131 | return str - str_start; | |
| 23 | } | ||
| 24 | |||
| 25 | 2221 | void parse_format_specifier(char **str, const char **format, va_list args, | |
| 26 | const char *str_start) { | ||
| 27 | FormatSpecifier fs; | ||
| 28 | 2221 | get_flags(format, &fs); | |
| 29 | 2221 | get_width(format, &fs, args); | |
| 30 | 2221 | get_precision(format, &fs, args); | |
| 31 | 2221 | get_length(format, &fs); | |
| 32 | 2221 | get_specifier(format, &fs); | |
| 33 | 2221 | write_arg(str, &fs, args, str_start); | |
| 34 | 2221 | } | |
| 35 | |||
| 36 | 2221 | void write_arg(char **str, FormatSpecifier *fs, va_list args, | |
| 37 | const char *str_start) { | ||
| 38 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 2168 times.
|
2221 | if (fs->specifier == 'c') { |
| 39 | 53 | handle_specifier_c(str, fs, args); | |
| 40 |
4/4✓ Branch 0 taken 196 times.
✓ Branch 1 taken 1972 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 192 times.
|
2168 | } else if (fs->specifier == 'd' || fs->specifier == 'i') { |
| 41 | 1976 | handle_specifier_d(str, fs, args); | |
| 42 |
4/4✓ Branch 0 taken 179 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 170 times.
|
192 | } else if (fs->specifier == 'e' || fs->specifier == 'E') { |
| 43 | 22 | handle_specifiers_eE(str, fs, args); | |
| 44 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 148 times.
|
170 | } else if (fs->specifier == 'f') { |
| 45 | 22 | handle_specifier_f(str, fs, args); | |
| 46 |
4/4✓ Branch 0 taken 134 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 120 times.
|
148 | } else if (fs->specifier == 'g' || fs->specifier == 'G') { |
| 47 | 28 | handle_specifiers_gG(str, fs, args); | |
| 48 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 101 times.
|
120 | } else if (fs->specifier == 'o') { |
| 49 | 19 | handle_specifier_o(str, fs, args); | |
| 50 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 84 times.
|
101 | } else if (fs->specifier == 'u') { |
| 51 | 17 | handle_specifier_u(str, fs, args); | |
| 52 |
4/4✓ Branch 0 taken 68 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 61 times.
|
84 | } else if (fs->specifier == 'x' || fs->specifier == 'X') { |
| 53 | 23 | handle_specifiers_xX(str, fs, args); | |
| 54 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 18 times.
|
61 | } else if (fs->specifier == 's') { |
| 55 | 43 | handle_specifier_s(str, fs, args); | |
| 56 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
|
18 | } else if (fs->specifier == 'p') { |
| 57 | 8 | handle_specifier_p(str, fs, args); | |
| 58 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | } else if (fs->specifier == 'n') { |
| 59 | 5 | handle_specifier_n((const char **)str, fs, args, str_start); | |
| 60 | } else { | ||
| 61 | 5 | **str = fs->specifier; | |
| 62 | 5 | (*str)++; | |
| 63 | } | ||
| 64 | 2221 | } | |
| 65 | |||
| 66 | 1962 | void write_string(char **str, const char **format) { | |
| 67 |
4/4✓ Branch 0 taken 28900 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 26947 times.
✓ Branch 3 taken 1953 times.
|
28909 | while (**format && **format != '%') { |
| 68 | 26947 | **str = **format; | |
| 69 | 26947 | (*str)++; | |
| 70 | 26947 | (*format)++; | |
| 71 | } | ||
| 72 | 1962 | } | |
| 73 |