MCPcopy Create free account
hub / github.com/DFHack/dfhack / UTF2DF

Function UTF2DF

library/MiscUtils.cpp:623–665  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

621}
622
623std::string UTF2DF(const std::string &in)
624{
625 // Unicode to normal lookup table
626 static std::map<uint32_t, char> ctable;
627
628 if (ctable.empty())
629 {
630 for (uint16_t i = 0; i < 256; i++)
631 if (character_table[i] != i)
632 ctable[character_table[i]] = char(i);
633 }
634
635 // Actual conversion loop
636 size_t size = in.size();
637 std::string out(size, char(0));
638
639 uint32_t codepoint = 0;
640 uint32_t state = UTF8_ACCEPT, prev = UTF8_ACCEPT;
641 uint32_t pos = 0;
642
643 for (unsigned i = 0; i < size; prev = state, i++) {
644 switch (decode(&state, &codepoint, uint8_t(in[i]))) {
645 case UTF8_ACCEPT:
646 if (codepoint < 256 && character_table[codepoint] == codepoint) {
647 out[pos++] = char(codepoint);
648 } else {
649 char v = ctable[codepoint];
650 out[pos++] = v ? v : '?';
651 }
652 break;
653
654 case UTF8_REJECT:
655 out[pos++] = '?';
656 if (prev != UTF8_ACCEPT) --i;
657 state = UTF8_ACCEPT;
658 break;
659 }
660 }
661
662 if (pos != size)
663 out.resize(pos);
664 return out;
665}
666static bool console_is_utf8()
667{
668#ifdef LINUX_BUILD

Callers 5

utf2dfFunction · 0.85
getClipboardTextCp437Method · 0.85
RenameSquadFunction · 0.85
RenameUnitFunction · 0.85

Calls 4

decodeFunction · 0.70
emptyMethod · 0.45
sizeMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected