diff options
| author | Zufu Liu <unknown> | 2019-06-17 08:49:43 +1000 | 
|---|---|---|
| committer | Zufu Liu <unknown> | 2019-06-17 08:49:43 +1000 | 
| commit | 1e79a7c53fbfd7481e26f92037de92597dedaf79 (patch) | |
| tree | 7ea049531f97b9bba03c23f28555563480cee58c /src/Editor.cxx | |
| parent | a82f89692231c7bd44b1dab9f4343836bbf1fa54 (diff) | |
| download | scintilla-mirror-1e79a7c53fbfd7481e26f92037de92597dedaf79.tar.gz | |
Feature [feature-requests:#1293]. InsertCharacter replaces AddCharUTF.
Diffstat (limited to 'src/Editor.cxx')
| -rw-r--r-- | src/Editor.cxx | 33 | 
1 files changed, 17 insertions, 16 deletions
diff --git a/src/Editor.cxx b/src/Editor.cxx index 2500e6157..2f35862e8 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -100,10 +100,10 @@ Timer::Timer() noexcept :  Idler::Idler() noexcept :  		state(false), idlerID(0) {} -static constexpr bool IsAllSpacesOrTabs(const char *s, unsigned int len) noexcept { -	for (unsigned int i = 0; i < len; i++) { +static constexpr bool IsAllSpacesOrTabs(std::string_view sv) noexcept { +	for (const char ch : sv) {  		// This is safe because IsSpaceOrTab() will return false for null terminators -		if (!IsSpaceOrTab(s[i])) +		if (!IsSpaceOrTab(ch))  			return false;  	}  	return true; @@ -1891,7 +1891,7 @@ void Editor::AddChar(char ch) {  	char s[2];  	s[0] = ch;  	s[1] = '\0'; -	AddCharUTF(s, 1); +	InsertCharacter(std::string_view(s, 1));  }  void Editor::FilterSelections() { @@ -1901,9 +1901,9 @@ void Editor::FilterSelections() {  	}  } -// AddCharUTF inserts an array of bytes which may or may not be in UTF-8. -void Editor::AddCharUTF(const char *s, unsigned int len, bool treatAsDBCS) { -	if (len == 0) { +// InsertCharacter inserts a character encoded in document code page. +void Editor::InsertCharacter(std::string_view sv) { +	if (sv.empty()) {  		return;  	}  	FilterSelections(); @@ -1943,7 +1943,7 @@ void Editor::AddCharUTF(const char *s, unsigned int len, bool treatAsDBCS) {  					}  				}  				positionInsert = RealizeVirtualSpace(positionInsert, currentSel->caret.VirtualSpace()); -				const Sci::Position lengthInserted = pdoc->InsertString(positionInsert, s, len); +				const Sci::Position lengthInserted = pdoc->InsertString(positionInsert, sv.data(), sv.length());  				if (lengthInserted > 0) {  					currentSel->caret.SetPosition(positionInsert + lengthInserted);  					currentSel->anchor.SetPosition(positionInsert + lengthInserted); @@ -1972,32 +1972,33 @@ void Editor::AddCharUTF(const char *s, unsigned int len, bool treatAsDBCS) {  	// Avoid blinking during rapid typing:  	ShowCaretAtCurrentPosition();  	if ((caretSticky == SC_CARETSTICKY_OFF) || -		((caretSticky == SC_CARETSTICKY_WHITESPACE) && !IsAllSpacesOrTabs(s, len))) { +		((caretSticky == SC_CARETSTICKY_WHITESPACE) && !IsAllSpacesOrTabs(sv))) {  		SetLastXChosen();  	} -	int ch = static_cast<unsigned char>(s[0]); -	if (treatAsDBCS || pdoc->dbcsCodePage != SC_CP_UTF8) { -		if (len > 1) { +	int ch = static_cast<unsigned char>(sv[0]); +	if (pdoc->dbcsCodePage != SC_CP_UTF8) { +		if (sv.length() > 1) {  			// DBCS code page or DBCS font character set. -			ch = (ch << 8) | static_cast<unsigned char>(s[1]); +			ch = (ch << 8) | static_cast<unsigned char>(sv[1]);  		}  	} else { -		if ((ch < 0xC0) || (1 == len)) { +		if ((ch < 0xC0) || (1 == sv.length())) {  			// Handles UTF-8 characters between 0x01 and 0x7F and single byte  			// characters when not in UTF-8 mode.  			// Also treats \0 and naked trail bytes 0x80 to 0xBF as valid  			// characters representing themselves.  		} else {  			unsigned int utf32[1] = { 0 }; -			UTF32FromUTF8(std::string_view(s, len), utf32, std::size(utf32)); +			UTF32FromUTF8(sv, utf32, std::size(utf32));  			ch = utf32[0];  		}  	}  	NotifyChar(ch);  	if (recordingMacro) { -		NotifyMacroRecord(SCI_REPLACESEL, 0, reinterpret_cast<sptr_t>(s)); +		std::string copy(sv); // ensure NUL-terminated +		NotifyMacroRecord(SCI_REPLACESEL, 0, reinterpret_cast<sptr_t>(copy.data()));  	}  }  | 
