MXVM 1.8.1
Virtual Machine, Compiler, and Pascal Frontend
Loading...
Searching...
No Matches
cstring.c
Go to the documentation of this file.
1
6#include <stddef.h>
7#include <stdint.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12int64_t strfind(const char *src, const char *search, long n) {
13 if (!src || !search || n < 0)
14 return -1;
15 size_t src_len = strlen(src);
16 if ((size_t)n > src_len)
17 return -1;
18 const char *start = src + n;
19 const char *found = strstr(start, search);
20 if (!found)
21 return -1;
22 return (long)(found - src);
23}
24
25int64_t substr(char *dest, int64_t size, const char *src, int64_t pos, int64_t len) {
26 if (!dest || !src || size <= 0 || pos < 0 || len < 0)
27 return 0;
28 size_t src_len = strlen(src);
29 if ((size_t)pos > src_len)
30 return 0;
31 size_t copy_len = (size_t)len;
32 if (pos + copy_len > src_len)
33 copy_len = src_len - pos;
34 if (copy_len >= (size_t)size)
35 copy_len = (size_t)size - 1;
36 memcpy(dest, src + pos, copy_len);
37 dest[copy_len] = '\0';
38 return (long)copy_len;
39}
40
41int64_t strat(const char *src, int64_t pos) {
42 if (!src || pos < 0)
43 return 0;
44 size_t len = strlen(src);
45 if ((size_t)pos >= len)
46 return 0;
47 char c = *(src + pos);
48 return (int64_t)c;
49}
50
51int64_t pos(const char *substr, const char *s) {
52 if (!substr || !s)
53 return 0;
54 const char *found = strstr(s, substr);
55 if (!found)
56 return 0;
57 return (int64_t)(found - s) + 1;
58}
59
60char *copy(const char *s, int64_t index, int64_t count) {
61 if (!s || index < 1 || count < 0)
62 return NULL;
63
64 size_t s_len = strlen(s);
65 size_t start_pos = (size_t)index - 1;
66 if (start_pos > s_len)
67 return NULL;
68
69 size_t copy_len = (size_t)count;
70 if (start_pos + copy_len > s_len) {
71 copy_len = s_len - start_pos;
72 }
73
74 char *new_buf = (char *)malloc(copy_len + 1);
75 if (!new_buf)
76 return NULL;
77
78 memcpy(new_buf, s + start_pos, copy_len);
79 new_buf[copy_len] = '\0';
80 return new_buf;
81}
82
83char *insert(const char *source, const char *dest, int64_t index) {
84 if (!source || !dest || index < 1)
85 return NULL;
86
87 size_t source_len = strlen(source);
88 size_t dest_len = strlen(dest);
89 size_t insert_pos = (size_t)index - 1;
90 if (insert_pos > dest_len)
91 insert_pos = dest_len;
92
93 size_t new_len = source_len + dest_len;
94 char *new_buf = (char *)malloc(new_len + 1);
95 if (!new_buf)
96 return NULL;
97
98 memcpy(new_buf, dest, insert_pos);
99 memcpy(new_buf + insert_pos, source, source_len);
100 memcpy(new_buf + insert_pos + source_len, dest + insert_pos, dest_len - insert_pos);
101
102 new_buf[new_len] = '\0';
103 return new_buf;
104}
105
106char *delete_str(const char *s, int64_t index, int64_t count) {
107 if (!s || index < 1 || count < 0)
108 return NULL;
109
110 size_t s_len = strlen(s);
111 size_t start_pos = (size_t)index - 1;
112 if (start_pos >= s_len) {
113 char *new_buf = (char *)malloc(s_len + 1);
114 if (!new_buf)
115 return NULL;
116 strcpy(new_buf, s);
117 return new_buf;
118 }
119
120 size_t del_count = (size_t)count;
121 if (start_pos + del_count > s_len) {
122 del_count = s_len - start_pos;
123 }
124
125 size_t new_len = s_len - del_count;
126 char *new_buf = (char *)malloc(new_len + 1);
127 if (!new_buf)
128 return NULL;
129
130 memcpy(new_buf, s, start_pos);
131 memcpy(new_buf + start_pos, s + start_pos + del_count, s_len - (start_pos + del_count));
132 new_buf[new_len] = '\0';
133 return new_buf;
134}
135
136char *inttostr(int64_t value) {
137 char *buf = (char *)malloc(21);
138 if (!buf)
139 return NULL;
140 snprintf(buf, 21, "%lld", value);
141 return buf;
142}
143
144int64_t strtoint(const char *s) {
145 if (!s)
146 return 0;
147 return atoll(s);
148}
char * copy(const char *s, int64_t index, int64_t count)
Definition cstring.c:60
int64_t strat(const char *src, int64_t pos)
Definition cstring.c:41
char * insert(const char *source, const char *dest, int64_t index)
Definition cstring.c:83
int64_t strtoint(const char *s)
Definition cstring.c:144
char * delete_str(const char *s, int64_t index, int64_t count)
Definition cstring.c:106
int64_t substr(char *dest, int64_t size, const char *src, int64_t pos, int64_t len)
Definition cstring.c:25
int64_t pos(const char *substr, const char *s)
Definition cstring.c:51
char * inttostr(int64_t value)
Definition cstring.c:136
int64_t strfind(const char *src, const char *search, long n)
Definition cstring.c:12