Skip to content

Commit

Permalink
layer: Add string to int
Browse files Browse the repository at this point in the history
  • Loading branch information
christophe-lunarg committed Aug 3, 2023
1 parent a191ca0 commit 55197ff
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/layer/layer_settings_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,46 @@ std::string ToUpper(const std::string &s) {
return result;
}

uint32_t ToUint32(const std::string &token) {
uint32_t int_id = 0;
if ((token.find("0x") == 0) || token.find("0X") == 0) { // Handle hex format
int_id = static_cast<uint32_t>(std::strtoul(token.c_str(), nullptr, 16));
} else {
int_id = static_cast<uint32_t>(std::strtoul(token.c_str(), nullptr, 10)); // Decimal format
}
return int_id;
}

uint64_t ToUint64(const std::string &token) {
uint64_t int_id = 0;
if ((token.find("0x") == 0) || token.find("0X") == 0) { // Handle hex format
int_id = static_cast<uint64_t>(std::strtoull(token.c_str(), nullptr, 16));
} else {
int_id = static_cast<uint64_t>(std::strtoull(token.c_str(), nullptr, 10)); // Decimal format
}
return int_id;
}

int32_t ToInt32(const std::string &token) {
int32_t int_id = 0;
if (token.find("0x") == 0 || token.find("0X") == 0 || token.find("-0x") == 0 || token.find("-0X") == 0) { // Handle hex format
int_id = static_cast<int32_t>(std::strtol(token.c_str(), nullptr, 16));
} else {
int_id = static_cast<int32_t>(std::strtol(token.c_str(), nullptr, 10)); // Decimal format
}
return int_id;
}

int64_t ToInt64(const std::string &token) {
int64_t int_id = 0;
if (token.find("0x") == 0 || token.find("0X") == 0 || token.find("-0x") == 0 || token.find("-0X") == 0) { // Handle hex format
int_id = static_cast<uint64_t>(std::strtoll(token.c_str(), nullptr, 16));
} else {
int_id = static_cast<uint64_t>(std::strtoll(token.c_str(), nullptr, 10)); // Decimal format
}
return int_id;
}

VkFrameset ToFrameSet(const std::string &s) {
assert(IsFrameSets(s));

Expand Down
8 changes: 8 additions & 0 deletions src/layer/layer_settings_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ namespace vl {

std::string ToUpper(const std::string &s);

uint32_t ToUint32(const std::string &token);

uint64_t ToUint64(const std::string &token);

int32_t ToInt32(const std::string &token);

int64_t ToInt64(const std::string &token);

bool IsFrameSets(const std::string &s);

VkFrameset ToFrameSet(const std::string &s);
Expand Down
49 changes: 49 additions & 0 deletions tests/layer/test_setting_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,55 @@ TEST(test_layer_settings_util, is_float) {
EXPECT_EQ(false, vl::IsFloat("A"));
}

TEST(test_layer_settings_util, ToUint32) {
EXPECT_EQ(24, vl::ToUint32("24"));
EXPECT_EQ(3000300000, vl::ToUint32("3000300000"));
EXPECT_EQ(15, vl::ToUint32("0xF"));
EXPECT_EQ(15, vl::ToUint32("0XF"));
EXPECT_EQ(4294967295, vl::ToUint32("0xFFFFFFFF"));
}

TEST(test_layer_settings_util, ToUint64) {
EXPECT_EQ(24, vl::ToUint64("24"));
EXPECT_EQ(3000300000, vl::ToUint64("3000300000"));
EXPECT_EQ(15, vl::ToUint64("0xF"));
EXPECT_EQ(15, vl::ToUint64("0XF"));
EXPECT_EQ(4294967295, vl::ToUint64("0xFFFFFFFF"));
EXPECT_EQ(4294967296ull, vl::ToUint64("0x100000000"));
}

TEST(test_layer_settings_util, ToInt32) {
EXPECT_EQ(24, vl::ToInt32("24"));
EXPECT_EQ(-24, vl::ToInt32("-24"));
EXPECT_EQ(2147483647, vl::ToInt32("2147483647"));
EXPECT_EQ(-2147483648, vl::ToInt32("-2147483648"));
EXPECT_EQ(2147483647, vl::ToInt32("2147483650"));
EXPECT_EQ(-2147483648, vl::ToInt32("-2147483650"));
EXPECT_EQ(2147483647, vl::ToInt32("0xFFFFFFFF"));
EXPECT_EQ(-2147483648, vl::ToInt32("-0xFFFFFFFF"));
EXPECT_EQ(15, vl::ToInt32("0xF"));
EXPECT_EQ(-15, vl::ToInt32("-0xF"));
EXPECT_EQ(15, vl::ToInt32("0XF"));
EXPECT_EQ(-15, vl::ToInt32("-0XF"));
}

TEST(test_layer_settings_util, ToInt64) {
EXPECT_EQ(24LL, vl::ToInt64("24"));
EXPECT_EQ(-24LL, vl::ToInt64("-24"));
EXPECT_EQ(2147483647LL, vl::ToInt64("2147483647"));
EXPECT_EQ(-2147483648LL, vl::ToInt64("-2147483648"));
EXPECT_EQ(2147483650LL, vl::ToInt64("2147483650"));
EXPECT_EQ(-2147483650LL, vl::ToInt64("-2147483650"));
EXPECT_EQ(4294967295LL, vl::ToInt64("0xFFFFFFFF"));
EXPECT_EQ(-4294967295LL, vl::ToInt64("-0xFFFFFFFF"));
EXPECT_EQ(4294967296LL, vl::ToInt64("0x100000000"));
EXPECT_EQ(-4294967296LL, vl::ToInt64("-0x100000000"));
EXPECT_EQ(15LL, vl::ToInt64("0xF"));
EXPECT_EQ(-15LL, vl::ToInt64("-0xF"));
EXPECT_EQ(15LL, vl::ToInt64("0XF"));
EXPECT_EQ(-15LL, vl::ToInt64("-0XF"));
}

TEST(test_layer_settings_util, is_framesets) {
EXPECT_EQ(true, vl::IsFrameSets("0"));
EXPECT_EQ(true, vl::IsFrameSets("0-2"));
Expand Down

0 comments on commit 55197ff

Please sign in to comment.