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
| #include <iostream>
#include <stdexcept>
#include <utility>
#include <iterator>
#include <cstddef>
using std::rel_ops::operator!=;
using std::rel_ops::operator>;
using std::rel_ops::operator<=;
using std::rel_ops::operator>=;
class mstring
{
public:
typedef size_t size_type;
typedef char value_type;
typedef ptrdiff_t difference_type;
typedef char* pointer;
typedef const char* const_pointer;
typedef char& reference;
typedef const char& const_reference;
typedef char* iterator;
typedef const char* const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
static const size_type npos = -1;
mstring();
mstring( const mstring& );
mstring( size_type, char );
mstring( const char* );
mstring( const char* str, size_type length );
mstring( const mstring&, size_type, size_type length = npos );
template<typename Iterator>
mstring( Iterator, Iterator );
~mstring();
friend bool operator== ( const mstring&, const mstring& );
friend bool operator< ( const mstring&, const mstring& );
char& at( size_type );
const char& at( size_type ) const;
char& operator[] ( size_type );
const char& operator[] ( size_type ) const;
mstring& operator= ( const mstring& );
mstring& operator= ( const char* );
mstring& operator= ( char );
friend std::ostream& operator<< ( std::ostream&, const mstring& );
friend std::istream& operator>> ( std::istream&, mstring& );
mstring& operator+= ( const mstring& );
mstring& operator+= ( const char* );
mstring& operator+= ( const char );
friend mstring operator+ ( const mstring&, const mstring& );
friend mstring operator+ ( const char*, const mstring& );
friend mstring operator+ ( char, const mstring& );
friend mstring operator+ ( const mstring&, const char* );
friend mstring operator+ ( const mstring&, char );
mstring& append( const mstring& );
mstring& append( const char* );
mstring& append( const mstring&, size_type, size_type );
mstring& append( const char*, size_type );
mstring& append( size_type, char );
template <class Iterator>
mstring& append( Iterator, Iterator );
mstring& assign( iterator, iterator );
mstring& assign( const mstring& );
mstring& assign( const char* );
mstring& assign( const char*, size_type );
mstring& assign( const mstring&, size_type, size_type );
mstring& assign( size_type, char );
iterator erase( iterator );
iterator erase( iterator, iterator );
mstring& erase( size_type index = 0, size_type num = npos );
iterator insert( iterator, char );
mstring& insert( size_type, const mstring& );
mstring& insert( size_type, const char* );
mstring& insert( size_type, const mstring&, size_type, size_type );
mstring& insert( size_type, const char*, size_type );
mstring& insert( size_type, size_type, char );
void insert( iterator, size_type, char );
template<class Iterator>
void insert( iterator, Iterator, Iterator );
size_type find( const mstring&, size_type index = 0 ) const;
size_type find( const char*, size_type index = 0 ) const;
size_type find( const char*, size_type, size_type ) const;
size_type find( char, size_type index = 0 ) const;
const char * data() const;
char * c_str() const;
mstring substr( size_type index = 0, size_type length = npos ) const;
size_type copy( char*, size_type, size_type index = 0 ) const;
void resize( size_type );
void push_back( const char& );
void pop_back();
void clear();
void swap( mstring& );
char& front();
char& back();
bool empty() const;
int compare( const mstring& ) const;
int compare( const char* ) const;
int compare( size_type, size_type, const mstring& ) const;
int compare( size_type, size_type, const char* s ) const;
int compare( size_type, size_type, const mstring& str,
size_type, size_type ) const;
int compare( size_type, size_type, const char*, size_type ) const;
size_type length() const;
size_type size() const;
size_type capacity() const;
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
private:
template <class Iterator>
size_type calc_size( Iterator beg, Iterator end );
private:
size_type m_len;
size_type m_capacity;
char *m_arr;
};
mstring::size_type mstrlen( const char *str )
{
mstring::size_type res = 0;
while( *str++ )
res++;
return res;
}
char * mstrcpy( char *dst, const char *src )
{
char *res = dst;
while( (*dst++ = *src++) );
return res;
}
char * mstrncpy( char *dst, const char *src, mstring::size_type len )
{
char *res = dst;
while( len-- && ( *dst++ = *src++ ) );
if ( *dst )
*dst = '\0';
return res;
}
char * mstrcat( char *str1, const char *str2 )
{
char *res = str1;
while( *str1 )
str1++;
while( (*str1++ = *str2++) );
return res;
}
char * mstrncat(char *str1, const char *str2, mstring::size_type count)
{
char *res = str1;
while(*str1)
str1++;
while(count-- && (*str1++ = *str2++));
if (*str1)
*str1 = '\0';
return res;
}
int mstrcmp( const char *str1, const char *str2 )
{
while( ( unsigned char )*str1 == ( unsigned char )*str2 && *str1 )
{
str1++;
str2++;
}
return ( int )*str1 - ( int )*str2;
}
int mstrncmp( const char *str1, const char *str2, size_t count )
{
while( --count && ( ( unsigned char )*str1 == ( unsigned char )*str2 ) && *str1 )
{
str1++;
str2++;
}
return ( int )*str1 - ( int )*str2;
}
char * mstrnset( char *str, int c, mstring::size_type n )
{
char *res = str;
if ( n > mstrlen( str ) )
n = mstrlen( str );
for( mstring::size_type i = 0; i != n; ++i )
*str++ = c;
return res;
}
char * mstrstr( const char *str1, const char *str2 )
{
mstring::size_type l1 = mstrlen( str1 ), l2 = mstrlen( str2 );
for ( mstring::size_type i = 0; i != l1 - l2 + 1; ++i )
{
mstring::size_type f = 0;
for ( mstring::size_type j = 0; j != l2; ++j )
{
if ( str1[i + j] == str2[j] )
f++;
else
break;
}
if ( f == l2 )
return ( char* const )str1 + i;
}
if ( l2 )
return nullptr;
else
return ( char* const )str1;
}
char * mstrnstr( const char *str1, const char *str2, mstring::size_type length1 )
{
mstring::size_type l1 = length1, l2 = mstrlen( str2 );
for ( mstring::size_type i = 0; i != l1 - l2 + 1; ++i )
{
mstring::size_type f = 0;
for ( mstring::size_type j = 0; j != l2; ++j )
{
if ( str1[i + j] == str2[j] )
f++;
else
break;
}
if ( f == l2 )
return ( char* const )str1 + i;
}
if ( l2 )
return nullptr;
else
return ( char* const )str1;
}
mstring::mstring():
m_len( 0 ), m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
*m_arr = '\0';
}
mstring::mstring( const mstring& s ):
m_len( s.m_len ), m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
mstrcpy( m_arr, s.m_arr );
}
mstring::mstring( size_type length, char ch ):
m_len( length ), m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
mstrnset( m_arr, ch, length );
}
mstring::mstring( const char* str ):
m_len( mstrlen( str ) ), m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
mstrcpy( m_arr, str );
}
mstring::mstring( const char* str, size_type length ):
m_len( std::min( length, mstrlen( str ) ) ),
m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
mstrncpy( m_arr, str, std::min( length, mstrlen( str ) ) );
}
mstring::mstring( const mstring& str, size_type index, size_type length ):
m_len( std::min( length, str.m_len ) - index ),
m_capacity( 2*m_len+1 ), m_arr( new char[m_capacity]() )
{
mstrncpy( m_arr, str.m_arr + index, std::min( length, str.m_len ) );
}
template <class Iterator>
mstring::mstring( Iterator start, Iterator end ):
m_len( calc_size( start, end ) ), m_capacity( 2*m_len+1 ),
m_arr( new char[m_capacity]() )
{
mstrncpy( m_arr, start, m_len );
}
mstring::~mstring()
{
delete [] m_arr;
}
bool operator== ( const mstring& c1, const mstring& c2 )
{
return mstrcmp( c1.m_arr, c2.m_arr ) == 0;
}
bool operator< ( const mstring& c1, const mstring& c2 )
{
return mstrcmp( c1.m_arr, c2.m_arr ) < 0;
}
char& mstring::at( size_type index )
{
if ( index >= m_len )
throw std::out_of_range( "Expression: string subscript out of range." );
return m_arr[index];
}
const char& mstring::at( size_type index ) const
{
if ( index >= m_len )
throw std::out_of_range( "Expression: string subscript out of range." );
return m_arr[index];
}
char& mstring::operator[] ( size_type index )
{
return at( index );
}
const char& mstring::operator[]( size_type index ) const
{
return at( index );
}
mstring& mstring::operator= ( const mstring& s )
{
if ( this != &s )
mstring( s ).swap( *this );
return *this;
}
mstring& mstring::operator= ( const char* s )
{
mstring( s ).swap( *this );
return *this;
}
mstring& mstring::operator= ( char ch )
{
mstring( 1, ch ).swap( *this );
return *this;
}
std::ostream& operator<< ( std::ostream& out, const mstring& s )
{
out << s.m_arr;
return out;
}
std::istream& operator>> ( std::istream& in, mstring& s )
{
char tmp[4096];
in >> tmp;
s = in ? tmp : mstring();
return in;
}
mstring& mstring::operator+= ( const mstring& append )
{
resize( m_len + append.m_len + 1 );
mstrcat( m_arr, append.m_arr );
return *this;
}
mstring& mstring::operator+= ( const char* append )
{
resize( m_len + mstrlen( append ) + 1 );
mstrcat( m_arr, append );
return *this;
}
mstring& mstring::operator+= ( const char append )
{
push_back( append );
return *this;
}
mstring operator+ ( const mstring& lhs, const mstring& rhs )
{
mstring ret( lhs );
ret += rhs;
return ret;
}
mstring operator+ ( const char* lhs, const mstring& rhs )
{
mstring ret( lhs );
ret += rhs;
return ret;
}
mstring operator+ ( char lhs, const mstring& rhs )
{
mstring ret( 1, lhs );
ret += rhs;
return ret;
}
mstring operator+ ( const mstring& lhs, const char* rhs )
{
mstring ret( lhs );
ret += rhs;
return ret;
}
mstring operator+ ( const mstring& lhs, char rhs )
{
mstring ret( lhs );
ret += rhs;
return ret;
}
mstring& mstring::append( const mstring& str )
{
mstrcat( m_arr, str.m_arr );
return *this;
}
mstring& mstring::append( const char* str )
{
mstrcat( m_arr, str );
return *this;
}
mstring& mstring::append( const mstring& str, size_type index, size_type len )
{
mstrncat( m_arr, str.m_arr + index, len );
return *this;
}
mstring& mstring::append( const char* str, size_type num )
{
mstrncat( m_arr, str, num );
return *this;
}
mstring& mstring::append( size_type num, char ch )
{
*this += mstring( num, ch );
return *this;
}
template<typename Iterator>
mstring& mstring::append( Iterator start, Iterator end )
{
mstrncat( m_arr, start, calc_size( start, end ) );
return *this;
}
mstring& mstring::assign( iterator start, iterator end )
{
mstring( start, end ).swap( *this );
return *this;
}
mstring& mstring::assign( const mstring& str )
{
mstring( str ).swap( *this );
return *this;
}
mstring& mstring::assign( const char* str )
{
mstring( str ).swap( *this );
return *this;
}
mstring& mstring::assign( const char* str, size_type num )
{
mstring( str, num ).swap( *this );
return *this;
}
mstring& mstring::assign( const mstring& str, size_type index, size_type len )
{
mstring( str, index, len ).swap( *this );
return *this;
}
mstring& mstring::assign( size_type num, char ch )
{
mstring( num, ch ).swap( *this );
return *this;
}
mstring::iterator mstring::erase( iterator position )
{
for ( iterator i = position; i != end(); ++i )
*i = *( i + 1 );
--m_len;
return position;
}
mstring::iterator mstring::erase( iterator first, iterator last )
{
size_type beg = first - begin();
size_type end = last - begin();
size_type new_len = m_len - end + beg;
size_type new_capacity = 2 * new_len + 1;
char *new_arr = new char[new_capacity];
for ( size_type i = 0; i != beg; ++i )
new_arr[i] = m_arr[i];
for ( size_type i = beg, j = end; j != m_len + 1; ++i, ++j )
new_arr[i] = m_arr[j];
delete [] m_arr;
m_len = new_len;
m_capacity = new_capacity;
m_arr = new_arr;
return begin() + beg;
}
mstring& mstring::erase( size_type index, size_type num )
{
num = std::min( num, m_len - index );
erase( begin() + index, begin() + index + num );
return *this;
}
mstring::iterator mstring::insert( iterator p, char c )
{
size_type tmp = p - begin();
if ( m_len + 1 > m_capacity )
resize( m_len + 1 );
else
++m_len;
p = begin() + tmp;
for ( iterator i = end(); i != p; --i )
*i = *( i - 1 );
*p = c;
return p;
}
void mstring::insert( iterator p, size_t n, char c )
{
size_type tmp = p - begin();
if ( m_len + n > m_capacity )
resize( m_len + n );
else
m_len += n;
p = begin() + tmp;
for ( iterator i = end(); i != p + n - 1; --i )
*i = *( i - n );
for ( iterator i = p; i != p + n; ++i )
*i = c;
}
template<typename Iterator>
void mstring::insert( iterator p, Iterator first, Iterator last )
{
size_type tmp = p - begin();
size_type n = calc_size( first, last );
if ( m_len + n > m_capacity )
resize( m_len + n );
else
m_len += n;
p = begin() + tmp;
for ( iterator i = end(); i != p + n - 1; --i )
*i = *( i - n );
for ( iterator i = p; i != p + n && first != last; ++i )
*i = *first++;
}
mstring& mstring::insert ( size_t pos1, const mstring& str )
{
insert( begin() + pos1, str.begin(), str.end() );
return *this;
}
mstring& mstring::insert ( size_t pos1, const mstring& str, size_t pos2, size_t n )
{
insert( begin() + pos1, str.begin() + pos2, str.begin() + n );
return *this;
}
mstring& mstring::insert ( size_t pos1, const char* s, size_t n)
{
insert( begin() + pos1, s, s + n );
return *this;
}
mstring& mstring::insert ( size_t pos1, const char* s )
{
insert( begin() + pos1, s, s + mstrlen(s) );
return *this;
}
mstring& mstring::insert ( size_t pos1, size_t n, char c )
{
insert( begin() + pos1, n, c );
return *this;
}
mstring::size_type mstring::find( const mstring& str, size_type index ) const
{
pointer ptr = mstrstr( m_arr + index, str.m_arr );
if (ptr != nullptr)
return ptr - begin();
else
return npos;
}
mstring::size_type mstring::find( const char* str, size_type index ) const
{
pointer ptr = mstrstr( m_arr + index, str );
if (ptr != nullptr)
return ptr - begin();
else
return npos;
}
mstring::size_type mstring::find( const char* str, size_type index,
size_type length ) const
{
pointer ptr = mstrnstr( m_arr + index, str , length);
if (ptr != nullptr)
return ptr - begin();
else
return npos;
}
mstring::size_type mstring::find( char ch, size_type index ) const
{
for ( size_type i = index; i != m_len; ++i )
{
if ( m_arr[i] == ch )
return i;
}
return npos;
}
const char * mstring::data() const
{
return m_arr;
}
char * mstring::c_str() const
{
return m_arr;
}
mstring mstring::substr( size_type index, size_type length ) const
{
mstring res( this->m_arr, index, length );
return res;
}
mstring::size_type mstring::copy( char* str, size_type num, size_type index ) const
{
mstrncpy( str, m_arr + index, num );
return num - index;
}
void mstring::resize( size_type new_len )
{
m_capacity = 2 * new_len + 1;
char *new_arr = new char[m_capacity]();
mstrcpy( new_arr, m_arr );
delete [] m_arr;
m_len = new_len;
m_arr = new_arr;
}
void mstring::push_back( const char& val )
{
if ( m_len == m_capacity )
{
resize( m_len + 1 );
--m_len;
}
m_arr[m_len++] = val;
}
void mstring::pop_back()
{
if ( m_len > 0 )
--m_len;
}
void mstring::clear()
{
delete [] m_arr;
m_len = 0;
m_capacity = 2*m_len+1;
m_arr = new char[m_capacity]();
*m_arr = '\0';
}
void mstring::swap( mstring& c )
{
if ( this != &c )
{
std::swap( m_len, c.m_len );
std::swap( m_capacity, c.m_capacity );
std::swap( m_arr, c.m_arr );
}
}
char& mstring::front()
{
return at( 0 );
}
char& mstring::back()
{
return at( m_len - 2 );
}
bool mstring::empty() const
{
return m_len == 0;
}
int mstring::compare( const mstring& str ) const
{
return mstrcmp( this->m_arr, str.m_arr );
}
int mstring::compare( const char* s ) const
{
return mstrcmp( this->m_arr, s );
}
int mstring::compare( size_type pos1, size_type n1, const mstring& str ) const
{
return mstrncmp( this->m_arr+pos1, str.m_arr, n1 - pos1 );
}
int mstring::compare( size_type pos1, size_type n1, const char* s ) const
{
return mstrncmp( this->m_arr+pos1, s, n1 - pos1 );
}
int mstring::compare( size_type pos1, size_type n1,
const mstring& str, size_type pos2, size_type n2 ) const
{
return mstrncmp( this->m_arr+pos1, str.m_arr+pos2,
std::min( n1 - pos1, n2 - pos2 ) );
}
int mstring::compare( size_type pos1, size_type n1,
const char* s, size_type n2 ) const
{
return mstrncmp( this->m_arr+pos1, s, std::min( n1 - pos1, n2 ) );
}
mstring::size_type mstring::length() const
{
return m_len;
}
mstring::size_type mstring::size() const
{
return m_len;
}
mstring::size_type mstring::capacity() const
{
return m_capacity;
}
mstring::iterator mstring::begin()
{
return m_arr;
}
mstring::const_iterator mstring::begin() const
{
return m_arr;
}
mstring::iterator mstring::end()
{
return m_arr + m_len;
}
mstring::const_iterator mstring::end() const
{
return m_arr + m_len;
}
mstring::reverse_iterator mstring::rbegin()
{
return reverse_iterator( end() );
}
mstring::const_reverse_iterator mstring::rbegin() const
{
return const_reverse_iterator( end() );
}
mstring::reverse_iterator mstring::rend()
{
return reverse_iterator( begin() );
}
mstring::const_reverse_iterator mstring::rend() const
{
return const_reverse_iterator( begin() );
}
template <class Iterator>
mstring::size_type mstring::calc_size( Iterator beg, Iterator end )
{
Iterator tmp = beg;
size_type res = 0;
while ( tmp != end )
{
++res;
++tmp;
}
return res;
} |