summaryrefslogtreecommitdiffstats
path: root/WDL/wdlstring.h
blob: 37c7d70ae4c7744915ef54816cbe17a46718b7dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
    WDL - wdlstring.h
    Copyright (C) 2005 and later, Cockos Incorporated
  
    This software is provided 'as-is', without any express or implied
    warranty.  In no event will the authors be held liable for any damages
    arising from the use of this software.

    Permission is granted to anyone to use this software for any purpose,
    including commercial applications, and to alter it and redistribute it
    freely, subject to the following restrictions:

    1. The origin of this software must not be misrepresented; you must not
       claim that you wrote the original software. If you use this software
       in a product, an acknowledgment in the product documentation would be
       appreciated but is not required.
    2. Altered source versions must be plainly marked as such, and must not be
       misrepresented as being the original software.
    3. This notice may not be removed or altered from any source distribution.
      
*/

/*

  This file provides a simple class for variable-length string manipulation.
  It provides only the simplest features, and does not do anything confusing like
  operator overloading. It uses a WDL_HeapBuf for internal storage.

  Actually: there are WDL_String and WDL_FastString -- the latter's Get() returns const char, and tracks
  the length of the string, which is often faster. Because of this, you are not permitted to directly modify
  the buffer returned by Get().

  
*/

#ifndef _WDL_STRING_H_
#define _WDL_STRING_H_

#include "heapbuf.h"
#include <stdio.h>
#include <stdarg.h>

#ifndef WDL_STRING_IMPL_ONLY
class WDL_String
{
  public:
  #ifdef WDL_STRING_INTF_ONLY
    void Set(const char *str, int maxlen=0);
    void Set(const WDL_String *str, int maxlen=0);
    void Append(const char *str, int maxlen=0);
    void Append(const WDL_String *str, int maxlen=0);
    void DeleteSub(int position, int len);
    void Insert(const char *str, int position, int maxlen=0);
    void Insert(const WDL_String *str, int position, int maxlen=0);
    #ifdef WDL_STRING_FASTSUB_DEFINED
    bool SetLen(int length, bool resizeDown=false, char fillchar=' '); // returns true on success
    #else
    bool SetLen(int length, bool resizeDown=false); // returns true on success
    #endif
    void Ellipsize(int minlen, int maxlen);
    const char *get_filepart() const; // returns whole string if no dir chars
    const char *get_fileext() const; // returns ".ext" or end of string "" if no extension
    bool remove_fileext(); // returns true if extension was removed
    char remove_filepart(bool keepTrailingSlash=false); // returns dir character used, or zero if string emptied
    int remove_trailing_dirchars(); // returns trailing dirchar count removed, will not convert "/" into ""

    void SetAppendFormattedArgs(bool append, int maxlen, const char* fmt, va_list arglist);
    void WDL_VARARG_WARN(printf,3,4) SetFormatted(int maxlen, const char *fmt, ...);
    void WDL_VARARG_WARN(printf,3,4) AppendFormatted(int maxlen, const char *fmt, ...);
  #endif

    const char *Get() const { return m_hb.GetSize()?(char*)m_hb.Get():""; }
    const char *GetAtOffs(int offs) const {
      const int l = GetLength();
      if (WDL_NOT_NORMALLY(offs < 0 || offs > l)) return Get()+l;
      return Get()+offs;
    }

  #ifdef WDL_STRING_FASTSUB_DEFINED
    int GetLength() const { int a = m_hb.GetSize(); return a>0?a-1:0; }

    // for binary-safe manipulations
    void SetRaw(const char *str, int len) { __doSet(0,str,len,0); }
    void AppendRaw(const char *str, int len) { __doSet(GetLength(),str,len,0); }
    void InsertRaw(const char *str, int position, int ilen)
    {
      const int srclen = GetLength();
      if (position<0) position=0;
      else if (position>srclen) position=srclen;
      if (ilen>0) __doSet(position,str,ilen,srclen-position);
    }

  #else
    char *Get()
    {
      if (m_hb.GetSize()) return (char *)m_hb.Get();
      static char c; c=0; return &c; // don't return "", in case it gets written to.
    }
    int GetLength() const { return m_hb.GetSize()?(int)strlen((const char*)m_hb.Get()):0; }
  #endif

    explicit WDL_String(int hbgran) : m_hb(hbgran) { }
    explicit WDL_String(const char *initial=NULL, int initial_len=0) : m_hb(128)
    {
      if (initial) Set(initial,initial_len);
    }
    WDL_String(const WDL_String &s) : m_hb(128) { Set(&s); }
    WDL_String(const WDL_String *s) : m_hb(128) { if (s && s != this) Set(s); }
    ~WDL_String() { }

    void SwapContentsWith(WDL_String *b) { m_hb.SwapContentsWith(&b->m_hb); }
#endif // ! WDL_STRING_IMPL_ONLY

#ifndef WDL_STRING_INTF_ONLY
  #ifdef WDL_STRING_IMPL_ONLY
    #define WDL_STRING_FUNCPREFIX WDL_String::
    #define WDL_STRING_DEFPARM(x)
  #else
    #define WDL_STRING_FUNCPREFIX 
    #define WDL_STRING_DEFPARM(x) =(x)
  #endif

    void WDL_STRING_FUNCPREFIX Set(const char *str, int maxlen WDL_STRING_DEFPARM(0))
    {
      int s=0;
      if (str)
      {
        if (maxlen>0) while (s < maxlen && str[s]) s++;
        else s=(int)strlen(str);   
      }
      __doSet(0,str,s,0);
    }

    void WDL_STRING_FUNCPREFIX Set(const WDL_String *str, int maxlen WDL_STRING_DEFPARM(0))
    {
      #ifdef WDL_STRING_FASTSUB_DEFINED
        int s = str ? str->GetLength() : 0;
        if (maxlen>0 && maxlen<s) s=maxlen;

        __doSet(0,str?str->Get():NULL,s,0);
      #else
        Set(str?str->Get():NULL, maxlen); // might be faster: "partial" strlen
      #endif
    }

    void WDL_STRING_FUNCPREFIX Append(const char *str, int maxlen WDL_STRING_DEFPARM(0))
    {
      int s=0;
      if (str)
      {
        if (maxlen>0) while (s < maxlen && str[s]) s++;
        else s=(int)strlen(str);
      }

      __doSet(GetLength(),str,s,0);
    }

    void WDL_STRING_FUNCPREFIX Append(const WDL_String *str, int maxlen WDL_STRING_DEFPARM(0))
    {
      #ifdef WDL_STRING_FASTSUB_DEFINED
        int s = str ? str->GetLength() : 0;
        if (maxlen>0 && maxlen<s) s=maxlen;

        __doSet(GetLength(),str?str->Get():NULL,s,0);
      #else
        Append(str?str->Get():NULL, maxlen); // might be faster: "partial" strlen
      #endif
    }

    void WDL_STRING_FUNCPREFIX DeleteSub(int position, int len)
    {
      int l=m_hb.GetSize()-1;
      char *p=(char *)m_hb.Get();
      if (l<0 || !*p || position < 0 || position >= l) return;
      if (position+len > l) len=l-position;
      if (len>0)
      {
        memmove(p+position,p+position+len,l-position-len+1);
        m_hb.Resize(l+1-len,false);
      }
    }

    void WDL_STRING_FUNCPREFIX Insert(const char *str, int position, int maxlen WDL_STRING_DEFPARM(0))
    {
      int ilen=0;
      if (str)
      {
        if (maxlen>0) while (ilen < maxlen && str[ilen]) ilen++;
        else ilen=(int)strlen(str);
      }

      const int srclen = GetLength();
      if (position<0) position=0;
      else if (position>srclen) position=srclen;
      if (ilen>0) __doSet(position,str,ilen,srclen-position);
    }

    void WDL_STRING_FUNCPREFIX Insert(const WDL_String *str, int position, int maxlen WDL_STRING_DEFPARM(0))
    {
      #ifdef WDL_STRING_FASTSUB_DEFINED
        int ilen = str ? str->GetLength() : 0;
        if (maxlen>0 && maxlen<ilen) ilen=maxlen;

        const int srclen = m_hb.GetSize()>0 ? m_hb.GetSize()-1 : 0;
        if (position<0) position=0;
        else if (position>srclen) position=srclen;
        if (ilen>0) __doSet(position,str->Get(),ilen,srclen-position);
      #else
        Insert(str?str->Get():NULL, position, maxlen); // might be faster: "partial" strlen
      #endif
    }

    bool WDL_STRING_FUNCPREFIX SetLen(int length, bool resizeDown WDL_STRING_DEFPARM(false)
      #ifdef WDL_STRING_FASTSUB_DEFINED
        , char fillchar WDL_STRING_DEFPARM(' ')
      #endif
        )
    {                       
      #ifdef WDL_STRING_FASTSUB_DEFINED
      int osz = m_hb.GetSize()-1;
      if (osz<0)osz=0;
      #endif
      if (length < 0) length=0;
      char *b=(char*)m_hb.ResizeOK(length+1,resizeDown);
      if (b) 
      {
        #ifdef WDL_STRING_FASTSUB_DEFINED
          const int fill = length-osz;
          if (fill > 0) memset(b+osz,fillchar,fill);
        #endif
        b[length]=0;
        return true;
      }
      return false;
    }

    void WDL_STRING_FUNCPREFIX SetAppendFormattedArgs(bool append, int maxlen, const char* fmt, va_list arglist) 
    {
      int offs = append ? GetLength() : 0;
      char *b= (char*) m_hb.ResizeOK(offs+maxlen+1,false);
      
      if (!b) return;

      b+=offs;

      #ifdef _WIN32
        int written = _vsnprintf(b, maxlen+1, fmt, arglist);
        if (written < 0 || written>=maxlen) b[written=b[0]?maxlen:0]=0;
      #else
        int written = vsnprintf(b, maxlen+1, fmt, arglist);
        if (written > maxlen) written=maxlen;
      #endif

      m_hb.Resize(offs + written + 1,false);
    }

    void WDL_VARARG_WARN(printf,3,4) WDL_STRING_FUNCPREFIX SetFormatted(int maxlen, const char *fmt, ...) 
    {
      va_list arglist;
      va_start(arglist, fmt);
      SetAppendFormattedArgs(false,maxlen,fmt,arglist);
      va_end(arglist);
    }

    void WDL_VARARG_WARN(printf,3,4) WDL_STRING_FUNCPREFIX AppendFormatted(int maxlen, const char *fmt, ...) 
    {
      va_list arglist;
      va_start(arglist, fmt);
      SetAppendFormattedArgs(true,maxlen,fmt,arglist);
      va_end(arglist);
    }

    void WDL_STRING_FUNCPREFIX Ellipsize(int minlen, int maxlen)
    {
      if (maxlen >= 4 && m_hb.GetSize() && GetLength() > maxlen) 
      {
        if (minlen<0) minlen=0;
        char *b = (char *)m_hb.Get();
        int i;
        for (i = maxlen-4; i >= minlen; --i) 
        {
          if (b[i] == ' ') 
          {
            memcpy(b+i, "...",4);
            m_hb.Resize(i+4,false);
            break;
          }
        }
        if (i < minlen && maxlen >= 4) 
        {
          memcpy(b+maxlen-4, "...",4);    
          m_hb.Resize(maxlen,false);
        }
      }
    }
    const char * WDL_STRING_FUNCPREFIX get_filepart() const // returns whole string if no dir chars
    {
      const char *s = Get();
      const char *p = s + GetLength() - 1;
      while (p >= s && !WDL_IS_DIRCHAR(*p)) --p;
      return p + 1;
    }
    const char * WDL_STRING_FUNCPREFIX get_fileext() const // returns ".ext" or end of string "" if no extension
    {
      const char *s = Get();
      const char *endp = s + GetLength();
      const char *p = endp - 1;
      while (p >= s && !WDL_IS_DIRCHAR(*p))
      {
        if (*p == '.') return p;
        --p;
      }
      return endp;
    }
    bool WDL_STRING_FUNCPREFIX remove_fileext() // returns true if extension was removed
    {
      const char *str = Get();
      int pos = GetLength() - 1;
      while (pos >= 0)
      {
        char c = str[pos];
        if (WDL_IS_DIRCHAR(c)) break;
        if (c == '.')
        {
          SetLen(pos);
          return true;
        }
        --pos;
      }
      return false;
    }

    char WDL_STRING_FUNCPREFIX remove_filepart(bool keepTrailingSlash WDL_STRING_DEFPARM(false)) // returns directory character used, or 0 if string emptied
    {
      char rv=0;
      const char *str = Get();
      int pos = GetLength() - 1;
      while (pos > 0)
      {
        char c = str[pos];
        if (WDL_IS_DIRCHAR(c)) 
        {
          rv=c;
          if (keepTrailingSlash) ++pos;
          break;
        }
        --pos;
      }
      SetLen(pos);
      return rv;
    }

    int WDL_STRING_FUNCPREFIX remove_trailing_dirchars() // returns trailing dirchar count removed
    {
      int cnt = 0;
      const char *str = Get();
      const int l = GetLength()-1;
      while (cnt < l)
      {
        char c = str[l - cnt];
        if (!WDL_IS_DIRCHAR(c)) break;
        ++cnt;
      }
      if (cnt > 0) SetLen(l + 1 - cnt);
      return cnt;
    }
#ifndef WDL_STRING_IMPL_ONLY
  private:
#endif
    void WDL_STRING_FUNCPREFIX __doSet(int offs, const char *str, int len, int trailkeep)
    {   
      // if non-empty, or (empty and allocated and Set() rather than append/insert), then allow update, otherwise do nothing
      if (len==0 && !trailkeep && !offs)
      {
        #ifdef WDL_STRING_FREE_ON_CLEAR
          m_hb.Resize(0,true);
        #else
          char *p = (char *)m_hb.ResizeOK(1,false);
          if (p) *p=0;
        #endif
      }
      else if (len>0 && offs >= 0) 
      {
        const int oldsz = m_hb.GetSize();
        const int newsz=offs+len+trailkeep+1;
        const int growamt = newsz-oldsz;
        if (growamt > 0)
        {
          const char *oldb = (const char *)m_hb.Get();
          const char *newb = (const char *)m_hb.ResizeOK(newsz,false); // resize up if necessary

          // in case str overlaps with input, keep it valid
          if (str && newb != oldb && str >= oldb && str < oldb+oldsz) str = newb + (str - oldb);
        }

        if (m_hb.GetSize() >= newsz)
        {
          char *newbuf = (char *)m_hb.Get();
          if (trailkeep>0) memmove(newbuf+offs+len,newbuf+offs,trailkeep);
          if (str) memmove(newbuf+offs,str,len);
          newbuf[newsz-1]=0;

          // resize down if necessary
          if (growamt < 0) m_hb.Resize(newsz,false);
        }
      }
    }

  #undef WDL_STRING_FUNCPREFIX
  #undef WDL_STRING_DEFPARM
#endif // ! WDL_STRING_INTF_ONLY

#ifndef WDL_STRING_IMPL_ONLY

  private:
    #ifdef WDL_STRING_INTF_ONLY
      void __doSet(int offs, const char *str, int len, int trailkeep);
    #endif

    WDL_HeapBuf m_hb;
};
#endif

#ifndef WDL_STRING_FASTSUB_DEFINED
#undef _WDL_STRING_H_
#define WDL_STRING_FASTSUB_DEFINED
#define WDL_String WDL_FastString
#include "wdlstring.h"
#undef WDL_STRING_FASTSUB_DEFINED
#undef WDL_String
#endif

#endif