Advertisement
jotto

test

Mar 21st, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace RS232
  14. {
  15.     public partial class Sender : Form
  16.     {
  17.         static string resource_data = Properties.Resources.dictionary;
  18.         List<string> words = resource_data.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
  19.         Client klient = new Client();
  20.  
  21.         public Sender()
  22.         {
  23.             InitializeComponent();
  24.             klient.Show();
  25.         }
  26.  
  27.         private void convertButton_Click(object o, EventArgs e)
  28.         {
  29.             //this.binBox.Text = ToBinary(ConvertToASCII(CheckDictionary(this.textBox.Text)));
  30.             foreach (byte b in ToBinary(ConvertToASCII(CheckDictionary(this.textBox.Text))))
  31.                 Console.Write(b);
  32.         }
  33.  
  34.         private byte[] ConvertToASCII(String data)
  35.         {
  36.             return Encoding.ASCII.GetBytes(data);
  37.         }
  38.  
  39.         private byte[] ToBinary(byte[] data)
  40.         {
  41.             string temp = string.Join("", data.Select(byt => Convert.ToString(byt, 2).PadLeft(8, '0').PadLeft(9, '1').PadRight(11, '1')));
  42.             return temp.Select(c => (byte)(c - '0')).ToArray();
  43.         }
  44.  
  45.         private string CheckDictionary(string data)
  46.         {
  47.             const string CensoredText = "*****";
  48.             const string PatternTemplate = @"\b({0})(s?)\b";
  49.             const RegexOptions Options = RegexOptions.IgnoreCase;
  50.  
  51.             IEnumerable<Regex> badWordMatchers = words.
  52.                 Select(x => new Regex(string.Format(PatternTemplate, x), Options));
  53.  
  54.             return badWordMatchers.Aggregate(data, (current, matcher) => matcher.Replace(current, CensoredText));
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement