2017-01-03 09:35:18 +08:00
|
|
|
// Copyright (c) 2014-2017 Thomas Fussell
|
2015-12-25 04:51:11 +08:00
|
|
|
// Copyright (c) 2010-2015 openpyxl
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE
|
|
|
|
//
|
|
|
|
// @license: http://www.opensource.org/licenses/mit-license.php
|
|
|
|
// @author: see AUTHORS file
|
2015-11-04 03:53:48 +08:00
|
|
|
#include <locale>
|
|
|
|
|
2015-11-21 09:41:32 +08:00
|
|
|
#include <xlnt/cell/index_types.hpp>
|
2015-11-04 03:53:48 +08:00
|
|
|
#include <xlnt/utils/exceptions.hpp>
|
2016-12-24 23:04:57 +08:00
|
|
|
#include <detail/constants.hpp>
|
2015-11-04 03:53:48 +08:00
|
|
|
|
|
|
|
namespace xlnt {
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
column_t::index_t column_t::column_index_from_string(const std::string &column_string)
|
2015-11-04 03:53:48 +08:00
|
|
|
{
|
|
|
|
if (column_string.length() > 3 || column_string.empty())
|
|
|
|
{
|
2017-03-31 11:52:57 +08:00
|
|
|
throw invalid_column_index();
|
2015-11-04 03:53:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
column_t::index_t column_index = 0;
|
|
|
|
int place = 1;
|
|
|
|
|
|
|
|
for (int i = static_cast<int>(column_string.length()) - 1; i >= 0; i--)
|
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
if (!std::isalpha(column_string[static_cast<std::size_t>(i)], std::locale::classic()))
|
2015-11-04 03:53:48 +08:00
|
|
|
{
|
2017-03-31 11:52:57 +08:00
|
|
|
throw invalid_column_index();
|
2015-11-04 03:53:48 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
auto char_index = std::toupper(column_string[static_cast<std::size_t>(i)], std::locale::classic()) - 'A';
|
2015-11-04 03:53:48 +08:00
|
|
|
|
|
|
|
column_index += static_cast<column_t::index_t>((char_index + 1) * place);
|
|
|
|
place *= 26;
|
|
|
|
}
|
|
|
|
|
|
|
|
return column_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a column number into a column letter (3 -> 'C')
|
|
|
|
// Right shift the column col_idx by 26 to find column letters in reverse
|
|
|
|
// order.These numbers are 1 - based, and can be converted to ASCII
|
|
|
|
// ordinals by adding 64.
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string column_t::column_string_from_index(column_t::index_t column_index)
|
2015-11-04 03:53:48 +08:00
|
|
|
{
|
|
|
|
// these indicies corrospond to A->ZZZ and include all allowed
|
|
|
|
// columns
|
2016-07-23 08:26:02 +08:00
|
|
|
if (column_index < constants::min_column() || column_index > constants::max_column())
|
2015-11-04 03:53:48 +08:00
|
|
|
{
|
2017-03-31 11:52:57 +08:00
|
|
|
throw invalid_column_index();
|
2015-11-04 03:53:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int temp = static_cast<int>(column_index);
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string column_letter = "";
|
2015-11-04 03:53:48 +08:00
|
|
|
|
|
|
|
while (temp > 0)
|
|
|
|
{
|
|
|
|
int quotient = temp / 26, remainder = temp % 26;
|
|
|
|
|
|
|
|
// check for exact division and borrow if needed
|
|
|
|
if (remainder == 0)
|
|
|
|
{
|
|
|
|
quotient -= 1;
|
|
|
|
remainder = 26;
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
column_letter = std::string(1, char(remainder + 64)) + column_letter;
|
2015-11-04 03:53:48 +08:00
|
|
|
temp = quotient;
|
|
|
|
}
|
|
|
|
|
|
|
|
return column_letter;
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t::column_t()
|
|
|
|
: index(1)
|
|
|
|
{
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t::column_t(index_t column_index)
|
|
|
|
: index(column_index)
|
|
|
|
{
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t::column_t(const std::string &column_string)
|
|
|
|
: index(column_index_from_string(column_string))
|
|
|
|
{
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t::column_t(const char *column_string)
|
|
|
|
: column_t(std::string(column_string))
|
|
|
|
{
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t::column_t(const column_t &other)
|
|
|
|
: column_t(other.index)
|
|
|
|
{
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t::column_t(column_t &&other)
|
|
|
|
{
|
|
|
|
swap(*this, other);
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
std::string column_t::column_string() const
|
|
|
|
{
|
|
|
|
return column_string_from_index(index);
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t &column_t::operator=(column_t rhs)
|
|
|
|
{
|
|
|
|
swap(*this, rhs);
|
|
|
|
return *this;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t &column_t::operator=(const std::string &rhs)
|
|
|
|
{
|
|
|
|
return *this = column_t(rhs);
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t &column_t::operator=(const char *rhs)
|
|
|
|
{
|
|
|
|
return *this = column_t(rhs);
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator==(const column_t &other) const
|
|
|
|
{
|
|
|
|
return index == other.index;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator!=(const column_t &other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator==(int other) const
|
|
|
|
{
|
|
|
|
return *this == column_t(static_cast<index_t>(other));
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator==(index_t other) const
|
|
|
|
{
|
|
|
|
return *this == column_t(other);
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator==(const std::string &other) const
|
|
|
|
{
|
|
|
|
return *this == column_t(other);
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator==(const char *other) const
|
|
|
|
{
|
|
|
|
return *this == column_t(other);
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator!=(int other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator!=(index_t other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator!=(const std::string &other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator!=(const char *other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator>(const column_t &other) const
|
|
|
|
{
|
|
|
|
return index > other.index;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator>=(const column_t &other) const
|
|
|
|
{
|
|
|
|
return index >= other.index;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator<(const column_t &other) const
|
|
|
|
{
|
|
|
|
return index < other.index;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator<=(const column_t &other) const
|
|
|
|
{
|
|
|
|
return index <= other.index;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator>(const column_t::index_t &other) const
|
|
|
|
{
|
|
|
|
return index > other;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator>=(const column_t::index_t &other) const
|
|
|
|
{
|
|
|
|
return index >= other;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator<(const column_t::index_t &other) const
|
|
|
|
{
|
|
|
|
return index < other;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool column_t::operator<=(const column_t::index_t &other) const
|
|
|
|
{
|
|
|
|
return index <= other;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t &column_t::operator++()
|
|
|
|
{
|
|
|
|
index++;
|
|
|
|
return *this;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t &column_t::operator--()
|
|
|
|
{
|
|
|
|
index--;
|
|
|
|
return *this;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t column_t::operator++(int)
|
|
|
|
{
|
|
|
|
column_t copy(index);
|
|
|
|
++(*this);
|
|
|
|
return copy;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t column_t::operator--(int)
|
|
|
|
{
|
|
|
|
column_t copy(index);
|
|
|
|
--(*this);
|
|
|
|
return copy;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t operator+(column_t lhs, const column_t &rhs)
|
|
|
|
{
|
|
|
|
lhs += rhs;
|
|
|
|
return lhs;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t operator-(column_t lhs, const column_t &rhs)
|
|
|
|
{
|
|
|
|
lhs -= rhs;
|
|
|
|
return lhs;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t &column_t::operator+=(const column_t &rhs)
|
|
|
|
{
|
|
|
|
index += rhs.index;
|
|
|
|
return *this;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
column_t &column_t::operator-=(const column_t &rhs)
|
|
|
|
{
|
|
|
|
index -= rhs.index;
|
|
|
|
return *this;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool operator>(const column_t::index_t &left, const column_t &right)
|
|
|
|
{
|
|
|
|
return column_t(left) > right;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool operator>=(const column_t::index_t &left, const column_t &right)
|
|
|
|
{
|
|
|
|
return column_t(left) >= right;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool operator<(const column_t::index_t &left, const column_t &right)
|
|
|
|
{
|
|
|
|
return column_t(left) < right;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
bool operator<=(const column_t::index_t &left, const column_t &right)
|
|
|
|
{
|
|
|
|
return column_t(left) <= right;
|
|
|
|
}
|
2015-11-20 11:54:54 +08:00
|
|
|
|
|
|
|
void swap(column_t &left, column_t &right)
|
|
|
|
{
|
|
|
|
using std::swap;
|
|
|
|
swap(left.index, right.index);
|
|
|
|
}
|
|
|
|
|
2015-11-04 03:53:48 +08:00
|
|
|
std::size_t column_hash::operator()(const column_t &k) const
|
|
|
|
{
|
|
|
|
static std::hash<column_t::index_t> hasher;
|
|
|
|
return hasher(k.index);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace xlnt
|