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 Actually: there are WDL_String and WDL_FastString -- the latter's Get() returns const char, and tracks
30 the length of the string, which is often faster. Because of this, you are not permitted to directly modify
31 the buffer returned by Get().
36 #ifndef _WDL_STRING_H_
37 #define _WDL_STRING_H_
43 #ifndef WDL_STRING_IMPL_ONLY
47 #ifdef WDL_STRING_INTF_ONLY
48 void Set(const char *str
, int maxlen
=0);
49 void Set(const WDL_String
*str
, int maxlen
=0);
50 void Append(const char *str
, int maxlen
=0);
51 void Append(const WDL_String
*str
, int maxlen
=0);
52 void DeleteSub(int position
, int len
);
53 void Insert(const char *str
, int position
, int maxlen
=0);
54 void Insert(const WDL_String
*str
, int position
, int maxlen
=0);
55 #ifdef WDL_STRING_FASTSUB_DEFINED
56 bool SetLen(int length
, bool resizeDown
=false, char fillchar
=' '); // returns true on success
58 bool SetLen(int length
, bool resizeDown
=false); // returns true on success
60 void Ellipsize(int minlen
, int maxlen
);
61 const char *get_filepart() const; // returns whole string if no dir chars
62 const char *get_fileext() const; // returns ".ext" or end of string "" if no extension
63 bool remove_fileext(); // returns true if extension was removed
64 char remove_filepart(bool keepTrailingSlash
=false); // returns dir character used, or zero if string emptied
65 int remove_trailing_dirchars(); // returns trailing dirchar count removed, will not convert "/" into ""
67 void SetAppendFormattedArgs(bool append
, int maxlen
, const char* fmt
, va_list arglist
);
68 void WDL_VARARG_WARN(printf
,3,4) SetFormatted(int maxlen
, const char *fmt
, ...);
69 void WDL_VARARG_WARN(printf
,3,4) AppendFormatted(int maxlen
, const char *fmt
, ...);
72 const char *Get() const { return m_hb
.GetSize()?(char*)m_hb
.Get():""; }
73 const char *GetAtOffs(int offs
) const {
74 const int l
= GetLength();
75 if (WDL_NOT_NORMALLY(offs
< 0 || offs
> l
)) return Get()+l
;
79 #ifdef WDL_STRING_FASTSUB_DEFINED
80 int GetLength() const { int a
= m_hb
.GetSize(); return a
>0?a
-1:0; }
82 // for binary-safe manipulations
83 void SetRaw(const char *str
, int len
) { __doSet(0,str
,len
,0); }
84 void AppendRaw(const char *str
, int len
) { __doSet(GetLength(),str
,len
,0); }
85 void InsertRaw(const char *str
, int position
, int ilen
)
87 const int srclen
= GetLength();
88 if (position
<0) position
=0;
89 else if (position
>srclen
) position
=srclen
;
90 if (ilen
>0) __doSet(position
,str
,ilen
,srclen
-position
);
96 if (m_hb
.GetSize()) return (char *)m_hb
.Get();
97 static char c
; c
=0; return &c
; // don't return "", in case it gets written to.
99 int GetLength() const { return m_hb
.GetSize()?(int)strlen((const char*)m_hb
.Get()):0; }
102 explicit WDL_String(int hbgran
) : m_hb(hbgran
) { }
103 explicit WDL_String(const char *initial
=NULL
, int initial_len
=0) : m_hb(128)
105 if (initial
) Set(initial
,initial_len
);
107 WDL_String(const WDL_String
&s
) : m_hb(128) { Set(&s
); }
108 WDL_String(const WDL_String
*s
) : m_hb(128) { if (s
&& s
!= this) Set(s
); }
110 #endif // ! WDL_STRING_IMPL_ONLY
112 #ifndef WDL_STRING_INTF_ONLY
113 #ifdef WDL_STRING_IMPL_ONLY
114 #define WDL_STRING_FUNCPREFIX WDL_String::
115 #define WDL_STRING_DEFPARM(x)
117 #define WDL_STRING_FUNCPREFIX
118 #define WDL_STRING_DEFPARM(x) =(x)
121 void WDL_STRING_FUNCPREFIX
Set(const char *str
, int maxlen
WDL_STRING_DEFPARM(0))
126 if (maxlen
>0) while (s
< maxlen
&& str
[s
]) s
++;
127 else s
=(int)strlen(str
);
132 void WDL_STRING_FUNCPREFIX
Set(const WDL_String
*str
, int maxlen
WDL_STRING_DEFPARM(0))
134 #ifdef WDL_STRING_FASTSUB_DEFINED
135 int s
= str
? str
->GetLength() : 0;
136 if (maxlen
>0 && maxlen
<s
) s
=maxlen
;
138 __doSet(0,str
?str
->Get():NULL
,s
,0);
140 Set(str
?str
->Get():NULL
, maxlen
); // might be faster: "partial" strlen
144 void WDL_STRING_FUNCPREFIX
Append(const char *str
, int maxlen
WDL_STRING_DEFPARM(0))
149 if (maxlen
>0) while (s
< maxlen
&& str
[s
]) s
++;
150 else s
=(int)strlen(str
);
153 __doSet(GetLength(),str
,s
,0);
156 void WDL_STRING_FUNCPREFIX
Append(const WDL_String
*str
, int maxlen
WDL_STRING_DEFPARM(0))
158 #ifdef WDL_STRING_FASTSUB_DEFINED
159 int s
= str
? str
->GetLength() : 0;
160 if (maxlen
>0 && maxlen
<s
) s
=maxlen
;
162 __doSet(GetLength(),str
?str
->Get():NULL
,s
,0);
164 Append(str
?str
->Get():NULL
, maxlen
); // might be faster: "partial" strlen
168 void WDL_STRING_FUNCPREFIX
DeleteSub(int position
, int len
)
170 int l
=m_hb
.GetSize()-1;
171 char *p
=(char *)m_hb
.Get();
172 if (l
<0 || !*p
|| position
< 0 || position
>= l
) return;
173 if (position
+len
> l
) len
=l
-position
;
176 memmove(p
+position
,p
+position
+len
,l
-position
-len
+1);
177 m_hb
.Resize(l
+1-len
,false);
181 void WDL_STRING_FUNCPREFIX
Insert(const char *str
, int position
, int maxlen
WDL_STRING_DEFPARM(0))
186 if (maxlen
>0) while (ilen
< maxlen
&& str
[ilen
]) ilen
++;
187 else ilen
=(int)strlen(str
);
190 const int srclen
= GetLength();
191 if (position
<0) position
=0;
192 else if (position
>srclen
) position
=srclen
;
193 if (ilen
>0) __doSet(position
,str
,ilen
,srclen
-position
);
196 void WDL_STRING_FUNCPREFIX
Insert(const WDL_String
*str
, int position
, int maxlen
WDL_STRING_DEFPARM(0))
198 #ifdef WDL_STRING_FASTSUB_DEFINED
199 int ilen
= str
? str
->GetLength() : 0;
200 if (maxlen
>0 && maxlen
<ilen
) ilen
=maxlen
;
202 const int srclen
= m_hb
.GetSize()>0 ? m_hb
.GetSize()-1 : 0;
203 if (position
<0) position
=0;
204 else if (position
>srclen
) position
=srclen
;
205 if (ilen
>0) __doSet(position
,str
->Get(),ilen
,srclen
-position
);
207 Insert(str
?str
->Get():NULL
, position
, maxlen
); // might be faster: "partial" strlen
211 bool WDL_STRING_FUNCPREFIX
SetLen(int length
, bool resizeDown
WDL_STRING_DEFPARM(false)
212 #ifdef WDL_STRING_FASTSUB_DEFINED
213 , char fillchar
WDL_STRING_DEFPARM(' ')
217 #ifdef WDL_STRING_FASTSUB_DEFINED
218 int osz
= m_hb
.GetSize()-1;
221 if (length
< 0) length
=0;
222 char *b
=(char*)m_hb
.ResizeOK(length
+1,resizeDown
);
225 #ifdef WDL_STRING_FASTSUB_DEFINED
226 const int fill
= length
-osz
;
227 if (fill
> 0) memset(b
+osz
,fillchar
,fill
);
235 void WDL_STRING_FUNCPREFIX
SetAppendFormattedArgs(bool append
, int maxlen
, const char* fmt
, va_list arglist
)
237 int offs
= append
? GetLength() : 0;
238 char *b
= (char*) m_hb
.ResizeOK(offs
+maxlen
+1,false);
245 int written
= _vsnprintf(b
, maxlen
+1, fmt
, arglist
);
246 if (written
< 0 || written
>=maxlen
) b
[written
=b
[0]?maxlen
:0]=0;
248 int written
= vsnprintf(b
, maxlen
+1, fmt
, arglist
);
249 if (written
> maxlen
) written
=maxlen
;
252 m_hb
.Resize(offs
+ written
+ 1,false);
255 void WDL_VARARG_WARN(printf
,3,4) WDL_STRING_FUNCPREFIX
SetFormatted(int maxlen
, const char *fmt
, ...)
258 va_start(arglist
, fmt
);
259 SetAppendFormattedArgs(false,maxlen
,fmt
,arglist
);
263 void WDL_VARARG_WARN(printf
,3,4) WDL_STRING_FUNCPREFIX
AppendFormatted(int maxlen
, const char *fmt
, ...)
266 va_start(arglist
, fmt
);
267 SetAppendFormattedArgs(true,maxlen
,fmt
,arglist
);
271 void WDL_STRING_FUNCPREFIX
Ellipsize(int minlen
, int maxlen
)
273 if (maxlen
>= 4 && m_hb
.GetSize() && GetLength() > maxlen
)
275 if (minlen
<0) minlen
=0;
276 char *b
= (char *)m_hb
.Get();
278 for (i
= maxlen
-4; i
>= minlen
; --i
)
282 memcpy(b
+i
, "...",4);
283 m_hb
.Resize(i
+4,false);
287 if (i
< minlen
&& maxlen
>= 4)
289 memcpy(b
+maxlen
-4, "...",4);
290 m_hb
.Resize(maxlen
,false);
294 const char * WDL_STRING_FUNCPREFIX
get_filepart() const // returns whole string if no dir chars
296 const char *s
= Get();
297 const char *p
= s
+ GetLength() - 1;
298 while (p
>= s
&& !WDL_IS_DIRCHAR(*p
)) --p
;
301 const char * WDL_STRING_FUNCPREFIX
get_fileext() const // returns ".ext" or end of string "" if no extension
303 const char *s
= Get();
304 const char *endp
= s
+ GetLength();
305 const char *p
= endp
- 1;
306 while (p
>= s
&& !WDL_IS_DIRCHAR(*p
))
308 if (*p
== '.') return p
;
313 bool WDL_STRING_FUNCPREFIX
remove_fileext() // returns true if extension was removed
315 const char *str
= Get();
316 int pos
= GetLength() - 1;
320 if (WDL_IS_DIRCHAR(c
)) break;
331 char WDL_STRING_FUNCPREFIX
remove_filepart(bool keepTrailingSlash
WDL_STRING_DEFPARM(false)) // returns directory character used, or 0 if string emptied
334 const char *str
= Get();
335 int pos
= GetLength() - 1;
339 if (WDL_IS_DIRCHAR(c
))
342 if (keepTrailingSlash
) ++pos
;
351 int WDL_STRING_FUNCPREFIX
remove_trailing_dirchars() // returns trailing dirchar count removed
354 const char *str
= Get();
355 const int l
= GetLength()-1;
358 char c
= str
[l
- cnt
];
359 if (!WDL_IS_DIRCHAR(c
)) break;
362 if (cnt
> 0) SetLen(l
+ 1 - cnt
);
365 #ifndef WDL_STRING_IMPL_ONLY
368 void WDL_STRING_FUNCPREFIX
__doSet(int offs
, const char *str
, int len
, int trailkeep
)
370 // if non-empty, or (empty and allocated and Set() rather than append/insert), then allow update, otherwise do nothing
371 if (len
==0 && !trailkeep
&& !offs
)
373 #ifdef WDL_STRING_FREE_ON_CLEAR
376 char *p
= (char *)m_hb
.ResizeOK(1,false);
380 else if (len
>0 && offs
>= 0)
382 const int oldsz
= m_hb
.GetSize();
383 const int newsz
=offs
+len
+trailkeep
+1;
384 const int growamt
= newsz
-oldsz
;
387 const char *oldb
= (const char *)m_hb
.Get();
388 const char *newb
= (const char *)m_hb
.ResizeOK(newsz
,false); // resize up if necessary
390 // in case str overlaps with input, keep it valid
391 if (str
&& newb
!= oldb
&& str
>= oldb
&& str
< oldb
+oldsz
) str
= newb
+ (str
- oldb
);
394 if (m_hb
.GetSize() >= newsz
)
396 char *newbuf
= (char *)m_hb
.Get();
397 if (trailkeep
>0) memmove(newbuf
+offs
+len
,newbuf
+offs
,trailkeep
);
398 if (str
) memmove(newbuf
+offs
,str
,len
);
401 // resize down if necessary
402 if (growamt
< 0) m_hb
.Resize(newsz
,false);
407 #undef WDL_STRING_FUNCPREFIX
408 #undef WDL_STRING_DEFPARM
409 #endif // ! WDL_STRING_INTF_ONLY
411 #ifndef WDL_STRING_IMPL_ONLY
414 #ifdef WDL_STRING_INTF_ONLY
415 void __doSet(int offs
, const char *str
, int len
, int trailkeep
);
422 #ifndef WDL_STRING_FASTSUB_DEFINED
423 #undef _WDL_STRING_H_
424 #define WDL_STRING_FASTSUB_DEFINED
425 #define WDL_String WDL_FastString
426 #include "wdlstring.h"
427 #undef WDL_STRING_FASTSUB_DEFINED