Page MenuHomePhorge

D4544.1767344557.diff
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

D4544.1767344557.diff

diff --git a/services/backup/src/Tools.cpp b/services/backup/src/Tools.cpp
--- a/services/backup/src/Tools.cpp
+++ b/services/backup/src/Tools.cpp
@@ -3,6 +3,7 @@
#include "GlobalConstants.h"
#include "GlobalTools.h"
+#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <random>
@@ -53,19 +54,19 @@
}
std::size_t getUtf8Length(std::string &str) {
- std::size_t result = 0;
- const char *ptr = str.data();
- const char *end = ptr + str.size();
- while (ptr < end) {
- int next = std::mblen(ptr, end - ptr);
- if (next == -1) {
- throw std::runtime_error("strlen_mb(): conversionn error");
- }
- ptr += next;
- ++result;
- }
- return result;
+ // How it works?
+ // Every byte of multibyte utf8 character starts with information about its
+ // own type. Additional bytes always start with a 2bit sequence "10xx xxxx"
+ // (which is equal to hex 0x80). To count the length of utf8 string we need
+ // to skip these bytes, so we use a bitmask.
+ //
+ // We need to compare first 2 bits of a byte, so we use bitwise operator
+ // & with mask 0xC0. If returned value is equal to 0x80, we skip it.
+
+ return std::count_if(str.begin(), str.end(), [](char c) {
+ return (static_cast<unsigned char>(c) & 0xC0) != 0x80;
+ });
}
} // namespace tools

File Metadata

Mime Type
text/plain
Expires
Fri, Jan 2, 9:02 AM (5 h, 16 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5878891
Default Alt Text
D4544.1767344557.diff (1 KB)

Event Timeline