/ external / linenoise / utf8.h
utf8.h
  1  #ifndef UTF8_UTIL_H
  2  #define UTF8_UTIL_H
  3  
  4  #ifdef __cplusplus
  5  extern "C" {
  6  #endif
  7  
  8  /**
  9   * UTF-8 utility functions
 10   *
 11   * (c) 2010-2019 Steve Bennett <steveb@workware.net.au>
 12   *
 13   * See utf8.c for licence details.
 14   */
 15  
 16  #ifndef USE_UTF8
 17  #include <ctype.h>
 18  
 19  #define MAX_UTF8_LEN 1
 20  
 21  /* No utf-8 support. 1 byte = 1 char */
 22  #define utf8_strlen(S, B) ((B) < 0 ? (int)strlen(S) : (B))
 23  #define utf8_strwidth(S, B) utf8_strlen((S), (B))
 24  #define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1)
 25  #define utf8_index(C, I) (I)
 26  #define utf8_charlen(C) 1
 27   #define utf8_width(C) 1
 28  
 29  #else
 30  
 31  #define MAX_UTF8_LEN 4
 32  
 33  /**
 34   * Converts the given unicode codepoint (0 - 0x1fffff) to utf-8
 35   * and stores the result at 'p'.
 36   *
 37   * Returns the number of utf-8 characters
 38   */
 39  int utf8_fromunicode(char *p, unsigned uc);
 40  
 41  /**
 42   * Returns the length of the utf-8 sequence starting with 'c'.
 43   *
 44   * Returns 1-4, or -1 if this is not a valid start byte.
 45   *
 46   * Note that charlen=4 is not supported by the rest of the API.
 47   */
 48  int utf8_charlen(int c);
 49  
 50  /**
 51   * Returns the number of characters in the utf-8
 52   * string of the given byte length.
 53   *
 54   * Any bytes which are not part of an valid utf-8
 55   * sequence are treated as individual characters.
 56   *
 57   * The string *must* be null terminated.
 58   *
 59   * Does not support unicode code points > \u1fffff
 60   */
 61  int utf8_strlen(const char *str, int bytelen);
 62  
 63  /**
 64   * Calculates the display width of the first 'charlen' characters in 'str'.
 65   * See utf8_width()
 66   */
 67  int utf8_strwidth(const char *str, int charlen);
 68  
 69  /**
 70   * Returns the byte index of the given character in the utf-8 string.
 71   *
 72   * The string *must* be null terminated.
 73   *
 74   * This will return the byte length of a utf-8 string
 75   * if given the char length.
 76   */
 77  int utf8_index(const char *str, int charindex);
 78  
 79  /**
 80   * Returns the unicode codepoint corresponding to the
 81   * utf-8 sequence 'str'.
 82   *
 83   * Stores the result in *uc and returns the number of bytes
 84   * consumed.
 85   *
 86   * If 'str' is null terminated, then an invalid utf-8 sequence
 87   * at the end of the string will be returned as individual bytes.
 88   *
 89   * If it is not null terminated, the length *must* be checked first.
 90   *
 91   * Does not support unicode code points > \u1fffff
 92   */
 93  int utf8_tounicode(const char *str, int *uc);
 94  
 95  /**
 96   * Returns the width (in characters) of the given unicode codepoint.
 97   * This is 1 for normal letters and 0 for combining characters and 2 for wide characters.
 98   */
 99  int utf8_width(int ch);
100  
101  #endif
102  
103  #ifdef __cplusplus
104  }
105  #endif
106  
107  #endif