CaseConverter.cpp
1.36 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
/*
* File: CaseConverter.cpp
* Author: lennyn
*
* Created on November 22, 2013, 2:36 PM
*/
#include "CaseConverter.hpp"
#include "caseconv.hpp"
using namespace std;
map<uint32_t, uint32_t> initializeExtCaseMap(const uint32_t (*table)[2], unsigned int tableSize) {
map<uint32_t, uint32_t> res;
for (unsigned int i = 0; i < tableSize; i++) {
uint32_t key = table[i][0];
uint32_t value = table[i][1];
res[key] = value;
}
return res;
}
CaseConverter::CaseConverter()
: extToLowercaseMap(initializeExtCaseMap(EXT_TO_LOWERCASE_TABLE, EXT_TO_LOWERCASE_TABLE_SIZE)),
extToTitlecaseMap(initializeExtCaseMap(EXT_TO_TITLECASE_TABLE, EXT_TO_TITLECASE_TABLE_SIZE)) {
}
static uint32_t getFromTables(const uint32_t* table, unsigned int tableSize, const map<uint32_t, uint32_t>& extMap, uint32_t codepoint) {
if (codepoint < tableSize) {
return table[codepoint];
}
else if (extMap.count(codepoint) != 0) {
return extMap.at(codepoint);
}
else {
return codepoint;
}
}
uint32_t CaseConverter::toLower(uint32_t codepoint) const {
return getFromTables(TO_LOWERCASE_TABLE, TO_LOWERCASE_TABLE_SIZE, this->extToLowercaseMap, codepoint);
}
uint32_t CaseConverter::toTitle(uint32_t codepoint) const {
return getFromTables(TO_TITLECASE_TABLE, TO_TITLECASE_TABLE_SIZE, this->extToTitlecaseMap, codepoint);
}