From 241ae8a538fd0695fc8e0350f43e329dea5d32c1 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Fri, 20 Mar 2015 23:37:42 +0300 Subject: [PATCH] Array methods for case conversion --- libuavcan/include/uavcan/marshal/array.hpp | 32 ++++++++++++++++++++++ libuavcan/test/marshal/array.cpp | 18 ++++++++++++ 2 files changed, 50 insertions(+) diff --git a/libuavcan/include/uavcan/marshal/array.hpp b/libuavcan/include/uavcan/marshal/array.hpp index 70f1c27d97..46215e311b 100644 --- a/libuavcan/include/uavcan/marshal/array.hpp +++ b/libuavcan/include/uavcan/marshal/array.hpp @@ -742,6 +742,38 @@ public: } } + /** + * Converts the string to upper/lower case in place, assuming that encoding is ASCII. + * These methods can only be used with string-like arrays; otherwise compilation will fail. + */ + void convertToUpperCaseASCII() + { + StaticAssert::check(); + + for (SizeType i = 0; i < size(); i++) + { + const int x = Base::at(i); + if ((x <= 'z') && (x >= 'a')) + { + Base::at(i) = static_cast(x + ('Z' - 'z')); + } + } + } + + void convertToLowerCaseASCII() + { + StaticAssert::check(); + + for (SizeType i = 0; i < size(); i++) + { + const int x = Base::at(i); + if ((x <= 'Z') && (x >= 'A')) + { + Base::at(i) = static_cast(x - ('Z' - 'z')); + } + } + } + /** * Fills this array as a packed square matrix from a static array. * Please refer to the specification to learn more about matrix packing. diff --git a/libuavcan/test/marshal/array.cpp b/libuavcan/test/marshal/array.cpp index e0402ec0cc..f3dfebb3ad 100644 --- a/libuavcan/test/marshal/array.cpp +++ b/libuavcan/test/marshal/array.cpp @@ -1278,3 +1278,21 @@ TEST(Array, FuzzyComparison) uavcan::YamlStreamer::stream(std::cout, array_d64, 0); std::cout << std::endl; } + +TEST(Array, CaseConversion) +{ + Array, ArrayModeDynamic, 30> str; + + str.convertToLowerCaseASCII(); + str.convertToUpperCaseASCII(); + + ASSERT_STREQ("", str.c_str()); + + str = "Hello World!"; + + ASSERT_STREQ("Hello World!", str.c_str()); + str.convertToLowerCaseASCII(); + ASSERT_STREQ("hello world!", str.c_str()); + str.convertToUpperCaseASCII(); + ASSERT_STREQ("HELLO WORLD!", str.c_str()); +}