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 5 #ifndef UI_GFX_RANGE_RANGE_H_ 6 #define UI_GFX_RANGE_RANGE_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <limits> 12 #include <ostream> 13 #include <string> 14 15 #include "build/build_config.h" 16 #include "ui/gfx/range/gfx_range_export.h" 17 18 #if defined(OS_MACOSX) 19 #if __OBJC__ 20 #import <Foundation/Foundation.h> 21 #else 22 typedef struct _NSRange NSRange; 23 #endif 24 #endif // defined(OS_MACOSX) 25 26 #if defined(OS_WIN) 27 typedef struct _charrange CHARRANGE; 28 #endif 29 30 namespace gfx { 31 32 // A Range contains two integer values that represent a numeric range, like the 33 // range of characters in a text selection. A range is made of a start and end 34 // position; when they are the same, the Range is akin to a caret. Note that 35 // |start_| can be greater than |end_| to respect the directionality of the 36 // range. 37 class GFX_RANGE_EXPORT Range { 38 public: 39 // Creates an empty range {0,0}. Range()40 constexpr Range() : Range(0) {} 41 42 // Initializes the range with a start and end. Range(uint32_t start,uint32_t end)43 constexpr Range(uint32_t start, uint32_t end) : start_(start), end_(end) {} 44 45 // Initializes the range with the same start and end positions. Range(uint32_t position)46 constexpr explicit Range(uint32_t position) : Range(position, position) {} 47 48 // Platform constructors. 49 #if defined(OS_MACOSX) 50 explicit Range(const NSRange& range); 51 #elif defined(OS_WIN) 52 // The |total_length| paramater should be used if the CHARRANGE is set to 53 // {0,-1} to indicate the whole range. 54 Range(const CHARRANGE& range, long total_length = -1); 55 #endif 56 57 // Returns a range that is invalid, which is {UINT32_MAX,UINT32_MAX}. InvalidRange()58 static constexpr Range InvalidRange() { 59 return Range(std::numeric_limits<uint32_t>::max()); 60 } 61 62 // Checks if the range is valid through comparison to InvalidRange(). IsValid()63 constexpr bool IsValid() const { return *this != InvalidRange(); } 64 65 // Getters and setters. start()66 constexpr uint32_t start() const { return start_; } set_start(uint32_t start)67 void set_start(uint32_t start) { start_ = start; } 68 end()69 constexpr uint32_t end() const { return end_; } set_end(uint32_t end)70 void set_end(uint32_t end) { end_ = end; } 71 72 // Returns the absolute value of the length. length()73 constexpr uint32_t length() const { return GetMax() - GetMin(); } 74 is_reversed()75 constexpr bool is_reversed() const { return start() > end(); } is_empty()76 constexpr bool is_empty() const { return start() == end(); } 77 78 // Returns the minimum and maximum values. GetMin()79 constexpr uint32_t GetMin() const { 80 return start() < end() ? start() : end(); 81 } GetMax()82 constexpr uint32_t GetMax() const { 83 return start() > end() ? start() : end(); 84 } 85 86 constexpr bool operator==(const Range& other) const { 87 return start() == other.start() && end() == other.end(); 88 } 89 constexpr bool operator!=(const Range& other) const { 90 return !(*this == other); 91 } EqualsIgnoringDirection(const Range & other)92 constexpr bool EqualsIgnoringDirection(const Range& other) const { 93 return GetMin() == other.GetMin() && GetMax() == other.GetMax(); 94 } 95 96 // Returns true if this range intersects the specified |range|. Intersects(const Range & range)97 constexpr bool Intersects(const Range& range) const { 98 return IsValid() && range.IsValid() && 99 !(range.GetMax() < GetMin() || range.GetMin() >= GetMax()); 100 } 101 102 // Returns true if this range contains the specified |range|. Contains(const Range & range)103 constexpr bool Contains(const Range& range) const { 104 return IsValid() && range.IsValid() && GetMin() <= range.GetMin() && 105 range.GetMax() <= GetMax(); 106 } 107 108 // Computes the intersection of this range with the given |range|. 109 // If they don't intersect, it returns an InvalidRange(). 110 // The returned range is always empty or forward (never reversed). 111 Range Intersect(const Range& range) const; 112 113 #if defined(OS_MACOSX) 114 Range& operator=(const NSRange& range); 115 116 // NSRange does not store the directionality of a range, so if this 117 // is_reversed(), the range will get flipped when converted to an NSRange. 118 NSRange ToNSRange() const; 119 #elif defined(OS_WIN) 120 CHARRANGE ToCHARRANGE() const; 121 #endif 122 // GTK+ has no concept of a range. 123 124 std::string ToString() const; 125 126 private: 127 // Note: we use uint32_t instead of size_t because this struct is sent over 128 // IPC which could span 32 & 64 bit processes. This is fine since text spans 129 // shouldn't exceed UINT32_MAX even on 64 bit builds. 130 uint32_t start_; 131 uint32_t end_; 132 }; 133 134 GFX_RANGE_EXPORT std::ostream& operator<<(std::ostream& os, const Range& range); 135 136 } // namespace gfx 137 138 #endif // UI_GFX_RANGE_RANGE_H_ 139