summaryrefslogtreecommitdiff
path: root/libs/litehtml/src/num_cvt.cpp
blob: b1d0f3b75e014734e8e7570ca4be775030b60774 (plain)
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
#include "num_cvt.h"
#include "utf8_strings.h"
#include <vector>

static std::vector<char> latin_lower = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
static std::vector<char> latin_upper = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
static std::vector<std::u32string> greek_lower = { U"α", U"β", U"γ", U"δ", U"ε", U"ζ", U"η", U"θ", U"ι", U"κ", U"λ", U"μ", U"ν", U"ξ", U"ο", U"π", U"ρ", U"σ", U"τ", U"υ", U"φ", U"χ", U"ψ", U"ω" };

static litehtml::string to_mapped_alpha(int num, const std::vector<char>& map)
{
	int dividend = num;
	litehtml::string 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::string to_mapped_alpha(int num, const std::vector<std::u32string>& map)
{
	int dividend = num;
	litehtml::string out;
	int modulo;

	while (dividend > 0)
	{
		modulo = (dividend - 1) % map.size();
		out = litehtml_from_utf32(map[modulo]).c_str() + out;
		dividend = (int)((dividend - modulo) / map.size());
	}

	return out;
}

litehtml::string litehtml::num_cvt::to_latin_lower(int val)
{
	return to_mapped_alpha(val, latin_lower);
}

litehtml::string litehtml::num_cvt::to_latin_upper(int val)
{
	return to_mapped_alpha(val, latin_upper);
}

litehtml::string litehtml::num_cvt::to_greek_lower(int val)
{
	return to_mapped_alpha(val, greek_lower);
}

litehtml::string litehtml::num_cvt::to_roman_lower(int value)
{
	struct romandata_t { int value; const char* numeral; };
	const struct romandata_t romandata[] =
	{
		{ 1000, "m" }, { 900, "cm" },
		{ 500, "d" }, { 400, "cd" },
		{ 100, "c" }, { 90, "xc" },
		{ 50, "l" }, { 40, "xl" },
		{ 10, "x" }, { 9, "ix" },
		{ 5, "v" }, { 4, "iv" },
		{ 1, "i" },
		{ 0, nullptr } // end marker
	};

	litehtml::string result;
	for (const romandata_t* current = romandata; current->value > 0; ++current)
	{
		while (value >= current->value)
		{
			result += current->numeral;
			value -= current->value;
		}
	}
	return result;
}

litehtml::string litehtml::num_cvt::to_roman_upper(int value)
{
	struct romandata_t { int value; const char* numeral; };
	const struct romandata_t romandata[] =
	{
		{ 1000, "M" }, { 900, "CM" },
		{ 500, "D" }, { 400, "CD" },
		{ 100, "C" }, { 90, "XC" },
		{ 50, "L" }, { 40, "XL" },
		{ 10, "X" }, { 9, "IX" },
		{ 5, "V" }, { 4, "IV" },
		{ 1, "I" },
		{ 0, nullptr } // end marker
	};

	litehtml::string result;
	for (const romandata_t* current = romandata; current->value > 0; ++current)
	{
		while (value >= current->value)
		{
			result += current->numeral;
			value -= current->value;
		}
	}
	return result;
}