The Labs \ Source Viewer \ SSCLI \ System.IO \ StringReader

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  StringReader
  18. **
  19. ** Purpose: For reading text from strings
  20. **
  21. **
  22. ===========================================================*/
  23. using System;
  24. using System.Runtime.InteropServices;
  25. namespace System.IO
  26. {
  27.     // This class implements a text reader that reads from a string.
  28.     //
  29.     [Serializable()]
  30.     [System.Runtime.InteropServices.ComVisible(true)]
  31.     public class StringReader : TextReader
  32.     {
  33.         private string _s;
  34.         private int _pos;
  35.         private int _length;
  36.        
  37.         public StringReader(string s)
  38.         {
  39.             if (s == null)
  40.                 throw new ArgumentNullException("s");
  41.             _s = s;
  42.             _length = s == null ? 0 : s.Length;
  43.         }
  44.        
  45.         // Closes this StringReader. Following a call to this method, the String
  46.         // Reader will throw an ObjectDisposedException.
  47.         public override void Close()
  48.         {
  49.             Dispose(true);
  50.         }
  51.        
  52.         protected override void Dispose(bool disposing)
  53.         {
  54.             _s = null;
  55.             _pos = 0;
  56.             _length = 0;
  57.             base.Dispose(disposing);
  58.         }
  59.        
  60.         // Returns the next available character without actually reading it from
  61.         // the underlying string. The current position of the StringReader is not
  62.         // changed by this operation. The returned value is -1 if no further
  63.         // characters are available.
  64.         //
  65.         public override int Peek()
  66.         {
  67.             if (_s == null)
  68.                 __Error.ReaderClosed();
  69.             if (_pos == _length)
  70.                 return -1;
  71.             return _s[_pos];
  72.         }
  73.        
  74.         // Reads the next character from the underlying string. The returned value
  75.         // is -1 if no further characters are available.
  76.         //
  77.         public override int Read()
  78.         {
  79.             if (_s == null)
  80.                 __Error.ReaderClosed();
  81.             if (_pos == _length)
  82.                 return -1;
  83.             return _s[_pos++];
  84.         }
  85.        
  86.         // Reads a block of characters. This method will read up to count
  87.         // characters from this StringReader into the buffer character
  88.         // array starting at position index. Returns the actual number of
  89.         // characters read, or zero if the end of the string is reached.
  90.         //
  91.         public override int Read(        [In(), Out()]
  92. char[] buffer, int index, int count)
  93.         {
  94.             if (buffer == null)
  95.                 throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
  96.             if (index < 0)
  97.                 throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  98.             if (count < 0)
  99.                 throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  100.             if (buffer.Length - index < count)
  101.                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
  102.             if (_s == null)
  103.                 __Error.ReaderClosed();
  104.            
  105.             int n = _length - _pos;
  106.             if (n > 0) {
  107.                 if (n > count)
  108.                     n = count;
  109.                 _s.CopyTo(_pos, buffer, index, n);
  110.                 _pos += n;
  111.             }
  112.             return n;
  113.         }
  114.        
  115.         public override string ReadToEnd()
  116.         {
  117.             if (_s == null)
  118.                 __Error.ReaderClosed();
  119.             string s;
  120.             if (_pos == 0)
  121.                 s = _s;
  122.             else
  123.                 s = _s.Substring(_pos, _length - _pos);
  124.             _pos = _length;
  125.             return s;
  126.         }
  127.        
  128.         // Reads a line. A line is defined as a sequence of characters followed by
  129.         // a carriage return ('\r'), a line feed ('\n'), or a carriage return
  130.         // immediately followed by a line feed. The resulting string does not
  131.         // contain the terminating carriage return and/or line feed. The returned
  132.         // value is null if the end of the underlying string has been reached.
  133.         //
  134.         public override string ReadLine()
  135.         {
  136.             if (_s == null)
  137.                 __Error.ReaderClosed();
  138.             int i = _pos;
  139.             while (i < _length) {
  140.                 char ch = _s[i];
  141.                 if (ch == '\r' || ch == '\n') {
  142.                     string result = _s.Substring(_pos, i - _pos);
  143.                     _pos = i + 1;
  144.                     if (ch == '\r' && _pos < _length && _s[_pos] == '\n')
  145.                         _pos++;
  146.                     return result;
  147.                 }
  148.                 i++;
  149.             }
  150.             if (i > _pos) {
  151.                 string result = _s.Substring(_pos, i - _pos);
  152.                 _pos = i;
  153.                 return result;
  154.             }
  155.             return null;
  156.         }
  157.     }
  158. }

Developer Fusion