File: decode_utf32.h

package info (click to toggle)
simdutf 7.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,244 kB
  • sloc: cpp: 60,074; ansic: 14,226; python: 3,364; sh: 321; makefile: 12
file content (47 lines) | stat: -rw-r--r-- 1,016 bytes parent folder | download | duplicates (2)
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
41
42
43
44
45
46
47
#pragma once

#include <cstdio>

namespace simdutf {
namespace tests {
namespace reference {
namespace utf32 {

enum class Error { too_large, forbidden_range };

template <typename CONSUMER, typename ERROR_HANDLER>
bool decode(const char32_t *codepoints, size_t size, CONSUMER consumer,
            ERROR_HANDLER error_handler) {
  const char32_t *curr = codepoints;
  const char32_t *end = codepoints + size;

  while (curr != end) {
    const uint32_t word = *curr;

    if (word > 0x10FFFF) {
      if (!error_handler(codepoints, curr, Error::too_large))
        return false;

      continue;
    }

    if (word >= 0xD800 && word <= 0xDFFF) { // required the next word, but we're
                                            // already at the end of data
      if (!error_handler(codepoints, curr, Error::forbidden_range))
        return false;

      break;
    }

    consumer(word);

    curr++;
  }

  return true;
}

} // namespace utf32
} // namespace reference
} // namespace tests
} // namespace simdutf