Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StringReader.cs « System.IO « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: faa665ec0092279667ff37715067911e46cbdc11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// System.IO.StringReader
//
// Author: Marcin Szczepanski (marcins@zipworld.com.au)
//


using System;

namespace System.IO {
	[Serializable]
	public class StringReader : TextReader {

		private string source;
		private char[] sourceChars;

		private int nextChar;
		private int sourceLength;

		public StringReader( string s ) {
			this.source = s;
			nextChar = 0;
			sourceLength = s.Length;
			sourceChars = s.ToCharArray();
		}

		public override void Close() {
			Dispose( true );
		}

		protected override void Dispose( bool disposing ) {
			return;
		}

		public override int Peek() {
			if( nextChar >= sourceLength ) {
				return -1;
			} else {
				return (int)source[ nextChar ];
			}
		}

		public override int Read() {
			if( nextChar >= sourceLength ) {
				return -1;
			} else {
				return (int)source[ nextChar++ ];
			}
		}


		// The method will read up to count characters from the StringReader
		// into the buffer character array starting at position index. Returns
		// the actual number of characters read, or zero if the end of the string
		// has been reached and no characters are read.

		public override int Read( char[] buffer, int index, int count ) {

			if( buffer == null ) {
				throw new ArgumentNullException();
			} else if( buffer.Length - index < count ) {
				throw new ArgumentException();
			} else if( index < 0 || count < 0 ) {
				throw new ArgumentOutOfRangeException();
			}

			int charsToRead;

			if( nextChar + count > sourceLength ) {
				charsToRead = sourceLength - nextChar;
			} else {
				charsToRead = count;
			}

			Array.Copy(sourceChars, nextChar, buffer, index, charsToRead );

			nextChar += count;

			return charsToRead;
		}

		public override string ReadLine() {
			// Reads until next \r or \n or \r\n, otherwise return null

                        // LAMESPEC:
                        // The Beta 2 SDK help says that the ReadLine method returns
                        // "The next line from the input stream [...] A line is defined as a sequence of
                        // characters followed by a carriage return (\r), a line feed (\n), or a carriage
                        // return immediately followed by a line feed (\r\n). [...]
                        // The returned value is a null reference if the end of the input stream has been reached."
                        //
                        // HOWEVER, the MS implementation returns the rest of the string if no \r and/or \n is found
                        // in the string

			if (nextChar == source.Length)
				return null;

			int nextCR = source.IndexOf( '\r', nextChar );
                        int nextLF = source.IndexOf( '\n', nextChar );

                        if( nextCR == -1 && nextLF == -1 ) {
                                return ReadToEnd();
                        }

                        int readTo;

                        if( nextCR == -1 ) {
                                readTo = nextLF;
                        } else {
                                readTo = nextCR;
                        }

                        string nextLine = source.Substring( nextChar, readTo - nextChar );

                        if( nextLF == nextCR + 1 ) {
		                nextChar = readTo + 2;
                        } else {
                                nextChar = readTo + 1;
                        }

			return nextLine;
		}

                public override string ReadToEnd() {
                        string toEnd = source.Substring( nextChar, sourceLength - nextChar );
                        nextChar = sourceLength;
                        return toEnd;
                }

	}
}