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

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

None.gif internal  static  string QuoteJScriptString( string value,  bool forUrl);
None.gif 
None.gifDeclaring Type: System.Web.UI.Util 
None.gifAssembly: System.Web, Version=2.0.0.0 

None.gif public  static  string QuoteJScriptString( string value,  bool forUrl)
ExpandedBlockStart.gif ContractedBlock.gif dot.gif{
InBlock.gif    StringBuilder strbQuoted =  null;
InBlock.gif     if (StringHelper.IsEmpty(value))
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif         return  string.Empty;
ExpandedSubBlockEnd.gif    }
InBlock.gif     int oldIndex = 0;
InBlock.gif     int walkCount = 0;
InBlock.gif     for ( int i = 0; i < value.Length; i++)
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif         char ch = value[i];
InBlock.gif         if  ( ch <= '"' )
ExpandedSubBlockStart.gif ContractedSubBlock.gif         dot.gif{
InBlock.gif             switch (ch)
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                 case '\t':
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif{
InBlock.gif                     if (strbQuoted ==  null)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted =  new StringBuilder(value.Length + 5);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                     if (walkCount > 0)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted.Append(value, oldIndex, walkCount);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                    strbQuoted.Append(@"\t");
InBlock.gif                    oldIndex = i + 1;
InBlock.gif                    walkCount = 0;
InBlock.gif                     break;
ExpandedSubBlockEnd.gif                }
InBlock.gif                 case '\n':
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif{
InBlock.gif                     if (strbQuoted ==  null)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted =  new StringBuilder(value.Length + 5);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                     if (walkCount > 0)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted.Append(value, oldIndex, walkCount);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                    strbQuoted.Append(@"\n");
InBlock.gif                    oldIndex = i + 1;
InBlock.gif                    walkCount = 0;
InBlock.gif                     break;
ExpandedSubBlockEnd.gif                }
InBlock.gif                 case '\v':
InBlock.gif                 case '\f':
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif{
InBlock.gif                    walkCount++;
InBlock.gif                     break;
ExpandedSubBlockEnd.gif                }
InBlock.gif                 case '\r':
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif{
InBlock.gif                     if (strbQuoted ==  null)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted =  new StringBuilder(value.Length + 5);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                     if (walkCount > 0)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted.Append(value, oldIndex, walkCount);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                    strbQuoted.Append(@"\r");
InBlock.gif                    oldIndex = i + 1;
InBlock.gif                    walkCount = 0;
InBlock.gif                     break;
ExpandedSubBlockEnd.gif                }
InBlock.gif                 case '"':
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif{
InBlock.gif                     if (strbQuoted ==  null)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted =  new StringBuilder(value.Length + 5);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                     if (walkCount > 0)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted.Append(value, oldIndex, walkCount);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                    strbQuoted.Append("\\\"");
InBlock.gif                    oldIndex = i + 1;
InBlock.gif                    walkCount = 0;
InBlock.gif                     break;
ExpandedSubBlockEnd.gif                }
InBlock.gif                 default:
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif{
InBlock.gif                    walkCount++;
InBlock.gif                     break;
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif         else
ExpandedSubBlockStart.gif ContractedSubBlock.gif         dot.gif{
InBlock.gif             switch (ch)
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                 case '%':
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif{
InBlock.gif                     if (!forUrl)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        walkCount++;
InBlock.gif                         break;
ExpandedSubBlockEnd.gif                    }
InBlock.gif                     if (strbQuoted ==  null)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted =  new StringBuilder(value.Length + 6);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                     if (walkCount > 0)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted.Append(value, oldIndex, walkCount);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                    strbQuoted.Append("%25");
InBlock.gif                    oldIndex = i + 1;
InBlock.gif                    walkCount = 0;
InBlock.gif                     break;
ExpandedSubBlockEnd.gif                }
InBlock.gif                 case '&':
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif{
InBlock.gif                    walkCount++;
InBlock.gif                     break;
ExpandedSubBlockEnd.gif                }
InBlock.gif                 case '\'':
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif{
InBlock.gif                     if (strbQuoted ==  null)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted =  new StringBuilder(value.Length + 5);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                     if (walkCount > 0)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted.Append(value, oldIndex, walkCount);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                    strbQuoted.Append(@"\'");
InBlock.gif                    oldIndex = i + 1;
InBlock.gif                    walkCount = 0;
InBlock.gif                     break;
ExpandedSubBlockEnd.gif                }
InBlock.gif                 case '\\':
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif{
InBlock.gif                     if (strbQuoted ==  null)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted =  new StringBuilder(value.Length + 5);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                     if (walkCount > 0)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        strbQuoted.Append(value, oldIndex, walkCount);
ExpandedSubBlockEnd.gif                    }
InBlock.gif                    strbQuoted.Append(@"\\");
InBlock.gif                    oldIndex = i + 1;
InBlock.gif                    walkCount = 0;
InBlock.gif                     break;
ExpandedSubBlockEnd.gif                }
InBlock.gif                 default:
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif{
InBlock.gif                    walkCount++;
InBlock.gif                     break;
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif     if (strbQuoted ==  null)
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif         return value;
ExpandedSubBlockEnd.gif    }
InBlock.gif     if (walkCount > 0)
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif        strbQuoted.Append(value, oldIndex, walkCount);
ExpandedSubBlockEnd.gif    }
InBlock.gif     return strbQuoted.ToString();
ExpandedBlockEnd.gif}

    // 看后是不是感到明白了微软为什么恐惧OpenSourceemembarrassed.gif?! teeth_smile.gifteeth_smile.gifteeth_smile.gif


本文转自博客园鸟食轩的博客,原文链接:http://www.cnblogs.com/birdshome/,如需转载请自行联系原博主。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值