Fixed String escaping

master
Harald Wolff 2019-08-30 12:41:45 +02:00
parent 6add70bde4
commit 72ec888c45
1 changed files with 10 additions and 4 deletions

View File

@ -1,5 +1,7 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
namespace ln.json
{
@ -16,6 +18,8 @@ namespace ln.json
System.Tuple.Create('t','\t'),
};
static HashSet<char> escapeSet = new HashSet<char>(escapeCharacters.Select((x) => x.Item2));
public string Value { get; private set; }
public override object ToNative() => Value;
@ -31,11 +35,9 @@ namespace ln.json
}
public static string Escape(string source){
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder();
foreach (char ch in source){
if ((ch >= 0x20) && (ch < 128)) {
sb.Append(ch);
} else if (ch < 0x20) {
if (escapeSet.Contains(ch)) {
foreach (System.Tuple<char, char> repl in escapeCharacters) {
if (repl.Item2 == ch) {
sb.Append("\\");
@ -43,6 +45,10 @@ namespace ln.json
break;
}
}
}
else if (ch < 128)
{
sb.Append(ch);
} else {
byte[] bytes = BitConverter.GetBytes((int)ch);
sb.Append("\\u");