Directory: | ./ |
---|---|
File: | s21_sprintf.c |
Date: | 2025-07-13 17:59:14 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 54 | 54 | 100.0% |
Branches: | 36 | 36 | 100.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "s21_sprintf.h" | ||
2 | |||
3 | 2129 | int s21_sprintf(char *str, const char *format, ...) { | |
4 | va_list args; | ||
5 | 2129 | va_start(args, format); | |
6 | 2129 | int chars_written = parse_format_string(str, format, args); | |
7 | 2129 | va_end(args); | |
8 | 2129 | return chars_written; | |
9 | } | ||
10 | |||
11 | 2129 | int parse_format_string(char *str, const char *format, va_list args) { | |
12 | 2129 | const char *str_start = str; | |
13 |
2/2✓ Branch 0 taken 4165 times.
✓ Branch 1 taken 2129 times.
|
6294 | while (*format) { |
14 |
2/2✓ Branch 0 taken 2213 times.
✓ Branch 1 taken 1952 times.
|
4165 | if (*format == '%') { |
15 | 2213 | format++; | |
16 | 2213 | parse_format_specifier(&str, &format, args, str_start); | |
17 | } else { | ||
18 | 1952 | write_string(&str, &format); | |
19 | } | ||
20 | } | ||
21 | 2129 | *str = '\0'; | |
22 | 2129 | return str - str_start; | |
23 | } | ||
24 | |||
25 | 2213 | void parse_format_specifier(char **str, const char **format, va_list args, | |
26 | const char *str_start) { | ||
27 | FormatSpecifier fs; | ||
28 | 2213 | get_flags(format, &fs); | |
29 | 2213 | get_width(format, &fs, args); | |
30 | 2213 | get_precision(format, &fs, args); | |
31 | 2213 | get_length(format, &fs); | |
32 | 2213 | get_specifier(format, &fs); | |
33 | 2213 | write_arg(str, &fs, args, str_start); | |
34 | 2213 | } | |
35 | |||
36 | 2213 | 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 2160 times.
|
2213 | if (fs->specifier == 'c') { |
39 | 53 | handle_specifier_c(str, fs, args); | |
40 |
2/2✓ Branch 0 taken 1972 times.
✓ Branch 1 taken 188 times.
|
2160 | } else if (fs->specifier == 'd') { |
41 | 1972 | handle_specifier_d(str, fs, args); | |
42 |
4/4✓ Branch 0 taken 175 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 166 times.
|
188 | } 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 144 times.
|
166 | } else if (fs->specifier == 'f') { |
45 | 22 | handle_specifier_f(str, fs, args); | |
46 |
4/4✓ Branch 0 taken 130 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 116 times.
|
144 | } 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 97 times.
|
116 | } else if (fs->specifier == 'o') { |
49 | 19 | handle_specifier_o(str, fs, args); | |
50 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 80 times.
|
97 | } else if (fs->specifier == 'u') { |
51 | 17 | handle_specifier_u(str, fs, args); | |
52 |
4/4✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 57 times.
|
80 | } 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 14 times.
|
57 | } else if (fs->specifier == 's') { |
55 | 43 | handle_specifier_s(str, fs, args); | |
56 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8 times.
|
14 | } else if (fs->specifier == 'p') { |
57 | 6 | handle_specifier_p(str, fs, args); | |
58 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
|
8 | } else if (fs->specifier == 'n') { |
59 | 3 | handle_specifier_n((const char **)str, fs, args, str_start); | |
60 | } else { | ||
61 | 5 | **str = fs->specifier; | |
62 | 5 | (*str)++; | |
63 | } | ||
64 | 2213 | } | |
65 | |||
66 | 1952 | void write_string(char **str, const char **format) { | |
67 |
4/4✓ Branch 0 taken 28828 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 26883 times.
✓ Branch 3 taken 1945 times.
|
28835 | while (**format && **format != '%') { |
68 | 26883 | **str = **format; | |
69 | 26883 | (*str)++; | |
70 | 26883 | (*format)++; | |
71 | } | ||
72 | 1952 | } | |
73 |