-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreplace.c
More file actions
92 lines (77 loc) · 2.97 KB
/
replace.c
File metadata and controls
92 lines (77 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*******************************************************************************
* File: replace.c
* Created: 2022-08-03
*
* Authors:
* Tyler Matijevich
*
* License:
* This file replace.c is part of the IecString project
* released under the MIT license agreement.
******************************************************************************/
#include "main.h"
/* Find in source and replace in destination */
int32_t IecStringReplace(char *destination, uint32_t size, char *source,
char *find, char *replace)
{
/* Gaurd null pointers */
if (!destination || !source || !find || !replace)
return IECSTRING_ERROR_NULL;
/* Check for zero size*/
if (!size)
return IECSTRING_ERROR_SIZE_ZERO;
/* Store lengths */
size_t source_length = strlen(source);
size_t find_length = strlen(find);
size_t replace_length = strlen(replace);
/* Check if source overlaps destination size */
if (destination <= source && source < destination + size)
return IECSTRING_ERROR_OVERLAP;
/* Check if destination overlaps source length */
if (source <= destination && destination <= source + source_length)
return IECSTRING_ERROR_OVERLAP;
/* Check if find overlaps destination size */
if (destination <= find && find < destination + size)
return IECSTRING_ERROR_OVERLAP;
/* Check if destination overlaps find length */
if (find <= destination && destination <= find + find_length)
return IECSTRING_ERROR_OVERLAP;
/* Check if replace overlaps destination size */
if (destination <= replace && replace < destination + size)
return IECSTRING_ERROR_OVERLAP;
/* Check if destination overlaps replace length */
if (replace <= destination && destination <= replace + replace_length)
return IECSTRING_ERROR_OVERLAP;
/* Find and replace, otherwise copy */
uint32_t i = 0;
size_t min_char_count;
while (*source && i < size - 1)
{
/* Break if nothing to find or not enough characters left to compare */
if (find_length == 0 || source_length - i < find_length)
{
strncpy(destination, source, size - 1 - i);
min_char_count = MIN(source_length, size - 1 - i);
destination += min_char_count;
source += min_char_count;
break;
}
/* Compare source against find */
if (strncmp(source, find, find_length) == 0)
{
strncpy(destination, replace, size - 1 - i);
min_char_count = MIN(replace_length, size - 1 - i);
destination += min_char_count;
source += find_length;
i += min_char_count;
continue;
}
/* Copy character */
*destination++ = *source++;
i++;
}
/* Add null terminator */
*destination = '\0';
/* Truncated if source characters remain and destination is full */
return IECSTRING_WARNING_TRUNCATE * (*source && i == size - 1);
}