1 // Scintilla source code edit control 2 /** @file CaseFolder.cxx 3 ** Classes for case folding. 4 **/ 5 // Copyright 1998-2013 by Neil Hodgson <[email protected]> 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; 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 CaseFolderTable::~CaseFolderTable() { 27 } 28 29 size_t CaseFolderTable::Fold(char *folded, size_t sizeFolded, const char *mixed, size_t lenMixed) { 30 if (lenMixed > sizeFolded) { 31 return 0; 32 } else { 33 for (size_t i=0; i<lenMixed; i++) { 34 folded[i] = mapping[static_cast<unsigned char>(mixed[i])]; 35 } 36 return lenMixed; 37 } 38 } 39 40 void CaseFolderTable::SetTranslation(char ch, char chTranslation) noexcept { 41 mapping[static_cast<unsigned char>(ch)] = chTranslation; 42 } 43 44 void CaseFolderTable::StandardASCII() noexcept { 45 for (size_t iChar=0; iChar<sizeof(mapping); iChar++) { 46 if (iChar >= 'A' && iChar <= 'Z') { 47 mapping[iChar] = static_cast<char>(iChar - 'A' + 'a'); 48 } else { 49 mapping[iChar] = static_cast<char>(iChar); 50 } 51 } 52 } 53 54 CaseFolderUnicode::CaseFolderUnicode() { 55 StandardASCII(); 56 converter = ConverterFor(CaseConversionFold); 57 } 58 59 size_t CaseFolderUnicode::Fold(char *folded, size_t sizeFolded, const char *mixed, size_t lenMixed) { 60 if ((lenMixed == 1) && (sizeFolded > 0)) { 61 folded[0] = mapping[static_cast<unsigned char>(mixed[0])]; 62 return 1; 63 } else { 64 return converter->CaseConvertString(folded, sizeFolded, mixed, lenMixed); 65 } 66 } 67