text
Converting a string to UTF-16 and back, for Windows and libraries that want that format.
import text
On this page
Salam strings are UTF-8, but some systems, Windows and a few C libraries above all, want text in UTF-16. In UTF-16 an ordinary character is one 16-bit "unit" and a rarer one, such as an emoji, takes two.
The text package converts in both directions. The memory ToUtf16 returns belongs to you, and ends with a zero unit.
Functions
Utf16Len
func Utf16Len(s: str): intReturns the number of UTF-16 units in the string. For most scripts this equals the number of characters, but each emoji counts as two.
import text
func main:
s := "salam 😀"
println s.len(), s.char_count(), text.Utf16Len(s)
u := text.ToUtf16(s)
println text.Utf16UnitAt(u, 0), text.Utf16UnitAt(u, 6)
println text.FromUtf16(u)
endOutput10 7 8
115 55357
salam 😀
ToUtf16
func ToUtf16(s: str): void*Converts a string into a zero-terminated UTF-16 array and returns its address.
The example for this item is shown with the ones above: text-Utf16Len.salam
Utf16UnitAt
func Utf16UnitAt(w: void*, i: int): intReads unit number
i from a UTF-16 array. For an emoji you see two consecutive units, a surrogate pair.The example for this item is shown with the ones above: text-Utf16Len.salam
FromUtf16
func FromUtf16(w: void*): strConverts a zero-terminated UTF-16 array back into a Salam string.
The example for this item is shown with the ones above: text-Utf16Len.salam