Skip to content
Myr

jv_unicode.c

This file

Path

src/jv_unicode.c

139 lines · 4.6 KiB · C

Last changed by

Fix undefined pointer arithmetic in UTF-8 helperstheyouchengbefore adoptiondf924ea

History of this file

Take it

1#include <stdio.h>2#include <assert.h>3#include "jv_unicode.h"4#include "jv_utf8_tables.h"56// jvp_utf8_backtrack returns the beginning of the last codepoint in the7// string, assuming that start is the last byte in the string.8// If the last codepoint is incomplete, returns the number of missing bytes via9// *missing_bytes.  If there are no leading bytes or an invalid byte is10// encountered, NULL is returned and *missing_bytes is not altered.11const char* jvp_utf8_backtrack(const char* start, const char* min, int *missing_bytes) {12  assert(min <= start);13  if (min == start) {14    return min;15  }16  int length = 0;17  int seen = 1;18  while ((length = utf8_coding_length[(unsigned char)*start]) == UTF8_CONTINUATION_BYTE) {19    if (start == min) break;20    start--;21    seen++;22  }23  if (length == 0 || length == UTF8_CONTINUATION_BYTE || length - seen < 0) {24    return NULL;25  }26  if (missing_bytes) *missing_bytes = length - seen;27  return start;28}2930const char* jvp_utf8_next(const char* in, const char* end, int* codepoint_ret) {31  assert(in <= end);32  if (in == end) {33    return 0;34  }35  int codepoint = -1;36  unsigned char first = (unsigned char)in[0];37  int length = utf8_coding_length[first];38  if ((first & 0x80) == 0) {39    /* Fast-path for ASCII */40    codepoint = first;41    length = 1;42  } else if (length == 0 || length == UTF8_CONTINUATION_BYTE) {43    /* Bad single byte - either an invalid byte or an out-of-place continuation byte */44    length = 1;45  } else if (length > end - in) {46    /* String ends before UTF8 sequence ends */47    length = end - in;48  } else {49    codepoint = ((unsigned)in[0]) & utf8_coding_bits[first];50    for (int i=1; i<length; i++) {51      unsigned ch = (unsigned char)in[i];52      if (utf8_coding_length[ch] != UTF8_CONTINUATION_BYTE){53        /* Invalid UTF8 sequence - not followed by the right number of continuation bytes */54        codepoint = -1;55        length = i;56        break;57      }58      codepoint = (codepoint << 6) | (ch & 0x3f);59    }60    if (codepoint < utf8_first_codepoint[length]) {61      /* Overlong UTF8 sequence */62      codepoint = -1;63    }64    if (0xD800 <= codepoint && codepoint <= 0xDFFF) {65      /* Surrogate codepoints can't be encoded in UTF8 */66      codepoint = -1;67    }68    if (codepoint > 0x10FFFF) {69      /* Outside Unicode range */70      codepoint = -1;71    }72  }73  assert(length > 0);74  *codepoint_ret = codepoint;75  return in + length;76}7778int jvp_utf8_is_valid(const char* in, const char* end) {79  int codepoint;80  while ((in = jvp_utf8_next(in, end, &codepoint))) {81    if (codepoint == -1) return 0;82  }83  return 1;84}8586/* Assumes startchar is the first byte of a valid character sequence */87int jvp_utf8_decode_length(char startchar) {88	if ((startchar & 0x80) == 0) return 1;         // 0___ ____89	else if ((startchar & 0xE0) == 0xC0) return 2; // 110_ ____90	else if ((startchar & 0xF0) == 0xE0) return 3; // 1110 ____91	else return 4;                                 // 1111 ____92}9394int jvp_utf8_encode_length(int codepoint) {95  if (codepoint <= 0x7F) return 1;96  else if (codepoint <= 0x7FF) return 2;97  else if (codepoint <= 0xFFFF) return 3;98  else return 4;99}100101int jvp_utf8_encode(int codepoint, char* out) {102  assert(codepoint >= 0 && codepoint <= 0x10FFFF);103  char* start = out;104  if (codepoint <= 0x7F) {105    *out++ = codepoint;106  } else if (codepoint <= 0x7FF) {107    *out++ = 0xC0 + ((codepoint & 0x7C0) >> 6);108    *out++ = 0x80 + ((codepoint & 0x03F));109  } else if(codepoint <= 0xFFFF) {110    *out++ = 0xE0 + ((codepoint & 0xF000) >> 12);111    *out++ = 0x80 + ((codepoint & 0x0FC0) >> 6);112    *out++ = 0x80 + ((codepoint & 0x003F));113  } else {114    *out++ = 0xF0 + ((codepoint & 0x1C0000) >> 18);115    *out++ = 0x80 + ((codepoint & 0x03F000) >> 12);116    *out++ = 0x80 + ((codepoint & 0x000FC0) >> 6);117    *out++ = 0x80 + ((codepoint & 0x00003F));118  }119  assert(out - start == jvp_utf8_encode_length(codepoint));120  return out - start;121}122123// characters with White_Space property in:124// https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt125int jvp_codepoint_is_whitespace(int c) {126  return127    (c >= 0x0009 && c <= 0x000D) || // <control-0009>..<control-000D>128    c == 0x0020                  || // SPACE129    c == 0x0085                  || // <control-0085>130    c == 0x00A0                  || // NO-BREAK SPACE131    c == 0x1680                  || // OGHAM SPACE MARK132    (c >= 0x2000 && c <= 0x200A) || // EN QUAD..HAIR SPACE133    c == 0x2028                  || // LINE SEPARATOR134    c == 0x2029                  || // PARAGRAPH SEPARATOR135    c == 0x202F                  || // NARROW NO-BREAK SPACE136    c == 0x205F                  || // MEDIUM MATHEMATICAL SPACE137    c == 0x3000                     // IDEOGRAPHIC SPACE138    ;139}