diff options
| author | Neil <nyamatongwe@gmail.com> | 2018-05-14 15:24:16 +1000 | 
|---|---|---|
| committer | Neil <nyamatongwe@gmail.com> | 2018-05-14 15:24:16 +1000 | 
| commit | 5b869b233d5f927f03dc9a70a7006c7dcbf5c88e (patch) | |
| tree | cbdab8a8f954763c72b6710be80fa6bd985ed5d4 /src | |
| parent | cebd3113ce6b1e4dbc98f458c6033fd41e29d69d (diff) | |
| download | scintilla-mirror-5b869b233d5f927f03dc9a70a7006c7dcbf5c88e.tar.gz | |
Use string_view for UniConversion functions.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Document.cxx | 9 | ||||
| -rw-r--r-- | src/Editor.cxx | 2 | ||||
| -rw-r--r-- | src/UniConversion.cxx | 62 | ||||
| -rw-r--r-- | src/UniConversion.h | 10 | 
4 files changed, 42 insertions, 41 deletions
| diff --git a/src/Document.cxx b/src/Document.cxx index fbe387f6d..bcc368a68 100644 --- a/src/Document.cxx +++ b/src/Document.cxx @@ -2939,12 +2939,13 @@ Sci::Position Cxx11RegexFindText(const Document *doc, Sci::Position minPos, Sci:  		bool matched = false;  		if (SC_CP_UTF8 == doc->dbcsCodePage) { -			const size_t lenS = strlen(s); -			std::vector<wchar_t> ws(lenS + 1); +			const std::string_view sv(s); +			const size_t lenS = sv.length(); +			std::vector<wchar_t> ws(sv.length() + 1);  #if WCHAR_T_IS_16 -			const size_t outLen = UTF16FromUTF8(s, lenS, &ws[0], lenS); +			const size_t outLen = UTF16FromUTF8(sv, &ws[0], lenS);  #else -			const size_t outLen = UTF32FromUTF8(s, lenS, reinterpret_cast<unsigned int *>(&ws[0]), lenS); +			const size_t outLen = UTF32FromUTF8(sv, reinterpret_cast<unsigned int *>(&ws[0]), lenS);  #endif  			ws[outLen] = 0;  			std::wregex regexp; diff --git a/src/Editor.cxx b/src/Editor.cxx index 36ebc5cea..7a61d503a 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -1957,7 +1957,7 @@ void Editor::AddCharUTF(const char *s, unsigned int len, bool treatAsDBCS) {  			// characters representing themselves.  		} else {  			unsigned int utf32[1] = { 0 }; -			UTF32FromUTF8(s, len, utf32, ELEMENTS(utf32)); +			UTF32FromUTF8(std::string_view(s, len), utf32, ELEMENTS(utf32));  			byte = utf32[0];  		}  		NotifyChar(byte); diff --git a/src/UniConversion.cxx b/src/UniConversion.cxx index a6830f950..1287aa612 100644 --- a/src/UniConversion.cxx +++ b/src/UniConversion.cxx @@ -17,10 +17,10 @@ using namespace Scintilla;  namespace Scintilla { -size_t UTF8Length(const wchar_t *uptr, size_t tlen) { +size_t UTF8Length(std::wstring_view wsv) {  	size_t len = 0; -	for (size_t i = 0; i < tlen && uptr[i];) { -		const unsigned int uch = uptr[i]; +	for (size_t i = 0; i < wsv.length() && wsv[i];) { +		const unsigned int uch = wsv[i];  		if (uch < 0x80) {  			len++;  		} else if (uch < 0x800) { @@ -37,10 +37,10 @@ size_t UTF8Length(const wchar_t *uptr, size_t tlen) {  	return len;  } -void UTF8FromUTF16(const wchar_t *uptr, size_t tlen, char *putf, size_t len) { +void UTF8FromUTF16(std::wstring_view wsv, char *putf, size_t len) {  	size_t k = 0; -	for (size_t i = 0; i < tlen && uptr[i];) { -		const unsigned int uch = uptr[i]; +	for (size_t i = 0; i < wsv.length() && wsv[i];) { +		const unsigned int uch = wsv[i];  		if (uch < 0x80) {  			putf[k++] = static_cast<char>(uch);  		} else if (uch < 0x800) { @@ -50,7 +50,7 @@ void UTF8FromUTF16(const wchar_t *uptr, size_t tlen, char *putf, size_t len) {  			(uch <= SURROGATE_TRAIL_LAST)) {  			// Half a surrogate pair  			i++; -			const unsigned int xch = 0x10000 + ((uch & 0x3ff) << 10) + (uptr[i] & 0x3ff); +			const unsigned int xch = 0x10000 + ((uch & 0x3ff) << 10) + (wsv[i] & 0x3ff);  			putf[k++] = static_cast<char>(0xF0 | (xch >> 18));  			putf[k++] = static_cast<char>(0x80 | ((xch >> 12) & 0x3f));  			putf[k++] = static_cast<char>(0x80 | ((xch >> 6) & 0x3f)); @@ -86,14 +86,14 @@ void UTF8FromUTF32Character(int uch, char *putf) {  	putf[k] = '\0';  } -size_t UTF16Length(const char *s, size_t len) { +size_t UTF16Length(std::string_view sv) {  	size_t ulen = 0; -	for (size_t i = 0; i < len;) { -		const unsigned char ch = s[i]; +	for (size_t i = 0; i<sv.length();) { +		const unsigned char ch = sv[i];  		const unsigned int byteCount = UTF8BytesOfLead[ch];  		const unsigned int utf16Len = UTF16LengthFromUTF8ByteCount(byteCount);  		i += byteCount; -		ulen += (i > len) ? 1 : utf16Len; +		ulen += (i > sv.length()) ? 1 : utf16Len;  	}  	return ulen;  } @@ -104,14 +104,14 @@ constexpr unsigned char TrailByteValue(unsigned char c) {  	return c & 0b0011'1111;  } -size_t UTF16FromUTF8(const char *s, size_t len, wchar_t *tbuf, size_t tlen) { +size_t UTF16FromUTF8(std::string_view sv, wchar_t *tbuf, size_t tlen) {  	size_t ui = 0; -	for (size_t i = 0; i < len;) { -		unsigned char ch = s[i]; +	for (size_t i = 0; i < sv.length();) { +		unsigned char ch = sv[i];  		const unsigned int byteCount = UTF8BytesOfLead[ch];  		unsigned int value; -		if (i + byteCount > len) { +		if (i + byteCount > sv.length()) {  			// Trying to read past end but still have space to write  			if (ui < tlen) {  				tbuf[ui] = ch; @@ -132,26 +132,26 @@ size_t UTF16FromUTF8(const char *s, size_t len, wchar_t *tbuf, size_t tlen) {  			break;  		case 2:  			value = (ch & 0x1F) << 6; -			ch = s[i++]; +			ch = sv[i++];  			value += TrailByteValue(ch);  			tbuf[ui] = static_cast<wchar_t>(value);  			break;  		case 3:  			value = (ch & 0xF) << 12; -			ch = s[i++]; +			ch = sv[i++];  			value += (TrailByteValue(ch) << 6); -			ch = s[i++]; +			ch = sv[i++];  			value += TrailByteValue(ch);  			tbuf[ui] = static_cast<wchar_t>(value);  			break;  		default:  			// Outside the BMP so need two surrogates  			value = (ch & 0x7) << 18; -			ch = s[i++]; +			ch = sv[i++];  			value += TrailByteValue(ch) << 12; -			ch = s[i++]; +			ch = sv[i++];  			value += TrailByteValue(ch) << 6; -			ch = s[i++]; +			ch = sv[i++];  			value += TrailByteValue(ch);  			tbuf[ui] = static_cast<wchar_t>(((value - 0x10000) >> 10) + SURROGATE_LEAD_FIRST);  			ui++; @@ -163,14 +163,14 @@ size_t UTF16FromUTF8(const char *s, size_t len, wchar_t *tbuf, size_t tlen) {  	return ui;  } -size_t UTF32FromUTF8(const char *s, size_t len, unsigned int *tbuf, size_t tlen) { +size_t UTF32FromUTF8(std::string_view sv, unsigned int *tbuf, size_t tlen) {  	size_t ui = 0; -	for (size_t i = 0; i < len;) { -		unsigned char ch = s[i]; +	for (size_t i = 0; i < sv.length();) { +		unsigned char ch = sv[i];  		const unsigned int byteCount = UTF8BytesOfLead[ch];  		unsigned int value; -		if (i + byteCount > len) { +		if (i + byteCount > sv.length()) {  			// Trying to read past end but still have space to write  			if (ui < tlen) {  				tbuf[ui] = ch; @@ -190,23 +190,23 @@ size_t UTF32FromUTF8(const char *s, size_t len, unsigned int *tbuf, size_t tlen)  			break;  		case 2:  			value = (ch & 0x1F) << 6; -			ch = s[i++]; +			ch = sv[i++];  			value += TrailByteValue(ch);  			break;  		case 3:  			value = (ch & 0xF) << 12; -			ch = s[i++]; +			ch = sv[i++];  			value += TrailByteValue(ch) << 6; -			ch = s[i++]; +			ch = sv[i++];  			value += TrailByteValue(ch);  			break;  		default:  			value = (ch & 0x7) << 18; -			ch = s[i++]; +			ch = sv[i++];  			value += TrailByteValue(ch) << 12; -			ch = s[i++]; +			ch = sv[i++];  			value += TrailByteValue(ch) << 6; -			ch = s[i++]; +			ch = sv[i++];  			value += TrailByteValue(ch);  			break;  		} diff --git a/src/UniConversion.h b/src/UniConversion.h index 4cdfe1fac..1b84b8f81 100644 --- a/src/UniConversion.h +++ b/src/UniConversion.h @@ -14,12 +14,12 @@ const int UTF8MaxBytes = 4;  const int unicodeReplacementChar = 0xFFFD; -size_t UTF8Length(const wchar_t *uptr, size_t tlen); -void UTF8FromUTF16(const wchar_t *uptr, size_t tlen, char *putf, size_t len); +size_t UTF8Length(std::wstring_view wsv); +void UTF8FromUTF16(std::wstring_view wsv, char *putf, size_t len);  void UTF8FromUTF32Character(int uch, char *putf); -size_t UTF16Length(const char *s, size_t len); -size_t UTF16FromUTF8(const char *s, size_t len, wchar_t *tbuf, size_t tlen); -size_t UTF32FromUTF8(const char *s, size_t len, unsigned int *tbuf, size_t tlen); +size_t UTF16Length(std::string_view sv); +size_t UTF16FromUTF8(std::string_view sv, wchar_t *tbuf, size_t tlen); +size_t UTF32FromUTF8(std::string_view sv, unsigned int *tbuf, size_t tlen);  unsigned int UTF16FromUTF32Character(unsigned int val, wchar_t *tbuf) noexcept;  std::string FixInvalidUTF8(const std::string &text); | 
