From 9e8adf8438d3ca343a9161725e6f0ff68e44832c Mon Sep 17 00:00:00 2001 From: Timo Weingärtner Date: Tue, 2 Jul 2013 01:32:13 +0200 Subject: rfc4251string: implement name-list (as vector) --- rfc4251.h | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/rfc4251.h b/rfc4251.h index 3365064..3f442f1 100644 --- a/rfc4251.h +++ b/rfc4251.h @@ -5,7 +5,7 @@ * rfc4251bool bool * rfc4251uint32 uint32 * rfc4251uint64 uint64 - * rfc4251string string, incl. mpint and (without splitting) name-list + * rfc4251string string, incl. mpint and name-list * * those structs contain the objects in their RFC 4251 representation, * conversions are provided via constructors and cast operators @@ -168,6 +168,7 @@ struct rfc4251string { inline explicit rfc4251string (char const *); inline explicit rfc4251string (char const *, size_t); inline explicit rfc4251string (std::string const & s) : rfc4251string{s.data(), s.size()} {} + inline explicit rfc4251string (std::vector const &); inline explicit rfc4251string (mpz_srcptr); inline explicit rfc4251string (mpz_class const & x) : rfc4251string{x.get_mpz_t()} {} @@ -175,6 +176,7 @@ struct rfc4251string { rfc4251string & operator= (rfc4251string &&) = default; inline operator std::string () const; + inline operator std::vector () const; inline operator mpz_class () const; }; @@ -187,6 +189,16 @@ inline rfc4251string::rfc4251string (char const * s, size_t l) { value.insert(value.end(), s, s + l); } +inline rfc4251string::rfc4251string (std::vector const & v) { + if (v.size()) { + value.assign(v.begin()->data(), v.begin()->data() + v.begin()->size()); + for (auto it = v.begin() + 1; it != v.end(); ++it) { + value.push_back(','); + value.insert(value.end(), it->data(), it->data() + it->size()); + } + } +} + inline rfc4251string::rfc4251string (mpz_srcptr x) { if (mpz_sgn(x) == 0) { } else if (mpz_sgn(x) == 1) { @@ -215,6 +227,23 @@ inline rfc4251string::operator std::string () const { return std::string(value.begin(), value.end()); } +inline rfc4251string::operator std::vector () const { + std::vector ret; + auto name_start = value.begin(); + if (name_start != value.end()) + for (auto it = name_start; ; ++it) { + if (it == value.end() or *it == ',') { + if (it == name_start) + throw std::length_error("name of zero length"); + ret.emplace_back(name_start, it); + name_start = it + 1; + } + if (it == value.end()) + break; + } + return ret; +} + inline rfc4251string::operator mpz_class () const { mpz_class ret; mpz_import(ret.get_mpz_t(), value.size(), 1, 1, 1, 0, value.data()); -- cgit v1.2.3