Programming Language

Nocter

A self-contained systems language built around simplicity, encapsulation, and foolproof design.

/development/std/num/text.nct

text.nct

//! Type-owned adapters from the shared decimal formatter into owned storage.

see ./index.nct

use /fmt
use /internal/mem
use /mem.TryAllocator
use /string.String

instance i8 {
    method self.to_string(): String {
        var result = String.empty()
        fmt.try_append_i8(&+result, self) catch allocation_error { return mem.allocation_abort() }
        return move result
    }

    method self.try_to_string(allocator: &+TryAllocator): String! from allocator {
        var result = String.try_with_capacity(allocator, 4)?
        fmt.try_append_i8(&+result, self)?
        return move result
    }
}

instance i16 {
    method self.to_string(): String {
        var result = String.empty()
        fmt.try_append_i16(&+result, self) catch allocation_error { return mem.allocation_abort() }
        return move result
    }

    method self.try_to_string(allocator: &+TryAllocator): String! from allocator {
        var result = String.try_with_capacity(allocator, 6)?
        fmt.try_append_i16(&+result, self)?
        return move result
    }
}

instance i32 {
    method self.to_string(): String {
        var result = String.empty()
        fmt.try_append_i32(&+result, self) catch allocation_error { return mem.allocation_abort() }
        return move result
    }

    method self.try_to_string(allocator: &+TryAllocator): String! from allocator {
        var result = String.try_with_capacity(allocator, 11)?
        fmt.try_append_i32(&+result, self)?
        return move result
    }
}

instance i64 {
    method self.to_string(): String {
        var result = String.empty()
        fmt.try_append_i64(&+result, self) catch allocation_error { return mem.allocation_abort() }
        return move result
    }

    method self.try_to_string(allocator: &+TryAllocator): String! from allocator {
        var result = String.try_with_capacity(allocator, 20)?
        fmt.try_append_i64(&+result, self)?
        return move result
    }
}

instance isize {
    method self.to_string(): String {
        var result = String.empty()
        fmt.try_append_isize(&+result, self) catch allocation_error { return mem.allocation_abort() }
        return move result
    }

    method self.try_to_string(allocator: &+TryAllocator): String! from allocator {
        var result = String.try_with_capacity(allocator, 20)?
        fmt.try_append_isize(&+result, self)?
        return move result
    }
}

instance u8 {
    method self.to_string(): String {
        var result = String.empty()
        fmt.try_append_u8(&+result, self) catch allocation_error { return mem.allocation_abort() }
        return move result
    }

    method self.try_to_string(allocator: &+TryAllocator): String! from allocator {
        var result = String.try_with_capacity(allocator, 3)?
        fmt.try_append_u8(&+result, self)?
        return move result
    }
}

instance u16 {
    method self.to_string(): String {
        var result = String.empty()
        fmt.try_append_u16(&+result, self) catch allocation_error { return mem.allocation_abort() }
        return move result
    }

    method self.try_to_string(allocator: &+TryAllocator): String! from allocator {
        var result = String.try_with_capacity(allocator, 5)?
        fmt.try_append_u16(&+result, self)?
        return move result
    }
}

instance u32 {
    method self.to_string(): String {
        var result = String.empty()
        fmt.try_append_u32(&+result, self) catch allocation_error { return mem.allocation_abort() }
        return move result
    }

    method self.try_to_string(allocator: &+TryAllocator): String! from allocator {
        var result = String.try_with_capacity(allocator, 10)?
        fmt.try_append_u32(&+result, self)?
        return move result
    }
}

instance u64 {
    method self.to_string(): String {
        var result = String.empty()
        fmt.try_append_u64(&+result, self) catch allocation_error { return mem.allocation_abort() }
        return move result
    }

    method self.try_to_string(allocator: &+TryAllocator): String! from allocator {
        var result = String.try_with_capacity(allocator, 20)?
        fmt.try_append_u64(&+result, self)?
        return move result
    }
}

instance usize {
    method self.to_string(): String {
        var result = String.empty()
        fmt.try_append_usize(&+result, self) catch allocation_error { return mem.allocation_abort() }
        return move result
    }

    method self.try_to_string(allocator: &+TryAllocator): String! from allocator {
        var result = String.try_with_capacity(allocator, 20)?
        fmt.try_append_usize(&+result, self)?
        return move result
    }
}