diff options
Diffstat (limited to 'win32/ScintillaWin.cxx')
-rw-r--r-- | win32/ScintillaWin.cxx | 39 |
1 files changed, 18 insertions, 21 deletions
diff --git a/win32/ScintillaWin.cxx b/win32/ScintillaWin.cxx index a11ac94a1..31b9a59c6 100644 --- a/win32/ScintillaWin.cxx +++ b/win32/ScintillaWin.cxx @@ -1201,34 +1201,31 @@ UINT ScintillaWin::CodePageOfDocument() const { } sptr_t ScintillaWin::GetTextLength() { - if (pdoc->Length() == 0) - return 0; - std::string docBytes(pdoc->Length(), '\0'); - pdoc->GetCharRange(&docBytes[0], 0, pdoc->Length()); - if (IsUnicodeMode()) { - return UTF16Length(std::string_view(&docBytes[0], docBytes.size())); - } else { - return WideCharLenFromMultiByte(CodePageOfDocument(), docBytes); - } + return pdoc->CountUTF16(0, pdoc->Length()); } sptr_t ScintillaWin::GetText(uptr_t wParam, sptr_t lParam) { + if (lParam == 0) { + return pdoc->CountUTF16(0, pdoc->Length()); + } + if (wParam == 0) { + return 0; + } wchar_t *ptr = static_cast<wchar_t *>(PtrFromSPtr(lParam)); if (pdoc->Length() == 0) { *ptr = L'\0'; return 0; } - std::string docBytes(pdoc->Length(), '\0'); - pdoc->GetCharRange(&docBytes[0], 0, pdoc->Length()); + const Sci::Position lengthWanted = wParam - 1; + Sci::Position sizeRequestedRange = pdoc->GetRelativePositionUTF16(0, lengthWanted); + if (sizeRequestedRange < 0) { + // Requested more text than there is in the document. + sizeRequestedRange = pdoc->CountUTF16(0, pdoc->Length()); + } + std::string docBytes(sizeRequestedRange, '\0'); + pdoc->GetCharRange(&docBytes[0], 0, sizeRequestedRange); if (IsUnicodeMode()) { - const std::string_view sv(&docBytes[0], docBytes.size()); - const size_t lengthUTF16 = UTF16Length(sv); - if (lParam == 0) - return lengthUTF16; - if (wParam == 0) - return 0; - size_t uLen = UTF16FromUTF8(sv, - ptr, wParam - 1); + const size_t uLen = UTF16FromUTF8(docBytes, ptr, lengthWanted); ptr[uLen] = L'\0'; return uLen; } else { @@ -1236,8 +1233,8 @@ sptr_t ScintillaWin::GetText(uptr_t wParam, sptr_t lParam) { // Convert to Unicode using the current Scintilla code page const UINT cpSrc = CodePageOfDocument(); int lengthUTF16 = WideCharLenFromMultiByte(cpSrc, docBytes); - if (lengthUTF16 >= static_cast<int>(wParam)) - lengthUTF16 = static_cast<int>(wParam)-1; + if (lengthUTF16 > lengthWanted) + lengthUTF16 = static_cast<int>(lengthWanted); WideCharFromMultiByte(cpSrc, docBytes, ptr, lengthUTF16); ptr[lengthUTF16] = L'\0'; return lengthUTF16; |