~vbursian/research-assistant/intervers

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
////////////////////////////////////////////////////////////////////////////////
/*! @file String.cpp   Class sString and related.
- Part of RANet - Research Assistant Net Library.
- Copyright(C) 1994-2017, Viktor E. Bursian, St.Petersburg, Russia.
                          VBursian@gmail.com
*///////////////////////////////////////////////////////////////////////////////
#include "Strings.h"
#include "Excepts.h"
#include "Symbols.h"
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <sstream>
namespace RA {
//------------------------------------------------------------------------------

literal  sString::EmptyLiteral("");
literal  sString::DefaultDelimiters(",;()[]=<>");
const sString   sString::PlusInfinity = sString("+")+sString(Symbol::Infinity);
const sString   sString::MinusInfinity = sString("-")+sString(Symbol::Infinity);

//------------------------------------------------------------------------------

sString  sString::FromReal (real                number
                           ,unsigned short int  digits_after_dot)
{
  //!@todo{UI++} Повсеместно подменять + и - на Symbol::Plus и Symbol::Minus, но
  //!            это надо делать и при вводе тоже (в обратную сторону).
  //!            Тогда, однако, нужна будет FromDouble (без подмены) для sBursiPan.
  if( number == -real_inf ){
    return MinusInfinity;
  }else if( number == real_inf ){
    return PlusInfinity;
  }else if( number == number ){
    std::ostringstream          S;
    S.setf(std::ios::fixed,std::ios::floatfield);   // floatfield set to fixed
    S.precision(digits_after_dot);
    S << ((long double)number);
    return sString(S.str().c_str());
  }else{
    return sString(Symbol::NaN);
  }
}


sString  sString::FromInteger (integer  number)
{
  std::ostringstream          S;
  S << number;
  return sString(S.str().c_str());
}


rsString  sString::operator >> (integer &  number)
{
  //!@bug из "sFolderNode" вводит 0 и опустошает строку
  //!     (после починки убрать патч из Storable.cpp)

  std::istringstream          S(std::string(literal(*this)));
  S >> number;
  int                         P = S.tellg();
  operator =(Tail(P));
  return *this;
}


rsString  sString::operator >> (real &  R)
{
  int                         P;
  if( Pos(PlusInfinity) == 0 ){
    R = real_inf;
    P = PlusInfinity.Length();
  }else if( Pos(MinusInfinity) == 0 ){
    R = - real_inf;
    P = MinusInfinity.Length();
  }else if( Pos(Symbol::NaN) == 0 ){
    R = real_nan;
    P = sString(Symbol::NaN).Length();
  }else{
    std::istringstream          S(std::string(literal(*this)));
    S.setf(std::ios::fixed,std::ios::floatfield);   // floatfield set to fixed
    long double       D(R);
    S >> D;
    R = D;
    P = S.tellg();
  }
  operator =(Tail(P));
  return *this;
}

//------------------------------------------------------------------------------

sString::~sString ()
{
  if( Body ){
    delete [] Body;  Body=NULL;
  }
}


sString::sString ()
    :Body(NULL)
    ,CurrentLength(0)
    //,CurrentSpace(0)
{
}


sString::sString (rcsString  source_string)
    :Body(NULL)
    ,CurrentLength(0)
    //,CurrentSpace(0)
{
  int                         length;
  if( ! source_string.Empty() ){
    length= source_string.Length();
    Body= new char [length+1];
    (void)memcpy((pointer)Body ,(pointer)source_string.Body ,length+1);
    CurrentLength= length;
    //CurrentSpace= length+1;
  }
}


sString::sString (literal Source)
    :Body(NULL)
    ,CurrentLength(0)
    //,CurrentSpace(0)
{
  int                         length;
  if( Source && *Source ){
    length= strlen(Source);
    Body= new char [length+1];
    (void)memcpy((pointer)Body ,(pointer)Source ,length+1);
    CurrentLength= length;
    //CurrentSpace= length+1;
  }
}


sString::sString (char C)
    :Body(NULL)
    ,CurrentLength(0)
    //,CurrentSpace(0)
{
  Body= new char [2];
  Body[0]=C;
  Body[1]='\0';
  CurrentLength=1;
  //CurrentSpace= 2;
}


bool  sString::Empty () const
{
  return Body ? (CurrentLength == 0) : true ;
}


size_t  sString::Length () const
{
  return Body ? CurrentLength : 0 ;
}


//char  sString::operator [] (size_t I) const
char  sString::operator [] (int  I) const
{
  if( Empty() ){
    throw xProgramError("sString::operator[] is called for empty string"
                       ,__FILE__,__LINE__);
  }
  if( I < 0 ){
    throw xProgramError("sString::operator[]: negative index"
                       ,__FILE__,__LINE__);
  }else if( (unsigned int)I >= CurrentLength ){
    throw xProgramError("sString::operator[]: index goes out of range"
                       ,__FILE__,__LINE__);
  }
  return Body[I];
}


//sString::operator literal () const
//{
//  return Body ? Body : EmptyLiteral ;
//}


sString::operator ptr_to_const_char () const
{
  return Body ? Body : EmptyLiteral ;
}


sString  sString::Substr (size_t from ,size_t length) const
{
  sString                     Result;
  if( Empty() ){
    return Result;
  }
  if( from >= CurrentLength ){
    return Result;
  }
  if( from+length > Length() ){
    length= Length()- from;
  }
  if( length > 0 ){
    Result.Body= new char [length+1];
    (void)memcpy(Result.Body,(Body+from),length);
    Result.Body[length]='\0';
    Result.CurrentLength= length;
    //Result.CurrentSpace= CurrentLength+1;
  }
  return Result;
}


sString  sString::Tail (size_t from) const
{
  if( Empty() ){
    return sString();
  }
  if( from >= CurrentLength ){
    return sString();
  }
  return Substr(from,Length()- from);
}


size_t  sString::Pos (rcsString S) const
{
  if( Empty() ){
    return CurrentLength;
  }else if( S.Empty() ){
    return CurrentLength;
  }else{
    char *                      P;
    P=strstr(Body,S.Body);  //! @todo{sString} won't go through null-chars
    return ( P ? P-Body : CurrentLength );
  }
}


size_t  sString::Pos (literal S) const
{
  if( Empty() ){
    return CurrentLength;
  }else if( !S ){
    return CurrentLength;
  }else if( !(*S) ){
    return CurrentLength;
  }else{
    char *                      P;
    P=strstr(Body,S);  //! @todo{sString} won't go through null-chars
    return ( P ? P-Body : CurrentLength );
  }
}


rsString  sString::operator = (rcsString S)
{
  int                         length =0;
  int                         space =0;
  char *                      NewBody = NULL;

  if( !S.Empty() ){
    length= S.Length();
    space=length+1;
    NewBody= new char [space];
    (void)memcpy(NewBody,S.Body,space);
  }
  if( Body )
    delete [] Body;
  Body=NewBody;
  CurrentLength=length;
  //CurrentSpace=space;
  return *this;
}


sString  sString::operator + (rcsString S) const
{
  return operator+(literal(S));
}


sString  sString::operator + (literal S) const
{
  sString                     Result;
  int                         LoS =0;
  if( S ){
    LoS=strlen(S);
  }
  if( Length()+LoS > 0 ){
    Result.Body= new char [Length() + LoS + 1];
    if( !Empty() ){
      (void)memcpy(Result.Body,Body,Length()+1);
    }
    if( S ){
      (void)memcpy(Result.Body+Length() ,(pointer)S ,LoS+1);
    }
    Result.CurrentLength= Length()+LoS;
    //Result.CurrentSpace= Result.CurrentLength+1;
  }
  return Result;
}


rsString  sString::operator += (rcsString Tail)
{
  return operator+=(literal(Tail));//! @todo{sString} bomb! что за нах!
}


rsString  sString::operator += (literal Tail)
{
  if( Tail && *Tail ){
    int                         length = strlen(Tail);
    char *                      NewBody = NULL;

    NewBody= new char [Length() + length +1];
    if( !Empty() )
      (void)memcpy(NewBody,Body,Length());
    (void)memcpy(NewBody+Length(),(pointer)Tail,length+1);
    if( Body )
      delete [] Body;
    Body=NewBody;
    CurrentLength+=length;
    //CurrentSpace=CurrentLength+1;
  }
  return *this ;
}


rsString  sString::operator += (const char C)
{
  char *                      NewBody = NULL;
  NewBody= new char [Length() + 2];
  if( !Empty() )
    (void)memcpy(NewBody,Body,Length());
  NewBody[Length()]=C;
  if( Body )
    delete [] Body;
  Body=NewBody;
  CurrentLength+=1;
  Body[Length()]='\0';
  //CurrentSpace=CurrentLength+1;
  return *this ;
}


//rsString  sString::operator << (const int I)
//{
//  char                        Buffer [30];
//  sprintf(Buffer,"%i",I);
//  (*this)+=Buffer;
//  return *this;
//}


//rsString  sString::operator << (const unsigned int I)
//{
//  char                        Buffer [30];
//  sprintf(Buffer,"%u",I);
//  (*this)+=Buffer;
//  return *this;
//}


//rsString  sString::operator << (const long int I)
//{
//  char                        Buffer [30];
//  sprintf(Buffer,"%li",I);
//  (*this)+=Buffer;
//  return *this;
//}


//rsString  sString::operator << (const unsigned long int I)
//{
//  char                        Buffer [30];
//  sprintf(Buffer,"%lu",I);
//  (*this)+=Buffer;
//  return *this;
//}


rsString  sString::operator << (real  R)
{
  char                        Buffer [90];
  /*!< @todo{sString} for X11 it was: ...,"%Lg",(long double)R */
  sprintf(Buffer,"%lg",(double)R);
  (*this)+=Buffer;
  return *this;
}


//rsString  sString::operator << (const double D)
//{
//  char                        Buffer [90];
//  sprintf(Buffer,"%lg",D);
//  (*this)+=Buffer;
//  return *this;
//}


bool  sString::operator == (rcsString S)  const
{
  if( Empty() && S.Empty() ){
    return true;
  }else if( !Empty() && !S.Empty() ){
    if( CurrentLength == S.Length() ){
      return memcmp(Body,S.Body,CurrentLength)==0;
    }else{
      return false;
    }
  }else{
    return false;
  }
}


bool  sString::operator == (literal L) const
{
  if( Empty() ){
    return !(L && *L);
  }else{
    if( CurrentLength != strlen(L) ){
      return false;
    }else{
      return memcmp(Body,L,CurrentLength)==0;
    }
  }
}


bool  sString::operator < (rcsString S) const
{
  if( Empty() ){
    return !S.Empty();
  }else if( S.Empty() ){
    return false;
  }else{
      unsigned int    lmin = std::min(CurrentLength,S.Length());
      int             result = memcmp(Body,S.Body,lmin);
    if( result ){
      return bool(result < 0);
    }else{
      return CurrentLength < S.Length();
    }
  }
}


sString  sString::UpperCase () const
{
  sString                     Result (*this);
  if( !Result.Empty() ){
    for (size_t i=0; i<CurrentLength; ++i)
      Result.Body[i]=toupper(Body[i]);
    //! @todo{sString} see locale::toupper
    //locale loc;
    //for (size_t i=0; i<CurrentLength; ++i)
    //  Result.Body[i]=toupper(Body[i],loc);
  }
  return Result;
}


sString  sString::Trim () const
{
  /*! @todo{sString} Trim*/
  sString                     Result (*this);
  if( !Result.Empty() ){
    throw xNotImplemented(__PRETTY_FUNCTION__);
  }
  return Result;
}


sString  sString::TrimSyntactic (literal /*Delimiters*/) const
{
  sString                     Result (*this);
  if( !Result.Empty() ){
    throw xNotImplemented(__PRETTY_FUNCTION__);
  }
  return Result;
}


sString  sString::Translate (char O ,char N) const
{
  sString                     Result (*this);
  for( size_t i=0 ; i < CurrentLength ; ++i )
    if( Body[i] == O )
      Result.Body[i]=N;
  return Result;
}


void  sString::EatLeadingSpaces ()
{
  if( ! Empty() ){
    //! @todo{sString} implement
  }
}


int  sString::WipeOut (literal  substring
                      ,bool     only_first)
{
  int                         occurences = 0;
  size_t                      L = strlen(substring);
  if( L ){
    size_t                      P;
    while( (P = Pos(substring)) < CurrentLength ){
      operator =( Substr(0,P) + Tail(P+L) );
      occurences++;
      if( only_first )
        break;
    }
  }
  return occurences;
}


int  sString::Replace (literal  substring
                      ,literal  replacement
                      ,bool     only_first)
{
  int                         occurences = 0;
  size_t                      L = strlen(substring);
  if( L ){
    size_t                      P;
    while( (P = Pos(substring)) < CurrentLength ){
      operator =( Substr(0,P) + replacement + Tail(P+L) );
      occurences++;
      if( only_first )
        break;
    }
  }
  return occurences;
}


//void  sString::InputTextLine (std::istream &  S)
//{
//  const int                   BufferSize = 1000;
//  char                        Buffer [BufferSize];
//  int                         L = 0;
//  tStreamPos                  S_InitialPosition = S.GetPosition();
//  tStreamPos                  S_Size = S.GetSize();
//  if( Body ){
//    delete [] Body;  Body=NULL;
//  }
//  CurrentLength=0;
//  //CurrentSpace=0;
//  while( S_InitialPosition+L < S_Size ){
//    S.ReadInto(Buffer+L,1);
//    if( Buffer[L] == '\r' ){
//    }else if( Buffer[L] == '\n' ){
//      Buffer[L]='\000';
//      operator+=(Buffer);
//      return;
//    }else{
//      L++;
//      if( L == BufferSize ){
//        throw xNotImplemented("Input of truly long lines");
//      }
//    }
//  }
//  if( L > 0 ){
//    Buffer[L]='\000';
//    operator+=(Buffer);
//  }
//}


void  sString::SetValue (literal Source)
{
  if( Body ){
    delete [] Body;  Body=NULL;
  }
  CurrentLength=0;
  //CurrentSpace=0;
  if( Source && *Source ){
    int                       L = strlen(Source);
    Body= new char [L+1];
    (void)memcpy(Body,(pointer)Source,L+1);
    CurrentLength=L;
    //CurrentSpace=L+1;
  }
}


void  sString::Read (std::istream &  a_stream)
{
  uint32_t                    L;
  if( Body ){
    delete [] Body;  Body=NULL;
  }
  CurrentLength=0;
  //CurrentSpace=0;
  a_stream.read( (char *)(&L), sizeof(L) );
  if( L ){
    Body= new char [L+1];
    a_stream.read(Body,L);
    *(byte*)(Body+L)=0;
    CurrentLength=L;
    //CurrentSpace=L+1;
  }
}


void  sString::Write (std::ostream &  a_stream) const
{
  uint32_t               L;
  if( Length() > std::numeric_limits<uint32_t>::max() ){
    L = std::numeric_limits<uint32_t>::max();
  }else{
    L = Length();
  }
  a_stream.write( (char *)(&L), sizeof(L) );
  if( L )
    a_stream.write(Body,L);
}

//------------------------------------------------------------------------------
} //namespace RA