惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - xingd

Minesweeper: 新版代码 Minesweeper: 代码结构改进 Minesweeper: GDI+ 初步实现 Minesweeper: GDI+ 综述 SQL Server 2008: Change Data Capture和Change Tracking SQL Server 2008: 常见缩写汇总 SQL Server 2008: Feb CTP 企业数据平台增强 SQL Server 2008: Feb CTP 关系数据库引擎增强 SQL Server 2008: Installation Center SQL Server 2008: Feb CTP 开放下载 Minesweeper: GDI+ Line Scratch Minesweeper: GDI+ 概述 Minesweeper: 索引 推荐.NET新书 2007年度总结 再度提升!.NET脏字过滤算法 单独谈谈C#3.0 (再发).NET脏字过滤算法 (重发).NET脏字过滤算法
忽略大小写的.NET脏字过滤算法
xingd · 2008-02-04 · via 博客园 - xingd

Posted on 2008-02-04 19:37  xingd  阅读(3871)  评论()    收藏  举报

重复的内容就不再重复了,参考http://www.cnblogs.com/xingd/archive/2008/02/01/1061800.html

除了实现忽略大小写外,其他方面的性能也做了一些改进,主要借助下面的类,StringSegement,实现了大小写无关的比较和GetHashCode,同时避免了Substring的调用。

public class StringSegment
{
    
private string original;
    
private int offset = 0;
    
private int length = 0;public StringSegment(string s)
    {
        
this.original = s;
        
this.length = original.Length;
    }
public void Slice(int offset, int length)
    {
        
this.offset = offset;
        
this.length = length;
    }
public override bool Equals(object obj)
    {
        StringSegment sg 
= obj as StringSegment;
        
return sg != null && sg.length == this.length && string.Compare(this.original, this.offset, sg.original, sg.offset, this.length, true== 0;
    }
public override int GetHashCode()
    {
        
// call char.tolower and calculate hash code
    }
}


GetHashCode的实现完全参考string的实现,就不重复了,挺长的一段。

另外,对于特征判断的数据,规划为64k x 4 bytes,也就是每char给4 bytes特征数据,如果有算法改进,只要在这4 bytes里重新规划,现在的定义如下:

[StructLayout(LayoutKind.Explicit, Size = 32)]
internal struct FastCheckFlag
{
    [FieldOffset(
0)]
    
public byte occur;    // 1st~8th char occurrence
    [FieldOffset(8)]
    
public byte length;   // 2~9 word length begin with this char
    [FieldOffset(16)]
    
public byte rlength; // 2~9 word length end with this char (reverse length);
    [FieldOffset(24)]
    
public bool single; // single char bad words
    [FieldOffset(25)]
    
public bool last;    // last occurrence
    [FieldOffset(26)]
    
public byte occurParity;  // 9th~ occurrence parity flag
    [FieldOffset(28)]
    
public byte lengthParity; // 10~ length parity flag
    [FieldOffset(30)]
    
public byte rlengthParity; // 10~ rlength parity flag
}

 另外在初始化数据的时候,取字符的大小写同时处理,例如:

if (word.Length == 1)
{
    fastCheck[
char.ToLower(word[0])].single = true;
    fastCheck[
char.ToUpper(word[0])].single = true;
}