NeKernel dev
Loading...
Searching...
No Matches
Support.h
Go to the documentation of this file.
1/* ========================================
2
3 Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
4
5======================================== */
6
7#pragma once
8
11
12#ifndef __NE_ARM64__
13#include <string.h>
14#endif
15
16#define kLongMax ((long) (~0UL >> 1))
17#define kLongMin (~kLongMax)
18
19#ifdef __BOOTZ__
20
25EXTERN_C void* memset(void* dst, int byte, long long unsigned int len);
26
31EXTERN_C void* memcpy(void* dst, const void* src, long long unsigned int len);
32
34EXTERN_C size_t strlen(const char* whatToCheck);
35
37EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight);
38
39#else
40
41#ifndef __NE_ARM64__
42#include <string.h>
43#endif
44
45#endif // __BOOTZ__
46
47#define SetMem(dst, c, sz) memset(dst, c, sz)
48#define MoveMem(dst, src, sz) memcpy(dst, src, sz)
49#define CopyMem(dst, src, sz) memcpy(dst, src, sz)
50#define StrLen(src) strlen(src)
51#define StrCmp(dst, src) strcmp(dst, src)
52
53inline int IsSpace(int c) {
54 return c == ' ';
55}
56
57inline int StringNCompare(const char* destination, const char* source, long length) {
58 long err = 0;
59
60 for (long i = 0UL; i < length; ++i) {
61 if (source[i] != destination[i]) ++err;
62 }
63
64 return err;
65}
66
67inline long StringToLong(const char* nptr, char** endptr, int base) {
68 const char *p = nptr, *endp;
69 bool is_neg = 0, overflow = 0;
70
71 /* Need unsigned so (-kLongMin) can fit in these: */
72 unsigned long n = 0UL, cutoff;
73 int cutlim;
74
75 if (base < 0 || base == 1 || base > 36) {
76 return 0L;
77 }
78
79 endp = nptr;
80
81 while (IsSpace(*p)) p++;
82
83 if (*p == '+') {
84 p++;
85 } else if (*p == '-') {
86 is_neg = 1, p++;
87 }
88 if (*p == '0') {
89 p++;
90 /* For strtol(" 0xZ", &endptr, 16), endptr should point to 'x';
91 * pointing to ' ' or '0' is non-compliant.
92 * (Many implementations do this wrong.) */
93 endp = p;
94 if (base == 16 && (*p == 'X' || *p == 'x')) {
95 p++;
96 } else if (base == 2 && (*p == 'B' || *p == 'b')) {
97 /* C23 standard supports "0B" and "0b" prefixes. */
98 p++;
99 } else if (base == 0) {
100 if (*p == 'X' || *p == 'x') {
101 base = 16, p++;
102 } else if (*p == 'B' || *p == 'b') {
103 base = 2, p++;
104 } else {
105 base = 8;
106 }
107 }
108 } else if (base == 0) {
109 base = 10;
110 }
111
112 cutoff = (is_neg) ? -(kLongMin / base) : kLongMax / base;
113 cutlim = (is_neg) ? -(kLongMin % base) : kLongMax % base;
114
115 while (1) {
116 int c;
117 if (*p >= 'A')
118 c = ((*p - 'A') & (~('a' ^ 'A'))) + 10;
119 else if (*p <= '9')
120 c = *p - '0';
121 else
122 break;
123 if (c < 0 || c >= base) break;
124 endp = ++p;
125 if (overflow) {
126 /* endptr should go forward and point to the non-digit character
127 * (of the given base); required by ANSI standard. */
128 if (endptr) continue;
129 break;
130 }
131 if (n > cutoff || (n == cutoff && c > cutlim)) {
132 overflow = 1;
133 continue;
134 }
135 n = n * base + c;
136 }
137
138 if (endptr) *endptr = (char*) endp;
139
140 if (overflow) {
141 return ((is_neg) ? kLongMin : kLongMax);
142 }
143
144 return (long) ((is_neg) ? -n : n);
145}
EXTERN_C void * memset(void *dst, int c, long long unsigned int len)
C Standard Library overrides. /// =========================================================== ///.
Definition CRuntimeOverrides.cc:15
EXTERN_C void * memcpy(void *dst, const void *src, long long unsigned int len)
Definition CRuntimeOverrides.cc:19
EXTERN_C Int32 strcmp(const char *a, const char *b)
Definition CRuntimeOverrides.cc:25
#define EXTERN_C
int IsSpace(int c)
Definition Support.h:53
int StringNCompare(const char *destination, const char *source, long length)
Definition Support.h:57
#define kLongMax
Definition Support.h:16
#define kLongMin
Definition Support.h:17
long StringToLong(const char *nptr, char **endptr, int base)
Definition Support.h:67