/// <summary> /// Unscape all symbols for given type /// </summary> /// <param name="str"></param> /// <param name="type"></param> /// <returns></returns> public static String Unescape(this String str, EscapingType type) { IEscapeProvider d = EscapingDataFactory.Get(type); StringBuilder res = new StringBuilder(str.Length); int offset = 0; foreach (var i in d.GetFirst()) { if (str.StartsWith(i.Value)) { res.Append(i.Key); offset = i.Value.Length; break; } } for (int i = offset; i < str.Length; i++) { String s = CheckSuffix(str, offset, i, d); if (s != null) { res.Append(s); offset = i + 1; } } return(res + str.Substring(offset)); }
/// <summary> /// /// </summary> /// <param name="c"></param> /// <param name="type"></param> /// <param name="first"></param> /// <returns></returns> public static bool IsLiteral(this char c, EscapingType type, bool first = false) { IEscapeProvider d = EscapingDataFactory.Get(type); return(!(first && d.GetFirst().ContainsKey(c) || d.GetCommon().ContainsKey(c) || d.NeedEscapeUnicode(c))); }
private static String EscapeCommon(char c, IEscapeProvider d) { String r; if (d.GetCommon().TryGetValue(c, out r)) { return(r); } return(EscapeUnicode(c, d)); }
private static String UnescapeEntity(this String s, IEscapeProvider d) { char r; if (d.GetUnescape().TryGetValue(s, out r)) { return(r.ToString()); } return(null); }
private static String EscapeUnicode(char c, IEscapeProvider d) { // escaping not defined if (d.GetUnicodePattern() == null) { return(c.ToString()); } if (!d.NeedEscapeUnicode(c)) { return(c.ToString()); } return(d.GetUnicodePattern().Replace(" ", ((int)c).ToString("X4"))); }
/// <summary> /// /// </summary> /// <param name="str"></param> /// <param name="type"></param> /// <returns></returns> public static bool IsLiteral(this String str, EscapingType type) { IEscapeProvider d = EscapingDataFactory.Get(type); if (d.GetFirst().ContainsKey(str[0])) { return(false); } foreach (char c in str) { if (d.GetCommon().ContainsKey(c)) { return(false); } } return(true); }
private static String CheckSuffix(String str, int start, int end, IEscapeProvider d) { String s; // the length of escaping code not constant so check all variants foreach (int j in d.GetUnescape().KeysLength) { if (j > end - start + 1) { continue; } s = str.Substring(end - j + 1, j); s = UnescapeEntity(s, d); if (s != null) { return(str.Substring(start, end - start - j + 1) + s); } } // check unicode if (d.GetUnicodePattern() == null) { return(null); } int p = d.GetUnicodePattern().Length + 3; if (p > end - start + 1) { return(null); } s = str.Substring(end - p + 1, p); s = UnescapeUnicode(s, d); if (s != null) { return(str.Substring(start, end - start - p + 1) + s); } return(null); }
private static String UnescapeUnicode(this String s, IEscapeProvider d) { String p = d.GetUnicodePattern(); if (p == null) { return(null); } if (p.Length + 3 == s.Length) { String hex = s.Substring(p.IndexOf(' '), 4); ushort value; if (ushort.TryParse(hex, NumberStyles.HexNumber, null, out value)) { return(((char)value).ToString()); } } return(null); }
/// <summary> /// Escape all symbols for given type /// </summary> /// <param name="str"></param> /// <param name="type"></param> /// <returns></returns> public static String Escape(this String str, EscapingType type) { if (type == EscapingType.BxlSinglelineString || type == EscapingType.BxlMultilineString) { if (string.IsNullOrEmpty(str)) { return("\"\""); } if (type == EscapingType.BxlSinglelineString) { return(ToBxlSingleLineString(str)); } if (type == EscapingType.BxlMultilineString) { return(ToBxlMultiLineString(str)); } } else if (type == EscapingType.BxlStringOrLiteral) { if (string.IsNullOrEmpty(str)) { return("\"\""); } if (str.IsLiteral(EscapingType.BxlLiteral) && !str.Contains("\\") && !str.Contains("#")) { return(str); } return(ToBxlSingleLineString(str)); } else if (type == EscapingType.JsonValue) { //идентично return(ToBxlSingleLineString(str, false)); } if (string.IsNullOrEmpty(str)) { return(string.Empty); } IEscapeProvider d = EscapingDataFactory.Get(type); var sb = new StringBuilder(str.Length); if (!IsLiteral(str[0], type, true)) { sb.Append(EscapeFirst(str[0], d)); } else { sb.Append(str[0]); } for (int i = 1; i < str.Length; i++) { if (!IsLiteral(str[i], type)) { sb.Append(EscapeCommon(str[i], d)); } else { sb.Append(str[i]); } } return(sb.ToString()); }
private static String UnescapeUnicode(this String s, IEscapeProvider d) { String p = d.GetUnicodePattern(); if (p == null) return null; if (p.Length + 3 == s.Length) { String hex = s.Substring(p.IndexOf(' '), 4); ushort value; if (ushort.TryParse(hex, NumberStyles.HexNumber, null, out value)) return ((char)value).ToString(); } return null; }
private static String UnescapeEntity(this String s, IEscapeProvider d) { char r; if (d.GetUnescape().TryGetValue(s, out r)) return r.ToString(); return null; }
private static String CheckSuffix(String str, int start, int end, IEscapeProvider d) { String s; // the length of escaping code not constant so check all variants foreach (int j in d.GetUnescape().KeysLength) { if (j > end - start + 1) continue; s = str.Substring(end - j + 1, j); s = UnescapeEntity(s, d); if (s != null) return str.Substring(start, end - start - j + 1) + s; } // check unicode if (d.GetUnicodePattern() == null) return null; int p = d.GetUnicodePattern().Length + 3; if (p > end - start + 1) return null; s = str.Substring(end - p + 1, p); s = UnescapeUnicode(s, d); if (s != null) return str.Substring(start, end - start - p + 1) + s; return null; }
private static String EscapeUnicode(char c, IEscapeProvider d) { // escaping not defined if (d.GetUnicodePattern() == null) return c.ToString(); if (!d.NeedEscapeUnicode(c)) return c.ToString(); return d.GetUnicodePattern().Replace(" ", ((int)c).ToString("X4")); }
private static String EscapeCommon(char c, IEscapeProvider d) { String r; if (d.GetCommon().TryGetValue(c, out r)) return r; return EscapeUnicode(c, d); }