English 中文(简体)
如何迅速替换阵列中的特性
原标题:how to replace characters in a array quickly

我在XML档案中使用XML文本读物,该文本可能含有对读者无效的特性。 我最初的想法是,创建我自己版本的流子读者,并清除坏的特性,但它正在严重减缓我的方案。

public class ClensingStream : StreamReader
{
        private static char[] badChars = {  x00 ,  x09 ,  x0A ,  x10  };
    //snip
        public override int Read(char[] buffer, int index, int count)
        {
            var tmp = base.Read(buffer, index, count);

            for (int i = 0; i < buffer.Length; ++i)
            {
                //check the element in the buffer to see if it is one of the bad characters.
                if(badChars.Contains(buffer[i]))
                    buffer[i] =    ;
            }

            return tmp;
        }
}

根据我的简介,该守则在if(badChars.Contains(buffer[i])上花费了88%的时间。

最佳回答

The reason that it spends so much time in that line is because the Contains method loops through the array to look for the character.

替代:

private static HashSet<char> badChars =
  new HashSet<char>(new char[] {  x00 ,  x09 ,  x0A ,  x10  });

检查这套清单是否包含特性与研究阵列时的特性相同,但该守则使用该特性的散列代码来寻找,而不是通过阵列的所有项目。

或者,你可以把特征放在开关上,这样汇编者就能够形成有效的比较:

switch (buffer[i]]) {
  case  x00 :
  case  x09 :
  case  x0A :
  case  x10 : buffer[i] =    ; break;
}

如果你具有更多的特性(5或6个国际仲裁裁决汇编),汇编者将实际制作一个黑板表格,以研究这些案件,从而类似于使用

问题回答

可通过<代码>switch取得更好的结果。 声明:

switch (buffer[i])
{
    case  x00 :
    case  x09 :
    case  x0A :
    case  x10 :
        buffer[i] =    ;
        break;
}

这应当由联合信托公司汇编员在运行时按速成编码。 简而言之,汇编者也可能关闭。 你们不需要一种方法。

您可使用 常规表述,对此应优化。 将案文阅读成一个插图和使用Replace,其特性在随后的定期表述中。

然而,你的法典也令我感到 fine荣,我猜测,除了通过你的文字进行搜查之外,我也可以做任何事情。

您可以检查一下它是否选择只检查阅读果园,从而做到正确。

for (int i = index; i < index + count; i++){
  //etc
}

Don know if if if if

将<代码>char[]改为指示,然后使用<代码>IndexOfAny。

You could use a boolean array

char[] badChars = {  x00 ,  x09 ,  x0A ,  x10  };
char maxChar = badChars.Max();
Debug.Assert(maxChar < 256);
bool[] badCharsTable = new bool[maxChar + 1];

Array.ForEach(badChars, ch => badCharsTable[ch] = true);

http://code>badChars.Contains(...)(ch <坏CharsTable)。 Length &&坏CharsTable[ch])。

Edit:最后有时间改进答案。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签