File: encode_utf8.h

package info (click to toggle)
simdutf 7.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,968 kB
  • sloc: cpp: 59,437; ansic: 14,080; python: 3,135; sh: 311; makefile: 12
file content (40 lines) | stat: -rw-r--r-- 824 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
30
31
32
33
34
35
36
37
38
39
40
#pragma once

#include <cstdint>

namespace simdutf {
namespace tests {
namespace reference {
namespace utf8 {

template <typename CONSUMER> int encode(uint32_t value, CONSUMER consumer) {
  if (value < 0x00000080) {
    consumer(uint8_t(value));
    return 1;
  }

  if (value < 0x00000800) {
    consumer(0xc0 | (value >> 6));
    consumer(0x80 | (value & 0x3f));
    return 2;
  }

  if (value < 0x00010000) {
    consumer(0xe0 | (value >> 12));
    consumer(0x80 | ((value >> 6) & 0x3f));
    consumer(0x80 | (value & 0x3f));
    return 3;
  }

  {
    consumer(0xf0 | (value >> 18));
    consumer(0x80 | ((value >> 12) & 0x3f));
    consumer(0x80 | ((value >> 6) & 0x3f));
    consumer(0x80 | (value & 0x3f));
    return 4;
  }
}
} // namespace utf8
} // namespace reference
} // namespace tests
} // namespace simdutf