📄 util.cs
字号:
/** Copyright (c) 2006, All-In-One Creations, Ltd.* All rights reserved.* * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:* * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.* * Neither the name of All-In-One Creations, Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.* * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLEFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.**//** * Project: emergetk: stateful web framework for the masses * File name: .cs * Description: * * Author: Ben Joldersma * */using System;using System.Collections;using System.Text.RegularExpressions;using System.Collections.Generic;using System.Web;namespace EmergeTk{ /// <summary> /// Summary description for Util. /// </summary> public class Util { public static string RootPath { get { return HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "\\"); } } public static string Join(IList list, string sep) { return Join( list, sep, false ); } public static string Join(IList list, string sep, bool formatForClient) { return Join(list, sep, formatForClient, 0); } public static string Join(IList list, string sep, bool formatForClient, int startIndex) { return Join(list, sep, formatForClient, startIndex, list.Count - 1); } public static string Join(IList list, string sep, bool formatForClient, int startIndex, int finishIndex) { if (list == null || list.Count == 0) return null; string o = ""; for (int i = startIndex; i < finishIndex; i++) { string val; val = prepareValue(list, formatForClient, i); o += val + sep; } o += prepareValue( list, formatForClient, finishIndex ); return o; } private static string prepareValue(IList list, bool formatForClient, int i) { string val; if (formatForClient) val = FormatForClient(list[i] as string); else { if (list[i] != null) val = list[i].ToString(); else val = string.Empty; } return val; } public static string Join<T>(T[] cols) { return Join(cols, ","); } public static string Join<T>(List<T> cols) { return Join(cols, ","); } public static string Quotize(string s) { if (s == null || s == string.Empty) { return "''"; } s = s.Replace("</script>", "</scr' + 'ipt>"); s = s.Replace("</SCRIPT>", "</scr' + 'ipt>"); return Surround(s, "'"); } public static string Surround( string inner, string surrounder ) { return surrounder + inner + surrounder; } public static string SurroundTag(string inner, string tag) { return string.Format("<{0}>{1}</{0}>", tag, inner); } public static string FormatForClient( string input ) { if( input == null ) return string.Empty; string thistext = input.Replace("\\", "\\\\"); thistext = thistext.Replace("\r\n", "\\r\\n"); thistext = thistext.Replace("\n", "\\n"); thistext = thistext.Replace("\r", "\\r"); thistext = thistext.Replace("'","\\'"); return thistext; } public static string ToJavaScriptString(string input) { return Quotize(FormatForClient(input)); } public static string Coalesce(params string[] args) { for (int i = 0; i < args.Length; i++) { if (args[i] != null) return args[i]; } return null; } public static string Textalize(string source) { Regex r = new Regex(@"(?<tag>====|===|==|'''|\''|\||##|\*\*)(?<inner>.*?)\k<tag>",RegexOptions.Singleline); MatchEvaluator me = new MatchEvaluator( matchTag ); source = r.Replace( source, me ); source = source.Replace("\r\n\r\n", "<br><br>"); source = source.Replace("\n\n", "<br><br>"); source = source.Replace("\\n", "<br>"); return source; } static private string matchTag( Match m ) { string tag = string.Empty; string atts = string.Empty; string inner = m.Groups["inner"].Value; switch( m.Groups["tag"].Captures[0].Value ) { case "'''": tag = "B"; break; case "''": tag = "I"; break; case "|": tag = "A"; atts = " HREF='" + m.Groups["inner"].Value + "'"; break; case "##": tag = "OL"; inner = inner.Replace("#.", "<LI>"); break; case "**": tag = "UL"; inner = inner.Replace("*.", "<LI>"); break; case "====": tag = "H3"; break; case "===": tag = "H2"; break; case "==": tag = "H1"; break; } return string.Format("<{0}{2}>{1}</{0}>", tag, Util.Textalize(inner), atts); } public object Coalesce( params object[] values ) { for( int i = 0; i < values.Length; i++ ) if( values[i] != null ) return values[ i ]; return null; } static Microsoft.JScript.Vsa.VsaEngine vsaEngine = null; public static object JsEval(string eval) { if( vsaEngine == null ) vsaEngine = Microsoft.JScript.Vsa.VsaEngine.CreateEngine(); try { return Microsoft.JScript.Eval.JScriptEvaluate(eval, vsaEngine); } catch (Exception e) { Debug.Trace("jscript eval error: {0} eval'ing: {1}", e.Message, eval); return null; } } public static string HashToMime(Dictionary<string, string> input) { string output = string.Empty; List<string> pairs = new List<string>(); foreach( string key in input.Keys ) { pairs.Add(key + "=" + input[key]); } return Join(pairs, "&"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -