-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_string.c
More file actions
74 lines (69 loc) · 1.89 KB
/
runtime_string.c
File metadata and controls
74 lines (69 loc) · 1.89 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
#define _GNU_SOURCE
#include "mcp_forth.h"
#include <string.h>
static int rt_search(void * param, m4_stack_t * stack)
{
if(!(stack->len >= 4)) return M4_STACK_UNDERFLOW_ERROR;
void * haystack = (void *) stack->data[-4];
int haystack_len = stack->data[-3];
void * result = memmem(haystack, haystack_len, (void *) stack->data[-2], stack->data[-1]);
if(result) {
stack->data[-4] = (int) result;
stack->data[-3] = m4_bytes_remaining(haystack, result, haystack_len);
stack->data[-2] = -1;
}
else {
stack->data[-2] = 0;
}
stack->data -= 1;
stack->len -= 1;
return 0;
}
static int rt_compare(void * param, m4_stack_t * stack)
{
if(!(stack->len >= 4)) return M4_STACK_UNDERFLOW_ERROR;
void * a1 = (void *) stack->data[-4];
unsigned u1 = stack->data[-3];
void * a2 = (void *) stack->data[-2];
unsigned u2 = stack->data[-1];
int result = memcmp(a1, a2, u1 < u2 ? u1 : u2);
if(result == 0) {
stack->data[-4] = u1 < u2 ? -1 : u1 > u2 ? 1 : 0;
} else {
stack->data[-4] = result < 0 ? -1 : 1;
}
stack->data -= 3;
stack->len -= 3;
return 0;
}
static int rt_move(void * param, m4_stack_t * stack)
{
if(!(stack->len >= 3)) return M4_STACK_UNDERFLOW_ERROR;
memmove(
(void *) stack->data[-2],
(void *) stack->data[-3],
stack->data[-1]
);
stack->data -= 3;
stack->len -= 3;
return 0;
}
static int rt_fill(void * param, m4_stack_t * stack)
{
if(!(stack->len >= 3)) return M4_STACK_UNDERFLOW_ERROR;
memset(
(void *) stack->data[-3],
stack->data[-1],
stack->data[-2]
);
stack->data -= 3;
stack->len -= 3;
return 0;
}
const m4_runtime_cb_array_t m4_runtime_lib_string[] = {
{"search", {rt_search}},
{"compare", {rt_compare}},
{"move", {rt_move}},
{"fill", {rt_fill}},
{NULL},
};