Directory: | ./ |
---|---|
File: | s21_insert.c |
Date: | 2025-07-13 17:59:14 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 21 | 21 | 100.0% |
Branches: | 12 | 12 | 100.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "s21_string.h" | ||
2 | |||
3 | 8 | void *s21_insert(const char *src, const char *str, s21_size_t start_index) { | |
4 |
4/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 6 times.
|
8 | if (src == S21_NULL || str == S21_NULL) { |
5 | 2 | return S21_NULL; | |
6 | } | ||
7 | |||
8 | 6 | s21_size_t srcLength = s21_strlen(src); | |
9 | 6 | s21_size_t strLength = s21_strlen(str); | |
10 | 6 | char *result = S21_NULL; | |
11 | |||
12 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | if (start_index <= srcLength) { |
13 | 5 | result = malloc((srcLength + strLength + 1) * sizeof(char)); | |
14 | 5 | s21_size_t length = srcLength + strLength; | |
15 | 5 | s21_size_t p = 0; | |
16 | |||
17 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5 times.
|
18 | while (p < start_index) { |
18 | 13 | result[p] = src[p]; | |
19 | 13 | p++; | |
20 | } | ||
21 | |||
22 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5 times.
|
18 | for (s21_size_t i = 0; i < strLength; i++) { |
23 | 13 | result[p] = str[i]; | |
24 | 13 | p++; | |
25 | } | ||
26 | |||
27 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5 times.
|
20 | while (p < length) { |
28 | 15 | result[p] = src[p - strLength]; | |
29 | 15 | p++; | |
30 | } | ||
31 | |||
32 | 5 | result[length] = '\0'; | |
33 | } | ||
34 | |||
35 | 6 | return result; | |
36 | } | ||
37 |