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

    下面这段代码是从.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 2005-07-15 00:04  birdshome  阅读(3654)  评论(13编辑  收藏  举报

导航