2000-07-24 13:53:19 +00:00
|
|
|
/* This file is part of
|
2002-03-21 17:09:55 +00:00
|
|
|
* ======================================================
|
|
|
|
*
|
2000-07-24 13:53:19 +00:00
|
|
|
* LyX, The Document Processor
|
2002-03-21 17:09:55 +00:00
|
|
|
*
|
2000-07-24 13:53:19 +00:00
|
|
|
* 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 "LString.h"
|
|
|
|
#include "lstrings.h"
|
2000-08-03 21:17:52 +00:00
|
|
|
#include "LAssert.h"
|
2002-03-01 17:42:43 +00:00
|
|
|
#include "debug.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
|
2002-05-25 00:19:56 +00:00
|
|
|
#include <boost/regex.hpp>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <cctype>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
1999-10-19 15:06:30 +00:00
|
|
|
using std::count;
|
1999-12-15 06:12:28 +00:00
|
|
|
using std::transform;
|
2002-04-15 12:05:07 +00:00
|
|
|
using std::vector;
|
2001-04-25 19:33:52 +00:00
|
|
|
|
2000-11-10 12:33:01 +00:00
|
|
|
#ifndef CXX_GLOBAL_CSTD
|
2002-06-10 07:57:39 +00:00
|
|
|
using std::atof;
|
|
|
|
using std::isdigit;
|
|
|
|
using std::strlen;
|
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())
|
2001-06-25 00:06:33 +00:00
|
|
|
return 0;
|
|
|
|
if (s.size() < s2.size())
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-06-27 14:10:35 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
int ascii_tolower(int c) {
|
|
|
|
if (c >= 'A' && c <= 'Z')
|
|
|
|
return c - 'A' + 'a';
|
|
|
|
return c;
|
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
2001-06-27 14:10:35 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
int compare_ascii_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 = ascii_tolower(*p);
|
|
|
|
int const lc2 = ascii_tolower(*p2);
|
|
|
|
if (lc1 != lc2)
|
|
|
|
return (lc1 < lc2) ? -1 : 1;
|
|
|
|
++p;
|
|
|
|
++p2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.size() == s2.size())
|
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-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;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
// Remove leading and trailing white space chars.
|
2002-07-28 18:13:51 +00:00
|
|
|
string const tmpstr = frontStrip(strip(str));
|
1999-10-02 16:21:10 +00:00
|
|
|
if (tmpstr.empty()) return false;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
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;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-12-04 17:18:01 +00:00
|
|
|
// Remove leading and trailing white space chars.
|
2002-07-28 18:13:51 +00:00
|
|
|
string const tmpstr = frontStrip(strip(str));
|
2000-12-04 17:18:01 +00:00
|
|
|
if (tmpstr.empty()) return false;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-12-04 17:18:01 +00:00
|
|
|
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.
|
2002-07-28 18:13:51 +00:00
|
|
|
string const tmpstr = frontStrip(strip(str));
|
2000-12-04 17:18:01 +00:00
|
|
|
// 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.
|
2002-07-28 18:13:51 +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;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
// Remove leading and trailing white space chars.
|
2002-07-28 18:13:51 +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.
|
2002-07-28 18:13:51 +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
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
char lowercase(char c)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
char uppercase(char c)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2001-04-25 19:33:52 +00:00
|
|
|
struct local_uppercase {
|
|
|
|
char operator()(char c) const {
|
|
|
|
return toupper(c);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-07-16 21:17:10 +00:00
|
|
|
struct local_ascii_lowercase {
|
|
|
|
char operator()(char c) const {
|
|
|
|
return ascii_tolower(c);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2001-04-25 19:33:52 +00:00
|
|
|
} // 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-16 21:17:10 +00:00
|
|
|
string const ascii_lowercase(string const & a)
|
|
|
|
{
|
|
|
|
string tmp(a);
|
|
|
|
transform(tmp.begin(), tmp.end(), tmp.begin(),
|
|
|
|
local_ascii_lowercase());
|
|
|
|
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);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2001-06-01 10:53:24 +00:00
|
|
|
size_t const l = strlen(pre);
|
2000-09-26 13:54:57 +00:00
|
|
|
string::size_type const alen = a.length();
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
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();
|
2002-03-21 17:09:55 +00:00
|
|
|
|
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);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2001-06-01 10:53:24 +00:00
|
|
|
size_t const suflen = strlen(suf);
|
2000-12-06 22:24:17 +00:00
|
|
|
string::size_type const alen = a.length();
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-12-06 22:24:17 +00:00
|
|
|
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();
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
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(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
bool containsOnly(string const & s, string const & cset)
|
|
|
|
{
|
|
|
|
return s.find_first_not_of(cset) == string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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();
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
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, "*", ".*");
|
2002-05-25 00:19:56 +00:00
|
|
|
boost::regex reg(regex);
|
|
|
|
return boost::regex_match(a, reg);
|
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);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string lstr(a);
|
|
|
|
string::size_type i = 0;
|
2001-06-01 10:53:24 +00:00
|
|
|
string::size_type olen = strlen(oldstr);
|
2001-12-05 08:04:20 +00:00
|
|
|
while ((i = lstr.find(oldstr, i)) != string::npos) {
|
1999-10-02 16:21:10 +00:00
|
|
|
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();
|
2001-12-05 08:04:20 +00:00
|
|
|
while ((i = lstr.find(oldstr, i)) != string::npos) {
|
2000-09-26 13:54:57 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-28 18:13:51 +00:00
|
|
|
string const strip(string const & a, char const * p)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2002-07-28 18:13:51 +00:00
|
|
|
lyx::Assert(p);
|
|
|
|
|
|
|
|
if (a.empty() || !*p) return a;
|
2000-08-03 21:17:52 +00:00
|
|
|
string tmp(a);
|
2002-07-28 18:13:51 +00:00
|
|
|
string::size_type i = tmp.find_last_not_of(p);
|
1999-10-02 16:21:10 +00:00
|
|
|
if (i == a.length() - 1) return tmp; // no c's at end of a
|
2002-03-21 17:09:55 +00:00
|
|
|
if (i != string::npos)
|
1999-10-02 16:21:10 +00:00
|
|
|
tmp.erase(i + 1, string::npos);
|
2001-01-23 12:53:05 +00:00
|
|
|
#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
|
2002-07-28 18:13:51 +00:00
|
|
|
// Ok This code is now suspect... (Lgb)
|
2001-01-23 12:53:05 +00:00
|
|
|
/// Needed for broken string::find_last_not_of
|
2002-07-28 18:13:51 +00:00
|
|
|
else if (tmp[0] != p[0]) {
|
2001-01-23 12:53:05 +00:00
|
|
|
if (a.length() == 1) return tmp;
|
|
|
|
tmp.erase(1, string::npos);
|
|
|
|
}
|
|
|
|
#endif
|
1999-10-02 16:21:10 +00:00
|
|
|
else
|
2002-07-28 18:13:51 +00:00
|
|
|
tmp.erase(); // only chars from p 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);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
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 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;
|
|
|
|
}
|
2001-06-27 14:10:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
// This function escapes 8-bit characters and other problematic
|
|
|
|
// characters that cause problems in latex labels.
|
|
|
|
string const escape(string const & lab)
|
|
|
|
{
|
|
|
|
char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
|
|
|
string enc;
|
|
|
|
for (string::size_type i = 0; i < lab.length(); ++i) {
|
|
|
|
unsigned char c= lab[i];
|
|
|
|
if (c >= 128 || c == '=' || c == '%') {
|
|
|
|
enc += '=';
|
|
|
|
enc += hexdigit[c>>4];
|
|
|
|
enc += hexdigit[c & 15];
|
|
|
|
} else {
|
|
|
|
enc += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return enc;
|
|
|
|
}
|
2002-04-15 12:05:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// gives a vector of stringparts which have the delimiter delim
|
|
|
|
vector<string> const getVectorFromString(string const & str,
|
|
|
|
string const & delim)
|
|
|
|
{
|
|
|
|
vector<string> vec;
|
|
|
|
if (str.empty())
|
|
|
|
return vec;
|
|
|
|
string keys(strip(str));
|
|
|
|
for(;;) {
|
|
|
|
string::size_type const idx = keys.find(delim);
|
|
|
|
if (idx == string::npos) {
|
|
|
|
vec.push_back(frontStrip(keys));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
string const key = strip(frontStrip(keys.substr(0, idx)));
|
|
|
|
if (!key.empty())
|
|
|
|
vec.push_back(key);
|
|
|
|
string::size_type const start = idx + delim.size();
|
|
|
|
keys = keys.substr(start);
|
|
|
|
}
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the same vice versa
|
|
|
|
string const getStringFromVector(vector<string> const & vec,
|
|
|
|
string const & delim)
|
|
|
|
{
|
|
|
|
string str;
|
|
|
|
int i = 0;
|
|
|
|
for (vector<string>::const_iterator it = vec.begin();
|
|
|
|
it != vec.end(); ++it) {
|
|
|
|
string item = strip(frontStrip(*it));
|
|
|
|
if (item.empty()) continue;
|
|
|
|
|
|
|
|
if (i++ > 0) str += delim;
|
|
|
|
str += item;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|