Commit f095cc95 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Merge branch 'ordinal' into 'master'

Ordinal

See merge request !43
parents 521219ba 43da4b87
Pipeline #14002 passed with stages
in 7 minutes and 47 seconds
......@@ -142,4 +142,6 @@ struct std::tm utc_to_time(const std::string &time, int &zone, int &daylight)
return res;
}
int ordinal(char c) { return (unsigned char)c; }
} // namespace radix
......@@ -17,6 +17,21 @@
namespace radix
{
/**
* @brief ordinal convert character to its ordinal value
* @param c char
* @return integer
*/
int RADIX_PUBLIC ordinal(char c);
/**
* @brief itoa converts integer into character
* @param number integer must be within character bounds
* @return char
*/
template <typename type = int>
char RADIX_PUBLIC itoa(type number);
/**
* @brief utc_to_time UTC time to
* @param time UTC formatted date and time
......
......@@ -33,4 +33,16 @@ std::string join(const std::string &delim, const T &x)
}
return ss.str();
}
template <typename type>
char itoa(type number)
{
char c[2];
c[0] = '0';
c[1] = '\0'; // null
if (number > type(255)) return c[0];
if (number < type(0)) return c[0];
sprintf(c, "%c", number);
return c[0];
}
} // namespace radix
......@@ -104,3 +104,36 @@ TEST(Split, NoDelim)
ASSERT_EQ(1, x.size());
EXPECT_EQ("nope", x[0]);
}
TEST(Radix, OrdinalItoa)
{
{
char c = itoa(127);
EXPECT_EQ('\x7F', c);
int i = ordinal(c);
EXPECT_EQ(127, i);
}
{
char c = itoa(0); // null
EXPECT_EQ('\0', c);
int i = ordinal(c);
EXPECT_EQ(0, i);
}
{
char c = itoa(99);
EXPECT_EQ('c', c);
int i = ordinal(c);
EXPECT_EQ(99, i);
}
{
char c = itoa(48);
EXPECT_EQ('0', c);
int i = ordinal(c);
EXPECT_EQ(48, i);
}
{
char c = itoa(140);
EXPECT_EQ('\x8C', c);
int i = ordinal(c);
EXPECT_EQ(140, i);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment