Skip to content

Commit d1323c5

Browse files
Merge pull request #606 from IENT/addToIntFunctionsAndTests
Add conversion function for int and tests for all
2 parents 5750251 + 9815cd6 commit d1323c5

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

YUViewLib/src/common/Functions.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,21 @@ ByteVector readData(std::istream &istream, const size_t nrBytes)
208208
return data;
209209
}
210210

211-
std::optional<unsigned long> toUnsigned(const std::string_view text)
211+
std::optional<unsigned> toUnsigned(const std::string_view text)
212+
{
213+
unsigned value{};
214+
const auto result = std::from_chars(text.data(), text.data() + text.size(), value);
215+
216+
if (result.ec != std::errc())
217+
return {};
218+
const auto allCharactersParsed = (result.ptr == &(*text.end()));
219+
if (!allCharactersParsed)
220+
return {};
221+
222+
return value;
223+
}
224+
225+
std::optional<int> toInt(const std::string_view text)
212226
{
213227
int value{};
214228
const auto result = std::from_chars(text.data(), text.data() + text.size(), value);

YUViewLib/src/common/Functions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ template <typename T, typename R> inline T clip(T val, Range<R> range)
9898
return (val < T(range.min)) ? T(range.min) : (val > T(range.max)) ? T(range.max) : val;
9999
}
100100

101-
std::optional<unsigned long> toUnsigned(const std::string_view text);
101+
std::optional<unsigned> toUnsigned(const std::string_view text);
102+
std::optional<int> toInt(const std::string_view text);
102103

103104
} // namespace functions
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* This file is part of YUView - The YUV player with advanced analytics toolset
2+
* <https://github.com/IENT/YUView>
3+
* Copyright (C) 2015 Institut für Nachrichtentechnik, RWTH Aachen University, GERMANY
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* In addition, as a special exception, the copyright holders give
11+
* permission to link the code of portions of this program with the
12+
* OpenSSL library under certain conditions as described in each
13+
* individual source file, and distribute linked combinations including
14+
* the two.
15+
*
16+
* You must obey the GNU General Public License in all respects for all
17+
* of the code used other than OpenSSL. If you modify file(s) with this
18+
* exception, you may extend this exception to your version of the
19+
* file(s), but you are not obligated to do so. If you do not wish to do
20+
* so, delete this exception statement from your version. If you delete
21+
* this exception statement from all source files in the program, then
22+
* also delete it here.
23+
*
24+
* This program is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
* GNU General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU General Public License
30+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
31+
*/
32+
33+
#include <common/Testing.h>
34+
35+
#include <common/Functions.h>
36+
37+
namespace
38+
{
39+
40+
TEST(FunctionsTest, toUnsigned)
41+
{
42+
EXPECT_EQ(functions::toUnsigned("0"), 0);
43+
EXPECT_EQ(functions::toUnsigned("256"), 256);
44+
EXPECT_EQ(functions::toUnsigned("4294967295"), 4294967295);
45+
46+
EXPECT_FALSE(functions::toUnsigned("4294967296"));
47+
EXPECT_FALSE(functions::toUnsigned("-1"));
48+
EXPECT_FALSE(functions::toUnsigned("-256"));
49+
EXPECT_FALSE(functions::toUnsigned("24A"));
50+
EXPECT_FALSE(functions::toUnsigned("A24"));
51+
EXPECT_FALSE(functions::toUnsigned("NotANumber"));
52+
}
53+
54+
TEST(FunctionsTest, toInt)
55+
{
56+
EXPECT_EQ(functions::toInt("0"), 0);
57+
EXPECT_EQ(functions::toInt("256"), 256);
58+
EXPECT_EQ(functions::toInt("2147483647"), 2147483647);
59+
EXPECT_EQ(functions::toInt("-1"), -1);
60+
EXPECT_EQ(functions::toInt("-256"), -256);
61+
EXPECT_EQ(functions::toInt("-2147483648"), -2147483648);
62+
63+
EXPECT_FALSE(functions::toInt("2147483648"));
64+
EXPECT_FALSE(functions::toInt("-2147483649"));
65+
EXPECT_FALSE(functions::toInt("24A"));
66+
EXPECT_FALSE(functions::toInt("A24"));
67+
EXPECT_FALSE(functions::toInt("NotANumber"));
68+
}
69+
70+
} // namespace

0 commit comments

Comments
 (0)