-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.c
47 lines (39 loc) · 1 KB
/
utility.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
//
// utility.c
// Système Info
//
// Created on 16/03/2015.
//
#include "utility.h"
#include <string.h>
#include <assert.h>
#include "assembly.h"
void stringReplaceWithShorter(char *replaced, size_t toReplace, char const *replacing, size_t replacingLength) {
if(replacingLength == 0) {
replacingLength = strlen(replacing);
}
memcpy(replaced, replacing, replacingLength);
size_t length2 = strlen(replaced + toReplace - replacingLength);
memmove(replaced + replacingLength, replaced + toReplace, length2);
replaced[length2] = 0;
}
char *safeComment(char const *unsafe) {
#ifndef STRIP_COMMENTS
size_t count = 0, len = strlen(unsafe);
for(size_t i = 0; i < len; ++i) {
if(unsafe[i] == '\n')
++count;
}
char *comment = malloc(len + count + 1), *pos = comment;
strcpy(comment, unsafe);
while((pos = strstr(pos, "\n")) != NULL) {
size_t length2 = strlen(pos + 1 - 2);
memmove(pos + 2, pos + 1, length2);
memcpy(pos, "\\n", 2);
pos[length2] = 0;
}
return comment;
#else
return NULL;
#endif
}