41
37
#include "strclass.h"
42
38
#include "config.h"
45
40
#include <limits.h>
47
42
#include <stdlib.h>
53
44
void string::error(const char* msg) const
55
cerr << "string: " << msg << "\n";
46
std::cerr << "string: " << msg << "\n";
60
string::operator const char*() const
62
return (const char *)chars();
65
string::operator char*() const
67
return (char *)chars();
73
52
// nil strings point here
1308
1287
return _substr(first, length() - first);
1311
constSubString string::from(const char* t, int startpos) const
1290
constSubString string::from(const char *t, int startpos) const
1313
1292
int tlen = slen(t);
1314
1293
int first = search(startpos, length(), t, tlen);
1363
string join(const string *src, int n, const string& separator) RETURNS(x)
1342
string join(const string *src, int n, const string& separator)
1365
RETURN_OBJECT(string, x);
1366
string sep = separator;
1345
const string& sep = separator;
1369
1348
for (i = 0; i < n; ++i)
1477
#if HAVE_NAMED_RETURN_VALUES
1479
#if 0 // already defined in -lg++
1480
string replicate(char c, int n) return w;
1456
string replicate(char y, int n)
1482
1459
w.rep = string_Sresize(w.rep, n);
1483
1460
char* p = w.rep->s;
1484
while (n-- > 0) *p++ = c;
1489
string replicate(const string& y, int n) return w
1491
int len = y.length();
1492
w.rep = string_Sresize(w.rep, n * len);
1494
1461
while (n-- > 0)
1496
ncopy(y.chars(), p, len);
1502
string common_prefix(const string& x, const string& y, int startpos) return r;
1504
if ((int)x.length() + startpos < 0 || (int)y.length() + startpos < 0)
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()]);
1515
for (l = 0; xs < topx && ys < topy && *xs++ == *ys++; ++l)
1517
r.rep = string_Salloc(r.rep, ss, l, l);
1520
string common_suffix(const string& x, const string& y, int startpos) return r;
1522
if ((int)x.length() + startpos < 0 || (int)y.length() + startpos < 0)
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;
1532
for (l = 0; xs >= botx && ys >= boty && *xs == *ys ; --xs, --ys, ++l)
1534
r.rep = string_Salloc(r.rep, ++xs, l, l);
1537
#else // !HAVE_NAMED_RETURN_VALUES
1540
string replicate(char c, int n)
1543
w.rep = string_Sresize(w.rep, n);
1552
1469
string replicate(const string& y, int n)
1606
#endif // !HAVE_NAMED_RETURN_VALUES
1610
istream& operator>>(istream& s, string& x)
1525
std::istream& operator>>(std::istream& s, string& x)
1612
1527
// Read whitespace
1615
s.clear(ios::failbit|s.rdstate());
1530
s.clear(std::ios::failbit|s.rdstate());
1619
if (s.flags() & ios::skipws)
1534
if (s.flags() & std::ios::skipws)
1624
s.clear(ios::failbit|s.rdstate());
1539
s.clear(std::ios::failbit|s.rdstate());
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)
1634
1549
if (isspace(ch))
1641
1556
x.rep->len = i;
1644
s.clear(ios::failbit|s.rdstate());
1559
s.clear(std::ios::failbit|s.rdstate());
1646
s.clear(ios::eofbit|s.rdstate());
1561
s.clear(std::ios::eofbit|s.rdstate());
1651
int readline(istream& s, string& x, char terminator, int discard)
1566
int readline(std::istream& s, string& x, char terminator, int discard)
1653
1568
assert(!x.consuming());
1655
1570
// Read whitespace
1658
s.clear(ios::failbit|s.rdstate());
1573
s.clear(std::ios::failbit|s.rdstate());
1662
if (s.flags() & ios::skipws)
1577
if (s.flags() & std::ios::skipws)
1667
s.clear(ios::failbit|s.rdstate());
1582
s.clear(std::ios::failbit|s.rdstate());
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)
1677
1592
if (ch != terminator || !discard)