/vendor/TouchJSON/Extensions/CDataScanner_Extensions.m

http://github.com/tgunr/passengerpane · Objective C · 135 lines · 86 code · 20 blank · 29 comment · 24 complexity · 4b326e15fac554cebe9a2633a95e417a MD5 · raw file

  1. //
  2. // CDataScanner_Extensions.m
  3. // TouchCode
  4. //
  5. // Created by Jonathan Wight on 12/08/2005.
  6. // Copyright 2005 toxicsoftware.com. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person
  9. // obtaining a copy of this software and associated documentation
  10. // files (the "Software"), to deal in the Software without
  11. // restriction, including without limitation the rights to use,
  12. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the
  14. // Software is furnished to do so, subject to the following
  15. // conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  22. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  24. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  25. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. // OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #import "CDataScanner_Extensions.h"
  30. #define LF 0x000a // Line Feed
  31. #define FF 0x000c // Form Feed
  32. #define CR 0x000d // Carriage Return
  33. #define NEL 0x0085 // Next Line
  34. #define LS 0x2028 // Line Separator
  35. #define PS 0x2029 // Paragraph Separator
  36. @implementation CDataScanner (CDataScanner_Extensions)
  37. - (BOOL)scanCStyleComment:(NSString **)outComment
  38. {
  39. if ([self scanString:@"/*" intoString:NULL] == YES)
  40. {
  41. NSString *theComment = NULL;
  42. if ([self scanUpToString:@"*/" intoString:&theComment] == NO)
  43. [NSException raise:NSGenericException format:@"Started to scan a C style comment but it wasn't terminated."];
  44. if ([theComment rangeOfString:@"/*"].location != NSNotFound)
  45. [NSException raise:NSGenericException format:@"C style comments should not be nested."];
  46. if ([self scanString:@"*/" intoString:NULL] == NO)
  47. [NSException raise:NSGenericException format:@"C style comment did not end correctly."];
  48. if (outComment != NULL)
  49. *outComment = theComment;
  50. return(YES);
  51. }
  52. else
  53. {
  54. return(NO);
  55. }
  56. }
  57. - (BOOL)scanCPlusPlusStyleComment:(NSString **)outComment
  58. {
  59. if ([self scanString:@"//" intoString:NULL] == YES)
  60. {
  61. unichar theCharacters[] = { LF, FF, CR, NEL, LS, PS, };
  62. NSCharacterSet *theLineBreaksCharacterSet = [NSCharacterSet characterSetWithCharactersInString:[NSString stringWithCharacters:theCharacters length:sizeof(theCharacters) / sizeof(*theCharacters)]];
  63. NSString *theComment = NULL;
  64. [self scanUpToCharactersFromSet:theLineBreaksCharacterSet intoString:&theComment];
  65. [self scanCharactersFromSet:theLineBreaksCharacterSet intoString:NULL];
  66. if (outComment != NULL)
  67. *outComment = theComment;
  68. return(YES);
  69. }
  70. else
  71. {
  72. return(NO);
  73. }
  74. }
  75. - (NSUInteger)lineOfScanLocation
  76. {
  77. NSUInteger theLine = 0;
  78. for (const u_int8_t *C = start; C < current; ++C)
  79. {
  80. // TODO: JIW What about MS-DOS line endings you bastard! (Also other unicode line endings)
  81. if (*C == '\n' || *C == '\r')
  82. {
  83. ++theLine;
  84. }
  85. }
  86. return(theLine);
  87. }
  88. - (NSDictionary *)userInfoForScanLocation
  89. {
  90. NSUInteger theLine = 0;
  91. const u_int8_t *theLineStart = start;
  92. for (const u_int8_t *C = start; C < current; ++C)
  93. {
  94. if (*C == '\n' || *C == '\r')
  95. {
  96. theLineStart = C - 1;
  97. ++theLine;
  98. }
  99. }
  100. NSUInteger theCharacter = current - theLineStart;
  101. NSRange theStartRange = NSIntersectionRange((NSRange){ .location = MAX((NSInteger)self.scanLocation - 20, 0), .length = 20 + (NSInteger)self.scanLocation - 20 }, (NSRange){ .location = 0, .length = self.data.length });
  102. NSRange theEndRange = NSIntersectionRange((NSRange){ .location = self.scanLocation, .length = 20 }, (NSRange){ .location = 0, .length = self.data.length });
  103. NSString *theSnippet = [NSString stringWithFormat:@"%@!HERE>!%@",
  104. [[[NSString alloc] initWithData:[self.data subdataWithRange:theStartRange] encoding:NSUTF8StringEncoding] autorelease],
  105. [[[NSString alloc] initWithData:[self.data subdataWithRange:theEndRange] encoding:NSUTF8StringEncoding] autorelease]
  106. ];
  107. NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
  108. [NSNumber numberWithUnsignedInteger:theLine], @"line",
  109. [NSNumber numberWithUnsignedInteger:theCharacter], @"character",
  110. [NSNumber numberWithUnsignedInteger:self.scanLocation], @"location",
  111. theSnippet, @"snippet",
  112. NULL];
  113. return(theUserInfo);
  114. }
  115. @end