diff options
author | Neil <nyamatongwe@gmail.com> | 2013-06-29 20:32:52 +1000 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2013-06-29 20:32:52 +1000 |
commit | d9f1a1b042f9458f2d4a905f20b9547c4ac2be70 (patch) | |
tree | 4db1263c22fcca231b754d8de86f1f5bb484fb87 /src | |
parent | 05cdbbd2ec9de91787c9306798669532c8779248 (diff) | |
download | scintilla-mirror-d9f1a1b042f9458f2d4a905f20b9547c4ac2be70.tar.gz |
Bug: [#1483]. Split GetRelativePosition into 2 calls one for moving between character
positions and the other for retrieving a character and width.
Diffstat (limited to 'src')
-rw-r--r-- | src/Document.cxx | 54 | ||||
-rw-r--r-- | src/Document.h | 3 |
2 files changed, 33 insertions, 24 deletions
diff --git a/src/Document.cxx b/src/Document.cxx index 472567068..a00fc9fc2 100644 --- a/src/Document.cxx +++ b/src/Document.cxx @@ -713,55 +713,63 @@ static inline int UnicodeFromBytes(const unsigned char *us) { } // Return -1 on out-of-bounds -int SCI_METHOD Document::GetRelativePosition(int start, int characterOffset, int *character, int *width) const { - int pos = start; +int SCI_METHOD Document::GetRelativePosition(int positionStart, int characterOffset) const { + int pos = positionStart; if (dbcsCodePage) { const int increment = (characterOffset > 0) ? 1 : -1; while (characterOffset != 0) { const int posNext = NextPosition(pos, increment); if (posNext == pos) - return -1; + return INVALID_POSITION; pos = posNext; characterOffset -= increment; } - const unsigned char leadByte = static_cast<unsigned char>(cb.CharAt(pos)); + } else { + pos = positionStart + characterOffset; + if ((pos < 0) || (pos > Length())) + return INVALID_POSITION; + } + return pos; +} + +int SCI_METHOD Document::GetCharacterAndWidth(int position, int *pWidth) const { + int character; + int bytesInCharacter = 1; + if (dbcsCodePage) { + const unsigned char leadByte = static_cast<unsigned char>(cb.CharAt(position)); if (SC_CP_UTF8 == dbcsCodePage) { if (UTF8IsAscii(leadByte)) { // Single byte character or invalid - *character = leadByte; - *width = 1; + character = leadByte; } else { const int widthCharBytes = UTF8BytesOfLead[leadByte]; unsigned char charBytes[UTF8MaxBytes] = {leadByte,0,0,0}; for (int b=1; b<widthCharBytes; b++) - charBytes[b] = static_cast<unsigned char>(cb.CharAt(pos+b)); + charBytes[b] = static_cast<unsigned char>(cb.CharAt(position+b)); int utf8status = UTF8Classify(charBytes, widthCharBytes); if (utf8status & UTF8MaskInvalid) { - // Report as singleton surrogate values which are invalid in Unicode - *character = 0xDC80 + leadByte; - *width = 1; + // Report as singleton surrogate values which are invalid Unicode + character = 0xDC80 + leadByte; } else { - *character = UnicodeFromBytes(charBytes); - *width = utf8status & UTF8MaskWidth; + bytesInCharacter = utf8status & UTF8MaskWidth; + character = UnicodeFromBytes(charBytes); } } - } else if (dbcsCodePage) { + } else { if (IsDBCSLeadByte(leadByte)) { - *character = (leadByte << 8) | static_cast<unsigned char>(cb.CharAt(pos+1)); - *width = 2; + bytesInCharacter = 2; + character = (leadByte << 8) | static_cast<unsigned char>(cb.CharAt(position+1)); } else { - *character = leadByte; - *width = 1; + character = leadByte; } } } else { - pos = start + characterOffset; - if ((pos < 0) || (pos > Length())) - return -1; - *character = cb.CharAt(pos); - *width = 1; + character = cb.CharAt(position); } - return pos; + if (pWidth) { + *pWidth = bytesInCharacter; + } + return character; } int SCI_METHOD Document::CodePage() const { diff --git a/src/Document.h b/src/Document.h index 8eb8db74a..5c7e8f8a0 100644 --- a/src/Document.h +++ b/src/Document.h @@ -279,7 +279,8 @@ public: int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true); int NextPosition(int pos, int moveDir) const; bool NextCharacter(int &pos, int moveDir) const; // Returns true if pos changed - int SCI_METHOD GetRelativePosition(int start, int characterOffset, int *character, int *width) const; + int SCI_METHOD GetRelativePosition(int positionStart, int characterOffset) const; + int SCI_METHOD GetCharacterAndWidth(int position, int *pWidth) const; int SCI_METHOD CodePage() const; bool SCI_METHOD IsDBCSLeadByte(char ch) const; int SafeSegment(const char *text, int length, int lengthSegment) const; |