Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
cta
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dCache
cta
Commits
84ef13de
Commit
84ef13de
authored
5 years ago
by
Steven Murray
Browse files
Options
Downloads
Patches
Plain Diff
Added utils::toUint32()
parent
66e76620
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
common/utils/UtilsTest.cpp
+45
-0
45 additions, 0 deletions
common/utils/UtilsTest.cpp
common/utils/utils.cpp
+36
-0
36 additions, 0 deletions
common/utils/utils.cpp
common/utils/utils.hpp
+8
-0
8 additions, 0 deletions
common/utils/utils.hpp
with
89 additions
and
0 deletions
common/utils/UtilsTest.cpp
+
45
−
0
View file @
84ef13de
...
...
@@ -321,6 +321,51 @@ TEST_F(cta_UtilsTest, toUint16_too_big) {
ASSERT_THROW
(
utils
::
toUint16
(
"65536"
),
std
::
exception
);
}
TEST_F
(
cta_UtilsTest
,
toUint32_12345
)
{
using
namespace
cta
;
uint32_t
i
=
0
;
ASSERT_NO_THROW
(
i
=
utils
::
toUint32
(
"12345"
));
ASSERT_EQ
((
uint32_t
)
12345
,
i
);
}
TEST_F
(
cta_UtilsTest
,
toUint32_zero
)
{
using
namespace
cta
;
uint32_t
i
=
0
;
ASSERT_NO_THROW
(
i
=
utils
::
toUint32
(
"0"
));
ASSERT_EQ
((
uint32_t
)
0
,
i
);
}
TEST_F
(
cta_UtilsTest
,
toUint32_4294967295
)
{
using
namespace
cta
;
uint32_t
i
=
0
;
ASSERT_NO_THROW
(
i
=
utils
::
toUint32
(
"4294967295"
));
ASSERT_EQ
((
uint32_t
)
4294967295
,
i
);
}
TEST_F
(
cta_UtilsTest
,
toUint32_empty_string
)
{
using
namespace
cta
;
ASSERT_THROW
(
utils
::
toUint32
(
""
),
std
::
exception
);
}
TEST_F
(
cta_UtilsTest
,
toUint32_negative
)
{
using
namespace
cta
;
ASSERT_THROW
(
utils
::
toUint32
(
"-12345"
),
std
::
exception
);
}
TEST_F
(
cta_UtilsTest
,
toUint32_too_big
)
{
using
namespace
cta
;
ASSERT_THROW
(
utils
::
toUint32
(
"4294967296"
),
std
::
exception
);
}
TEST_F
(
cta_UtilsTest
,
toUid_12345
)
{
using
namespace
cta
;
...
...
This diff is collapsed.
Click to expand it.
common/utils/utils.cpp
+
36
−
0
View file @
84ef13de
...
...
@@ -524,6 +524,42 @@ uint16_t toUint16(const std::string &str) {
return
value
;
}
//------------------------------------------------------------------------------
// toUint32
//------------------------------------------------------------------------------
uint32_t
toUint32
(
const
std
::
string
&
str
)
{
if
(
str
.
empty
())
{
std
::
ostringstream
msg
;
msg
<<
"Failed to convert empty string to uint32_t: An empty string is not"
" a valid unsigned integer"
;
throw
exception
::
Exception
(
msg
.
str
());
}
errno
=
0
;
const
long
int
value
=
strtol
(
str
.
c_str
(),
(
char
**
)
NULL
,
10
);
const
int
savedErrno
=
errno
;
if
(
savedErrno
)
{
std
::
ostringstream
msg
;
msg
<<
"Failed to convert
\'
"
<<
str
<<
"' to uint32_t: "
<<
errnoToString
(
savedErrno
);
throw
exception
::
Exception
(
msg
.
str
());
}
if
(
0
>
value
)
{
std
::
ostringstream
msg
;
msg
<<
"Failed to convert
\'
"
<<
str
<<
"' to uint32_t: Negative number"
;
throw
exception
::
Exception
(
msg
.
str
());
}
if
(
4294967295
<
value
)
{
std
::
ostringstream
msg
;
msg
<<
"Failed to convert
\'
"
<<
str
<<
"' to uint32_t: Number too big"
;
throw
exception
::
Exception
(
msg
.
str
());
}
return
value
;
}
//------------------------------------------------------------------------------
// toUid
//------------------------------------------------------------------------------
...
...
This diff is collapsed.
Click to expand it.
common/utils/utils.hpp
+
8
−
0
View file @
84ef13de
...
...
@@ -247,6 +247,14 @@ namespace utils {
*/
uint16_t
toUint16
(
const
std
::
string
&
str
);
/**
* Converts the specified string to an unsigned integer.
*
* @param str The string.
* @return The unsigned integer.
*/
uint32_t
toUint32
(
const
std
::
string
&
str
);
/**
* Converts the specified string to a uid.
*
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment