2006-09-01 15:41:38 +00:00
|
|
|
/**
|
|
|
|
* \file docstring.C
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Georg Baum
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "docstring.h"
|
|
|
|
#include "unicode.h"
|
|
|
|
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
docstring const from_ascii(char const * ascii)
|
|
|
|
{
|
|
|
|
docstring s;
|
|
|
|
for (char const * c = ascii; *c; ++c) {
|
|
|
|
BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
|
|
|
|
s.push_back(*c);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring const from_ascii(std::string const & ascii)
|
|
|
|
{
|
|
|
|
int const len = ascii.length();
|
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
BOOST_ASSERT(static_cast<unsigned char>(ascii[i]) < 0x80);
|
|
|
|
return docstring(ascii.begin(), ascii.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring const from_utf8(std::string const & utf8)
|
|
|
|
{
|
|
|
|
std::vector<boost::uint32_t> const ucs4 =
|
2006-09-10 18:34:24 +00:00
|
|
|
utf8_to_ucs4(utf8.data(), utf8.size());
|
2006-09-01 15:41:38 +00:00
|
|
|
return docstring(ucs4.begin(), ucs4.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string const to_utf8(docstring const & ucs4)
|
|
|
|
{
|
|
|
|
std::vector<char> const utf8 =
|
2006-09-10 18:34:24 +00:00
|
|
|
ucs4_to_utf8(ucs4.data(), ucs4.size());
|
2006-09-01 15:41:38 +00:00
|
|
|
return std::string(utf8.begin(), utf8.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator==(lyx::docstring const & l, char const * r)
|
|
|
|
{
|
|
|
|
int const len = l.length();
|
|
|
|
for (int i = 0; i < len; ++i) {
|
|
|
|
BOOST_ASSERT(static_cast<unsigned char>(r[i]) < 0x80);
|
|
|
|
if (!r[i])
|
|
|
|
return false;
|
|
|
|
if (l[i] != lyx::docstring::value_type(r[i]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return r[len] == '\0';
|
|
|
|
}
|
2006-09-11 07:13:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
lyx::docstring operator+(lyx::docstring const & l, char const * r)
|
|
|
|
{
|
|
|
|
lyx::docstring s(l);
|
|
|
|
for (char const * c = r; *c; ++c) {
|
|
|
|
BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
|
|
|
|
s.push_back(*c);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyx::docstring operator+(char const * l, lyx::docstring const & r)
|
|
|
|
{
|
|
|
|
lyx::docstring s;
|
|
|
|
for (char const * c = l; *c; ++c) {
|
|
|
|
BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
|
|
|
|
s.push_back(*c);
|
|
|
|
}
|
|
|
|
s += r;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyx::docstring operator+(lyx::docstring const & l, char r)
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
|
|
|
|
return l + lyx::docstring::value_type(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyx::docstring operator+(char l, lyx::docstring const & r)
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
|
|
|
|
return lyx::docstring::value_type(l) + r;
|
|
|
|
}
|