UTF-16 surrogatization

C

Public Domain

Converts between Unicode codepoints and UTF-16 surrogate pairs

Download (right click, save as, rename as appropriate)

Embed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <errno.h>

int* surrogate(long ch) {
 static int surrs[2];
 if (0xFFFF < ch && ch <= 0x10FFFF) {
  ch -= 0x10000;
  surrs[0] = 0xD800 + (ch >> 10);
  surrs[1] = 0xDC00 + (ch & 0x3FF);
  return surrs;
 } else {errno = EINVAL; return NULL; }
}

long desurrogate(int high, int low) {
 if (0xD800 <= high && high <= 0xDBFF && 0xDC00 <= low && low <= 0xDFFF) {
  return (high - 0xD800) << 10 + (low - 0xDC00) + (long) 0x10000;
 } else {errno = EINVAL; return -1; }
}