00001
00004
00005
00006
00007
00008 #ifndef _cel_stringutil_h_
00009 #define _cel_stringutil_h_
00010
00011 #include "cel_types.h"
00012
00013 namespace Celartem
00014 {
00018 template<typename T> struct DefaultCharTraits
00019 {
00021 typedef T CharCodeType;
00022
00032 static inline const T *next(const T *p) {return p + 1;}
00033
00044 static inline const CharCodeType getCharcode(const T *p) {return *p;}
00045 };
00046
00053 template<typename T, typename Traits = DefaultCharTraits<T> >
00054 struct StringUtil
00055 {
00060 static size_t strlen(const T *inString)
00061 {
00062 size_t n = 0;
00063 for(; inString[n] != 0; n++){}
00064 return n;
00065 }
00066
00071 static void strcpy(
00072 T * CEL_RESTRICT inDest, const T * CEL_RESTRICT inSrc)
00073 {
00074 while(*inSrc)
00075 *inDest++ = *inSrc++;
00076 *inDest = 0;
00077 }
00078
00083 static int strcmp(
00084 const T * CEL_RESTRICT str1, const T * CEL_RESTRICT str2)
00085 {
00086 if(!str1 && !str2) return 0;
00087 if(!str1 && str2) return -1;
00088 if(str1 && !str2) return 1;
00089
00090 for(size_t i = 0;; i++)
00091 {
00092 typename Traits::CharCodeType c1 = Traits::getCharcode(str1);
00093 typename Traits::CharCodeType c2 = Traits::getCharcode(str2);
00094 int cmp = c1 - c2;
00095 if(cmp != 0) return cmp;
00096 if(c1 == 0 && c2 == 0)
00097 return 0;
00098
00099 str1 = Traits::next(str1);
00100 str2 = Traits::next(str2);
00101 }
00102 }
00103
00108 static const T *strchr(
00109 const T *inString, typename Traits::CharCodeType inChar)
00110 {
00111 for(;
00112 Traits::getCharcode(inString) != 0;
00113 inString = Traits::next(inString))
00114 {
00115 if(Traits::getCharcode(inString) == inChar)
00116 return inString;
00117 }
00118 return NULL;
00119 }
00120
00125 static const T *strpbrk(
00126 const T * CEL_RESTRICT inString, const T * CEL_RESTRICT inChars)
00127 {
00128 for(; *inString != 0; inString++)
00129 if(strchr(inChars, Traits::getCharcode(inString)))
00130 return inString;
00131 return NULL;
00132 }
00133 };
00134
00135 }
00136
00137 #endif // _cel_stringutil_h_
00138