+// For debugging.
+string print_trigram(uint32_t trgm)
+{
+ char ch[3] = {
+ char(trgm & 0xff), char((trgm >> 8) & 0xff), char((trgm >> 16) & 0xff)
+ };
+
+ string str = "'";
+ for (unsigned i = 0; i < 3;) {
+ if (ch[i] == '\\') {
+ str.push_back('\\');
+ str.push_back(ch[i]);
+ ++i;
+ } else if (int(ch[i]) >= 32 && int(ch[i]) <= 127) { // Holds no matter whether char is signed or unsigned.
+ str.push_back(ch[i]);
+ ++i;
+ } else {
+ // See if we have an entire UTF-8 codepoint, and that it's reasonably printable.
+ mbtowc(nullptr, 0, 0);
+ wchar_t pwc;
+ int ret = mbtowc(&pwc, ch + i, 3 - i);
+ if (ret >= 1 && pwc >= 32) {
+ str.append(ch + i, ret);
+ i += ret;
+ } else {
+ char buf[16];
+ snprintf(buf, sizeof(buf), "\\x{%02x}", (unsigned char)ch[i]);
+ str += buf;
+ ++i;
+ }
+ }
+ }
+ str += "'";
+ return str;
+}
+