Facebook
From aaaa, 1 Month ago, written in C++.
Embed
Download Paste or View Raw
Hits: 145
  1. std::string decodeUTF8EscapeCodes(const std::string& input) {
  2.     std::stringstream result;
  3.  
  4.     for (size_t i = 0; i < input.size(); ++i) {
  5.         if (input[i] == '' && input[i + 1] == 'u') {
  6.             // Extracting hexadecimal value
  7.             std::string hexCode = input.substr(i + 2, 4);
  8.             unsigned int unicodeValue = std::stoi(hexCode, nullptr, 16);
  9.  
  10.             // Handling UTF-8 encoding
  11.             if (unicodeValue <= 0x7F) {
  12.                 result << static_cast<char>(unicodeValue);
  13.             } else if (unicodeValue <= 0x7FF) {
  14.                 result << static_cast<char>(0xC0 | ((unicodeValue >> 6) & 0x1F));
  15.                 result << static_cast<char>(0x80 | (unicodeValue & 0x3F));
  16.             } else if (unicodeValue <= 0xFFFF) {
  17.                 result << static_cast<char>(0xE0 | ((unicodeValue >> 12) & 0x0F));
  18.                 result << static_cast<char>(0x80 | ((unicodeValue >> 6) & 0x3F));
  19.                 result << static_cast<char>(0x80 | (unicodeValue & 0x3F));
  20.             } else if (unicodeValue <= 0x10FFFF) {
  21.                 result << static_cast<char>(0xF0 | ((unicodeValue >> 18) & 0x07));
  22.                 result << static_cast<char>(0x80 | ((unicodeValue >> 12) & 0x3F));
  23.                 result << static_cast<char>(0x80 | ((unicodeValue >> 6) & 0x3F));
  24.                 result << static_cast<char>(0x80 | (unicodeValue & 0x3F));
  25.             }
  26.             // Move ahead by 5 characters as uXXXX is 6 characters in total
  27.             i += 5;
  28.         } else {
  29.             result << input[i];
  30.         }
  31.     }
  32.  
  33.     return result.str();
  34. }