1 // mk4str.h --
2 // This is part of Metakit, see http://www.equi4.com/metakit/
3 
4 /** @file
5  * Declarations of the string package.
6  */
7 
8 #pragma once
9 
10 /////////////////////////////////////////////////////////////////////////////
11 
12 #if defined(q4_MFC)                      // Microsoft Foundation Classes
13 
14 #if defined(_WINDOWS)
15 #include <afxwin.h>
16 #else
17 #include <afxcoll.h>
18 #endif
19 
20 #if defined(_MSC_VER) && (_MSC_VER == 800)
21 // MSVC 1.52 thinks a typedef has no constructor, use define instead
22 #define c4_String CString
23 #elif defined(_MSC_VER) && (_MSC_VER >= 1300)
24 // VC 7.0 does not like "class" (6-2-2002, Zhang Dehua)
25 typedef CString c4_String;
26 #else
27 typedef class CString c4_String;
28 #endif
29 
30 #elif defined(q4_STD)                    // STL and standard strings
31 
32 #include <string>
33 
34 #if !defined (d4_std)           // the default is to use namespaces
35 #define d4_std std
36 #endif
37 
38 /// STL-based string class, modeled after the MFC version
39 class c4_String : public d4_std::string
40 {
41     typedef d4_std::string string;
42 
43 public:
44     c4_String();
45     c4_String(char ch, int nDup = 1);
46     c4_String(const char *str);
47     c4_String(const void *ptr, int len);
48     c4_String(const d4_std::string &s);
49     c4_String(const c4_String &s);
50     ~c4_String();
51 
52     const c4_String &operator=(const c4_String &);
53 
54     operator const char *() const;
55 
56     char operator[](int i) const;
57 
58     friend c4_String operator+(const c4_String &, const c4_String &);
59     friend c4_String operator+(const c4_String &, const char *);
60     friend c4_String operator+(const char *, const c4_String &);
61 
62     const c4_String &operator+=(const c4_String &s);
63     const c4_String &operator+=(const char *s);
64 
65     int GetLength() const;
66     bool IsEmpty() const;
67     void Empty();
68 
69     c4_String Mid(int nFirst, int nCount = 25000) const;
70     c4_String Left(int nCount) const;
71     c4_String Right(int nCount) const;
72 
73     int Compare(const char *str) const;
74     int CompareNoCase(const char *str) const;
75 
76     bool operator<(const c4_String &str) const;
77 
78     int Find(char ch) const;
79     int ReverseFind(char ch) const;
80     int FindOneOf(const char *set) const;
81 
82     int Find(const char *sub) const;
83 
84     c4_String SpanIncluding(const char *set) const;
85     c4_String SpanExcluding(const char *set) const;
86 };
87 
88 bool operator==(const c4_String &, const c4_String &);
89 bool operator!=(const c4_String &, const c4_String &);
90 
91 d4_inline bool operator==(const c4_String &s1, const char *s2);
92 d4_inline bool operator==(const char *s1, const c4_String &s2);
93 
94 d4_inline bool operator!=(const c4_String &s1, const char *s2);
95 d4_inline bool operator!=(const char *s1, const c4_String &s2);
96 
97 #else                           // Universal replacement classes
98 
99 /// An efficient string class, modeled after the MFC version
100 class c4_String
101 {
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 
163 bool operator==(const c4_String &s1, const char *s2);
164 bool operator==(const char *s1, const c4_String &s2);
165 
166 bool operator!=(const c4_String &s1, const char *s2);
167 bool operator!=(const char *s1, const c4_String &s2);
168 
169 #endif // q4_MFC elif q4_STD else q4_UNIV
170 
171 /////////////////////////////////////////////////////////////////////////////
172 
173 #if defined(q4_INLINE)
174 #include "mk4str.inl"
175 #endif
176 
177 /////////////////////////////////////////////////////////////////////////////
178 
179