Skip to content

Commit fadcef2

Browse files
committed
constexpr + noexcept unicode_util
1 parent c8def89 commit fadcef2

File tree

2 files changed

+54
-53
lines changed

2 files changed

+54
-53
lines changed

src/common/unicode_util.cpp

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,38 @@ using namespace Firebird;
5757

5858
namespace {
5959
#if defined(WIN_NT)
60-
const char* const inTemplate = "icuin%s.dll";
61-
const char* const ucTemplate = "icuuc%s.dll";
60+
constexpr const char* inTemplate = "icuin%s.dll";
61+
constexpr const char* ucTemplate = "icuuc%s.dll";
6262
#elif defined(DARWIN)
63-
const char* const inTemplate = "lib/libicui18n.%s.dylib";
64-
const char* const ucTemplate = "lib/libicuuc.%s.dylib";
63+
constexpr const char* inTemplate = "lib/libicui18n.%s.dylib";
64+
constexpr const char* ucTemplate = "lib/libicuuc.%s.dylib";
6565
#elif defined(HPUX)
66-
const char* const inTemplate = "libicui18n.sl.%s";
67-
const char* const ucTemplate = "libicuuc.sl.%s";
66+
constexpr const char* inTemplate = "libicui18n.sl.%s";
67+
constexpr const char* ucTemplate = "libicuuc.sl.%s";
6868
#elif defined(ANDROID)
69-
const char* const inTemplate = "libicui18n.%s.so";
70-
const char* const ucTemplate = "libicuuc.%s.so";
69+
constexpr const char* inTemplate = "libicui18n.%s.so";
70+
constexpr const char* ucTemplate = "libicuuc.%s.so";
7171
// In Android we need to load this library before others.
72-
const char* const dataTemplate = "libicudata.%s.so";
72+
constexpr const char* dataTemplate = "libicudata.%s.so";
7373
#else
74-
const char* const inTemplate = "libicui18n.so.%s";
75-
const char* const ucTemplate = "libicuuc.so.%s";
74+
constexpr const char* inTemplate = "libicui18n.so.%s";
75+
constexpr const char* ucTemplate = "libicuuc.so.%s";
7676
#endif
7777

7878
// encapsulate ICU library
7979
struct BaseICU
8080
{
81-
private:
82-
BaseICU(const BaseICU&); // not implemented
83-
BaseICU& operator =(const BaseICU&); // not implemented
84-
8581
public:
86-
BaseICU(int aMajorVersion, int aMinorVersion)
82+
BaseICU(int aMajorVersion, int aMinorVersion) noexcept
8783
: majorVersion(aMajorVersion),
8884
minorVersion(aMinorVersion),
8985
isSystem(aMajorVersion == 0)
9086
{
9187
}
9288

89+
BaseICU(const BaseICU&) = delete;
90+
BaseICU& operator =(const BaseICU&) = delete;
91+
9392
ModuleLoader::Module* formatAndLoad(const char* templateName);
9493
void initialize(ModuleLoader::Module* module);
9594

@@ -105,7 +104,7 @@ struct BaseICU
105104
else
106105
{
107106
// ICU has several schemas for entries names
108-
const char* const patterns[] =
107+
constexpr const char* patterns[] =
109108
{
110109
"%s_%d", "%s_%d_%d", "%s_%d%d", "%s"
111110
};
@@ -550,7 +549,7 @@ static void getVersions(const string& configInfo, ObjectsArray<string>& versions
550549

551550

552551
// BOCU-1
553-
USHORT UnicodeUtil::utf16KeyLength(USHORT len)
552+
USHORT UnicodeUtil::utf16KeyLength(USHORT len) noexcept
554553
{
555554
return (len / 2) * 4;
556555
}
@@ -718,7 +717,7 @@ ULONG UnicodeUtil::utf16UpperCase(ULONG srcLen, const USHORT* src, ULONG dstLen,
718717

719718

720719
ULONG UnicodeUtil::utf16ToUtf8(ULONG srcLen, const USHORT* src, ULONG dstLen, UCHAR* dst,
721-
USHORT* err_code, ULONG* err_position)
720+
USHORT* err_code, ULONG* err_position) noexcept
722721
{
723722
fb_assert(srcLen % sizeof(*src) == 0);
724723
fb_assert(src != NULL || dst == NULL);
@@ -849,7 +848,7 @@ ULONG UnicodeUtil::utf8ToUtf16(ULONG srcLen, const UCHAR* src, ULONG dstLen, USH
849848

850849

851850
ULONG UnicodeUtil::utf16ToUtf32(ULONG srcLen, const USHORT* src, ULONG dstLen, ULONG* dst,
852-
USHORT* err_code, ULONG* err_position)
851+
USHORT* err_code, ULONG* err_position) noexcept
853852
{
854853
fb_assert(srcLen % sizeof(*src) == 0);
855854
fb_assert(src != NULL || dst == NULL);
@@ -900,7 +899,7 @@ ULONG UnicodeUtil::utf16ToUtf32(ULONG srcLen, const USHORT* src, ULONG dstLen, U
900899

901900

902901
ULONG UnicodeUtil::utf32ToUtf16(ULONG srcLen, const ULONG* src, ULONG dstLen, USHORT* dst,
903-
USHORT* err_code, ULONG* err_position)
902+
USHORT* err_code, ULONG* err_position) noexcept
904903
{
905904
fb_assert(srcLen % sizeof(*src) == 0);
906905
fb_assert(src != NULL || dst == NULL);
@@ -966,8 +965,8 @@ SSHORT UnicodeUtil::utf16Compare(ULONG len1, const USHORT* str1, ULONG len2, con
966965
*error_flag = false;
967966

968967
// safe casts - alignment not changed
969-
int32_t cmp = getConversionICU().u_strCompare(reinterpret_cast<const UChar*>(str1), len1 / sizeof(*str1),
970-
reinterpret_cast<const UChar*>(str2), len2 / sizeof(*str2), true);
968+
const int32_t cmp = getConversionICU().u_strCompare(reinterpret_cast<const UChar*>(str1),
969+
len1 / sizeof(*str1), reinterpret_cast<const UChar*>(str2), len2 / sizeof(*str2), true);
971970

972971
return (cmp < 0 ? -1 : (cmp > 0 ? 1 : 0));
973972
}
@@ -982,7 +981,7 @@ ULONG UnicodeUtil::utf16Length(ULONG len, const USHORT* str)
982981

983982

984983
ULONG UnicodeUtil::utf16Substring(ULONG srcLen, const USHORT* src, ULONG dstLen, USHORT* dst,
985-
ULONG startPos, ULONG length)
984+
ULONG startPos, ULONG length) noexcept
986985
{
987986
fb_assert(srcLen % sizeof(*src) == 0);
988987
fb_assert(src != NULL && dst != NULL);
@@ -1059,7 +1058,7 @@ INTL_BOOL UnicodeUtil::utf8WellFormed(ULONG len, const UCHAR* str, ULONG* offend
10591058
}
10601059

10611060

1062-
INTL_BOOL UnicodeUtil::utf16WellFormed(ULONG len, const USHORT* str, ULONG* offending_position)
1061+
INTL_BOOL UnicodeUtil::utf16WellFormed(ULONG len, const USHORT* str, ULONG* offending_position) noexcept
10631062
{
10641063
fb_assert(str != NULL);
10651064
fb_assert(len % sizeof(*str) == 0);
@@ -1085,7 +1084,7 @@ INTL_BOOL UnicodeUtil::utf16WellFormed(ULONG len, const USHORT* str, ULONG* offe
10851084
}
10861085

10871086

1088-
INTL_BOOL UnicodeUtil::utf32WellFormed(ULONG len, const ULONG* str, ULONG* offending_position)
1087+
INTL_BOOL UnicodeUtil::utf32WellFormed(ULONG len, const ULONG* str, ULONG* offending_position) noexcept
10891088
{
10901089
fb_assert(str != NULL);
10911090
fb_assert(len % sizeof(*str) == 0);
@@ -1115,7 +1114,7 @@ void UnicodeUtil::utf8Normalize(UCharBuffer& data)
11151114
HalfStaticArray<USHORT, BUFFER_MEDIUM> utf16Buffer(data.getCount());
11161115
USHORT errCode;
11171116
ULONG errPosition;
1118-
ULONG utf16BufferLen = utf8ToUtf16(data.getCount(), data.begin(), data.getCount() * sizeof(USHORT),
1117+
const ULONG utf16BufferLen = utf8ToUtf16(data.getCount(), data.begin(), data.getCount() * sizeof(USHORT),
11191118
utf16Buffer.getBuffer(data.getCount()), &errCode, &errPosition);
11201119

11211120
UTransliterator* trans = icu->getCiAiTransliterator();
@@ -1154,7 +1153,7 @@ UnicodeUtil::ICU* UnicodeUtil::loadICU(const string& icuVersion, const string& c
11541153
for (ObjectsArray<string>::const_iterator i(versions.begin()); i != versions.end(); ++i)
11551154
{
11561155
int majorVersion, minorVersion;
1157-
int n = sscanf((*i == "default" ? version : *i).c_str(), "%d.%d",
1156+
const int n = sscanf((*i == "default" ? version : *i).c_str(), "%d.%d",
11581157
&majorVersion, &minorVersion);
11591158

11601159
if (n == 1)
@@ -1273,7 +1272,7 @@ UnicodeUtil::ICU* UnicodeUtil::loadICU(const string& icuVersion, const string& c
12731272
}
12741273

12751274

1276-
void UnicodeUtil::getICUVersion(ICU* icu, int& majorVersion, int& minorVersion)
1275+
void UnicodeUtil::getICUVersion(ICU* icu, int& majorVersion, int& minorVersion) noexcept
12771276
{
12781277
majorVersion = icu->majorVersion;
12791278
minorVersion = icu->minorVersion;
@@ -1294,9 +1293,9 @@ UnicodeUtil::ConversionICU& UnicodeUtil::getConversionICU()
12941293
return *convIcu;
12951294
}
12961295

1297-
// Try "favorite" (distributed on windows) version first
1298-
const int favMaj = 77;
1299-
const int favMin = 1;
1296+
// Try "favorite" (distributed on Windows) version first
1297+
constexpr int favMaj = 77;
1298+
constexpr int favMin = 1;
13001299
try
13011300
{
13021301
if ((convIcu = ImplementConversionICU::create(favMaj, favMin)))
@@ -1382,7 +1381,7 @@ UnicodeUtil::ConversionICU& UnicodeUtil::getConversionICU()
13821381
string UnicodeUtil::getDefaultIcuVersion()
13831382
{
13841383
string rc;
1385-
UnicodeUtil::ConversionICU& icu(UnicodeUtil::getConversionICU());
1384+
const UnicodeUtil::ConversionICU& icu(UnicodeUtil::getConversionICU());
13861385

13871386
if (icu.vMajor >= 10 && icu.vMinor == 0)
13881387
rc.printf("%d", icu.vMajor);
@@ -1603,23 +1602,24 @@ UnicodeUtil::Utf16Collation* UnicodeUtil::Utf16Collation::create(
16031602
// status not verified here.
16041603
icu->ucolGetContractionsAndExpansions(partialCollator, contractions, nullptr, false, &status);
16051604

1606-
int contractionsCount = icu->usetGetItemCount(contractions);
1605+
const int contractionsCount = icu->usetGetItemCount(contractions);
16071606

16081607
for (int contractionIndex = 0; contractionIndex < contractionsCount; ++contractionIndex)
16091608
{
16101609
UChar strChars[10];
16111610
UChar32 start, end;
16121611

16131612
status = U_ZERO_ERROR;
1614-
int len = icu->usetGetItem(contractions, contractionIndex, &start, &end, strChars, sizeof(strChars), &status);
1613+
const int len = icu->usetGetItem(contractions, contractionIndex, &start, &end, strChars,
1614+
sizeof(strChars), &status);
16151615

16161616
if (len >= 2)
16171617
{
16181618
obj->maxContractionsPrefixLength = static_cast<ULONG>(len - 1) > obj->maxContractionsPrefixLength ?
16191619
len - 1 : obj->maxContractionsPrefixLength;
16201620

16211621
UCHAR key[100];
1622-
int keyLen = icu->ucolGetSortKey(partialCollator, strChars, len, key, sizeof(key));
1622+
const int keyLen = icu->ucolGetSortKey(partialCollator, strChars, len, key, sizeof(key));
16231623

16241624
for (int prefixLen = 1; prefixLen < len; ++prefixLen)
16251625
{
@@ -1631,7 +1631,7 @@ UnicodeUtil::Utf16Collation* UnicodeUtil::Utf16Collation::create(
16311631
keySet = obj->contractionsPrefix.put(str);
16321632

16331633
UCHAR prefixKey[100];
1634-
int prefixKeyLen = icu->ucolGetSortKey(partialCollator,
1634+
const int prefixKeyLen = icu->ucolGetSortKey(partialCollator,
16351635
strChars, prefixLen, prefixKey, sizeof(prefixKey));
16361636

16371637
keySet->add(Array<UCHAR>(prefixKey, prefixKeyLen));
@@ -1654,8 +1654,8 @@ UnicodeUtil::Utf16Collation* UnicodeUtil::Utf16Collation::create(
16541654
continue;
16551655

16561656
fb_assert(accessor.current()->first.hasData());
1657-
USHORT firstCh = accessor.current()->first.front();
1658-
USHORT lastCh = accessor.current()->first.back();
1657+
const USHORT firstCh = accessor.current()->first.front();
1658+
const USHORT lastCh = accessor.current()->first.back();
16591659

16601660
if ((firstCh >= 0xFDD0 && firstCh <= 0xFDEF) || UTF_IS_SURROGATE(lastCh))
16611661
{
@@ -1707,7 +1707,7 @@ UnicodeUtil::Utf16Collation* UnicodeUtil::Utf16Collation::create(
17071707
++secondKeyDataIt;
17081708
}
17091709

1710-
unsigned backSize = commonKeys.back().getCount();
1710+
const unsigned backSize = commonKeys.back().getCount();
17111711

17121712
if (common > backSize)
17131713
commonKeys.back().append(secondKeyIt->begin() + backSize, common - backSize);
@@ -1755,7 +1755,7 @@ UnicodeUtil::Utf16Collation::~Utf16Collation()
17551755
}
17561756

17571757

1758-
USHORT UnicodeUtil::Utf16Collation::keyLength(USHORT len) const
1758+
USHORT UnicodeUtil::Utf16Collation::keyLength(USHORT len) const noexcept
17591759
{
17601760
return (len / 4) * 6;
17611761
}
@@ -1842,7 +1842,7 @@ USHORT UnicodeUtil::Utf16Collation::stringToKey(USHORT srcLen, const USHORT* src
18421842
}
18431843

18441844
auto originalDst = dst;
1845-
auto originalDstLen = dstLen;
1845+
const auto originalDstLen = dstLen;
18461846

18471847
if (!trailingNumbersRemoved)
18481848
{

src/common/unicode_util.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#ifndef COMMON_UNICODE_UTIL_H
2828
#define COMMON_UNICODE_UTIL_H
2929

30+
#include <algorithm>
3031
#include "intlobj_new.h"
3132
#include "../common/IntlUtil.h"
3233
#include "../common/os/mod_loader.h"
@@ -141,35 +142,35 @@ class UnicodeUtil
141142
class ICUModules;
142143
// routines semantically equivalent with intlobj_new.h
143144

144-
static USHORT utf16KeyLength(USHORT len); // BOCU-1
145+
static USHORT utf16KeyLength(USHORT len) noexcept; // BOCU-1
145146
static USHORT utf16ToKey(USHORT srcLen, const USHORT* src, USHORT dstLen, UCHAR* dst); // BOCU-1
146147
static ULONG utf16LowerCase(ULONG srcLen, const USHORT* src, ULONG dstLen, USHORT* dst,
147148
const ULONG* exceptions);
148149
static ULONG utf16UpperCase(ULONG srcLen, const USHORT* src, ULONG dstLen, USHORT* dst,
149150
const ULONG* exceptions);
150151
static ULONG utf16ToUtf8(ULONG srcLen, const USHORT* src, ULONG dstLen, UCHAR* dst,
151-
USHORT* err_code, ULONG* err_position);
152+
USHORT* err_code, ULONG* err_position) noexcept;
152153
static ULONG utf8ToUtf16(ULONG srcLen, const UCHAR* src, ULONG dstLen, USHORT* dst,
153154
USHORT* err_code, ULONG* err_position);
154155
static ULONG utf16ToUtf32(ULONG srcLen, const USHORT* src, ULONG dstLen, ULONG* dst,
155-
USHORT* err_code, ULONG* err_position);
156+
USHORT* err_code, ULONG* err_position) noexcept;
156157
static ULONG utf32ToUtf16(ULONG srcLen, const ULONG* src, ULONG dstLen, USHORT* dst,
157-
USHORT* err_code, ULONG* err_position);
158+
USHORT* err_code, ULONG* err_position) noexcept;
158159
static SSHORT utf16Compare(ULONG len1, const USHORT* str1, ULONG len2, const USHORT* str2,
159160
INTL_BOOL* error_flag);
160161

161162
static ULONG utf16Length(ULONG len, const USHORT* str);
162163
static ULONG utf16Substring(ULONG srcLen, const USHORT* src, ULONG dstLen, USHORT* dst,
163-
ULONG startPos, ULONG length);
164+
ULONG startPos, ULONG length) noexcept;
164165
static INTL_BOOL utf8WellFormed(ULONG len, const UCHAR* str, ULONG* offending_position);
165-
static INTL_BOOL utf16WellFormed(ULONG len, const USHORT* str, ULONG* offending_position);
166-
static INTL_BOOL utf32WellFormed(ULONG len, const ULONG* str, ULONG* offending_position);
166+
static INTL_BOOL utf16WellFormed(ULONG len, const USHORT* str, ULONG* offending_position) noexcept;
167+
static INTL_BOOL utf32WellFormed(ULONG len, const ULONG* str, ULONG* offending_position) noexcept;
167168

168169
static void utf8Normalize(Firebird::UCharBuffer& data);
169170

170171
static ConversionICU& getConversionICU();
171172
static ICU* loadICU(const Firebird::string& icuVersion, const Firebird::string& configInfo);
172-
static void getICUVersion(ICU* icu, int& majorVersion, int& minorVersion);
173+
static void getICUVersion(ICU* icu, int& majorVersion, int& minorVersion) noexcept;
173174
static ICU* getCollVersion(const Firebird::string& icuVersion,
174175
const Firebird::string& configInfo, Firebird::string& collVersion);
175176

@@ -187,7 +188,7 @@ class UnicodeUtil
187188

188189
~Utf16Collation();
189190

190-
USHORT keyLength(USHORT len) const;
191+
USHORT keyLength(USHORT len) const noexcept;
191192
USHORT stringToKey(USHORT srcLen, const USHORT* src, USHORT dstLen, UCHAR* dst,
192193
USHORT key_type) const;
193194
SSHORT compare(ULONG len1, const USHORT* str1, ULONG len2, const USHORT* str2,
@@ -201,8 +202,8 @@ class UnicodeUtil
201202
public:
202203
static bool greaterThan(const Firebird::Array<T>& i1, const Firebird::Array<T>& i2)
203204
{
204-
FB_SIZE_T minCount = MIN(i1.getCount(), i2.getCount());
205-
int cmp = memcmp(i1.begin(), i2.begin(), minCount * sizeof(T));
205+
const FB_SIZE_T minCount = std::min(i1.getCount(), i2.getCount());
206+
const int cmp = memcmp(i1.begin(), i2.begin(), minCount * sizeof(T));
206207

207208
if (cmp != 0)
208209
return cmp > 0;

0 commit comments

Comments
 (0)