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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/* -------------------------------------------
Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
------------------------------------------- */
#include <NeKit/Utils.h>
namespace Kernel {
Int32 rt_string_cmp(const Char* src, const Char* cmp, Size size) {
for (Size i = 0; i < size; ++i) {
if (src[i] != cmp[i]) return static_cast<Int32>(src[i]) - static_cast<Int32>(cmp[i]);
}
return 0;
}
SizeT rt_string_len(const Char* str, SizeT max_len) {
SizeT len = 0;
while (len < max_len && str[len] != '\0') ++len;
return len;
}
Size rt_string_len(const Char* ptr) {
Size cnt = 0;
while (ptr[cnt] != '\0') ++cnt;
return cnt;
}
const Char* rt_alloc_string(const Char* src) {
SizeT slen = rt_string_len(src);
Char* buffer = new Char[slen + 1];
if (!buffer) return nullptr;
if (rt_copy_memory_safe(reinterpret_cast<voidPtr>(const_cast<Char*>(src)),
reinterpret_cast<voidPtr>(buffer), slen, slen + 1) < 0) {
delete[] buffer;
return nullptr;
}
buffer[slen] = '\0';
return buffer;
}
Int32 rt_copy_memory_safe(const voidPtr src, voidPtr dst, Size len, Size dst_size) {
if (!src || !dst || len > dst_size) {
if (dst && dst_size) {
rt_set_memory_safe(dst, 0, dst_size, dst_size);
}
return -1;
}
auto s = reinterpret_cast<const UInt8*>(src);
auto d = reinterpret_cast<UInt8*>(dst);
for (Size i = 0; i < len; ++i) d[i] = s[i];
return static_cast<Int>(len);
}
voidPtr rt_set_memory_safe(voidPtr dst, UInt32 value, Size len, Size dst_size) {
if (!dst || len > dst_size) return nullptr;
auto p = reinterpret_cast<UInt8*>(dst);
UInt8 v = static_cast<UInt8>(value & 0xFF);
for (Size i = 0; i < len; ++i) p[i] = v;
return dst;
}
Void rt_zero_memory(voidPtr pointer, Size len) {
rt_set_memory_safe(pointer, 0, len, len);
}
#ifdef __NE_ENFORCE_DEPRECATED_WARNINGS
[[deprecated("Use rt_set_memory_safe instead")]]
#endif
voidPtr
rt_set_memory(voidPtr src, UInt32 value, Size len) {
if (!src) return nullptr;
auto p = reinterpret_cast<UInt8*>(src);
UInt8 v = static_cast<UInt8>(value & 0xFF);
for (Size i = 0; i < len; ++i) p[i] = v;
return src;
}
#ifdef __NE_ENFORCE_DEPRECATED_WARNINGS
[[deprecated("Use rt_copy_memory_safe instead")]]
#endif
Int32 rt_copy_memory(const voidPtr src, voidPtr dst, Size len) {
if (!src || !dst) return -1;
auto s = reinterpret_cast<const UInt8*>(src);
auto d = reinterpret_cast<UInt8*>(dst);
for (Size i = 0; i < len; ++i) d[i] = s[i];
return static_cast<Int>(len);
}
Int32 rt_to_uppercase(Int ch) {
return (ch >= 'a' && ch <= 'z') ? ch - 0x20 : ch;
}
Int32 rt_to_lower(Int ch) {
return (ch >= 'A' && ch <= 'Z') ? ch + 0x20 : ch;
}
Int32 rt_is_alnum(Int ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9');
}
Boolean rt_is_space(Int ch) {
return ch == ' ';
}
Boolean rt_is_newln(Int ch) {
return ch == '\n';
}
Char rt_to_char(UInt64 value, Int32 base) {
static constexpr Char kDigits[] = "0123456789ABCDEF";
return kDigits[value % base];
}
Bool rt_to_string(Char* str, UInt64 value, Int32 base) {
if (!str || base < 2 || base > 16) return NO;
Int i = 0;
do {
str[i++] = rt_to_char(value, base);
value /= base;
} while (value);
str[i] = '\0';
// in-place
for (Int j = 0; j < i / 2; ++j) {
Char tmp = str[j];
str[j] = str[i - j - 1];
str[i - j - 1] = tmp;
}
return YES;
}
VoidPtr rt_string_in_string(const Char* haystack, const Char* needle) {
if (!haystack || !needle) return nullptr;
SizeT needle_len = rt_string_len(needle);
SizeT hay_len = rt_string_len(haystack);
if (needle_len > hay_len) return nullptr;
for (SizeT i = 0; i <= hay_len - needle_len; ++i) {
if (rt_string_cmp(haystack + i, needle, needle_len) == 0) {
return reinterpret_cast<voidPtr>(const_cast<Char*>(haystack + i));
}
}
return nullptr;
}
Char* rt_string_has_char(Char* str, Char ch) {
if (!str) return nullptr;
while (*str && *str != ch) ++str;
return (*str == ch) ? str : nullptr;
}
} // namespace Kernel
|