File: putu8.c

package info (click to toggle)
uniutils 2.28-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,032 kB
  • sloc: ansic: 49,905; sh: 1,168; awk: 55; makefile: 18
file content (29 lines) | stat: -rw-r--r-- 597 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * Takes a UTF-32 code as input and outputs UTF-8.
 * Based on  Roman Czyborra:  http://czyborra.com/utf/
 */
#include <stdio.h>

void
putu8(unsigned long c)
{
  if (c < 0x80) {		/* ASCII */
    putchar (c);
  }
  else if (c < 0x800) {
    putchar (0xC0 | c>>6);
    putchar (0x80 | c & 0x3F);
  }
  else if (c < 0x10000) {
    putchar (0xE0 | c>>12);
    putchar (0x80 | c>>6 & 0x3F);
    putchar (0x80 | c & 0x3F);
  }
  else if (c < 0x200000) {	/* 2^21 */
    putchar (0xF0 | c>>18);
    putchar (0x80 | c>>12 & 0x3F);
    putchar (0x80 | c>>6 & 0x3F);
    putchar (0x80 | c & 0x3F);
  }
}