| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using System;
- using OpenVIII.Encoding.Tags;
- namespace OpenVIII.Encoding
- {
- public sealed class FF8TextEncoder
- {
- private readonly FF8TextEncodingCodepage _codepage;
- public FF8TextEncoder(FF8TextEncodingCodepage codepage)
- {
- _codepage = codepage ?? throw new ArgumentNullException(nameof(codepage));
- }
- public int GetMaxByteCount(int charCount)
- {
- return charCount;
- }
- public int GetByteCount(char[] chars, int index, int count)
- {
- var result = 0;
- var buff = new byte[2];
- while (count > 0)
- {
- var tag = FF8TextTag.TryRead(chars, ref index, ref count);
- if (tag != null)
- {
- var offset = 0;
- result += tag.Write(buff, ref offset);
- }
- else if (FF8TextComment.TryRead(chars, ref index, ref count) == null)
- {
- count--;
- result++;
- index++;
- }
- }
- return result;
- }
- public int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
- {
- var result = 0;
- while (charCount > 0)
- {
- var tag = FF8TextTag.TryRead(chars, ref charIndex, ref charCount);
- if (tag != null)
- {
- result += tag.Write(bytes, ref byteIndex);
- }
- else if (FF8TextComment.TryRead(chars, ref charIndex, ref charCount) == null)
- {
- bytes[byteIndex++] = _codepage[chars[charIndex++]];
- charCount--;
- result++;
- }
- }
- return result;
- }
- }
- }
|