2000-07-24 13:53:19 +00:00
|
|
|
/* This file is part of
|
|
|
|
* ======================================================
|
|
|
|
*
|
|
|
|
* LyX, The Document Processor
|
|
|
|
*
|
|
|
|
* Copyright 1995 Matthias Ettrich
|
2001-05-30 13:53:44 +00:00
|
|
|
* Copyright 1995-2001 The LyX Team.
|
2000-07-24 13:53:19 +00:00
|
|
|
*
|
|
|
|
* ====================================================== */
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2000-07-24 13:53:19 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
#include <algorithm>
|
2000-01-06 11:35:29 +00:00
|
|
|
|
1999-10-07 18:44:17 +00:00
|
|
|
#include <cctype>
|
|
|
|
#include <cstdlib>
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
#include "LString.h"
|
|
|
|
#include "lstrings.h"
|
1999-11-22 16:19:48 +00:00
|
|
|
#include "LRegex.h"
|
2000-08-03 21:17:52 +00:00
|
|
|
#include "LAssert.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
|
1999-10-19 15:06:30 +00:00
|
|
|
using std::count;
|
1999-12-15 06:12:28 +00:00
|
|
|
using std::transform;
|
2001-04-25 19:33:52 +00:00
|
|
|
|
2000-11-10 12:33:01 +00:00
|
|
|
#ifndef CXX_GLOBAL_CSTD
|
2000-01-06 02:44:26 +00:00
|
|
|
using std::tolower;
|
|
|
|
using std::toupper;
|
2000-03-16 04:29:22 +00:00
|
|
|
#endif
|
2000-09-26 13:54:57 +00:00
|
|
|
|
|
|
|
|
1999-12-19 22:35:36 +00:00
|
|
|
int compare_no_case(string const & s, string const & s2)
|
|
|
|
{
|
|
|
|
string::const_iterator p = s.begin();
|
|
|
|
string::const_iterator p2 = s2.begin();
|
|
|
|
|
|
|
|
while (p != s.end() && p2 != s2.end()) {
|
|
|
|
int const lc1 = tolower(*p);
|
|
|
|
int const lc2 = tolower(*p2);
|
|
|
|
if (lc1 != lc2)
|
|
|
|
return (lc1 < lc2) ? -1 : 1;
|
|
|
|
++p;
|
|
|
|
++p2;
|
|
|
|
}
|
2000-12-13 13:30:36 +00:00
|
|
|
|
1999-12-19 22:35:36 +00:00
|
|
|
if (s.size() == s2.size())
|
|
|
|
return 0;
|
|
|
|
if (s.size() < s2.size())
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-01-06 02:44:26 +00:00
|
|
|
|
1999-12-19 22:35:36 +00:00
|
|
|
int compare_no_case(string const & s, string const & s2, unsigned int len)
|
|
|
|
{
|
|
|
|
string::const_iterator p = s.begin();
|
|
|
|
string::const_iterator p2 = s2.begin();
|
|
|
|
unsigned int i = 0;
|
|
|
|
while (i < len && p != s.end() && p2 != s2.end()) {
|
|
|
|
int const lc1 = tolower(*p);
|
|
|
|
int const lc2 = tolower(*p2);
|
|
|
|
if (lc1 != lc2)
|
|
|
|
return (lc1 < lc2) ? -1 : 1;
|
|
|
|
++i;
|
|
|
|
++p;
|
|
|
|
++p2;
|
|
|
|
}
|
2000-12-13 13:30:36 +00:00
|
|
|
|
|
|
|
if (s.size() >= len && s2.size() >= len)
|
1999-12-19 22:35:36 +00:00
|
|
|
return 0;
|
|
|
|
if (s.size() < s2.size())
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-01-06 02:44:26 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
bool isStrInt(string const & str)
|
|
|
|
{
|
|
|
|
if (str.empty()) return false;
|
|
|
|
|
|
|
|
// Remove leading and trailing white space chars.
|
2000-09-26 13:54:57 +00:00
|
|
|
string const tmpstr = frontStrip(strip(str, ' '), ' ');
|
1999-10-02 16:21:10 +00:00
|
|
|
if (tmpstr.empty()) return false;
|
|
|
|
|
|
|
|
string::const_iterator cit = tmpstr.begin();
|
2000-11-04 10:00:12 +00:00
|
|
|
if ((*cit) == '-') ++cit;
|
2000-08-03 21:17:52 +00:00
|
|
|
string::const_iterator end = tmpstr.end();
|
|
|
|
for (; cit != end; ++cit) {
|
1999-10-02 16:21:10 +00:00
|
|
|
if (!isdigit((*cit))) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-04 17:18:01 +00:00
|
|
|
bool isStrUnsignedInt(string const & str)
|
|
|
|
{
|
|
|
|
if (str.empty()) return false;
|
|
|
|
|
|
|
|
// Remove leading and trailing white space chars.
|
|
|
|
string const tmpstr = frontStrip(strip(str, ' '), ' ');
|
|
|
|
if (tmpstr.empty()) return false;
|
|
|
|
|
|
|
|
string::const_iterator cit = tmpstr.begin();
|
|
|
|
string::const_iterator end = tmpstr.end();
|
|
|
|
for (; cit != end; ++cit) {
|
|
|
|
if (!isdigit((*cit))) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
int strToInt(string const & str)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
if (isStrInt(str)) {
|
|
|
|
// Remove leading and trailing white space chars.
|
2000-12-04 17:18:01 +00:00
|
|
|
string const tmpstr = frontStrip(strip(str, ' '), ' ');
|
|
|
|
// Do the conversion proper.
|
|
|
|
return lyx::atoi(tmpstr);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int strToUnsignedInt(string const & str)
|
|
|
|
{
|
|
|
|
if (isStrUnsignedInt(str)) {
|
|
|
|
// Remove leading and trailing white space chars.
|
2000-09-26 13:54:57 +00:00
|
|
|
string const tmpstr = frontStrip(strip(str, ' '), ' ');
|
1999-10-02 16:21:10 +00:00
|
|
|
// Do the conversion proper.
|
2000-09-26 13:54:57 +00:00
|
|
|
return lyx::atoi(tmpstr);
|
2000-06-12 11:27:15 +00:00
|
|
|
} else {
|
1999-10-02 16:21:10 +00:00
|
|
|
return 0;
|
2000-06-12 11:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool isStrDbl(string const & str)
|
|
|
|
{
|
|
|
|
if (str.empty()) return false;
|
|
|
|
|
|
|
|
// Remove leading and trailing white space chars.
|
2000-09-26 13:54:57 +00:00
|
|
|
string const tmpstr = frontStrip(strip(str, ' '), ' ');
|
2000-06-12 11:27:15 +00:00
|
|
|
if (tmpstr.empty()) return false;
|
|
|
|
// if (1 < tmpstr.count('.')) return false;
|
|
|
|
|
|
|
|
string::const_iterator cit = tmpstr.begin();
|
|
|
|
bool found_dot(false);
|
2000-11-04 10:00:12 +00:00
|
|
|
if ((*cit) == '-') ++cit;
|
2000-08-03 21:17:52 +00:00
|
|
|
string::const_iterator end = tmpstr.end();
|
|
|
|
for (; cit != end; ++cit) {
|
2000-06-12 11:27:15 +00:00
|
|
|
if (!isdigit((*cit))
|
|
|
|
&& '.' != (*cit)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ('.' == (*cit)) {
|
|
|
|
if (found_dot) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
found_dot = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
double strToDbl(string const & str)
|
|
|
|
{
|
|
|
|
if (isStrDbl(str)) {
|
|
|
|
// Remove leading and trailing white space chars.
|
2000-09-26 13:54:57 +00:00
|
|
|
string const tmpstr = frontStrip(strip(str, ' '), ' ');
|
2000-06-12 11:27:15 +00:00
|
|
|
// Do the conversion proper.
|
2000-09-26 13:54:57 +00:00
|
|
|
return ::atof(tmpstr.c_str());
|
2000-06-12 11:27:15 +00:00
|
|
|
} else {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
|
2000-07-24 13:53:19 +00:00
|
|
|
char lowercase(char c)
|
|
|
|
{
|
2000-10-18 14:07:29 +00:00
|
|
|
return char( tolower(c) );
|
2000-07-24 13:53:19 +00:00
|
|
|
}
|
2000-06-12 11:27:15 +00:00
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
|
2000-07-24 13:53:19 +00:00
|
|
|
char uppercase(char c)
|
|
|
|
{
|
2000-10-18 14:07:29 +00:00
|
|
|
return char( toupper(c) );
|
2000-07-24 13:53:19 +00:00
|
|
|
}
|
1999-10-02 16:21:10 +00:00
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
|
2001-04-25 19:33:52 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// since we cannot use std::tolower and std::toupper directly in the
|
|
|
|
// calls to std::transform yet, we use these helper clases. (Lgb)
|
|
|
|
|
|
|
|
struct local_lowercase {
|
|
|
|
char operator()(char c) const {
|
|
|
|
return tolower(c);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct local_uppercase {
|
|
|
|
char operator()(char c) const {
|
|
|
|
return toupper(c);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end of anon namespace
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
string const lowercase(string const & a)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
1999-12-15 06:12:28 +00:00
|
|
|
string tmp(a);
|
2001-04-25 19:33:52 +00:00
|
|
|
transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
|
1999-12-15 06:12:28 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
string const uppercase(string const & a)
|
1999-12-15 06:12:28 +00:00
|
|
|
{
|
|
|
|
string tmp(a);
|
2001-04-25 19:33:52 +00:00
|
|
|
transform(tmp.begin(), tmp.end(), tmp.begin(), local_uppercase());
|
1999-12-15 06:12:28 +00:00
|
|
|
return tmp;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool prefixIs(string const & a, char const * pre)
|
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(pre);
|
2000-08-03 21:17:52 +00:00
|
|
|
|
2001-05-31 02:23:46 +00:00
|
|
|
size_t const l = std::strlen(pre);
|
2000-09-26 13:54:57 +00:00
|
|
|
string::size_type const alen = a.length();
|
|
|
|
|
|
|
|
if (l > alen || a.empty())
|
1999-10-02 16:21:10 +00:00
|
|
|
return false;
|
2000-07-24 21:49:58 +00:00
|
|
|
else {
|
|
|
|
#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
|
|
|
|
// Delete this code when the compilers get a bit better.
|
|
|
|
return ::strncmp(a.c_str(), pre, l) == 0;
|
|
|
|
#else
|
|
|
|
// This is the code that we really want to use
|
|
|
|
// but until gcc ships with a basic_string that
|
|
|
|
// implements std::string correctly we have to
|
|
|
|
// use the code above.
|
1999-10-02 16:21:10 +00:00
|
|
|
return a.compare(0, l, pre, l) == 0;
|
2000-07-24 21:49:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
bool prefixIs(string const & a, string const & pre)
|
|
|
|
{
|
|
|
|
string::size_type const prelen = pre.length();
|
|
|
|
string::size_type const alen = a.length();
|
|
|
|
|
2000-12-10 05:20:36 +00:00
|
|
|
if (prelen > alen || a.empty())
|
2000-09-26 13:54:57 +00:00
|
|
|
return false;
|
|
|
|
else {
|
|
|
|
#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
|
|
|
|
return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
|
|
|
|
#else
|
|
|
|
return a.compare(0, prelen, pre) == 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
bool suffixIs(string const & a, char c)
|
|
|
|
{
|
|
|
|
if (a.empty()) return false;
|
1999-11-22 16:19:48 +00:00
|
|
|
return a[a.length() - 1] == c;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool suffixIs(string const & a, char const * suf)
|
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(suf);
|
2000-08-03 21:17:52 +00:00
|
|
|
|
2001-05-31 02:23:46 +00:00
|
|
|
size_t const suflen = std::strlen(suf);
|
2000-12-06 22:24:17 +00:00
|
|
|
string::size_type const alen = a.length();
|
|
|
|
|
|
|
|
if (suflen > alen)
|
1999-10-02 16:21:10 +00:00
|
|
|
return false;
|
|
|
|
else {
|
2000-07-24 21:49:58 +00:00
|
|
|
#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
|
|
|
|
// Delete this code when the compilers get a bit better.
|
2000-12-06 22:24:17 +00:00
|
|
|
string tmp(a, alen - suflen);
|
2000-07-24 21:49:58 +00:00
|
|
|
return ::strncmp(tmp.c_str(), suf, suflen) == 0;
|
|
|
|
#else
|
|
|
|
// This is the code that we really want to use
|
|
|
|
// but until gcc ships with a basic_string that
|
|
|
|
// implements std::string correctly we have to
|
|
|
|
// use the code above.
|
2000-12-06 22:24:17 +00:00
|
|
|
return a.compare(alen - suflen, suflen, suf) == 0;
|
2000-07-24 21:49:58 +00:00
|
|
|
#endif
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
bool suffixIs(string const & a, string const & suf)
|
|
|
|
{
|
|
|
|
string::size_type const suflen = suf.length();
|
|
|
|
string::size_type const alen = a.length();
|
|
|
|
|
|
|
|
if (suflen > alen) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
|
|
|
|
string tmp(a, alen - suflen);
|
|
|
|
return ::strncmp(tmp.c_str(), suf.c_str(), suflen) == 0;
|
|
|
|
#else
|
2000-10-02 00:55:02 +00:00
|
|
|
return a.compare(alen - suflen, suflen, suf) == 0;
|
2000-09-26 13:54:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
bool contains(char const * a, string const & b)
|
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(a);
|
2000-09-26 13:54:57 +00:00
|
|
|
string const at(a);
|
|
|
|
return contains(at, b);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool contains(string const & a, char const * b)
|
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(b);
|
2000-09-26 13:54:57 +00:00
|
|
|
string const bt(b);
|
|
|
|
return contains(a, bt);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool contains(string const & a, string const & b)
|
|
|
|
{
|
|
|
|
if (a.empty())
|
|
|
|
return false;
|
|
|
|
return a.find(b) != string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-15 14:05:45 +00:00
|
|
|
bool contains(string const & a, char b)
|
|
|
|
{
|
|
|
|
if (a.empty())
|
|
|
|
return false;
|
|
|
|
return a.find(b) != string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
bool contains(char const * a, char const * b)
|
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(a && b);
|
2000-09-26 13:54:57 +00:00
|
|
|
string const at(a);
|
|
|
|
string const bt(b);
|
|
|
|
return contains(at, bt);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
bool containsOnly(string const & s, char const * cset)
|
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(cset);
|
2000-08-03 21:17:52 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
return s.find_first_not_of(cset) == string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool containsOnly(string const & s, string const & cset)
|
|
|
|
{
|
|
|
|
return s.find_first_not_of(cset) == string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool containsOnly(char const * s, char const * cset)
|
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(s && cset);
|
2000-08-03 21:17:52 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
return string(s).find_first_not_of(cset) == string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool containsOnly(char const * s, string const & cset)
|
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(s);
|
2000-08-03 21:17:52 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
return string(s).find_first_not_of(cset) == string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-18 14:07:29 +00:00
|
|
|
string::size_type countChar(string const & a, char c)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
1999-12-21 06:10:21 +00:00
|
|
|
#ifdef HAVE_STD_COUNT
|
1999-11-22 16:19:48 +00:00
|
|
|
return count(a.begin(), a.end(), c);
|
1999-12-21 06:10:21 +00:00
|
|
|
#else
|
|
|
|
unsigned int n = 0;
|
|
|
|
count(a.begin(), a.end(), c, n);
|
|
|
|
return n;
|
|
|
|
#endif
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ale970405+lasgoutt-970425
|
|
|
|
// rewritten to use new string (Lgb)
|
2000-08-03 21:17:52 +00:00
|
|
|
string const token(string const & a, char delim, int n)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
if (a.empty()) return string();
|
|
|
|
|
|
|
|
string::size_type k = 0;
|
|
|
|
string::size_type i = 0;
|
|
|
|
|
|
|
|
// Find delimiter or end of string
|
|
|
|
for (; n--;)
|
|
|
|
if ((i = a.find(delim, i)) == string::npos)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
++i; // step delim
|
|
|
|
// i is now the n'th delim (or string::npos)
|
|
|
|
if (i == string::npos) return string();
|
|
|
|
k = a.find(delim, i);
|
|
|
|
// k is now the n'th + 1 delim (or string::npos)
|
|
|
|
|
|
|
|
return a.substr(i, k - i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// this could probably be faster and/or cleaner, but it seems to work (JMarc)
|
|
|
|
// rewritten to use new string (Lgb)
|
|
|
|
int tokenPos(string const & a, char delim, string const & tok)
|
|
|
|
{
|
|
|
|
int i = 0;
|
2000-08-03 21:17:52 +00:00
|
|
|
string str(a);
|
1999-10-02 16:21:10 +00:00
|
|
|
string tmptok;
|
|
|
|
|
|
|
|
while (!str.empty()) {
|
|
|
|
str = split(str, tmptok, delim);
|
1999-11-15 10:54:16 +00:00
|
|
|
if (tok == tmptok)
|
1999-10-02 16:21:10 +00:00
|
|
|
return i;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool regexMatch(string const & a, string const & pattern)
|
|
|
|
{
|
1999-11-22 16:19:48 +00:00
|
|
|
// We massage the pattern a bit so that the usual
|
|
|
|
// shell pattern we all are used to will work.
|
|
|
|
// One nice thing about using a real regex is that
|
|
|
|
// things like "*.*[^~]" will work also.
|
|
|
|
// build the regex string.
|
|
|
|
string regex(pattern);
|
|
|
|
regex = subst(regex, ".", "\\.");
|
|
|
|
regex = subst(regex, "*", ".*");
|
|
|
|
LRegex reg(regex);
|
|
|
|
return reg.exact_match(a);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
string const subst(string const & a, char oldchar, char newchar)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2000-08-03 21:17:52 +00:00
|
|
|
string tmp(a);
|
1999-10-02 16:21:10 +00:00
|
|
|
string::iterator lit = tmp.begin();
|
2000-08-03 21:17:52 +00:00
|
|
|
string::iterator end = tmp.end();
|
2000-11-04 10:00:12 +00:00
|
|
|
for (; lit != end; ++lit)
|
1999-10-02 16:21:10 +00:00
|
|
|
if ((*lit) == oldchar)
|
|
|
|
(*lit) = newchar;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
string const subst(string const & a,
|
2001-04-25 19:33:52 +00:00
|
|
|
char const * oldstr, string const & newstr)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(oldstr);
|
2000-08-03 21:17:52 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string lstr(a);
|
|
|
|
string::size_type i = 0;
|
2001-05-31 02:23:46 +00:00
|
|
|
string::size_type olen = std::strlen(oldstr);
|
1999-10-02 16:21:10 +00:00
|
|
|
while((i = lstr.find(oldstr, i)) != string::npos) {
|
|
|
|
lstr.replace(i, olen, newstr);
|
|
|
|
i += newstr.length(); // We need to be sure that we dont
|
|
|
|
// use the same i over and over again.
|
|
|
|
}
|
|
|
|
return lstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
string const subst(string const & a,
|
|
|
|
string const & oldstr, string const & newstr)
|
|
|
|
{
|
|
|
|
string lstr(a);
|
|
|
|
string::size_type i = 0;
|
|
|
|
string::size_type const olen = oldstr.length();
|
|
|
|
while((i = lstr.find(oldstr, i)) != string::npos) {
|
|
|
|
lstr.replace(i, olen, newstr);
|
|
|
|
i += newstr.length(); // We need to be sure that we dont
|
|
|
|
// use the same i over and over again.
|
|
|
|
}
|
|
|
|
return lstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
string const strip(string const & a, char c)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
if (a.empty()) return a;
|
2000-08-03 21:17:52 +00:00
|
|
|
string tmp(a);
|
1999-10-02 16:21:10 +00:00
|
|
|
string::size_type i = tmp.find_last_not_of(c);
|
|
|
|
if (i == a.length() - 1) return tmp; // no c's at end of a
|
|
|
|
if (i != string::npos)
|
|
|
|
tmp.erase(i + 1, string::npos);
|
2001-01-23 12:53:05 +00:00
|
|
|
#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
|
|
|
|
/// Needed for broken string::find_last_not_of
|
|
|
|
else if (tmp[0] != c) {
|
|
|
|
if (a.length() == 1) return tmp;
|
|
|
|
tmp.erase(1, string::npos);
|
|
|
|
}
|
|
|
|
#endif
|
1999-10-02 16:21:10 +00:00
|
|
|
else
|
2000-05-04 10:57:00 +00:00
|
|
|
tmp.erase(); // only c in the whole string
|
1999-10-02 16:21:10 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
string const frontStrip(string const & a, char const * p)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(p);
|
2000-08-03 21:17:52 +00:00
|
|
|
|
|
|
|
if (a.empty() || !*p) return a;
|
|
|
|
string tmp(a);
|
1999-10-02 16:21:10 +00:00
|
|
|
string::size_type i = tmp.find_first_not_of(p);
|
|
|
|
if (i > 0)
|
|
|
|
tmp.erase(0, i);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
string const frontStrip(string const & a, char c)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
if (a.empty()) return a;
|
2000-08-03 21:17:52 +00:00
|
|
|
string tmp(a);
|
1999-10-02 16:21:10 +00:00
|
|
|
string::size_type i = tmp.find_first_not_of(c);
|
|
|
|
if (i > 0)
|
|
|
|
tmp.erase(0, i);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
string const split(string const & a, string & piece, char delim)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
string tmp;
|
|
|
|
string::size_type i = a.find(delim);
|
|
|
|
if (i == a.length() - 1) {
|
|
|
|
piece = a.substr(0, i);
|
|
|
|
} else if (i != string::npos) {
|
|
|
|
piece = a.substr(0, i);
|
|
|
|
tmp = a.substr(i + 1);
|
|
|
|
} else if (i == 0) {
|
2000-05-04 10:57:00 +00:00
|
|
|
piece.erase();
|
1999-10-02 16:21:10 +00:00
|
|
|
tmp = a.substr(i + 1);
|
|
|
|
} else {
|
|
|
|
piece = a;
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
string const split(string const & a, char delim)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
string tmp;
|
|
|
|
string::size_type i = a.find(delim);
|
|
|
|
if (i != string::npos) // found delim
|
|
|
|
tmp = a.substr(i + 1);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ale970521
|
2000-08-03 21:17:52 +00:00
|
|
|
string const rsplit(string const & a, string & piece, char delim)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
string tmp;
|
|
|
|
string::size_type i = a.rfind(delim);
|
|
|
|
if (i != string::npos) { // delimiter was found
|
|
|
|
piece = a.substr(0, i);
|
|
|
|
tmp = a.substr(i + 1);
|
|
|
|
} else { // delimter was not found
|
2000-05-04 10:57:00 +00:00
|
|
|
piece.erase();
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|