Mojo struct
CStringSlice
@register_passable
struct CStringSlice[origin: ImmutOrigin]
A non-owning immutable view to a nul-terminated C string (const char*).
This type can be safely constructed from any sort of StringSlice or
Span[Byte] that is nul-terminated, or unsafely from a raw pointer.
Parameters
- origin (
ImmutOrigin): The origin of theCStringSlice.
Implemented traits
AnyType,
Copyable,
Defaultable,
Equatable,
ImplicitlyCopyable,
Movable,
Sized,
UnknownDestructibility,
Writable
Aliases
__copyinit__is_trivial
comptime __copyinit__is_trivial = True
__del__is_trivial
comptime __del__is_trivial = True
__moveinit__is_trivial
comptime __moveinit__is_trivial = True
Methods
__init__
__init__() -> Self
Constructs a null CStringSlice.
__init__(*, unsafe_from_ptr: UnsafePointer[Int8, origin]) -> Self
Construct a CStringSlice from an UnsafePointer.
Safety:
The UnsafePointer must be a valid nul-terminated C string or null.
Example:
from sys.ffi import c_char, CStringSlice, external_call
fn getenv_wrapper(
name: CStringSlice,
) raises -> CStringSlice[StaticConstantOrigin]:
# External call to 'getenv'.
# C signature: const char *getenv(const char *name);
var result = external_call[
"getenv",
UnsafePointer[c_char, StaticConstantOrigin],
](name.unsafe_ptr())
if result:
return CStringSlice(unsafe_from_ptr=result)
else:
raise Error("getenv returned an error!")Args:
- unsafe_from_ptr (
UnsafePointer): TheUnsafePointerto construct theCStringSlicefrom.
__init__(out self, slice: StringSlice[origin])
Construct a CStringSlice from a StringSlice.
Example:
from sys.ffi import CStringSlice
from testing import assert_raises
var string = String("Hello, World!")
with assert_raises():
# This will raise an error since the string is not nul-terminated.
_ = CStringSlice(string)Args:
- slice (
StringSlice): TheStringto construct theCStringSlicefrom.
Raises:
An error if the slice is not nul-terminated or has interior nul bytes.
__init__(out self, span: Span[Byte, origin])
Construct a CStringSlice from a Span[Byte].
Args:
- span (
Span): TheSpan[Byte]to construct theCStringSlicefrom.
Raises:
An error if the slice is not nul-terminated or has interior nul bytes.
__eq__
__eq__(self, rhs_same: Self) -> Bool
Compare two CStringSlices for equality.
Args:
- rhs_same (
Self): TheCStringSliceto compare against.
Returns:
Bool: True if the CStringSlices are equal, False otherwise.
__eq__(self, rhs: CStringSlice[origin]) -> Bool
Compare two CStringSlices for equality.
Args:
- rhs (
CStringSlice): TheCStringSliceto compare against.
Returns:
Bool: True if the CStringSlices are equal, False otherwise.
__ne__
__ne__(self, rhs: CStringSlice[origin]) -> Bool
Compare two CStringSlices for inequality.
Args:
- rhs (
CStringSlice): TheCStringSliceto compare against.
Returns:
Bool: True if the CStringSlices are not equal, False otherwise.
__len__
__len__(self) -> Int
Get the length of the C string. Like C's strlen this does not include the nul terminator.
Returns:
Int: The length of the C string.
write_to
write_to(self, mut writer: T)
Write the CStringSlice to a Writer, the nul terminator is omitted.
Args:
- writer (
T): TheWriterto write theCStringSliceto.
unsafe_ptr
unsafe_ptr(self) -> UnsafePointer[Int8, origin]
Get a pointer to the underlying CStringSlice.
Returns:
UnsafePointer: A pointer to the underlying CStringSlice.
as_bytes
as_bytes(self) -> Span[Byte, origin]
Get a span of the underlying CStringSlice as bytes.
The returned span does not include the nul terminator.
If you want a byte span including the nul terminator, use
as_bytes_with_nul().
Returns:
Span: A span of the underlying CStringSlice as bytes.
as_bytes_with_nul
as_bytes_with_nul(self) -> Span[Byte, origin]
Get a span of the underlying CStringSlice as bytes including the nul terminator.
If you want a byte span not including the nul terminator, use
as_bytes().
Returns:
Span: A span of the underlying CStringSlice as bytes.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!