using System;
using System.Collections.Generic;
using System.Text;
namespace QiHe.CodeLib
{
///
/// TextEncoding
///
public class TextEncoding
{
///
/// check if all charaters in text are ASCII charater
///
///
///
public static bool FitsInASCIIEncoding(string text)
{
if(string.IsNullOrEmpty(text))return true;
byte[] bytes = Encoding.UTF8.GetBytes(text);
for (int i = 0; i < bytes.Length; i++)
{
if (bytes[i] > 127)
{
return false;
}
}
return true;
}
///
/// Encodings the is right.
///
/// The encoding.
/// The data.
///
public static bool EncodingIsRight(Encoding encoding, byte[] data)
{
string text = encoding.GetString(data);
byte[] bytes = encoding.GetBytes(text);
if (Algorithm.ArrayEqual(bytes, data))
{
return true;
}
else
{
return false;
}
}
///
/// Safes the decode string.
///
/// The encoding.
/// The data.
///
public static object SafeDecodeString(Encoding encoding, byte[] data)
{
string text = encoding.GetString(data);
byte[] bytes = encoding.GetBytes(text);
if (Algorithm.ArrayEqual(bytes, data))
{
return text;
}
else
{
return data;
}
}
///
/// Group text by encoding.
///
///
///
public static List> GroupTextByEncoding(string text)
{
List> frags = new List>();
StringBuilder buffer = new StringBuilder();
string encoding = "ascii";
foreach (char ch in text)
{
if (encoding == "ascii" && ch > 0x7f)
{
if (buffer.Length > 0)
{
frags.Add(new Pair("ascii", buffer.ToString()));
buffer.Length = 0;
}
encoding = "unicode";
}
else if (encoding == "unicode" && ch <= 0x7f)
{
if (buffer.Length > 0)
{
frags.Add(new Pair("unicode", buffer.ToString()));
buffer.Length = 0;
}
encoding = "ascii";
}
buffer.Append(ch);
}
if (buffer.Length > 0)
{
frags.Add(new Pair(encoding, buffer.ToString()));
}
return frags;
}
public static string RemoveByteOrderMark(string text)
{
if (text[0] == 0xFEFF || text[0] == 0xFFFE)
{
return text.Substring(1);
}
else
{
return text;
}
}
}
}