JsonBuffer.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright 2010-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. namespace MongoDB.Bson.IO
  16. {
  17. /// <summary>
  18. /// This class represents a JSON string buffer.
  19. /// </summary>
  20. public class JsonBuffer
  21. {
  22. // private fields
  23. private string _buffer;
  24. private int _position;
  25. // constructors
  26. /// <summary>
  27. /// Initializes a new instance of the JsonBuffer class.
  28. /// </summary>
  29. /// <param name="buffer">The string.</param>
  30. public JsonBuffer(string buffer)
  31. {
  32. _buffer = buffer;
  33. _position = 0;
  34. }
  35. // internal properties
  36. /// <summary>
  37. /// Gets the length of the JSON string.
  38. /// </summary>
  39. public int Length
  40. {
  41. get { return _buffer.Length; }
  42. }
  43. /// <summary>
  44. /// Gets or sets the current position.
  45. /// </summary>
  46. public int Position
  47. {
  48. get { return _position; }
  49. set { _position = value; }
  50. }
  51. // public methods
  52. /// <summary>
  53. /// Reads a character from the buffer.
  54. /// </summary>
  55. /// <returns>The next character (or -1 if at the end of the buffer).</returns>
  56. public int Read()
  57. {
  58. return (_position >= _buffer.Length) ? -1 : _buffer[_position++];
  59. }
  60. /// <summary>
  61. /// Reads a substring from the buffer.
  62. /// </summary>
  63. /// <param name="start">The zero based index of the start of the substring.</param>
  64. /// <returns>The substring.</returns>
  65. public string Substring(int start)
  66. {
  67. return _buffer.Substring(start);
  68. }
  69. /// <summary>
  70. /// Reads a substring from the buffer.
  71. /// </summary>
  72. /// <param name="start">The zero based index of the start of the substring.</param>
  73. /// <param name="count">The number of characters in the substring.</param>
  74. /// <returns>The substring.</returns>
  75. public string Substring(int start, int count)
  76. {
  77. return _buffer.Substring(start, count);
  78. }
  79. /// <summary>
  80. /// Returns one character to the buffer (if the character matches the one at the current position the current position is moved back by one).
  81. /// </summary>
  82. /// <param name="c">The character to return.</param>
  83. public void UnRead(int c)
  84. {
  85. if (c != -1 && _buffer[_position - 1] == c)
  86. {
  87. _position -= 1;
  88. }
  89. }
  90. }
  91. }