/* Copyright 2010-2014 MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace MongoDB.Bson.IO
{
///
/// This class represents a JSON string buffer.
///
public class JsonBuffer
{
// private fields
private string _buffer;
private int _position;
// constructors
///
/// Initializes a new instance of the JsonBuffer class.
///
/// The string.
public JsonBuffer(string buffer)
{
_buffer = buffer;
_position = 0;
}
// internal properties
///
/// Gets the length of the JSON string.
///
public int Length
{
get { return _buffer.Length; }
}
///
/// Gets or sets the current position.
///
public int Position
{
get { return _position; }
set { _position = value; }
}
// public methods
///
/// Reads a character from the buffer.
///
/// The next character (or -1 if at the end of the buffer).
public int Read()
{
return (_position >= _buffer.Length) ? -1 : _buffer[_position++];
}
///
/// Reads a substring from the buffer.
///
/// The zero based index of the start of the substring.
/// The substring.
public string Substring(int start)
{
return _buffer.Substring(start);
}
///
/// Reads a substring from the buffer.
///
/// The zero based index of the start of the substring.
/// The number of characters in the substring.
/// The substring.
public string Substring(int start, int count)
{
return _buffer.Substring(start, count);
}
///
/// Returns one character to the buffer (if the character matches the one at the current position the current position is moved back by one).
///
/// The character to return.
public void UnRead(int c)
{
if (c != -1 && _buffer[_position - 1] == c)
{
_position -= 1;
}
}
}
}