1// mk4str.h -- 2// $Id: mk4str.h 1230 2007-03-09 15:58:53Z jcw $ 3// This is part of Metakit, see http://www.equi4.com/metakit.html 4 5/** @file 6 * Declarations of the string package. 7 */ 8 9#ifndef __MK4STR_H__ 10#define __MK4STR_H__ 11 12///////////////////////////////////////////////////////////////////////////// 13 14#if q4_MFC // Microsoft Foundation Classes 15 16#ifdef _WINDOWS 17#include <afxwin.h> 18#else 19#include <afxcoll.h> 20#endif 21 22#if _MSC_VER == 800 23// MSVC 1.52 thinks a typedef has no constructor, use define instead 24#define c4_String CString 25#elif _MSC_VER >= 1300 26// VC 7.0 does not like "class" (6-2-2002, Zhang Dehua) 27typedef CString c4_String; 28#else 29typedef class CString c4_String; 30#endif 31 32#elif q4_STD // STL and standard strings 33 34#include <string> 35 36#if !defined (d4_std) // the default is to use namespaces 37#define d4_std std 38#endif 39 40/// STL-based string class, modeled after the MFC version 41class c4_String: public d4_std::string { 42 typedef d4_std::string string; 43 44 public: 45 c4_String(); 46 c4_String(char ch, int nDup = 1); 47 c4_String(const char *str); 48 c4_String(const void *ptr, int len); 49 c4_String(const d4_std::string &s); 50 c4_String(const c4_String &s); 51 ~c4_String(); 52 53 const c4_String &operator = (const c4_String &); 54 55 operator const char *()const; 56 57 char operator[](int i)const; 58 59 friend c4_String operator + (const c4_String &, const c4_String &); 60 friend c4_String operator + (const c4_String &, const char*); 61 friend c4_String operator + (const char *, const c4_String &); 62 63 const c4_String &operator += (const c4_String &s); 64 const c4_String &operator += (const char *s); 65 66 int GetLength()const; 67 bool IsEmpty()const; 68 void Empty(); 69 70 c4_String Mid(int nFirst, int nCount = 25000)const; 71 c4_String Left(int nCount)const; 72 c4_String Right(int nCount)const; 73 74 int Compare(const char *str)const; 75 int CompareNoCase(const char *str)const; 76 77 bool operator < (const c4_String &str)const; 78 79 int Find(char ch)const; 80 int ReverseFind(char ch)const; 81 int FindOneOf(const char *set)const; 82 83 int Find(const char *sub)const; 84 85 c4_String SpanIncluding(const char *set)const; 86 c4_String SpanExcluding(const char *set)const; 87}; 88 89bool operator == (const c4_String &, const c4_String &); 90bool operator != (const c4_String &, const c4_String &); 91 92d4_inline bool operator == (const c4_String &s1, const char *s2); 93d4_inline bool operator == (const char *s1, const c4_String &s2); 94 95d4_inline bool operator != (const c4_String &s1, const char *s2); 96d4_inline bool operator != (const char *s1, const c4_String &s2); 97 98#else // Universal replacement classes 99 100/// An efficient string class, modeled after the MFC version 101class c4_String { 102 public: 103 c4_String(); 104 c4_String(char ch, int nDup = 1); 105 c4_String(const char *str); 106 c4_String(const unsigned char *str); 107 c4_String(const void *ptr, int len); 108 c4_String(const c4_String &s); 109 ~c4_String(); 110 111 const c4_String &operator = (const c4_String &); 112 113 operator const char *()const; 114 operator const unsigned char *()const; 115 116 char operator[](int i)const; 117 118 friend c4_String operator + (const c4_String &, const c4_String &); 119 friend c4_String operator + (const c4_String &, const char*); 120 friend c4_String operator + (const char *, const c4_String &); 121 // friend c4_String operator+ (const c4_String&, char); 122 // friend c4_String operator+ (char, const c4_String&); 123 124 const c4_String &operator += (const c4_String &s); 125 const c4_String &operator += (const char *s); 126 // const c4_String& operator+= (char c); 127 128 int GetLength()const; 129 bool IsEmpty()const; 130 void Empty(); // free up the data 131 132 c4_String Mid(int nFirst, int nCount = 25000)const; 133 c4_String Left(int nCount)const; // first nCount chars 134 c4_String Right(int nCount)const; // last nCount chars 135 136 friend bool operator == (const c4_String &, const c4_String &); // memcmp 137 friend bool operator != (const c4_String &, const c4_String &); // opposite 138 139 // only defined for strings having no zero bytes inside them: 140 141 int Compare(const char *str)const; // strcmp 142 int CompareNoCase(const char *str)const; // stricmp 143 144 bool operator < (const c4_String &str)const; 145 146 int Find(char ch)const; // strchr 147 int ReverseFind(char ch)const; // strrchr 148 int FindOneOf(const char *set)const; // strpbrk 149 150 int Find(const char *sub)const; // strstr 151 152 c4_String SpanIncluding(const char *set)const; // strspn 153 c4_String SpanExcluding(const char *set)const; // strcspn 154 155 private: 156 void Init(const void *p, int n); 157 const char *Data()const; 158 int FullLength()const; 159 160 unsigned char *_value; 161}; 162 163bool operator == (const c4_String &s1, const char *s2); 164bool operator == (const char *s1, const c4_String &s2); 165 166bool operator != (const c4_String &s1, const char *s2); 167bool operator != (const char *s1, const c4_String &s2); 168 169#endif // q4_MFC elif q4_STD else q4_UNIV 170 171///////////////////////////////////////////////////////////////////////////// 172 173#if q4_INLINE 174#include "mk4str.inl" 175#endif 176 177///////////////////////////////////////////////////////////////////////////// 178 179#endif // __MK4STR_H__ 180