Skip to content

Commit f2c724c

Browse files
committed
noexcept in MetaString
1 parent a719b99 commit f2c724c

File tree

2 files changed

+57
-59
lines changed

2 files changed

+57
-59
lines changed

src/common/classes/MetaString.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828

2929
#include "firebird.h"
3030

31+
#include <algorithm>
3132
#include <stdarg.h>
3233

3334
#include "../common/classes/MetaString.h"
3435

3536
namespace Firebird {
3637

37-
MetaString& MetaString::assign(const char* s, FB_SIZE_T l)
38+
MetaString& MetaString::assign(const char* s, FB_SIZE_T l) noexcept
3839
{
3940
init();
4041
if (s)
@@ -49,20 +50,20 @@ MetaString& MetaString::assign(const char* s, FB_SIZE_T l)
4950
return *this;
5051
}
5152

52-
char* MetaString::getBuffer(const FB_SIZE_T l)
53+
char* MetaString::getBuffer(const FB_SIZE_T l) noexcept
5354
{
54-
fb_assert (l < MAX_SQL_IDENTIFIER_SIZE);
55+
fb_assert(l < MAX_SQL_IDENTIFIER_SIZE);
5556
init();
5657
count = l;
5758
return data;
5859
}
5960

60-
int MetaString::compare(const char* s, FB_SIZE_T l) const
61+
int MetaString::compare(const char* s, FB_SIZE_T l) const noexcept
6162
{
6263
if (s)
6364
{
6465
adjustLength(s, l);
65-
FB_SIZE_T x = length() < l ? length() : l;
66+
const FB_SIZE_T x = std::min(length(), l);
6667
int rc = memcmp(c_str(), s, x);
6768
if (rc)
6869
{
@@ -72,7 +73,7 @@ int MetaString::compare(const char* s, FB_SIZE_T l) const
7273
return length() - l;
7374
}
7475

75-
void MetaString::adjustLength(const char* const s, FB_SIZE_T& l)
76+
void MetaString::adjustLength(const char* const s, FB_SIZE_T& l) noexcept
7677
{
7778
fb_assert(s);
7879
if (l > MAX_SQL_IDENTIFIER_LEN)
@@ -98,7 +99,7 @@ void MetaString::printf(const char* format, ...)
9899
init();
99100
va_list params;
100101
va_start(params, format);
101-
int l = VSNPRINTF(data, MAX_SQL_IDENTIFIER_LEN, format, params);
102+
int l = vsnprintf(data, MAX_SQL_IDENTIFIER_LEN, format, params);
102103
if (l < 0 || FB_SIZE_T(l) > MAX_SQL_IDENTIFIER_LEN)
103104
{
104105
l = MAX_SQL_IDENTIFIER_LEN;
@@ -112,10 +113,7 @@ FB_SIZE_T MetaString::copyTo(char* to, FB_SIZE_T toSize) const
112113
{
113114
fb_assert(to);
114115
fb_assert(toSize);
115-
if (--toSize > length())
116-
{
117-
toSize = length();
118-
}
116+
toSize = std::min(toSize - 1, length());
119117
memcpy(to, c_str(), toSize);
120118
to[toSize] = 0;
121119
return toSize;

src/common/classes/MetaString.h

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -74,35 +74,35 @@ class MetaString
7474
char data[MAX_SQL_IDENTIFIER_SIZE];
7575
unsigned int count;
7676

77-
void init()
77+
void init() noexcept
7878
{
7979
memset(data, 0, MAX_SQL_IDENTIFIER_SIZE);
8080
}
81-
MetaString& set(const MetaString& m)
81+
MetaString& set(const MetaString& m) noexcept
8282
{
8383
memcpy(data, m.data, MAX_SQL_IDENTIFIER_SIZE);
8484
count = m.count;
8585
return *this;
8686
}
8787

8888
public:
89-
MetaString() { init(); count = 0; }
90-
MetaString(const char* s) { assign(s); }
91-
MetaString(const char* s, FB_SIZE_T l) { assign(s, l); }
92-
MetaString(const MetaString& m) { set(m); }
93-
MetaString(const AbstractString& s) { assign(s.c_str(), s.length()); }
94-
explicit MetaString(MemoryPool&) { init(); count = 0; }
95-
MetaString(MemoryPool&, const char* s) { assign(s); }
96-
MetaString(MemoryPool&, const char* s, FB_SIZE_T l) { assign(s, l); }
97-
MetaString(MemoryPool&, const MetaString& m) { set(m); }
98-
MetaString(MemoryPool&, const AbstractString& s) { assign(s.c_str(), s.length()); }
89+
MetaString() noexcept { init(); count = 0; }
90+
MetaString(const char* s) noexcept { assign(s); }
91+
MetaString(const char* s, FB_SIZE_T l) noexcept { assign(s, l); }
92+
MetaString(const MetaString& m) noexcept { set(m); }
93+
MetaString(const AbstractString& s) noexcept { assign(s.c_str(), s.length()); }
94+
explicit MetaString(MemoryPool&) noexcept { init(); count = 0; }
95+
MetaString(MemoryPool&, const char* s) noexcept { assign(s); }
96+
MetaString(MemoryPool&, const char* s, FB_SIZE_T l) noexcept { assign(s, l); }
97+
MetaString(MemoryPool&, const MetaString& m) noexcept { set(m); }
98+
MetaString(MemoryPool&, const AbstractString& s) noexcept { assign(s.c_str(), s.length()); }
9999

100100
public:
101101
static void parseList(const string& str, ObjectsArray<MetaString>& list)
102102
{
103103
auto pos = str.begin();
104104

105-
const auto skipSpaces = [&pos, &str]
105+
const auto skipSpaces = [&pos, &str]() noexcept
106106
{
107107
while (pos != str.end() && (*pos == ' ' || *pos == '\t' || *pos == '\f' || *pos == '\r' || *pos == '\n'))
108108
++pos;
@@ -213,52 +213,52 @@ class MetaString
213213
}
214214

215215
public:
216-
MetaString& assign(const char* s, FB_SIZE_T l);
217-
MetaString& assign(const char* s) { return assign(s, s ? fb_strlen(s) : 0); }
218-
MetaString& clear() { return assign(nullptr, 0); }
219-
MetaString& operator=(const char* s) { return assign(s); }
220-
MetaString& operator=(const AbstractString& s) { return assign(s.c_str(), s.length()); }
221-
MetaString& operator=(const MetaString& m) { return set(m); }
222-
char* getBuffer(const FB_SIZE_T l);
223-
224-
FB_SIZE_T length() const { return count; }
225-
const char* c_str() const { return data; }
226-
const char* nullStr() const { return (count == 0 ? NULL : data); }
227-
bool isEmpty() const { return count == 0; }
228-
bool hasData() const { return count != 0; }
229-
230-
char& operator[](unsigned n) { return data[n]; }
231-
char operator[](unsigned n) const { return data[n]; }
232-
233-
const char* begin() const { return data; }
234-
const char* end() const { return data + count; }
235-
236-
int compare(const char* s, FB_SIZE_T l) const;
237-
int compare(const char* s) const { return compare(s, s ? fb_strlen(s) : 0); }
238-
int compare(const AbstractString& s) const { return compare(s.c_str(), s.length()); }
239-
int compare(const MetaString& m) const { return memcmp(data, m.data, MAX_SQL_IDENTIFIER_SIZE); }
216+
MetaString& assign(const char* s, FB_SIZE_T l) noexcept;
217+
MetaString& assign(const char* s) noexcept { return assign(s, s ? fb_strlen(s) : 0); }
218+
MetaString& clear() noexcept { return assign(nullptr, 0); }
219+
MetaString& operator=(const char* s) noexcept { return assign(s); }
220+
MetaString& operator=(const AbstractString& s) noexcept { return assign(s.c_str(), s.length()); }
221+
MetaString& operator=(const MetaString& m) noexcept { return set(m); }
222+
char* getBuffer(const FB_SIZE_T l) noexcept;
223+
224+
FB_SIZE_T length() const noexcept { return count; }
225+
const char* c_str() const noexcept { return data; }
226+
const char* nullStr() const noexcept { return (count == 0 ? NULL : data); }
227+
bool isEmpty() const noexcept { return count == 0; }
228+
bool hasData() const noexcept { return count != 0; }
229+
230+
char& operator[](unsigned n) noexcept { return data[n]; }
231+
char operator[](unsigned n) const noexcept { return data[n]; }
232+
233+
const char* begin() const noexcept { return data; }
234+
const char* end() const noexcept { return data + count; }
235+
236+
int compare(const char* s, FB_SIZE_T l) const noexcept;
237+
int compare(const char* s) const noexcept { return compare(s, s ? fb_strlen(s) : 0); }
238+
int compare(const AbstractString& s) const noexcept { return compare(s.c_str(), s.length()); }
239+
int compare(const MetaString& m) const noexcept { return memcmp(data, m.data, MAX_SQL_IDENTIFIER_SIZE); }
240240

241241
string toQuotedString() const
242242
{
243243
return Firebird::toQuotedString(*this);
244244
}
245245

246-
bool operator==(const char* s) const { return compare(s) == 0; }
247-
bool operator!=(const char* s) const { return compare(s) != 0; }
248-
bool operator==(const AbstractString& s) const { return compare(s) == 0; }
249-
bool operator!=(const AbstractString& s) const { return compare(s) != 0; }
250-
bool operator==(const MetaString& m) const { return compare(m) == 0; }
251-
bool operator!=(const MetaString& m) const { return compare(m) != 0; }
252-
bool operator<=(const MetaString& m) const { return compare(m) <= 0; }
253-
bool operator>=(const MetaString& m) const { return compare(m) >= 0; }
254-
bool operator< (const MetaString& m) const { return compare(m) < 0; }
255-
bool operator> (const MetaString& m) const { return compare(m) > 0; }
246+
bool operator==(const char* s) const noexcept { return compare(s) == 0; }
247+
bool operator!=(const char* s) const noexcept { return compare(s) != 0; }
248+
bool operator==(const AbstractString& s) const noexcept { return compare(s) == 0; }
249+
bool operator!=(const AbstractString& s) const noexcept { return compare(s) != 0; }
250+
bool operator==(const MetaString& m) const noexcept { return compare(m) == 0; }
251+
bool operator!=(const MetaString& m) const noexcept { return compare(m) != 0; }
252+
bool operator<=(const MetaString& m) const noexcept { return compare(m) <= 0; }
253+
bool operator>=(const MetaString& m) const noexcept { return compare(m) >= 0; }
254+
bool operator< (const MetaString& m) const noexcept { return compare(m) < 0; }
255+
bool operator> (const MetaString& m) const noexcept { return compare(m) > 0; }
256256

257257
void printf(const char*, ...);
258258
FB_SIZE_T copyTo(char* to, FB_SIZE_T toSize) const;
259259

260260
protected:
261-
static void adjustLength(const char* const s, FB_SIZE_T& l);
261+
static void adjustLength(const char* const s, FB_SIZE_T& l) noexcept;
262262
};
263263

264264
} // namespace Firebird

0 commit comments

Comments
 (0)