-
Notifications
You must be signed in to change notification settings - Fork 0
/
colr_replace_example.c
89 lines (81 loc) · 2.22 KB
/
colr_replace_example.c
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
#include "colr.h"
int main(void) {
// The string we are modifying.
char* mystring = "This is a foo line.";
char* pattern = "foo";
/*
Replace a string with a string.
*/
char* replaced = colr_replace(
mystring,
pattern,
"replaced"
);
// Failed to allocate for new replaced string?
if (!replaced) return EXIT_FAILURE;
// Print the result and `free()` it.
printf("%s\n", replaced);
free(replaced);
/*
Replace a string with a ColorText.
*/
replaced = colr_replace(
mystring,
pattern,
Colr("replaced", fore(RED))
);
// Failed to allocate for new replaced string?
if (!replaced) return EXIT_FAILURE;
// Print the result and `free()` it.
printf("%s\n", replaced);
free(replaced);
/*
Replace a string with a ColorResult.
*/
replaced = colr_replace(
mystring,
pattern,
Colr_join(
" ",
Colr("really", style(BRIGHT)),
Colr("replaced", fore(BLUE))
)
);
// Failed to allocate for new replaced string?
if (!replaced) return EXIT_FAILURE;
// Print the result and `free()` it.
printf("%s\n", replaced);
free(replaced);
/*
Replace a string with a ColorResult.
*/
char* mytemplate = "This is REDuseful?" NC;
replaced = colr_replace(
mytemplate,
"RED",
fore(RED)
);
// Failed to allocate for new replaced string?
if (!replaced) return EXIT_FAILURE;
// Print the result and `free()` it.
printf("%s\n", replaced);
free(replaced);
/*
Replace a compiled regex pattern with a ColorText.
*/
char* mypatstring = "I think this is a beautiful thing.";
regex_t mypat;
if (regcomp(&mypat, "th[a-z]+", REG_EXTENDED)) {
regfree(&mypat);
fprintf(stderr, "Failed to compile regex!\n");
return EXIT_FAILURE;
}
replaced = colr_replace(mypatstring, &mypat, Colr("know", fore(BLUE)));
// We don't need the pattern anymore, `free()` it.
regfree(&mypat);
if (!replaced) return EXIT_FAILURE;
// Print the result and `free()` it.
puts(replaced);
free(replaced);
return EXIT_SUCCESS;
}