我正在尝试组合一个函数来将 PSTR 转换为 PWSTR,同时转义某些字符。我无法控制 PSTR 包含的输入。它可以是任何网页上的内容。
我已经有以下代码,但我想知道是否有更好/更有效的方法,或者我所拥有的是否可以工作。
__inline PCWSTR ToEscape(CHAR c)
{
switch (c)
{
case '"': return L"\"";
case '\\': return L"\\";
case '\b': return L"\b";
case '\f': return L"\f";
case '\n': return L"\n";
case '\r': return L"\r";
case '\t': return L"\t";
default: return NULL;
}
}
// Helper for mbstowcs_s that does not return NULL terminator included in size
__inline DWORD CharToWChar(__out_ecount_opt(cchBuffer) WCHAR* pszBuffer, __in DWORD cchBuffer, __in_ecount(valueLen) PCSTR value, __in DWORD valueLen)
{
if (valueLen == 0) return 0;
size_t dwSize = 0;
mbstowcs_s(&dwSize, pszBuffer, cchBuffer, value, valueLen);
return dwSize - 1;
}
// Writes the input value to pszBuffer, or if pszBuffer is NULL, returns the length necessary to write value
__inline DWORD WriteJsonValue(__out_ecount_opt(cchBuffer) WCHAR* pszBuffer, __in DWORD cchBuffer, __in PCSTR value)
{
if (pszBuffer == NULL)
{
DWORD offset = 2;
PCSTR start = value;
DWORD index = 0;
while (value[index] != '\0')
{
PCWSTR Escape = ToEscape(value[index]);
if (Escape)
{
offset += CharToWChar(NULL, 0, start, (value + index) - start);
start = value + index + 1;
offset += 2;
}
index++;
}
// Any more left to write?
offset += CharToWChar(NULL, 0, start, (value + index) - start);
return offset;
}
else
{
DWORD offset = 0;
if (cchBuffer < 1) return 0;
pszBuffer[offset++] = L'"';
PCSTR start = value;
DWORD index = 0;
while (value[index] != '\0')
{
PCWSTR Escape = ToEscape(value[index]);
if (Escape)
{
DWORD dwLen = (value + index) - start;
offset += CharToWChar(pszBuffer+offset, cchBuffer-offset, start, dwLen);
start = value + index + 1;
if (cchBuffer < offset + 2) return offset;
pszBuffer[offset++] = Escape[0];
pszBuffer[offset++] = Escape[1];
}
index++;
}
// Any more left to write?
DWORD dwLen = (value + index) - start;
offset += CharToWChar(pszBuffer+offset, cchBuffer-offset, start, dwLen);
if (cchBuffer < offset + 1) return offset;
pszBuffer[offset++] = L'"';
return offset;
}
}