|
1
2
3
4
5
6
7
8
|
/*
* File: CaseConverter.cpp
* Author: lennyn
*
* Created on November 22, 2013, 2:36 PM
*/
#include "CaseConverter.hpp"
|
|
9
|
#include "caseconv.hpp"
|
|
10
11
12
|
using namespace std;
|
|
13
|
map<uint32_t, uint32_t> initializeExtCaseMap(const uint32_t (*table)[2], unsigned int tableSize) {
|
|
14
|
map<uint32_t, uint32_t> res;
|
|
15
16
17
|
for (unsigned int i = 0; i < tableSize; i++) {
uint32_t key = table[i][0];
uint32_t value = table[i][1];
|
|
18
19
20
21
22
23
|
res[key] = value;
}
return res;
}
CaseConverter::CaseConverter()
|
|
24
25
|
: extToLowercaseMap(initializeExtCaseMap(EXT_TO_LOWERCASE_TABLE, EXT_TO_LOWERCASE_TABLE_SIZE)),
extToTitlecaseMap(initializeExtCaseMap(EXT_TO_TITLECASE_TABLE, EXT_TO_TITLECASE_TABLE_SIZE)) {
|
|
26
27
|
}
|
|
28
|
static uint32_t getFromTables(const uint32_t* table, unsigned int tableSize, const map<uint32_t, uint32_t>& extMap, const uint32_t codepoint) {
|
|
29
30
|
if (codepoint < tableSize) {
return table[codepoint];
|
|
31
|
}
|
|
32
|
else if (extMap.count(codepoint) != 0) {
|
|
33
34
35
|
map<uint32_t, uint32_t>::const_iterator it;
it = extMap.find(codepoint);
return it->second;
|
|
36
37
38
39
40
41
|
}
else {
return codepoint;
}
}
|
|
42
43
44
45
46
47
48
|
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);
}
|