lyx_mirror/src/support/docstring.C

108 lines
2.2 KiB
C++
Raw Normal View History

/**
* \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<lyx::char_type> const ucs4 =
utf8_to_ucs4(utf8.data(), utf8.size());
return docstring(ucs4.begin(), ucs4.end());
}
std::string const to_utf8(docstring const & ucs4)
{
std::vector<char> const utf8 =
ucs4_to_utf8(ucs4.data(), ucs4.size());
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';
}
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;
}