public System.String DecodeFromNonLossyAscii(System.String original) { Java.Util.Regex.Pattern UNICODE_HEX_PATTERN = Java.Util.Regex.Pattern.Compile("\\\\u([0-9A-Fa-f]{4})"); Java.Util.Regex.Pattern UNICODE_OCT_PATTERN = Java.Util.Regex.Pattern.Compile("\\\\([0 - 7]{3})"); Matcher matcher = UNICODE_HEX_PATTERN.Matcher(original); StringBuffer charBuffer = new StringBuffer(original.Length); while (matcher.Find()) { System.String match = matcher.Group(1); char unicodeChar = (char)Integer.ParseInt(match, 16); matcher.AppendReplacement(charBuffer, Character.ToString(unicodeChar)); } matcher.AppendTail(charBuffer); System.String parsedUnicode = charBuffer.ToString(); matcher = UNICODE_OCT_PATTERN.Matcher(parsedUnicode); charBuffer = new StringBuffer(parsedUnicode.Length); while (matcher.Find()) { System.String match = matcher.Group(1); char unicodeChar = (char)Integer.ParseInt(match, 8); matcher.AppendReplacement(charBuffer, Character.ToString(unicodeChar)); } matcher.AppendTail(charBuffer); return(charBuffer.ToString()); }
/// <summary> /// Matches a template string against a pattern, replaces matched tokens with /// the supplied replacements, and returns the result. /// </summary> /// <remarks> /// Matches a template string against a pattern, replaces matched tokens with /// the supplied replacements, and returns the result. The regular expression /// must use a capturing group. The value of the first capturing group is used /// to look up the replacement. If no replacement is found for the token, then /// it is replaced with the empty string. /// For example, assume template is "%foo%_%bar%_%baz%", pattern is "%(.*?)%", /// and replacements contains 2 entries, mapping "foo" to "zoo" and "baz" to /// "zaz". The result returned would be "zoo__zaz". /// </remarks> /// <param name="template">String template to receive replacements</param> /// <param name="pattern"> /// Pattern to match for identifying tokens, must use a capturing /// group /// </param> /// <param name="replacements"> /// Map<String, String> mapping tokens identified by the /// capturing group to their replacement values /// </param> /// <returns>String template with replacements</returns> public static string ReplaceTokens(string template, Pattern pattern, IDictionary <string, string> replacements) { StringBuilder sb = new StringBuilder(); Matcher matcher = pattern.Matcher(template); while (matcher.Find()) { string replacement = replacements[matcher.Group(1)]; if (replacement == null) { replacement = string.Empty; } matcher.AppendReplacement(sb, Matcher.QuoteReplacement(replacement)); } matcher.AppendTail(sb); return(sb.ToString()); }
public static void SetEnvFromInputString(IDictionary <string, string> env, string envString, string classPathSeparator) { if (envString != null && envString.Length > 0) { string[] childEnvs = envString.Split(","); Sharpen.Pattern p = Sharpen.Pattern.Compile(Shell.GetEnvironmentVariableRegex()); foreach (string cEnv in childEnvs) { string[] parts = cEnv.Split("="); // split on '=' Matcher m = p.Matcher(parts[1]); StringBuilder sb = new StringBuilder(); while (m.Find()) { string var = m.Group(1); // replace $env with the child's env constructed by tt's string replace = env[var]; // if this key is not configured by the tt for the child .. get it // from the tt's env if (replace == null) { replace = Runtime.Getenv(var); } // the env key is note present anywhere .. simply set it if (replace == null) { replace = string.Empty; } m.AppendReplacement(sb, Matcher.QuoteReplacement(replace)); } m.AppendTail(sb); AddToEnvironment(env, parts[0], sb.ToString(), classPathSeparator); } } }