开发者

What is the fastest way to skip unicode characters

开发者 https://www.devze.com 2023-04-08 04:33 出处:网络
开发者_JS百科I am trying to get to certain charcters in a file that is in UTF-16 format. I know how many characters I want to skip. I am currently using the TextReader.ReadBlock command to read a te
开发者_JS百科

I am trying to get to certain charcters in a file that is in UTF-16 format.

I know how many characters I want to skip. I am currently using the TextReader.ReadBlock command to read a temporary array of all of the characters I want to skip, but I believe that setting the position would be faster. I just do not how to determine the new position.

Any idea what would be the fastest way to skip to a position in a unicode file if you have how many characters that you want to skip?


It's not so easy to skip a block, that requires relative positioning.

If you can calculate the begiining of the next block (offset from the start of the file) it is doable:

        int nextPos = ...;

        reader.DiscardBufferedData();
        reader.BaseStream.Position = nextPos;
        line = reader.ReadLine();

You may have to tweak your calculation because UTF-16 file can have a BOM (2 leading bytes).


Considwring that this os UTF-16 and not UTF-8 (where character size can vary) you have 2 bytes per character. So to skip x characters you have to skip x*2 bytes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号