From 5b869b233d5f927f03dc9a70a7006c7dcbf5c88e Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 14 May 2018 15:24:16 +1000 Subject: Use string_view for UniConversion functions. --- win32/PlatWin.cxx | 12 ++++++------ win32/ScintillaWin.cxx | 46 +++++++++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 27 deletions(-) (limited to 'win32') diff --git a/win32/PlatWin.cxx b/win32/PlatWin.cxx index 388002c70..beacc26fb 100644 --- a/win32/PlatWin.cxx +++ b/win32/PlatWin.cxx @@ -291,7 +291,7 @@ void SetLogFont(LOGFONTW &lf, const char *faceName, int characterSet, float size lf.lfItalic = italic ? 1 : 0; lf.lfCharSet = static_cast(characterSet); lf.lfQuality = Win32MapFontQuality(extraFontFlag); - UTF16FromUTF8(faceName, strlen(faceName)+1, lf.lfFaceName, LF_FACESIZE); + UTF16FromUTF8(faceName, lf.lfFaceName, LF_FACESIZE); } /** @@ -345,8 +345,8 @@ FontCached::FontCached(const FontParameters &fp) : #if defined(USE_D2D) IDWriteTextFormat *pTextFormat; const int faceSize = 200; - WCHAR wszFace[faceSize]; - UTF16FromUTF8(fp.faceName, strlen(fp.faceName)+1, wszFace, faceSize); + WCHAR wszFace[faceSize] = L""; + UTF16FromUTF8(fp.faceName, wszFace, faceSize); const FLOAT fHeight = fp.size; const DWRITE_FONT_STYLE style = fp.italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL; HRESULT hr = pIDWriteFactory->CreateTextFormat(wszFace, NULL, @@ -395,8 +395,8 @@ bool FontCached::SameAs(const FontParameters &fp) { (lf.lfCharSet == fp.characterSet) && (lf.lfQuality == Win32MapFontQuality(fp.extraFontFlag)) && (technology == fp.technology)) { - wchar_t wszFace[LF_FACESIZE]; - UTF16FromUTF8(fp.faceName, strlen(fp.faceName)+1, wszFace, LF_FACESIZE); + wchar_t wszFace[LF_FACESIZE] = L""; + UTF16FromUTF8(fp.faceName, wszFace, LF_FACESIZE); return 0 == wcscmp(lf.lfFaceName,wszFace); } return false; @@ -503,7 +503,7 @@ public: TextWide(std::string_view text, bool unicodeMode, int codePage=0) : VarBuffer(text.length()) { if (unicodeMode) { - tlen = static_cast(UTF16FromUTF8(text.data(), text.length(), buffer, text.length())); + tlen = static_cast(UTF16FromUTF8(text, buffer, text.length())); } else { // Support Asian string display in 9x English tlen = ::MultiByteToWideChar(codePage, 0, text.data(), static_cast(text.length()), diff --git a/win32/ScintillaWin.cxx b/win32/ScintillaWin.cxx index 869b1909a..c0b6fb3e2 100644 --- a/win32/ScintillaWin.cxx +++ b/win32/ScintillaWin.cxx @@ -803,9 +803,10 @@ Sci::Position ScintillaWin::EncodedFromUTF8(const char *utf8, char *encoded) con // the current codepage. Code is similar to HandleCompositionWindowed(). void ScintillaWin::AddCharUTF16(wchar_t const *wcs, unsigned int wclen) { if (IsUnicodeMode()) { - size_t len = UTF8Length(wcs, wclen); + const std::wstring_view wsv(wcs, wclen); + size_t len = UTF8Length(wsv); char utfval[maxLenInputIME * 3]; - UTF8FromUTF16(wcs, wclen, utfval, len); + UTF8FromUTF16(wsv, utfval, len); utfval[len] = '\0'; AddCharUTF(utfval, static_cast(len)); } else { @@ -1207,7 +1208,7 @@ sptr_t ScintillaWin::GetTextLength() { std::vector docBytes(pdoc->Length(), '\0'); pdoc->GetCharRange(&docBytes[0], 0, pdoc->Length()); if (IsUnicodeMode()) { - return UTF16Length(&docBytes[0], docBytes.size()); + return UTF16Length(std::string_view(&docBytes[0], docBytes.size())); } else { return ::MultiByteToWideChar(CodePageOfDocument(), 0, &docBytes[0], static_cast(docBytes.size()), NULL, 0); @@ -1223,12 +1224,13 @@ sptr_t ScintillaWin::GetText(uptr_t wParam, sptr_t lParam) { std::vector docBytes(pdoc->Length(), '\0'); pdoc->GetCharRange(&docBytes[0], 0, pdoc->Length()); if (IsUnicodeMode()) { - const size_t lengthUTF16 = UTF16Length(&docBytes[0], docBytes.size()); + 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(&docBytes[0], docBytes.size(), + size_t uLen = UTF16FromUTF8(sv, ptr, wParam - 1); ptr[uLen] = L'\0'; return uLen; @@ -2051,8 +2053,7 @@ public: if (foldedUTF8) { // Maximum length of a case conversion is 6 bytes, 3 characters wchar_t wFolded[20]; - const size_t charsConverted = UTF16FromUTF8(foldedUTF8, - strlen(foldedUTF8), + const size_t charsConverted = UTF16FromUTF8(std::string_view(foldedUTF8), wFolded, ELEMENTS(wFolded)); for (size_t j=0; j putf(mlen+1); - UTF8FromUTF16(&uptr[0], ulen, &putf[0], mlen); + UTF8FromUTF16(wsv, &putf[0], mlen); InsertPasteShape(&putf[0], mlen, pasteShape); } else { @@ -2684,7 +2686,7 @@ void ScintillaWin::ImeStartComposition() { lf.lfFaceName[0] = L'\0'; if (vs.styles[styleHere].fontName) { const char* fontName = vs.styles[styleHere].fontName; - UTF16FromUTF8(fontName, strlen(fontName)+1, lf.lfFaceName, LF_FACESIZE); + UTF16FromUTF8(std::string_view(fontName), lf.lfFaceName, LF_FACESIZE); } ::ImmSetCompositionFontW(imc.hIMC, &lf); @@ -2796,11 +2798,11 @@ void ScintillaWin::CopyToClipboard(const SelectionText &selectedText) { // Default Scintilla behaviour in Unicode mode if (IsUnicodeMode()) { - const size_t uchars = UTF16Length(selectedText.Data(), - selectedText.LengthWithTerminator()); + const std::string_view sv(selectedText.Data(), selectedText.LengthWithTerminator()); + const size_t uchars = UTF16Length(sv); uniText.Allocate(2 * uchars); if (uniText) { - UTF16FromUTF8(selectedText.Data(), selectedText.LengthWithTerminator(), + UTF16FromUTF8(sv, static_cast(uniText.ptr), uchars); } } else { @@ -3101,9 +3103,10 @@ STDMETHODIMP ScintillaWin::Drop(LPDATAOBJECT pIDataSource, DWORD grfKeyState, if (IsUnicodeMode()) { const size_t tlen = memUDrop.Size(); // Convert UTF-16 to UTF-8 - const size_t dataLen = UTF8Length(udata, tlen/2); + const std::wstring_view wsv(udata, tlen / 2); + const size_t dataLen = UTF8Length(wsv); data.resize(dataLen+1); - UTF8FromUTF16(udata, tlen/2, &data[0], dataLen); + UTF8FromUTF16(wsv, &data[0], dataLen); } else { // Convert UTF-16 to ANSI // @@ -3176,10 +3179,11 @@ STDMETHODIMP ScintillaWin::GetData(FORMATETC *pFEIn, STGMEDIUM *pSTM) { GlobalMemory text; if (pFEIn->cfFormat == CF_UNICODETEXT) { - const size_t uchars = UTF16Length(drag.Data(), drag.LengthWithTerminator()); + const std::string_view sv(drag.Data(), drag.LengthWithTerminator()); + const size_t uchars = UTF16Length(sv); text.Allocate(2 * uchars); if (text) { - UTF16FromUTF8(drag.Data(), drag.LengthWithTerminator(), + UTF16FromUTF8(sv, static_cast(text.ptr), uchars); } } else { -- cgit v1.2.3