FF8TextDecoder.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using OpenVIII.Encoding.Tags;
  3. namespace OpenVIII.Encoding
  4. {
  5. public sealed class FF8TextDecoder
  6. {
  7. private readonly FF8TextEncodingCodepage _codepage;
  8. public FF8TextDecoder(FF8TextEncodingCodepage codepage)
  9. {
  10. _codepage = codepage ?? throw new ArgumentNullException(nameof(codepage));
  11. }
  12. public int GetMaxCharCount(int byteCount)
  13. {
  14. return byteCount * FF8TextTag.MaxTagLength;
  15. }
  16. public int GetCharCount(byte[] bytes, int index, int count)
  17. {
  18. var result = 0;
  19. var buff = new char[FF8TextTag.MaxTagLength];
  20. while (count > 0)
  21. {
  22. var tag = FF8TextTag.TryRead(bytes, ref index, ref count);
  23. if (tag != null)
  24. {
  25. var offset = 0;
  26. result += tag.Write(buff, ref offset);
  27. }
  28. else
  29. {
  30. count--;
  31. result++;
  32. index++;
  33. }
  34. }
  35. return result;
  36. }
  37. public int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
  38. {
  39. var result = 0;
  40. while (byteCount > 0)
  41. {
  42. var tag = FF8TextTag.TryRead(bytes, ref byteIndex, ref byteCount);
  43. if (tag != null)
  44. {
  45. result += tag.Write(chars, ref charIndex);
  46. }
  47. else
  48. {
  49. chars[charIndex++] = _codepage[bytes[byteIndex++]];
  50. byteCount--;
  51. result++;
  52. }
  53. }
  54. return result;
  55. }
  56. }
  57. }