yyl
21 小时以前 ad14e2586cd13c52b30e1cc481724a1e09407f6a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using System.Threading;
 
public partial class DirtyNameConfig : ConfigBase<int, DirtyNameConfig>
{
 
    private static WordGroup[] DIRTYWORLD = new WordGroup[(int)char.MaxValue];
 
    private static List<string> memoryList = new List<string>();
 
    private static bool dirtyNameInited = false;
 
    static int cursor = 0;
 
    static int wordlenght = 0;
 
    static int nextCursor = 0;
 
    protected override void OnConfigParseCompleted()
    {
        if (string.IsNullOrEmpty(word))
        {
            Debug.LogErrorFormat("屏蔽名字 id = {0} 的内容为空 需要修改", id);
            return;
        }
        string key = ToDBC(word);
        memoryList.Add(key);
    }
 
    private static string ToDBC(string input)
    {
        char[] c = input.ToCharArray();
        for (int i = 0; i < c.Length; i++)
        {
            if (c[i] == 12288)
            {
                c[i] = (char)32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char)(c[i] - 65248);
        }
        return new string(c).ToLower();
    }
 
    public static bool IsDirtName(string source)
    {
        if (source != string.Empty)
        {
            cursor = 0;
            nextCursor = 0;
            for (int i = 0; i < source.Length; i++)
            {
                //查询以该字为首字符的词组
                WordGroup group = DIRTYWORLD[(int)ToDBC(source)[i]];
                if (group != null)
                {
                    for (int z = 0; z < group.Count(); z++)
                    {
                        string word = group.GetWord(z);
                        if (word.Length == 0 || Check(word, source))
                        {
                            return true;
                        }
                    }
                }
                cursor++;
            }
        }
        return false;
    }
 
    private static bool Check(string blackWord, string source)
    {
        wordlenght = 0;
        int wordCnt = 0;
        //检测源下一位游标
        nextCursor = cursor + 1;
        bool found = false;
        //遍历词的每一位做匹配
        for (int i = 0; i < blackWord.Length; i++)
        {
            //特殊字符偏移游标
            int offset = 0;
            if (nextCursor >= source.Length)
            {
                break;
            }
            else
            {
                //检测下位字符如果不是汉字 数字 字符 偏移量加1,,过滤特殊字符
                for (int y = nextCursor; y < source.Length; y++)
                {
                    //if (!IsCHS(source[y]) && !IsNum(source[y]) && !IsAlphabet(source[y])) {
                    if (IsSpecial(source[y]))
                    {
                        offset++;
                        //避让特殊字符,下位游标如果>=字符串长度 跳出
                        if (nextCursor + offset >= source.Length) break;
                        wordlenght++;
 
                    }
                    else break;
                }
                if (nextCursor + offset >= source.Length)
                {
                    break;
                }
                if ((int)blackWord[i] == (int)source[nextCursor + offset])
                {
                    wordCnt++;
                    found = true;
                }
                else
                {
                    found = false;
                    break;
                }
            }
            nextCursor = nextCursor + 1 + offset;
            wordlenght++;
        }
        if (blackWord.Length != wordCnt) return false;
        return found;
    }
 
    private static bool IsSpecial(char character)
    {
        if (character == ' ')
        {
            return true;
        }
        return false;
    }
 
    static public void DirtyNameInit()
    {
        if (dirtyNameInited)
        {
            return;
        }
 
        dirtyNameInited = true;
 
        ThreadPool.QueueUserWorkItem((object aaa) =>
        {
            memoryList.Sort((string x, string y) =>
            {
                return x.CompareTo(y);
            });
            for (int i = memoryList.Count - 1; i > 0; i--)
            {
                if (memoryList[i].ToString() == memoryList[i - 1].ToString())
                {
                    memoryList.RemoveAt(i);
                }
            }
            foreach (string word in memoryList)
            {
                WordGroup group = DIRTYWORLD[(int)word[0]];
                if (group == null)
                {
                    group = new WordGroup();
                    DIRTYWORLD[(int)word[0]] = group;
                }
                group.Add(word.Substring(1));
            }
        });
    }
 
    public static bool IsCHS(char character)
    {
        //  中文表意字符的范围 4E00-9FA5
        int charVal = (int)character;
        return (charVal >= 0x4e00 && charVal <= 0x9fa5);
    }
 
    private static bool IsNum(char character)
    {
        int charVal = (int)character;
        return (charVal >= 48 && charVal <= 57);
    }
 
    private static bool IsAlphabet(char character)
    {
        int charVal = (int)character;
        return ((charVal >= 97 && charVal <= 122) || (charVal >= 65 && charVal <= 90));
    }
 
    private const string FACE_REPLACE = @"#~[0-9a-zA-Z]{1,3}";
    private static Regex FaceRegex = new Regex(FACE_REPLACE, RegexOptions.Singleline);
    public static string FitterSpecial(string source)
    {
        return FaceRegex.Replace(source, "     ");
    }
}