diff options
Diffstat (limited to 'lexlib/CharacterSet.h')
-rw-r--r-- | lexlib/CharacterSet.h | 100 |
1 files changed, 95 insertions, 5 deletions
diff --git a/lexlib/CharacterSet.h b/lexlib/CharacterSet.h index 9b8869635..18cb0aa93 100644 --- a/lexlib/CharacterSet.h +++ b/lexlib/CharacterSet.h @@ -5,6 +5,13 @@ // Copyright 2007 by Neil Hodgson <neilh@scintilla.org> // The License.txt file describes the conditions under which this software may be distributed. +#ifndef CHARACTERSET_H +#define CHARACTERSET_H + +#ifdef SCI_NAMESPACE +namespace Scintilla { +#endif + class CharacterSet { int size; bool valueAfter; @@ -39,21 +46,104 @@ public: size = 0; } void Add(int val) { - PLATFORM_ASSERT(val >= 0); - PLATFORM_ASSERT(val < size); + assert(val >= 0); + assert(val < size); bset[val] = true; } void AddString(const char *CharacterSet) { for (const char *cp=CharacterSet; *cp; cp++) { int val = static_cast<unsigned char>(*cp); - PLATFORM_ASSERT(val >= 0); - PLATFORM_ASSERT(val < size); + assert(val >= 0); + assert(val < size); bset[val] = true; } } bool Contains(int val) const { - PLATFORM_ASSERT(val >= 0); + assert(val >= 0); if (val < 0) return false; return (val < size) ? bset[val] : valueAfter; } }; + +// Functions for classifying characters + +inline bool IsASpace(int ch) { + return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d)); +} + +inline bool IsASpaceOrTab(int ch) { + return (ch == ' ') || (ch == '\t'); +} + +inline bool IsADigit(int ch) { + return (ch >= '0') && (ch <= '9'); +} + +inline bool IsADigit(int ch, int base) { + if (base <= 10) { + return (ch >= '0') && (ch < '0' + base); + } else { + return ((ch >= '0') && (ch <= '9')) || + ((ch >= 'A') && (ch < 'A' + base - 10)) || + ((ch >= 'a') && (ch < 'a' + base - 10)); + } +} + +inline bool IsASCII(int ch) { + return ch < 0x80; +} + +inline bool IsAlphaNumeric(int ch) { + return + ((ch >= '0') && (ch <= '9')) || + ((ch >= 'a') && (ch <= 'z')) || + ((ch >= 'A') && (ch <= 'Z')); +} + +/** + * Check if a character is a space. + * This is ASCII specific but is safe with chars >= 0x80. + */ +inline bool isspacechar(int ch) { + return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d)); +} + +inline bool iswordchar(int ch) { + return IsASCII(ch) && (IsAlphaNumeric(ch) || ch == '.' || ch == '_'); +} + +inline bool iswordstart(int ch) { + return IsASCII(ch) && (IsAlphaNumeric(ch) || ch == '_'); +} + +inline bool isoperator(int ch) { + if (IsASCII(ch) && IsAlphaNumeric(ch)) + return false; + // '.' left out as it is used to make up numbers + if (ch == '%' || ch == '^' || ch == '&' || ch == '*' || + ch == '(' || ch == ')' || ch == '-' || ch == '+' || + ch == '=' || ch == '|' || ch == '{' || ch == '}' || + ch == '[' || ch == ']' || ch == ':' || ch == ';' || + ch == '<' || ch == '>' || ch == ',' || ch == '/' || + ch == '?' || ch == '!' || ch == '.' || ch == '~') + return true; + return false; +} + +// Simple case functions for ASCII. + +inline char MakeUpperCase(char ch) { + if (ch < 'a' || ch > 'z') + return ch; + else + return static_cast<char>(ch - 'a' + 'A'); +} + +int CompareCaseInsensitive(const char *a, const char *b); +int CompareNCaseInsensitive(const char *a, const char *b, size_t len); + +#ifdef SCI_NAMESPACE +} +#endif + +#endif |