[IPLUG/EXAMPLES] IPlugResampler: qualification of min no longer needed
[wdl/wdl-ol.git] / WDL / wdlstring.h
blob04478adef277929a864053ac36497309de3f2aec
1 /*
2 WDL - wdlstring.h
3 Copyright (C) 2005 and later, Cockos Incorporated
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
25 This file provides a simple class for variable-length string manipulation.
26 It provides only the simplest features, and does not do anything confusing like
27 operator overloading. It uses a WDL_HeapBuf for internal storage.
29 You can do Set, Get, Append, Insert, and SetLen.. that's about it
33 #ifndef _WDL_STRING_H_
34 #define _WDL_STRING_H_
36 #include "heapbuf.h"
37 #include <stdio.h>
38 #include <stdarg.h>
40 #ifndef WDL_STRING_IMPL_ONLY
41 class WDL_String
43 public:
44 explicit WDL_String(int hbgran) : m_hb(hbgran WDL_HEAPBUF_TRACEPARM("WDL_String(4)"))
47 explicit WDL_String(const char *initial=NULL, int initial_len=0) : m_hb(128 WDL_HEAPBUF_TRACEPARM("WDL_String"))
49 if (initial) Set(initial,initial_len);
51 WDL_String(const WDL_String &s) : m_hb(128 WDL_HEAPBUF_TRACEPARM("WDL_String(2)"))
53 Set(&s);
55 WDL_String(const WDL_String *s) : m_hb(128 WDL_HEAPBUF_TRACEPARM("WDL_String(3)"))
57 if (s && s != this) Set(s);
59 ~WDL_String()
62 #define WDL_STRING_PREFIX
63 #else
64 #define WDL_STRING_PREFIX WDL_String::
65 #endif
67 void WDL_STRING_PREFIX Set(const char *str, int maxlen
68 #ifdef WDL_STRING_INTF_ONLY
69 =0);
70 #else
71 #ifdef WDL_STRING_IMPL_ONLY
73 #else
74 =0)
75 #endif
77 int s=0;
78 if (maxlen>0) while (s < maxlen && str[s]) s++;
79 else s=(int)strlen(str);
80 __doSet(0,str,s,0);
82 #endif
84 void WDL_STRING_PREFIX Set(const WDL_String *str, int maxlen
85 #ifdef WDL_STRING_INTF_ONLY
86 =0);
87 #else
88 #ifdef WDL_STRING_IMPL_ONLY
90 #else
91 =0)
92 #endif
94 #ifdef WDL_STRING_FASTSUB_DEFINED
95 int s = str->GetLength();
96 if (maxlen>0 && maxlen<s) s=maxlen;
98 __doSet(0,str->Get(),s,0);
99 #else
100 Set(str->Get(), maxlen); // might be faster: "partial" strlen
101 #endif
103 #endif
105 void WDL_STRING_PREFIX Append(const char *str, int maxlen
106 #ifdef WDL_STRING_INTF_ONLY
107 =0);
108 #else
109 #ifdef WDL_STRING_IMPL_ONLY
111 #else
113 #endif
115 int s=0;
116 if (maxlen>0) while (s < maxlen && str[s]) s++;
117 else s=(int)strlen(str);
119 __doSet(GetLength(),str,s,0);
121 #endif
123 void WDL_STRING_PREFIX Append(const WDL_String *str, int maxlen
124 #ifdef WDL_STRING_INTF_ONLY
125 =0);
126 #else
127 #ifdef WDL_STRING_IMPL_ONLY
129 #else
131 #endif
133 #ifdef WDL_STRING_FASTSUB_DEFINED
134 int s = str->GetLength();
135 if (maxlen>0 && maxlen<s) s=maxlen;
137 __doSet(GetLength(),str->Get(),s,0);
138 #else
139 Append(str->Get(), maxlen); // might be faster: "partial" strlen
140 #endif
142 #endif
144 void WDL_STRING_PREFIX DeleteSub(int position, int len)
145 #ifdef WDL_STRING_INTF_ONLY
147 #else
149 char *p=(char *)m_hb.Get();
150 if (!m_hb.GetSize() || !*p) return;
151 int l=m_hb.GetSize()-1;
152 if (position < 0 || position >= l) return;
153 if (position+len > l) len=l-position;
154 if (len>0)
156 memmove(p+position,p+position+len,l-position-len+1);
157 m_hb.Resize(l+1-len,false);
160 #endif
162 void WDL_STRING_PREFIX Insert(const char *str, int position, int maxlen
163 #ifdef WDL_STRING_INTF_ONLY
164 =0);
165 #else
166 #ifdef WDL_STRING_IMPL_ONLY
168 #else
170 #endif
172 int ilen=0;
173 if (maxlen>0) while (ilen < maxlen && str[ilen]) ilen++;
174 else ilen=(int)strlen(str);
176 int srclen = GetLength();
177 if (position<0) position=0;
178 else if (position>srclen) position=srclen;
179 if (ilen>0) __doSet(position,str,ilen,srclen-position);
181 #endif
183 void WDL_STRING_PREFIX Insert(const WDL_String *str, int position, int maxlen
184 #ifdef WDL_STRING_INTF_ONLY
185 =0);
186 #else
187 #ifdef WDL_STRING_IMPL_ONLY
189 #else
191 #endif
193 #ifdef WDL_STRING_FASTSUB_DEFINED
194 int ilen = str->GetLength();
195 if (maxlen>0 && maxlen<ilen) ilen=maxlen;
197 int srclen = m_hb.GetSize()>0 ? m_hb.GetSize()-1 : 0;
198 if (position<0) position=0;
199 else if (position>srclen) position=srclen;
200 if (ilen>0) __doSet(position,str->Get(),ilen,srclen-position);
201 #else
202 Insert(str->Get(), position, maxlen); // might be faster: "partial" strlen
203 #endif
205 #endif
207 void WDL_STRING_PREFIX SetLen(int length, bool resizeDown
208 #ifdef WDL_STRING_INTF_ONLY
209 =false);
210 #else
211 #ifdef WDL_STRING_IMPL_ONLY
213 #else
214 =false)
215 #endif
217 #ifdef WDL_STRING_FASTSUB_DEFINED
218 int osz = m_hb.GetSize()?m_hb.GetSize()-1:0;
219 #endif
220 char *b=(char*)m_hb.Resize(length+1,resizeDown);
221 if (m_hb.GetSize()==length+1)
223 #ifdef WDL_STRING_FASTSUB_DEFINED
224 if (length > osz) memset(b+osz,' ',length-osz);
225 #endif
226 b[length]=0;
229 #endif
230 void WDL_STRING_PREFIX SetAppendFormattedArgs(bool append, int maxlen, const char* fmt, va_list arglist)
231 #ifdef WDL_STRING_INTF_ONLY
233 #else
235 int offs = append ? GetLength() : 0;
236 char* b= (char*) m_hb.Resize(offs+maxlen+1,false)+offs;
237 if (m_hb.GetSize() != offs+maxlen+1) return;
239 #ifdef _WIN32
240 int written = _vsnprintf(b, maxlen+1, fmt, arglist);
241 if (written < 0 || written>=maxlen) b[written=b[0]?maxlen:0]=0;
242 #else
243 int written = vsnprintf(b, maxlen+1, fmt, arglist);
244 if (written > maxlen) written=maxlen;
245 #endif
247 m_hb.Resize(offs + written + 1,false);
249 #endif
252 void WDL_VARARG_WARN(printf,3,4) WDL_STRING_PREFIX SetFormatted(int maxlen, const char *fmt, ...)
253 #ifdef WDL_STRING_INTF_ONLY
255 #else
257 va_list arglist;
258 va_start(arglist, fmt);
259 SetAppendFormattedArgs(false,maxlen,fmt,arglist);
260 va_end(arglist);
262 #endif
264 void WDL_VARARG_WARN(printf,3,4) WDL_STRING_PREFIX AppendFormatted(int maxlen, const char* fmt, ...)
265 #ifdef WDL_STRING_INTF_ONLY
267 #else
269 va_list arglist;
270 va_start(arglist, fmt);
271 SetAppendFormattedArgs(true,maxlen,fmt,arglist);
272 va_end(arglist);
274 #endif
277 void WDL_STRING_PREFIX Ellipsize(int minlen, int maxlen)
278 #ifdef WDL_STRING_INTF_ONLY
280 #else
282 if (maxlen >= 4 && m_hb.GetSize() && GetLength() > maxlen)
284 if (minlen<0) minlen=0;
285 char *b = (char *)m_hb.Get();
286 int i;
287 for (i = maxlen-4; i >= minlen; --i)
289 if (b[i] == ' ')
291 memcpy(b+i, "...",4);
292 m_hb.Resize(i+4,false);
293 break;
296 if (i < minlen && maxlen >= 4)
298 memcpy(b+maxlen-4, "...",4);
299 m_hb.Resize(maxlen,false);
303 #endif
305 #ifndef WDL_STRING_IMPL_ONLY
306 #ifdef WDL_STRING_FASTSUB_DEFINED
307 const char *Get() const { return m_hb.GetSize()?(char*)m_hb.Get():""; }
308 int GetLength() const { int a = m_hb.GetSize(); return a>0?a-1:0; }
309 #else
310 char *Get() const
312 if (m_hb.GetSize()) return (char *)m_hb.Get();
313 static char c; c=0; return &c; // don't return "", in case it gets written to.
315 int GetLength() const { return m_hb.GetSize()?(int)strlen((const char*)m_hb.Get()):0; }
316 #endif
318 private:
320 #endif
322 void WDL_STRING_PREFIX __doSet(int offs, const char *str, int len, int trailkeep)
323 #ifdef WDL_STRING_INTF_ONLY
325 #else
327 if (len>0 || (!trailkeep && !offs && m_hb.GetSize()>1)) // if non-empty, or (empty and allocated and Set() rather than append/insert), then allow update, otherwise do nothing
329 char *newbuf=(char*)m_hb.Resize(offs+len+trailkeep+1,false);
330 if (m_hb.GetSize()==offs+len+trailkeep+1)
332 if (trailkeep>0) memmove(newbuf+offs+len,newbuf+offs,trailkeep);
333 memcpy(newbuf+offs,str,len);
334 newbuf[offs+len+trailkeep]=0;
338 #endif
340 #ifndef WDL_STRING_IMPL_ONLY
342 WDL_HeapBuf m_hb;
344 #endif
346 #ifndef WDL_STRING_FASTSUB_DEFINED
347 #undef _WDL_STRING_H_
348 #define WDL_STRING_FASTSUB_DEFINED
349 #define WDL_String WDL_FastString
350 #include "wdlstring.h"
351 #undef WDL_STRING_FASTSUB_DEFINED
352 #undef WDL_String
353 #endif
355 #endif