-
Notifications
You must be signed in to change notification settings - Fork 1
/
num_cvt.cpp
109 lines (95 loc) · 3.18 KB
/
num_cvt.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "litehtml/num_cvt.h"
#include "litehtml/types.h"
#include "litehtml/utf8_strings.h"
#include <vector>
static std::vector<litehtml::tchar_t> latin_lower = { _t('a'), _t('b'), _t('c'), _t('d'), _t('e'), _t('f'), _t('g'), _t('h'), _t('i'), _t('j'), _t('k'), _t('l'), _t('m'), _t('n'), _t('o'), _t('p'), _t('q'), _t('r'), _t('s'), _t('t'), _t('u'), _t('v'), _t('w'), _t('x'), _t('y'), _t('z') };
static std::vector<litehtml::tchar_t> latin_upper = { _t('A'), _t('B'), _t('C'), _t('D'), _t('E'), _t('F'), _t('G'), _t('H'), _t('I'), _t('J'), _t('K'), _t('L'), _t('M'), _t('N'), _t('O'), _t('P'), _t('Q'), _t('R'), _t('S'), _t('T'), _t('U'), _t('V'), _t('W'), _t('X'), _t('Y'), _t('Z') };
static std::vector<std::wstring> greek_lower = { L"α", L"β", L"γ", L"δ", L"ε", L"ζ", L"η", L"θ", L"ι", L"κ", L"λ", L"μ", L"ν", L"ξ", L"ο", L"π", L"ρ", L"σ", L"τ", L"υ", L"φ", L"χ", L"ψ", L"ω" };
static litehtml::tstring to_mapped_alpha(int num, const std::vector<litehtml::tchar_t>& map)
{
int dividend = num;
litehtml::tstring out;
int modulo;
while (dividend > 0)
{
modulo = (dividend - 1) % map.size();
out = map[modulo] + out;
dividend = (int)((dividend - modulo) / map.size());
}
return out;
}
static litehtml::tstring to_mapped_alpha(int num, const std::vector<std::wstring>& map)
{
int dividend = num;
litehtml::tstring out;
int modulo;
while (dividend > 0)
{
modulo = (dividend - 1) % map.size();
out = litehtml_from_wchar(map[modulo]).c_str() + out;
dividend = (int)((dividend - modulo) / map.size());
}
return out;
}
litehtml::tstring litehtml::num_cvt::to_latin_lower(int val)
{
return to_mapped_alpha(val, latin_lower);
}
litehtml::tstring litehtml::num_cvt::to_latin_upper(int val)
{
return to_mapped_alpha(val, latin_upper);
}
litehtml::tstring litehtml::num_cvt::to_greek_lower(int val)
{
return to_mapped_alpha(val, greek_lower);
}
litehtml::tstring litehtml::num_cvt::to_roman_lower(int value)
{
struct romandata_t { int value; litehtml::tchar_t* numeral; };
const struct romandata_t romandata[] =
{
{ 1000, _t("m") }, { 900, _t("cm" )},
{ 500, _t("d") }, { 400, _t("cd") },
{ 100, _t("c") }, { 90, _t("xc") },
{ 50, _t("l") }, { 40, _t("xl") },
{ 10, _t("x") }, { 9, _t("ix") },
{ 5, _t("v") }, { 4, _t("iv") },
{ 1, _t("i") },
{ 0, NULL } // end marker
};
litehtml::tstring result;
for (const romandata_t* current = romandata; current->value > 0; ++current)
{
while (value >= current->value)
{
result += current->numeral;
value -= current->value;
}
}
return result;
}
litehtml::tstring litehtml::num_cvt::to_roman_upper(int value)
{
struct romandata_t { int value; litehtml::tchar_t* numeral; };
const struct romandata_t romandata[] =
{
{ 1000, _t("M") }, { 900, _t("CM") },
{ 500, _t("D") }, { 400, _t("CD") },
{ 100, _t("C") }, { 90, _t("XC") },
{ 50, _t("L") }, { 40, _t("XL") },
{ 10, _t("X") }, { 9, _t("IX") },
{ 5, _t("V") }, { 4, _t("IV") },
{ 1, _t("I") },
{ 0, NULL } // end marker
};
litehtml::tstring result;
for (const romandata_t* current = romandata; current->value > 0; ++current)
{
while (value >= current->value)
{
result += current->numeral;
value -= current->value;
}
}
return result;
}