欢迎大家来重构一下微软的这段代码

    下面这段代码是从.NET Framework 2.0 beta2中弄出来的,看后除了大为faint外,真的是没有语言了,虽然作者还是考虑了不少的优化。欢迎大家来重构一下这段代码:) 这个方法的功能是把JScript字符串转换为文本形式,也就是把一些特殊的字符转意一下。

internal static string QuoteJScriptString(string value, bool forUrl);
 
Declaring Type: System.Web.UI.Util 
Assembly: System.Web, Version
=2.0.0.0 

public static string QuoteJScriptString(string value, bool forUrl)
{
    StringBuilder strbQuoted 
= null;
    
if (StringHelper.IsEmpty(value))
    
{
        
return string.Empty;
    }

    
int oldIndex = 0;
    
int walkCount = 0;
    
for (int i = 0; i < value.Length; i++)
    
{
        
char ch = value[i];
        
if  ( ch <= '"' )
        
{
            
switch (ch)
            
{
                
case '\t':
                
{
                    
if (strbQuoted == null)
                    
{
                        strbQuoted 
= new StringBuilder(value.Length + 5);
                    }

                    
if (walkCount > 0)
                    
{
                        strbQuoted.Append(value, oldIndex, walkCount);
                    }

                    strbQuoted.Append(
@"\t");
                    oldIndex 
= i + 1;
                    walkCount 
= 0;
                    
break;
                }

                
case '\n':
                
{
                    
if (strbQuoted == null)
                    
{
                        strbQuoted 
= new StringBuilder(value.Length + 5);
                    }

                    
if (walkCount > 0)
                    
{
                        strbQuoted.Append(value, oldIndex, walkCount);
                    }

                    strbQuoted.Append(
@"\n");
                    oldIndex 
= i + 1;
                    walkCount 
= 0;
                    
break;
                }

                
case '\v':
                
case '\f':
                
{
                    walkCount
++;
                    
break;
                }

                
case '\r':
                
{
                    
if (strbQuoted == null)
                    
{
                        strbQuoted 
= new StringBuilder(value.Length + 5);
                    }

                    
if (walkCount > 0)
                    
{
                        strbQuoted.Append(value, oldIndex, walkCount);
                    }

                    strbQuoted.Append(
@"\r");
                    oldIndex 
= i + 1;
                    walkCount 
= 0;
                    
break;
                }

                
case '"':
                
{
                    
if (strbQuoted == null)
                    
{
                        strbQuoted 
= new StringBuilder(value.Length + 5);
                    }

                    
if (walkCount > 0)
                    
{
                        strbQuoted.Append(value, oldIndex, walkCount);
                    }

                    strbQuoted.Append(
"\\\"");
                    oldIndex = i + 1;
                    walkCount 
= 0;
                    
break;
                }

                
default:
                
{
                    walkCount
++;
                    
break;
                }

            }

        }

        
else
        
{
            
switch (ch)
            
{
                
case '%':
                
{
                    
if (!forUrl)
                    
{
                        walkCount
++;
                        
break;
                    }

                    
if (strbQuoted == null)
                    
{
                        strbQuoted 
= new StringBuilder(value.Length + 6);
                    }

                    
if (walkCount > 0)
                    
{
                        strbQuoted.Append(value, oldIndex, walkCount);
                    }

                    strbQuoted.Append(
"%25");
                    oldIndex 
= i + 1;
                    walkCount 
= 0;
                    
break;
                }

                
case '&':
                
{
                    walkCount
++;
                    
break;
                }

                
case '\'':
                {
                    
if (strbQuoted == null)
                    
{
                        strbQuoted 
= new StringBuilder(value.Length + 5);
                    }

                    
if (walkCount > 0)
                    
{
                        strbQuoted.Append(value, oldIndex, walkCount);
                    }

                    strbQuoted.Append(
@"\'");
                    oldIndex 
= i + 1;
                    walkCount 
= 0;
                    
break;
                }

                
case '\\':
                
{
                    
if (strbQuoted == null)
                    
{
                        strbQuoted 
= new StringBuilder(value.Length + 5);
                    }

                    
if (walkCount > 0)
                    
{
                        strbQuoted.Append(value, oldIndex, walkCount);
                    }

                    strbQuoted.Append(
@"\\");
                    oldIndex 
= i + 1;
                    walkCount 
= 0;
                    
break;
                }

                
default:
                
{
                    walkCount
++;
                    
break;
                }

            }

        }

    }

    
if (strbQuoted == null)
    
{
        
return value;
    }

    
if (walkCount > 0)
    
{
        strbQuoted.Append(value, oldIndex, walkCount);
    }

    
return strbQuoted.ToString();
}

    // 看后是不是感到明白了微软为什么恐惧OpenSource?!

posted on   birdshome  阅读(3657)  评论(13编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器

导航

点击右上角即可分享
微信分享提示