summaryrefslogtreecommitdiff
path: root/zen/zstring.cpp
diff options
context:
space:
mode:
authorB. Stack <bgstack15@gmail.com>2023-01-22 13:56:55 -0500
committerB. Stack <bgstack15@gmail.com>2023-01-22 13:56:55 -0500
commit75e05bc441382db69c842a64c562738cb749214e (patch)
tree698b60b3b4b914bf7958cf1174d0373909bf1e8f /zen/zstring.cpp
parentadd upstream 11.29 (diff)
downloadFreeFileSync-75e05bc441382db69c842a64c562738cb749214e.tar.gz
FreeFileSync-75e05bc441382db69c842a64c562738cb749214e.tar.bz2
FreeFileSync-75e05bc441382db69c842a64c562738cb749214e.zip
add upstream 12.0
Diffstat (limited to 'zen/zstring.cpp')
-rw-r--r--zen/zstring.cpp26
1 files changed, 17 insertions, 9 deletions
diff --git a/zen/zstring.cpp b/zen/zstring.cpp
index 59e90956..019973e9 100644
--- a/zen/zstring.cpp
+++ b/zen/zstring.cpp
@@ -22,11 +22,17 @@ Zstring getUnicodeNormalForm_NonAsciiValidUtf(const Zstring& str, UnicodeNormalF
try
{
- gchar* outStr = ::g_utf8_normalize(str.c_str(), str.length(), form == UnicodeNormalForm::nfc ? G_NORMALIZE_NFC : G_NORMALIZE_NFD);
- if (!outStr)
+ gchar* strNorm = ::g_utf8_normalize(str.c_str(), str.length(), form == UnicodeNormalForm::nfc ? G_NORMALIZE_NFC : G_NORMALIZE_NFD);
+ if (!strNorm)
throw SysError(formatSystemError("g_utf8_normalize", L"", L"Conversion failed."));
- ZEN_ON_SCOPE_EXIT(::g_free(outStr));
- return outStr;
+ ZEN_ON_SCOPE_EXIT(::g_free(strNorm));
+
+ const std::string_view strNormView(strNorm, strLength(strNorm));
+
+ if (equalString(str, strNormView)) //avoid extra memory allocation
+ return str;
+
+ return Zstring(strNormView);
}
catch (const SysError& e)
@@ -37,7 +43,7 @@ Zstring getUnicodeNormalForm_NonAsciiValidUtf(const Zstring& str, UnicodeNormalF
}
-Zstring getUnicodeNormalFormNonAscii(const Zstring& str, UnicodeNormalForm form)
+Zstring getValidUtf(const Zstring& str)
{
/* 1. do NOT fail on broken UTF encoding, instead normalize using REPLACEMENT_CHAR!
2. NormalizeString() haateeez them Unicode non-characters: ERROR_NO_UNICODE_TRANSLATION! http://www.unicode.org/faq/private_use.html#nonchar1
@@ -68,10 +74,10 @@ Zstring getUnicodeNormalFormNonAscii(const Zstring& str, UnicodeNormalForm form)
codePointToUtf<Zchar>(*cp, [&](Zchar ch) { validStr += ch; });
}
- return getUnicodeNormalForm_NonAsciiValidUtf(validStr, form);
+ return validStr;
}
else
- return getUnicodeNormalForm_NonAsciiValidUtf(str, form);
+ return str;
}
@@ -88,9 +94,11 @@ Zstring getUpperCaseAscii(const Zstring& str)
Zstring getUpperCaseNonAscii(const Zstring& str)
{
- Zstring strNorm = getUnicodeNormalFormNonAscii(str, UnicodeNormalForm::native);
+ const Zstring& strValidUtf = getValidUtf(str);
try
{
+ const Zstring strNorm = getUnicodeNormalForm_NonAsciiValidUtf(strValidUtf, UnicodeNormalForm::native);
+
Zstring output;
output.reserve(strNorm.size());
@@ -118,7 +126,7 @@ Zstring getUnicodeNormalForm(const Zstring& str, UnicodeNormalForm form)
if (isAsciiString(str)) //fast path: in the range of 3.5ns
return str;
- return getUnicodeNormalFormNonAscii(str, form); //slow path
+ return getUnicodeNormalForm_NonAsciiValidUtf(getValidUtf(str), form); //slow path
}
bgstack15