gnopaste

Sorry for this ugly message. We are searching for some translators.
Please take a look here
Thanks to all translators, you're doing a great job!

Name
Anonymous
Scriptlanguage
C++
Tabwidth
4
Date
01/25/2010 03:10:19 pm
IP
84.168.204.156

The user was too lazy to give a description

  1. #ifndef CONST_STRING_HEADER_INCLUDED
  2. #define CONST_STRING_HEADER_INCLUDED
  3.  
  4. #include <iterator>
  5. #include <cstddef>
  6. #include <algorithm>
  7. #include <iostream>
  8.  
  9. template<typename T>
  10. class basic_const_string
  11. {
  12. public:
  13.     typedef const T value_type;
  14.     typedef value_type& reference;
  15.     typedef value_type& const_reference;
  16.     typedef value_type* pointer;
  17.     typedef value_type* const_pointer;
  18.     typedef pointer iterator;
  19.     typedef const_pointer const_iterator;
  20.     typedef std::reverse_iterator<iterator> reverse_iterator;
  21.     typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  22.     typedef std::size_t size_type;
  23.     typedef std::ptrdiff_t difference_type;
  24.        
  25.     static const size_type npos;
  26.        
  27. private:
  28.     static size_type stringlength(const_pointer string)
  29.     {
  30.         size_type i = 0;
  31.         while(string[i])
  32.             ++i;
  33.         return i;
  34.     }
  35.        
  36. public:
  37.     basic_const_string():
  38.         begin_(0),
  39.         end_(0)
  40.     {}
  41.     basic_const_string(const_pointer string, size_type length):
  42.         begin_(string),
  43.         end_(string + length)
  44.     {}
  45.     basic_const_string(const_pointer string):
  46.         begin_(string),
  47.         end_(string + stringlength(string))
  48.     {}
  49.  
  50.     basic_const_string(iterator begin, iterator end):
  51.         begin_(begin),
  52.         end_(end)
  53.     {}
  54.    
  55.     const_iterator begin() const
  56.     {
  57.         return begin_;
  58.     }
  59.     const_iterator end() const
  60.     {
  61.         return end_;
  62.     }
  63.     const_reverse_iterator rbegin() const
  64.     {
  65.         return reverse_iterator(end());
  66.     }
  67.     const_reverse_iterator rend() const
  68.     {
  69.         return reverse_iterator(begin());
  70.     }
  71.    
  72.     size_type size() const
  73.     {
  74.         return end() - begin();
  75.     }
  76.        
  77.     value_type operator[](size_type i) const
  78.     {
  79.         return begin()[i];
  80.     }
  81.        
  82.     basic_const_string substr(size_type pos, size_type n = npos) const
  83.     {
  84.         iterator iter = begin() + pos;
  85.         return basic_const_string(iter, iter + std::min(size() - pos, n));
  86.     }
  87.        
  88.     size_type find(value_type needle, size_type pos = 0) const
  89.     {
  90.         for(iterator iter = begin() + pos; iter != end(); ++iter)
  91.         {
  92.             if(*iter == needle)
  93.             {
  94.                 return iter - begin();
  95.             }
  96.         }
  97.         return npos;
  98.     }
  99.        
  100.     size_type find(const basic_const_string& needle, size_type pos = 0) const
  101.     {
  102.         iterator to_find = needle.begin();
  103.         for(iterator iter = begin() + pos; iter != end(); ++iter)
  104.         {               
  105.             if(*iter == *to_find)
  106.             {
  107.                 ++to_find;
  108.             }
  109.             else
  110.             {
  111.                 to_find = needle.begin();
  112.             }
  113.            
  114.             if(to_find == needle.end())
  115.             {
  116.                 return iter + 1 - needle.size() - begin();
  117.             }
  118.         }
  119.         return npos;
  120.     }
  121.        
  122.     void copy(T* str, size_type num, size_type index = 0) const
  123.     {
  124.         std::copy(begin() + index, begin() + index + num, str);
  125.     }
  126.        
  127.     template<size_type N>
  128.     void copy(T (&str)[N], size_type index = 0) const
  129.     {
  130.         this->copy(str, N, index);
  131.     }
  132.        
  133. private:
  134.     iterator begin_;
  135.     iterator end_;
  136. };
  137.    
  138. template<typename T>
  139. const typename basic_const_string<T>::size_type
  140. basic_const_string<T>::npos = typename basic_const_string<T>::size_type(-1);
  141.    
  142. typedef basic_const_string<char> const_string;
  143. typedef basic_const_string<wchar_t> const_wstring;
  144.  
  145. template<typename T, typename CharTraits>
  146. std::basic_ostream<T, CharTraits>& operator<<(std::basic_ostream<T, CharTraits>& os, const basic_const_string<T>& str)
  147. {
  148.     std::copy(str.begin(), str.end(), std::ostream_iterator<T, typename CharTraits::char_type, CharTraits>(os));
  149.     return os;
  150. }
  151.    
  152. template<typename T>
  153. int compare(const basic_const_string<T>& lhs, const basic_const_string<T>& rhs)
  154. {
  155.     typedef typename basic_const_string<T>::const_iterator iterator;
  156.     iterator l = lhs.begin();
  157.     iterator r = rhs.begin();
  158.     for(; l != lhs.end() && r != rhs.end(); ++l, ++r)
  159.     {
  160.         if(*l < *r)
  161.         {
  162.             return -1;
  163.         }
  164.         else if(*l > *r)
  165.         {
  166.             return 1;
  167.         }
  168.     }
  169.     if(r != rhs.end())
  170.     {
  171.         return -1;
  172.     }
  173.     return l != lhs.end();
  174. }
  175.            
  176. template<typename T>
  177. bool operator==(const basic_const_string<T>& lhs, const basic_const_string<T>& rhs)
  178. {
  179.     return compare(lhs, rhs) == 0;
  180. }
  181.  
  182. template<typename T>
  183. bool operator==(const T* lhs, const basic_const_string<T>& rhs)
  184. {
  185.     return basic_const_string<T>(lhs) == rhs;
  186. }
  187.  
  188. template<typename T>
  189. bool operator==(const basic_const_string<T>& lhs, const T* rhs)
  190. {
  191.     return lhs == basic_const_string<T>(rhs);
  192. }
  193.  
  194. template<typename T>
  195. bool operator!=(const basic_const_string<T>& lhs, const basic_const_string<T>& rhs)
  196. {
  197.     return !(lhs == rhs);
  198. }
  199.  
  200. template<typename T>
  201. bool operator!=(const T* lhs, const basic_const_string<T>& rhs)
  202. {
  203.     return basic_const_string<T>(lhs) != rhs;
  204. }
  205.  
  206. template<typename T>
  207. bool operator!=(const basic_const_string<T>& lhs, const T* rhs)
  208. {
  209.     return lhs != basic_const_string<T>(rhs);
  210. }
  211.      
  212. template<typename T>
  213. bool operator<(const basic_const_string<T>& lhs, const basic_const_string<T>& rhs)
  214. {
  215.     return compare(lhs, rhs) == -1;
  216. }
  217.    
  218. template<typename T>
  219. bool operator<(const T* lhs, const basic_const_string<T>& rhs)
  220. {
  221.     return basic_const_string<T>(lhs) < rhs;
  222. }
  223.    
  224. template<typename T>
  225. bool operator<(const basic_const_string<T>& lhs, const T* rhs)
  226. {
  227.     return lhs < basic_const_string<T>(rhs);
  228. }
  229.        
  230. template<typename T>
  231. bool operator>(const basic_const_string<T>& lhs, const basic_const_string<T>& rhs)
  232. {
  233.     return rhs < lhs;
  234. }
  235.  
  236. template<typename T>
  237. bool operator>(const T* lhs, const basic_const_string<T>& rhs)
  238. {
  239.     return basic_const_string<T>(lhs) > rhs;
  240. }
  241.    
  242. template<typename T>
  243. bool operator>(const basic_const_string<T>& lhs, const T* rhs)
  244. {
  245.     return lhs > basic_const_string<T>(rhs);
  246. }
  247.      
  248. template<typename T>
  249. bool operator<=(const basic_const_string<T>& lhs, const basic_const_string<T>& rhs)
  250. {
  251.     return !(lhs > rhs);
  252. }
  253.    
  254. template<typename T>
  255. bool operator<=(const T* lhs, const basic_const_string<T>& rhs)
  256. {
  257.     return basic_const_string<T>(lhs) <= rhs;
  258. }
  259.  
  260. template<typename T>
  261. bool operator<=(const basic_const_string<T>& lhs, const T* rhs)
  262. {
  263.     return lhs <= basic_const_string<T>(rhs);
  264. }
  265.        
  266. template<typename T>
  267. bool operator>=(const basic_const_string<T>& lhs, const basic_const_string<T>& rhs)
  268. {
  269.     return !(lhs < rhs);
  270. }
  271.  
  272. template<typename T>
  273. bool operator>=(const T* lhs, const basic_const_string<T>& rhs)
  274. {
  275.     return basic_const_string<T>(lhs) >= rhs;
  276. }
  277.  
  278. template<typename T>
  279. bool operator>=(const basic_const_string<T>& lhs, const T* rhs)
  280. {
  281.     return lhs >= basic_const_string<T>(rhs);
  282. }
  283.  
  284. #endif
  285.  
submitter » gnopaster | imprint « imprint     
» Terms of use «


Donate via Bitcoin:
1KCq5UvwuTMwgSAHUsd7eqkBWTHZau1ydN