39 lines · 1.6 KB · Text
RawDownload
= byte2char, char2byte: convert between bytes and characters     include "sys.m";    sys := load Sys Sys->PATH;     byte2char: fn(buf: array of byte, n: int): (int, int, int);    char2byte: fn(c: int, buf: array of byte, n: int): int; Byte2char  converts a byte sequence to one Unicode character.Buf is an array of bytes and n is the index of the first byteto examine in the array.  The returned tuple, say (c, length,status), specifies the result of the translation:  c  is  theresulting  Unicode character, status is non-zero if the bytesare a valid UTF sequence and zero otherwise,  and  length  isset  to  the number of bytes consumed by the translation.  Ifthe input sequence is not long enough to determine its valid‐ity,  byte2char consumes zero bytes; if the input sequence isotherwise invalid, byte2char consumes one input byte and gen‐erates  an  error  character  (Sys->UTFerror,  16r80),  whichprints in most fonts as a boxed question mark. Char2byte performs the inverse of byte2char.  It translates aUnicode character, c, to a UTF byte sequence, which is placedin successive bytes starting  at  buf[n].   The  longest  UTFsequence  for  a  single Unicode character is Sys->UTFmax (4)bytes.  If the translation succeeds,  char2byte  returns  thenumber  of  bytes placed in the buffer.  If the buffer is toosmall to hold the result, char2byte returns zero  and  leavesthe array unchanged. == source/libinterp/runt.c == see ALSOsys-intro(2), sys-utfbytes(2), utf(6) == diagnosticsA run-time error occurs if n exceeds the bounds of the array.