mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-08 11:38:21 +02:00
Merge pull request #1501 from EinScott/corlib-fix
string quote/unquote/escape/unescape fix
This commit is contained in:
commit
cab7f3cdb7
2 changed files with 66 additions and 52 deletions
|
@ -446,37 +446,14 @@ namespace System
|
|||
{
|
||||
strBuffer.Append(this);
|
||||
}
|
||||
|
||||
[Obsolete("Replaced with Quote", false)]
|
||||
public static void QuoteString(char8* ptr, int length, String outString) => Quote(ptr, length, outString);
|
||||
|
||||
public static void Quote(char8* ptr, int length, String outString)
|
||||
{
|
||||
outString.Append('"');
|
||||
for (int i < length)
|
||||
{
|
||||
char8 c = ptr[i];
|
||||
switch (c)
|
||||
{
|
||||
case '\'': outString.Append(@"\'");
|
||||
case '\"': outString.Append("\\\"");
|
||||
case '\\': outString.Append(@"\\");
|
||||
case '\0': outString.Append(@"\0");
|
||||
case '\a': outString.Append(@"\a");
|
||||
case '\b': outString.Append(@"\b");
|
||||
case '\f': outString.Append(@"\f");
|
||||
case '\n': outString.Append(@"\n");
|
||||
case '\r': outString.Append(@"\r");
|
||||
case '\t': outString.Append(@"\t");
|
||||
case '\v': outString.Append(@"\v");
|
||||
default:
|
||||
if (c < (char8)32)
|
||||
{
|
||||
outString.Append(@"\x");
|
||||
outString.Append(sHexUpperChars[((int)c>>4) & 0xF]);
|
||||
outString.Append(sHexUpperChars[(int)c & 0xF]);
|
||||
break;
|
||||
}
|
||||
outString.Append(c);
|
||||
}
|
||||
}
|
||||
Escape(ptr, length, outString);
|
||||
outString.Append('"');
|
||||
}
|
||||
|
||||
|
@ -485,12 +462,45 @@ namespace System
|
|||
Quote(Ptr, Length, outString);
|
||||
}
|
||||
|
||||
[Obsolete("Replaced with Quote", false)]
|
||||
public void QuoteString(String outString)
|
||||
public static void Escape(char8* ptr, int length, String outString)
|
||||
{
|
||||
Quote(Ptr, Length, outString);
|
||||
for (int i < length)
|
||||
{
|
||||
char8 c = ptr[i];
|
||||
switch (c)
|
||||
{
|
||||
case '\'': outString.Append(@"\'");
|
||||
case '\"': outString.Append("\\\"");
|
||||
case '\\': outString.Append(@"\\");
|
||||
case '\0': outString.Append(@"\0");
|
||||
case '\a': outString.Append(@"\a");
|
||||
case '\b': outString.Append(@"\b");
|
||||
case '\f': outString.Append(@"\f");
|
||||
case '\n': outString.Append(@"\n");
|
||||
case '\r': outString.Append(@"\r");
|
||||
case '\t': outString.Append(@"\t");
|
||||
case '\v': outString.Append(@"\v");
|
||||
default:
|
||||
if (c < (char8)32)
|
||||
{
|
||||
outString.Append(@"\x");
|
||||
outString.Append(sHexUpperChars[((int)c>>4) & 0xF]);
|
||||
outString.Append(sHexUpperChars[(int)c & 0xF]);
|
||||
break;
|
||||
}
|
||||
outString.Append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Escape(String outString)
|
||||
{
|
||||
Escape(Ptr, Length, outString);
|
||||
}
|
||||
|
||||
[Obsolete("Replaced with Unquote", false)]
|
||||
public static Result<void> UnQuoteString(char8* ptr, int length, String outString) => Unquote(ptr, length, outString);
|
||||
|
||||
public static Result<void> Unquote(char8* ptr, int length, String outString)
|
||||
{
|
||||
if (length < 2)
|
||||
|
@ -504,11 +514,9 @@ namespace System
|
|||
}
|
||||
|
||||
if ((*ptr != '\"') && (ptr[length - 1] != '\"'))
|
||||
{
|
||||
return .Err;
|
||||
}
|
||||
|
||||
return Unescape(ptr, length, outString);
|
||||
return Unescape(ptr + 1, length - 2, outString);
|
||||
}
|
||||
|
||||
public Result<void> Unquote(String outString)
|
||||
|
@ -516,20 +524,10 @@ namespace System
|
|||
return Unquote(Ptr, Length, outString);
|
||||
}
|
||||
|
||||
[Obsolete("Replaced with Unquote", false)]
|
||||
public Result<void> UnQuoteString(String outString)
|
||||
{
|
||||
return Unquote(outString);
|
||||
}
|
||||
|
||||
public static Result<void> Unescape(char8* ptr, int length, String outString)
|
||||
{
|
||||
if (length < 2)
|
||||
return .Err;
|
||||
|
||||
var ptr;
|
||||
ptr++;
|
||||
char8* endPtr = ptr + length - 2;
|
||||
char8* endPtr = ptr + length;
|
||||
|
||||
while (ptr < endPtr)
|
||||
{
|
||||
|
@ -553,6 +551,24 @@ namespace System
|
|||
case 'r': outString.Append("\r");
|
||||
case 't': outString.Append("\t");
|
||||
case 'v': outString.Append("\v");
|
||||
case 'x':
|
||||
uint8 num = 0;
|
||||
for (let i < 2)
|
||||
{
|
||||
if (ptr == endPtr)
|
||||
return .Err;
|
||||
let hexC = *(ptr++);
|
||||
|
||||
if ((hexC >= '0') && (hexC <= '9'))
|
||||
num = num*0x10 + (uint8)(hexC - '0');
|
||||
else if ((hexC >= 'A') && (hexC <= 'F'))
|
||||
num = num*0x10 + (uint8)(c - 'A') + 10;
|
||||
else if ((hexC >= 'a') && (hexC <= 'f'))
|
||||
num = num*0x10 + (uint8)(hexC - 'a') + 10;
|
||||
else return .Err;
|
||||
}
|
||||
|
||||
outString.Append((char8)num);
|
||||
default:
|
||||
return .Err;
|
||||
}
|
||||
|
@ -565,10 +581,10 @@ namespace System
|
|||
return .Ok;
|
||||
}
|
||||
|
||||
public Result<void> Unescape(String outString)
|
||||
{
|
||||
return Unescape(Ptr, Length, outString);
|
||||
}
|
||||
public Result<void> Unescape(String outString)
|
||||
{
|
||||
return Unescape(Ptr, Length, outString);
|
||||
}
|
||||
|
||||
static String sHexUpperChars = "0123456789ABCDEF";
|
||||
public void ToString(String outString, String format, IFormatProvider formatProvider)
|
||||
|
|
|
@ -1283,10 +1283,8 @@ namespace System.Reflection
|
|||
strBuffer.Append((*(bool*)&mValue) ? "true" : "false");
|
||||
case typeof(char8), typeof(char16), typeof(char32):
|
||||
strBuffer.Append('\'');
|
||||
var str = (*(char32*)&mValue).ToString(.. scope .());
|
||||
let len = str.Length;
|
||||
String.Quote(&str[0], len, str);
|
||||
strBuffer.Append(str[(len + 1)...^2]);
|
||||
let str = (*(char32*)&mValue).ToString(.. scope .(4));
|
||||
str.Escape(strBuffer);
|
||||
strBuffer.Append('\'');
|
||||
case typeof(uint64), typeof(uint):
|
||||
(*(uint64*)&mValue).ToString(strBuffer);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue