fundoshi.hpp
A C++ library for multiple string instances with shared memory
 All Classes Namespaces Files Functions Typedefs Pages
fundoshi.hpp
Go to the documentation of this file.
1 
29 #ifndef _FUNDOSHI_HPP_
30 #define _FUNDOSHI_HPP_
31 
32 #include <iostream>
33 #include <string>
34 #include <cstring>
35 
36 namespace fundoshi{
37  template <class CharType> class basic_string;
38 
39  template <class CharType>
40  static size_t strlen(const CharType * str){
41  size_t result = 0;
42 
43  while(*str != 0){
44  str++;
45  result++;
46  }
47  return result;
48  }
49 
50  template <class CharType>
51  static void print_char(std::ostream & os, CharType c){
52  if(c >= 0x20 && c <= 0x7E){
53  os << static_cast<char>(c);
54  }else{
55  os << '[' << c << ']';
56  }
57  }
58 
59  template <>
60  void print_char(std::ostream & os, signed char c){
61  if(c >= 0x20 && c <= 0x7E){
62  os << c;
63  }else{
64  os << '[' << static_cast<signed int>(c) << ']';
65  }
66  }
67 
68  template <>
69  void print_char(std::ostream & os, unsigned char c){
70  if(c >= 0x20 && c <= 0x7E){
71  os << c;
72  }else{
73  os << '[' << static_cast<unsigned int>(c) << ']';
74  }
75  }
76 
77  template <class CharType>
78  static int compare(const basic_string<CharType> & str1, const basic_string<CharType> & str2){
79  size_t min_length = (str1.length() < str2.length() ? str1.length() : str2.length());
80  for(size_t i = 0; i < min_length; i++){
81  if(str1[i] < str2[i]){
82  return -1;
83  }else if(str1[i] > str2[i]){
84  return 1;
85  }
86  }
87 
88  // if all characters are compared
89  if(str1.length() < str2.length()){
90  return -1;
91  }else if(str1.length() > str2.length()){
92  return 1;
93  }
94  return 0;
95  }
96 
97  // ---------- basic_string ----------
98  template <class CharType>
99  class basic_string{
100  public:
101  typedef size_t size_type;
102  typedef CharType value_type;
103  typedef const value_type & const_reference;
104  typedef const value_type * iterator;
105  typedef const value_type * const_iterator;
108 
109  private:
110  const CharType * buffer;
111  size_type buffer_length;
112 
113  public:
114  // default constructor (empty string)
115  basic_string() : buffer(NULL), buffer_length(0) {}
116 
117  // constructors for fundoshi::basic_string, std::basic_string, const CharType *
118  basic_string(const basic_string<CharType> & other) : buffer(other.buffer), buffer_length(other.buffer_length) {}
119  basic_string(const std::basic_string<CharType> & newstr) : buffer(newstr.c_str()), buffer_length(newstr.length()) {}
120  basic_string(const CharType * newstr){
121  buffer = newstr;
122  buffer_length = fundoshi::strlen(buffer);
123  }
124  basic_string(const CharType * newstr, size_type newstr_length) : buffer(newstr), buffer_length(newstr_length) {}
125  basic_string(iterator b, iterator e) : buffer(b), buffer_length(e - b) {}
126 
127  // constructors for obtaining substrings
128  basic_string(const basic_string<CharType> & other, size_type offset, size_type length) : buffer(&(other.buffer[offset])), buffer_length(length) {}
129  basic_string(const std::basic_string<CharType> & newstr, size_type offset, size_type length) : buffer(&((newstr.c_str())[offset])), buffer_length(length) {}
130  basic_string(const CharType * newstr, size_type offset, size_type length) : buffer(&(newstr[offset])), buffer_length(length) {}
131 
133 
134  // operations
135  const value_type * c_str(void) const{ return buffer; }
136  size_type size(void) const{ return buffer_length; }
137  size_type length(void) const{ return buffer_length; }
138  const_reference operator [](size_t pos) const{ return buffer[pos]; }
139  iterator begin(void){ return buffer; }
140  iterator end(void){ return &(buffer[buffer_length]); }
141  operator std::basic_string<value_type>(void) const{
142  return std::basic_string<value_type>(buffer, buffer_length);
143  }
144 
145  // comparisons
146  int compare(const basic_string<CharType> & other) const{
147  return fundoshi::compare<CharType>(*this, other); }
148 
149  bool operator ==(const basic_string<CharType> & other) const {
150  return fundoshi::compare<CharType>(*this, other) == 0; }
151 
152  bool operator !=(const basic_string<CharType> & other) const {
153  return fundoshi::compare<CharType>(*this, other) != 0; }
154 
155  bool operator < (const basic_string<CharType> & other) const {
156  return fundoshi::compare<CharType>(*this, other) < 0; }
157 
158  bool operator <=(const basic_string<CharType> & other) const {
159  return fundoshi::compare<CharType>(*this, other) <= 0; }
160 
161  bool operator > (const basic_string<CharType> & other) const {
162  return fundoshi::compare<CharType>(*this, other) > 0; }
163 
164  bool operator >=(const basic_string<CharType> & other) const {
165  return fundoshi::compare<CharType>(*this, other) >= 0; }
166  };
167 
172 
177 };
178 
183 template <class CharType>
184 static std::ostream & operator <<(std::ostream & os, const fundoshi::basic_string<CharType> & str){
185  for(size_t i = 0; i < str.length(); i++){
186  fundoshi::print_char(os, str[i]);
187  }
188  return os;
189 }
190 
191 #endif // _FUNDOSHI_HPP_
bool operator!=(const basic_string< CharType > &other) const
Definition: fundoshi.hpp:152
const value_type * const_iterator
Definition: fundoshi.hpp:105
CharType value_type
Definition: fundoshi.hpp:102
basic_string(const basic_string< CharType > &other)
Definition: fundoshi.hpp:118
iterator begin(void)
Definition: fundoshi.hpp:139
basic_string(const CharType *newstr, size_type offset, size_type length)
Definition: fundoshi.hpp:130
~basic_string()
Definition: fundoshi.hpp:132
basic_string(const CharType *newstr, size_type newstr_length)
Definition: fundoshi.hpp:124
bool operator>=(const basic_string< CharType > &other) const
Definition: fundoshi.hpp:164
Definition: fundoshi.hpp:37
basic_string(const basic_string< CharType > &other, size_type offset, size_type length)
Definition: fundoshi.hpp:128
basic_string< char > string
Definition: fundoshi.hpp:171
static void print_char(std::ostream &os, CharType c)
Definition: fundoshi.hpp:51
const_reference operator[](size_t pos) const
Definition: fundoshi.hpp:138
bool operator>(const basic_string< CharType > &other) const
Definition: fundoshi.hpp:161
basic_string(const std::basic_string< CharType > &newstr)
Definition: fundoshi.hpp:119
size_t size_type
Definition: fundoshi.hpp:101
const_iterator const_reverse_iterator
Definition: fundoshi.hpp:107
iterator reverse_iterator
Definition: fundoshi.hpp:106
static int compare(const basic_string< CharType > &str1, const basic_string< CharType > &str2)
Definition: fundoshi.hpp:78
basic_string(const std::basic_string< CharType > &newstr, size_type offset, size_type length)
Definition: fundoshi.hpp:129
iterator end(void)
Definition: fundoshi.hpp:140
basic_string(iterator b, iterator e)
Definition: fundoshi.hpp:125
size_type size(void) const
Definition: fundoshi.hpp:136
size_type length(void) const
Definition: fundoshi.hpp:137
static size_t strlen(const CharType *str)
Definition: fundoshi.hpp:40
basic_string< wchar_t > wstring
Definition: fundoshi.hpp:176
basic_string()
Definition: fundoshi.hpp:115
bool operator==(const basic_string< CharType > &other) const
Definition: fundoshi.hpp:149
const value_type * iterator
Definition: fundoshi.hpp:104
int compare(const basic_string< CharType > &other) const
Definition: fundoshi.hpp:146
const value_type & const_reference
Definition: fundoshi.hpp:103
const value_type * c_str(void) const
Definition: fundoshi.hpp:135
basic_string(const CharType *newstr)
Definition: fundoshi.hpp:120