~pac72/ubuntu/lucid/ddd/devel

« back to all changes in this revision

Viewing changes to ddd/strclass.C

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Schepler
  • Date: 2004-07-22 03:49:37 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040722034937-cysl08t1jvba4jrx
Tags: 1:3.3.9-3
USERINFO has been renamed to USERINFO.txt; adjust debian/rules code
to match, to get correct information on the About DDD dialog.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// $Id: strclass.C,v 1.23 2001/03/30 12:31:46 zeller Exp $ -*- C++ -*-
 
1
// $Id$ -*- C++ -*-
2
2
// The libg++ String class, but named "string" for coexistence with Xt
3
3
 
4
4
/* 
19
19
*/
20
20
 
21
21
char strclass_rcsid[] = 
22
 
    "$Id: strclass.C,v 1.23 2001/03/30 12:31:46 zeller Exp $";
 
22
    "$Id$";
23
23
 
24
24
/* 
25
25
  string class implementation
26
26
 */
27
27
 
28
 
#ifdef __GNUG__
29
 
#pragma implementation
30
 
#endif
31
 
 
32
28
#ifndef MALLOC_DEBUG
33
29
#define MALLOC_DEBUG 0
34
30
#endif
40
36
 
41
37
#include "strclass.h"
42
38
#include "config.h"
43
 
#include "return.h"
44
39
#include <ctype.h>
45
40
#include <limits.h>
46
 
#include <new.h>
 
41
#include <new>
47
42
#include <stdlib.h>
48
43
 
49
 
#if HAVE_LIMITS_H
50
 
#include <limits.h>
51
 
#endif
52
 
 
53
44
void string::error(const char* msg) const
54
45
{
55
 
    cerr << "string: " << msg << "\n";
 
46
    std::cerr << "string: " << msg << "\n";
56
47
    abort();
57
48
}
58
49
 
59
 
#if 0
60
 
string::operator const char*() const
61
 
62
 
    return (const char *)chars();
63
 
}
64
 
 
65
 
string::operator char*() const
66
 
67
 
    return (char *)chars();
68
 
}
69
 
#endif
70
 
 
71
50
//  globals
72
51
 
73
52
// nil strings point here
181
160
#if 0
182
161
    if ((unsigned long)allocsiz >= MAX_STRREP_SIZE)
183
162
    {
184
 
        cerr << "string: requested length out of range\n";
 
163
        std::cerr << "string: requested length out of range\n";
185
164
        abort();
186
165
    }
187
166
#endif
1228
1207
{
1229
1208
    int mlen;
1230
1209
    int first = r.search(chars(), length(), mlen, startpos);
1231
 
    if (first >= 0) 
 
1210
    if (first >= 0)
1232
1211
        first += mlen;
1233
1212
    return _substr(first, length() - first);
1234
1213
}
1308
1287
    return _substr(first, length() - first);
1309
1288
}
1310
1289
 
1311
 
constSubString string::from(const char* t, int startpos) const
 
1290
constSubString string::from(const char *t, int startpos) const
1312
1291
{
1313
1292
    int tlen = slen(t);
1314
1293
    int first = search(startpos, length(), t, tlen);
1323
1302
 
1324
1303
int split(const string& src, string *results, int n, const string& sep)
1325
1304
{
1326
 
    string x = src;
 
1305
    const string& x = src;
1327
1306
    const char* s = x.chars();
1328
1307
    int sl = x.length();
1329
1308
    int i = 0;
1342
1321
 
1343
1322
int split(const string& src, string *results, int n, const regex& r)
1344
1323
{
1345
 
    string x = src;
 
1324
    const string& x = src;
1346
1325
    const char* s = x.chars();
1347
1326
    int sl = x.length();
1348
1327
    int i = 0;
1360
1339
    return i;
1361
1340
}
1362
1341
 
1363
 
string join(const string *src, int n, const string& separator) RETURNS(x)
 
1342
string join(const string *src, int n, const string& separator)
1364
1343
{
1365
 
    RETURN_OBJECT(string, x);
1366
 
    string sep = separator;
 
1344
    string x;
 
1345
    const string& sep = separator;
1367
1346
    int xlen = 0;
1368
1347
    int i;
1369
1348
    for (i = 0; i < n; ++i)
1383
1362
        j += sep.length();
1384
1363
    }
1385
1364
    ncopy0(src[i].chars(), &(x.rep->s[j]), src[i].length());
1386
 
    RETURN(x);
 
1365
    return x;
1387
1366
}
1388
1367
 
1389
1368
  
1474
1453
    return dest;
1475
1454
}
1476
1455
 
1477
 
#if HAVE_NAMED_RETURN_VALUES
1478
 
 
1479
 
#if 0                           // already defined in -lg++
1480
 
string replicate(char c, int n) return w;
 
1456
string replicate(char y, int n)
1481
1457
{
 
1458
    string w;
1482
1459
    w.rep = string_Sresize(w.rep, n);
1483
1460
    char* p = w.rep->s;
1484
 
    while (n-- > 0) *p++ = c;
1485
 
    *p = 0;
1486
 
}
1487
 
#endif
1488
 
 
1489
 
string replicate(const string& y, int n) return w
1490
 
{
1491
 
    int len = y.length();
1492
 
    w.rep = string_Sresize(w.rep, n * len);
1493
 
    char* p = w.rep->s;
1494
1461
    while (n-- > 0)
1495
1462
    {
1496
 
        ncopy(y.chars(), p, len);
1497
 
        p += len;
 
1463
        *p++ = y;
1498
1464
    }
1499
1465
    *p = 0;
1500
 
}
1501
 
 
1502
 
string common_prefix(const string& x, const string& y, int startpos) return r;
1503
 
{
1504
 
    if ((int)x.length() + startpos < 0 || (int)y.length() + startpos < 0)
1505
 
        return;
1506
 
 
1507
 
    const char* xchars = x.chars();
1508
 
    const char* ychars = y.chars();
1509
 
    const char* xs = &(xchars[startpos]);
1510
 
    const char* ss = xs;
1511
 
    const char* topx = &(xchars[x.length()]);
1512
 
    const char* ys = &(ychars[startpos]);
1513
 
    const char* topy = &(ychars[y.length()]);
1514
 
    int l;
1515
 
    for (l = 0; xs < topx && ys < topy && *xs++ == *ys++; ++l)
1516
 
        ;
1517
 
    r.rep = string_Salloc(r.rep, ss, l, l);
1518
 
}
1519
 
 
1520
 
string common_suffix(const string& x, const string& y, int startpos) return r;
1521
 
{
1522
 
    if ((int)x.length() + startpos < 0 || (int)y.length() + startpos < 0)
1523
 
        return;
1524
 
 
1525
 
    const char* xchars = x.chars();
1526
 
    const char* ychars = y.chars();
1527
 
    const char* xs = &(xchars[x.length() + startpos]);
1528
 
    const char* botx = xchars;
1529
 
    const char* ys = &(ychars[y.length() + startpos]);
1530
 
    const char* boty = ychars;
1531
 
    int l;
1532
 
    for (l = 0; xs >= botx && ys >= boty && *xs == *ys ; --xs, --ys, ++l)
1533
 
        ;
1534
 
    r.rep = string_Salloc(r.rep, ++xs, l, l);
1535
 
}
1536
 
 
1537
 
#else // !HAVE_NAMED_RETURN_VALUES
1538
 
 
1539
 
#if 0
1540
 
string replicate(char c, int n)
1541
 
{
1542
 
    string w;
1543
 
    w.rep = string_Sresize(w.rep, n);
1544
 
    char* p = w.rep->s;
1545
 
    while (n-- > 0) 
1546
 
        *p++ = c;
1547
 
    *p = 0;
1548
1466
    return w;
1549
1467
}
1550
 
#endif
1551
1468
 
1552
1469
string replicate(const string& y, int n)
1553
1470
{
1603
1520
    return r;
1604
1521
}
1605
1522
 
1606
 
#endif // !HAVE_NAMED_RETURN_VALUES
1607
 
 
1608
1523
// IO
1609
1524
 
1610
 
istream& operator>>(istream& s, string& x)
 
1525
std::istream& operator>>(std::istream& s, string& x)
1611
1526
{
1612
1527
    // Read whitespace
1613
1528
    if (!s.good()) 
1614
1529
    {
1615
 
        s.clear(ios::failbit|s.rdstate());
 
1530
        s.clear(std::ios::failbit|s.rdstate());
1616
1531
        return s;
1617
1532
    }
1618
1533
 
1619
 
    if (s.flags() & ios::skipws)
1620
 
        ws(s);
 
1534
    if (s.flags() & std::ios::skipws)
 
1535
        std::ws(s);
1621
1536
 
1622
1537
    if (!s.good()) 
1623
1538
    {
1624
 
        s.clear(ios::failbit|s.rdstate());
 
1539
        s.clear(std::ios::failbit|s.rdstate());
1625
1540
        return s;
1626
1541
    }
1627
1542
 
1628
1543
    int ch;
1629
1544
    unsigned i = 0;
1630
1545
    x.rep = string_Sresize(x.rep, 20);
1631
 
    register streambuf *sb = s.rdbuf();
 
1546
    register std::streambuf *sb = s.rdbuf();
1632
1547
    while ((ch = sb->sbumpc()) != EOF)
1633
1548
    {
1634
1549
        if (isspace(ch))
1641
1556
    x.rep->len = i;
1642
1557
 
1643
1558
    if (i == 0)
1644
 
        s.clear(ios::failbit|s.rdstate());
 
1559
        s.clear(std::ios::failbit|s.rdstate());
1645
1560
    if (ch == EOF)
1646
 
        s.clear(ios::eofbit|s.rdstate());
 
1561
        s.clear(std::ios::eofbit|s.rdstate());
1647
1562
        
1648
1563
    return s;
1649
1564
}
1650
1565
 
1651
 
int readline(istream& s, string& x, char terminator, int discard)
 
1566
int readline(std::istream& s, string& x, char terminator, int discard)
1652
1567
{
1653
1568
    assert(!x.consuming());
1654
1569
 
1655
1570
    // Read whitespace
1656
1571
    if (!s.good()) 
1657
1572
    {
1658
 
        s.clear(ios::failbit|s.rdstate());
 
1573
        s.clear(std::ios::failbit|s.rdstate());
1659
1574
        return 0;
1660
1575
    }
1661
1576
 
1662
 
    if (s.flags() & ios::skipws)
1663
 
        ws(s);
 
1577
    if (s.flags() & std::ios::skipws)
 
1578
        std::ws(s);
1664
1579
 
1665
1580
    if (!s.good()) 
1666
1581
    {
1667
 
        s.clear(ios::failbit|s.rdstate());
 
1582
        s.clear(std::ios::failbit|s.rdstate());
1668
1583
        return 0;
1669
1584
    }
1670
1585
 
1671
1586
    int ch;
1672
1587
    unsigned i = 0;
1673
1588
    x.rep = string_Sresize(x.rep, 80);
1674
 
    register streambuf *sb = s.rdbuf();
 
1589
    register std::streambuf *sb = s.rdbuf();
1675
1590
    while ((ch = sb->sbumpc()) != EOF)
1676
1591
    {
1677
1592
        if (ch != terminator || !discard)
1686
1601
    x.rep->s[i] = 0;
1687
1602
    x.rep->len = i;
1688
1603
    if (ch == EOF)
1689
 
        s.clear(s.rdstate() | ios::eofbit);
 
1604
        s.clear(s.rdstate() | std::ios::eofbit);
1690
1605
    return i;
1691
1606
}
1692
1607
 
1756
1671
{
1757
1672
    // Check for legal string and pos and len outside bounds
1758
1673
    if (!S.OK() || pos + len > S.rep->len)
1759
 
        S.error("subString invariant failure");
 
1674
        S.error("constsubString invariant failure");
1760
1675
    return true;
1761
1676
}