version 0.3.25
[fms.git] / src / unicode / unicodeformatter.cpp
diff --git a/src/unicode/unicodeformatter.cpp b/src/unicode/unicodeformatter.cpp
new file mode 100644 (file)
index 0000000..c89cc7c
--- /dev/null
@@ -0,0 +1,78 @@
+#include "../../include/unicode/unicodeformatter.h"\r
+#include "../../include/unicode/utfconversion.h"\r
+\r
+std::wstring UnicodeFormatter::m_unicodenewline=L"\n";\r
+wchar_t UnicodeFormatter::m_unicodewhitespace[]={0x0009,0x000A,0x000B,0x000C,0x000D,\r
+                                                                                               0x0020,0x0085,0x00A0,0x1680,0x180E,\r
+                                                                                               0x2000,0x2001,0x2002,0x2003,0x2004,\r
+                                                                                               0x2005,0x2006,0x2007,0x2008,0x2009,\r
+                                                                                               0x200A,0x200B,0x2029,0x202F,0x205F,\r
+                                                                                               0x3000,0xFEFF};\r
+\r
+const bool UnicodeFormatter::LineWrap(const std::string &utf8input, const int linelength, const std::string &ignorechars, std::string &utf8output)\r
+{\r
+       std::wstring wcstring;\r
+       std::wstring wcignorechars;\r
+\r
+       if(UTFConversion::FromUTF8(utf8input,wcstring) && UTFConversion::FromUTF8(ignorechars,wcignorechars))\r
+       {\r
+\r
+               std::wstring::size_type currentpos=0;\r
+               std::wstring::size_type lastnewlinepos=0;\r
+               std::wstring::size_type whitespacepos=0;\r
+\r
+               while(currentpos+linelength<wcstring.length())\r
+               {\r
+                       if(ignorechars.size()==0 || wcstring.find_first_of(wcignorechars,currentpos)!=currentpos)\r
+                       {\r
+                               lastnewlinepos=wcstring.rfind(m_unicodenewline,currentpos+linelength);\r
+                               // newline found within line length - we don't need to wrap\r
+                               if(lastnewlinepos!=std::wstring::npos && lastnewlinepos>=currentpos)\r
+                               {\r
+                                       currentpos=lastnewlinepos+1;\r
+                               }\r
+                               // newline doesn't exist at all - force one in\r
+                               else if(lastnewlinepos==std::wstring::npos)\r
+                               {\r
+                                       wcstring.insert(currentpos+linelength,m_unicodenewline);\r
+                                       currentpos+=linelength+m_unicodenewline.length();\r
+                               }\r
+                               else\r
+                               {\r
+                                       whitespacepos=wcstring.find_last_of(m_unicodewhitespace,currentpos+linelength);\r
+                                       // whitespace found within line length - erase whitespace and insert newline\r
+                                       if(whitespacepos!=std::wstring::npos && whitespacepos>=currentpos)\r
+                                       {\r
+                                               wcstring.erase(whitespacepos,1);\r
+                                               wcstring.insert(whitespacepos,m_unicodenewline);\r
+                                               currentpos=whitespacepos+m_unicodenewline.length();\r
+                                       }\r
+                                       // whitespace not found within line length - force newline at line length\r
+                                       else\r
+                                       {\r
+                                               wcstring.insert(currentpos+linelength,m_unicodenewline);\r
+                                               currentpos+=linelength+m_unicodenewline.length();\r
+                                       }\r
+                               }\r
+                       }\r
+                       else\r
+                       {\r
+                               currentpos=wcstring.find(m_unicodenewline,currentpos+1);\r
+                               if(currentpos==std::string::npos)\r
+                               {\r
+                                       currentpos=wcstring.size();\r
+                               }\r
+                               currentpos++;\r
+                       }\r
+               }\r
+\r
+               if(UTFConversion::ToUTF8(wcstring,utf8output))\r
+               {\r
+                       return true;\r
+               }\r
+\r
+       }\r
+\r
+       return false;\r
+\r
+}\r