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_
40 #ifndef WDL_STRING_IMPL_ONLY
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)"))
55 WDL_String(const WDL_String
*s
) : m_hb(128 WDL_HEAPBUF_TRACEPARM("WDL_String(3)"))
57 if (s
&& s
!= this) Set(s
);
62 #define WDL_STRING_PREFIX
64 #define WDL_STRING_PREFIX WDL_String::
67 void WDL_STRING_PREFIX
Set(const char *str
, int maxlen
68 #ifdef WDL_STRING_INTF_ONLY
71 #ifdef WDL_STRING_IMPL_ONLY
78 if (maxlen
>0) while (s
< maxlen
&& str
[s
]) s
++;
79 else s
=(int)strlen(str
);
84 void WDL_STRING_PREFIX
Set(const WDL_String
*str
, int maxlen
85 #ifdef WDL_STRING_INTF_ONLY
88 #ifdef WDL_STRING_IMPL_ONLY
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);
100 Set(str
->Get(), maxlen
); // might be faster: "partial" strlen
105 void WDL_STRING_PREFIX
Append(const char *str
, int maxlen
106 #ifdef WDL_STRING_INTF_ONLY
109 #ifdef WDL_STRING_IMPL_ONLY
116 if (maxlen
>0) while (s
< maxlen
&& str
[s
]) s
++;
117 else s
=(int)strlen(str
);
119 __doSet(GetLength(),str
,s
,0);
123 void WDL_STRING_PREFIX
Append(const WDL_String
*str
, int maxlen
124 #ifdef WDL_STRING_INTF_ONLY
127 #ifdef WDL_STRING_IMPL_ONLY
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);
139 Append(str
->Get(), maxlen
); // might be faster: "partial" strlen
144 void WDL_STRING_PREFIX
DeleteSub(int position
, int len
)
145 #ifdef WDL_STRING_INTF_ONLY
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
;
156 memmove(p
+position
,p
+position
+len
,l
-position
-len
+1);
157 m_hb
.Resize(l
+1-len
,false);
162 void WDL_STRING_PREFIX
Insert(const char *str
, int position
, int maxlen
163 #ifdef WDL_STRING_INTF_ONLY
166 #ifdef WDL_STRING_IMPL_ONLY
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
);
183 void WDL_STRING_PREFIX
Insert(const WDL_String
*str
, int position
, int maxlen
184 #ifdef WDL_STRING_INTF_ONLY
187 #ifdef WDL_STRING_IMPL_ONLY
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
);
202 Insert(str
->Get(), position
, maxlen
); // might be faster: "partial" strlen
207 void WDL_STRING_PREFIX
SetLen(int length
, bool resizeDown
208 #ifdef WDL_STRING_INTF_ONLY
211 #ifdef WDL_STRING_IMPL_ONLY
217 #ifdef WDL_STRING_FASTSUB_DEFINED
218 int osz
= m_hb
.GetSize()?m_hb
.GetSize()-1:0;
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
);
230 void WDL_STRING_PREFIX
SetAppendFormattedArgs(bool append
, int maxlen
, const char* fmt
, va_list arglist
)
231 #ifdef WDL_STRING_INTF_ONLY
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;
240 int written
= _vsnprintf(b
, maxlen
+1, fmt
, arglist
);
241 if (written
< 0 || written
>=maxlen
) b
[written
=b
[0]?maxlen
:0]=0;
243 int written
= vsnprintf(b
, maxlen
+1, fmt
, arglist
);
244 if (written
> maxlen
) written
=maxlen
;
247 m_hb
.Resize(offs
+ written
+ 1,false);
252 void WDL_VARARG_WARN(printf
,3,4) WDL_STRING_PREFIX
SetFormatted(int maxlen
, const char *fmt
, ...)
253 #ifdef WDL_STRING_INTF_ONLY
258 va_start(arglist
, fmt
);
259 SetAppendFormattedArgs(false,maxlen
,fmt
,arglist
);
264 void WDL_VARARG_WARN(printf
,3,4) WDL_STRING_PREFIX
AppendFormatted(int maxlen
, const char* fmt
, ...)
265 #ifdef WDL_STRING_INTF_ONLY
270 va_start(arglist
, fmt
);
271 SetAppendFormattedArgs(true,maxlen
,fmt
,arglist
);
277 void WDL_STRING_PREFIX
Ellipsize(int minlen
, int maxlen
)
278 #ifdef WDL_STRING_INTF_ONLY
282 if (maxlen
>= 4 && m_hb
.GetSize() && GetLength() > maxlen
)
284 if (minlen
<0) minlen
=0;
285 char *b
= (char *)m_hb
.Get();
287 for (i
= maxlen
-4; i
>= minlen
; --i
)
291 memcpy(b
+i
, "...",4);
292 m_hb
.Resize(i
+4,false);
296 if (i
< minlen
&& maxlen
>= 4)
298 memcpy(b
+maxlen
-4, "...",4);
299 m_hb
.Resize(maxlen
,false);
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; }
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; }
322 void WDL_STRING_PREFIX
__doSet(int offs
, const char *str
, int len
, int trailkeep
)
323 #ifdef WDL_STRING_INTF_ONLY
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;
340 #ifndef WDL_STRING_IMPL_ONLY
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