From 712f1984ac3ae48131cf2f94102c77a9352995e1 Mon Sep 17 00:00:00 2001 From: Maxim Samsonov Date: Wed, 5 Jun 2024 23:22:40 +0300 Subject: [PATCH] Test 15 --- include/sexpp/sexp.h | 140 ++++++++++++++++++++++++++----------------- 1 file changed, 84 insertions(+), 56 deletions(-) diff --git a/include/sexpp/sexp.h b/include/sexpp/sexp.h index 3bba921..8afef1d 100644 --- a/include/sexpp/sexp.h +++ b/include/sexpp/sexp.h @@ -44,6 +44,90 @@ #include "sexp-public.h" #include "sexp-error.h" +using octet_t = uint8_t; + +namespace sexp { + +struct octet_traits : public std::char_traits { + typedef octet_t char_type; + typedef int int_type; + typedef std::streampos pos_type; + typedef std::streamoff off_type; + typedef mbstate_t state_type; + + static void assign(char_type &__c1, const char_type &__c2) noexcept { __c1 = __c2; } + + static constexpr bool eq(const char_type &__c1, const char_type &__c2) noexcept + { + return __c1 == __c2; + } + + static constexpr bool lt(const char_type &__c1, const char_type &__c2) noexcept + { + return __c1 < __c2; + } + + static int compare(const char_type *__s1, const char_type *__s2, size_t __n) + { + return memcmp(__s1, __s2, __n); + } + + static size_t length(const char_type *__s) + { + return strlen(reinterpret_cast(__s)); + } + + static const char_type *find(const char_type *__s, size_t __n, const char_type &__a) + { + return static_cast(memchr(__s, __a, __n)); + } + + static char_type *move(char_type *__s1, const char_type *__s2, size_t __n) + { + return static_cast(memmove(__s1, __s2, __n)); + } + + static char_type *copy(char_type *__s1, const char_type *__s2, size_t __n) + { + return static_cast(memcpy(__s1, __s2, __n)); + } + + static char_type *assign(char_type *__s, size_t __n, char_type __a) + { + return static_cast(memset(__s, __a, __n)); + } + + static constexpr char_type to_char_type(const int_type &__c) noexcept + { + return static_cast(__c); + } + + // To keep both the byte 0xff and the eof symbol 0xffffffff + // from ending up as 0xffffffff. + static constexpr int_type to_int_type(const char_type &__c) noexcept + { + return static_cast(static_cast(__c)); + } + + static constexpr bool eq_int_type(const int_type &__c1, const int_type &__c2) noexcept + { + return __c1 == __c2; + } + + static constexpr int_type eof() noexcept + { + return static_cast(0xFFFFFFFF); + } + + static constexpr int_type not_eof(const int_type &__c) noexcept + { + return (__c == eof()) ? 0 : __c; + } + +}; +} + + namespace sexp { /* @@ -99,62 +183,6 @@ class sexp_input_stream_t; * SEXP simple string */ -using octet_t = uint8_t; - -struct octet_traits : std::char_traits { - 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(EOF); } - - static int_type not_eof(const int_type &c) { return eq_int_type(c, eof()) ? 0 : c; } -}; - using octet_string = std::basic_string; class SEXP_PUBLIC_SYMBOL sexp_simple_string_t : public octet_string, private sexp_char_defs_t {