当前位置: 首页> 科技> 名企 > 石龙疫情最新消息_做网站公司上什么平台_靠谱的拉新平台_新闻稿代写

石龙疫情最新消息_做网站公司上什么平台_靠谱的拉新平台_新闻稿代写

时间:2025/7/11 18:20:58来源:https://blog.csdn.net/Flame_Cyclone/article/details/143060823 浏览次数:1次
石龙疫情最新消息_做网站公司上什么平台_靠谱的拉新平台_新闻稿代写

 main.cpp

#include <iostream>
#include <tchar.h>
#include <locale>
#include <string>#ifdef _UNICODE
#define __T(x)      L ## x
using _tstring = std::wstring;
using _tchar = wchar_t;
using _utchar = _tchar;#else#define __T(x)      x
using _tstring = std::string;
using _tchar = char;
using _utchar = unsigned char;#endifconst _tchar* _SkipStringWhitespace(const _tchar* pData);
bool _GetUnicodeCodePoint(const _tchar* pData, uint32_t* pCp, const _tchar** pEnd);
bool _ParseJsonString(const _tchar* pData, _tstring& val, const _tchar** pEnd, bool fFromFile);
_tstring _DumpJsonString(const _tstring strText, bool fEscapeCh);
std::string _CodePointToUtf8(uint32_t cp32);
const _tchar* _SkipStringWhitespace(const _tchar* pData);int main()
{setlocale(LC_ALL, "");_tstring strDump1 = _DumpJsonString(_T(R"("我是地球🌍")"), false);_tstring strDump2 = _DumpJsonString(_T(R"("我是地球🌍")"), true);_tstring strDump3;_ParseJsonString(_T(R"("我是地球🌍")"), strDump3, nullptr, true);_tstring strDump4;_ParseJsonString(_T(R"("我是地球🌍")"), strDump4, nullptr, true);return 0;
}std::string _CodePointToUtf8(uint32_t cp32)
{char szBuf[16] = { 0 };// 1字节 0xxxxxxxif (cp32 >= 0x00000000 && cp32 <= 0x0000007F){szBuf[0] = (uint8_t)cp32;szBuf[1] = 0;}// 2字节 110xxxxx 10xxxxxxif (cp32 >= 0x00000080 && cp32 <= 0x000007FF){szBuf[0] = ((cp32 >>  6) & 0x1F) | 0xC0;szBuf[1] = ((cp32 & 0x3F)) | 0x80;szBuf[2] = 0;}// 3字节 1110xxxx 10xxxxxx 10xxxxxxif (cp32 >= 0x00000800 && cp32 <= 0x0000FFFF){szBuf[0] = ((cp32 >> 12) & 0x0F) | 0xE0;szBuf[1] = ((cp32 >>  6) & 0x3F) | 0x80;szBuf[2] = ((cp32 & 0x3F)) | 0x80;szBuf[3] = 0;}// 4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxxif (cp32 >= 0x00010000 && cp32 <= 0x001FFFFF){szBuf[0] = ((cp32 >> 18) & 0x07) | 0xF0;szBuf[1] = ((cp32 >> 12) & 0x3F) | 0x80;szBuf[2] = ((cp32 >>  6) & 0x3F) | 0x80;szBuf[3] = ((cp32 & 0x3F)) | 0x80;szBuf[4] = 0;}// 5字节 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxxif (cp32 >= 0x00200000 && cp32 <= 0x03FFFFFF){szBuf[0] = ((cp32 >> 24) & 0x03) | 0xF8;szBuf[1] = ((cp32 >> 18) & 0x3F) | 0x80;szBuf[2] = ((cp32 >> 12) & 0x3F) | 0x80;szBuf[3] = ((cp32 >>  6) & 0x3F) | 0x80;szBuf[4] = ((cp32 & 0x3F)) | 0x80;szBuf[5] = 0;}// 6字节 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxxif (cp32 >= 0x04000000 && cp32 <= 0x7FFFFFFF){szBuf[0] = ((cp32 >> 30) & 0x01) | 0xFC;szBuf[1] = ((cp32 >> 24) & 0x3F) | 0x80;szBuf[2] = ((cp32 >> 18) & 0x3F) | 0x80;szBuf[3] = ((cp32 >> 12) & 0x3F) | 0x80;szBuf[4] = ((cp32 >>  6) & 0x3F) | 0x80;szBuf[5] = ((cp32 & 0x3F)) | 0x80;szBuf[6] = 0;}return szBuf;
}const _tchar* _SkipStringWhitespace(const _tchar* pData)
{while (_T('\0') != *pData){if (_T(' ' == *pData) ||_T('\t' == *pData) ||_T('\r' == *pData) ||_T('\n' == *pData) ||_T('\f' == *pData)){pData++;}else{break;}}return pData;
}bool _GetUnicodeCodePoint(const _tchar* pData, uint32_t* pCp, const _tchar** pEnd)
{_tchar szBuf[16] = { 0 };_tchar* pChEnd = nullptr;bool fResult = false;do{int count = 0;for (count = 0; count < 4; count++){_tchar ch = pData[count];if (0 == _istxdigit(ch)){break;}szBuf[count] = ch;}if (4 != count){break;}if (pCp){*pCp = _tcstol(szBuf, &pChEnd, 16);}fResult = true;} while (false);if (pEnd){*pEnd = pData;}return fResult;
}bool _ParseJsonString(const _tchar* pData, _tstring& val, const _tchar** pEnd, bool fFromFile)
{const _tchar* pStart = pData;bool fAbort = false;// 跳过空格pData = _SkipStringWhitespace(pData);// 检查双引号if (_T('\"') != *pData){return false;}pData++;pStart = pData;_tstring strResult;while (_T('\0') != *pData){_tchar ch = *pData;if (_T('\"') == ch){break;}if (fFromFile){if (_T('\\') == ch){pData++;ch = *pData;switch (ch){case _T('\"'):{strResult.push_back(_T('\"'));}break;case _T('\\'):{strResult.push_back(_T('\\'));}break;case _T('/'):{strResult.push_back(_T('/'));}break;case _T('b'):{strResult.push_back(_T('\b'));}break;case _T('n'):{strResult.push_back(_T('\n'));}break;case _T('r'):{strResult.push_back(_T('\r'));}break;case _T('t'):{strResult.push_back(_T('\t'));}break;case _T('u'):{pData++;uint32_t cp32 = 0;if (!_GetUnicodeCodePoint(pData, &cp32, &pData)){fAbort = true;break;}// 高位if (cp32 >= 0xD800 && cp32 <= 0xDBFF){cp32 -= 0xD800;if (0 != _tcsnicmp(_T(R"(\u)"), pData, 2)){fAbort = true;break;}pData += 2;uint32_t cpLo = 0;if (!_GetUnicodeCodePoint(pData, &cpLo, &pData)){fAbort = true;break;}// 低位if (cpLo >= 0xDC00 && cpLo <= 0xDFFF){cpLo -= 0xDC00;cp32 = 0x10000 + ((cp32 << 10) | cpLo);
#ifdef _UNICODEuint16_t cp = (uint16_t)(cp32 - 0x10000);uint16_t cp32Hi = (uint16_t)(cp >> 10) + 0xD800;uint16_t cp32Lo = (uint16_t)(cp & 0x3FF) + 0xDC00;strResult.push_back(cp32Hi);strResult.push_back(cp32Lo);
#elsestrResult += _CodePointToUtf8(cp32);
#endif}else{fAbort = true;break;}}else{
#ifdef _UNICODEstrResult.push_back((_tchar)cp32);
#elsestrResult += _CodePointToUtf8(cp32);
#endif}continue;}break;default:pData--;fAbort = true;break;}}else{strResult.push_back(ch);}}else{strResult.push_back(ch);}if (fAbort){break;}pData++;}// 检查双引号if (_T('\"') != *pData || fAbort){*pEnd = pData;return false;}pData++;val = strResult;if (pEnd){*pEnd = pData;}return true;
}_tstring _DumpJsonString(const _tstring strText, bool fEscapeCh)
{const _tchar* pData = strText.c_str();_tstring strResult;strResult.reserve(strText.size());while (_T('\0') != *pData){_utchar ch = *pData;if (_T('\"') == ch){strResult += _T(R"(\")");}else if (_T('\\') == ch){strResult += _T(R"(\\)");}else if (_T('/') == ch){strResult += _T(R"(/)");}else if (_T('\b') == ch){strResult += _T(R"(\b)");}else if (_T('\f') == ch){strResult += _T(R"(\f)");}else if (_T('\n') == ch){strResult += _T(R"(\n)");}else if (_T('\r') == ch){strResult += _T(R"(\r)");}else if (_T('\t') == ch){strResult += _T(R"(\t)");}else{
#ifdef _UNICODE_tchar szBuf[32] = { 0 };if (ch < 0x80 || !fEscapeCh){strResult.push_back(ch);pData++;continue;}_stprintf_s(szBuf, sizeof(szBuf) / sizeof(_tchar), _T(R"(\u%0.4x)"), ch);strResult += szBuf;#elsebool fResult = true;if (ch < 0x80 || !fEscapeCh){strResult.push_back(ch);pData++;continue;}if (ch >= 0xC0){uint8_t u8CodeMask = 0xC0;     // 11000000uint8_t u8DataMask = 0x1F;      // 000xxxxxint nCount = 2;                 // 有效字节数量: 2-6// 检索字符使用的字节数量size_t nByteCount = 0;uint32_t cp32 = 0;while (u8CodeMask <= 0xFC){uint8_t u8MaskMax = u8CodeMask | u8DataMask;if (ch >= u8CodeMask && ch <= u8MaskMax){cp32 = ch & u8DataMask;nByteCount = nCount;break;}u8CodeMask = (u8CodeMask >> 1) | 0x80;u8DataMask = u8DataMask >> 1;nCount++;}if (0 == nByteCount){fResult = false;break;}for (size_t i = 1; i < nByteCount; i++){cp32 = cp32 << 6;cp32 |= pData[i] & 0x3F;}char szBuf[32] = { 0 };if (cp32 < 0x10000){sprintf_s(szBuf, sizeof(szBuf), R"(\u%0.4x)", cp32);strResult += szBuf;}else{uint32_t cp = (uint16_t)(cp32 - 0x10000);uint16_t cp32Hi = (uint16_t)(cp >> 10) + 0xD800;uint16_t cp32Lo = (uint16_t)(cp & 0x3FF) + 0xDC00;sprintf_s(szBuf, sizeof(szBuf), R"(\u%0.4x)", cp32Hi);strResult += szBuf;sprintf_s(szBuf, sizeof(szBuf), R"(\u%0.4x)", cp32Lo);strResult += szBuf;}pData += nByteCount;continue;}
#endif}pData++;}return strResult;
}

关键字:石龙疫情最新消息_做网站公司上什么平台_靠谱的拉新平台_新闻稿代写

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: