Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added utxx::find_pos() and nchar::len()

  • Loading branch information...
commit 16a4fd1b0c35ea50cf37271a4a82cfdd7e7f319c 1 parent 1e474af
@saleyn authored
View
11 include/utxx/nchar.hpp
@@ -42,6 +42,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <string.h>
#include <utxx/endian.hpp>
#include <utxx/convert.hpp>
+#include <utxx/string.hpp>
namespace utxx {
@@ -100,6 +101,7 @@ namespace detail {
const char* data() const { return m_data; }
char* data() { return m_data; }
size_t size() const { return N; }
+ const char* end() const { return m_data + N; }
operator uint8_t* () const { return reinterpret_cast<uint8_t*>(m_data); }
operator uint8_t* () { return reinterpret_cast<uint8_t*>(m_data); }
@@ -119,6 +121,15 @@ namespace detail {
return std::string(m_data, end - m_data);
}
+ /// Find the length of the string contained in the buffer up to the
+ /// given delimiter.
+ /// @return the string length up to the \a delimiter or size() if
+ /// the \a delimiter is not found
+ size_t len(char delimiter) {
+ const char* pos = find_pos(m_data, end(), delimiter);
+ return pos - m_data;
+ }
+
std::ostream& to_bin_string(std::ostream& out, int until_char = -1) const {
out << "<<" << (int)*(uint8_t*)m_data;
for(const char* p = m_data+1, *end = m_data + N;
View
50 include/utxx/string.hpp
@@ -58,23 +58,63 @@ namespace utxx {
return N-1;
}
- /// Find <s> in the static array of string <choices>.
- /// @return position of <s> in the <choices> array of <def> if string not found.
+ /// Find the position of character \a c.
+ /// @return pointer to the position of character \a c, or \a end if
+ /// no character \a c is found.
+ inline const char* find_pos(const char* str, const char* end, char c) {
+ for (const char* p = str; p != end; ++p)
+ if (*p == c)
+ return p;
+ return end;
+ }
+
+ /// Find \a s in the static array of string \a choices.
+ /// @return position of \a s in the \a choices array or \a def if
+ /// the string not found.
///
template <typename T>
inline T find_index(const char* choices[], size_t n,
- const std::string& s, T def = static_cast<T>(-1)) {
+ const std::string& s, T def = static_cast<T>(-1)) {
for (size_t i=0; i < n; ++i)
if (s == choices[i])
return static_cast<T>(i);
return def;
}
+ /// Find \a value in the static array of string \a choices.
+ /// @return position of \a value in the \a choices array or \a def if
+ /// the string not found.
+ ///
+ template <typename T>
+ inline T find_index(const char* choices[], size_t n,
+ const char* value, size_t len,
+ T def = static_cast<T>(-1)) {
+ for (size_t i=0; i < n; ++i)
+ if (!strncmp(value, choices[i], len))
+ return static_cast<T>(i);
+ return def;
+ }
+
+ /// Find \a value in the static array of string \a choices.
+ /// @return position of \a value in the \a choices array or \a def if
+ /// the string not found.
+ ///
+ template <typename T, int N>
+ inline T find_index(const char* (&choices)[N], const std::string& value,
+ T def = static_cast<T>(-1))
+ {
+ return find_index(choices, N, value, def);
+ }
+
+ /// Find \a value in the static array of string \a choices.
+ /// @return position of \a value in the \a choices array or \a def if
+ /// the string not found.
+ ///
template <typename T, int N>
- inline T find_index(const char* (&choices)[N], const std::string& s,
+ inline T find_index(const char* (&choices)[N], const char* value, size_t len,
T def = static_cast<T>(-1))
{
- return find_index(choices, N, s, def);
+ return find_index(choices, N, value, len, def);
}
template <typename T, int N>
View
7 test/test_nchar.cpp
@@ -88,6 +88,13 @@ BOOST_AUTO_TEST_CASE( test_nchar )
BOOST_REQUIRE_EQUAL(" abc ", rc.to_string());
BOOST_REQUIRE_EQUAL(" abc", rc.to_string(' '));
}
+
+ {
+ nchar<6> rc("abc\n ", 6);
+ BOOST_REQUIRE_EQUAL(3u, rc.len('\n'));
+ BOOST_REQUIRE_EQUAL(6u, rc.len('\0'));
+ BOOST_REQUIRE_EQUAL(6u, rc.len('X'));
+ }
}
BOOST_AUTO_TEST_CASE( test_nchar_to_binary )
View
19 test/test_string.cpp
@@ -39,24 +39,35 @@ using namespace utxx;
BOOST_AUTO_TEST_CASE( test_string_length )
{
const char s[] = "abc";
- BOOST_REQUIRE_EQUAL(3, length(s));
+ BOOST_REQUIRE_EQUAL(3u, length(s));
static const char* s_ops[] = {"a", "b", "c"};
- BOOST_REQUIRE_EQUAL(3, length(s_ops));
+ BOOST_REQUIRE_EQUAL(3u, length(s_ops));
struct t { int a; t(int a) : a(a) {} };
const t arr[] = { t(1), t(2) };
- BOOST_REQUIRE_EQUAL(2, length(arr));
+ BOOST_REQUIRE_EQUAL(2u, length(arr));
}
enum op_type {OP_UNDEFINED = -1, OP_ADD, OP_REMOVE, OP_REPLACE, OP_UPDATE};
+BOOST_AUTO_TEST_CASE( test_string_find_pos )
+{
+ static const char* s_test = "abc\n ";
+ static const char* s_end = s_test + 7;
+ static const char* s_expect = s_test + 3;
+ BOOST_REQUIRE_EQUAL(s_expect, utxx::find_pos(s_test, s_end, '\n'));
+ BOOST_REQUIRE_EQUAL(s_end, utxx::find_pos(s_test, s_end, 'X'));
+ BOOST_REQUIRE_EQUAL(s_end, utxx::find_pos(s_test, s_end, '\0'));
+}
+
BOOST_AUTO_TEST_CASE( test_string_find_index )
{
static const char* s_ops[] = {"add", "remove", "replace", "update"};
- BOOST_REQUIRE(OP_REMOVE == utxx::find_index<op_type>(s_ops, "remove"));
+ BOOST_REQUIRE(OP_REMOVE == utxx::find_index<op_type>(s_ops, "remove ", 6));
BOOST_REQUIRE(OP_REPLACE == utxx::find_index<op_type>(s_ops, sizeof(s_ops), "replace"));
+ BOOST_REQUIRE(OP_REPLACE == utxx::find_index<op_type>(s_ops, sizeof(s_ops), "replace ", 7));
BOOST_REQUIRE(OP_UNDEFINED == utxx::find_index<op_type>(s_ops, "xxx"));
}
Please sign in to comment.
Something went wrong with that request. Please try again.