static class · System.Runtime.InteropServices

CStrings

static class CStrings

Temporary C strings: what the compiler calls around an imported function that takes strings.

Kind
static class
Namespace
System.Runtime.InteropServices
Assembly
System.Core
Access
internal, static, abstract
Source file
Interop.ant

Inheritance

Base classes
Any CStrings

Methods

unsafe extern("mem.alloc") Allocate(i64 bytes) : u8*
#

static

unsafe extern("mem.free") Release(u8* block)
#

static

unsafe Utf8(str text) : u8*
#

static

A null-terminated UTF-8 copy of text (Free gives it back).

unsafe Utf16(String<u16> text) : u16*
#

static

A null-terminated UTF-16 copy of text (Free16 gives it back).

unsafe Utf8Into(str text, u8* buffer, i32 capacity) : bool
#

static

text null-terminated into buffer (capacity units) when it fits: whether it did.

unsafe Utf16Into(String<u16> text, u16* buffer, i32 capacity) : bool
#

static

unsafe Free(u8* block)
#

static

unsafe Free16(u16* block)
#

static

unsafe Length8(u8* text) : i32
#

static

How many units come before the first 0.

unsafe Length16(u16* text) : i32
#

static

Inherited members (5)

Source

CStrings as Antril, decompiled from its Larva HIR.

System.Runtime.InteropServices.CStrings.ant
// System.Runtime.InteropServices.CStrings — System.Core (decompiled from Larva HIR)

import System;

into namespace System.Runtime.InteropServices
{
	/// Temporary C strings: what the compiler calls around an imported function that takes strings.
	static class CStrings
	{
		public:
			unsafe extern("mem.alloc") Allocate(i64 bytes) : u8*;

			unsafe extern("mem.free") Release(u8* block);

			/// A null-terminated UTF-8 copy of `text` (Free gives it back).
			unsafe Utf8(str text) : u8*
			{
				u8[] units = text.Units();
				i32 n = units.Length;
				u8* copy = Allocate((i64)n + 1);
				loop (i32 i = 0..n)
					*(copy + (i64)i) = units[i];
				return copy;
			}

			/// A null-terminated UTF-16 copy of `text` (Free16 gives it back).
			unsafe Utf16(String<u16> text) : u16*
			{
				u16[] units = text.Units();
				i32 n = units.Length;
				u16* copy = (u16*)Allocate(2 * ((i64)n + 1));
				loop (i32 i = 0..n)
					*(copy + (i64)i) = units[i];
				return copy;
			}

			/// `text` null-terminated into `buffer` (`capacity` units) when it fits: whether it did.
			unsafe Utf8Into(str text, u8* buffer, i32 capacity) : bool
			{
				u8[] units = text.Units();
				i32 n = units.Length;
				if (n >= capacity)
					return false;
				loop (i32 i = 0..n)
					*(buffer + (i64)i) = units[i];
				*(buffer + (i64)n) = 0;
				return true;
			}

			unsafe Utf16Into(String<u16> text, u16* buffer, i32 capacity) : bool
			{
				u16[] units = text.Units();
				i32 n = units.Length;
				if (n >= capacity)
					return false;
				loop (i32 i = 0..n)
					*(buffer + (i64)i) = units[i];
				*(buffer + (i64)n) = 0;
				return true;
			}

			unsafe Free(u8* block)
			{
				Release(block);
			}

			unsafe Free16(u16* block)
			{
				Release((u8*)block);
			}

			/// How many units come before the first 0.
			unsafe Length8(u8* text) : i32
			{
				i32 n = 0;
				loop (*(text + (i64)n) != 0)
					n++;
				return n;
			}

			unsafe Length16(u16* text) : i32
			{
				i32 n = 0;
				loop (*(text + (i64)n) != 0)
					n++;
				return n;
			}
	}
}
Esc