
import libc;


//
// CStringRef - null terminated string
//

record CStringRef (
    ptr : Pointer[Int8]
);



//
// predicates
//

overload ContiguousSequence?(static CStringRef) = true;



//
// constructors
//

[T | ByteSizedInteger?(T)]
inline overload CStringRef(ptr : Pointer[T]) --> returned:CStringRef {
    returned.ptr = Pointer[Int8](ptr);
}

inline overload CStringRef(ptr : Pointer[Char]) = CStringRef(Pointer[Int8](ptr));



//
// size, index
//

inline overload size(a:CStringRef) = SizeT(libc.strlen(a.ptr));

[I | Integer?(I)]
inline overload index(a:CStringRef, i:I) {
    assert[#boundsChecks](i >= 0 and i < size(a), "CStringRef index out of bounds");
    return ref (begin(a) + i)^;
}



//
// coordinates and iteration
//

inline overload begin(a:CStringRef) = Pointer[Char](a.ptr);
inline overload end(a:CStringRef) = Pointer[Char](a.ptr + size(a));

inline overload iterator(a:CStringRef) = coordinateRange(begin(a), end(a));

inline overload reverseIterator(a:CStringRef) =
    reverseCoordinateRange(end(a), begin(a));



//
// cstring
//

inline overload cstring(a:CStringRef) = a.ptr;
