Optimize %path% parser code
[TortoiseGit.git] / ext / scintilla / src / CharClassify.cxx
blobabe39f8ba1cbfc3081674f21f0637c1aef99e275
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.
8 #include <stdlib.h>
9 #include <ctype.h>
11 #include "CharClassify.h"
13 #ifdef SCI_NAMESPACE
14 using namespace Scintilla;
15 #endif
17 // Shut up annoying Visual C++ warnings:
18 #ifdef _MSC_VER
19 #pragma warning(disable: 4514)
20 #endif
22 CharClassify::CharClassify() {
23 SetDefaultCharClasses(true);
26 void CharClassify::SetDefaultCharClasses(bool includeWordClass) {
27 // Initialize all char classes to default values
28 for (int ch = 0; ch < 256; ch++) {
29 if (ch == '\r' || ch == '\n')
30 charClass[ch] = ccNewLine;
31 else if (ch < 0x20 || ch == ' ')
32 charClass[ch] = ccSpace;
33 else if (includeWordClass && (ch >= 0x80 || isalnum(ch) || ch == '_'))
34 charClass[ch] = ccWord;
35 else
36 charClass[ch] = ccPunctuation;
40 void CharClassify::SetCharClasses(const unsigned char *chars, cc newCharClass) {
41 // Apply the newCharClass to the specifed chars
42 if (chars) {
43 while (*chars) {
44 charClass[*chars] = static_cast<unsigned char>(newCharClass);
45 chars++;
50 int CharClassify::GetCharsOfClass(cc characterClass, unsigned char *buffer) {
51 // Get characters belonging to the given char class; return the number
52 // of characters (if the buffer is NULL, don't write to it).
53 int count = 0;
54 for (int ch = maxChar - 1; ch >= 0; --ch) {
55 if (charClass[ch] == characterClass) {
56 ++count;
57 if (buffer) {
58 *buffer = static_cast<unsigned char>(ch);
59 buffer++;
63 return count;