FIRDecimal128Value.mm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright 2025 Google LLC
  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. #include "Firestore/Source/Public/FirebaseFirestore/FIRDecimal128Value.h"
  17. #include "Firestore/core/src/util/quadruple.h"
  18. #include "Firestore/core/src/util/string_apple.h"
  19. using firebase::firestore::util::MakeString;
  20. using firebase::firestore::util::Quadruple;
  21. @implementation FIRDecimal128Value
  22. - (instancetype)initWithValue:(NSString *)value {
  23. self = [super init];
  24. if (self) {
  25. _value = [value copy];
  26. }
  27. return self;
  28. }
  29. - (BOOL)isEqual:(nullable id)object {
  30. if (self == object) {
  31. return YES;
  32. }
  33. if (![object isKindOfClass:[FIRDecimal128Value class]]) {
  34. return NO;
  35. }
  36. FIRDecimal128Value *other = (FIRDecimal128Value *)object;
  37. Quadruple lhs = Quadruple();
  38. Quadruple rhs = Quadruple();
  39. lhs.Parse(MakeString(self.value));
  40. rhs.Parse(MakeString(other.value));
  41. // Firestore considers +0 and -0 to be equal, but `Quadruple::Compare()` does not.
  42. if (lhs.Compare(Quadruple(-0.0)) == 0) lhs = Quadruple();
  43. if (rhs.Compare(Quadruple(-0.0)) == 0) rhs = Quadruple();
  44. return lhs.Compare(rhs) == 0;
  45. }
  46. - (id)copyWithZone:(__unused NSZone *_Nullable)zone {
  47. return [[FIRDecimal128Value alloc] initWithValue:self.value];
  48. }
  49. - (NSString *)description {
  50. return [NSString stringWithFormat:@"<FIRDecimal128Value: (%@)>", self.value];
  51. }
  52. @end