FLEXSearchToken.m 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // FLEXSearchToken.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 3/22/17.
  6. // Copyright © 2017 Tanner Bennett. All rights reserved.
  7. //
  8. #import "FLEXSearchToken.h"
  9. @interface FLEXSearchToken () {
  10. NSString *flex_description;
  11. }
  12. @end
  13. @implementation FLEXSearchToken
  14. + (instancetype)any {
  15. static FLEXSearchToken *any = nil;
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. any = [self string:nil options:TBWildcardOptionsAny];
  19. });
  20. return any;
  21. }
  22. + (instancetype)string:(NSString *)string options:(TBWildcardOptions)options {
  23. FLEXSearchToken *token = [self new];
  24. token->_string = string;
  25. token->_options = options;
  26. return token;
  27. }
  28. - (BOOL)isAbsolute {
  29. return _options == TBWildcardOptionsNone;
  30. }
  31. - (BOOL)isAny {
  32. return _options == TBWildcardOptionsAny;
  33. }
  34. - (BOOL)isEmpty {
  35. return self.isAny && self.string.length == 0;
  36. }
  37. - (NSString *)description {
  38. if (flex_description) {
  39. return flex_description;
  40. }
  41. switch (_options) {
  42. case TBWildcardOptionsNone:
  43. flex_description = _string;
  44. break;
  45. case TBWildcardOptionsAny:
  46. flex_description = @"*";
  47. break;
  48. default: {
  49. NSMutableString *desc = [NSMutableString new];
  50. if (_options & TBWildcardOptionsPrefix) {
  51. [desc appendString:@"*"];
  52. }
  53. [desc appendString:_string];
  54. if (_options & TBWildcardOptionsSuffix) {
  55. [desc appendString:@"*"];
  56. }
  57. flex_description = desc;
  58. }
  59. }
  60. return flex_description;
  61. }
  62. - (NSUInteger)hash {
  63. return self.description.hash;
  64. }
  65. - (BOOL)isEqual:(id)object {
  66. if ([object isKindOfClass:[FLEXSearchToken class]]) {
  67. FLEXSearchToken *token = object;
  68. return [_string isEqualToString:token->_string] && _options == token->_options;
  69. }
  70. return NO;
  71. }
  72. @end