range.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GFX_RANGE_RANGE_H_
  5. #define UI_GFX_RANGE_RANGE_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <algorithm>
  9. #include <limits>
  10. #include <ostream>
  11. #include <string>
  12. #include "base/numerics/safe_conversions.h"
  13. #include "build/build_config.h"
  14. #include "ui/gfx/range/gfx_range_export.h"
  15. #if BUILDFLAG(IS_APPLE)
  16. #if __OBJC__
  17. #import <Foundation/Foundation.h>
  18. #else
  19. typedef struct _NSRange NSRange;
  20. #endif
  21. #endif // BUILDFLAG(IS_APPLE)
  22. namespace gfx {
  23. // This class represents either a forward range [min, max) or a reverse range
  24. // (max, min]. |start_| is always the first of these and |end_| the second; as a
  25. // result, the range is forward if (start_ <= end_). The zero-width range
  26. // [val, val) is legal, contains and intersects itself, and is contained by and
  27. // intersects any nonempty range [min, max) where min <= val < max.
  28. class GFX_RANGE_EXPORT Range {
  29. public:
  30. // Creates an empty range {0,0}.
  31. constexpr Range() : Range(0) {}
  32. // Initializes the range with a start and end.
  33. constexpr Range(size_t start, size_t end)
  34. : start_(base::checked_cast<uint32_t>(start)),
  35. end_(base::checked_cast<uint32_t>(end)) {}
  36. // Initializes the range with the same start and end positions.
  37. constexpr explicit Range(size_t position) : Range(position, position) {}
  38. // Platform constructors.
  39. #if BUILDFLAG(IS_APPLE)
  40. explicit Range(const NSRange& range);
  41. #endif
  42. // Returns a range that is invalid, which is {UINT32_MAX,UINT32_MAX}.
  43. static constexpr Range InvalidRange() {
  44. return Range(std::numeric_limits<uint32_t>::max());
  45. }
  46. // Checks if the range is valid through comparison to InvalidRange(). If this
  47. // is not valid, you must not call start()/end().
  48. constexpr bool IsValid() const { return *this != InvalidRange(); }
  49. // Getters and setters.
  50. constexpr size_t start() const { return start_; }
  51. void set_start(size_t start) { start_ = base::checked_cast<uint32_t>(start); }
  52. constexpr size_t end() const { return end_; }
  53. void set_end(size_t end) { end_ = base::checked_cast<uint32_t>(end); }
  54. // Returns the absolute value of the length.
  55. constexpr size_t length() const { return GetMax() - GetMin(); }
  56. constexpr bool is_reversed() const { return start() > end(); }
  57. constexpr bool is_empty() const { return start() == end(); }
  58. // Returns the minimum and maximum values.
  59. constexpr size_t GetMin() const {
  60. return start() < end() ? start() : end();
  61. }
  62. constexpr size_t GetMax() const {
  63. return start() > end() ? start() : end();
  64. }
  65. constexpr bool operator==(const Range& other) const {
  66. return start() == other.start() && end() == other.end();
  67. }
  68. constexpr bool operator!=(const Range& other) const {
  69. return !(*this == other);
  70. }
  71. constexpr bool EqualsIgnoringDirection(const Range& other) const {
  72. return GetMin() == other.GetMin() && GetMax() == other.GetMax();
  73. }
  74. // Returns true if this range intersects the specified |range|.
  75. constexpr bool Intersects(const Range& range) const {
  76. return Intersect(range).IsValid();
  77. }
  78. // Returns true if this range contains the specified |range|.
  79. constexpr bool Contains(const Range& range) const {
  80. return range.IsBoundedBy(*this) &&
  81. // A non-empty range doesn't contain the range [max, max).
  82. (range.GetMax() != GetMax() || range.is_empty() == is_empty());
  83. }
  84. // Returns true if this range is contained by the specified |range| or it is
  85. // an empty range and ending the range |range|.
  86. constexpr bool IsBoundedBy(const Range& range) const {
  87. return IsValid() && range.IsValid() && GetMin() >= range.GetMin() &&
  88. GetMax() <= range.GetMax();
  89. }
  90. // Computes the intersection of this range with the given |range|.
  91. // If they don't intersect, it returns an InvalidRange().
  92. // The returned range is always empty or forward (never reversed).
  93. constexpr Range Intersect(const Range& range) const {
  94. const size_t min = std::max(GetMin(), range.GetMin());
  95. const size_t max = std::min(GetMax(), range.GetMax());
  96. return (min < max || Contains(range) || range.Contains(*this))
  97. ? Range(min, max)
  98. : InvalidRange();
  99. }
  100. #if BUILDFLAG(IS_APPLE)
  101. Range& operator=(const NSRange& range);
  102. // NSRange does not store the directionality of a range, so if this
  103. // is_reversed(), the range will get flipped when converted to an NSRange.
  104. NSRange ToNSRange() const;
  105. #endif
  106. // GTK+ has no concept of a range.
  107. std::string ToString() const;
  108. private:
  109. // Note: we use uint32_t instead of size_t because this struct is sent over
  110. // IPC which could span 32 & 64 bit processes.
  111. uint32_t start_;
  112. uint32_t end_;
  113. };
  114. GFX_RANGE_EXPORT std::ostream& operator<<(std::ostream& os, const Range& range);
  115. } // namespace gfx
  116. #endif // UI_GFX_RANGE_RANGE_H_