Blame view

morfeusz/charset/CaseConverter.cpp 1.44 KB
Michał Lenart authored
1
2
3
4
5
6
7
8
/* 
 * File:   CaseConverter.cpp
 * Author: lennyn
 * 
 * Created on November 22, 2013, 2:36 PM
 */

#include "CaseConverter.hpp"
Michał Lenart authored
9
#include "caseconv.hpp"
Michał Lenart authored
10
11
12

using namespace std;
Michał Lenart authored
13
map<uint32_t, uint32_t> initializeExtCaseMap(const uint32_t (*table)[2], unsigned int tableSize) {
Michał Lenart authored
14
    map<uint32_t, uint32_t> res;
Michał Lenart authored
15
16
17
    for (unsigned int i = 0; i < tableSize; i++) {
        uint32_t key = table[i][0];
        uint32_t value = table[i][1];
Michał Lenart authored
18
19
20
21
22
23
        res[key] = value;
    }
    return res;
}

CaseConverter::CaseConverter()
Michał Lenart authored
24
25
: extToLowercaseMap(initializeExtCaseMap(EXT_TO_LOWERCASE_TABLE, EXT_TO_LOWERCASE_TABLE_SIZE)),
extToTitlecaseMap(initializeExtCaseMap(EXT_TO_TITLECASE_TABLE, EXT_TO_TITLECASE_TABLE_SIZE)) {
Michał Lenart authored
26
27
}
Michał Lenart authored
28
static uint32_t getFromTables(const uint32_t* table, unsigned int tableSize, const map<uint32_t, uint32_t>& extMap, const uint32_t codepoint) {
Michał Lenart authored
29
30
    if (codepoint < tableSize) {
        return table[codepoint];
Michał Lenart authored
31
    }
Michał Lenart authored
32
    else if (extMap.count(codepoint) != 0) {
Michał Lenart authored
33
34
35
        map<uint32_t, uint32_t>::const_iterator it;
        it = extMap.find(codepoint);
        return it->second;
Michał Lenart authored
36
37
38
39
40
41
    }
    else {
        return codepoint;
    }
}
Michał Lenart authored
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);
}