arabica/test/silly_string/silly_string.hpp

103 lines
3.4 KiB
C++
Raw Normal View History

2005-09-08 23:41:36 +02:00
#ifndef ARABICA_TEST_SILLY_STRING_HPP
#define ARABICA_TEST_SILLY_STRING_HPP
#include <string>
#include <functional>
#include <Utils/stringadaptortag.hpp>
2005-12-07 12:23:44 +01:00
#include <SAX/ArabicaConfig.h>
2005-09-08 23:41:36 +02:00
// testing purposes only
// a string with as minimal interface as possible
class silly_string
{
public:
silly_string();
silly_string(const silly_string& rhs);
~silly_string();
bool operator==(const silly_string& rhs) const;
silly_string& operator=(const silly_string& rhs);
private:
// use a std::string to the actual work
std::string s_;
friend class silly_string_adaptor;
}; // class silly_string
class silly_string_adaptor : public Arabica::string_adaptor_tag
2005-09-08 23:41:36 +02:00
{
public:
2005-09-30 23:36:11 +02:00
typedef silly_string string_type;
2005-09-28 22:02:00 +02:00
typedef std::string::const_iterator const_iterator;
2005-09-30 23:36:11 +02:00
typedef std::string::iterator mutable_iterator;
2005-09-08 23:41:36 +02:00
typedef char value_type;
typedef std::string::size_type size_type;
static const size_type npos;
2005-09-30 23:36:11 +02:00
template<class InputIterator>
static silly_string construct(InputIterator from, InputIterator to)
2005-09-28 22:02:00 +02:00
{
2005-09-30 23:36:11 +02:00
silly_string ss;
ss.s_ = std::string(from, to);
return ss;
} // construct
2005-09-28 22:02:00 +02:00
2005-09-30 23:36:11 +02:00
static string_type construct(const value_type* s)
{
silly_string ss;
ss.s_ = std::string(s);
return ss;
} // construct
static char convert_from_utf8(char c);
static silly_string construct_from_utf8(const char* str);
static silly_string construct_from_utf8(const char* str, int length);
#ifndef ARABICA_NO_WCHAR_T
2005-11-17 23:05:06 +01:00
static silly_string construct_from_utf16(const wchar_t* str);
static silly_string construct_from_utf16(const wchar_t* str, int length);
#endif
2005-09-08 23:41:36 +02:00
// here we go
static bool empty(const silly_string& s);
static size_type find(const silly_string& str, const silly_string& what);
2005-09-30 23:36:11 +02:00
static size_type find(const silly_string& str, value_type c);
2005-09-08 23:41:36 +02:00
static silly_string substr(const silly_string& str, const size_type& offset);
static silly_string substr(const silly_string& str, const size_type& offset, const size_type& count);
static size_type length(const silly_string& str);
static void append(silly_string& str, const silly_string& a);
static void insert(silly_string& str, size_type offset, const silly_string& a);
static void replace(silly_string& str, size_type offset, size_type count, const silly_string& a);
2005-09-28 22:02:00 +02:00
static const_iterator begin(const silly_string& str) { return str.s_.begin(); }
2005-09-30 23:36:11 +02:00
static mutable_iterator begin(silly_string& str) { return str.s_.begin(); }
2005-09-28 22:02:00 +02:00
static const_iterator end(const silly_string& str) { return str.s_.end(); }
2005-09-30 23:36:11 +02:00
static mutable_iterator end(silly_string& str) { return str.s_.end(); }
2005-09-08 23:41:36 +02:00
// mainly used to constuct error strings - don't have to be highly efficient!
2005-09-30 23:36:11 +02:00
static std::string asStdString(const silly_string& str);
2005-12-07 12:23:44 +01:00
#ifndef ARABICA_NO_WCHAR_T
static std::wstring asStdWString(const silly_string& str);
2005-12-07 12:23:44 +01:00
#endif
2005-09-08 23:41:36 +02:00
}; // class silly_string_adaptor
template<class CharType, class Traits>
std::basic_ostream<CharType, Traits>& operator<<(std::basic_ostream<CharType, Traits>& os, const silly_string& s)
{
2005-09-30 23:36:11 +02:00
os << silly_string_adaptor::asStdString(s);
2005-09-08 23:41:36 +02:00
return os;
} // operator<<
2005-10-31 13:07:30 +01:00
namespace std {
2005-09-08 23:41:36 +02:00
template<>
2005-10-31 13:07:30 +01:00
struct less<silly_string> : public binary_function<silly_string, silly_string, bool>
2005-09-08 23:41:36 +02:00
{ // functor for operator<
bool operator()(const silly_string& lhs, const silly_string& rhs) const
{ // apply operator< to operands
2005-09-30 23:36:11 +02:00
return (silly_string_adaptor::asStdString(lhs) < silly_string_adaptor::asStdString(rhs));
2005-09-08 23:41:36 +02:00
}
};
2005-10-31 13:07:30 +01:00
} // namespace std
2005-09-08 23:41:36 +02:00
#endif