yggm/libs/string.cu
2025-03-14 22:52:59 +05:00

67 lines
1.6 KiB
Plaintext

__device__ int cstring_length(const char* s) {
int len = 0;
while (s[len]) len++;
return len;
}
__device__ int cstring_find(const char* s, const char* sub) {
int i, j;
int n = cstring_length(s);
int m = cstring_length(sub);
if (m == 0) return 0;
for (i = 0; i <= n - m; i++) {
for (j = 0; j < m; j++) {
if (s[i + j] != sub[j]) break;
}
if (j == m) return i;
}
return -1;
}
__device__ int cstring_to_ull(const char* s, unsigned* val) {
unsigned result = 0;
int i = 0;
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
i = 2;
}
if (s[i] == '\0') return 1;
for (; s[i]; i++) {
char c = s[i];
int digit;
if (c >= '0' && c <= '9') {
digit = c - '0';
} else if (c >= 'a' && c <= 'f') {
digit = 10 + (c - 'a');
} else if (c >= 'A' && c <= 'F') {
digit = 10 + (c - 'A');
} else {
return 1;
}
result = result * 16 + digit;
}
*val = result;
return 0;
}
__device__ void extract_substring(const char* src, int start, char* dest, int dest_size) {
int i = 0;
while (src[start + i] && i < dest_size - 1) {
dest[i] = src[start + i];
i++;
}
dest[i] = '\0';
}
__device__ void concat(const char* s1, const char* s2, char* out, int outSize) {
int i = 0, j = 0;
while (s1[i] && i < outSize - 1) {
out[i] = s1[i];
i++;
}
if (i < outSize - 1) {
out[i] = ' ';
i++;
}
while (s2[j] && i < outSize - 1) {
out[i] = s2[j];
i++; j++;
}
out[i] = '\0';
}