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 <neilh@scintilla.org>
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 "CharacterType.h"
14#include "CharClassify.h"
15
16using namespace Scintilla::Internal;
17
18CharClassify::CharClassify() : charClass{} {
19 SetDefaultCharClasses(true);
20}
21
22void CharClassify::SetDefaultCharClasses(bool includeWordClass) {
23 // Initialize all char classes to default values
24 for (int ch = 0; ch < maxChar; ch++) {
25 if (ch == '\r' || ch == '\n')
26 charClass[ch] = CharacterClass::newLine;
27 else if (IsControl(ch) || ch == ' ')
28 charClass[ch] = CharacterClass::space;
29 else if (includeWordClass && (ch >= 0x80 || IsAlphaNumeric(ch) || ch == '_'))
30 charClass[ch] = CharacterClass::word;
31 else
32 charClass[ch] = CharacterClass::punctuation;
33 }
34}
35
36void CharClassify::SetCharClasses(const unsigned char *chars, CharacterClass newCharClass) {
37 // Apply the newCharClass to the specified chars
38 if (chars) {
39 while (*chars) {
40 charClass[*chars] = newCharClass;
41 chars++;
42 }
43 }
44}
45
46int CharClassify::GetCharsOfClass(CharacterClass 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