StringReader.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // System.IO.StringReader
  3. //
  4. // Authors:
  5. // Marcin Szczepanski ([email protected])
  6. // Marek Safar ([email protected])
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. // Copyright 2011 Xamarin Inc.
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Globalization;
  33. using System.Runtime.InteropServices;
  34. #if NET_4_5
  35. using System.Threading.Tasks;
  36. #endif
  37. namespace System.IO {
  38. [Serializable]
  39. [ComVisible (true)]
  40. public class StringReader : TextReader {
  41. string source;
  42. int nextChar;
  43. int sourceLength;
  44. static char[] cr_lf;
  45. public StringReader (string s)
  46. {
  47. if (s == null)
  48. throw new ArgumentNullException ("s");
  49. this.source = s;
  50. nextChar = 0;
  51. sourceLength = s.Length;
  52. }
  53. public override void Close ()
  54. {
  55. Dispose (true);
  56. }
  57. protected override void Dispose (bool disposing)
  58. {
  59. source = null;
  60. base.Dispose (disposing);
  61. }
  62. public override int Peek ()
  63. {
  64. if (source == null)
  65. ObjectDisposedException ();
  66. if (nextChar >= sourceLength)
  67. return -1;
  68. return (int)source [nextChar];
  69. }
  70. public override int Read ()
  71. {
  72. if (source == null)
  73. ObjectDisposedException ();
  74. if (nextChar >= sourceLength)
  75. return -1;
  76. return (int)source [nextChar++];
  77. }
  78. // The method will read up to count characters from the StringReader
  79. // into the buffer character array starting at position index. Returns
  80. // the actual number of characters read, or zero if the end of the string
  81. // has been reached and no characters are read.
  82. public override int Read ([In, Out] char[] buffer, int index, int count)
  83. {
  84. if (source == null)
  85. ObjectDisposedException ();
  86. if (buffer == null)
  87. throw new ArgumentNullException ("buffer");
  88. if (buffer.Length - index < count)
  89. throw new ArgumentException ();
  90. if (index < 0 || count < 0)
  91. throw new ArgumentOutOfRangeException ();
  92. int charsToRead;
  93. // reordered to avoir possible integer overflow
  94. if (nextChar > sourceLength - count)
  95. charsToRead = sourceLength - nextChar;
  96. else
  97. charsToRead = count;
  98. source.CopyTo (nextChar, buffer, index, charsToRead);
  99. nextChar += charsToRead;
  100. return charsToRead;
  101. }
  102. public override string ReadLine ()
  103. {
  104. // Reads until next \r or \n or \r\n, otherwise return null
  105. if (source == null)
  106. ObjectDisposedException ();
  107. if (nextChar >= source.Length)
  108. return null;
  109. if (cr_lf == null)
  110. cr_lf = new char [] { '\n', '\r' };
  111. int readto = source.IndexOfAny (cr_lf, nextChar);
  112. if (readto == -1)
  113. return ReadToEnd ();
  114. bool consecutive = source[readto] == '\r'
  115. && readto + 1 < source.Length
  116. && source[readto + 1] == '\n';
  117. string nextLine = source.Substring (nextChar, readto - nextChar);
  118. nextChar = readto + ((consecutive) ? 2 : 1);
  119. return nextLine;
  120. }
  121. public override string ReadToEnd ()
  122. {
  123. if (source == null)
  124. ObjectDisposedException ();
  125. string toEnd = source.Substring (nextChar, sourceLength - nextChar);
  126. nextChar = sourceLength;
  127. return toEnd;
  128. }
  129. #if NET_4_5
  130. //
  131. // All async methods return finished task with a result as it's faster
  132. // than setting up async call
  133. //
  134. public override Task<int> ReadAsync (char[] buffer, int index, int count)
  135. {
  136. return Task.FromResult (Read (buffer, index, count));
  137. }
  138. public override Task<int> ReadBlockAsync (char[] buffer, int index, int count)
  139. {
  140. return Task.FromResult (ReadBlock (buffer, index, count));
  141. }
  142. public override Task<string> ReadLineAsync ()
  143. {
  144. return Task.FromResult (ReadLine ());
  145. }
  146. public override Task<string> ReadToEndAsync ()
  147. {
  148. return Task.FromResult (ReadToEnd ());
  149. }
  150. #endif
  151. static void ObjectDisposedException ()
  152. {
  153. throw new ObjectDisposedException ("StringReader",
  154. Locale.GetText ("Cannot read from a closed StringReader"));
  155. }
  156. }
  157. }