~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to .pc/support-gcc-5.x.patch/src/lib/std/shl/String.cpp

  • Committer: Package Import Robot
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2015-07-11 02:00:35 UTC
  • mfrom: (10.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150711020035-2nhpztq7s15qyc0v
Tags: 2.5.1-1
* New upstream release. (Closes: #789968)
* Update debian/control.
  - Update Standards-Version to 3.9.6.
* Add support mips64(el) and ppc64el. (Closes: #741508, #748146)
* Add patches/support-gcc-5.x.patch. (Closes: #777767)
  - Fix build with gcc-5.x.
* Add patches/Disable-NET0001.als.patch.
  - Disable test of NET0001.als.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ---------------------------------------------------------------------------
 
2
// - String.cpp                                                              -
 
3
// - standard object library - string class implementation                   -
 
4
// ---------------------------------------------------------------------------
 
5
// - This program is free software;  you can redistribute it  and/or  modify -
 
6
// - it provided that this copyright notice is kept intact.                  -
 
7
// -                                                                         -
 
8
// - This program  is  distributed in  the hope  that it will be useful, but -
 
9
// - without  any  warranty;  without  even   the   implied    warranty   of -
 
10
// - merchantability or fitness for a particular purpose.  In no event shall -
 
11
// - the copyright holder be liable for any  direct, indirect, incidental or -
 
12
// - special damages arising in any way out of the use of this software.     -
 
13
// ---------------------------------------------------------------------------
 
14
// - copyright (c) 1999-2015 amaury darsch                                   -
 
15
// ---------------------------------------------------------------------------
 
16
 
 
17
#include "Ascii.hpp"
 
18
#include "Stdsid.hxx"
 
19
#include "String.hpp"
 
20
#include "Strvec.hpp"
 
21
#include "Vector.hpp"
 
22
#include "Buffer.hpp"
 
23
#include "Central.hpp"
 
24
#include "Unicode.hpp"
 
25
#include "Integer.hpp"
 
26
#include "Boolean.hpp"
 
27
#include "Runnable.hpp"
 
28
#include "Character.hpp"
 
29
#include "QuarkZone.hpp"
 
30
#include "Exception.hpp"
 
31
#include "InputStream.hpp"
 
32
#include "OutputStream.hpp"
 
33
#include "csys.hpp"
 
34
 
 
35
namespace afnix {
 
36
    
 
37
  // -------------------------------------------------------------------------
 
38
  // - private section                                                       -
 
39
  // -------------------------------------------------------------------------
 
40
  
 
41
  // 32 bit hash function
 
42
  static inline long hash_32 (const t_quad* buf) {
 
43
    if (buf == nilp) return 0L;
 
44
    t_quad chr = nilq;
 
45
    long   hvl = 0;
 
46
    long   sht = 17;
 
47
    while ((chr = *buf++) != nilq) {
 
48
      hvl = hvl ^ (((long) chr) << sht);
 
49
      if ((sht = sht - 7) < 0) sht += 24;
 
50
    }
 
51
    return hvl;
 
52
  }
 
53
 
 
54
  // 64 bit hash function
 
55
  static inline long hash_64 (const t_quad* buf) {
 
56
    if (buf == nilp) return 0LL;
 
57
    t_quad chr = nilq;
 
58
    long   hvl = 0;
 
59
    long   sht = 47;
 
60
    while ((chr = *buf++) != nilq) {
 
61
      hvl = hvl ^ (((long) chr) << sht);
 
62
      if ((sht = sht - 7) < 0) sht += 56;
 
63
    }
 
64
    return hvl;
 
65
  }
 
66
 
 
67
  // -------------------------------------------------------------------------
 
68
  // - public section                                                        -
 
69
  // -------------------------------------------------------------------------
 
70
  
 
71
  // intern a string and return a quark
 
72
  
 
73
  long String::intern (const String& name) {
 
74
    return Central::intern (name);
 
75
  }
 
76
  
 
77
  // remap a string from a quark
 
78
  
 
79
  String String::qmap (const long quark) {
 
80
    return Central::qmap (quark);
 
81
  }
 
82
 
 
83
  // compute the hash value from a quad array
 
84
 
 
85
  long String::hashid (const t_quad* s) {
 
86
    long result = (sizeof (long) == 8) ? hash_64 (s) : hash_32 (s);
 
87
    return (result > 0) ? result : -result;
 
88
  }
 
89
  
 
90
  // case insensitive string comparision
 
91
 
 
92
  bool String::strcic (const String& s1, const String& s2) {
 
93
    String ls1 = s1.tolower ();
 
94
    String ls2 = s2.tolower ();
 
95
    return (ls1 == ls2);
 
96
  }
 
97
 
 
98
  // -------------------------------------------------------------------------
 
99
  // - class section                                                         -
 
100
  // -------------------------------------------------------------------------
 
101
 
 
102
  // create an empty string
 
103
  
 
104
  String::String (void) {
 
105
    p_sval = nilp;
 
106
    d_nrmf = false;
 
107
  }
 
108
  
 
109
  // create a string from a character
 
110
  
 
111
  String::String (const char c) {
 
112
    p_sval = Unicode::strmak (c);
 
113
    d_nrmf = false;
 
114
  }
 
115
  
 
116
   // create a string from an unicode character
 
117
  
 
118
  String::String (const t_quad c) {
 
119
    p_sval = Unicode::strmak (c);
 
120
    d_nrmf = false;
 
121
  }
 
122
 
 
123
  // create a string from a c-string
 
124
  
 
125
  String::String (const char* s) {
 
126
    p_sval = Unicode::strdup (s, true);
 
127
    d_nrmf = true;
 
128
  }
 
129
 
 
130
  // create a string from an unicode string
 
131
  
 
132
  String::String (const t_quad* s) {
 
133
    p_sval = Unicode::strdup (s, true);
 
134
    d_nrmf = true;
 
135
  }
 
136
 
 
137
  // copy constructor
 
138
  
 
139
  String::String (const String& that) {
 
140
    that.rdlock ();
 
141
    try {
 
142
      p_sval = Unicode::strdup (that.p_sval, !that.d_nrmf);
 
143
      d_nrmf = true;
 
144
      that.unlock ();
 
145
    } catch (...) {
 
146
      that.unlock ();
 
147
      throw;
 
148
    }
 
149
  }
 
150
  
 
151
  // destroy this string
 
152
  
 
153
  String::~String (void) {
 
154
    delete [] p_sval;
 
155
  }
 
156
 
 
157
  // return the class name
 
158
 
 
159
  String String::repr (void) const {
 
160
    return "String";
 
161
  }
 
162
 
 
163
  // return a clone of this object
 
164
 
 
165
  Object* String::clone (void) const {
 
166
    return new String (*this);
 
167
  }
 
168
 
 
169
  // clear this string
 
170
 
 
171
  void String::clear (void) {
 
172
    wrlock ();
 
173
    try {
 
174
      delete [] p_sval; p_sval = nilp;
 
175
      d_nrmf = false;
 
176
      unlock ();
 
177
    } catch (...) {
 
178
      unlock ();
 
179
      throw;
 
180
    }
 
181
  }
 
182
 
 
183
  // return a literal representation of this string
 
184
  
 
185
  String String::toliteral (void) const {
 
186
    rdlock ();
 
187
    try {
 
188
      String result = '"';
 
189
      result += p_sval;
 
190
      result += '"';
 
191
      unlock ();
 
192
      return result;
 
193
    } catch (...) {
 
194
      unlock ();
 
195
      throw;
 
196
    }
 
197
  }
 
198
 
 
199
  // return a printable representation of this string
 
200
  
 
201
  String String::tostring (void) const {
 
202
    rdlock ();
 
203
    try {
 
204
      String result = *this;
 
205
      unlock ();
 
206
      return result;
 
207
    } catch (...) {
 
208
      unlock ();
 
209
      throw;
 
210
    }
 
211
  }
 
212
 
 
213
  // return the string serial code
 
214
 
 
215
  t_byte String::serialid (void) const {
 
216
    return SERIAL_STRG_ID;
 
217
  }
 
218
 
 
219
  // serialize this string
 
220
 
 
221
  void String::wrstream (OutputStream& os) const {
 
222
    rdlock ();
 
223
    try {
 
224
      char* sbuf = Unicode::encode (Encoding::UTF8, p_sval);
 
225
      os.write (sbuf);
 
226
      os.write (nilc);
 
227
      delete [] sbuf;
 
228
      unlock ();
 
229
    } catch (...) {
 
230
      unlock ();
 
231
      throw;
 
232
    }
 
233
  }
 
234
 
 
235
  // deserialize this string
 
236
 
 
237
  void String::rdstream (InputStream& is) {
 
238
    wrlock ();
 
239
    try {
 
240
      Buffer buffer (Encoding::UTF8);
 
241
      char c = nilc;
 
242
      while ((c = is.read ()) != nilc) buffer.add (c);
 
243
      *this = buffer.tostring ();
 
244
      unlock ();
 
245
    } catch (...) {
 
246
      unlock ();
 
247
      throw;
 
248
    }
 
249
  }
 
250
 
 
251
  // return a quark from this string
 
252
 
 
253
  long String::toquark (void) const {
 
254
    rdlock ();
 
255
    try {
 
256
      long result = Central::intern (*this);
 
257
      unlock ();
 
258
      return result;
 
259
    } catch (...) {
 
260
      unlock ();
 
261
      throw;
 
262
    }
 
263
  }
 
264
 
 
265
  // return true if the string is nil
 
266
 
 
267
  bool String::isnil (void) const {
 
268
    rdlock ();
 
269
    try {
 
270
      bool result = (p_sval == nilp) || (p_sval[0] == nilq);
 
271
      unlock ();
 
272
      return result;
 
273
    } catch (...) {
 
274
      unlock ();
 
275
      throw;
 
276
    }
 
277
  }
 
278
 
 
279
  // assign a c-string to this string
 
280
  
 
281
  String& String::operator = (const char* s) {
 
282
    wrlock ();
 
283
    try {
 
284
      delete [] p_sval;
 
285
      p_sval = Unicode::strdup (s, true);
 
286
      d_nrmf = true;
 
287
      unlock ();
 
288
      return *this;
 
289
    } catch (...) {
 
290
      unlock ();
 
291
      throw;
 
292
    }
 
293
 
 
294
  }
 
295
  
 
296
  // assign a character to this string
 
297
  
 
298
  String& String::operator = (const char c) {
 
299
    wrlock ();
 
300
    try {
 
301
      delete [] p_sval;
 
302
      p_sval = Unicode::strmak (c);
 
303
      d_nrmf = false;
 
304
      unlock ();
 
305
      return *this;
 
306
    } catch (...) {
 
307
      unlock ();
 
308
      throw;
 
309
    }
 
310
  }
 
311
 
 
312
  // assign a unicode string to this string
 
313
  
 
314
  String& String::operator = (const t_quad* s) {
 
315
    wrlock ();
 
316
    try {
 
317
      delete [] p_sval;
 
318
      p_sval = Unicode::strdup (s, true);
 
319
      d_nrmf = true;
 
320
      unlock ();
 
321
      return *this;
 
322
    } catch (...) {
 
323
      unlock ();
 
324
      throw;
 
325
    }
 
326
 
 
327
  }
 
328
  
 
329
  // assign a unicode character to this string
 
330
  
 
331
  String& String::operator = (const t_quad c) {
 
332
    wrlock ();
 
333
    try {
 
334
      delete [] p_sval;
 
335
      p_sval = Unicode::strmak (c);
 
336
      d_nrmf = false;
 
337
      unlock ();
 
338
      return *this;
 
339
    } catch (...) {
 
340
      unlock ();
 
341
      throw;
 
342
    }
 
343
 
 
344
  }
 
345
  
 
346
  // assign a string to this string
 
347
  
 
348
  String& String::operator = (const String& that) {
 
349
    // protect the case this == that
 
350
    if (this == &that) return *this;
 
351
    // assign new string
 
352
    wrlock ();
 
353
    that.rdlock ();
 
354
    try {
 
355
      delete [] p_sval;
 
356
      p_sval = Unicode::strdup (that.p_sval, !that.d_nrmf);
 
357
      d_nrmf = true;
 
358
      that.unlock ();
 
359
      unlock ();
 
360
      return *this;
 
361
    } catch (...) {
 
362
      unlock ();
 
363
      that.unlock ();
 
364
      throw;
 
365
    }
 
366
 
 
367
  }
 
368
 
 
369
  // get a character at a certain position
 
370
 
 
371
  t_quad String::operator [] (const long index) const {
 
372
    rdlock ();
 
373
    try {
 
374
      // check for good position first
 
375
      if ((index < 0) || (index >= length ())) {
 
376
        throw Exception ("index-error", "invalid string index");
 
377
      }
 
378
      // everything is fine
 
379
      t_quad result = p_sval[index];
 
380
      unlock ();
 
381
      return result;
 
382
    } catch (...) {
 
383
      unlock ();
 
384
      throw;
 
385
    }
 
386
 
 
387
  }
 
388
 
 
389
  // add a string to the current one and return a new one
 
390
  
 
391
  String String::operator + (const String& s) const {
 
392
    rdlock   ();
 
393
    s.rdlock ();
 
394
    try {
 
395
      String result;
 
396
      result.p_sval = Unicode::strmak (p_sval, s.p_sval);
 
397
      result.d_nrmf = false;
 
398
      unlock   ();
 
399
      s.unlock ();
 
400
      return result;
 
401
    } catch (...) {
 
402
      unlock   ();
 
403
      s.unlock ();
 
404
      throw;
 
405
    }
 
406
  }
 
407
  
 
408
  // add a character to the current string and return a new one
 
409
  
 
410
  String String::operator + (const char c) const {
 
411
    rdlock ();
 
412
    try {
 
413
      String result;
 
414
      result.p_sval = Unicode::strmak (p_sval, c);
 
415
      result.d_nrmf = false;
 
416
      unlock ();
 
417
      return result;
 
418
    } catch (...) {
 
419
      unlock ();
 
420
      throw;
 
421
    }
 
422
  }
 
423
 
 
424
  // add a unicode character to the current string and return a new one
 
425
  
 
426
  String String::operator + (const t_quad c) const {
 
427
    rdlock ();
 
428
    try {
 
429
      String result;
 
430
      result.p_sval = Unicode::strmak (p_sval, c);
 
431
      result.d_nrmf = false;
 
432
      unlock ();
 
433
      return result;
 
434
    } catch (...) {
 
435
      unlock ();
 
436
      throw;
 
437
    }
 
438
  }
 
439
 
 
440
  // add an integer to the current string and return a new one
 
441
  
 
442
  String String::operator + (const long value) const {
 
443
    rdlock ();
 
444
    try {
 
445
      // create a temporary buffer to hold the string
 
446
      char* buf = Ascii::ltoa (value);
 
447
      // concatenate the two buffers
 
448
      String result;
 
449
      result.p_sval = Unicode::strmak (p_sval, buf);
 
450
      result.d_nrmf = false;
 
451
      delete [] buf;
 
452
      unlock ();
 
453
      return result;
 
454
    } catch (...) {
 
455
      unlock ();
 
456
      throw;
 
457
    }
 
458
  }
 
459
 
 
460
  // add a long integer to the current string and return a new one
 
461
  
 
462
  String String::operator + (const t_long value) const {
 
463
    rdlock ();
 
464
    try {
 
465
      // create a temporary buffer to hold the string
 
466
      char* buf = Ascii::lltoa (value);
 
467
      // concatenate the two buffers
 
468
      String result;
 
469
      result.p_sval = Unicode::strmak (p_sval, buf);
 
470
      result.d_nrmf = false;
 
471
      delete [] buf;
 
472
      unlock ();
 
473
      return result;
 
474
    } catch (...) {
 
475
      unlock ();
 
476
      throw;
 
477
    }
 
478
  }
 
479
  
 
480
  // add a string to this string
 
481
  
 
482
  String& String::operator += (const String& s) {
 
483
    wrlock   ();
 
484
    s.rdlock ();
 
485
    try {
 
486
      // create a new buffer by concatenation
 
487
      t_quad* buf = Unicode::strmak (p_sval, s.p_sval);
 
488
      // clean the old string and save the buffer
 
489
      delete [] p_sval;
 
490
      p_sval = buf;
 
491
      d_nrmf = false;
 
492
      unlock   ();
 
493
      s.unlock ();
 
494
      return *this;
 
495
    } catch (...) {
 
496
      unlock   ();
 
497
      s.unlock ();
 
498
      throw;
 
499
    }
 
500
  }
 
501
 
 
502
  // add a character to the current string
 
503
  
 
504
  String& String::operator += (const char c) {
 
505
    wrlock   ();
 
506
    try {
 
507
      // create a new buffer by concatenation
 
508
      t_quad* buf = Unicode::strmak (p_sval, c);
 
509
      // clean the old string and save the buffer
 
510
      delete [] p_sval;
 
511
      p_sval = buf;
 
512
      d_nrmf = false;
 
513
      unlock ();
 
514
      return *this;
 
515
    } catch (...) {
 
516
      unlock ();
 
517
      throw;
 
518
    }
 
519
  }
 
520
 
 
521
  // add a character to the current string
 
522
  
 
523
  String& String::operator += (const t_quad c) {
 
524
    wrlock   ();
 
525
    try {
 
526
      // create a new buffer by concatenation
 
527
      t_quad* buf = Unicode::strmak (p_sval, c);
 
528
      // clean the old string and save the buffer
 
529
      delete [] p_sval;
 
530
      p_sval = buf;
 
531
      d_nrmf = false;
 
532
      unlock ();
 
533
      return *this;
 
534
    } catch (...) {
 
535
      unlock ();
 
536
      throw;
 
537
    }
 
538
  }
 
539
 
 
540
  // add an integer to this string
 
541
  
 
542
  String& String::operator += (const long value) {
 
543
    wrlock ();
 
544
    try {
 
545
      // create a temporary buffer to hold the converted value
 
546
      char* ibuf = Ascii::ltoa (value);
 
547
      // create a new buffer by concatenation
 
548
      t_quad* sbuf = Unicode::strmak (p_sval, ibuf);
 
549
      // clean the old string and save the buffer
 
550
      delete [] p_sval;
 
551
      delete [] ibuf;
 
552
      p_sval = sbuf;
 
553
      d_nrmf = false;
 
554
      unlock ();
 
555
      return *this;
 
556
    } catch (...) {
 
557
      unlock ();
 
558
      throw;
 
559
    }
 
560
  }
 
561
 
 
562
  // add a long integer to this string
 
563
  
 
564
  String& String::operator += (const t_long value) {
 
565
    rdlock ();
 
566
    try {
 
567
      // create a temporary buffer to hold the converted value
 
568
      char* ibuf = Ascii::lltoa (value);
 
569
      // create a new buffer by concatenation
 
570
      t_quad* sbuf = Unicode::strmak (p_sval, ibuf);
 
571
      // clean the old string and save the buffer
 
572
      delete [] p_sval;
 
573
      delete [] ibuf;
 
574
      p_sval = sbuf;
 
575
      d_nrmf = false;
 
576
      unlock ();
 
577
      return *this;
 
578
    } catch (...) {
 
579
      unlock ();
 
580
      throw;
 
581
    }
 
582
  }
 
583
 
 
584
  // compare a string with another one
 
585
  
 
586
  bool String::operator == (const String& s) const {
 
587
    rdlock   ();
 
588
    s.rdlock ();
 
589
    try {
 
590
      bool result = Unicode::strcmp (p_sval, d_nrmf, s.p_sval, s.d_nrmf);
 
591
      unlock   ();
 
592
      s.unlock ();
 
593
      return result;
 
594
    } catch (...) {
 
595
      unlock   ();
 
596
      s.unlock ();
 
597
      throw;
 
598
    }
 
599
  }
 
600
 
 
601
  // compare a string with a c-string
 
602
  
 
603
  bool String::operator == (const char* s) const {
 
604
    rdlock ();
 
605
    try {
 
606
      bool result = Unicode::strcmp (p_sval, d_nrmf, s);
 
607
      unlock ();
 
608
      return result;
 
609
    } catch (...) {
 
610
      unlock ();
 
611
      throw;
 
612
    }
 
613
  }
 
614
  
 
615
  // compare a string with unicode array
 
616
  
 
617
  bool String::operator == (const t_quad* s) const {
 
618
    rdlock ();
 
619
    try {
 
620
      bool result = Unicode::strcmp (p_sval, d_nrmf, s, false);
 
621
      unlock ();
 
622
      return result;
 
623
    } catch (...) {
 
624
      unlock ();
 
625
      throw;
 
626
    }
 
627
  }
 
628
 
 
629
  // compare a string with another one
 
630
  
 
631
  bool String::operator != (const String& s) const {
 
632
    rdlock   ();
 
633
    s.rdlock ();
 
634
    try {
 
635
      bool result = Unicode::strcmp (p_sval, d_nrmf, s.p_sval, s.d_nrmf);
 
636
      unlock   ();
 
637
      s.unlock ();
 
638
      return !result;
 
639
    } catch (...) {
 
640
      unlock   ();
 
641
      s.unlock ();
 
642
      throw;
 
643
    }
 
644
  }
 
645
  
 
646
  // compare a string with a c-string
 
647
  
 
648
  bool String::operator != (const char* s) const {
 
649
    rdlock ();
 
650
    try {
 
651
      bool result = Unicode::strcmp (p_sval, d_nrmf, s);
 
652
      unlock ();
 
653
      return !result;
 
654
    } catch (...) {
 
655
      unlock ();
 
656
      throw;
 
657
    }
 
658
  }
 
659
 
 
660
  // compare a string with a unicode array
 
661
  
 
662
  bool String::operator != (const t_quad* s) const {
 
663
    rdlock ();
 
664
    try {
 
665
      bool result = Unicode::strcmp (p_sval, d_nrmf, s, false);
 
666
      unlock ();
 
667
      return !result;
 
668
    } catch (...) {
 
669
      unlock ();
 
670
      throw;
 
671
    }
 
672
  }
 
673
 
 
674
  // compare a string with another one
 
675
  
 
676
  bool String::operator < (const String& s) const {
 
677
    rdlock   ();
 
678
    s.rdlock ();
 
679
    try {
 
680
      bool result = Unicode::strlth (p_sval, s.p_sval);
 
681
      unlock   ();
 
682
      s.unlock ();
 
683
      return result;
 
684
    } catch (...) {
 
685
      unlock   ();
 
686
      s.unlock ();
 
687
      throw;
 
688
    }
 
689
  }
 
690
 
 
691
  // compare a string with a c-string
 
692
  
 
693
  bool String::operator < (const char* s) const {
 
694
    rdlock ();
 
695
    try {
 
696
      bool result = Unicode::strlth (p_sval, s);
 
697
      unlock ();
 
698
      return result;
 
699
    } catch (...) {
 
700
      unlock ();
 
701
      throw;
 
702
    }
 
703
  }
 
704
 
 
705
  // compare a string with a unicode array
 
706
  
 
707
  bool String::operator < (const t_quad* s) const {
 
708
    rdlock ();
 
709
    try {
 
710
      bool result = Unicode::strlth (p_sval, s);
 
711
      unlock   ();
 
712
      return result;
 
713
    } catch (...) {
 
714
      unlock ();
 
715
      throw;
 
716
    }
 
717
  }
 
718
 
 
719
  // compare a string with another one
 
720
  
 
721
  bool String::operator <= (const String& s) const {
 
722
    rdlock   ();
 
723
    s.rdlock ();
 
724
    try {
 
725
      bool result = Unicode::strleq (p_sval, s.p_sval);
 
726
      unlock   ();
 
727
      s.unlock ();
 
728
      return result;
 
729
    } catch (...) {
 
730
      unlock   ();
 
731
      s.unlock ();
 
732
      throw;
 
733
    }
 
734
  }
 
735
 
 
736
  // compare a string with a c-string
 
737
  
 
738
  bool String::operator <= (const char* s) const {
 
739
    rdlock ();
 
740
    try {
 
741
      bool result = Unicode::strleq (p_sval, s);
 
742
      unlock ();
 
743
      return result;
 
744
    } catch (...) {
 
745
      unlock ();
 
746
      throw;
 
747
    }
 
748
  }
 
749
 
 
750
  // compare a string with a unicode array
 
751
  
 
752
  bool String::operator <= (const t_quad* s) const {
 
753
    rdlock ();
 
754
    try {
 
755
      bool result = Unicode::strleq (p_sval, s);
 
756
      unlock ();
 
757
      return result;
 
758
    } catch (...) {
 
759
      unlock ();
 
760
      throw;
 
761
    }
 
762
  }
 
763
 
 
764
  // compare a string with another one
 
765
  
 
766
  bool String::operator > (const String& s) const {
 
767
    rdlock   ();
 
768
    s.rdlock ();
 
769
    try {
 
770
      bool result = Unicode::strleq (p_sval, s.p_sval);
 
771
      unlock   ();
 
772
      s.unlock ();
 
773
      return !result;
 
774
    } catch (...) {
 
775
      unlock   ();
 
776
      s.unlock ();
 
777
      throw;
 
778
    }
 
779
  }
 
780
 
 
781
  // compare a string with a c-string
 
782
  
 
783
  bool String::operator > (const char* s) const {
 
784
    rdlock ();
 
785
    try {
 
786
      bool result = Unicode::strleq (p_sval, s);
 
787
      unlock ();
 
788
      return !result;
 
789
    } catch (...) {
 
790
      unlock ();
 
791
      throw;
 
792
    }
 
793
  }
 
794
 
 
795
  // compare a string with a unicode array
 
796
  
 
797
  bool String::operator > (const t_quad* s) const {
 
798
    rdlock ();
 
799
    try {
 
800
      bool result = Unicode::strleq (p_sval, s);
 
801
      unlock ();
 
802
      return !result;
 
803
    } catch (...) {
 
804
      unlock ();
 
805
      throw;
 
806
    }
 
807
  }
 
808
 
 
809
  // compare a string with another one
 
810
  
 
811
  bool String::operator >= (const String& s) const {
 
812
    rdlock   ();
 
813
    s.rdlock ();
 
814
    try {
 
815
      bool result = Unicode::strlth (p_sval, s.p_sval);
 
816
      unlock   ();
 
817
      s.unlock ();
 
818
      return !result;
 
819
    } catch (...) {
 
820
      unlock   ();
 
821
      s.unlock ();
 
822
      throw;
 
823
    }
 
824
  }
 
825
 
 
826
  // compare a string with a c-string
 
827
  
 
828
  bool String::operator >= (const char* s) const {
 
829
    rdlock ();
 
830
    try {
 
831
      bool result = Unicode::strlth (p_sval, s);
 
832
      unlock ();
 
833
      return !result;
 
834
    } catch (...) {
 
835
      unlock ();
 
836
      throw;
 
837
    }
 
838
  }
 
839
 
 
840
  // compare a string with a unicode array
 
841
  
 
842
  bool String::operator >= (const t_quad* s) const {
 
843
    rdlock ();
 
844
    try {
 
845
      bool result = Unicode::strlth (p_sval, s);
 
846
      unlock ();
 
847
      return !result;
 
848
    } catch (...) {
 
849
      unlock ();
 
850
      throw;
 
851
    }
 
852
  }
 
853
 
 
854
  // return the length of this string
 
855
  
 
856
  long String::length (void) const {
 
857
    rdlock ();
 
858
    try {
 
859
      long result = Unicode::strlen (p_sval);
 
860
      unlock ();
 
861
      return result;
 
862
    } catch (...) {
 
863
      unlock ();
 
864
      throw;
 
865
    }
 
866
  }
 
867
  
 
868
  // return the non combining length of this string
 
869
 
 
870
  long String::ncclen (void) const {
 
871
    rdlock ();
 
872
    try {
 
873
      long result = Unicode::ncclen (p_sval);
 
874
      unlock ();
 
875
      return result;
 
876
    } catch (...) {
 
877
      unlock ();
 
878
      throw;
 
879
    }
 
880
  }
 
881
 
 
882
  // compare a string with an array in normal form
 
883
 
 
884
  bool String::equals (const t_quad* s) const {
 
885
    rdlock ();
 
886
    try {
 
887
      bool result = Unicode::strcmp (p_sval, d_nrmf, s, true);
 
888
      unlock   ();
 
889
      return result;
 
890
    } catch (...) {
 
891
      unlock ();
 
892
      throw;
 
893
    }
 
894
  }
 
895
 
 
896
  // return a c-string representation or nilp for this string
 
897
 
 
898
  char* String::tochar (void) const {
 
899
    rdlock ();
 
900
    try {
 
901
      char* result = Ascii::strdup (p_sval);
 
902
      unlock ();
 
903
      return result;
 
904
    } catch (...) {
 
905
      unlock ();
 
906
      throw;
 
907
    }
 
908
  }
 
909
  
 
910
  // return an unicode string representation or nilp for this string
 
911
 
 
912
  t_quad* String::toquad (void) const {
 
913
    rdlock ();
 
914
    try {
 
915
      t_quad* result = Unicode::strdup (p_sval);
 
916
      unlock ();
 
917
      return result;
 
918
    } catch (...) {
 
919
      unlock ();
 
920
      throw;
 
921
    }
 
922
  }
 
923
 
 
924
  // return the first character
 
925
 
 
926
  t_quad String::first (void) const {
 
927
    rdlock ();
 
928
    try {
 
929
      t_quad result = isnil () ? nilq : p_sval[0];
 
930
      unlock ();
 
931
      return result;
 
932
    } catch (...) {
 
933
      unlock ();
 
934
      throw;
 
935
    }
 
936
  }
 
937
 
 
938
  // return the last character
 
939
 
 
940
  t_quad String::last (void) const {
 
941
    rdlock ();
 
942
    try {
 
943
      long len = length ();
 
944
      t_quad result = (len == 0) ? nilq : p_sval[len-1];
 
945
      unlock ();
 
946
      return result;
 
947
    } catch (...) {
 
948
      unlock ();
 
949
      throw;
 
950
    }
 
951
  }
 
952
 
 
953
  // remove leading blank from this string
 
954
  
 
955
  String String::stripl (void) const {
 
956
    rdlock ();
 
957
    try {
 
958
      t_quad* buffer = Unicode::stripl (p_sval);
 
959
      String  result = buffer;
 
960
      delete [] buffer;
 
961
      unlock ();
 
962
      return result;
 
963
    } catch (...) {
 
964
      unlock ();
 
965
      throw;
 
966
    }
 
967
  }
 
968
 
 
969
  // strip left a string with a separator specification
 
970
  
 
971
  String String::stripl (const String& sep) const {
 
972
    rdlock ();
 
973
    t_quad* qsep = sep.toquad ();
 
974
    try {
 
975
      t_quad* buffer = Unicode::stripl (p_sval, qsep);
 
976
      String  result = buffer;
 
977
      delete [] qsep;
 
978
      delete [] buffer;
 
979
      unlock ();
 
980
      return result;
 
981
    } catch (...) {
 
982
      delete [] qsep;
 
983
      unlock ();
 
984
      throw;
 
985
    }
 
986
  }
 
987
  
 
988
  // remove trailing blank from this string
 
989
  
 
990
  String String::stripr (void) const {
 
991
    rdlock ();
 
992
    try {
 
993
      t_quad* buffer = Unicode::stripr (p_sval);
 
994
      String  result = buffer;
 
995
      delete [] buffer;
 
996
      unlock ();
 
997
      return result;
 
998
    } catch (...) {
 
999
      unlock ();
 
1000
      throw;
 
1001
    }
 
1002
  }
 
1003
  
 
1004
  // strip right a string with a separator specification
 
1005
  
 
1006
  String String::stripr (const String& sep) const {
 
1007
    rdlock ();
 
1008
    t_quad* qsep = sep.toquad ();
 
1009
    try {
 
1010
      t_quad* buffer = Unicode::stripr (p_sval, qsep);
 
1011
      String  result = buffer;
 
1012
      delete [] qsep;
 
1013
      delete [] buffer;
 
1014
      unlock ();
 
1015
      return result;
 
1016
    } catch (...) {
 
1017
      delete [] qsep;
 
1018
      unlock ();
 
1019
      throw;
 
1020
    }
 
1021
  }
 
1022
 
 
1023
  // remove leading and trailing blank from this string
 
1024
  
 
1025
  String String::strip (void) const {
 
1026
    rdlock ();
 
1027
    try {
 
1028
      t_quad* lbuffer = Unicode::stripl (p_sval);
 
1029
      t_quad* rbuffer = Unicode::stripr (lbuffer);
 
1030
      String  result  = rbuffer;
 
1031
      delete [] lbuffer;
 
1032
      delete [] rbuffer;
 
1033
      unlock ();
 
1034
      return result;
 
1035
    } catch (...) {
 
1036
      unlock ();
 
1037
      throw;
 
1038
    }
 
1039
  }
 
1040
 
 
1041
  // strip a string with a separator specification
 
1042
  
 
1043
  String String::strip (const String& sep) const {
 
1044
    rdlock ();
 
1045
    t_quad* qsep = sep.toquad ();
 
1046
    try {
 
1047
      t_quad* lbuffer = Unicode::stripl (p_sval, qsep);
 
1048
      t_quad* rbuffer = Unicode::stripr (lbuffer, qsep);
 
1049
      String  result  = rbuffer;
 
1050
      delete [] qsep;
 
1051
      delete [] lbuffer;
 
1052
      delete [] rbuffer;
 
1053
      unlock ();
 
1054
      return result;
 
1055
    } catch (...) {
 
1056
      delete [] qsep;
 
1057
      unlock ();
 
1058
      throw;
 
1059
    }
 
1060
  }
 
1061
 
 
1062
  // reduce the string into a small form
 
1063
 
 
1064
  String String::redex (void) const {
 
1065
    rdlock ();
 
1066
    try {
 
1067
      t_quad* buffer = Unicode::redex (p_sval);
 
1068
      String  result = buffer;
 
1069
      delete [] buffer;
 
1070
      unlock ();
 
1071
      return result;
 
1072
    } catch (...) {
 
1073
      unlock ();
 
1074
      throw;
 
1075
    }
 
1076
  }
 
1077
 
 
1078
  // remove the quote from a string
 
1079
 
 
1080
  String String::rmquote (void) const {
 
1081
    rdlock ();
 
1082
    try {
 
1083
      t_quad* buffer = Unicode::rmquote (p_sval);
 
1084
      String  result = buffer;
 
1085
      delete [] buffer;
 
1086
      unlock ();
 
1087
      return result;
 
1088
    } catch (...) {
 
1089
      unlock ();
 
1090
      throw;
 
1091
    }
 
1092
  }
 
1093
 
 
1094
  // convert the string to upper case
 
1095
  
 
1096
  String String::toupper (void) const {
 
1097
    rdlock ();
 
1098
    try {
 
1099
      t_quad* buffer = Unicode::toupper (p_sval);
 
1100
      String  result = buffer;
 
1101
      delete [] buffer;
 
1102
      unlock ();
 
1103
      return result;
 
1104
    } catch (...) {
 
1105
      unlock ();
 
1106
      throw;
 
1107
    }
 
1108
  }
 
1109
 
 
1110
  // convert the string into a normal decomposition form
 
1111
 
 
1112
  String String::tonfd (void) const {
 
1113
    rdlock ();
 
1114
    try {
 
1115
      t_quad* buffer = Unicode::strnrm (p_sval);
 
1116
      String  result = buffer;
 
1117
      delete [] buffer;
 
1118
      unlock ();
 
1119
      return result;
 
1120
    } catch (...) {
 
1121
      unlock ();
 
1122
      throw;
 
1123
    }
 
1124
  }
 
1125
 
 
1126
  // convert the string to lower case
 
1127
  
 
1128
  String String::tolower (void) const {
 
1129
    rdlock ();
 
1130
    try {
 
1131
      t_quad* buffer = Unicode::tolower (p_sval);
 
1132
      String  result = buffer;
 
1133
      delete [] buffer;
 
1134
      unlock ();
 
1135
      return result;
 
1136
    } catch (...) {
 
1137
      unlock ();
 
1138
      throw;
 
1139
    }
 
1140
  }
 
1141
 
 
1142
  // return the right substring starting at a certain index
 
1143
 
 
1144
  String String::rsubstr (const long index) const {
 
1145
    rdlock ();
 
1146
    try {
 
1147
      String result;
 
1148
      long len = length ();
 
1149
      if ((len == 0) || (index >= len)) {
 
1150
        unlock ();
 
1151
        return result;
 
1152
      }
 
1153
      t_quad* sptr = p_sval + index;
 
1154
      result = sptr;
 
1155
      unlock ();
 
1156
      return result;
 
1157
    } catch (...) {
 
1158
      unlock ();
 
1159
      throw;
 
1160
    }
 
1161
  }
 
1162
 
 
1163
  // return the left substring of a string
 
1164
 
 
1165
  String String::lsubstr (const long index) const {
 
1166
    rdlock ();
 
1167
    try {
 
1168
      String result;
 
1169
      long len = length ();
 
1170
      if ((len == 0) || (index > len)) {
 
1171
        unlock ();
 
1172
        return result;
 
1173
      }
 
1174
      t_quad* sptr = Unicode::strdup (p_sval);
 
1175
      sptr[index] = nilq;
 
1176
      result = sptr;
 
1177
      delete [] sptr;
 
1178
      unlock ();
 
1179
      return result;
 
1180
    } catch (...) {
 
1181
      unlock ();
 
1182
      throw;
 
1183
    }
 
1184
  }
 
1185
 
 
1186
  // return a substring of a string
 
1187
 
 
1188
  String String::substr (const long lidx, const long ridx) const {
 
1189
    rdlock ();
 
1190
    try {
 
1191
      long len = length ();
 
1192
      if ((lidx >= ridx) || (lidx < 0) || (lidx >= len) || (ridx <0) ||
 
1193
          (ridx >  len)  || (len == 0)) {
 
1194
        throw Exception ("index-error", "invalid index for sub-string");
 
1195
      }
 
1196
      // create th result string
 
1197
      String result;
 
1198
      t_quad* buf = Unicode::strdup (p_sval);
 
1199
      t_quad* sub = buf + lidx;
 
1200
      buf[ridx] = nilq;
 
1201
      result = sub;
 
1202
      delete [] buf;
 
1203
      unlock ();
 
1204
      return result;
 
1205
    } catch (...) {
 
1206
      unlock ();
 
1207
      throw;
 
1208
    }
 
1209
  }
 
1210
 
 
1211
  // return the hashid for this string
 
1212
  
 
1213
  long String::hashid (void) const {
 
1214
    rdlock ();
 
1215
    try {
 
1216
      long result = String::hashid (p_sval);
 
1217
      unlock ();
 
1218
      return result;
 
1219
    } catch (...) {
 
1220
      unlock ();
 
1221
      throw;
 
1222
    }
 
1223
  }
 
1224
 
 
1225
  // case insensitive string comparision
 
1226
 
 
1227
  bool String::strcic (const String& s) const {
 
1228
    rdlock ();
 
1229
    try {
 
1230
      bool result = String::strcic (*this, s);
 
1231
      unlock ();
 
1232
      return result;
 
1233
    } catch (...) {
 
1234
      unlock ();
 
1235
      throw;
 
1236
    }
 
1237
  }
 
1238
 
 
1239
  // fill this string with a character until a given size is reached
 
1240
  
 
1241
  String String::lfill (const char c, const long size) const {
 
1242
    rdlock ();
 
1243
    try {
 
1244
      // do nothing if size exceed
 
1245
      long len = size - ncclen ();
 
1246
      if (len <= 0) {
 
1247
        String result = *this;
 
1248
        unlock ();
 
1249
        return result;
 
1250
      }
 
1251
      // fill the string first
 
1252
      String result;
 
1253
      for (long i = 0; i < len; i++) result = result + c;
 
1254
      result += *this;
 
1255
      unlock ();
 
1256
      return result;
 
1257
    } catch (...) {
 
1258
      unlock ();
 
1259
      throw;
 
1260
    }
 
1261
  }
 
1262
 
 
1263
  // fill this string with a character until a given size is reached
 
1264
  
 
1265
  String String::lfill (const t_quad c, const long size) const {
 
1266
    rdlock ();
 
1267
    try {
 
1268
      // do nothing if size exceed
 
1269
      long len = size - ncclen ();
 
1270
      if (len <= 0) {
 
1271
        String result = *this;
 
1272
        unlock ();
 
1273
        return result;
 
1274
      }
 
1275
      // fill the string first
 
1276
      String result;
 
1277
      for (long i = 0; i < len; i++) result = result + c;
 
1278
      result += *this;
 
1279
      unlock ();
 
1280
      return result;
 
1281
    } catch (...) {
 
1282
      unlock ();
 
1283
      throw;
 
1284
    }
 
1285
  }
 
1286
 
 
1287
  // fill this string with a character until a given size is reached
 
1288
  
 
1289
  String String::rfill (const char c, const long size) const {
 
1290
    rdlock ();
 
1291
    try {
 
1292
      // do nothing if size exceed
 
1293
      long len = size - ncclen ();
 
1294
      if (len <= 0) {
 
1295
        String result = *this;
 
1296
        unlock ();
 
1297
        return result;
 
1298
      }
 
1299
      // fill the string first
 
1300
      String result = *this;
 
1301
      for (long i = 0; i < len; i++) result = result + c;
 
1302
      unlock ();
 
1303
      return result;
 
1304
    } catch (...) {
 
1305
      unlock ();
 
1306
      throw;
 
1307
    }
 
1308
  }
 
1309
 
 
1310
  // fill this string with a character until a given size is reached
 
1311
  
 
1312
  String String::rfill (const t_quad c, const long size) const {
 
1313
    rdlock ();
 
1314
    try {
 
1315
      // do nothing if size exceed
 
1316
      long len = size - ncclen ();
 
1317
      if (len <= 0) {
 
1318
        String result = *this;
 
1319
        unlock ();
 
1320
        return result;
 
1321
      }
 
1322
      // fill the string first
 
1323
      String result = *this;
 
1324
      for (long i = 0; i < len; i++) result = result + c;
 
1325
      unlock ();
 
1326
      return result;
 
1327
    } catch (...) {
 
1328
      unlock ();
 
1329
      throw;
 
1330
    }
 
1331
  }
 
1332
 
 
1333
  // split a string with blank and tab
 
1334
 
 
1335
  Vector* String::split (void) const {
 
1336
    rdlock ();
 
1337
    try {
 
1338
     Vector* result = split ("");
 
1339
     unlock ();
 
1340
     return result;
 
1341
    } catch (...) {
 
1342
      unlock ();
 
1343
      throw;
 
1344
    }
 
1345
  }
 
1346
 
 
1347
  // split a string according to a string break
 
1348
 
 
1349
  Vector* String::split (const String& sbrk) const {
 
1350
    rdlock ();
 
1351
    Vector* result = new Vector;
 
1352
    try {
 
1353
      Strvec vec = Strvec::split (*this, sbrk);
 
1354
      long len = vec.length ();
 
1355
      for (long i = 0; i < len; i++) {
 
1356
        String data = vec.get (i);
 
1357
        result->add (new String (data));
 
1358
      }
 
1359
    } catch (...) {
 
1360
      delete result;
 
1361
      unlock ();
 
1362
      throw;
 
1363
    }
 
1364
    return result;
 
1365
  }
 
1366
 
 
1367
  // accumulate strings between a control character
 
1368
 
 
1369
  Vector* String::extract (const t_quad cbrk) const {
 
1370
    rdlock ();
 
1371
    Vector* result = new Vector;
 
1372
    long len = length ();
 
1373
    for (long i = 0; i < len; i++) {
 
1374
      t_quad c = p_sval[i];
 
1375
      if (c == cbrk) {
 
1376
        i++;
 
1377
        String buf;
 
1378
        while ((c = p_sval[i]) != cbrk) {
 
1379
          buf += c ;
 
1380
          i++;
 
1381
          if (i == len) {
 
1382
            delete result;
 
1383
            unlock ();
 
1384
            throw Exception ("extract-error", "unterminated string", *this);
 
1385
          }
 
1386
        }
 
1387
        result->add (new String (buf));
 
1388
      }
 
1389
    }
 
1390
    unlock ();
 
1391
    return result;
 
1392
  }
 
1393
 
 
1394
  // -------------------------------------------------------------------------
 
1395
  // - object section                                                        -
 
1396
  // -------------------------------------------------------------------------
 
1397
 
 
1398
  // the quark zone
 
1399
  static const long QUARK_ZONE_LENGTH = 31;
 
1400
  static QuarkZone  zone (QUARK_ZONE_LENGTH);
 
1401
 
 
1402
  // the supported quark for this string class
 
1403
  static const long QUARK_ADD       = zone.intern ("+");
 
1404
  static const long QUARK_EQL       = zone.intern ("==");
 
1405
  static const long QUARK_NEQ       = zone.intern ("!=");
 
1406
  static const long QUARK_LTH       = zone.intern ("<");
 
1407
  static const long QUARK_LEQ       = zone.intern ("<=");
 
1408
  static const long QUARK_GTH       = zone.intern (">");
 
1409
  static const long QUARK_GEQ       = zone.intern (">=");
 
1410
  static const long QUARK_AEQ       = zone.intern ("+=");
 
1411
  static const long QUARK_GET       = zone.intern ("get");
 
1412
  static const long QUARK_NILP      = zone.intern ("nil-p");
 
1413
  static const long QUARK_LAST      = zone.intern ("last");
 
1414
  static const long QUARK_FIRST     = zone.intern ("first");
 
1415
  static const long QUARK_TONFD     = zone.intern ("to-normal-form");
 
1416
  static const long QUARK_SPLIT     = zone.intern ("split");
 
1417
  static const long QUARK_STRIP     = zone.intern ("strip");
 
1418
  static const long QUARK_REDEX     = zone.intern ("redex");
 
1419
  static const long QUARK_LENGTH    = zone.intern ("length");
 
1420
  static const long QUARK_NCCLEN    = zone.intern ("non-combining-length");
 
1421
  static const long QUARK_HASHID    = zone.intern ("hashid");
 
1422
  static const long QUARK_STRIPL    = zone.intern ("strip-left");
 
1423
  static const long QUARK_STRIPR    = zone.intern ("strip-right");
 
1424
  static const long QUARK_SUBSTR    = zone.intern ("substr");
 
1425
  static const long QUARK_STRCIC    = zone.intern ("strcic");
 
1426
  static const long QUARK_RMQUOTE   = zone.intern ("remove-quote");
 
1427
  static const long QUARK_EXTRACT   = zone.intern ("extract");
 
1428
  static const long QUARK_TOUPPER   = zone.intern ("to-upper");
 
1429
  static const long QUARK_TOLOWER   = zone.intern ("to-lower");
 
1430
  static const long QUARK_SUBLEFT   = zone.intern ("sub-left");
 
1431
  static const long QUARK_SUBRIGHT  = zone.intern ("sub-right");
 
1432
  static const long QUARK_FILLLEFT  = zone.intern ("fill-left");
 
1433
  static const long QUARK_FILLRIGHT = zone.intern ("fill-right");
 
1434
 
 
1435
  // create a new object in a generic way
 
1436
 
 
1437
  Object* String::mknew (Vector* argv) {
 
1438
    if ((argv == nilp) || (argv->length () == 0)) return new String;
 
1439
    if (argv->length () != 1) 
 
1440
      throw Exception ("argument-error", 
 
1441
                       "too many argument with string constructor");
 
1442
    // try to map the string argument
 
1443
    Object* obj = argv->get (0);
 
1444
    if (obj == nilp) return new String;
 
1445
 
 
1446
    // try a literal object
 
1447
    Literal* lval = dynamic_cast <Literal*> (obj);
 
1448
    if (lval != nilp) return new String (lval->tostring ());
 
1449
 
 
1450
    // illegal object
 
1451
    throw Exception ("type-error", "illegal object with string constructor",
 
1452
                     obj->repr ());
 
1453
  }
 
1454
 
 
1455
  // return true if the given quark is defined
 
1456
 
 
1457
  bool String::isquark (const long quark, const bool hflg) const {
 
1458
    rdlock ();
 
1459
    if (zone.exists (quark) == true) {
 
1460
      unlock ();
 
1461
      return true;
 
1462
    }
 
1463
    bool result = hflg ? Literal::isquark (quark, hflg) : false;
 
1464
    unlock ();
 
1465
    return result;
 
1466
  }
 
1467
 
 
1468
  // operate this object with another object
 
1469
 
 
1470
  Object* String::oper (t_oper type, Object* object) {
 
1471
    Literal* lobj = dynamic_cast <Literal*> (object);
 
1472
    String*  sobj = dynamic_cast <String*>    (object);
 
1473
 
 
1474
    switch (type) {
 
1475
    case Object::ADD:
 
1476
      if (lobj != nilp) return new String (*this + lobj->tostring ());
 
1477
      break;  
 
1478
    case Object::EQL:
 
1479
      if (sobj != nilp) return new Boolean (*this == *sobj);
 
1480
      break;
 
1481
    case Object::NEQ:
 
1482
      if (sobj != nilp) return new Boolean (*this != *sobj);
 
1483
      break;
 
1484
    case Object::LTH:
 
1485
      if (sobj != nilp) return new Boolean (*this < *sobj);
 
1486
      break;
 
1487
    case Object::LEQ:
 
1488
      if (sobj != nilp) return new Boolean (*this <= *sobj);
 
1489
      break;
 
1490
    case Object::GTH:
 
1491
      if (sobj != nilp) return new Boolean (*this > *sobj);
 
1492
      break;
 
1493
    case Object::GEQ:
 
1494
      if (sobj != nilp) return new Boolean (*this >= *sobj);
 
1495
      break;
 
1496
    default:
 
1497
      throw Exception ("operator-error", "unsupported string operator");
 
1498
    }
 
1499
    throw Exception ("type-error", "invalid operand with string",
 
1500
                     Object::repr (object));
 
1501
  }
 
1502
 
 
1503
  // set an object to this object
 
1504
 
 
1505
  Object* String::vdef (Runnable* robj, Nameset* nset, Object* object) {
 
1506
    Literal* lobj = dynamic_cast <Literal*> (object);
 
1507
    if (lobj != nilp) {
 
1508
      *this = lobj->tostring ();
 
1509
      return this;
 
1510
    }
 
1511
    throw Exception ("type-error", "invalid object with string vdef",
 
1512
                     Object::repr (object));
 
1513
  }
 
1514
 
 
1515
  // apply this object with a set of arguments and a quark
 
1516
 
 
1517
  Object* String::apply (Runnable* robj, Nameset* nset, long quark,
 
1518
                         Vector* argv) {
 
1519
    // get the arguments length
 
1520
    long argc = (argv == nilp) ? 0 : argv->length ();
 
1521
 
 
1522
    // dispatch 0 argument
 
1523
    if (argc == 0) {
 
1524
      if (quark == QUARK_NILP)    return new Boolean   (isnil   ());
 
1525
      if (quark == QUARK_LAST)    return new Character (last    ());
 
1526
      if (quark == QUARK_FIRST)   return new Character (first   ());
 
1527
      if (quark == QUARK_TONFD)   return new String    (tonfd   ());
 
1528
      if (quark == QUARK_LENGTH)  return new Integer   (length  ());
 
1529
      if (quark == QUARK_NCCLEN)  return new Integer   (ncclen  ());
 
1530
      if (quark == QUARK_STRIPL)  return new String    (stripl  ());
 
1531
      if (quark == QUARK_STRIPR)  return new String    (stripr  ());
 
1532
      if (quark == QUARK_STRIP)   return new String    (strip   ());
 
1533
      if (quark == QUARK_REDEX)   return new String    (redex   ());
 
1534
      if (quark == QUARK_RMQUOTE) return new String    (rmquote ());
 
1535
      if (quark == QUARK_TOUPPER) return new String    (toupper ());
 
1536
      if (quark == QUARK_TOLOWER) return new String    (tolower ());
 
1537
      if (quark == QUARK_HASHID)  return new Integer   (hashid  ());
 
1538
      if (quark == QUARK_SPLIT) {
 
1539
        Object* result = split ();
 
1540
        robj->post (result);
 
1541
        return result;
 
1542
      }
 
1543
    }
 
1544
    // dispatch 1 argument
 
1545
    if (argc == 1) {
 
1546
      if (quark == QUARK_ADD)   return oper (Object::ADD, argv->get (0));
 
1547
      if (quark == QUARK_EQL)   return oper (Object::EQL, argv->get (0));
 
1548
      if (quark == QUARK_NEQ)   return oper (Object::NEQ, argv->get (0));
 
1549
      if (quark == QUARK_SPLIT) {
 
1550
        Object* result = split (argv->getstring (0));
 
1551
        robj->post (result);
 
1552
        return result;
 
1553
      }
 
1554
      if (quark == QUARK_AEQ) {
 
1555
        Object*   obj = argv->get (0);
 
1556
        Literal* lobj = dynamic_cast <Literal*> (obj);
 
1557
        if (lobj == nilp) {
 
1558
          throw Exception ("type-error", "invalid object with operator +=",
 
1559
                           Object::repr (obj));
 
1560
        }
 
1561
        String val = lobj->tostring ();
 
1562
        *this = *this + val;
 
1563
        robj->post (this);
 
1564
        return this;
 
1565
      }
 
1566
      if (quark == QUARK_GET) {
 
1567
        t_long val = argv->getlong (0);
 
1568
        char c = (*this)[val];
 
1569
        return new Character (c);
 
1570
      }
 
1571
      if (quark == QUARK_EXTRACT) {
 
1572
        t_quad cbrk = argv->getchar (0);
 
1573
        Object* result = extract (cbrk);
 
1574
        robj->post (result);
 
1575
        return result;
 
1576
      }
 
1577
      if (quark == QUARK_STRIPL) {
 
1578
        String sep = argv->getstring (0);
 
1579
        String result = stripl (sep);
 
1580
        return new String (result);
 
1581
      }
 
1582
      if (quark == QUARK_STRIPR) {
 
1583
        String sep = argv->getstring (0);
 
1584
        String result = stripr (sep);
 
1585
        return new String (result);
 
1586
      }
 
1587
      if (quark == QUARK_STRIP) {
 
1588
        String sep = argv->getstring (0);
 
1589
        String result = strip (sep);
 
1590
        return new String (result);
 
1591
      }
 
1592
      if (quark == QUARK_SUBRIGHT) {
 
1593
        t_long val = argv->getlong (0);
 
1594
        String result = rsubstr (val);
 
1595
        return new String (result);
 
1596
      }
 
1597
      if (quark == QUARK_SUBLEFT) {
 
1598
        t_long val = argv->getlong (0);
 
1599
        String result = lsubstr (val);
 
1600
        return new String (result);
 
1601
      }
 
1602
      if (quark == QUARK_STRCIC) {
 
1603
        String    s = argv->getstring (0);
 
1604
        bool result = strcic (s);
 
1605
        return new Boolean (result);
 
1606
      }
 
1607
    }
 
1608
    // dispatch 2 arguments
 
1609
    if (argc == 2) {
 
1610
      if (quark == QUARK_FILLLEFT) {
 
1611
        t_quad fill   = argv->getchar (0);
 
1612
        t_long size   = argv->getlong (1);
 
1613
        String result = lfill (fill, size);
 
1614
        return new String (result);
 
1615
      }
 
1616
      if (quark == QUARK_FILLRIGHT) {
 
1617
        t_quad fill   = argv->getchar (0);
 
1618
        t_long size   = argv->getlong (1);
 
1619
        String result = rfill (fill, size);
 
1620
        return new String (result);
 
1621
      }
 
1622
      if (quark == QUARK_SUBSTR) {
 
1623
        t_long lidx   = argv->getlong (0);
 
1624
        t_long ridx   = argv->getlong (1);
 
1625
        String result = substr (lidx, ridx);
 
1626
        return new String (result);
 
1627
      }
 
1628
    }
 
1629
    // call the literal method
 
1630
    return Literal::apply (robj, nset, quark, argv);
 
1631
  }
 
1632
}