123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- using System;
- using System.Text;
- using System.Collections.Generic;
- using System.IO;
- public class Str
- {
- static Encoding asciiEncoding = Encoding.ASCII;
- public static Encoding AsciiEncoding
- {
- get { return Str.asciiEncoding; }
- }
- static Encoding utf8Encoding = Encoding.UTF8;
- public static Encoding Utf8Encoding
- {
- get { return Str.utf8Encoding; }
- }
- static Encoding uniEncoding = Encoding.Unicode;
- public static Encoding UniEncoding
- {
- get { return Str.uniEncoding; }
- }
- public static string ByteToHex3(byte[] data)
- {
- if (data.Length == 0)
- {
- return "";
- }
- return BitConverter.ToString(data) + "-";
- }
- public static string ByteToHex(byte[] data)
- {
- StringBuilder ret = new StringBuilder();
- foreach (byte b in data)
- {
- string s = b.ToString("X");
- if (s.Length == 1)
- {
- s = "0" + s;
- }
- ret.Append(s);
- }
- return ret.ToString();
- }
- public static byte[] HexToByte(string str)
- {
- try
- {
- List<byte> o = new List<byte>();
- string tmp = "";
- int i, len;
- str = str.ToUpper().Trim();
- len = str.Length;
- for (i = 0; i < len; i++)
- {
- char c = str[i];
- if (('0' <= c && c <= '9') || ('A' <= c && c <= 'F'))
- {
- tmp += c;
- if (tmp.Length == 2)
- {
- byte b = Convert.ToByte(tmp, 16);
- o.Add(b);
- tmp = "";
- }
- }
- else if (c == ' ' || c == ',' || c == '-' || c == ';')
- {
- }
- else
- {
- break;
- }
- }
- return o.ToArray();
- }
- catch
- {
- return new byte[0];
- }
- }
- public static string ByteToStr(byte[] data, Encoding enc)
- {
- try
- {
- return enc.GetString(data);
- }
- catch
- {
- return "";
- }
- }
- public static byte[] StrToByte(string str, Encoding enc)
- {
- try
- {
- return enc.GetBytes(str);
- }
- catch
- {
- return new byte[0];
- }
- }
- public static string[] GetLines(string str)
- {
- List<string> a = new List<string>();
- StringReader sr = new StringReader(str);
- while (true)
- {
- string s = sr.ReadLine();
- if (s == null)
- {
- break;
- }
- a.Add(s);
- }
- return a.ToArray();
- }
- public static string LinesToStr(string[] lines)
- {
- StringWriter sw = new StringWriter();
- foreach (string s in lines)
- {
- sw.WriteLine(s);
- }
- return sw.ToString();
- }
- public static bool IsEmptyStr(string str)
- {
- if (str == null || str.Trim().Length == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static bool IsSplitChar(char c, string splitStr)
- {
- if (splitStr == null)
- {
- splitStr = StrToken.DefaultSplitStr;
- }
- foreach (char t in splitStr)
- {
- string a = "" + t;
- string b = "" + c;
- if (Util.StrCmpi(a, b))
- {
- return true;
- }
- }
- return false;
- }
- public static bool GetKeyAndValue(string str, out string key, out string value)
- {
- return GetKeyAndValue(str, out key, out value, null);
- }
- public static bool GetKeyAndValue(string str, out string key, out string value, string splitStr)
- {
- uint mode = 0;
- string keystr = "", valuestr = "";
- if (splitStr == null)
- {
- splitStr = StrToken.DefaultSplitStr;
- }
- foreach (char c in str)
- {
- switch (mode)
- {
- case 0:
- if (IsSplitChar(c, splitStr) == false)
- {
- mode = 1;
- keystr += c;
- }
- break;
- case 1:
- if (IsSplitChar(c, splitStr) == false)
- {
- keystr += c;
- }
- else
- {
- mode = 2;
- }
- break;
- case 2:
- if (IsSplitChar(c, splitStr) == false)
- {
- mode = 3;
- valuestr += c;
- }
- break;
- case 3:
- valuestr += c;
- break;
- }
- }
- if (mode == 0)
- {
- value = "";
- key = "";
- return false;
- }
- else
- {
- value = valuestr;
- key = keystr;
- return true;
- }
- }
- public static int StrCmpRetInt(string s1, string s2)
- {
- return s1.CompareTo(s2);
- }
- public static bool StrCmp(string s1, string s2)
- {
- return StrCmpRetInt(s1, s2) == 0 ? true : false;
- }
- public static int StrCmpiRetInt(string s1, string s2)
- {
- s1 = s1.ToUpper();
- s2 = s2.ToUpper();
- return StrCmpRetInt(s1, s2);
- }
- public static bool StrCmpi(string s1, string s2)
- {
- return StrCmpiRetInt(s1, s2) == 0 ? true : false;
- }
- public static bool IsStrInList(string str, params string[] args)
- {
- return IsStrInList(str, true, args);
- }
- public static bool IsStrInList(string str, bool ignoreCase, params string[] args)
- {
- foreach (string s in args)
- {
- if (ignoreCase)
- {
- if (StrCmpi(str, s))
- {
- return true;
- }
- }
- else
- {
- if (StrCmp(str, s))
- {
- return true;
- }
- }
- }
- return false;
- }
- public static bool IsNumber(string str)
- {
- str = str.Trim();
- foreach (char c in str)
- {
- if (c >= '0' && c <= '9')
- {
- }
- else
- {
- return false;
- }
- }
- return true;
- }
- public static string DateToString(DateTime dt)
- {
- if (dt.Ticks != 0)
- {
- return dt.ToString("yyyyMMdd HHmmss").Substring(2);
- }
- else
- {
- return "000000_000000";
- }
- }
- public static DateTime StringToDate(string str)
- {
- str = str.Replace("(JST)", "").Trim();
- try
- {
- return DateTime.Parse(str);
- }
- catch
- {
- return new DateTime(0);
- }
- }
- public static string ToSafeString(string str)
- {
- char[] chars =
- {
- ';', '?', '=', '<', '>', ':', '@', '%', '$', '\\', '/', '|', '\"', '\r', '\n',
- };
- string ret = str;
- foreach (char c in chars)
- {
- ret = ret.Replace(c, '_');
- }
- string ret2 = "";
- foreach (char c in ret)
- {
- bool b = false;
- if (c >= 0x00 && c <= 0x1f)
- {
- b = true;
- }
- if (c >= 0x7f && c <= 0xff)
- {
- b = true;
- }
- if (b == false)
- {
- ret2 += c;
- }
- else
- {
- ret2 += "_";
- }
- }
- return ret2;
- }
- public static byte[] Base64Decode(string src)
- {
- try
- {
- return System.Convert.FromBase64String(src);
- }
- catch
- {
- return null;
- }
- }
- public static string DecodeMime(string src)
- {
- string[] s = src.Split('?');
- byte[] b;
- if (s[2] == "B")
- {
- b = System.Convert.FromBase64String(s[3]);
- }
- else
- {
- throw new Exception("Bad Encode.");
- }
- string ret = System.Text.Encoding.GetEncoding(s[1]).GetString(b);
- if (s.Length >= 4)
- {
- string tmp = s[s.Length - 1];
- if (tmp.StartsWith("="))
- {
- ret += tmp.Substring(1);
- }
- }
- return ret;
- }
- public static bool HasNullChar(string str)
- {
- if (str.IndexOf('\0') != -1)
- {
- return true;
- }
- return false;
- }
- public static bool IsMailHeaderStr(string str)
- {
- if (str == null)
- {
- return false;
- }
- if (HasNullChar(str))
- {
- return false;
- }
- string[] sep = { ": " };
- string[] tokens = str.Split(sep, StringSplitOptions.RemoveEmptyEntries);
- if (tokens.Length >= 2)
- {
- return true;
- }
- return false;
- }
- public static bool IsStrForBase64(string str)
- {
- string b64str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/+=";
- foreach (char c in str)
- {
- bool b = false;
- foreach (char c2 in b64str)
- {
- if (c == c2)
- {
- b = true;
- break;
- }
- }
- if (b == false)
- {
- return false;
- }
- }
- return true;
- }
- }
- public class StrToken
- {
- string[] tokens;
- public string[] Tokens
- {
- get { return tokens; }
- }
- public string this[uint index]
- {
- get { return tokens[index]; }
- }
- public uint NumTokens
- {
- get
- {
- return (uint)Tokens.Length;
- }
- }
- const string defaultSplitStr = " ,\t\r\n";
- public static string DefaultSplitStr
- {
- get { return defaultSplitStr; }
- }
- public StrToken(string str)
- : this(str, null)
- {
- }
- public StrToken(string str, string splitStr)
- {
- if (splitStr == null)
- {
- splitStr = defaultSplitStr;
- }
- int i, len;
- len = splitStr.Length;
- char[] chars = new char[len];
- for (i = 0; i < len; i++)
- {
- chars[i] = splitStr[i];
- }
- tokens = str.Split(chars, StringSplitOptions.RemoveEmptyEntries);
- }
- }
- public class StrData
- {
- string strValue;
- public string StrValue
- {
- get { return strValue; }
- }
- public uint IntValue
- {
- get
- {
- return Util.StrToUInt(strValue);
- }
- }
- public ulong Int64Value
- {
- get
- {
- return Util.StrToULong(strValue);
- }
- }
- public StrData(string str)
- {
- if (str == null)
- {
- str = "";
- }
- strValue = str;
- }
- }
|