This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/core/text/tokenizer_nfc.cpp
T
2026-10-04 12:02:41 +02:00

130 lines
5.9 KiB
C++

#include "text/tokenizer_nfc.h"
#include "text/unicode_norm.h"
#include <algorithm>
#include <cstring>
#include <vector>
namespace rad {
namespace {
/* NFC, applied only where it can change something.
*
* UAX #15's quick check: a codepoint whose NFC_QC is Yes and whose combining class is 0 is a
* BOUNDARY -- nothing before it can compose with it or reorder across it, and nothing it becomes
* can reach back past it -- so normalising the text between two boundaries on its own gives exactly
* the bytes normalising the whole text gives there. The text is scanned for the codepoints that
* are not boundaries (NFC_QC No or Maybe, a mark out of canonical order, anything not strictly
* well-formed), and only the stretch from the boundary before each one to the boundary after it
* goes through unorm_utf8; everything else is already NFC and is copied. The properties are
* derived from the same tables unorm_utf8 runs on -- "No" is a codepoint NFC does not leave alone
* by itself, "Maybe" is one that can compose with what precedes it -- so the scan agrees with that
* implementation rather than with some other one. Invalid bytes are never at a boundary, so the
* decoder's replacement of them happens inside a normalised stretch, byte for byte as it does when
* the whole text is normalised. */
class NfcQuick {
public:
NfcQuick() : not_yes_(kWords, 0), marked_(kWords, 0) {
for (size_t i = 0; i < k_decomp_rows_n; ++i) {
const DecompRow& r = k_decomp_rows[i];
if (r.len_c == 0) continue;
if (unorm_nfc({ r.cpt }) != std::vector<uint32_t>{ r.cpt }) set(not_yes_, r.cpt);
}
for (size_t i = 0; i < k_compose_n; ++i) set(not_yes_, k_compose[i].b);
for (uint32_t c = 0x1161; c <= 0x1175; ++c) set(not_yes_, c); /* Hangul V, composes after L */
for (uint32_t c = 0x11A8; c <= 0x11C2; ++c) set(not_yes_, c); /* Hangul T, composes after LV */
for (size_t i = 0; i < k_ccc_n; ++i)
for (uint32_t c = k_ccc[i].first; c <= k_ccc[i].last; ++c) set(marked_, c);
}
/* Normalise `text` in place; false when it was already NFC and is untouched. */
bool apply(std::string& text) const {
const uint8_t* p = (const uint8_t*)text.data();
const size_t n = text.size();
std::string out;
size_t copied = 0; /* text[0, copied) is in `out` */
size_t boundary = 0; /* start of the last boundary codepoint seen */
uint32_t last = 0; /* combining class of the previous codepoint */
for (size_t i = 0; i < n; ) {
if (i + 8 <= n) {
uint64_t w;
std::memcpy(&w, p + i, 8);
if (!(w & 0x8080808080808080ull)) { last = 0; boundary = i + 7; i += 8; continue; }
}
uint32_t len, ccc;
const int k = classify(p + i, n - i, &len, &ccc);
if (k == kBoundary) { last = 0; boundary = i; i += len; continue; }
if (k == kKeep && !(ccc != 0 && last > ccc)) { last = ccc; i += len; continue; }
/* Something NFC may change: normalise from the boundary before it to the next one. */
size_t end = i + len;
while (end < n) {
uint32_t l2, c2;
if (classify(p + end, n - end, &l2, &c2) == kBoundary) break;
end += l2;
}
const size_t start = std::max(boundary, copied);
out.append(text, copied, start - copied);
out += unorm_utf8(text.substr(start, end - start), false, true);
copied = end;
boundary = end;
last = 0;
i = end;
}
if (copied == 0) return false;
out.append(text, copied, std::string::npos);
text.swap(out);
return true;
}
private:
enum { kBoundary, kKeep, kChange };
static constexpr uint32_t kInvalidLen = UINT32_MAX;
static constexpr size_t kWords = (0x110000 + 63) / 64;
static void set(std::vector<uint64_t>& b, uint32_t c) { b[c >> 6] |= 1ull << (c & 63); }
static bool get(const std::vector<uint64_t>& b, uint32_t c) { return (b[c >> 6] >> (c & 63)) & 1; }
/* kBoundary: a well-formed codepoint with class 0 and NFC_QC Yes. kKeep: well-formed, NFC_QC
* Yes, a nonzero class (in `ccc`) the caller checks for order. kChange: anything else; an
* invalid byte reports a length of 1 and kInvalidLen in `ccc`. */
int classify(const uint8_t* p, size_t n, uint32_t* len, uint32_t* ccc) const {
const uint32_t b = p[0];
*ccc = 0;
if (b < 0x80) { *len = 1; return kBoundary; }
uint32_t c;
auto cont = [&](size_t k) { return k < n && (p[k] & 0xC0) == 0x80; };
if (b >= 0xC2 && b < 0xE0 && cont(1)) {
c = ((b & 0x1F) << 6) | (p[1] & 0x3F);
*len = 2;
} else if (b >= 0xE0 && b < 0xF0 && cont(1) && cont(2)) {
c = ((b & 0x0F) << 12) | ((p[1] & 0x3F) << 6) | (p[2] & 0x3F);
*len = 3;
if (c < 0x800 || (c >= 0xD800 && c <= 0xDFFF)) { *len = 1; *ccc = kInvalidLen; return kChange; }
} else if (b >= 0xF0 && b < 0xF5 && cont(1) && cont(2) && cont(3)) {
c = ((b & 0x07) << 18) | ((p[1] & 0x3F) << 12) | ((p[2] & 0x3F) << 6) | (p[3] & 0x3F);
*len = 4;
if (c < 0x10000 || c > 0x10FFFF) { *len = 1; *ccc = kInvalidLen; return kChange; }
} else {
*len = 1;
*ccc = kInvalidLen;
return kChange;
}
if (get(not_yes_, c)) { *ccc = get(marked_, c) ? unorm_ccc(c) : 0; return kChange; }
if (!get(marked_, c)) return kBoundary;
*ccc = unorm_ccc(c);
return *ccc ? kKeep : kBoundary;
}
std::vector<uint64_t> not_yes_; /* NFC_QC is No or Maybe */
std::vector<uint64_t> marked_; /* canonical combining class is not 0 */
};
} /* namespace */
bool tok_nfc(std::string& text) {
static const NfcQuick q;
return q.apply(text);
}
} /* namespace rad */