StringReader.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // System.IO.StringReader
  3. //
  4. // Author: Marcin Szczepanski ([email protected])
  5. //
  6. using System;
  7. namespace System.IO {
  8. public class StringReader : TextReader {
  9. protected string source;
  10. protected char[] sourceChars;
  11. protected int nextChar;
  12. protected int sourceLength;
  13. public StringReader( string s ) {
  14. this.source = s;
  15. nextChar = 0;
  16. sourceLength = s.Length;
  17. sourceChars = s.ToCharArray();
  18. }
  19. public override void Close() {
  20. Dispose( true );
  21. }
  22. protected override void Dispose( bool disposing ) {
  23. return;
  24. }
  25. public override int Peek() {
  26. if( nextChar >= sourceLength ) {
  27. return -1;
  28. } else {
  29. return (int)source[ nextChar ];
  30. }
  31. }
  32. public override int Read() {
  33. if( nextChar >= sourceLength ) {
  34. return -1;
  35. } else {
  36. return (int)source[ nextChar++ ];
  37. }
  38. }
  39. // The method will read up to count characters from the StringReader
  40. // into the buffer character array starting at position index. Returns
  41. // the actual number of characters read, or zero if the end of the string
  42. // has been reached and no characters are read.
  43. public override int Read( char[] buffer, int index, int count ) {
  44. if( buffer == null ) {
  45. throw new ArgumentNullException();
  46. } else if( buffer.Length - index < count ) {
  47. throw new ArgumentException();
  48. } else if( index < 0 || count < 0 ) {
  49. throw new ArgumentOutOfRangeException();
  50. }
  51. int charsToRead;
  52. if( nextChar + count > sourceLength ) {
  53. charsToRead = sourceLength - nextChar;
  54. } else {
  55. charsToRead = count;
  56. }
  57. Array.Copy(sourceChars, nextChar, buffer, index, charsToRead );
  58. nextChar += count;
  59. return charsToRead;
  60. }
  61. public override string ReadLine() {
  62. // Reads until next \r or \n or \r\n, otherwise return null
  63. // LAMESPEC:
  64. // The Beta 2 SDK help says that the ReadLine method returns
  65. // "The next line from the input stream [...] A line is defined as a sequence of
  66. // characters followed by a carriage return (\r), a line feed (\n), or a carriage
  67. // return immediately followed by a line feed (\r\n). [...]
  68. // The returned value is a null reference if the end of the input stream has been reached."
  69. //
  70. // HOWEVER, the MS implementation returns the rest of the string if no \r and/or \n is found
  71. // in the string
  72. int nextCR = source.IndexOf( '\r', nextChar );
  73. int nextLF = source.IndexOf( '\n', nextChar );
  74. if( nextCR == -1 && nextLF == -1 ) {
  75. return ReadToEnd();
  76. }
  77. if( nextChar > sourceLength ) return null;
  78. int readTo;
  79. if( nextCR == -1 ) {
  80. readTo = nextLF;
  81. } else {
  82. readTo = nextCR;
  83. }
  84. string nextLine = source.Substring( nextChar, readTo - nextChar );
  85. if( nextLF == nextCR + 1 ) {
  86. nextChar = readTo + 2;
  87. } else {
  88. nextChar = readTo + 1;
  89. }
  90. return nextLine;
  91. }
  92. public override string ReadToEnd() {
  93. string toEnd = source.Substring( nextChar, sourceLength - nextChar );
  94. nextChar = sourceLength;
  95. return toEnd;
  96. }
  97. }
  98. }