EncoderReplacementFallbackBuffer.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // EncoderReplacementFallbackBuffer.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. namespace System.Text
  31. {
  32. // This EncoderFallbackBuffer is simple. It ignores the input buffers.
  33. // EncoderFallbackBuffer users could implement their own complex
  34. // fallback buffers.
  35. public sealed class EncoderReplacementFallbackBuffer
  36. : EncoderFallbackBuffer
  37. {
  38. string replacement;
  39. int current;
  40. bool fallback_assigned;
  41. public EncoderReplacementFallbackBuffer (
  42. EncoderReplacementFallback fallback)
  43. {
  44. if (fallback == null)
  45. throw new ArgumentNullException ("fallback");
  46. replacement = fallback.DefaultString;
  47. current = 0;
  48. }
  49. public override int Remaining {
  50. get { return replacement.Length - current; }
  51. }
  52. public override bool Fallback (char charUnknown, int index)
  53. {
  54. return Fallback (index);
  55. }
  56. public override bool Fallback (char charUnknownHigh, char charUnknownLow, int index)
  57. {
  58. return Fallback (index);
  59. }
  60. // hmm, what is this index for???
  61. private bool Fallback (int index)
  62. {
  63. if (fallback_assigned && Remaining != 0)
  64. throw new ArgumentException ("Reentrant Fallback method invocation occured. It might be because either this FallbackBuffer is incorrectly shared by multiple threads, invoked inside Encoding recursively, or Reset invocation is forgotten.");
  65. if (index < 0)
  66. throw new ArgumentOutOfRangeException ("index");
  67. fallback_assigned = true;
  68. current = 0;
  69. return replacement.Length > 0;
  70. }
  71. public override char GetNextChar ()
  72. {
  73. if (current >= replacement.Length)
  74. return char.MinValue;
  75. return replacement [current++];
  76. }
  77. public override bool MovePrevious ()
  78. {
  79. if (current == 0)
  80. return false;
  81. current--;
  82. return true;
  83. }
  84. public override void Reset ()
  85. {
  86. fallback_assigned = false;
  87. current = 0;
  88. }
  89. }
  90. }