1 // Scintilla source code edit control 2 /** @file CharClassify.cxx 3 ** Character classifications used by Document and RESearch. 4 **/ 5 // Copyright 2006 by Neil Hodgson <[email protected]> 6 // The License.txt file describes the conditions under which this software may be distributed. 7 8 #include <cstdlib> 9 #include <cassert> 10 11 #include <stdexcept> 12 13 #include "CharacterSet.h" 14 #include "CharClassify.h" 15 16 using namespace Scintilla; 17 18 CharClassify::CharClassify() : charClass{} { 19 SetDefaultCharClasses(true); 20 } 21 22 void CharClassify::SetDefaultCharClasses(bool includeWordClass) { 23 // Initialize all char classes to default values 24 for (int ch = 0; ch < 256; ch++) { 25 if (ch == '\r' || ch == '\n') 26 charClass[ch] = ccNewLine; 27 else if (ch < 0x20 || ch == ' ') 28 charClass[ch] = ccSpace; 29 else if (includeWordClass && (ch >= 0x80 || IsAlphaNumeric(ch) || ch == '_')) 30 charClass[ch] = ccWord; 31 else 32 charClass[ch] = ccPunctuation; 33 } 34 } 35 36 void CharClassify::SetCharClasses(const unsigned char *chars, cc newCharClass) { 37 // Apply the newCharClass to the specified chars 38 if (chars) { 39 while (*chars) { 40 charClass[*chars] = static_cast<unsigned char>(newCharClass); 41 chars++; 42 } 43 } 44 } 45 46 int CharClassify::GetCharsOfClass(cc characterClass, unsigned char *buffer) const noexcept { 47 // Get characters belonging to the given char class; return the number 48 // of characters (if the buffer is NULL, don't write to it). 49 int count = 0; 50 for (int ch = maxChar - 1; ch >= 0; --ch) { 51 if (charClass[ch] == characterClass) { 52 ++count; 53 if (buffer) { 54 *buffer = static_cast<unsigned char>(ch); 55 buffer++; 56 } 57 } 58 } 59 return count; 60 } 61