1 | // Scintilla source code edit control |
2 | /** @file CaseFolder.cxx |
3 | ** Classes for case folding. |
4 | **/ |
5 | // Copyright 1998-2013 by Neil Hodgson <neilh@scintilla.org> |
6 | // The License.txt file describes the conditions under which this software may be distributed. |
7 | |
8 | #include <stdexcept> |
9 | #include <vector> |
10 | #include <algorithm> |
11 | |
12 | #include "CaseFolder.h" |
13 | #include "CaseConvert.h" |
14 | |
15 | using namespace Scintilla::Internal; |
16 | |
17 | CaseFolder::~CaseFolder() { |
18 | } |
19 | |
20 | CaseFolderTable::CaseFolderTable() noexcept : mapping{} { |
21 | for (size_t iChar=0; iChar<sizeof(mapping); iChar++) { |
22 | mapping[iChar] = static_cast<char>(iChar); |
23 | } |
24 | } |
25 | |
26 | size_t CaseFolderTable::Fold(char *folded, size_t sizeFolded, const char *mixed, size_t lenMixed) { |
27 | if (lenMixed > sizeFolded) { |
28 | return 0; |
29 | } else { |
30 | for (size_t i=0; i<lenMixed; i++) { |
31 | folded[i] = mapping[static_cast<unsigned char>(mixed[i])]; |
32 | } |
33 | return lenMixed; |
34 | } |
35 | } |
36 | |
37 | void CaseFolderTable::SetTranslation(char ch, char chTranslation) noexcept { |
38 | mapping[static_cast<unsigned char>(ch)] = chTranslation; |
39 | } |
40 | |
41 | void CaseFolderTable::StandardASCII() noexcept { |
42 | for (size_t iChar=0; iChar<sizeof(mapping); iChar++) { |
43 | if (iChar >= 'A' && iChar <= 'Z') { |
44 | mapping[iChar] = static_cast<char>(iChar - 'A' + 'a'); |
45 | } else { |
46 | mapping[iChar] = static_cast<char>(iChar); |
47 | } |
48 | } |
49 | } |
50 | |
51 | CaseFolderUnicode::CaseFolderUnicode() { |
52 | StandardASCII(); |
53 | converter = ConverterFor(CaseConversion::fold); |
54 | } |
55 | |
56 | size_t CaseFolderUnicode::Fold(char *folded, size_t sizeFolded, const char *mixed, size_t lenMixed) { |
57 | if ((lenMixed == 1) && (sizeFolded > 0)) { |
58 | folded[0] = mapping[static_cast<unsigned char>(mixed[0])]; |
59 | return 1; |
60 | } else { |
61 | return converter->CaseConvertString(folded, sizeFolded, mixed, lenMixed); |
62 | } |
63 | } |
64 | |