SBJsonUTF8Stream.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright (c) 2011, Stig Brautaset. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are
  5. met:
  6. Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. Neither the name of the the author nor the names of its contributors
  12. may be used to endorse or promote products derived from this software
  13. without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  15. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  16. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  17. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #import "SBJsonUTF8Stream.h"
  27. @implementation SBJsonUTF8Stream
  28. @synthesize index = _index;
  29. - (id)init {
  30. self = [super init];
  31. if (self) {
  32. _data = [[NSMutableData alloc] initWithCapacity:4096u];
  33. }
  34. return self;
  35. }
  36. - (void)appendData:(NSData *)data_ {
  37. if (_index) {
  38. // Discard data we've already parsed
  39. [_data replaceBytesInRange:NSMakeRange(0, _index) withBytes:"" length:0];
  40. // Reset index to point to current position
  41. _index = 0;
  42. }
  43. [_data appendData:data_];
  44. // This is an optimisation.
  45. _bytes = (const char*)[_data bytes];
  46. _length = [_data length];
  47. }
  48. - (BOOL)getUnichar:(unichar*)ch {
  49. if (_index < _length) {
  50. *ch = (unichar)_bytes[_index];
  51. return YES;
  52. }
  53. return NO;
  54. }
  55. - (BOOL)getNextUnichar:(unichar*)ch {
  56. if (++_index < _length) {
  57. *ch = (unichar)_bytes[_index];
  58. return YES;
  59. }
  60. return NO;
  61. }
  62. - (BOOL)getStringFragment:(NSString **)string {
  63. NSUInteger start = _index;
  64. while (_index < _length) {
  65. switch (_bytes[_index]) {
  66. case '"':
  67. case '\\':
  68. case 0 ... 0x1f:
  69. *string = [[NSString alloc] initWithBytes:(_bytes + start)
  70. length:(_index - start)
  71. encoding:NSUTF8StringEncoding];
  72. return YES;
  73. break;
  74. default:
  75. _index++;
  76. break;
  77. }
  78. }
  79. return NO;
  80. }
  81. - (void)skip {
  82. _index++;
  83. }
  84. - (void)skipWhitespace {
  85. while (_index < _length) {
  86. switch (_bytes[_index]) {
  87. case ' ':
  88. case '\t':
  89. case '\r':
  90. case '\n':
  91. _index++;
  92. break;
  93. default:
  94. return;
  95. break;
  96. }
  97. }
  98. }
  99. - (BOOL)haveRemainingCharacters:(NSUInteger)chars {
  100. return [_data length] - _index >= chars;
  101. }
  102. - (BOOL)skipCharacters:(const char *)chars length:(NSUInteger)len {
  103. const void *bytes = ((const char*)[_data bytes]) + _index;
  104. if (!memcmp(bytes, chars, len)) {
  105. _index += len;
  106. return YES;
  107. }
  108. return NO;
  109. }
  110. - (NSString*)stringWithRange:(NSRange)range {
  111. return [[NSString alloc] initWithBytes:_bytes + range.location length:range.length encoding:NSUTF8StringEncoding];
  112. }
  113. @end