File indexing completed on 2025-01-26 04:47:04

0001 // mk4str.h --
0002 // This is part of Metakit, see http://www.equi4.com/metakit/
0003 
0004 /** @file
0005  * Declarations of the string package.
0006  */
0007 
0008 #pragma once
0009 
0010 /////////////////////////////////////////////////////////////////////////////
0011 
0012 #if defined(q4_MFC)                      // Microsoft Foundation Classes
0013 
0014 #if defined(_WINDOWS)
0015 #include <afxwin.h>
0016 #else
0017 #include <afxcoll.h>
0018 #endif
0019 
0020 #if defined(_MSC_VER) && (_MSC_VER == 800)
0021 // MSVC 1.52 thinks a typedef has no constructor, use define instead
0022 #define c4_String CString
0023 #elif defined(_MSC_VER) && (_MSC_VER >= 1300)
0024 // VC 7.0 does not like "class" (6-2-2002, Zhang Dehua)
0025 typedef CString c4_String;
0026 #else
0027 typedef class CString c4_String;
0028 #endif
0029 
0030 #elif defined(q4_STD)                    // STL and standard strings
0031 
0032 #include <string>
0033 
0034 #if !defined (d4_std)           // the default is to use namespaces
0035 #define d4_std std
0036 #endif
0037 
0038 /// STL-based string class, modeled after the MFC version
0039 class c4_String : public d4_std::string
0040 {
0041     typedef d4_std::string string;
0042 
0043 public:
0044     c4_String();
0045     c4_String(char ch, int nDup = 1);
0046     c4_String(const char *str);
0047     c4_String(const void *ptr, int len);
0048     c4_String(const d4_std::string &s);
0049     c4_String(const c4_String &s);
0050     ~c4_String();
0051 
0052     const c4_String &operator=(const c4_String &);
0053 
0054     operator const char *() const;
0055 
0056     char operator[](int i) const;
0057 
0058     friend c4_String operator+(const c4_String &, const c4_String &);
0059     friend c4_String operator+(const c4_String &, const char *);
0060     friend c4_String operator+(const char *, const c4_String &);
0061 
0062     const c4_String &operator+=(const c4_String &s);
0063     const c4_String &operator+=(const char *s);
0064 
0065     int GetLength() const;
0066     bool IsEmpty() const;
0067     void Empty();
0068 
0069     c4_String Mid(int nFirst, int nCount = 25000) const;
0070     c4_String Left(int nCount) const;
0071     c4_String Right(int nCount) const;
0072 
0073     int Compare(const char *str) const;
0074     int CompareNoCase(const char *str) const;
0075 
0076     bool operator<(const c4_String &str) const;
0077 
0078     int Find(char ch) const;
0079     int ReverseFind(char ch) const;
0080     int FindOneOf(const char *set) const;
0081 
0082     int Find(const char *sub) const;
0083 
0084     c4_String SpanIncluding(const char *set) const;
0085     c4_String SpanExcluding(const char *set) const;
0086 };
0087 
0088 bool operator==(const c4_String &, const c4_String &);
0089 bool operator!=(const c4_String &, const c4_String &);
0090 
0091 d4_inline bool operator==(const c4_String &s1, const char *s2);
0092 d4_inline bool operator==(const char *s1, const c4_String &s2);
0093 
0094 d4_inline bool operator!=(const c4_String &s1, const char *s2);
0095 d4_inline bool operator!=(const char *s1, const c4_String &s2);
0096 
0097 #else                           // Universal replacement classes
0098 
0099 /// An efficient string class, modeled after the MFC version
0100 class c4_String
0101 {
0102 public:
0103     c4_String();
0104     c4_String(char ch, int nDup = 1);
0105     c4_String(const char *str);
0106     c4_String(const unsigned char *str);
0107     c4_String(const void *ptr, int len);
0108     c4_String(const c4_String &s);
0109     ~c4_String();
0110 
0111     const c4_String &operator=(const c4_String &);
0112 
0113     operator const char *() const;
0114     operator const unsigned char *() const;
0115 
0116     char operator[](int i) const;
0117 
0118     friend c4_String operator+(const c4_String &, const c4_String &);
0119     friend c4_String operator+(const c4_String &, const char *);
0120     friend c4_String operator+(const char *, const c4_String &);
0121 //  friend c4_String operator+ (const c4_String&, char);
0122 //  friend c4_String operator+ (char, const c4_String&);
0123 
0124     const c4_String &operator+=(const c4_String &s);
0125     const c4_String &operator+=(const char *s);
0126 //  const c4_String& operator+= (char c);
0127 
0128     int GetLength() const;
0129     bool IsEmpty() const;
0130     void Empty(); // free up the data
0131 
0132     c4_String Mid(int nFirst, int nCount = 25000) const;
0133     c4_String Left(int nCount) const; // first nCount chars
0134     c4_String Right(int nCount) const; // last nCount chars
0135 
0136     friend bool operator==(const c4_String &, const c4_String &);  // memcmp
0137     friend bool operator!=(const c4_String &, const c4_String &);  // opposite
0138 
0139     // only defined for strings having no zero bytes inside them:
0140 
0141     int Compare(const char *str) const; // strcmp
0142     int CompareNoCase(const char *str) const; // stricmp
0143 
0144     bool operator<(const c4_String &str) const;
0145 
0146     int Find(char ch) const; // strchr
0147     int ReverseFind(char ch) const; // strrchr
0148     int FindOneOf(const char *set) const; // strpbrk
0149 
0150     int Find(const char *sub) const; // strstr
0151 
0152     c4_String SpanIncluding(const char *set) const; // strspn
0153     c4_String SpanExcluding(const char *set) const; // strcspn
0154 
0155 private:
0156     void Init(const void *p, int n);
0157     const char *Data() const;
0158     int FullLength() const;
0159 
0160     unsigned char *_value;
0161 };
0162 
0163 bool operator==(const c4_String &s1, const char *s2);
0164 bool operator==(const char *s1, const c4_String &s2);
0165 
0166 bool operator!=(const c4_String &s1, const char *s2);
0167 bool operator!=(const char *s1, const c4_String &s2);
0168 
0169 #endif // q4_MFC elif q4_STD else q4_UNIV
0170 
0171 /////////////////////////////////////////////////////////////////////////////
0172 
0173 #if defined(q4_INLINE)
0174 #include "mk4str.inl"
0175 #endif
0176 
0177 /////////////////////////////////////////////////////////////////////////////
0178