Advertisement
Guest User

Untitled

a guest
May 6th, 2012
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. /*
  2.  * constStr.h
  3.  *
  4.  *  Created on: 18.8.2009
  5.  *      Author: ondra
  6.  */
  7.  
  8. #ifndef LIGHTSPEED_CONTAINER_CONSTSTR_H_
  9. #define LIGHTSPEED_CONTAINER_CONSTSTR_H_
  10.  
  11. #include "arrayref.h"
  12.  
  13. namespace LightSpeed {
  14.  
  15.     ///Declaration of class handles basic operations with ASCIIZ
  16.  
  17.     template<class T>
  18.     class StringBase {
  19.     public:
  20.  
  21.         static const bool needZeroChar = false;
  22.  
  23.         static bool isZeroChar(const T &itm) {return false;}
  24.         template<class X>
  25.         static void writeZeroChar(IWriteIterator<T,X> &iter) {}
  26.         static natural stringLen(const T *str);
  27.         static natural stringLen(natural originLen) {return originLen;}
  28.         static natural originalLen(natural stringLen) {return stringLen;}
  29.         static const T *emptyStr;
  30.  
  31.     };
  32.  
  33.  
  34.  
  35.     namespace _intr {
  36.         template<class T>
  37.         class StringBaseCharAndWide {
  38.         public:
  39.             static const bool needZeroChar = true;
  40.  
  41.             static bool isZeroChar(const T &itm) {return itm == 0;}
  42.             template<class X>
  43.             static void writeZeroChar(IWriteIterator<T,X> &iter) {
  44.                 iter.write(T(0));
  45.             }
  46.             static natural stringLen(const T *str) {
  47.                 if (str == 0) return 0;
  48.                 natural sz = 0;
  49.                 while (str[sz] != 0) sz++;
  50.                 return sz;
  51.             }
  52.             static natural stringLen(natural originLen) {return originLen+1;}
  53.             static natural originalLen(natural stringLen) {return stringLen-1;}
  54.             static const T *emptyStr;
  55.         };
  56.     }
  57.  
  58.     ///Specialization of StringBase for char
  59.     template<> class StringBase<char>: public _intr::StringBaseCharAndWide<char> {};
  60.     ///Specialization of StringBase for wchar_t
  61.     template<> class StringBase<wchar_t>: public _intr::StringBaseCharAndWide<wchar_t> {};
  62.  
  63.     ///Specialization of StringBase for char
  64.     template<> class StringBase<char const>: public _intr::StringBaseCharAndWide<char const> {};
  65.     ///Specialization of StringBase for wchar_t
  66.     template<> class StringBase<wchar_t const>: public _intr::StringBaseCharAndWide<wchar_t const> {};
  67.  
  68.     ///Constant string
  69.     /** Useful to fast create GenericArray like container from constant string
  70.      * If used with immediately value, it can be used anytime
  71.      * If used with const char ptr variable, pointer should be valid
  72.      * until instance is destroyed
  73.      */
  74.     template<class T>
  75.     class ConstStringT: public StringBase<T>,
  76.                         public ArrayRef<const T>
  77.                          {
  78.     public:
  79.         typedef ArrayRef<const T> Super;
  80.         typedef StringBase<const T> Super2;
  81.         ConstStringT():Super() {}
  82.         ConstStringT(NullType x):Super() {}
  83.         ConstStringT(const T *asciiZ):Super(asciiZ,Super2::stringLen(asciiZ)) {}
  84.         ConstStringT(const T *asciiZ, natural count):Super(asciiZ,count) {}
  85.         template<int n>
  86.         ConstStringT(const T (& asciiZ)[n]):Super(asciiZ,n) {}
  87.         template<typename Tr>
  88.         ConstStringT(const FlatArray<typename ConstObject<T>::Remove,Tr> &arr):Super(arr) {}
  89.         template<typename Tr>
  90.         ConstStringT(const FlatArray<typename ConstObject<T>::Add,Tr> &arr):Super(arr) {}
  91.         ConstStringT(const T &obj):Super(&obj,1) {}
  92.  
  93.  
  94.         using Super::operator==;
  95.         using Super::operator!=;
  96.         using Super::operator>;
  97.         using Super::operator<;
  98.         using Super::operator>=;
  99.         using Super::operator<=;
  100.  
  101. /*      bool operator==(const T *x) const {return (*this) == ConstStringT(x);}
  102.         bool operator!=(const T *x) const {return (*this) != ConstStringT(x);}
  103.         bool operator>=(const T *x) const {return (*this) >= ConstStringT(x);}
  104.         bool operator<=(const T *x) const {return (*this) <= ConstStringT(x);}
  105.         bool operator>(const T *x) const {return (*this) > ConstStringT(x);}
  106.         bool operator<(const T *x) const {return (*this) < ConstStringT(x);}*/
  107.  
  108.         bool operator==(NullType x) const {return this->data() == 0;}
  109.         bool operator!=(NullType x) const {return this->data() != 0;}
  110.     };
  111.  
  112.     typedef ConstStringT<char> ConstStrC;
  113.     typedef ConstStringT<char> ConstStrA;
  114.     typedef ConstStringT<wchar_t> ConstStrW;
  115.  
  116.  
  117.     template<typename T, int n>
  118.     ConstStringT<T> __arrayToString(const T (&arr)[n]) {
  119.         if (n > 0 && StringBase<T>::needZeroChar
  120.             && StringBase<T>::isZeroChar(arr[n-1])) {
  121.                 return ConstStringT<T>(arr,n-1);
  122.         } else {
  123.             return ConstStringT<T>(arr,n);
  124.         }
  125.     }
  126.  
  127.  
  128.     template<typename T>
  129.     struct StrCmpCS {
  130.         CompareResult operator()(const ConstStringT<T> &a, const ConstStringT<T> &b);
  131.     };
  132.  
  133.     template<typename T>
  134.     struct StrCmpCI;
  135.  
  136.     template<>
  137.     struct StrCmpCI<char> {
  138.         CompareResult operator()(const ConstStringT<char> &a, const ConstStringT<char> &b);
  139.     };
  140.  
  141.     template<>
  142.     struct StrCmpCI<wchar_t> {
  143.         CompareResult operator()(const ConstStringT<wchar_t> &a, const ConstStringT<wchar_t> &b);
  144.     };
  145.  
  146.  
  147.  
  148. }
  149. #endif /* CONSTSTR_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement