Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * constStr.h
- *
- * Created on: 18.8.2009
- * Author: ondra
- */
- #ifndef LIGHTSPEED_CONTAINER_CONSTSTR_H_
- #define LIGHTSPEED_CONTAINER_CONSTSTR_H_
- #include "arrayref.h"
- namespace LightSpeed {
- ///Declaration of class handles basic operations with ASCIIZ
- template<class T>
- class StringBase {
- public:
- static const bool needZeroChar = false;
- static bool isZeroChar(const T &itm) {return false;}
- template<class X>
- static void writeZeroChar(IWriteIterator<T,X> &iter) {}
- static natural stringLen(const T *str);
- static natural stringLen(natural originLen) {return originLen;}
- static natural originalLen(natural stringLen) {return stringLen;}
- static const T *emptyStr;
- };
- namespace _intr {
- template<class T>
- class StringBaseCharAndWide {
- public:
- static const bool needZeroChar = true;
- static bool isZeroChar(const T &itm) {return itm == 0;}
- template<class X>
- static void writeZeroChar(IWriteIterator<T,X> &iter) {
- iter.write(T(0));
- }
- static natural stringLen(const T *str) {
- if (str == 0) return 0;
- natural sz = 0;
- while (str[sz] != 0) sz++;
- return sz;
- }
- static natural stringLen(natural originLen) {return originLen+1;}
- static natural originalLen(natural stringLen) {return stringLen-1;}
- static const T *emptyStr;
- };
- }
- ///Specialization of StringBase for char
- template<> class StringBase<char>: public _intr::StringBaseCharAndWide<char> {};
- ///Specialization of StringBase for wchar_t
- template<> class StringBase<wchar_t>: public _intr::StringBaseCharAndWide<wchar_t> {};
- ///Specialization of StringBase for char
- template<> class StringBase<char const>: public _intr::StringBaseCharAndWide<char const> {};
- ///Specialization of StringBase for wchar_t
- template<> class StringBase<wchar_t const>: public _intr::StringBaseCharAndWide<wchar_t const> {};
- ///Constant string
- /** Useful to fast create GenericArray like container from constant string
- * If used with immediately value, it can be used anytime
- * If used with const char ptr variable, pointer should be valid
- * until instance is destroyed
- */
- template<class T>
- class ConstStringT: public StringBase<T>,
- public ArrayRef<const T>
- {
- public:
- typedef ArrayRef<const T> Super;
- typedef StringBase<const T> Super2;
- ConstStringT():Super() {}
- ConstStringT(NullType x):Super() {}
- ConstStringT(const T *asciiZ):Super(asciiZ,Super2::stringLen(asciiZ)) {}
- ConstStringT(const T *asciiZ, natural count):Super(asciiZ,count) {}
- template<int n>
- ConstStringT(const T (& asciiZ)[n]):Super(asciiZ,n) {}
- template<typename Tr>
- ConstStringT(const FlatArray<typename ConstObject<T>::Remove,Tr> &arr):Super(arr) {}
- template<typename Tr>
- ConstStringT(const FlatArray<typename ConstObject<T>::Add,Tr> &arr):Super(arr) {}
- ConstStringT(const T &obj):Super(&obj,1) {}
- using Super::operator==;
- using Super::operator!=;
- using Super::operator>;
- using Super::operator<;
- using Super::operator>=;
- using Super::operator<=;
- /* bool operator==(const T *x) const {return (*this) == ConstStringT(x);}
- bool operator!=(const T *x) const {return (*this) != ConstStringT(x);}
- bool operator>=(const T *x) const {return (*this) >= ConstStringT(x);}
- bool operator<=(const T *x) const {return (*this) <= ConstStringT(x);}
- bool operator>(const T *x) const {return (*this) > ConstStringT(x);}
- bool operator<(const T *x) const {return (*this) < ConstStringT(x);}*/
- bool operator==(NullType x) const {return this->data() == 0;}
- bool operator!=(NullType x) const {return this->data() != 0;}
- };
- typedef ConstStringT<char> ConstStrC;
- typedef ConstStringT<char> ConstStrA;
- typedef ConstStringT<wchar_t> ConstStrW;
- template<typename T, int n>
- ConstStringT<T> __arrayToString(const T (&arr)[n]) {
- if (n > 0 && StringBase<T>::needZeroChar
- && StringBase<T>::isZeroChar(arr[n-1])) {
- return ConstStringT<T>(arr,n-1);
- } else {
- return ConstStringT<T>(arr,n);
- }
- }
- template<typename T>
- struct StrCmpCS {
- CompareResult operator()(const ConstStringT<T> &a, const ConstStringT<T> &b);
- };
- template<typename T>
- struct StrCmpCI;
- template<>
- struct StrCmpCI<char> {
- CompareResult operator()(const ConstStringT<char> &a, const ConstStringT<char> &b);
- };
- template<>
- struct StrCmpCI<wchar_t> {
- CompareResult operator()(const ConstStringT<wchar_t> &a, const ConstStringT<wchar_t> &b);
- };
- }
- #endif /* CONSTSTR_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement