Skip to content

Commit

Permalink
Implemented char_traits for SEXP octet_t
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Jun 3, 2024
1 parent 92ce195 commit d5f9bae
Showing 1 changed file with 70 additions and 4 deletions.
74 changes: 70 additions & 4 deletions include/sexpp/sexp.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,80 @@ class sexp_input_stream_t;
* SEXP simple string
*/

typedef uint8_t octet_t;
//typedef uint8_t octet_t;

class SEXP_PUBLIC_SYMBOL sexp_simple_string_t : public std::basic_string<octet_t>,
using octet_t = uint8_t;

struct octet_traits : std::char_traits<octet_t> {
static void assign(char_type& r, const char_type& a) {
r = a;
}

static bool eq(const char_type& c1, const char_type& c2) {
return c1 == c2;
}

static bool lt(const char_type& c1, const char_type& c2) {
return c1 < c2;
}

static int compare(const char_type* s1, const char_type* s2, std::size_t n) {
while ( n-- != 0 ) {
if ( *s1 < *s2 ) return -1;
if ( *s2++ < *s1++ ) return 1;
}
return 0;
}

static const char_type* find(const char_type* s, std::size_t n, const char_type& a) {
while ( n-- != 0 ) {
if ( *s == a ) return s;
s++;
}
return nullptr;
}

static char_type* move(char_type* s1, const char_type* s2, std::size_t n) {
return (char_type*)std::memmove(s1, s2, n);
}

static char_type* copy(char_type* s1, const char_type* s2, std::size_t n) {
return (char_type*)std::memcpy(s1, s2, n);
}

static char_type* assign(char_type* s, std::size_t n, char_type a) {
return (char_type*)std::memset(s, a, n);
}

static char_type to_char_type(const int_type& c) {
return char_type(c);
}

static int_type to_int_type(const char_type& c) {
return int_type(c);
}

static bool eq_int_type(const int_type& c1, const int_type& c2) {
return c1 == c2;
}

static int_type eof() {
return static_cast<int_type>(EOF);
}

static int_type not_eof(const int_type& c) {
return eq_int_type(c, eof()) ? 0 : c;
}
};

using octet_string = std::basic_string<octet_t, octet_traits>;

class SEXP_PUBLIC_SYMBOL sexp_simple_string_t : public octet_string,
private sexp_char_defs_t {
public:
sexp_simple_string_t(void) = default;
sexp_simple_string_t(const octet_t *dt) : std::basic_string<octet_t>{dt} {}
sexp_simple_string_t(const octet_t *bt, size_t ln) : std::basic_string<octet_t>{bt, ln} {}
sexp_simple_string_t(const octet_t *dt) : octet_string{dt} {}
sexp_simple_string_t(const octet_t *bt, size_t ln) : octet_string{bt, ln} {}
sexp_simple_string_t &append(int c)
{
(*this) += (octet_t)(c & 0xFF);
Expand Down

0 comments on commit d5f9bae

Please sign in to comment.