Standard library · Basics

text

Converting a string to UTF-16 and back, for Windows and libraries that want that format.

import text
On this page
  1. Functions

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): int
Returns the number of UTF-16 units in the string. For most scripts this equals the number of characters, but each emoji counts as two.
text-Utf16Len.salam
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)
end
Output10 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): int
Reads 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*): str
Converts 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