StringReader.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // System.IO.StringReader
  3. //
  4. // Author: Marcin Szczepanski ([email protected])
  5. //
  6. // Copyright (C) 2004 Novell (http://www.novell.com)
  7. //
  8. //
  9. // Copyright (C) 2004 Novell, Inc (http://www.novell.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. using System;
  31. using System.Globalization;
  32. using System.Runtime.InteropServices;
  33. namespace System.IO {
  34. [Serializable]
  35. #if NET_2_0
  36. [ComVisible (true)]
  37. #endif
  38. public class StringReader : TextReader {
  39. string source;
  40. int nextChar;
  41. int sourceLength;
  42. public StringReader( string s ) {
  43. if (s == null)
  44. throw new ArgumentNullException ("s");
  45. this.source = s;
  46. nextChar = 0;
  47. sourceLength = s.Length;
  48. }
  49. public override void Close ()
  50. {
  51. Dispose (true);
  52. }
  53. protected override void Dispose (bool disposing)
  54. {
  55. source = null;
  56. base.Dispose (disposing);
  57. }
  58. public override int Peek() {
  59. CheckObjectDisposedException ();
  60. if( nextChar >= sourceLength ) {
  61. return -1;
  62. } else {
  63. return (int)source[ nextChar ];
  64. }
  65. }
  66. public override int Read() {
  67. CheckObjectDisposedException ();
  68. if( nextChar >= sourceLength ) {
  69. return -1;
  70. } else {
  71. return (int)source[ nextChar++ ];
  72. }
  73. }
  74. // The method will read up to count characters from the StringReader
  75. // into the buffer character array starting at position index. Returns
  76. // the actual number of characters read, or zero if the end of the string
  77. // has been reached and no characters are read.
  78. public override int Read ([In, Out] char[] buffer, int index, int count)
  79. {
  80. CheckObjectDisposedException ();
  81. if (buffer == null)
  82. throw new ArgumentNullException ("buffer");
  83. if (buffer.Length - index < count)
  84. throw new ArgumentException ();
  85. if (index < 0 || count < 0)
  86. throw new ArgumentOutOfRangeException ();
  87. int charsToRead;
  88. // reordered to avoir possible integer overflow
  89. if (nextChar > sourceLength - count)
  90. charsToRead = sourceLength - nextChar;
  91. else
  92. charsToRead = count;
  93. source.CopyTo (nextChar, buffer, index, charsToRead);
  94. nextChar += charsToRead;
  95. return charsToRead;
  96. }
  97. public override string ReadLine ()
  98. {
  99. // Reads until next \r or \n or \r\n, otherwise return null
  100. // LAMESPEC:
  101. // The Beta 2 SDK help says that the ReadLine method
  102. // returns "The next line from the input stream [...] A
  103. // line is defined as a sequence of characters followed by
  104. // a carriage return (\r), a line feed (\n), or a carriage
  105. // return immediately followed by a line feed (\r\n).
  106. // [...] The returned value is a null reference if the end
  107. // of the input stream has been reached."
  108. //
  109. // HOWEVER, the MS implementation returns the rest of
  110. // the string if no \r and/or \n is found in the string
  111. CheckObjectDisposedException ();
  112. if (nextChar >= source.Length)
  113. return null;
  114. int nextCR = source.IndexOf ('\r', nextChar);
  115. int nextLF = source.IndexOf ('\n', nextChar);
  116. int readTo;
  117. bool consecutive = false;
  118. if (nextCR == -1) {
  119. if (nextLF == -1)
  120. return ReadToEnd ();
  121. readTo = nextLF;
  122. } else if (nextLF == -1) {
  123. readTo = nextCR;
  124. } else {
  125. readTo = (nextCR > nextLF) ? nextLF : nextCR;
  126. consecutive = (nextCR + 1 == nextLF || nextLF + 1 == nextCR);
  127. }
  128. string nextLine = source.Substring (nextChar, readTo - nextChar);
  129. nextChar = readTo + ((consecutive) ? 2 : 1);
  130. return nextLine;
  131. }
  132. public override string ReadToEnd() {
  133. CheckObjectDisposedException ();
  134. string toEnd = source.Substring( nextChar, sourceLength - nextChar );
  135. nextChar = sourceLength;
  136. return toEnd;
  137. }
  138. private void CheckObjectDisposedException ()
  139. {
  140. if (source == null)
  141. throw new ObjectDisposedException ("StringReader",
  142. Locale.GetText ("Cannot read from a closed StringReader"));
  143. }
  144. }
  145. }