Skip to content

Commit 94c1f94

Browse files
committed
Use named constants instead of magic numbers. Ignore backslash on POSIX.
1 parent 6e7a318 commit 94c1f94

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

src/common/os/path_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ class PathUtils
4747

4848
/// String used to point to current directory
4949
static const char* curr_dir_link;
50+
static const size_t curr_dir_link_len;
5051

5152
/// String used to point to parent directory
5253
static const char* up_dir_link;
54+
static const size_t up_dir_link_len;
5355

5456
/// The directory list separator for the platform.
5557
static const char dir_list_sep;

src/common/os/posix/path_utils.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const char PathUtils::dir_sep = '/';
3838
const char* PathUtils::curr_dir_link = ".";
3939
const char* PathUtils::up_dir_link = "..";
4040
const char PathUtils::dir_list_sep = ':';
41+
const size_t PathUtils::curr_dir_link_len = strlen(curr_dir_link);
42+
const size_t PathUtils::up_dir_link_len = strlen(up_dir_link);
4143

4244
class PosixDirItr : public PathUtils::dir_iterator
4345
{
@@ -148,7 +150,7 @@ void PathUtils::concatPath(Firebird::PathName& result,
148150

149151
for (Firebird::PathName::size_type pos = 0; cur_pos < second.length(); cur_pos = pos + 1)
150152
{
151-
pos = second.find_first_of("/\\", cur_pos);
153+
pos = second.find(dir_sep, cur_pos);
152154
if (pos == Firebird::PathName::npos) // simple name, simple handling
153155
{
154156
pos = second.length();
@@ -157,16 +159,16 @@ void PathUtils::concatPath(Firebird::PathName& result,
157159
{
158160
continue;
159161
}
160-
if (pos == cur_pos + 1 && memcmp(second.c_str() + cur_pos, PathUtils::curr_dir_link, 1) == 0) // Current dir, ignore
162+
if (pos == cur_pos + curr_dir_link_len && memcmp(second.c_str() + cur_pos, curr_dir_link, curr_dir_link_len) == 0) // Current dir, ignore
161163
{
162164
continue;
163165
}
164-
if (pos == cur_pos + 2 && memcmp(second.c_str() + cur_pos, PathUtils::up_dir_link, 2) == 0) // One dir up
166+
if (pos == cur_pos + up_dir_link_len && memcmp(second.c_str() + cur_pos, up_dir_link, up_dir_link_len) == 0) // One dir up
165167
{
166168
if (result.length() < 2) // We have nothing to cut off, ignore this piece (may be throw an error?..)
167169
continue;
168170

169-
const Firebird::PathName::size_type up_dir = result.find_last_of("/\\", result.length() - 2, 2);
171+
const Firebird::PathName::size_type up_dir = result.rfind(dir_sep, result.length() - 2);
170172
if (up_dir == Firebird::PathName::npos)
171173
continue;
172174

src/common/os/win32/path_utils.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const char PathUtils::dir_sep = '\\';
1010
const char* PathUtils::curr_dir_link = ".";
1111
const char* PathUtils::up_dir_link = "..";
1212
const char PathUtils::dir_list_sep = ';';
13+
const size_t PathUtils::curr_dir_link_len = strlen(curr_dir_link);
14+
const size_t PathUtils::up_dir_link_len = strlen(up_dir_link);
1315

1416
class Win32DirItr : public PathUtils::dir_iterator
1517
{
@@ -121,7 +123,10 @@ void PathUtils::concatPath(Firebird::PathName& result,
121123

122124
for (Firebird::PathName::size_type pos = 0; cur_pos < second.length(); cur_pos = pos + 1)
123125
{
124-
pos = second.find_first_of("/\\", cur_pos);
126+
static const char separators[] = "/\\";
127+
static const Firebird::PathName::size_type separatorsLen = static_cast<Firebird::PathName::size_type>(strlen(separators));
128+
129+
pos = second.find_first_of(separators, cur_pos, separatorsLen);
125130
if (pos == Firebird::PathName::npos) // simple name, simple handling
126131
{
127132
pos = second.length();
@@ -130,16 +135,16 @@ void PathUtils::concatPath(Firebird::PathName& result,
130135
{
131136
continue;
132137
}
133-
if (pos == cur_pos + 1 && memcmp(second.c_str() + cur_pos, PathUtils::curr_dir_link, 1) == 0) // Current dir, ignore
138+
if (pos == cur_pos + curr_dir_link_len && memcmp(second.c_str() + cur_pos, curr_dir_link, curr_dir_link_len) == 0) // Current dir, ignore
134139
{
135140
continue;
136141
}
137-
if (pos == cur_pos + 2 && memcmp(second.c_str() + cur_pos, PathUtils::up_dir_link, 2) == 0) // One dir up
142+
if (pos == cur_pos + up_dir_link_len && memcmp(second.c_str() + cur_pos, up_dir_link, up_dir_link_len) == 0) // One dir up
138143
{
139144
if (result.length() < 2) // We have nothing to cut off, ignore this piece (may be throw an error?..)
140145
continue;
141146

142-
const Firebird::PathName::size_type up_dir = result.find_last_of("/\\", result.length() - 2, 2);
147+
const Firebird::PathName::size_type up_dir = result.find_last_of(separators, result.length() - 2, separatorsLen);
143148
if (up_dir == Firebird::PathName::npos)
144149
continue;
145150

0 commit comments

Comments
 (0)