FPath.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FPath.h"
  17. #import "FUtilities.h"
  18. @interface FPath()
  19. @property (nonatomic, readwrite, assign) NSInteger pieceNum;
  20. @property (nonatomic, strong) NSArray * pieces;
  21. @end
  22. @implementation FPath
  23. #pragma mark -
  24. #pragma mark Initializers
  25. + (FPath *) relativePathFrom:(FPath *)outer to:(FPath *)inner {
  26. NSString* outerFront = [outer getFront];
  27. NSString* innerFront = [inner getFront];
  28. if (outerFront == nil) {
  29. return inner;
  30. } else if ([outerFront isEqualToString:innerFront]) {
  31. return [self relativePathFrom:[outer popFront] to:[inner popFront]];
  32. } else {
  33. @throw [[NSException alloc] initWithName:@"FirebaseDatabaseInternalError" reason:[NSString stringWithFormat:@"innerPath (%@) is not within outerPath (%@)", inner, outer] userInfo:nil];
  34. }
  35. }
  36. + (FPath *)pathWithString:(NSString *)string
  37. {
  38. return [[FPath alloc] initWith:string];
  39. }
  40. - (id)initWith:(NSString *)path
  41. {
  42. self = [super init];
  43. if (self) {
  44. NSArray *pathPieces = [path componentsSeparatedByString:@"/"];
  45. NSMutableArray *newPieces = [[NSMutableArray alloc] init];
  46. for (NSInteger i = 0; i < pathPieces.count; i++) {
  47. NSString *piece = [pathPieces objectAtIndex:i];
  48. if (piece.length > 0) {
  49. [newPieces addObject:piece];
  50. }
  51. }
  52. self.pieces = newPieces;
  53. self.pieceNum = 0;
  54. }
  55. return self;
  56. }
  57. - (id)initWithPieces:(NSArray *)somePieces andPieceNum:(NSInteger)aPieceNum {
  58. self = [super init];
  59. if (self) {
  60. self.pieceNum = aPieceNum;
  61. self.pieces = somePieces;
  62. }
  63. return self;
  64. }
  65. - (id)copyWithZone:(NSZone *)zone
  66. {
  67. // Immutable, so it's safe to return self
  68. return self;
  69. }
  70. - (NSString *)description {
  71. return [self toString];
  72. }
  73. #pragma mark -
  74. #pragma mark Public methods
  75. - (NSString *) getFront {
  76. if(self.pieceNum >= self.pieces.count) {
  77. return nil;
  78. }
  79. return [self.pieces objectAtIndex:self.pieceNum];
  80. }
  81. /**
  82. * @return The number of segments in this path
  83. */
  84. - (NSUInteger) length {
  85. return self.pieces.count - self.pieceNum;
  86. }
  87. - (FPath *) popFront {
  88. NSInteger newPieceNum = self.pieceNum;
  89. if (newPieceNum < self.pieces.count) {
  90. newPieceNum++;
  91. }
  92. return [[FPath alloc] initWithPieces:self.pieces andPieceNum:newPieceNum];
  93. }
  94. - (NSString *) getBack {
  95. if(self.pieceNum < self.pieces.count) {
  96. return [self.pieces lastObject];
  97. }
  98. else {
  99. return nil;
  100. }
  101. }
  102. - (NSString *) toString {
  103. return [self toStringWithTrailingSlash:NO];
  104. }
  105. - (NSString *) toStringWithTrailingSlash {
  106. return [self toStringWithTrailingSlash:YES];
  107. }
  108. - (NSString *) toStringWithTrailingSlash:(BOOL)trailingSlash {
  109. NSMutableString* pathString = [[NSMutableString alloc] init];
  110. for(NSInteger i = self.pieceNum; i < self.pieces.count; i++) {
  111. [pathString appendString:@"/"];
  112. [pathString appendString:[self.pieces objectAtIndex:i]];
  113. }
  114. if ([pathString length] == 0) {
  115. return @"/";
  116. } else {
  117. if (trailingSlash) {
  118. [pathString appendString:@"/"];
  119. }
  120. return pathString;
  121. }
  122. }
  123. - (NSString *)wireFormat {
  124. if ([self isEmpty]) {
  125. return @"/";
  126. } else {
  127. NSMutableString* pathString = [[NSMutableString alloc] init];
  128. for (NSInteger i = self.pieceNum; i < self.pieces.count; i++) {
  129. if (i > self.pieceNum) {
  130. [pathString appendString:@"/"];
  131. }
  132. [pathString appendString:[self.pieces objectAtIndex:i]];
  133. }
  134. return pathString;
  135. }
  136. }
  137. - (FPath *) parent {
  138. if(self.pieceNum >= self.pieces.count) {
  139. return nil;
  140. } else {
  141. NSMutableArray* newPieces = [[NSMutableArray alloc] init];
  142. for (NSInteger i = self.pieceNum; i < self.pieces.count - 1; i++) {
  143. [newPieces addObject:[self.pieces objectAtIndex:i]];
  144. }
  145. return [[FPath alloc] initWithPieces:newPieces andPieceNum:0];
  146. }
  147. }
  148. - (FPath *) child:(FPath *)childPathObj {
  149. NSMutableArray* newPieces = [[NSMutableArray alloc] init];
  150. for (NSInteger i = self.pieceNum; i < self.pieces.count; i++) {
  151. [newPieces addObject:[self.pieces objectAtIndex:i]];
  152. }
  153. for (NSInteger i = childPathObj.pieceNum; i < childPathObj.pieces.count; i++) {
  154. [newPieces addObject:[childPathObj.pieces objectAtIndex:i]];
  155. }
  156. return [[FPath alloc] initWithPieces:newPieces andPieceNum:0];
  157. }
  158. - (FPath *)childFromString:(NSString *)childPath {
  159. NSMutableArray* newPieces = [[NSMutableArray alloc] init];
  160. for (NSInteger i = self.pieceNum; i < self.pieces.count; i++) {
  161. [newPieces addObject:[self.pieces objectAtIndex:i]];
  162. }
  163. NSArray *pathPieces = [childPath componentsSeparatedByString:@"/"];
  164. for (unsigned int i = 0; i < pathPieces.count; i++) {
  165. NSString *piece = [pathPieces objectAtIndex:i];
  166. if (piece.length > 0) {
  167. [newPieces addObject:piece];
  168. }
  169. }
  170. return [[FPath alloc] initWithPieces:newPieces andPieceNum:0];
  171. }
  172. /**
  173. * @return True if there are no segments in this path
  174. */
  175. - (BOOL) isEmpty {
  176. return self.pieceNum >= self.pieces.count;
  177. }
  178. /**
  179. * @return Singleton to represent an empty path
  180. */
  181. + (FPath *) empty {
  182. static dispatch_once_t oneEmptyPath;
  183. static FPath *emptyPath;
  184. dispatch_once(&oneEmptyPath, ^{
  185. emptyPath = [[FPath alloc] initWith:@""];
  186. });
  187. return emptyPath;
  188. }
  189. - (BOOL) contains:(FPath *)other {
  190. if (self.length > other.length) {
  191. return NO;
  192. }
  193. NSInteger i = self.pieceNum;
  194. NSInteger j = other.pieceNum;
  195. while (i < self.pieces.count) {
  196. NSString* thisSeg = [self.pieces objectAtIndex:i];
  197. NSString* otherSeg = [other.pieces objectAtIndex:j];
  198. if (![thisSeg isEqualToString:otherSeg]) {
  199. return NO;
  200. }
  201. ++i;
  202. ++j;
  203. }
  204. return YES;
  205. }
  206. - (void) enumerateComponentsUsingBlock:(void (^)(NSString *, BOOL *))block {
  207. BOOL stop = NO;
  208. for (NSInteger i = self.pieceNum; !stop && i < self.pieces.count; i++) {
  209. block(self.pieces[i], &stop);
  210. }
  211. }
  212. - (NSComparisonResult) compare:(FPath *)other {
  213. NSInteger myCount = self.pieces.count;
  214. NSInteger otherCount = other.pieces.count;
  215. for (NSInteger i = self.pieceNum, j = other.pieceNum; i < myCount && j < otherCount; i++, j++) {
  216. NSComparisonResult comparison = [FUtilities compareKey:self.pieces[i] toKey:other.pieces[j]];
  217. if (comparison != NSOrderedSame) {
  218. return comparison;
  219. }
  220. }
  221. if (self.length < other.length) {
  222. return NSOrderedAscending;
  223. } else if (other.length < self.length) {
  224. return NSOrderedDescending;
  225. } else {
  226. NSAssert(self.length == other.length, @"Paths must be the same lengths");
  227. return NSOrderedSame;
  228. }
  229. }
  230. /**
  231. * @return YES if paths are the same
  232. */
  233. - (BOOL)isEqual:(id)other
  234. {
  235. if (other == self) {
  236. return YES;
  237. }
  238. if (!other || ![other isKindOfClass:[self class]]) {
  239. return NO;
  240. }
  241. FPath *otherPath = (FPath *)other;
  242. if (self.length != otherPath.length) {
  243. return NO;
  244. }
  245. for (NSUInteger i = self.pieceNum, j = otherPath.pieceNum; i < self.pieces.count; i++, j++) {
  246. if (![self.pieces[i] isEqualToString:otherPath.pieces[j]]) {
  247. return NO;
  248. }
  249. }
  250. return YES;
  251. }
  252. - (NSUInteger) hash {
  253. NSUInteger hashCode = 0;
  254. for (NSInteger i = self.pieceNum; i < self.pieces.count; i++) {
  255. hashCode = hashCode * 37 + [self.pieces[i] hash];
  256. }
  257. return hashCode;
  258. }
  259. @end