public override string Replace(string input, Guider guider, IGuidFormatter formatter=null, bool upcase = false, Action<Replacement> onReplacement = null, int lineNumber = -1) { string output = (input != null)?input:""; return s_B64.Replace(input, (m)=> { string s = s_SpaceStripper.Replace(m.Value, ""); StringBuilder replacementText = new StringBuilder(); List<Guid> guids = new List<Guid>(); foreach(Guid g in ToGuid(s)) { if (guider.MoveNext(g)) { Guid replacementGuid = guider.Current; guids.Add(replacementGuid); string currentText = (formatter != null)?formatter.ToString(replacementGuid, upcase, false):replacementGuid.ToString("D"); if (formatter != null) { if (replacementText.Length > 0) replacementText.Append("\r\n"); replacementText.Append(currentText); } if (onReplacement != null) onReplacement(new Replacement() { Line=lineNumber, Column = m.Index, FoundText=g.ToString("D"), FoundGuid=g, ReplacedByText=currentText, ReplacedByGuid=replacementGuid }); } } if (guids.Count > 0 && replacementText.Length == 0) replacementText.Append(ToString(guids, upcase, false)); else if (guids.Count == 0) return m.Value; return replacementText.ToString(); }); }
public virtual string Replace(string input, Guider guider, IGuidFormatter formatter=null, bool upcase = false, Action<Replacement> onReplacement = null, int lineNumber = -1) { if (!string.IsNullOrEmpty(input)) { return Matcher.Replace(input, (m) => { Guid foundGuid = MatchToGuid(m); if (guider.MoveNext(foundGuid)) { Guid replacementGuid = guider.Current; string replacementText = formatter==null?Replace(m.Value, m.Index, m, foundGuid, replacementGuid, upcase):formatter.ToString(replacementGuid, upcase, false); if (onReplacement != null) onReplacement(new Replacement() { Line=lineNumber, Column = m.Index, FoundText=m.Value, FoundGuid=foundGuid, ReplacedByText=replacementText, ReplacedByGuid=replacementGuid }); return replacementText; } return m.Value; }); } return input; }
public static int Replace(System.IO.TextReader reader, System.IO.TextWriter writer, Guider guider, Dictionary<Guid, Guid> replacements, string format, bool upcase, bool copyByLine = false) { int totalReplacements = 0; string ucformat = upcase ? "X2" : "x2"; Guid replacement; string line; while ((line = reader.ReadLine()) != null) { int lc = 0; MatchCollection mc = s_Parser.Matches(line); foreach (Match m in mc) { lc++; byte[] bytes = new byte[16]; for (int i = 0; i < 16; i++) { byte b; if (!byte.TryParse(m.Groups["b" + i.ToString()].Value, System.Globalization.NumberStyles.HexNumber, null, out b)) { if (!m.Success) throw new ArgumentOutOfRangeException(); } bytes[i] = b; } Guid active = new Guid(bytes); if (!replacements.TryGetValue(active, out replacement)) { if (!guider.MoveNext(active)) continue; replacement = guider.Current; replacements[active] = replacement; } if (format == null) { byte[] rbytes = replacement.ToByteArray(); for (int i = 0; i < 16; i++) { Group g = m.Groups["b" + i.ToString()]; string hex = rbytes[i].ToString(ucformat); line = line.Substring(0, g.Index) + hex + line.Substring(g.Index + 2); } } else { string formattedGuid = GuidFormats.Format(format, replacement, upcase, false); //replace line = line.Replace(m.Value, formattedGuid); } totalReplacements++; } mc = s_B64Parser_Single.Matches(line); foreach (Match m in mc) { lc++; Guid active = new Guid(Convert.FromBase64String(m.Value)); if (!replacements.TryGetValue(active, out replacement)) { if (!guider.MoveNext(active)) continue; replacement = guider.Current; replacements[active] = replacement; } if (format == null) { byte[] rbytes = replacement.ToByteArray(); string b64Replacement = Convert.ToBase64String(rbytes); line = line.Substring(0, m.Index) + b64Replacement + line.Substring(m.Index + m.Length); } else { string formattedGuid = GuidFormats.Format(format, replacement, upcase, false); //replace line = line.Replace(m.Value, formattedGuid); } totalReplacements++; } if (writer != null) writer.WriteLine(line); if (copyByLine) System.Windows.Forms.Clipboard.SetData(System.Windows.Forms.DataFormats.Text, line); } return totalReplacements; }