File: utf8.ha

package info (click to toggle)
hare 0.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,948 kB
  • sloc: asm: 1,264; makefile: 123; sh: 114; lisp: 101
file content (36 lines) | stat: -rw-r--r-- 1,136 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use encoding::utf8;
use types;

// Converts a byte slice into a string, but does not test if it is valid UTF-8.
// This is faster than the safe equivalent, but if the string is not valid UTF-8
// it may cause undefined behavior. The return value is borrowed from the input.
export fn fromutf8_unsafe(in: []u8) str = {
	const s = types::string {
		data     = in: *[*]u8,
		length   = len(in),
		capacity = len(in),
	};
	return *(&s: *const str);
};

// Converts a byte slice into a string. The return value is borrowed from the
// input. If the slice contains invalid UTF-8 sequences,
// [[encoding::utf8::invalid]] is returned instead.
export fn fromutf8(in: []u8) (str | utf8::invalid) = {
	utf8::validate(in)?;
	return fromutf8_unsafe(in);
};

// Converts a string to a UTF-8 byte slice. The return value is borrowed from
// the input.
export fn toutf8(in: str) []u8 = *(&in: *[]u8);

@test fn utf8() void = {
	assert(fromutf8([
		0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64,
	])! == "hello world");
	assert(fromutf8([])! == "");
};