X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Funicode%2Funicodeformatter.cpp;fp=src%2Funicode%2Funicodeformatter.cpp;h=c89cc7c65ceffdbb95927fc35b2e5413df9b4a11;hb=56f67ecca96efc7b72d03c95c8c42cfb66e31468;hp=0000000000000000000000000000000000000000;hpb=f2545574af789b63fc655decfe31a3d9f1b30504;p=fms.git diff --git a/src/unicode/unicodeformatter.cpp b/src/unicode/unicodeformatter.cpp new file mode 100644 index 0000000..c89cc7c --- /dev/null +++ b/src/unicode/unicodeformatter.cpp @@ -0,0 +1,78 @@ +#include "../../include/unicode/unicodeformatter.h" +#include "../../include/unicode/utfconversion.h" + +std::wstring UnicodeFormatter::m_unicodenewline=L"\n"; +wchar_t UnicodeFormatter::m_unicodewhitespace[]={0x0009,0x000A,0x000B,0x000C,0x000D, + 0x0020,0x0085,0x00A0,0x1680,0x180E, + 0x2000,0x2001,0x2002,0x2003,0x2004, + 0x2005,0x2006,0x2007,0x2008,0x2009, + 0x200A,0x200B,0x2029,0x202F,0x205F, + 0x3000,0xFEFF}; + +const bool UnicodeFormatter::LineWrap(const std::string &utf8input, const int linelength, const std::string &ignorechars, std::string &utf8output) +{ + std::wstring wcstring; + std::wstring wcignorechars; + + if(UTFConversion::FromUTF8(utf8input,wcstring) && UTFConversion::FromUTF8(ignorechars,wcignorechars)) + { + + std::wstring::size_type currentpos=0; + std::wstring::size_type lastnewlinepos=0; + std::wstring::size_type whitespacepos=0; + + while(currentpos+linelength=currentpos) + { + currentpos=lastnewlinepos+1; + } + // newline doesn't exist at all - force one in + else if(lastnewlinepos==std::wstring::npos) + { + wcstring.insert(currentpos+linelength,m_unicodenewline); + currentpos+=linelength+m_unicodenewline.length(); + } + else + { + whitespacepos=wcstring.find_last_of(m_unicodewhitespace,currentpos+linelength); + // whitespace found within line length - erase whitespace and insert newline + if(whitespacepos!=std::wstring::npos && whitespacepos>=currentpos) + { + wcstring.erase(whitespacepos,1); + wcstring.insert(whitespacepos,m_unicodenewline); + currentpos=whitespacepos+m_unicodenewline.length(); + } + // whitespace not found within line length - force newline at line length + else + { + wcstring.insert(currentpos+linelength,m_unicodenewline); + currentpos+=linelength+m_unicodenewline.length(); + } + } + } + else + { + currentpos=wcstring.find(m_unicodenewline,currentpos+1); + if(currentpos==std::string::npos) + { + currentpos=wcstring.size(); + } + currentpos++; + } + } + + if(UTFConversion::ToUTF8(wcstring,utf8output)) + { + return true; + } + + } + + return false; + +}