프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / Behaviors / SpecialcharRemove.cs @ master

이력 | 보기 | 이력해설 | 다운로드 (3.4 KB)

1 42d49521 taeseongkim
using System;
2
using System.Collections.Generic;
3
using System.IO;
4
using System.Linq;
5
using System.Text;
6
using System.Text.RegularExpressions;
7
using System.Threading.Tasks;
8
using System.Windows;
9
using System.Windows.Controls;
10
using System.Windows.Interactivity;
11
12
namespace KCOM.Behaviors
13
{
14
    public class SpecialcharRemove : Behavior<TextBox>
15
    {
16
        protected override void OnAttached()
17
        {
18
            base.OnAttached();
19
            DataObject.AddPastingHandler(this.AssociatedObject, OnPaste);
20
        }
21
22
        private void OnPaste(object sender, DataObjectPastingEventArgs e)
23
        {
24
            if (!e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true))
25
                return;
26
27
            var pastedText = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
28
            if (string.IsNullOrEmpty(pastedText))
29
                return;
30
31
            #region Regex는 특수문자까지 제외되버림 (,),[,]
32
33
            //Regex regex1 = new Regex(@"^[a-zA-Z0-9_]+$");
34
            //List<string> strArray = new List<string>();
35
36
            //StringReader sr = new StringReader(pastedText);
37
38
            //while(sr.ReadLine() is string str)
39
            //{
40
            //    var convertStr = str.Where(x => regex1.IsMatch(Convert.ToString(x)));
41
            //    strArray.Add(string.Join("", convertStr));
42
            //}
43
            #endregion
44
45
            e.CancelCommand();
46
47
            var replaceStr = ReplaceNonPrintables(pastedText);
48
            var startInx = this.AssociatedObject.CaretIndex + replaceStr.Length;
49
50
            this.AssociatedObject.Text = this.AssociatedObject.Text.Insert(this.AssociatedObject.CaretIndex, replaceStr);
51
            this.AssociatedObject.CaretIndex = startInx;
52
        }
53
54
        public static string ReplaceNonPrintables(string stringToProcess)
55
        {
56
            StringBuilder buf = new StringBuilder();
57
58
            for (int i = 0; i < stringToProcess.Length; i++)
59
            {
60
                var c = stringToProcess[i];
61
                System.Diagnostics.Debug.WriteLine(c + " " + BitConverter.ToString(BitConverter.GetBytes(c)));
62
63
                if (!CharsToReplace.Contains(c))
64
                {
65
                    if (c == '\x0A' && stringToProcess[i-1] != '\x0D')
66
                    {
67
                        buf.Append(Environment.NewLine);
68
                    }
69
                    else
70
                    {
71
                        buf.Append(c);
72
                    }
73
                }
74
                else
75
                {
76
                    System.Diagnostics.Debug.WriteLine("Contains " + c + " " + BitConverter.ToString(BitConverter.GetBytes(c)));
77
                }
78
            }
79
80
            return buf.ToString();
81
        }
82
83
        private static readonly char[] CharsToReplace =
84
        {
85
             '\x02',
86
             '\x03',
87
             '\x04',
88
             '\x05',
89
             '\x06',
90
             '\x07',
91
             //'\x08',
92
             '\x0B',
93
             '\x0C',
94
             '\x0E',
95
             '\x0F',
96
             '\x10',
97
             '\x11',
98
             '\x12',
99
             '\x13',
100
             '\x14',
101
             '\x15',
102
             '\x16',
103
             '\x17',
104
             '\x18',
105
             '\x19',
106
             '\x1a',
107
             '\x1b',
108
             '\x1c',
109
             '\x1d',
110
             '\x1e',
111
             '\x84',
112
             '\x86',
113
             '\x87',
114
             '\x88',
115
             '\x89',
116
         };
117
118
    }
119
}
클립보드 이미지 추가 (최대 크기: 500 MB)