1 // Copyright 2016 PDFium 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef CORE_FPDFDOC_CPVT_WORDRANGE_H_ 8 #define CORE_FPDFDOC_CPVT_WORDRANGE_H_ 9 10 #include <algorithm> 11 #include <utility> 12 13 #include "core/fpdfdoc/cpvt_wordplace.h" 14 #include "core/fxcrt/fx_system.h" 15 16 struct CPVT_WordRange { CPVT_WordRangeCPVT_WordRange17 CPVT_WordRange() {} 18 CPVT_WordRangeCPVT_WordRange19 CPVT_WordRange(const CPVT_WordPlace& begin, const CPVT_WordPlace& end) 20 : BeginPos(begin), EndPos(end) { 21 Normalize(); 22 } 23 ResetCPVT_WordRange24 void Reset() { 25 BeginPos.Reset(); 26 EndPos.Reset(); 27 } 28 SetCPVT_WordRange29 void Set(const CPVT_WordPlace& begin, const CPVT_WordPlace& end) { 30 BeginPos = begin; 31 EndPos = end; 32 Normalize(); 33 } 34 SetBeginPosCPVT_WordRange35 void SetBeginPos(const CPVT_WordPlace& begin) { 36 BeginPos = begin; 37 Normalize(); 38 } 39 SetEndPosCPVT_WordRange40 void SetEndPos(const CPVT_WordPlace& end) { 41 EndPos = end; 42 Normalize(); 43 } 44 IntersectCPVT_WordRange45 CPVT_WordRange Intersect(const CPVT_WordRange& that) const { 46 if (that.EndPos < BeginPos || that.BeginPos > EndPos || 47 EndPos < that.BeginPos || BeginPos > that.EndPos) { 48 return CPVT_WordRange(); 49 } 50 51 return CPVT_WordRange(std::max(BeginPos, that.BeginPos), 52 std::min(EndPos, that.EndPos)); 53 } 54 IsEmptyCPVT_WordRange55 inline bool IsEmpty() const { return BeginPos == EndPos; } 56 inline bool operator==(const CPVT_WordRange& wr) const { 57 return wr.BeginPos == BeginPos && wr.EndPos == EndPos; 58 } 59 inline bool operator!=(const CPVT_WordRange& wr) const { 60 return !(*this == wr); 61 } 62 NormalizeCPVT_WordRange63 void Normalize() { 64 if (BeginPos > EndPos) 65 std::swap(BeginPos, EndPos); 66 } 67 68 CPVT_WordPlace BeginPos; 69 CPVT_WordPlace EndPos; 70 }; 71 72 #endif // CORE_FPDFDOC_CPVT_WORDRANGE_H_ 73