range_reference.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <locale>
  24. #include <xlnt/worksheet/range_reference.hpp>
  25. namespace xlnt {
  26. range_reference range_reference::make_absolute(const xlnt::range_reference &relative)
  27. {
  28. range_reference copy = relative;
  29. copy.top_left_.make_absolute(true, true);
  30. copy.bottom_right_.make_absolute(true, true);
  31. return copy;
  32. }
  33. range_reference::range_reference()
  34. : range_reference("A1")
  35. {
  36. }
  37. range_reference::range_reference(const char *range_string)
  38. : range_reference(std::string(range_string))
  39. {
  40. }
  41. range_reference::range_reference(const std::string &range_string)
  42. : top_left_("A1"), bottom_right_("A1")
  43. {
  44. auto colon_index = range_string.find(' ');
  45. if (colon_index != std::string::npos)
  46. {
  47. // Multiple cell selection is not supported at this time.
  48. return;
  49. }
  50. colon_index = range_string.find(':');
  51. if (colon_index != std::string::npos)
  52. {
  53. top_left_ = cell_reference(range_string.substr(0, colon_index));
  54. bottom_right_ = cell_reference(range_string.substr(colon_index + 1));
  55. }
  56. else
  57. {
  58. top_left_ = cell_reference(range_string);
  59. bottom_right_ = cell_reference(range_string);
  60. }
  61. }
  62. range_reference::range_reference(const cell_reference &top_left,
  63. const cell_reference &bottom_right)
  64. : top_left_(top_left),
  65. bottom_right_(bottom_right)
  66. {
  67. }
  68. range_reference::range_reference(column_t column_index_start,
  69. row_t row_index_start,
  70. column_t column_index_end,
  71. row_t row_index_end)
  72. : top_left_(column_index_start, row_index_start),
  73. bottom_right_(column_index_end, row_index_end)
  74. {
  75. }
  76. range_reference range_reference::make_offset(int column_offset, int row_offset) const
  77. {
  78. auto top_left = top_left_.make_offset(column_offset, row_offset);
  79. auto bottom_right = bottom_right_.make_offset(column_offset, row_offset);
  80. return top_left, bottom_right; // lol
  81. }
  82. std::size_t range_reference::height() const
  83. {
  84. return 1 + bottom_right_.row() - top_left_.row();
  85. }
  86. std::size_t range_reference::width() const
  87. {
  88. return 1 + (bottom_right_.column() - top_left_.column()).index;
  89. }
  90. bool range_reference::is_single_cell() const
  91. {
  92. return width() == 1 && height() == 1;
  93. }
  94. std::string range_reference::to_string() const
  95. {
  96. return top_left_.to_string() + ":" + bottom_right_.to_string();
  97. }
  98. bool range_reference::operator==(const range_reference &comparand) const
  99. {
  100. return comparand.top_left_ == top_left_ && comparand.bottom_right_ == bottom_right_;
  101. }
  102. bool range_reference::operator!=(const range_reference &comparand) const
  103. {
  104. return comparand.top_left_ != top_left_ || comparand.bottom_right_ != bottom_right_;
  105. }
  106. cell_reference range_reference::top_left() const
  107. {
  108. return top_left_;
  109. }
  110. cell_reference range_reference::top_right() const
  111. {
  112. return cell_reference(bottom_right_.column(), top_left_.row());
  113. }
  114. cell_reference range_reference::bottom_left() const
  115. {
  116. return cell_reference(top_left_.column(), bottom_right_.row());
  117. }
  118. cell_reference range_reference::bottom_right() const
  119. {
  120. return bottom_right_;
  121. }
  122. bool range_reference::contains(const cell_reference &ref) const
  123. {
  124. return top_left_.column_index() <= ref.column_index()
  125. && bottom_right_.column_index() >= ref.column_index()
  126. && top_left_.row() <= ref.row()
  127. && bottom_right_.row() >= ref.row();
  128. }
  129. bool range_reference::operator==(const std::string &reference_string) const
  130. {
  131. return *this == range_reference(reference_string);
  132. }
  133. bool range_reference::operator==(const char *reference_string) const
  134. {
  135. return *this == std::string(reference_string);
  136. }
  137. bool range_reference::operator!=(const std::string &reference_string) const
  138. {
  139. return *this != range_reference(reference_string);
  140. }
  141. bool range_reference::operator!=(const char *reference_string) const
  142. {
  143. return *this != std::string(reference_string);
  144. }
  145. XLNT_API bool operator==(const std::string &reference_string, const range_reference &ref)
  146. {
  147. return ref == reference_string;
  148. }
  149. XLNT_API bool operator==(const char *reference_string, const range_reference &ref)
  150. {
  151. return ref == reference_string;
  152. }
  153. XLNT_API bool operator!=(const std::string &reference_string, const range_reference &ref)
  154. {
  155. return ref != reference_string;
  156. }
  157. XLNT_API bool operator!=(const char *reference_string, const range_reference &ref)
  158. {
  159. return ref != reference_string;
  160. }
  161. } // namespace xlnt