MCPcopy Create free account
hub / github.com/Tablecruncher/tablecruncher / encodeUtf8

Method encodeUtf8

src/helper.cpp:744–771  ·  view source on GitHub ↗

Encode a code point as UTF-8 https://stackoverflow.com/questions/23502153/utf-8-encoding-algorithm-vs-utf-16-algorithm printf("TEST: %s\n",Helper::encodeUtf8(65).c_str()); => A printf("TEST: %s\n",Helper::encodeUtf8(0xA2).c_str()); => ¢ printf("TEST: %s\n",Helper::encodeUtf8(0x20AC).c_str()); => € printf("TEST: %s\n",Helper::encodeUtf8(0x1D11E).c_str()); => 𝄞

Source from the content-addressed store, hash-verified

742// printf("TEST: %s\n",Helper::encodeUtf8(0x20AC).c_str()); => €
743// printf("TEST: %s\n",Helper::encodeUtf8(0x1D11E).c_str()); => 𝄞
744std::string Helper::encodeUtf8(uint32_t cp) {
745 std::string ret = "";
746 unsigned char chars[4];
747
748 if( cp <= 0x7F ) {
749 ret = (char) cp;
750 } else if( cp > 0x10FFFF) {
751 ret = "\xEF\xBF\xBD"; // U+FFFD REPLACEMENT CHARACTER
752 } else {
753 int count;
754 if (cp <= 0x7FF) {
755 count = 1;
756 } else if (cp <= 0xFFFF) {
757 count = 2;
758 } else {
759 count = 3;
760 }
761 for (int i = 0; i < count; ++i) {
762 chars[count-i] = 0x80 | (cp & 0x3F);
763 cp >>= 6;
764 }
765 chars[0] = (0x1E << (6-count)) | (cp & (0x3F >> count));
766 for(int i = 0; i <= count; ++i) {
767 ret += chars[i];
768 }
769 }
770 return ret;
771}
772
773
774unsigned long Helper::getTimestamp() {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected