-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheet_encoding.h
More file actions
47 lines (41 loc) · 1.19 KB
/
Copy pathsheet_encoding.h
File metadata and controls
47 lines (41 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef CPP_ALGORITHM_SHEET_ENCODING_H
#define CPP_ALGORITHM_SHEET_ENCODING_H
#include <string>
namespace SheetEncoding
{
/**
* \brief Convert a column title to a corresponding column number.
* \param column column title
* \return number
*/
int DecodingSheetColumnId(const std::string& column);
/**
* \brief Convert a column number to a corresponding column title.
* \param column_id column number
* \return column title
*/
std::string EncodingSheetColumnId(int column_id);
}
// ----------------------------------------------------------------------------
inline int SheetEncoding::DecodingSheetColumnId(const std::string& column)
{
int result = 0;
for (const char c : column)
{
result = (result * 26) + (c - 'A' + 1);
}
return result;
}
// ----------------------------------------------------------------------------
inline std::string SheetEncoding::EncodingSheetColumnId(int column_id)
{
std::string result;
while (column_id > 0)
{
result.push_back('A' + (column_id - 1) % 26);
column_id = (column_id - 1) / 26;
}
std::reverse(result.begin(), result.end());
return result;
}
#endif