Files
nt-lite/code/public/ntlite/string_view.h
T

128 lines
3.8 KiB
C

// Public NTLite string view ABI declarations shared between kernel, drivers, and user mode.
#pragma once
// sys_string.h - shared native string-view ABI.
// A SYS_STRING_VIEW is a borrowed byte span. It is not NUL-terminated by
// contract and it does not own the pointed-to storage.
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#ifndef SYS_STRING_VIEW_DEFINED
#define SYS_STRING_VIEW_DEFINED 1
typedef struct SYS_STRING_VIEW {
const char* Data;
size_t Length;
} SYS_STRING_VIEW;
#endif
#define SYS_SV_LITERAL(s) ((SYS_STRING_VIEW){ (s), sizeof(s) - 1 })
#define SYS_SV_NULL ((SYS_STRING_VIEW){ (const char*)0, 0 })
// Implements the sv From Parts operation.
static inline SYS_STRING_VIEW SysSvFromParts(const char* data, size_t length) {
SYS_STRING_VIEW v;
v.Data = data;
v.Length = length;
return v;
}
// Implements the sv From Cstr operation.
static inline SYS_STRING_VIEW SysSvFromCstr(const char* s) {
SYS_STRING_VIEW v;
v.Data = s;
v.Length = s ? strlen(s) : 0;
return v;
}
// Implements the sv Is Empty operation.
static inline int SysSvIsEmpty(SYS_STRING_VIEW s) {
return s.Length == 0;
}
// Implements the sv Is Valid operation.
static inline int SysSvIsValid(SYS_STRING_VIEW s) {
return s.Data != (const char*)0 || s.Length == 0;
}
// Implements the sv Equal operation.
static inline int SysSvEqual(SYS_STRING_VIEW a, SYS_STRING_VIEW b) {
if (a.Length != b.Length) return 0;
if (a.Length == 0) return 1;
if (!a.Data || !b.Data) return 0;
return memcmp(a.Data, b.Data, a.Length) == 0;
}
// Implements the ascii Lower operation.
static inline char SysAsciiLower(char c) {
return (c >= 'A' && c <= 'Z') ? (char)(c + ('a' - 'A')) : c;
}
// Implements the sv Equal Ascii I operation.
static inline int SysSvEqualAsciiI(SYS_STRING_VIEW a, SYS_STRING_VIEW b) {
if (a.Length != b.Length) return 0;
if (a.Length == 0) return 1;
if (!a.Data || !b.Data) return 0;
for (size_t i = 0; i < a.Length; i++) {
if (SysAsciiLower(a.Data[i]) != SysAsciiLower(b.Data[i])) return 0;
}
return 1;
}
// Implements the sv Equal Lit operation.
static inline int SysSvEqualLit(SYS_STRING_VIEW a, const char* lit) {
return SysSvEqual(a, SysSvFromCstr(lit));
}
// Implements the sv Equal Lit Ascii I operation.
static inline int SysSvEqualLitAsciiI(SYS_STRING_VIEW a, const char* lit) {
return SysSvEqualAsciiI(a, SysSvFromCstr(lit));
}
// Implements the sv Contains Byte operation.
static inline int SysSvContainsByte(SYS_STRING_VIEW s, char ch) {
if (!s.Data && s.Length) return 0;
for (size_t i = 0; i < s.Length; i++) {
if (s.Data[i] == ch) return 1;
}
return 0;
}
// Implements the sv Slice operation.
static inline SYS_STRING_VIEW SysSvSlice(SYS_STRING_VIEW s, size_t off, size_t len) {
if (off > s.Length) off = s.Length;
if (len > s.Length - off) len = s.Length - off;
return SysSvFromParts(s.Data ? s.Data + off : (const char*)0, len);
}
// Implements the sv Trim Ascii operation.
static inline SYS_STRING_VIEW SysSvTrimAscii(SYS_STRING_VIEW s) {
size_t begin = 0;
size_t end = s.Length;
while (begin < end) {
char c = s.Data[begin];
if (c != ' ' && c != '\t' && c != '\r' && c != '\n') break;
begin++;
}
while (end > begin) {
char c = s.Data[end - 1];
if (c != ' ' && c != '\t' && c != '\r' && c != '\n') break;
end--;
}
return SysSvSlice(s, begin, end - begin);
}
// Implements the sv Copy Z operation.
static inline int SysSvCopyZ(char* dst, size_t dstSize, SYS_STRING_VIEW src) {
if (!dst || dstSize == 0) return 0;
if (src.Length && !src.Data) return 0;
if (src.Length + 1 > dstSize) {
dst[0] = '\0';
return 0;
}
if (src.Length) memcpy(dst, src.Data, src.Length);
dst[src.Length] = '\0';
return 1;
}