FPath.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "FirebaseDatabase/Sources/Core/Utilities/FPath.h"
  17. #import "FirebaseDatabase/Sources/Utilities/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]
  34. initWithName:@"FirebaseDatabaseInternalError"
  35. reason:[NSString
  36. stringWithFormat:
  37. @"innerPath (%@) is not within outerPath (%@)",
  38. inner, outer]
  39. userInfo:nil];
  40. }
  41. }
  42. + (FPath *)pathWithString:(NSString *)string {
  43. return [[FPath alloc] initWith:string];
  44. }
  45. - (id)initWith:(NSString *)path {
  46. self = [super init];
  47. if (self) {
  48. NSArray *pathPieces = [path componentsSeparatedByString:@"/"];
  49. NSMutableArray *newPieces = [[NSMutableArray alloc] init];
  50. for (NSInteger i = 0; i < pathPieces.count; i++) {
  51. NSString *piece = [pathPieces objectAtIndex:i];
  52. if (piece.length > 0) {
  53. [newPieces addObject:piece];
  54. }
  55. }
  56. self.pieces = newPieces;
  57. self.pieceNum = 0;
  58. }
  59. return self;
  60. }
  61. - (id)initWithPieces:(NSArray *)somePieces andPieceNum:(NSInteger)aPieceNum {
  62. self = [super init];
  63. if (self) {
  64. self.pieceNum = aPieceNum;
  65. self.pieces = somePieces;
  66. }
  67. return self;
  68. }
  69. - (id)copyWithZone:(NSZone *)zone {
  70. // Immutable, so it's safe to return self
  71. return self;
  72. }
  73. - (NSString *)description {
  74. return [self toString];
  75. }
  76. #pragma mark -
  77. #pragma mark Public methods
  78. - (NSString *)getFront {
  79. if (self.pieceNum >= self.pieces.count) {
  80. return nil;
  81. }
  82. return [self.pieces objectAtIndex:self.pieceNum];
  83. }
  84. /**
  85. * @return The number of segments in this path
  86. */
  87. - (NSUInteger)length {
  88. return self.pieces.count - self.pieceNum;
  89. }
  90. - (FPath *)popFront {
  91. NSInteger newPieceNum = self.pieceNum;
  92. if (newPieceNum < self.pieces.count) {
  93. newPieceNum++;
  94. }
  95. return [[FPath alloc] initWithPieces:self.pieces andPieceNum:newPieceNum];
  96. }
  97. - (NSString *)getBack {
  98. if (self.pieceNum < self.pieces.count) {
  99. return [self.pieces lastObject];
  100. } else {
  101. return nil;
  102. }
  103. }
  104. - (NSString *)toString {
  105. return [self toStringWithTrailingSlash:NO];
  106. }
  107. - (NSString *)toStringWithTrailingSlash {
  108. return [self toStringWithTrailingSlash:YES];
  109. }
  110. - (NSString *)toStringWithTrailingSlash:(BOOL)trailingSlash {
  111. NSMutableString *pathString = [[NSMutableString alloc] init];
  112. for (NSInteger i = self.pieceNum; i < self.pieces.count; i++) {
  113. [pathString appendString:@"/"];
  114. [pathString appendString:[self.pieces objectAtIndex:i]];
  115. }
  116. if ([pathString length] == 0) {
  117. return @"/";
  118. } else {
  119. if (trailingSlash) {
  120. [pathString appendString:@"/"];
  121. }
  122. return pathString;
  123. }
  124. }
  125. - (NSString *)wireFormat {
  126. if ([self isEmpty]) {
  127. return @"/";
  128. } else {
  129. NSMutableString *pathString = [[NSMutableString alloc] init];
  130. for (NSInteger i = self.pieceNum; i < self.pieces.count; i++) {
  131. if (i > self.pieceNum) {
  132. [pathString appendString:@"/"];
  133. }
  134. [pathString appendString:[self.pieces objectAtIndex:i]];
  135. }
  136. return pathString;
  137. }
  138. }
  139. - (FPath *)parent {
  140. if (self.pieceNum >= self.pieces.count) {
  141. return nil;
  142. } else {
  143. NSMutableArray *newPieces = [[NSMutableArray alloc] init];
  144. for (NSInteger i = self.pieceNum; i < self.pieces.count - 1; i++) {
  145. [newPieces addObject:[self.pieces objectAtIndex:i]];
  146. }
  147. return [[FPath alloc] initWithPieces:newPieces andPieceNum:0];
  148. }
  149. }
  150. - (FPath *)child:(FPath *)childPathObj {
  151. NSMutableArray *newPieces = [[NSMutableArray alloc] init];
  152. for (NSInteger i = self.pieceNum; i < self.pieces.count; i++) {
  153. [newPieces addObject:[self.pieces objectAtIndex:i]];
  154. }
  155. for (NSInteger i = childPathObj.pieceNum; i < childPathObj.pieces.count;
  156. i++) {
  157. [newPieces addObject:[childPathObj.pieces objectAtIndex:i]];
  158. }
  159. return [[FPath alloc] initWithPieces:newPieces andPieceNum:0];
  160. }
  161. - (FPath *)childFromString:(NSString *)childPath {
  162. NSMutableArray *newPieces = [[NSMutableArray alloc] init];
  163. for (NSInteger i = self.pieceNum; i < self.pieces.count; i++) {
  164. [newPieces addObject:[self.pieces objectAtIndex:i]];
  165. }
  166. NSArray *pathPieces = [childPath componentsSeparatedByString:@"/"];
  167. for (unsigned int i = 0; i < pathPieces.count; i++) {
  168. NSString *piece = [pathPieces objectAtIndex:i];
  169. if (piece.length > 0) {
  170. [newPieces addObject:piece];
  171. }
  172. }
  173. return [[FPath alloc] initWithPieces:newPieces andPieceNum:0];
  174. }
  175. /**
  176. * @return True if there are no segments in this path
  177. */
  178. - (BOOL)isEmpty {
  179. return self.pieceNum >= self.pieces.count;
  180. }
  181. /**
  182. * @return Singleton to represent an empty path
  183. */
  184. + (FPath *)empty {
  185. static dispatch_once_t oneEmptyPath;
  186. static FPath *emptyPath;
  187. dispatch_once(&oneEmptyPath, ^{
  188. emptyPath = [[FPath alloc] initWith:@""];
  189. });
  190. return emptyPath;
  191. }
  192. - (BOOL)contains:(FPath *)other {
  193. if (self.length > other.length) {
  194. return NO;
  195. }
  196. NSInteger i = self.pieceNum;
  197. NSInteger j = other.pieceNum;
  198. while (i < self.pieces.count) {
  199. NSString *thisSeg = [self.pieces objectAtIndex:i];
  200. NSString *otherSeg = [other.pieces objectAtIndex:j];
  201. if (![thisSeg isEqualToString:otherSeg]) {
  202. return NO;
  203. }
  204. ++i;
  205. ++j;
  206. }
  207. return YES;
  208. }
  209. - (void)enumerateComponentsUsingBlock:(void (^)(NSString *, BOOL *))block {
  210. BOOL stop = NO;
  211. for (NSInteger i = self.pieceNum; !stop && i < self.pieces.count; i++) {
  212. block(self.pieces[i], &stop);
  213. }
  214. }
  215. - (NSComparisonResult)compare:(FPath *)other {
  216. NSInteger myCount = self.pieces.count;
  217. NSInteger otherCount = other.pieces.count;
  218. for (NSInteger i = self.pieceNum, j = other.pieceNum;
  219. i < myCount && j < otherCount; i++, j++) {
  220. NSComparisonResult comparison = [FUtilities compareKey:self.pieces[i]
  221. toKey:other.pieces[j]];
  222. if (comparison != NSOrderedSame) {
  223. return comparison;
  224. }
  225. }
  226. if (self.length < other.length) {
  227. return NSOrderedAscending;
  228. } else if (other.length < self.length) {
  229. return NSOrderedDescending;
  230. } else {
  231. NSAssert(self.length == other.length,
  232. @"Paths must be the same lengths");
  233. return NSOrderedSame;
  234. }
  235. }
  236. /**
  237. * @return YES if paths are the same
  238. */
  239. - (BOOL)isEqual:(id)other {
  240. if (other == self) {
  241. return YES;
  242. }
  243. if (!other || ![other isKindOfClass:[self class]]) {
  244. return NO;
  245. }
  246. FPath *otherPath = (FPath *)other;
  247. if (self.length != otherPath.length) {
  248. return NO;
  249. }
  250. for (NSUInteger i = self.pieceNum, j = otherPath.pieceNum;
  251. i < self.pieces.count; i++, j++) {
  252. if (![self.pieces[i] isEqualToString:otherPath.pieces[j]]) {
  253. return NO;
  254. }
  255. }
  256. return YES;
  257. }
  258. - (NSUInteger)hash {
  259. NSUInteger hashCode = 0;
  260. for (NSInteger i = self.pieceNum; i < self.pieces.count; i++) {
  261. hashCode = hashCode * 37 + [self.pieces[i] hash];
  262. }
  263. return hashCode;
  264. }
  265. @end