| 1 | // Scintilla source code edit control |
| 2 | /** @file LexAccessor.cxx |
| 3 | ** Interfaces between Scintilla and lexers. |
| 4 | **/ |
| 5 | // Copyright 1998-2010 by Neil Hodgson <neilh@scintilla.org> |
| 6 | // The License.txt file describes the conditions under which this software may be distributed. |
| 7 | #include <cassert> |
| 8 | #include <cstring> |
| 9 | |
| 10 | #include <string> |
| 11 | #include <algorithm> |
| 12 | |
| 13 | #include "ILexer.h" |
| 14 | |
| 15 | #include "LexAccessor.h" |
| 16 | #include "CharacterSet.h" |
| 17 | |
| 18 | using namespace Lexilla; |
| 19 | |
| 20 | namespace Lexilla { |
| 21 | |
| 22 | bool LexAccessor::MatchIgnoreCase(Sci_Position pos, const char *s) { |
| 23 | for (; *s; s++, pos++) { |
| 24 | if (*s != MakeLowerCase(SafeGetCharAt(pos))) { |
| 25 | return false; |
| 26 | } |
| 27 | } |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | void LexAccessor::GetRange(Sci_PositionU startPos_, Sci_PositionU endPos_, char *s, Sci_PositionU len) { |
| 32 | assert(startPos_ <= endPos_ && len != 0 && s != nullptr); |
| 33 | endPos_ = std::min(endPos_, startPos_ + len - 1); |
| 34 | len = endPos_ - startPos_; |
| 35 | if (startPos_ >= static_cast<Sci_PositionU>(startPos) && endPos_ <= static_cast<Sci_PositionU>(endPos)) { |
| 36 | const char * const p = buf + (startPos_ - startPos); |
| 37 | memcpy(s, p, len); |
| 38 | } else { |
| 39 | pAccess->GetCharRange(s, startPos_, len); |
| 40 | } |
| 41 | s[len] = '\0'; |
| 42 | } |
| 43 | |
| 44 | void LexAccessor::GetRangeLowered(Sci_PositionU startPos_, Sci_PositionU endPos_, char *s, Sci_PositionU len) { |
| 45 | GetRange(startPos_, endPos_, s, len); |
| 46 | while (*s) { |
| 47 | if (*s >= 'A' && *s <= 'Z') { |
| 48 | *s += 'a' - 'A'; |
| 49 | } |
| 50 | ++s; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | std::string LexAccessor::GetRange(Sci_PositionU startPos_, Sci_PositionU endPos_) { |
| 55 | assert(startPos_ < endPos_); |
| 56 | const Sci_PositionU len = endPos_ - startPos_; |
| 57 | std::string s(len, '\0'); |
| 58 | GetRange(startPos_, endPos_, s.data(), len); |
| 59 | return s; |
| 60 | } |
| 61 | |
| 62 | std::string LexAccessor::GetRangeLowered(Sci_PositionU startPos_, Sci_PositionU endPos_) { |
| 63 | assert(startPos_ < endPos_); |
| 64 | const Sci_PositionU len = endPos_ - startPos_; |
| 65 | std::string s(len, '\0'); |
| 66 | GetRangeLowered(startPos_, endPos_, s.data(), len); |
| 67 | return s; |
| 68 | } |
| 69 | |
| 70 | } |
| 71 | |