bc6a2387ef0ff9658ab0e776e76ff08291533e88
[fms.git] / src / unicode / unicodeformatter.cpp
1 #include "../../include/unicode/unicodeformatter.h"\r
2 #include "../../include/unicode/utfconversion.h"\r
3 \r
4 std::wstring UnicodeFormatter::m_unicodenewline=L"\n";\r
5 std::wstring::value_type UnicodeFormatter::m_unicodewhitespace[]={0x0009,0x000A,0x000B,0x000C,0x000D,\r
6                                                                                                 0x0020,0x0085,0x00A0,0x1680,0x180E,\r
7                                                                                                 0x2000,0x2001,0x2002,0x2003,0x2004,\r
8                                                                                                 0x2005,0x2006,0x2007,0x2008,0x2009,\r
9                                                                                                 0x200A,0x200B,0x2029,0x202F,0x205F,\r
10                                                                                                 0x3000,0xFEFF};\r
11 \r
12 const bool UnicodeFormatter::LineWrap(const std::string &utf8input, const int linelength, const std::string &ignorechars, std::string &utf8output)\r
13 {\r
14         std::wstring wcstring=L"";\r
15         std::wstring wcignorechars=L"";\r
16 \r
17         if(UTFConversion::FromUTF8(utf8input,wcstring) && UTFConversion::FromUTF8(ignorechars,wcignorechars))\r
18         {\r
19 \r
20                 std::wstring::size_type currentpos=0;\r
21                 std::wstring::size_type lastnewlinepos=0;\r
22                 std::wstring::size_type whitespacepos=0;\r
23 \r
24                 while(currentpos+linelength<wcstring.length())\r
25                 {\r
26                         if(ignorechars.size()==0 || wcstring.find_first_of(wcignorechars,currentpos)!=currentpos)\r
27                         {\r
28                                 lastnewlinepos=wcstring.rfind(m_unicodenewline,currentpos+linelength);\r
29                                 // newline found within line length - we don't need to wrap\r
30                                 if(lastnewlinepos!=std::wstring::npos && lastnewlinepos>=currentpos)\r
31                                 {\r
32                                         currentpos=lastnewlinepos+1;\r
33                                 }\r
34                                 // newline doesn't exist at all - force one in\r
35                                 else if(lastnewlinepos==std::wstring::npos)\r
36                                 {\r
37                                         wcstring.insert(currentpos+linelength,m_unicodenewline);\r
38                                         currentpos+=linelength+m_unicodenewline.length();\r
39                                 }\r
40                                 else\r
41                                 {\r
42                                         whitespacepos=wcstring.find_last_of(m_unicodewhitespace,currentpos+linelength);\r
43                                         // whitespace found within line length - erase whitespace and insert newline\r
44                                         if(whitespacepos!=std::wstring::npos && whitespacepos>=currentpos)\r
45                                         {\r
46                                                 wcstring.erase(whitespacepos,1);\r
47                                                 wcstring.insert(whitespacepos,m_unicodenewline);\r
48                                                 currentpos=whitespacepos+m_unicodenewline.length();\r
49                                         }\r
50                                         // whitespace not found within line length - force newline at line length\r
51                                         else\r
52                                         {\r
53                                                 wcstring.insert(currentpos+linelength,m_unicodenewline);\r
54                                                 currentpos+=linelength+m_unicodenewline.length();\r
55                                         }\r
56                                 }\r
57                         }\r
58                         else\r
59                         {\r
60                                 currentpos=wcstring.find(m_unicodenewline,currentpos+1);\r
61                                 if(currentpos==std::string::npos)\r
62                                 {\r
63                                         currentpos=wcstring.size();\r
64                                 }\r
65                                 currentpos++;\r
66                         }\r
67                 }\r
68 \r
69                 if(UTFConversion::ToUTF8(wcstring,utf8output))\r
70                 {\r
71                         return true;\r
72                 }\r
73 \r
74         }\r
75 \r
76         return false;\r
77 \r
78 }\r