shithub: femtolisp

ref: 62389c8990cafb8e99ae29b2bccbcd100ce4c7f0
dir: /utf8.h/

View raw version
#pragma once

/* is c the start of a utf8 sequence? */
#define isutf(c) (((c)&0xC0) != 0x80)

int u8_iswprint(Rune c) fl_constfn;

/* byte offset to character number */
size_t u8_charnum(const char *s, size_t offset) fl_purefn;

/* next character without NUL character terminator */
Rune u8_nextmemchar(const char *s, size_t *i);

/* returns length of next utf-8 sequence */
size_t u8_seqlen(const char *s) fl_purefn;

char read_escape_control_char(char c) fl_constfn;

/* given a wide character, convert it to an ASCII escape sequence stored in
   buf, where buf is "sz" bytes. returns the number of characters output.
   sz must be at least 3. */
int u8_escape_rune(char *buf, size_t sz, Rune ch);

/* convert UTF-8 "src" to escape sequences.

   sz is buf size in bytes. must be at least 12.

   if escape_quotes is nonzero, quote characters will be escaped.

   if ascii is nonzero, the output is 7-bit ASCII, no UTF-8 survives.

   starts at src[*pi], updates *pi to point to the first unprocessed
   byte of the input.

   end is one more than the last allowable value of *pi.

   returns number of bytes placed in buf, including a NUL terminator.
*/
size_t u8_escape(char *buf, size_t sz, const char *src, size_t *pi, size_t end,
                 bool escape_quotes, bool ascii);

/* utility predicates used by the above */
bool octal_digit(char c) fl_constfn;
bool hex_digit(char c) fl_constfn;

/* same as the above, but searches a buffer of a given size instead of
   a NUL-terminated string. */
char *u8_memchr(char *s, Rune ch, size_t sz, size_t *charn);

/* number of columns occupied by a string */
size_t u8_strwidth(const char *s) fl_purefn;

/* determine whether a sequence of bytes is valid UTF-8. length is in bytes */
int u8_isvalid(const char *str, int length) fl_purefn;

/* reverse a UTF-8 string. len is length in bytes. dest and src must both
   be allocated to at least len+1 bytes. returns 1 for error, 0 otherwise */
int u8_reverse(char *dest, char *src, size_t len);