2002-09-25 10:03:41 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file lstrings.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:09:55 +00:00
|
|
|
|
*
|
2002-09-25 10:03:41 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author Jean-Marc Lasgouttes
|
2002-03-21 17:09:55 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-09-25 10:03:41 +00:00
|
|
|
|
*/
|
2000-07-24 13:53:19 +00:00
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "lstrings.h"
|
2002-03-01 17:42:43 +00:00
|
|
|
|
#include "debug.h"
|
2003-06-30 23:56:22 +00:00
|
|
|
|
#include "lyxlib.h"
|
2003-09-05 19:20:26 +00:00
|
|
|
|
#include "tostr.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
2002-11-04 00:15:56 +00:00
|
|
|
|
#include <boost/tokenizer.hpp>
|
2003-10-06 09:35:38 +00:00
|
|
|
|
#include <boost/assert.hpp>
|
2004-01-06 19:32:05 +00:00
|
|
|
|
#include <boost/format.hpp>
|
2002-05-25 00:19:56 +00:00
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
#include <cctype>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
2004-07-24 10:55:30 +00:00
|
|
|
|
#include <sstream>
|
|
|
|
|
|
1999-12-15 06:12:28 +00:00
|
|
|
|
using std::transform;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
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;
|
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
|
|
|
|
|
2003-09-08 00:33:41 +00:00
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
namespace support {
|
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 22:50:13 +00:00
|
|
|
|
string const tmpstr = trim(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 22:50:13 +00:00
|
|
|
|
string const tmpstr = trim(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 22:50:13 +00:00
|
|
|
|
string const tmpstr = trim(str);
|
2000-12-04 17:18:01 +00:00
|
|
|
|
// Do the conversion proper.
|
2003-06-30 23:56:22 +00:00
|
|
|
|
return atoi(tmpstr);
|
2000-12-04 17:18:01 +00:00
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int strToUnsignedInt(string const & str)
|
|
|
|
|
{
|
|
|
|
|
if (isStrUnsignedInt(str)) {
|
|
|
|
|
// Remove leading and trailing white space chars.
|
2002-07-28 22:50:13 +00:00
|
|
|
|
string const tmpstr = trim(str);
|
1999-10-02 16:21:10 +00:00
|
|
|
|
// Do the conversion proper.
|
2003-06-30 23:56:22 +00:00
|
|
|
|
return 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 22:50:13 +00:00
|
|
|
|
string const tmpstr = trim(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 22:50:13 +00:00
|
|
|
|
string const tmpstr = trim(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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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 {
|
2004-01-31 15:30:24 +00:00
|
|
|
|
#if defined(STD_STRING_IS_GOOD)
|
2000-09-26 13:54:57 +00:00
|
|
|
|
return a.compare(0, prelen, pre) == 0;
|
2004-01-31 15:30:24 +00:00
|
|
|
|
#else
|
|
|
|
|
return ::strncmp(a.c_str(), pre.c_str(), prelen) == 0;
|
2000-09-26 13:54:57 +00:00
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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-09-26 13:54:57 +00:00
|
|
|
|
string const subst(string const & a,
|
|
|
|
|
string const & oldstr, string const & newstr)
|
|
|
|
|
{
|
2003-05-13 16:24:49 +00:00
|
|
|
|
string lstr = a;
|
2000-09-26 13:54:57 +00:00
|
|
|
|
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 22:50:13 +00:00
|
|
|
|
string const trim(string const & a, char const * p)
|
1999-10-02 16:21:10 +00:00
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(p);
|
2002-07-28 18:13:51 +00:00
|
|
|
|
|
2002-07-28 22:50:13 +00:00
|
|
|
|
if (a.empty() || !*p)
|
|
|
|
|
return a;
|
|
|
|
|
|
|
|
|
|
string::size_type r = a.find_last_not_of(p);
|
|
|
|
|
string::size_type l = a.find_first_not_of(p);
|
|
|
|
|
|
|
|
|
|
// Is this the minimal test? (lgb)
|
|
|
|
|
if (r == string::npos && l == string::npos)
|
|
|
|
|
return string();
|
|
|
|
|
|
|
|
|
|
return a.substr(l, r - l + 1);
|
1999-10-02 16:21:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-07-28 22:50:13 +00:00
|
|
|
|
string const rtrim(string const & a, char const * p)
|
1999-10-02 16:21:10 +00:00
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(p);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
2002-07-28 22:50:13 +00:00
|
|
|
|
if (a.empty() || !*p)
|
|
|
|
|
return a;
|
|
|
|
|
|
|
|
|
|
string::size_type r = a.find_last_not_of(p);
|
|
|
|
|
|
|
|
|
|
// Is this test really needed? (Lgb)
|
|
|
|
|
if (r == string::npos)
|
|
|
|
|
return string();
|
|
|
|
|
|
|
|
|
|
return a.substr(0, r + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string const ltrim(string const & a, char const * p)
|
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(p);
|
2002-07-28 22:50:13 +00:00
|
|
|
|
|
|
|
|
|
if (a.empty() || !*p)
|
|
|
|
|
return a;
|
|
|
|
|
|
|
|
|
|
string::size_type l = a.find_first_not_of(p);
|
|
|
|
|
|
|
|
|
|
if (l == string::npos)
|
|
|
|
|
return string();
|
|
|
|
|
|
|
|
|
|
return a.substr(l, string::npos);
|
1999-10-02 16:21:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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);
|
2003-05-13 16:24:49 +00:00
|
|
|
|
} else { // delimiter 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)
|
|
|
|
|
{
|
2002-11-12 13:18:07 +00:00
|
|
|
|
// Lars would like this code to go, but for now his replacement (below)
|
|
|
|
|
// doesn't fullfil the same function. I have, therefore, reactivated the
|
|
|
|
|
// old code for now. Angus 11 Nov 2002.
|
|
|
|
|
#if 1
|
2002-11-04 00:15:56 +00:00
|
|
|
|
vector<string> vec;
|
|
|
|
|
if (str.empty())
|
|
|
|
|
return vec;
|
|
|
|
|
string keys(rtrim(str));
|
|
|
|
|
for(;;) {
|
|
|
|
|
string::size_type const idx = keys.find(delim);
|
|
|
|
|
if (idx == string::npos) {
|
|
|
|
|
vec.push_back(ltrim(keys));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
string const key = trim(keys.substr(0, idx));
|
|
|
|
|
if (!key.empty())
|
|
|
|
|
vec.push_back(key);
|
|
|
|
|
string::size_type const start = idx + delim.size();
|
|
|
|
|
keys = keys.substr(start);
|
2002-04-15 12:05:07 +00:00
|
|
|
|
}
|
2002-11-04 00:15:56 +00:00
|
|
|
|
return vec;
|
|
|
|
|
#else
|
|
|
|
|
boost::char_separator<char> sep(delim.c_str());
|
|
|
|
|
boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
|
2002-11-04 02:12:42 +00:00
|
|
|
|
#ifndef USE_INCLUDED_STRING
|
2002-11-04 00:15:56 +00:00
|
|
|
|
return vector<string>(tokens.begin(), tokens.end());
|
2002-11-04 02:12:42 +00:00
|
|
|
|
#else
|
|
|
|
|
vector<string> vec;
|
|
|
|
|
using boost::tokenizer;
|
|
|
|
|
using boost::char_separator;
|
|
|
|
|
|
|
|
|
|
tokenizer<char_separator<char> >::iterator it = tokens.begin();
|
|
|
|
|
tokenizer<char_separator<char> >::iterator end = tokens.end();
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
|
vec.push_back(STRCONV((*it)));
|
|
|
|
|
}
|
|
|
|
|
return vec;
|
|
|
|
|
#endif
|
2002-11-04 00:15:56 +00:00
|
|
|
|
#endif
|
2002-04-15 12:05:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-04 00:15:56 +00:00
|
|
|
|
|
2002-04-15 12:05:07 +00:00
|
|
|
|
// 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) {
|
2002-07-28 22:50:13 +00:00
|
|
|
|
string item = trim(*it);
|
2003-05-19 14:10:32 +00:00
|
|
|
|
if (item.empty())
|
|
|
|
|
continue;
|
|
|
|
|
if (i++ > 0)
|
|
|
|
|
str += delim;
|
2002-04-15 12:05:07 +00:00
|
|
|
|
str += item;
|
|
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
2003-05-13 09:48:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if USE_BOOST_FORMAT
|
|
|
|
|
|
2003-05-16 14:59:48 +00:00
|
|
|
|
string bformat(string const & fmt, string const & arg1)
|
2003-05-13 09:48:57 +00:00
|
|
|
|
{
|
2003-09-15 11:00:00 +00:00
|
|
|
|
return (boost::format(fmt) % arg1).str();
|
2003-05-13 09:48:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-16 14:59:48 +00:00
|
|
|
|
string bformat(string const & fmt, string const & arg1, string const & arg2)
|
2003-05-13 09:48:57 +00:00
|
|
|
|
{
|
2003-09-15 11:00:00 +00:00
|
|
|
|
return (boost::format(fmt) % arg1 % arg2).str();
|
2003-05-13 09:48:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-19 14:10:32 +00:00
|
|
|
|
|
2003-09-04 14:33:16 +00:00
|
|
|
|
string bformat(string const & fmt, int arg1, int arg2)
|
|
|
|
|
{
|
2003-09-15 11:00:00 +00:00
|
|
|
|
return (boost::format(fmt) % arg1 % arg2).str();
|
2003-09-04 14:33:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-19 14:10:32 +00:00
|
|
|
|
string bformat(string const & fmt, string const & arg1, string const & arg2,
|
|
|
|
|
string const & arg3)
|
|
|
|
|
{
|
2003-09-15 11:00:00 +00:00
|
|
|
|
return (boost::format(fmt) % arg1 % arg2 % arg3).str();
|
2003-05-19 14:10:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-16 14:59:48 +00:00
|
|
|
|
string bformat(string const & fmt, string const & arg1, string const & arg2,
|
2003-05-13 09:48:57 +00:00
|
|
|
|
string const & arg3, string const & arg4)
|
|
|
|
|
{
|
2003-09-15 11:00:00 +00:00
|
|
|
|
return (boost::format(fmt) % arg1 % arg2 % arg3 % arg4).str();
|
2003-06-30 15:06:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string bformat(string const & fmt, string const & arg1, string const & arg2,
|
|
|
|
|
string const & arg3, string const & arg4, string const & arg5)
|
|
|
|
|
{
|
2003-09-15 11:00:00 +00:00
|
|
|
|
return (boost::format(fmt) % arg1 % arg2 % arg3 % arg4 % arg5).str();
|
2003-05-13 09:48:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-05-22 10:47:31 +00:00
|
|
|
|
#else
|
2003-05-13 09:48:57 +00:00
|
|
|
|
|
2003-05-16 14:59:48 +00:00
|
|
|
|
string bformat(string const & fmt, string const & arg1)
|
2003-05-13 09:48:57 +00:00
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(contains(fmt, "%1$s"));
|
2003-05-13 09:48:57 +00:00
|
|
|
|
string const str = subst(fmt, "%1$s", arg1);
|
|
|
|
|
return subst(str, "%%", "%");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-16 14:59:48 +00:00
|
|
|
|
string bformat(string const & fmt, string const & arg1, string const & arg2)
|
2003-05-13 09:48:57 +00:00
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(contains(fmt, "%1$s"));
|
|
|
|
|
BOOST_ASSERT(contains(fmt, "%2$s"));
|
2003-05-13 09:48:57 +00:00
|
|
|
|
string str = subst(fmt, "%1$s", arg1);
|
|
|
|
|
str = subst(str, "%2$s", arg2);
|
|
|
|
|
return subst(str, "%%", "%");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-09-04 14:33:16 +00:00
|
|
|
|
string bformat(string const & fmt, int arg1, int arg2)
|
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(contains(fmt, "%1$d"));
|
|
|
|
|
BOOST_ASSERT(contains(fmt, "%2$d"));
|
2003-09-05 19:20:26 +00:00
|
|
|
|
string str = subst(fmt, "%1$d", tostr(arg1));
|
|
|
|
|
str = subst(str, "%2$d", tostr(arg2));
|
2003-09-04 14:33:16 +00:00
|
|
|
|
return subst(str, "%%", "%");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-19 14:13:50 +00:00
|
|
|
|
string bformat(string const & fmt, string const & arg1, string const & arg2,
|
2003-05-19 14:10:32 +00:00
|
|
|
|
string const & arg3)
|
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(contains(fmt, "%1$s"));
|
|
|
|
|
BOOST_ASSERT(contains(fmt, "%2$s"));
|
|
|
|
|
BOOST_ASSERT(contains(fmt, "%3$s"));
|
2003-05-19 14:10:32 +00:00
|
|
|
|
string str = subst(fmt, "%1$s", arg1);
|
|
|
|
|
str = subst(str, "%2$s", arg2);
|
|
|
|
|
str = subst(str, "%3$s", arg3);
|
|
|
|
|
return subst(str, "%%", "%");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-19 14:13:50 +00:00
|
|
|
|
string bformat(string const & fmt, string const & arg1, string const & arg2,
|
2003-05-13 09:48:57 +00:00
|
|
|
|
string const & arg3, string const & arg4)
|
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(contains(fmt, "%1$s"));
|
|
|
|
|
BOOST_ASSERT(contains(fmt, "%2$s"));
|
|
|
|
|
BOOST_ASSERT(contains(fmt, "%3$s"));
|
|
|
|
|
BOOST_ASSERT(contains(fmt, "%4$s"));
|
2003-05-13 09:48:57 +00:00
|
|
|
|
string str = subst(fmt, "%1$s", arg1);
|
|
|
|
|
str = subst(str, "%2$s", arg2);
|
|
|
|
|
str = subst(str, "%3$s", arg3);
|
|
|
|
|
str = subst(str, "%4$s", arg4);
|
|
|
|
|
return subst(str, "%%", "%");
|
|
|
|
|
}
|
|
|
|
|
|
2003-06-30 15:06:30 +00:00
|
|
|
|
|
|
|
|
|
string bformat(string const & fmt, string const & arg1, string const & arg2,
|
|
|
|
|
string const & arg3, string const & arg4, string const & arg5)
|
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(contains(fmt, "%1$s"));
|
|
|
|
|
BOOST_ASSERT(contains(fmt, "%2$s"));
|
|
|
|
|
BOOST_ASSERT(contains(fmt, "%3$s"));
|
|
|
|
|
BOOST_ASSERT(contains(fmt, "%4$s"));
|
|
|
|
|
BOOST_ASSERT(contains(fmt, "%5$s"));
|
2003-06-30 15:06:30 +00:00
|
|
|
|
string str = subst(fmt, "%1$s", arg1);
|
|
|
|
|
str = subst(str, "%2$s", arg2);
|
|
|
|
|
str = subst(str, "%3$s", arg3);
|
|
|
|
|
str = subst(str, "%4$s", arg4);
|
|
|
|
|
str = subst(str, "%5$s", arg5);
|
|
|
|
|
return subst(str, "%%", "%");
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-13 09:48:57 +00:00
|
|
|
|
#endif
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
|
|
|
|
} // namespace support
|
|
|
|
|
} // namespace lyx
|