2002-09-25 10:03:41 +00:00
|
|
|
/**
|
Rename files in src/support, step one.
src/support/package.h src/support/Package.h Package
src/support/package.C.in src/support/Package.C.in Package
src/support/path.h src/support/Path.h Path
src/support/fs_extras.h src/support/fs_extras.h NOCLASSES
src/support/RandomAccessList.h src/support/RandomAccessList.h RandomAccessList
src/support/lyxmanip.h src/support/lyxmanip.h NOCLASSES
src/support/rename.C src/support/rename.cpp NOCLASSES
src/support/abort.C src/support/abort.cpp NOCLASSES
src/support/lyxlib.h src/support/lyxlib.h NOCLASSES
src/support/ExceptionMessage.h src/support/ExceptionMessage.h ExceptionMessage
src/support/copy.C src/support/copy.cpp NOCLASSES
src/support/limited_stack.h src/support/limited_stack.h limited_stack
src/support/filefilterlist.C src/support/FileFilterList.cpp ['FileFilterList', 'Filter']
src/support/cow_ptr.h src/support/cow_ptr.h cow_ptr
src/support/os_unix.C src/support/os_unix.cpp NOCLASSES
src/support/socktools.h src/support/socktools.h NOCLASSES
src/support/forkedcontr.h src/support/ForkedcallsController.h ForkedcallsController
src/support/os.h src/support/os.h NOCLASSES
src/support/FileMonitor.h src/support/FileMonitor.h FileMonitor
src/support/copied_ptr.h src/support/copied_ptr.h copied_ptr
src/support/translator.h src/support/Translator.h Translator
src/support/filetools.C src/support/filetools.cpp NOCLASSES
src/support/unlink.C src/support/unlink.cpp NOCLASSES
src/support/os_win32.C src/support/os_win32.cpp GetFolderPath
src/support/lstrings.C src/support/lstrings.cpp NOCLASSES
src/support/qstring_helpers.C src/support/qstring_helpers.cpp NOCLASSES
src/support/getcwd.C src/support/getcwd.cpp NOCLASSES
src/support/systemcall.C src/support/Systemcall.cpp Systemcall
src/support/lyxalgo.h src/support/lyxalgo.h NOCLASSES
src/support/filefilterlist.h src/support/FileFilterList.h ['FileFilterList', 'Filter']
src/support/unicode.C src/support/unicode.cpp IconvProcessor
src/support/userinfo.C src/support/userinfo.cpp NOCLASSES
src/support/lyxtime.C src/support/lyxtime.cpp NOCLASSES
src/support/kill.C src/support/kill.cpp NOCLASSES
src/support/docstring.C src/support/docstring.cpp to_local8bit_failure
src/support/os_cygwin.C src/support/os_cygwin.cpp NOCLASSES
src/support/lyxsum.C src/support/lyxsum.cpp NOCLASSES
src/support/environment.C src/support/environment.cpp NOCLASSES
src/support/filetools.h src/support/filetools.h NOCLASSES
src/support/textutils.C src/support/textutils.cpp NOCLASSES
src/support/mkdir.C src/support/mkdir.cpp NOCLASSES
src/support/forkedcall.C src/support/Forkedcall.cpp ['ForkedProcess', 'Forkedcall']
src/support/tempname.C src/support/tempname.cpp NOCLASSES
src/support/os_win32.h src/support/os_win32.h GetFolderPath
src/support/types.h src/support/types.h NOCLASSES
src/support/lstrings.h src/support/lstrings.h NOCLASSES
src/support/forkedcallqueue.C src/support/ForkedCallQueue.cpp ForkedCallQueue
src/support/qstring_helpers.h src/support/qstring_helpers.h NOCLASSES
src/support/convert.C src/support/convert.cpp NOCLASSES
src/support/filename.C src/support/FileName.cpp ['FileName', 'DocFileName']
src/support/tests/convert.C src/support/tests/convert.cpp NOCLASSES
src/support/tests/filetools.C src/support/tests/filetools.cpp NOCLASSES
src/support/tests/lstrings.C src/support/tests/lstrings.cpp NOCLASSES
src/support/tests/boost.C src/support/tests/boost.cpp NOCLASSES
src/support/docstream.C src/support/docstream.cpp ['iconv_codecvt_facet_exception', 'idocfstream', 'odocfstream']
src/support/std_istream.h src/support/std_istream.h NOCLASSES
src/support/systemcall.h src/support/Systemcall.h Systemcall
src/support/chdir.C src/support/chdir.cpp NOCLASSES
src/support/std_ostream.h src/support/std_ostream.h NOCLASSES
src/support/unicode.h src/support/unicode.h IconvProcessor
src/support/path.C src/support/Path.cpp Path
src/support/fs_extras.C src/support/fs_extras.cpp NOCLASSES
src/support/userinfo.h src/support/userinfo.h NOCLASSES
src/support/lyxtime.h src/support/lyxtime.h NOCLASSES
src/support/docstring.h src/support/docstring.h to_local8bit_failure
src/support/debugstream.h src/support/debugstream.h basic_debugstream
src/support/environment.h src/support/environment.h NOCLASSES
src/support/textutils.h src/support/textutils.h NOCLASSES
src/support/forkedcall.h src/support/Forkedcall.h ['ForkedProcess', 'Forkedcall']
src/support/socktools.C src/support/socktools.cpp NOCLASSES
src/support/forkedcallqueue.h src/support/ForkedCallQueue.h ForkedCallQueue
src/support/forkedcontr.C src/support/ForkedcallsController.cpp ForkedcallsController
src/support/os.C src/support/os.cpp NOCLASSES
src/support/convert.h src/support/convert.h NOCLASSES
src/support/filename.h src/support/FileName.h ['FileName', 'DocFileName']
src/support/docstream.h src/support/docstream.h ['iconv_codecvt_facet_exception', 'idocfstream', 'odocfstream']
src/support/FileMonitor.C src/support/FileMonitor.cpp FileMonitor
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18024 a592a061-630c-0410-9148-cb99ea01b6c8
2007-04-26 05:12:52 +00:00
|
|
|
* \file lstrings.cpp
|
2002-09-25 10:03:41 +00:00
|
|
|
* 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
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2002-09-25 10:03:41 +00:00
|
|
|
* \author Jean-Marc Lasgouttes
|
2008-04-20 20:32:00 +00:00
|
|
|
* \author Dekel Tsur
|
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>
|
|
|
|
|
2004-11-07 13:22:51 +00:00
|
|
|
#include "support/lstrings.h"
|
2007-11-08 00:09:58 +00:00
|
|
|
|
2005-01-06 16:39:35 +00:00
|
|
|
#include "support/convert.h"
|
2007-02-26 09:03:21 +00:00
|
|
|
#include "support/qstring_helpers.h"
|
2004-11-07 13:22:51 +00:00
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2005-01-06 15:40:49 +00:00
|
|
|
|
2008-04-20 20:32:00 +00:00
|
|
|
#include <QString>
|
|
|
|
|
2008-11-17 12:49:47 +00:00
|
|
|
#include <cstdio>
|
2004-11-07 13:22:51 +00:00
|
|
|
#include <algorithm>
|
2004-07-24 10:55:30 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
namespace lyx {
|
2007-11-08 00:09:58 +00:00
|
|
|
|
2007-11-30 20:57:53 +00:00
|
|
|
// Using this allows us to have docstring default arguments in headers
|
|
|
|
// without #include "support/docstring" there.
|
|
|
|
docstring const & empty_docstring()
|
|
|
|
{
|
|
|
|
static docstring s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
// Using this allows us to have string default arguments in headers
|
2007-11-30 20:57:53 +00:00
|
|
|
// without #include <string>
|
2007-12-12 19:28:07 +00:00
|
|
|
string const & empty_string()
|
2007-11-30 20:57:53 +00:00
|
|
|
{
|
2007-12-12 19:28:07 +00:00
|
|
|
static string s;
|
2007-11-30 20:57:53 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2010-11-17 22:00:42 +00:00
|
|
|
namespace {
|
2007-11-08 00:09:58 +00:00
|
|
|
/**
|
|
|
|
* Convert a QChar into a UCS4 character.
|
|
|
|
* This is a hack (it does only make sense for the common part of the UCS4
|
|
|
|
* and UTF16 encodings) and should not be used.
|
|
|
|
* This does only exist because of performance reasons (a real conversion
|
|
|
|
* using iconv is too slow on windows).
|
|
|
|
*/
|
2010-11-17 22:00:42 +00:00
|
|
|
inline char_type qchar_to_ucs4(QChar const & qchar)
|
2007-11-08 00:09:58 +00:00
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(is_utf16(static_cast<char_type>(qchar.unicode())), /**/);
|
2007-11-08 00:09:58 +00:00
|
|
|
return static_cast<char_type>(qchar.unicode());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a UCS4 character into a QChar.
|
|
|
|
* This is a hack (it does only make sense for the common part of the UCS4
|
|
|
|
* and UTF16 encodings) and should not be used.
|
|
|
|
* This does only exist because of performance reasons (a real conversion
|
|
|
|
* using iconv is too slow on windows).
|
|
|
|
*/
|
2010-11-17 22:00:42 +00:00
|
|
|
inline QChar const ucs4_to_qchar(char_type const ucs4)
|
2007-11-08 00:09:58 +00:00
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(is_utf16(ucs4), /**/);
|
2007-11-08 00:09:58 +00:00
|
|
|
return QChar(static_cast<unsigned short>(ucs4));
|
|
|
|
}
|
|
|
|
|
2010-11-17 22:00:42 +00:00
|
|
|
/// Maximum valid UCS4 code point
|
|
|
|
char_type const ucs4_max = 0x10ffff;
|
|
|
|
} // anon namespace
|
2007-11-08 00:09:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool isLetterChar(char_type c)
|
|
|
|
{
|
|
|
|
if (!is_utf16(c)) {
|
|
|
|
if (c > ucs4_max)
|
|
|
|
// outside the UCS4 range
|
|
|
|
return false;
|
|
|
|
// assume that all non-utf16 characters are letters
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return ucs4_to_qchar(c).isLetter();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-04 23:35:08 +00:00
|
|
|
bool isLower(char_type c)
|
|
|
|
{
|
|
|
|
if (!is_utf16(c))
|
|
|
|
return false;
|
|
|
|
return ucs4_to_qchar(c).isLower();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-08 00:09:58 +00:00
|
|
|
bool isAlphaASCII(char_type c)
|
|
|
|
{
|
|
|
|
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool isPrintable(char_type c)
|
|
|
|
{
|
|
|
|
if (!is_utf16(c)) {
|
|
|
|
if (c > ucs4_max)
|
|
|
|
// outside the UCS4 range
|
|
|
|
return false;
|
|
|
|
// assume that all non-utf16 characters are printable
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-02 09:06:09 +00:00
|
|
|
// Not yet recognized by QChar::isPrint()
|
|
|
|
// See https://bugreports.qt-project.org/browse/QTBUG-12144
|
|
|
|
// LATIN CAPITAL LETTER SHARP S
|
|
|
|
else if (c == 0x1e9e)
|
|
|
|
return true;
|
2007-11-08 00:09:58 +00:00
|
|
|
return ucs4_to_qchar(c).isPrint();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool isPrintableNonspace(char_type c)
|
|
|
|
{
|
|
|
|
if (!is_utf16(c)) {
|
|
|
|
if (c > ucs4_max)
|
|
|
|
// outside the UCS4 range
|
|
|
|
return false;
|
|
|
|
// assume that all non-utf16 characters are printable and
|
|
|
|
// no space
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
QChar const qc = ucs4_to_qchar(c);
|
|
|
|
return qc.isPrint() && !qc.isSpace();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool isSpace(char_type c)
|
|
|
|
{
|
|
|
|
if (!is_utf16(c)) {
|
|
|
|
// assume that no non-utf16 character is a space
|
|
|
|
// c outside the UCS4 range is catched as well
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QChar const qc = ucs4_to_qchar(c);
|
|
|
|
return qc.isSpace();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-07 00:13:19 +00:00
|
|
|
bool isNumber(char_type c)
|
2007-11-08 00:09:58 +00:00
|
|
|
{
|
|
|
|
if (!is_utf16(c))
|
2010-12-07 00:13:19 +00:00
|
|
|
// assume that no non-utf16 character is a numeral
|
2007-11-08 00:09:58 +00:00
|
|
|
// c outside the UCS4 range is catched as well
|
|
|
|
return false;
|
2010-12-07 00:13:19 +00:00
|
|
|
return ucs4_to_qchar(c).isNumber();
|
2007-11-08 00:09:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool isDigitASCII(char_type c)
|
|
|
|
{
|
|
|
|
return '0' <= c && c <= '9';
|
|
|
|
}
|
|
|
|
|
2010-11-17 22:17:53 +00:00
|
|
|
|
|
|
|
bool isAlnumASCII(char_type c)
|
|
|
|
{
|
2010-12-07 00:13:19 +00:00
|
|
|
return isAlphaASCII(c) || isDigitASCII(c);
|
2010-11-17 22:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
namespace support {
|
2000-09-26 13:54:57 +00:00
|
|
|
|
2006-09-09 22:27:22 +00:00
|
|
|
int compare_no_case(docstring const & s, docstring const & s2)
|
|
|
|
{
|
|
|
|
docstring::const_iterator p = s.begin();
|
|
|
|
docstring::const_iterator p2 = s2.begin();
|
|
|
|
|
|
|
|
while (p != s.end() && p2 != s2.end()) {
|
2006-11-13 09:53:25 +00:00
|
|
|
char_type const lc1 = lowercase(*p);
|
|
|
|
char_type const lc2 = lowercase(*p2);
|
2006-09-09 22:27:22 +00:00
|
|
|
if (lc1 != lc2)
|
|
|
|
return (lc1 < lc2) ? -1 : 1;
|
|
|
|
++p;
|
|
|
|
++p2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.size() == s2.size())
|
|
|
|
return 0;
|
|
|
|
if (s.size() < s2.size())
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-27 14:10:35 +00:00
|
|
|
namespace {
|
2006-10-11 19:40:50 +00:00
|
|
|
|
2006-11-13 09:53:25 +00:00
|
|
|
template<typename Char>
|
|
|
|
Char ascii_tolower(Char c) {
|
2006-10-11 19:40:50 +00:00
|
|
|
if (c >= 'A' && c <= 'Z')
|
|
|
|
return c - 'A' + 'a';
|
|
|
|
return c;
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
2006-11-13 09:53:25 +00:00
|
|
|
}
|
|
|
|
|
2001-06-27 14:10:35 +00:00
|
|
|
|
2006-11-13 09:53:25 +00:00
|
|
|
int compare_ascii_no_case(string const & s, string const & s2)
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
2006-11-13 09:53:25 +00:00
|
|
|
string::const_iterator p = s.begin();
|
|
|
|
string::const_iterator p2 = s2.begin();
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-10-11 19:40:50 +00:00
|
|
|
|
2006-11-13 09:53:25 +00:00
|
|
|
int compare_ascii_no_case(docstring const & s, docstring const & s2)
|
2006-10-11 19:40:50 +00:00
|
|
|
{
|
2006-11-13 09:53:25 +00:00
|
|
|
docstring::const_iterator p = s.begin();
|
|
|
|
docstring::const_iterator p2 = s2.begin();
|
2006-10-11 19:40:50 +00:00
|
|
|
|
2006-11-13 09:53:25 +00:00
|
|
|
while (p != s.end() && p2 != s2.end()) {
|
|
|
|
char_type const lc1 = ascii_tolower(*p);
|
|
|
|
char_type const lc2 = ascii_tolower(*p2);
|
|
|
|
if (lc1 != lc2)
|
|
|
|
return (lc1 < lc2) ? -1 : 1;
|
|
|
|
++p;
|
|
|
|
++p2;
|
|
|
|
}
|
2006-10-11 19:40:50 +00:00
|
|
|
|
2006-11-13 09:53:25 +00:00
|
|
|
if (s.size() == s2.size())
|
|
|
|
return 0;
|
|
|
|
if (s.size() < s2.size())
|
|
|
|
return -1;
|
|
|
|
return 1;
|
2006-10-11 19:40:50 +00:00
|
|
|
}
|
|
|
|
|
2000-01-06 02:44:26 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
bool isStrInt(string const & str)
|
|
|
|
{
|
2007-11-30 20:57:53 +00:00
|
|
|
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);
|
2007-11-30 20:57:53 +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();
|
2007-11-30 20:57:53 +00:00
|
|
|
if ((*cit) == '-')
|
|
|
|
++cit;
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
string::const_iterator end = tmpstr.end();
|
2007-11-30 20:57:53 +00:00
|
|
|
for (; cit != end; ++cit)
|
2010-12-07 00:13:19 +00:00
|
|
|
if (!isDigitASCII(*cit))
|
2007-11-30 20:57:53 +00:00
|
|
|
return false;
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-04 17:18:01 +00:00
|
|
|
bool isStrUnsignedInt(string const & str)
|
|
|
|
{
|
2007-11-30 20:57:53 +00:00
|
|
|
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);
|
2007-11-30 20:57:53 +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();
|
2007-11-30 20:57:53 +00:00
|
|
|
for (; cit != end; ++cit)
|
2010-12-07 00:13:19 +00:00
|
|
|
if (!isDigitASCII(*cit))
|
2007-11-30 20:57:53 +00:00
|
|
|
return false;
|
|
|
|
|
2000-12-04 17:18:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
bool isStrDbl(string const & str)
|
|
|
|
{
|
2007-11-30 20:57:53 +00:00
|
|
|
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);
|
2007-11-30 20:57:53 +00:00
|
|
|
if (tmpstr.empty())
|
|
|
|
return false;
|
|
|
|
// if (tmpstr.count('.') > 1) return false;
|
2000-06-12 11:27:15 +00:00
|
|
|
|
|
|
|
string::const_iterator cit = tmpstr.begin();
|
2007-11-30 20:57:53 +00:00
|
|
|
bool found_dot = false;
|
|
|
|
if (*cit == '-')
|
|
|
|
++cit;
|
2000-08-03 21:17:52 +00:00
|
|
|
string::const_iterator end = tmpstr.end();
|
|
|
|
for (; cit != end; ++cit) {
|
2010-12-07 00:13:19 +00:00
|
|
|
if (!isDigitASCII(*cit) && *cit != '.')
|
2000-06-12 11:27:15 +00:00
|
|
|
return false;
|
|
|
|
if ('.' == (*cit)) {
|
2007-11-30 20:57:53 +00:00
|
|
|
if (found_dot)
|
2000-06-12 11:27:15 +00:00
|
|
|
return false;
|
2007-11-30 20:57:53 +00:00
|
|
|
found_dot = true;
|
2000-06-12 11:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
|
2010-12-07 00:13:19 +00:00
|
|
|
bool hasDigitASCII(docstring const & str)
|
2010-02-09 17:35:35 +00:00
|
|
|
{
|
2010-02-10 06:51:49 +00:00
|
|
|
docstring::const_iterator cit = str.begin();
|
|
|
|
docstring::const_iterator const end = str.end();
|
2010-12-07 00:13:19 +00:00
|
|
|
for (; cit != end; ++cit)
|
|
|
|
if (isDigitASCII(*cit))
|
2010-02-09 17:35:35 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-07 22:53:07 +00:00
|
|
|
bool isHexChar(char_type c)
|
2006-10-22 18:47:19 +00:00
|
|
|
{
|
|
|
|
return c == '0' ||
|
|
|
|
c == '1' ||
|
|
|
|
c == '2' ||
|
|
|
|
c == '3' ||
|
|
|
|
c == '4' ||
|
|
|
|
c == '5' ||
|
|
|
|
c == '6' ||
|
|
|
|
c == '7' ||
|
|
|
|
c == '8' ||
|
|
|
|
c == '9' ||
|
|
|
|
c == 'a' || c == 'A' ||
|
|
|
|
c == 'b' || c == 'B' ||
|
|
|
|
c == 'c' || c == 'C' ||
|
|
|
|
c == 'd' || c == 'D' ||
|
|
|
|
c == 'e' || c == 'E' ||
|
|
|
|
c == 'f' || c == 'F';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool isHex(docstring const & str)
|
|
|
|
{
|
|
|
|
int index = 0;
|
|
|
|
|
2006-10-22 19:09:09 +00:00
|
|
|
if (str.length() > 2 && str[0] == '0' &&
|
2006-10-22 18:47:19 +00:00
|
|
|
(str[1] == 'x' || str[1] == 'X'))
|
|
|
|
index = 2;
|
|
|
|
|
|
|
|
int const len = str.length();
|
|
|
|
|
|
|
|
for (; index < len; ++index) {
|
|
|
|
if (!isHexChar(str[index]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int hexToInt(docstring const & str)
|
|
|
|
{
|
|
|
|
string s = to_ascii(str);
|
|
|
|
int h;
|
|
|
|
sscanf(s.c_str(), "%x", &h);
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-11 12:00:34 +00:00
|
|
|
bool isAscii(docstring const & str)
|
|
|
|
{
|
|
|
|
int const len = str.length();
|
|
|
|
for (int i = 0; i < len; ++i)
|
2007-07-06 15:50:39 +00:00
|
|
|
if (str[i] >= 0x80)
|
2007-07-06 14:43:18 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool isAscii(string const & str)
|
|
|
|
{
|
|
|
|
int const len = str.length();
|
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
if (static_cast<unsigned char>(str[i]) >= 0x80)
|
2006-11-11 12:00:34 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
char lowercase(char c)
|
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
|
2002-03-21 17:09:55 +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
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
char uppercase(char c)
|
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(static_cast<unsigned char>(c) < 0x80, /**/);
|
2002-03-21 17:09:55 +00:00
|
|
|
return char(toupper(c));
|
2000-07-24 13:53:19 +00:00
|
|
|
}
|
1999-10-02 16:21:10 +00:00
|
|
|
|
2006-11-13 09:53:25 +00:00
|
|
|
|
2006-09-03 14:09:24 +00:00
|
|
|
char_type lowercase(char_type c)
|
|
|
|
{
|
2007-02-26 09:03:21 +00:00
|
|
|
if (!is_utf16(c))
|
|
|
|
// We don't know how to lowercase a non-utf16 char
|
2006-09-03 14:09:24 +00:00
|
|
|
return c;
|
2007-02-26 09:03:21 +00:00
|
|
|
return qchar_to_ucs4(ucs4_to_qchar(c).toLower());
|
2006-09-03 14:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char_type uppercase(char_type c)
|
|
|
|
{
|
2007-02-26 09:03:21 +00:00
|
|
|
if (!is_utf16(c))
|
|
|
|
// We don't know how to uppercase a non-utf16 char
|
2006-09-03 14:09:24 +00:00
|
|
|
return c;
|
2007-02-26 09:03:21 +00:00
|
|
|
return qchar_to_ucs4(ucs4_to_qchar(c).toUpper());
|
2006-09-03 14:09:24 +00:00
|
|
|
}
|
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
|
2010-10-14 11:01:15 +00:00
|
|
|
bool isLowerCase(char_type ch) {
|
|
|
|
return lowercase(ch) == ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool isUpperCase(char_type ch) {
|
|
|
|
return uppercase(ch) == ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-25 19:33:52 +00:00
|
|
|
namespace {
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
// since we cannot use tolower and toupper directly in the
|
|
|
|
// calls to transform yet, we use these helper clases. (Lgb)
|
2001-04-25 19:33:52 +00:00
|
|
|
|
2007-02-26 09:03:21 +00:00
|
|
|
struct local_lowercase {
|
|
|
|
char_type operator()(char_type c) const {
|
2010-11-17 21:41:23 +00:00
|
|
|
return lowercase(c);
|
2007-02-26 09:03:21 +00:00
|
|
|
}
|
2001-04-25 19:33:52 +00:00
|
|
|
};
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2001-04-25 19:33:52 +00:00
|
|
|
struct local_uppercase {
|
2007-02-26 18:54:03 +00:00
|
|
|
char_type operator()(char_type c) const {
|
2010-11-17 21:41:23 +00:00
|
|
|
return uppercase(c);
|
2001-04-25 19:33:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-12-15 16:09:05 +00:00
|
|
|
template<typename Char> struct local_ascii_lowercase {
|
2007-11-10 13:44:50 +00:00
|
|
|
Char operator()(Char c) const { return ascii_tolower(c); }
|
2002-07-16 21:17:10 +00:00
|
|
|
};
|
|
|
|
|
2001-04-25 19:33:52 +00:00
|
|
|
} // end of anon namespace
|
|
|
|
|
2010-11-17 21:41:23 +00:00
|
|
|
|
2006-12-10 11:52:46 +00:00
|
|
|
docstring const lowercase(docstring const & a)
|
|
|
|
{
|
|
|
|
docstring tmp(a);
|
2007-02-26 09:03:21 +00:00
|
|
|
transform(tmp.begin(), tmp.end(), tmp.begin(), local_lowercase());
|
2006-12-10 11:52:46 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-26 18:54:03 +00:00
|
|
|
docstring const uppercase(docstring const & a)
|
1999-12-15 06:12:28 +00:00
|
|
|
{
|
2007-02-26 18:54:03 +00:00
|
|
|
docstring 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(),
|
2006-12-15 16:09:05 +00:00
|
|
|
local_ascii_lowercase<char>());
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring const ascii_lowercase(docstring const & a)
|
|
|
|
{
|
|
|
|
docstring tmp(a);
|
|
|
|
transform(tmp.begin(), tmp.end(), tmp.begin(),
|
|
|
|
local_ascii_lowercase<char_type>());
|
2002-07-16 21:17:10 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 17:00:12 +00:00
|
|
|
char_type superscript(char_type c)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case '2': return 0x00b2;
|
|
|
|
case '3': return 0x00b3;
|
|
|
|
case '1': return 0x00b9;
|
|
|
|
case '0': return 0x2070;
|
|
|
|
case 'i': return 0x2071;
|
|
|
|
case '4': return 0x2074;
|
|
|
|
case '5': return 0x2075;
|
|
|
|
case '6': return 0x2076;
|
|
|
|
case '7': return 0x2077;
|
|
|
|
case '8': return 0x2078;
|
|
|
|
case '9': return 0x2079;
|
|
|
|
case '+': return 0x207a;
|
|
|
|
case '-': return 0x207b;
|
|
|
|
case '=': return 0x207c;
|
|
|
|
case '(': return 0x207d;
|
|
|
|
case ')': return 0x207e;
|
|
|
|
case 'n': return 0x207f;
|
|
|
|
case 'h': return 0x02b0;
|
|
|
|
case 0x0266: return 0x02b1; // LATIN SMALL LETTER H WITH HOOK
|
|
|
|
case 'j': return 0x02b2;
|
|
|
|
case 'r': return 0x02b3;
|
|
|
|
case 0x0279: return 0x02b4; // LATIN SMALL LETTER TURNED R
|
|
|
|
case 0x027b: return 0x02b5; // LATIN SMALL LETTER TURNED R WITH HOOK
|
|
|
|
case 0x0281: return 0x02b6; // LATIN SMALL LETTER CAPITAL INVERTED R
|
|
|
|
case 'w': return 0x02b7;
|
|
|
|
case 'y': return 0x02b8;
|
|
|
|
// case 0x0294: return 0x02c0; // LATIN LETTER GLOTTAL STOP)
|
|
|
|
// case 0x0295: return 0x02c1; // LATIN LETTER PHARYNGEAL VOICED FRICATIVE
|
|
|
|
// (= LATIN LETTER REVERSED GLOTTAL STOP)
|
|
|
|
case 'l': return 0x02e1;
|
|
|
|
case 's': return 0x02e2;
|
|
|
|
case 'x': return 0x02e3;
|
|
|
|
// case 0x0295: return 0x02e4; // LATIN SMALL LETTER REVERSED GLOTTAL STOP
|
|
|
|
case 'A': return 0x1d2c;
|
|
|
|
case 0x00c6: return 0x1d2d; // LATIN CAPITAL LETTER AE
|
|
|
|
case 'B': return 0x1d2e;
|
|
|
|
case 'D': return 0x1d30;
|
|
|
|
case 'E': return 0x1d31;
|
|
|
|
case 'G': return 0x1d33;
|
|
|
|
case 'H': return 0x1d34;
|
|
|
|
case 'I': return 0x1d35;
|
|
|
|
case 'J': return 0x1d36;
|
|
|
|
case 'K': return 0x1d37;
|
|
|
|
case 'L': return 0x1d38;
|
|
|
|
case 'M': return 0x1d39;
|
|
|
|
case 'N': return 0x1d3a;
|
|
|
|
case 'O': return 0x1d3c;
|
|
|
|
case 'P': return 0x1d3e;
|
|
|
|
case 'R': return 0x1d3f;
|
|
|
|
case 'T': return 0x1d40;
|
|
|
|
case 'U': return 0x1d41;
|
|
|
|
case 'W': return 0x1d42;
|
|
|
|
case 'a': return 0x1d43;
|
|
|
|
case 0x0250: return 0x1d44; // LATIN SMALL LETTER TURNED A
|
|
|
|
case 0x0251: return 0x1d45; // LATIN SMALL LETTER ALPHA
|
|
|
|
case 'b': return 0x1d47;
|
|
|
|
case 'd': return 0x1d48;
|
|
|
|
case 'e': return 0x1d49;
|
|
|
|
case 0x0259: return 0x1d4a; // LATIN SMALL LETTER SCHWA
|
|
|
|
case 0x025b: return 0x1d4b; // LATIN SMALL LETTER OPEN E
|
|
|
|
case 0x1d08: return 0x1d4c; // LATIN SMALL LETTER TURNED OPEN E
|
|
|
|
case 'g': return 0x1d4d;
|
|
|
|
case 0x1d09: return 0x1d4e; // LATIN SMALL LETTER TURNED I
|
|
|
|
case 'k': return 0x1d4f;
|
|
|
|
case 'm': return 0x1d50;
|
|
|
|
case 0x014b: return 0x1d51; // LATIN SMALL LETTER ENG
|
|
|
|
case 'o': return 0x1d52;
|
|
|
|
case 0x0254: return 0x1d53; // LATIN SMALL LETTER OPEN O
|
|
|
|
case 0x1d16: return 0x1d54; // LATIN SMALL LETTER TOP HALF O
|
|
|
|
case 0x1d17: return 0x1d55; // LATIN SMALL LETTER BOTTOM HALF O
|
|
|
|
case 'p': return 0x1d56;
|
|
|
|
case 't': return 0x1d57;
|
|
|
|
case 'u': return 0x1d58;
|
|
|
|
case 0x1d1d: return 0x1d59; // LATIN SMALL LETTER SIDEWAYS U
|
|
|
|
case 0x1d1f: return 0x1d5a; // LATIN SMALL LETTER SIDEWAYS TURNED M
|
|
|
|
case 'v': return 0x1d5b;
|
|
|
|
case 0x03b2: return 0x1d5d; // GREEK SMALL LETTER BETA
|
|
|
|
case 0x03b3: return 0x1d5e; // GREEK SMALL LETTER GAMMA
|
|
|
|
case 0x03b4: return 0x1d5f; // GREEK SMALL LETTER DELTA
|
|
|
|
case 0x03c6: return 0x1d60; // GREEK SMALL LETTER PHI
|
|
|
|
case 0x03c7: return 0x1d61; // GREEK SMALL LETTER CHI
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char_type subscript(char_type c)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case 'i': return 0x1d62;
|
|
|
|
case 'r': return 0x1d63;
|
|
|
|
case 'u': return 0x1d64;
|
|
|
|
case 'v': return 0x1d65;
|
|
|
|
case 0x03b2: return 0x1d66; // GREEK SMALL LETTER BETA
|
|
|
|
case 0x03b3: return 0x1d67; // GREEK SMALL LETTER GAMMA
|
|
|
|
case 0x03c1: return 0x1d68; // GREEK SMALL LETTER RHO
|
|
|
|
case 0x03c6: return 0x1d69; // GREEK SMALL LETTER PHI
|
|
|
|
case 0x03c7: return 0x1d6a; // GREEK SMALL LETTER CHI
|
|
|
|
case '0': return 0x2080;
|
|
|
|
case '1': return 0x2081;
|
|
|
|
case '2': return 0x2082;
|
|
|
|
case '3': return 0x2083;
|
|
|
|
case '4': return 0x2084;
|
|
|
|
case '5': return 0x2085;
|
|
|
|
case '6': return 0x2086;
|
|
|
|
case '7': return 0x2087;
|
|
|
|
case '8': return 0x2088;
|
|
|
|
case '9': return 0x2089;
|
|
|
|
case '+': return 0x208a;
|
|
|
|
case '-': return 0x208b;
|
|
|
|
case '=': return 0x208c;
|
|
|
|
case '(': return 0x208d;
|
|
|
|
case ')': return 0x208e;
|
|
|
|
case 'a': return 0x2090;
|
|
|
|
case 'e': return 0x2091;
|
|
|
|
case 'o': return 0x2092;
|
|
|
|
case 'x': return 0x2093;
|
|
|
|
case 0x0259: return 0x2093; // LATIN SMALL LETTER SCHWA
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-05 17:11:32 +00:00
|
|
|
bool prefixIs(docstring const & a, char_type c)
|
|
|
|
{
|
|
|
|
if (a.empty())
|
|
|
|
return false;
|
|
|
|
return a[0] == c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
bool prefixIs(string const & a, string const & pre)
|
|
|
|
{
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t const prelen = pre.length();
|
|
|
|
size_t const alen = a.length();
|
2008-02-01 17:04:59 +00:00
|
|
|
return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
|
2000-09-26 13:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-20 16:12:49 +00:00
|
|
|
bool prefixIs(docstring const & a, docstring const & pre)
|
|
|
|
{
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t const prelen = pre.length();
|
|
|
|
size_t const alen = a.length();
|
2008-02-01 17:04:59 +00:00
|
|
|
return prelen <= alen && !a.empty() && a.compare(0, prelen, pre) == 0;
|
2006-10-20 16:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
bool suffixIs(string const & a, char c)
|
|
|
|
{
|
2009-05-11 07:52:39 +00:00
|
|
|
if (a.empty())
|
2008-07-16 15:29:00 +00:00
|
|
|
return false;
|
1999-11-22 16:19:48 +00:00
|
|
|
return a[a.length() - 1] == c;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-05 17:11:32 +00:00
|
|
|
bool suffixIs(docstring const & a, char_type c)
|
|
|
|
{
|
|
|
|
if (a.empty())
|
|
|
|
return false;
|
|
|
|
return a[a.length() - 1] == c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
bool suffixIs(string const & a, string const & suf)
|
|
|
|
{
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t const suflen = suf.length();
|
|
|
|
size_t const alen = a.length();
|
2008-02-01 17:04:59 +00:00
|
|
|
return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
|
2000-09-26 13:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-16 15:29:00 +00:00
|
|
|
bool suffixIs(docstring const & a, docstring const & suf)
|
|
|
|
{
|
|
|
|
size_t const suflen = suf.length();
|
|
|
|
size_t const alen = a.length();
|
|
|
|
return suflen <= alen && a.compare(alen - suflen, suflen, suf) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2007-11-10 13:44:50 +00:00
|
|
|
if (a.empty())
|
|
|
|
return string();
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t k = 0;
|
|
|
|
size_t i = 0;
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
// Find delimiter or end of string
|
2007-11-10 13:44:50 +00:00
|
|
|
for (; n--;) {
|
1999-10-02 16:21:10 +00:00
|
|
|
if ((i = a.find(delim, i)) == string::npos)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
++i; // step delim
|
2007-11-10 13:44:50 +00:00
|
|
|
}
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
// i is now the n'th delim (or string::npos)
|
2007-11-10 13:44:50 +00:00
|
|
|
if (i == string::npos)
|
|
|
|
return string();
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
k = a.find(delim, i);
|
|
|
|
// k is now the n'th + 1 delim (or string::npos)
|
|
|
|
|
|
|
|
return a.substr(i, k - i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-09 22:27:22 +00:00
|
|
|
docstring const token(docstring const & a, char_type delim, int n)
|
|
|
|
{
|
2007-11-30 20:57:53 +00:00
|
|
|
if (a.empty())
|
|
|
|
return docstring();
|
2006-09-09 22:27:22 +00:00
|
|
|
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t k = 0;
|
|
|
|
size_t i = 0;
|
2006-09-09 22:27:22 +00:00
|
|
|
|
|
|
|
// Find delimiter or end of string
|
2007-11-10 13:44:50 +00:00
|
|
|
for (; n--;) {
|
2006-09-09 22:27:22 +00:00
|
|
|
if ((i = a.find(delim, i)) == docstring::npos)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
++i; // step delim
|
2007-11-10 13:44:50 +00:00
|
|
|
}
|
|
|
|
|
2006-09-09 22:27:22 +00:00
|
|
|
// i is now the n'th delim (or string::npos)
|
2007-11-10 13:44:50 +00:00
|
|
|
if (i == docstring::npos)
|
|
|
|
return docstring();
|
|
|
|
|
2006-09-09 22:27:22 +00:00
|
|
|
k = a.find(delim, i);
|
|
|
|
// k is now the n'th + 1 delim (or string::npos)
|
|
|
|
|
|
|
|
return a.substr(i, k - i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
// 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;
|
2007-11-10 13:44:50 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-19 15:52:35 +00:00
|
|
|
// this could probably be faster and/or cleaner, but it seems to work (JMarc)
|
|
|
|
// rewritten to use new string (Lgb)
|
|
|
|
int tokenPos(docstring const & a, char_type delim, docstring const & tok)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
docstring str = a;
|
|
|
|
docstring tmptok;
|
|
|
|
|
|
|
|
while (!str.empty()) {
|
|
|
|
str = split(str, tmptok, delim);
|
|
|
|
if (tok == tmptok)
|
|
|
|
return i;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-03 07:02:38 +00:00
|
|
|
namespace {
|
|
|
|
|
2006-10-11 19:40:50 +00:00
|
|
|
/// Substitute all \a oldchar with \a newchar
|
2006-09-03 07:02:38 +00:00
|
|
|
template<typename Ch> inline
|
2007-12-12 19:28:07 +00:00
|
|
|
basic_string<Ch> const subst_char(basic_string<Ch> const & a,
|
2006-09-03 14:09:24 +00:00
|
|
|
Ch oldchar, Ch newchar)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2007-12-12 19:28:07 +00:00
|
|
|
typedef basic_string<Ch> String;
|
2006-09-03 07:02:38 +00:00
|
|
|
String tmp(a);
|
|
|
|
typename String::iterator lit = tmp.begin();
|
|
|
|
typename 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;
|
|
|
|
}
|
|
|
|
|
2010-04-16 23:47:39 +00:00
|
|
|
|
2007-10-31 22:40:34 +00:00
|
|
|
/// Substitute all \a oldchar with \a newchar
|
|
|
|
docstring const subst_char(docstring const & a,
|
|
|
|
docstring::value_type oldchar, docstring::value_type newchar)
|
|
|
|
{
|
|
|
|
docstring tmp(a);
|
|
|
|
docstring::iterator lit = tmp.begin();
|
|
|
|
docstring::iterator end = tmp.end();
|
|
|
|
for (; lit != end; ++lit)
|
|
|
|
if ((*lit) == oldchar)
|
|
|
|
(*lit) = newchar;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
2006-10-11 19:40:50 +00:00
|
|
|
/// substitutes all instances of \a oldstr with \a newstr
|
2006-09-03 07:02:38 +00:00
|
|
|
template<typename String> inline
|
2006-09-03 14:09:24 +00:00
|
|
|
String const subst_string(String const & a,
|
|
|
|
String const & oldstr, String const & newstr)
|
2000-09-26 13:54:57 +00:00
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(!oldstr.empty(), /**/);
|
2006-09-03 07:02:38 +00:00
|
|
|
String lstr = a;
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t i = 0;
|
|
|
|
size_t 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;
|
|
|
|
}
|
|
|
|
|
2010-04-16 23:47:39 +00:00
|
|
|
|
2007-10-31 22:40:34 +00:00
|
|
|
docstring const subst_string(docstring const & a,
|
|
|
|
docstring const & oldstr, docstring const & newstr)
|
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(!oldstr.empty(), /**/);
|
2007-10-31 22:40:34 +00:00
|
|
|
docstring lstr = a;
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t i = 0;
|
|
|
|
size_t const olen = oldstr.length();
|
2007-10-31 22:40:34 +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;
|
|
|
|
}
|
|
|
|
|
2006-09-03 07:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const subst(string const & a, char oldchar, char newchar)
|
|
|
|
{
|
2006-09-03 14:09:24 +00:00
|
|
|
return subst_char(a, oldchar, newchar);
|
2006-09-03 07:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring const subst(docstring const & a,
|
|
|
|
char_type oldchar, char_type newchar)
|
|
|
|
{
|
2006-09-03 14:09:24 +00:00
|
|
|
return subst_char(a, oldchar, newchar);
|
2006-09-03 07:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const subst(string const & a,
|
|
|
|
string const & oldstr, string const & newstr)
|
|
|
|
{
|
2006-09-03 14:09:24 +00:00
|
|
|
return subst_string(a, oldstr, newstr);
|
2006-09-03 07:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring const subst(docstring const & a,
|
|
|
|
docstring const & oldstr, docstring const & newstr)
|
|
|
|
{
|
2006-09-03 14:09:24 +00:00
|
|
|
return subst_string(a, oldstr, newstr);
|
2006-09-03 07:02:38 +00:00
|
|
|
}
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
|
2010-04-16 23:47:39 +00:00
|
|
|
/// Count all occurences of char \a chr inside \a str
|
|
|
|
int count_char(docstring const & str, docstring::value_type chr)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
docstring::const_iterator lit = str.begin();
|
|
|
|
docstring::const_iterator end = str.end();
|
|
|
|
for (; lit != end; ++lit)
|
|
|
|
if ((*lit) == chr)
|
|
|
|
count++;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-09 09:15:37 +00:00
|
|
|
docstring const trim(docstring const & a, char const * p)
|
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(p, /**/);
|
2006-10-09 09:15:37 +00:00
|
|
|
|
|
|
|
if (a.empty() || !*p)
|
|
|
|
return a;
|
|
|
|
|
2007-11-10 13:44:50 +00:00
|
|
|
docstring s = from_ascii(p);
|
|
|
|
size_t r = a.find_last_not_of(s);
|
|
|
|
size_t l = a.find_first_not_of(s);
|
2006-10-09 09:15:37 +00:00
|
|
|
|
|
|
|
// Is this the minimal test? (lgb)
|
|
|
|
if (r == docstring::npos && l == docstring::npos)
|
|
|
|
return docstring();
|
|
|
|
|
|
|
|
return a.substr(l, r - l + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(p, /**/);
|
2002-07-28 18:13:51 +00:00
|
|
|
|
2002-07-28 22:50:13 +00:00
|
|
|
if (a.empty() || !*p)
|
|
|
|
return a;
|
|
|
|
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t r = a.find_last_not_of(p);
|
|
|
|
size_t l = a.find_first_not_of(p);
|
2002-07-28 22:50:13 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(p, /**/);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2002-07-28 22:50:13 +00:00
|
|
|
if (a.empty() || !*p)
|
|
|
|
return a;
|
|
|
|
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t r = a.find_last_not_of(p);
|
2002-07-28 22:50:13 +00:00
|
|
|
|
|
|
|
// Is this test really needed? (Lgb)
|
|
|
|
if (r == string::npos)
|
|
|
|
return string();
|
|
|
|
|
|
|
|
return a.substr(0, r + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-17 10:52:04 +00:00
|
|
|
docstring const rtrim(docstring const & a, char const * p)
|
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(p, /**/);
|
2006-12-17 10:52:04 +00:00
|
|
|
|
|
|
|
if (a.empty() || !*p)
|
|
|
|
return a;
|
|
|
|
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t r = a.find_last_not_of(from_ascii(p));
|
2006-12-17 10:52:04 +00:00
|
|
|
|
|
|
|
// Is this test really needed? (Lgb)
|
|
|
|
if (r == docstring::npos)
|
|
|
|
return docstring();
|
|
|
|
|
|
|
|
return a.substr(0, r + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-28 22:50:13 +00:00
|
|
|
string const ltrim(string const & a, char const * p)
|
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(p, /**/);
|
2002-07-28 22:50:13 +00:00
|
|
|
if (a.empty() || !*p)
|
|
|
|
return a;
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t l = a.find_first_not_of(p);
|
2002-07-28 22:50:13 +00:00
|
|
|
if (l == string::npos)
|
|
|
|
return string();
|
|
|
|
return a.substr(l, string::npos);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
docstring const ltrim(docstring const & a, char const * p)
|
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(p, /**/);
|
2006-10-22 10:15:23 +00:00
|
|
|
if (a.empty() || !*p)
|
|
|
|
return a;
|
|
|
|
size_t l = a.find_first_not_of(from_ascii(p));
|
|
|
|
if (l == docstring::npos)
|
|
|
|
return docstring();
|
|
|
|
return a.substr(l, docstring::npos);
|
|
|
|
}
|
|
|
|
|
2006-10-11 19:40:50 +00:00
|
|
|
namespace {
|
|
|
|
|
2006-10-11 18:32:32 +00:00
|
|
|
template<typename String, typename Char> inline
|
|
|
|
String const doSplit(String const & a, String & piece, Char delim)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2006-10-11 18:32:32 +00:00
|
|
|
String tmp;
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t i = a.find(delim);
|
1999-10-02 16:21:10 +00:00
|
|
|
if (i == a.length() - 1) {
|
|
|
|
piece = a.substr(0, i);
|
2006-10-11 18:32:32 +00:00
|
|
|
} else if (i != String::npos) {
|
1999-10-02 16:21:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-10-31 22:40:34 +00:00
|
|
|
template<typename Char> inline
|
|
|
|
docstring const doSplit(docstring const & a, docstring & piece, Char delim)
|
|
|
|
{
|
|
|
|
docstring tmp;
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t i = a.find(delim);
|
2007-10-31 22:40:34 +00:00
|
|
|
if (i == a.length() - 1) {
|
|
|
|
piece = a.substr(0, i);
|
|
|
|
} else if (i != docstring::npos) {
|
|
|
|
piece = a.substr(0, i);
|
|
|
|
tmp = a.substr(i + 1);
|
|
|
|
} else if (i == 0) {
|
|
|
|
piece.erase();
|
|
|
|
tmp = a.substr(i + 1);
|
|
|
|
} else {
|
|
|
|
piece = a;
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2007-11-08 00:09:58 +00:00
|
|
|
} // anon
|
2006-10-11 19:40:50 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
2006-10-11 18:32:32 +00:00
|
|
|
string const split(string const & a, string & piece, char delim)
|
|
|
|
{
|
|
|
|
return doSplit(a, piece, delim);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring const split(docstring const & a, docstring & piece, char_type delim)
|
|
|
|
{
|
|
|
|
return doSplit(a, piece, delim);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t i = a.find(delim);
|
1999-10-02 16:21:10 +00:00
|
|
|
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;
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t i = a.rfind(delim);
|
1999-10-02 16:21:10 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2009-07-25 10:44:36 +00:00
|
|
|
docstring const rsplit(docstring const & a, char_type delim)
|
|
|
|
{
|
|
|
|
docstring tmp;
|
|
|
|
size_t i = a.rfind(delim);
|
|
|
|
if (i != string::npos)
|
|
|
|
tmp = a.substr(i + 1);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-19 16:51:30 +00:00
|
|
|
docstring const escape(docstring const & lab)
|
2001-06-27 14:10:35 +00:00
|
|
|
{
|
2006-11-12 13:42:20 +00:00
|
|
|
char_type hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
|
2007-05-28 22:27:45 +00:00
|
|
|
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
2006-10-19 16:51:30 +00:00
|
|
|
docstring enc;
|
2007-11-10 13:44:50 +00:00
|
|
|
for (size_t i = 0; i < lab.length(); ++i) {
|
2006-11-12 13:42:20 +00:00
|
|
|
char_type c = lab[i];
|
2012-06-29 23:24:40 +00:00
|
|
|
if (c >= 128 || c == '=' || c == '%' || c == '#' || c == '$'
|
|
|
|
|| c == '}' || c == '{' || c == ']' || c == '[' || c == '&') {
|
2006-11-12 13:42:20 +00:00
|
|
|
// Although char_type is a 32 bit type we know that
|
|
|
|
// UCS4 occupies only 21 bits, so we don't need to
|
|
|
|
// encode bigger values. Test for 2^24 because we
|
|
|
|
// can encode that with the 6 hex digits that are
|
|
|
|
// needed for 21 bits anyway.
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(c < (1 << 24), /**/);
|
2001-06-27 14:10:35 +00:00
|
|
|
enc += '=';
|
2006-11-12 13:42:20 +00:00
|
|
|
enc += hexdigit[(c>>20) & 15];
|
|
|
|
enc += hexdigit[(c>>16) & 15];
|
|
|
|
enc += hexdigit[(c>>12) & 15];
|
|
|
|
enc += hexdigit[(c>> 8) & 15];
|
|
|
|
enc += hexdigit[(c>> 4) & 15];
|
|
|
|
enc += hexdigit[ c & 15];
|
2001-06-27 14:10:35 +00:00
|
|
|
} else {
|
|
|
|
enc += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return enc;
|
|
|
|
}
|
2002-04-15 12:05:07 +00:00
|
|
|
|
|
|
|
|
2009-05-07 18:04:05 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// this doesn't check whether str is empty, so do that first.
|
2009-09-19 16:15:09 +00:00
|
|
|
vector<docstring> wrapToVec(docstring const & str, int ind,
|
2009-05-11 07:52:39 +00:00
|
|
|
size_t const width)
|
2009-05-07 12:23:12 +00:00
|
|
|
{
|
|
|
|
docstring s = trim(str);
|
2009-05-11 07:52:39 +00:00
|
|
|
if (s.empty())
|
2009-05-07 18:04:05 +00:00
|
|
|
return vector<docstring>();
|
2009-05-07 12:23:12 +00:00
|
|
|
|
|
|
|
docstring indent;
|
2009-09-19 16:15:09 +00:00
|
|
|
if (ind < 0) {
|
|
|
|
indent.insert(0, -ind, ' ');
|
|
|
|
ind = 0;
|
|
|
|
} else if (ind > 0)
|
|
|
|
s.insert(0, ind, ' ');
|
2009-05-07 12:23:12 +00:00
|
|
|
|
2009-05-07 18:04:05 +00:00
|
|
|
vector<docstring> retval;
|
2009-05-07 12:23:12 +00:00
|
|
|
while (s.size() > width) {
|
2009-09-19 16:15:09 +00:00
|
|
|
// find the last space within the first 'width' chars
|
2009-11-13 13:47:25 +00:00
|
|
|
size_t const i = s.find_last_of(' ', width - 1);
|
2009-09-30 13:50:07 +00:00
|
|
|
if (i == docstring::npos || i <= size_t(ind)) {
|
2009-05-07 12:23:12 +00:00
|
|
|
// no space found
|
|
|
|
s = s.substr(0, width - 3) + "...";
|
|
|
|
break;
|
|
|
|
}
|
2009-05-07 18:04:05 +00:00
|
|
|
retval.push_back(s.substr(0, i));
|
2009-05-07 12:23:12 +00:00
|
|
|
s = indent + s.substr(i);
|
2009-09-20 22:58:55 +00:00
|
|
|
ind = indent.size();
|
2009-05-07 12:23:12 +00:00
|
|
|
}
|
2009-05-11 07:52:39 +00:00
|
|
|
if (!s.empty())
|
2009-05-07 18:04:05 +00:00
|
|
|
retval.push_back(s);
|
2009-05-07 12:23:12 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2009-05-07 18:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring wrap(docstring const & str, int const ind, size_t const width)
|
|
|
|
{
|
|
|
|
docstring s = trim(str);
|
2009-05-11 07:52:39 +00:00
|
|
|
if (s.empty())
|
2009-05-07 18:04:05 +00:00
|
|
|
return docstring();
|
|
|
|
|
|
|
|
vector<docstring> const svec = wrapToVec(str, ind, width);
|
|
|
|
return getStringFromVector(svec, from_ascii("\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring wrapParas(docstring const & str, int const indent,
|
2009-05-11 07:52:39 +00:00
|
|
|
size_t const width, size_t const maxlines)
|
2009-05-07 18:04:05 +00:00
|
|
|
{
|
2010-12-22 01:28:32 +00:00
|
|
|
docstring const dots = from_ascii("...");
|
2009-05-07 18:04:05 +00:00
|
|
|
if (str.empty())
|
|
|
|
return docstring();
|
|
|
|
|
2009-11-13 13:47:25 +00:00
|
|
|
vector<docstring> const pars = getVectorFromString(str, from_ascii("\n"), true);
|
2009-05-07 18:04:05 +00:00
|
|
|
vector<docstring> retval;
|
|
|
|
|
2009-11-13 13:47:25 +00:00
|
|
|
vector<docstring>::const_iterator it = pars.begin();
|
|
|
|
vector<docstring>::const_iterator const en = pars.end();
|
2009-05-07 18:04:05 +00:00
|
|
|
for (; it != en; ++it) {
|
|
|
|
vector<docstring> tmp = wrapToVec(*it, indent, width);
|
2009-05-11 07:52:39 +00:00
|
|
|
size_t const nlines = tmp.size();
|
2009-05-07 18:04:05 +00:00
|
|
|
if (nlines == 0)
|
|
|
|
continue;
|
2009-05-11 07:52:39 +00:00
|
|
|
size_t const curlines = retval.size();
|
2010-12-22 01:28:32 +00:00
|
|
|
if (maxlines > 0 && curlines + nlines > maxlines) {
|
|
|
|
tmp.resize(maxlines - curlines);
|
|
|
|
docstring last = tmp.back();
|
|
|
|
size_t const lsize = last.size();
|
|
|
|
if (lsize > width - 3) {
|
|
|
|
size_t const i = last.find_last_of(' ', width - 3);
|
|
|
|
if (i == docstring::npos || i <= size_t(indent))
|
|
|
|
// no space found
|
|
|
|
last = last.substr(0, lsize - 3) + dots;
|
|
|
|
else
|
|
|
|
last = last.substr(0, i) + dots;
|
|
|
|
} else
|
|
|
|
last += dots;
|
|
|
|
tmp.pop_back();
|
|
|
|
tmp.push_back(last);
|
2009-05-07 18:04:05 +00:00
|
|
|
}
|
|
|
|
retval.insert(retval.end(), tmp.begin(), tmp.end());
|
2009-05-07 20:44:07 +00:00
|
|
|
if (maxlines > 0 && retval.size() >= maxlines)
|
2009-05-07 18:04:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return getStringFromVector(retval, from_ascii("\n"));
|
|
|
|
}
|
2009-05-07 12:23:12 +00:00
|
|
|
|
|
|
|
|
2006-12-17 10:52:04 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
template<typename String> vector<String> const
|
2008-07-14 16:09:59 +00:00
|
|
|
getVectorFromStringT(String const & str, String const & delim, bool keepempty)
|
2002-04-15 12:05:07 +00:00
|
|
|
{
|
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
|
2006-12-17 10:52:04 +00:00
|
|
|
vector<String> vec;
|
2002-11-04 00:15:56 +00:00
|
|
|
if (str.empty())
|
|
|
|
return vec;
|
2006-12-17 10:52:04 +00:00
|
|
|
String keys = rtrim(str);
|
2007-11-10 13:44:50 +00:00
|
|
|
while (true) {
|
|
|
|
size_t const idx = keys.find(delim);
|
2006-12-17 10:52:04 +00:00
|
|
|
if (idx == String::npos) {
|
2002-11-04 00:15:56 +00:00
|
|
|
vec.push_back(ltrim(keys));
|
|
|
|
break;
|
|
|
|
}
|
2006-12-17 10:52:04 +00:00
|
|
|
String const key = trim(keys.substr(0, idx));
|
2008-07-14 16:09:59 +00:00
|
|
|
if (!key.empty() || keepempty)
|
2002-11-04 00:15:56 +00:00
|
|
|
vec.push_back(key);
|
2007-11-10 13:44:50 +00:00
|
|
|
size_t const start = idx + delim.size();
|
2002-11-04 00:15:56 +00:00
|
|
|
keys = keys.substr(start);
|
2002-04-15 12:05:07 +00:00
|
|
|
}
|
2002-11-04 00:15:56 +00:00
|
|
|
return vec;
|
|
|
|
#else
|
2006-12-17 10:52:04 +00:00
|
|
|
typedef boost::char_separator<typename String::value_type> Separator;
|
|
|
|
typedef boost::tokenizer<Separator, typename String::const_iterator, String> Tokenizer;
|
|
|
|
Separator sep(delim.c_str());
|
|
|
|
Tokenizer tokens(str, sep);
|
|
|
|
return vector<String>(tokens.begin(), tokens.end());
|
2002-11-04 00:15:56 +00:00
|
|
|
#endif
|
2002-04-15 12:05:07 +00:00
|
|
|
}
|
|
|
|
|
2009-05-07 18:04:05 +00:00
|
|
|
|
|
|
|
template<typename String> const String
|
|
|
|
getStringFromVector(vector<String> const & vec, String const & delim)
|
|
|
|
{
|
|
|
|
String str;
|
|
|
|
typename vector<String>::const_iterator it = vec.begin();
|
|
|
|
typename vector<String>::const_iterator en = vec.end();
|
|
|
|
for (; it != en; ++it) {
|
|
|
|
String item = trim(*it);
|
|
|
|
if (item.empty())
|
|
|
|
continue;
|
|
|
|
if (!str.empty())
|
|
|
|
str += delim;
|
|
|
|
str += item;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-11-10 13:44:50 +00:00
|
|
|
} // namespace anon
|
2006-12-17 10:52:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
vector<string> const getVectorFromString(string const & str,
|
2008-07-14 16:09:59 +00:00
|
|
|
string const & delim,
|
|
|
|
bool keepempty)
|
2006-12-17 10:52:04 +00:00
|
|
|
{
|
2008-07-14 16:09:59 +00:00
|
|
|
return getVectorFromStringT<string>(str, delim, keepempty);
|
2006-12-17 10:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<docstring> const getVectorFromString(docstring const & str,
|
2008-07-14 16:09:59 +00:00
|
|
|
docstring const & delim,
|
|
|
|
bool keepempty)
|
2006-12-17 10:52:04 +00:00
|
|
|
{
|
2008-07-14 16:09:59 +00:00
|
|
|
return getVectorFromStringT<docstring>(str, delim, keepempty);
|
2006-12-17 10:52:04 +00:00
|
|
|
}
|
|
|
|
|
2002-11-04 00:15:56 +00:00
|
|
|
|
2002-04-15 12:05:07 +00:00
|
|
|
string const getStringFromVector(vector<string> const & vec,
|
2009-05-11 07:52:39 +00:00
|
|
|
string const & delim)
|
2002-04-15 12:05:07 +00:00
|
|
|
{
|
2009-05-07 18:04:05 +00:00
|
|
|
return getStringFromVector<string>(vec, delim);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring const getStringFromVector(vector<docstring> const & vec,
|
2009-05-11 07:52:39 +00:00
|
|
|
docstring const & delim)
|
2009-05-07 18:04:05 +00:00
|
|
|
{
|
|
|
|
return getStringFromVector<docstring>(vec, delim);
|
2002-04-15 12:05:07 +00:00
|
|
|
}
|
2003-05-13 09:48:57 +00:00
|
|
|
|
|
|
|
|
2006-04-20 09:55:45 +00:00
|
|
|
int findToken(char const * const str[], string const & search_token)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (str[i][0] && str[i] != search_token)
|
|
|
|
++i;
|
|
|
|
if (!str[i][0])
|
|
|
|
i = -1;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring bformat(docstring const & fmt, int arg1)
|
2005-01-06 15:40:49 +00:00
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
|
2007-11-10 13:44:50 +00:00
|
|
|
docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
|
|
|
|
return subst(str, from_ascii("%%"), from_ascii("%"));
|
2005-01-06 15:40:49 +00:00
|
|
|
}
|
|
|
|
|
2003-06-30 15:06:30 +00:00
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring bformat(docstring const & fmt, long arg1)
|
2003-06-30 15:06:30 +00:00
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
|
2007-11-10 13:44:50 +00:00
|
|
|
docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
|
|
|
|
return subst(str, from_ascii("%%"), from_ascii("%"));
|
2003-05-13 09:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring bformat(docstring const & fmt, unsigned int arg1)
|
2005-01-06 15:40:49 +00:00
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
|
2007-11-10 13:44:50 +00:00
|
|
|
docstring const str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
|
|
|
|
return subst(str, from_ascii("%%"), from_ascii("%"));
|
2005-01-06 15:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<>
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring bformat(docstring const & fmt, docstring arg1)
|
2003-05-13 09:48:57 +00:00
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
|
2007-11-10 13:44:50 +00:00
|
|
|
docstring const str = subst(fmt, from_ascii("%1$s"), arg1);
|
|
|
|
return subst(str, from_ascii("%%"), from_ascii("%"));
|
2003-05-13 09:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring bformat(docstring const & fmt, char * arg1)
|
2005-01-06 15:40:49 +00:00
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
|
2007-11-10 13:44:50 +00:00
|
|
|
docstring const str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
|
|
|
|
return subst(str, from_ascii("%%"), from_ascii("%"));
|
2005-01-06 15:40:49 +00:00
|
|
|
}
|
2006-09-13 11:18:13 +00:00
|
|
|
|
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring bformat(docstring const & fmt, docstring arg1, docstring arg2)
|
2003-05-13 09:48:57 +00:00
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
|
|
|
|
LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
|
2007-11-10 13:44:50 +00:00
|
|
|
docstring str = subst(fmt, from_ascii("%1$s"), arg1);
|
|
|
|
str = subst(str, from_ascii("%2$s"), arg2);
|
|
|
|
return subst(str, from_ascii("%%"), from_ascii("%"));
|
2003-05-13 09:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-02 09:38:14 +00:00
|
|
|
template<>
|
|
|
|
docstring bformat(docstring const & fmt, docstring arg1, int arg2)
|
|
|
|
{
|
|
|
|
LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
|
|
|
|
LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
|
|
|
|
docstring str = subst(fmt, from_ascii("%1$s"), arg1);
|
|
|
|
str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
|
|
|
|
return subst(str, from_ascii("%%"), from_ascii("%"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring bformat(docstring const & fmt, char const * arg1, docstring arg2)
|
2003-09-04 14:33:16 +00:00
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
|
|
|
|
LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
|
2007-11-10 13:44:50 +00:00
|
|
|
docstring str = subst(fmt, from_ascii("%1$s"), from_ascii(arg1));
|
|
|
|
str = subst(fmt, from_ascii("%2$s"), arg2);
|
|
|
|
return subst(str, from_ascii("%%"), from_ascii("%"));
|
2003-09-04 14:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring bformat(docstring const & fmt, int arg1, int arg2)
|
2003-05-19 14:10:32 +00:00
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(contains(fmt, from_ascii("%1$d")), /**/);
|
|
|
|
LASSERT(contains(fmt, from_ascii("%2$d")), /**/);
|
2007-11-10 13:44:50 +00:00
|
|
|
docstring str = subst(fmt, from_ascii("%1$d"), convert<docstring>(arg1));
|
|
|
|
str = subst(str, from_ascii("%2$d"), convert<docstring>(arg2));
|
|
|
|
return subst(str, from_ascii("%%"), from_ascii("%"));
|
2003-05-19 14:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring bformat(docstring const & fmt, docstring arg1, docstring arg2, docstring arg3)
|
2003-05-13 09:48:57 +00:00
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
|
|
|
|
LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
|
|
|
|
LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
|
2007-11-10 13:44:50 +00:00
|
|
|
docstring str = subst(fmt, from_ascii("%1$s"), arg1);
|
|
|
|
str = subst(str, from_ascii("%2$s"), arg2);
|
|
|
|
str = subst(str, from_ascii("%3$s"), arg3);
|
|
|
|
return subst(str, from_ascii("%%"), from_ascii("%"));
|
2003-05-13 09:48:57 +00:00
|
|
|
}
|
|
|
|
|
2003-06-30 15:06:30 +00:00
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring bformat(docstring const & fmt,
|
|
|
|
docstring arg1, docstring arg2, docstring arg3, docstring arg4)
|
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(contains(fmt, from_ascii("%1$s")), /**/);
|
|
|
|
LASSERT(contains(fmt, from_ascii("%2$s")), /**/);
|
|
|
|
LASSERT(contains(fmt, from_ascii("%3$s")), /**/);
|
|
|
|
LASSERT(contains(fmt, from_ascii("%4$s")), /**/);
|
2007-11-10 13:44:50 +00:00
|
|
|
docstring str = subst(fmt, from_ascii("%1$s"), arg1);
|
|
|
|
str = subst(str, from_ascii("%2$s"), arg2);
|
|
|
|
str = subst(str, from_ascii("%3$s"), arg3);
|
|
|
|
str = subst(str, from_ascii("%4$s"), arg4);
|
|
|
|
return subst(str, from_ascii("%%"), from_ascii("%"));
|
2003-06-30 15:06:30 +00:00
|
|
|
}
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
} // namespace support
|
|
|
|
} // namespace lyx
|