-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvstring_internal.cpp
More file actions
2132 lines (1897 loc) · 53.4 KB
/
vstring_internal.cpp
File metadata and controls
2132 lines (1897 loc) · 53.4 KB
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
#
# VSTRING Library
#
# Copyright (c) 1996-2026 Vladi Belperchinov-Shabanski "Cade"
# http://cade.noxrun.com/ <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org>
#
# Distributed under the GPL license, you should receive copy of GPLv2!
#
# SEE 'README', 'LICENSE' OR 'COPYING' FILE FOR LICENSE AND OTHER DETAILS!
#
# VSTRING library provides wide set of string manipulation features
# including dynamic string object that can be freely exchanged with
# standard char* (or wchar_t*) type, so there is no need to change
# function calls nor the implementation when you change from
# char* to VString (and from wchar_t* to WString).
#
***************************************************************************/
#include "vstring_internal.h"
ssize_t str_len( const VS_CHAR *s )
{
return VS_FN_STRLEN( s );
}
/****************************************************************************
**
** VSTRING BOX
**
****************************************************************************/
VS_STRING_BOX* VS_STRING_BOX::clone()
{
VS_STRING_BOX* box = new VS_STRING_BOX();
box->resize_buf( size );
box->sl = sl;
box->compact = compact;
vs_memcpy( box->s, s, size ); //TODO: FIXME: just sl?
return box;
}
void VS_STRING_BOX::resize_buf( int new_size )
{
/* FIXME: this breaks a lot of things: ==, strcmp, const VS_CHAR*, ...
if ( new_size == 0 )
{
sl = 0;
if ( s ) free( s );
s = NULL;
size = 0;
return;
}
*/
new_size++; /* for the trailing 0 */
if ( ! compact )
{
new_size = new_size / block_size + ( new_size % block_size != 0 );
new_size *= block_size;
}
if( s == NULL )
{ /* first time alloc */
s = (VS_CHAR*)malloc( new_size * sizeof( VS_CHAR ) );
ASSERT( s );
s[ 0 ] = 0;
size = new_size;
sl = 0;
} else
if ( size != new_size )
{ /* expand/shrink */
s = (VS_CHAR*)realloc( s, new_size * sizeof( VS_CHAR ) );
size = new_size;
s[ size - 1 ] = 0;
if ( sl > size - 1 ) sl = size - 1;
}
}
void VS_STRING_BOX::set_block_size( int new_block_size )
{
block_size = new_block_size < 1 ? VSTRING_DEFAULT_BLOCK_SIZE : new_block_size;
}
VS_STRING_BOX::~VS_STRING_BOX()
{
undef();
if ( s ) free( s );
/*
FILE* fo = fopen( "/tmp/vstrboxstats.txt", "a" );
fprintf( fo, "cbs=%d sbs=%d sbc=%d sbg=%f\n", cbs, sbs, sbc, sbg );
fclose( fo );
*/
}
/****************************************************************************
**
** VSTRING
**
****************************************************************************/
VS_STRING_CLASS::VS_STRING_CLASS( VS_STRING_CLASS_R rs )
{
box = new VS_STRING_BOX();
set( rs.data() );
}
void VS_STRING_CLASS::detach()
{
if ( box->refs() == 1 ) return;
VS_STRING_BOX *new_box = box->clone();
box->unref();
box = new_box;
}
void VS_STRING_CLASS::i( const int n )
{
VS_CHAR tmp[64];
VS_FN_SPRINTF( tmp, LENOF_VS_CHAR(tmp), VS_CHAR_L("%d"), n );
set( tmp );
}
void VS_STRING_CLASS::l( const long n )
{
VS_CHAR tmp[64];
VS_FN_SPRINTF( tmp, LENOF_VS_CHAR(tmp), VS_CHAR_L("%ld"), n );
set( tmp );
}
void VS_STRING_CLASS::ll( const long long n )
{
VS_CHAR tmp[64];
VS_FN_SPRINTF( tmp, LENOF_VS_CHAR(tmp), VS_CHAR_L("%lld"), n );
set( tmp );
}
void VS_STRING_CLASS::f( const double d )
{
VS_CHAR tmp[64];
VS_FN_SPRINTF( tmp, LENOF_VS_CHAR(tmp), VS_CHAR_L("%.10f"), d );
int z = VS_FN_STRLEN( tmp );
while( tmp[z-1] == VS_CHAR_L('0') ) z--;
if ( tmp[z-1] == VS_CHAR_L('.') ) z--;
tmp[z] = 0;
set( tmp );
}
void VS_STRING_CLASS::fi( const double d ) // sets double as int (w/o frac)
{
VS_CHAR tmp[64];
VS_FN_SPRINTF( tmp, LENOF_VS_CHAR(tmp), VS_CHAR_L("%.0f"), d );
set( tmp );
}
void VS_STRING_CLASS::set( const VS_CHAR* ps )
{
if (ps == NULL || ps[0] == 0)
{
resize( 0 );
ASSERT( box->s );
box->sl = 0;
box->s[ 0 ] = 0;
}
else
{
int sl = str_len( ps );
resize( sl );
vs_memcpy( box->s, ps, sl );
box->sl = sl;
box->s[ sl ] = 0;
}
}
void VS_STRING_CLASS::cat( const VS_CHAR* ps )
{
if (ps == NULL) return;
if (ps[0] == 0) return;
int psl = str_len( ps );
resize( box->sl + psl );
vs_memcpy( box->s + box->sl, ps, psl );
box->s[ box->sl + psl ] = 0;
box->sl += psl;
}
void VS_STRING_CLASS::setn( const VS_CHAR* ps, int len )
{
if ( !ps || len < 1 )
{
resize( 0 );
box->sl = 0;
box->s[ 0 ] = 0;
return;
}
int z = str_len( ps );
if ( len < z ) z = len;
resize( z );
box->sl = z;
vs_memcpy( box->s, ps, z );
box->s[z] = 0;
}
void VS_STRING_CLASS::catn( const VS_CHAR* ps, int len )
{
if ( !ps || len < 1 ) return;
int z = str_len( ps );
if ( len < z ) z = len;
resize( box->sl + z );
vs_memcpy( box->s + box->sl, ps, z );
box->sl += z;
box->s[ box->sl ] = 0;
}
const VS_STRING_CLASS& VS_STRING_CLASS::operator = ( const VS_STRING_CLASS_R& rs )
{
set( rs.data() );
return *this;
}
const VS_STRING_CLASS& VS_STRING_CLASS::operator = ( const VS_CHAR_R* prs )
{
set( prs );
return *this;
}
/****************************************************************************
**
** VS_STRING_CLASS Functions (for class VS_STRING_CLASS)
**
****************************************************************************/
VS_STRING_CLASS &str_mul( VS_STRING_CLASS &target, int n ) // multiplies the VS_STRING_CLASS n times, i.e. "1"*5 = "11111"
{
target.resize( target.box->sl * n );
str_mul( target.box->s, n );
target.box->sl *= n;
return target;
}
VS_STRING_CLASS &str_del( VS_STRING_CLASS &target, int pos, int len ) // deletes `len' VS_CHARs starting from `pos'
{
if ( pos > target.box->sl || pos < 0 ) return target;
target.detach();
str_del( target.box->s, pos, len );
if ( pos + len < target.box->sl )
target.box->sl -= len;
else
target.box->sl = pos;
return target;
}
VS_STRING_CLASS &str_ins( VS_STRING_CLASS &target, int pos, const VS_CHAR* s ) // inserts `s' in position `pos'
{
if ( pos > target.box->sl || pos < 0 ) return target;
int ssl = str_len(s);
target.resize( target.box->sl + ssl );
str_ins( target.box->s, pos, s );
target.box->sl += ssl;
return target;
}
VS_STRING_CLASS &str_ins_ch( VS_STRING_CLASS &target, int pos, VS_CHAR ch ) // inserts `ch' in position `pos'
{
if ( pos > target.box->sl || pos < 0 ) return target;
target.resize( target.box->sl + 1 );
str_ins_ch( target.box->s, pos, ch );
target.box->sl++;
return target;
}
VS_STRING_CLASS &str_replace( VS_STRING_CLASS &target, const VS_CHAR* out, const VS_CHAR* in ) // replace `out' w. `in'
{
int outl = str_len( out );
int inl = str_len( in );
int z = str_find( target, out );
while(z != -1)
{
str_del( target, z, outl );
str_ins( target, z, in );
z = str_find( target, out, z + inl );
}
ASSERT(target.check());
return target;
}
VS_STRING_CLASS &str_copy( VS_STRING_CLASS &target, const VS_CHAR* source, int pos, int len ) // returns `len' VS_CHARs from `pos'
{
if( __str_copy_calc_offsets( source, pos, len ) )
{
target.undef();
return target;
}
target.resize( len );
vs_memmove( target.box->s, source + pos, len );
target.box->s[ len ] = 0;
target.box->sl = len;
return target;
}
VS_STRING_CLASS &str_left( VS_STRING_CLASS &target, const VS_CHAR* source, int len ) // returns `len' VS_CHARs from the left
{
return str_copy( target, source, 0, len );
}
VS_STRING_CLASS &str_right( VS_STRING_CLASS &target, const VS_CHAR* source, int len ) // returns `len' VS_CHARs from the right
{
return str_copy( target, source, str_len( source ) - len, len );
}
VS_STRING_CLASS &str_sleft( VS_STRING_CLASS &target, int len ) // SelfLeft -- just as 'Left' but works on `this'
{
if ( len < target.box->sl )
{
target.detach();
target.box->s[len] = 0;
target.fix();
}
return target;
}
VS_STRING_CLASS &str_sright( VS_STRING_CLASS &target, int len ) // SelfRight -- just as 'Right' but works on `this'
{
target.detach();
str_sright( target.box->s, len );
target.fix();
return target;
}
VS_STRING_CLASS &str_trim_left( VS_STRING_CLASS &target, int len ) // trims `len' VS_CHARs from the beginning (left)
{
target.detach();
str_trim_left( target.box->s, len );
target.fix();
return target;
}
VS_STRING_CLASS &str_trim_right( VS_STRING_CLASS &target, int len ) // trim `len' VS_CHARs from the end (right)
{
target.detach();
str_trim_right( target.box->s, len );
target.fix();
return target;
}
VS_STRING_CLASS &str_cut_left( VS_STRING_CLASS &target, const VS_CHAR* charlist ) // remove all VS_CHARs `charlist' from the beginning (i.e. from the left)
{
target.detach();
str_cut_left( target.box->s, charlist );
target.fix();
return target;
}
VS_STRING_CLASS &str_cut_right( VS_STRING_CLASS &target, const VS_CHAR* charlist ) // remove all VS_CHARs `charlist' from the end (i.e. from the right)
{
target.detach();
str_cut_right( target.box->s, charlist );
target.fix();
return target;
}
VS_STRING_CLASS &str_cut( VS_STRING_CLASS &target, const VS_CHAR* charlist ) // does `CutR(charlist);CutL(charlist);'
{
target.detach();
str_cut_left( target.box->s, charlist );
str_cut_right( target.box->s, charlist );
target.fix();
return target;
}
VS_STRING_CLASS &str_cut_spc( VS_STRING_CLASS &target ) // does `Cut(" ");'
{
return str_cut( target, VS_CHAR_L(" ") );
}
VS_STRING_CLASS &str_pad( VS_STRING_CLASS &target, int len, VS_CHAR ch )
{
target.resize( (len > 0) ? len : -len );
str_pad( target.box->s, len, ch );
target.fixlen();
return target;
}
VS_STRING_CLASS &str_comma( VS_STRING_CLASS &target, VS_CHAR delim )
{
int new_size = str_len( target ) / 3 + str_len( target );
target.resize( new_size );
str_comma( target.box->s, delim );
target.fix();
return target;
}
void str_set_ch( VS_STRING_CLASS &target, int pos, const VS_CHAR ch ) // sets `ch' VS_CHAR at position `pos'
{
if ( pos < 0 ) pos = target.box->sl + pos;
if ( pos < 0 || pos >= target.box->sl ) return;
if (target.box->s[pos] != ch) target.detach();
target.box->s[pos] = ch;
}
VS_CHAR str_get_ch( VS_STRING_CLASS &target, int pos ) // return VS_CHAR at position `pos'
{
if ( pos < 0 ) pos = target.box->sl + pos;
if ( pos < 0 || pos >= target.box->sl ) return 0;
return target.box->s[pos];
}
void str_add_ch( VS_STRING_CLASS &target, const VS_CHAR ch ) // adds `ch' at the end
{
int sl = target.box->sl;
if( sl + 1 >= target.box->size ) target.resize( sl + 1 );
target.box->s[sl] = ch;
target.box->s[sl+1] = 0;
target.box->sl++;
}
void str_add_ch_range( VS_STRING_CLASS &target, const VS_CHAR fr, const VS_CHAR to ) // adds all from `fr' to 'to' at the end
{
if( fr > to ) return;
target.resize( target.box->sl + ( to - fr ) + 1 );
for( int i = fr; i <= to; i++ )
target.box->s[target.box->sl++] = i;
target.box->s[target.box->sl] = 0;
}
VS_CHAR* str_word( VS_STRING_CLASS &target, const VS_CHAR* delimiters, VS_CHAR* result, size_t result_max_count )
{
target.detach();
str_word( target.box->s, delimiters, result, result_max_count );
target.fix();
return result[0] ? result : NULL;
}
VS_CHAR* str_rword( VS_STRING_CLASS &target, const VS_CHAR* delimiters, VS_CHAR* result, size_t result_max_count )
{
target.detach();
str_rword( target.box->s, delimiters, result, result_max_count );
target.fix();
return result;
}
int sprintf( int init_size, VS_STRING_CLASS &target, const VS_CHAR *format, ... )
{
VS_CHAR *tmp = new VS_CHAR[init_size];
va_list vlist;
va_start( vlist, format );
int res = VS_FN_VSPRINTF( tmp, init_size, format, vlist );
va_end( vlist );
target = tmp;
delete [] tmp;
return res;
}
int sprintf( VS_STRING_CLASS &target, const VS_CHAR *format, ... )
{
#define VSPRINTF_BUF_SIZE 1024
VS_CHAR tmp[VSPRINTF_BUF_SIZE];
va_list vlist;
va_start( vlist, format );
int res = VS_FN_VSPRINTF( tmp, VSPRINTF_BUF_SIZE, format, vlist );
va_end( vlist );
target = tmp;
return res;
}
VS_STRING_CLASS& str_tr ( VS_STRING_CLASS& target, const VS_CHAR *from, const VS_CHAR *to )
{
target.detach();
str_tr( target.box->s, from, to );
return target;
}
VS_STRING_CLASS& str_up ( VS_STRING_CLASS& target )
{
target.detach();
str_up( target.box->s );
return target;
}
VS_STRING_CLASS& str_low( VS_STRING_CLASS& target )
{
target.detach();
str_low( target.box->s );
return target;
}
VS_STRING_CLASS& str_flip_case( VS_STRING_CLASS& target )
{
target.detach();
str_flip_case( target.box->s );
return target;
}
VS_STRING_CLASS& str_reverse( VS_STRING_CLASS& target )
{
target.detach();
str_reverse( target.box->s );
return target;
}
VS_STRING_CLASS &str_squeeze( VS_STRING_CLASS &target, const VS_CHAR* sq_VS_CHARs ) // squeeze repeating VS_CHARs to one only
{
target.detach();
str_squeeze( target.box->s, sq_VS_CHARs );
target.fix();
return target;
}
/* utilities */
void VS_STRING_CLASS::print() // print string data to stdout (console)
{
#ifdef _VSTRING_WIDE_
wprintf( L"%ls\n", box->s );
#else
printf( "%s\n", box->s );
#endif
}
/* conversions/reversed char type functions */
void VS_STRING_CLASS::set( const VS_CHAR_R* prs )
{
if (prs == NULL || prs[0] == 0)
resize( 0 );
else
{
#ifdef _VSTRING_WIDE_
set_failsafe( prs );
#else
int rz = VS_FN_CONVERT( NULL, prs, 0 ); // calc required "result size"
if( rz == -1 )
return resize( 0 );
resize( rz );
VS_FN_CONVERT( box->s, prs, rz );
box->s[rz] = 0;
box->sl = rz;
#endif
}
}
#ifdef _VSTRING_WIDE_
int VS_STRING_CLASS::set_failsafe( const char* mbs )
{
int err = 0;
undef();
mbtowc( NULL, NULL, 0 ); /* reset mbtowc shift state */
wchar_t wch;
const char* ps = mbs;
while( ps )
{
int r = mbtowc( &wch, ps, 4 );
if( r == -1 )
{
err++;
wch = 0xFFFD;
ps++;
}
else if( r > 0 )
{
ps += r;
}
else
{
return err;
}
str_add_ch( *this, wch );
}
return err;
}
#endif
/****************************************************************************
**
** VS_STRING_CLASS Functions (for class VS_STRING_CLASS)
**
****************************************************************************/
/****************************************************************************
**
** VS_STRING_CLASS Functions (for VS_CHAR*)
**
****************************************************************************/
VS_CHAR* str_set( VS_CHAR* target, const VS_CHAR* ps )
{
target[0] = 0;
if (ps) VS_FN_STRCPY( target, ps );
VS_FN_STRCPY( target, ps );
return target;
}
VS_CHAR* str_mul( VS_CHAR* target, int n ) // multiplies the string n times, i.e. "1"*5 = "11111"
{
if ( n < 0 ) return target;
if ( n == 0 )
{
target[0] = 0;
return target;
}
int sl = str_len( target );
int z = (n - 1) * sl;
while( z > 0 )
{
vs_memcpy( target + z, target, sl );
z -= sl;
}
target[sl*n] = 0;
return target;
}
int str_find( const VS_CHAR* target, const VS_CHAR c, int startpos ) // returns first zero-based position of VS_CHAR, or -1 if not found
{
if (startpos < 0) return -1;
int sl = str_len( target );
if (startpos >= sl) return -1;
const VS_CHAR* pc = VS_FN_STRCHR( target + startpos, c );
if( ! pc )
return -1;
return pc - target;
}
int str_rfind( const VS_CHAR* target, const VS_CHAR c, int startpos ) // returns last zero-based position of VS_CHAR, or -1 if not found
{
if( startpos == 0 )
{
const VS_CHAR* pc = VS_FN_STRRCHR( target, c );
if( ! pc )
return -1;
return pc - target;
}
else
{
int sl = str_len( target );
int pos = startpos > 0 ? startpos : sl + startpos;
if( pos < 0 ) return -1;
if( pos >= sl ) pos = sl - 1;
while( pos > -1 )
{
if( target[pos] == c ) return pos;
pos--;
}
return pos;
}
}
int str_find( const VS_CHAR* target, const VS_CHAR* s, int startpos ) // returns first zero-based position of VS_STRING_CLASS, or -1 if not found
{
if (startpos < 0) return -1;
int sl = str_len( target );
if (startpos >= sl) return -1;
const VS_CHAR* pc = VS_FN_STRSTR( target + startpos, s );
if( ! pc )
return -1;
return pc - target;
}
int str_rfind( const VS_CHAR* target, const VS_CHAR* s, int startpos ) // returns last zero-based position of VS_STRING_CLASS, or -1 if not found
{
int sl = str_len( target );
int sls = str_len( s );
int z = sl - sls;
if( startpos < 0 )
{
z += startpos;
if( z < 0 ) return -1;
}
else if( startpos > 0 )
{
if( startpos > z ) return -1;
z = startpos;
}
while ( z > 0 )
{
if ( VS_FN_STRNCMP( target + z, s, sls ) == 0 ) return z;
z--;
}
return -1;
}
VS_CHAR* str_del( VS_CHAR* target, int pos, int len ) // deletes `len' VS_CHARs starting from `pos'
{
int sl = str_len( target );
if ( pos > sl || pos < 0 ) return target;
int z = sl - pos - len;
if ( pos + len < sl )
vs_memmove( target + pos, target + pos + len, z + 1 );
else
target[pos] = 0;
return target;
}
VS_CHAR* str_ins( VS_CHAR* target, int pos, const VS_CHAR* s ) // inserts `s' in position `pos'
{
int sl = str_len( target );
if ( pos > sl || pos < 0 ) return target;
int sls = str_len( s );
if ( sls < 1 ) return target;
vs_memmove( target + pos + sls, target + pos, sl - pos + 1 );
vs_memmove( target + pos, s, sls );
return target;
}
VS_CHAR* str_ins_ch( VS_CHAR* target, int pos, VS_CHAR ch ) // inserts `ch' in position `pos'
{
VS_CHAR tmp[2];
tmp[0] = ch;
tmp[1] = 0;
str_ins( target, pos, tmp );
return target;
}
VS_CHAR* str_replace( VS_CHAR* target, const VS_CHAR* out, const VS_CHAR* in ) // replace `out' w. `in'
{
int outl = str_len( out );
int inl = str_len( in );
int z = str_find( target, out );
while(z != -1)
{
str_del( target, z, outl );
str_ins( target, z, in );
z = str_find( target, out, z + inl );
}
return target;
}
inline int __str_copy_calc_offsets( const VS_CHAR* source, int& pos, int& len )
{
ASSERT( len >= -1 );
int sl = str_len( source );
if ( pos < 0 )
{
pos = sl + pos;
if ( pos < 0 ) pos = 0;
}
if ( pos < 0 || pos >= sl ) return 1;
if ( len == -1 ) len = sl - pos; // untill the end of the string (default arg)
if ( len < 1 ) return 1;
if ( pos + len >= sl ) len = sl - pos;
return 0;
}
VS_CHAR* str_copy( VS_CHAR* target, const VS_CHAR* source, int pos, int len ) // returns `len' VS_CHARs from `pos'
{
if( __str_copy_calc_offsets( source, pos, len ) )
{
target[ 0 ] = 0;
return target;
}
vs_memmove( target, source + pos, len );
target[ len ] = 0;
return target;
}
VS_CHAR* str_left( VS_CHAR* target, const VS_CHAR* source, int len ) // returns `len' VS_CHARs from the left
{
return str_copy( target, source, 0, len );
}
VS_CHAR* str_right( VS_CHAR* target, const VS_CHAR* source, int len ) // returns `len' VS_CHARs from the right
{
return str_copy( target, source, str_len( source ) - len, len );
}
VS_CHAR* str_sleft( VS_CHAR* target, int len ) // SelfLeft -- just as 'Left' but works on `this'
{
if ( len < str_len(target) && len >= 0 ) target[len] = 0;
return target;
}
VS_CHAR* str_sright( VS_CHAR* target, int len ) // SelfRight -- just as 'Right' but works on `this'
{
int sl = str_len( target );
if( len < sl )
{
vs_memmove( target, target + ( sl - len ), len + 1 );
target[len] = 0;
}
return target;
}
VS_CHAR* str_trim_left( VS_CHAR* target, int len ) // trims `len' VS_CHARs from the beginning (left)
{
int sl = str_len( target );
int sr = sl - len; // result string length (without trailing 0)
if( sr >= sl )
return target;
if( sr > 0 )
{
vs_memmove( target, target + len, sr + 1 );
target[sr] = 0;
}
else
target[0] = 0;
return target;
}
VS_CHAR* str_trim_right( VS_CHAR* target, int len ) // trim `len' VS_CHARs from the end (right)
{
int sl = str_len( target );
int sr = sl - len; // result string length (without trailing 0)
if( sr >= sl )
return target;
if( sr > 0 )
target[sr] = 0;
else
target[0] = 0;
return target;
}
void str_set_ch( VS_CHAR* target, int pos, const VS_CHAR ch ) // sets `ch' VS_CHAR at position `pos'
{
int sl = str_len( target );
if ( pos < 0 )
pos = sl + pos;
if ( pos < 0 || pos >= sl ) return;
target[pos] = ch;
}
VS_CHAR str_get_ch( VS_CHAR* target, int pos ) // return VS_CHAR at position `pos'
{
int sl = str_len( target );
if ( pos < 0 )
pos = sl + pos;
if ( pos < 0 || pos >= sl ) return 0;
return target[pos];
}
void str_add_ch( VS_CHAR* target, const VS_CHAR ch ) // adds `ch' at the end
{
int sl = str_len( target );
target[sl] = ch;
target[sl+1] = 0;
}
void str_add_ch_range( VS_CHAR* target, const VS_CHAR fr, const VS_CHAR to ) // adds all from `fr' to 'to' at the end
{
if( fr > to ) return;
int sl = str_len( target );
for( int i = fr; i < to; i++ )
target[sl++] = i;
target[sl] = 0;
}
// return first `word', i.e. from pos 0 to first found delimiter VS_CHAR
// after that deletes this `word' from the target
VS_CHAR* str_word( VS_CHAR* target, const VS_CHAR* delimiters, VS_CHAR* result, size_t result_max_count )
{
if ( result_max_count == 0 ) return NULL;
int z = 0;
int sl = str_len( target );
while ( (VS_FN_STRCHR(delimiters, target[z]) == NULL) && (target[z] != 0) )
z++;
/* copy at most result_max_count - 1 chars/wchars; always NUL-terminate.
the full word is still consumed from target. */
int cz = z;
if ( (size_t)cz > result_max_count - 1 ) cz = (int)(result_max_count - 1);
vs_memmove( result, target, cz );
result[cz] = 0;
if ( z > 0 )
{
if ( z < sl )
vs_memmove( target, target + z + 1, sl - z ); /* including trailing zero */
else
target[0] = 0;
}
return result[0] ? result : NULL;
}
// ...same but `last' word
VS_CHAR* str_rword( VS_CHAR* target, const VS_CHAR* delimiters, VS_CHAR* result, size_t result_max_count )
{
if ( result_max_count == 0 ) return NULL;
result[0] = 0;
int sl = str_len( target );
if ( sl == 0 ) return NULL; /* fixes pre-existing target[-1] OOB read */
int z = sl - 1;
while ( VS_FN_STRCHR( delimiters, target[z] ) == NULL && z > 0 ) z--;
if ( z < 0 ) return NULL;
int cz = sl - z - 1;
if ( (size_t)cz > result_max_count - 1 ) cz = (int)(result_max_count - 1);
vs_memmove( result, target + z + 1, cz );
result[cz] = 0;
target[z] = 0;
return result;
}
VS_CHAR* str_cut_left( VS_CHAR* target, const VS_CHAR* charlist ) // remove all VS_CHARs `charlist' from the beginning (i.e. from the left)
{
int sl = str_len(target);
if (sl == 0) return target;
int z = 0;
while ((VS_FN_STRCHR(charlist, target[z]) != NULL) && (target[z] != 0)) z++;
if (z == 0) return target;
vs_memmove( target, target + z, sl - z + 1 );
return target;
}
VS_CHAR* str_cut_right( VS_CHAR* target, const VS_CHAR* charlist ) // remove all VS_CHARs `charlist' from the end (i.e. from the right)
{
if (str_len(target) == 0) return target;
int z = str_len(target) - 1;
while ((VS_FN_STRCHR(charlist, target[z]) != NULL) && (z > 0)) z--;
target[z+1] = 0;
return target;
}
VS_CHAR* str_cut( VS_CHAR* target, const VS_CHAR* charlist ) // does `CutR(charlist);CutL(charlist);'
{
str_cut_left( target, charlist );
str_cut_right( target, charlist );
return target;
}
VS_CHAR* str_cut_spc( VS_CHAR* target ) // does `Cut(" ");'
{
str_cut_left( target, VS_CHAR_L(" ") );
str_cut_right( target, VS_CHAR_L(" ") );
return target;
}
// expand string to width 'len' filling with VS_CHAR 'ch'
// if len > 0 target will be padded right, else left
VS_CHAR* str_pad( VS_CHAR* target, int len, VS_CHAR ch )
{
int sl = str_len( target );
int _len;
_len = (len >= 0) ? len : - len;
if ( _len <= sl )
{
target[_len] = 0;
return target;
}
// FIXME: do it without new mem
VS_CHAR *tmp = new VS_CHAR[_len + 1]; // result buffer -- need len + 1
tmp[0] = ch;
tmp[1] = 0;
str_mul( tmp, _len - sl );
if ( len < 0 )
{
VS_FN_STRCAT( target, tmp );
}
else
{
// TODO: optimize, 2 memcpy-s
VS_FN_STRCAT( tmp, target );
VS_FN_STRCPY( target, tmp );
}
delete [] tmp;
return target;
}
// insert `commas' for 1000's delimiter or use another delimiter
// VS_STRING_CLASS supposed to be a integer or real w/o `e' format
VS_CHAR* str_comma( VS_CHAR* target, VS_CHAR delim )
{
int dot = str_rfind( target, VS_CHAR_L('.') );
if (dot == -1) dot = str_len( target );
dot -= 3;
while( dot > 0 )
{
str_ins_ch( target, dot , delim );
dot -= 3;
}
return target;
}
// translate VS_CHARs from `from' to `to'
// length of `from' MUST be equal to length of `to'
VS_CHAR* str_tr( VS_CHAR* target, const VS_CHAR *from, const VS_CHAR *to )
{
ASSERT( str_len( from ) == str_len( to ) );
if ( str_len( from ) != str_len( to ) ) return target;
int z = 0;
int sl = str_len( target );
for( z = 0; z < sl; z++ )
{
const VS_CHAR *pc = VS_FN_STRCHR( from, target[z] );
if (pc) target[z] = to[ pc - from ];
}
return target;
}
VS_CHAR* str_up( VS_CHAR* target )
{
int sl = str_len( target );