generic_codecvt.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. //
  2. // Copyright (c) 2015 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #ifndef BOOST_LOCALE_GENERIC_CODECVT_HPP
  9. #define BOOST_LOCALE_GENERIC_CODECVT_HPP
  10. #include <boost/locale/utf.hpp>
  11. #include <boost/cstdint.hpp>
  12. #include <boost/static_assert.hpp>
  13. #include <locale>
  14. namespace boost {
  15. namespace locale {
  16. #ifndef BOOST_LOCALE_DOXYGEN
  17. //
  18. // Make sure that mbstate can keep 16 bit of UTF-16 sequence
  19. //
  20. BOOST_STATIC_ASSERT(sizeof(std::mbstate_t)>=2);
  21. #endif
  22. #if defined(_MSC_VER) && _MSC_VER < 1700
  23. // up to MSVC 11 (2012) do_length is non-standard it counts wide characters instead of narrow and does not change mbstate
  24. #define BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
  25. #endif
  26. ///
  27. /// \brief A base class that used to define constants for generic_codecvt
  28. ///
  29. class generic_codecvt_base {
  30. public:
  31. ///
  32. /// Initail state for converting to or from unicode code points, used by initial_state in derived classes
  33. ///
  34. enum initial_convertion_state {
  35. to_unicode_state, ///< The state would be used by to_unicode functions
  36. from_unicode_state ///< The state would be used by from_unicode functions
  37. };
  38. };
  39. ///
  40. /// \brief Geneneric generic codecvt facet, various stateless encodings to UTF-16 and UTF-32 using wchar_t, char32_t and char16_t
  41. ///
  42. /// Implementations should dervide from this class defining itself as CodecvtImpl and provide following members
  43. ///
  44. /// - `state_type` - a type of special object that allows to store intermediate cached data, for example `iconv_t` descriptor
  45. /// - `state_type initial_state(generic_codecvt_base::initial_convertion_state direction) const` - member function that creates initial state
  46. /// - `int max_encoding_length() const` - a maximal length that one Unicode code point is represented, for UTF-8 for example it is 4 from ISO-8859-1 it is 1
  47. /// - `utf::code_point to_unicode(state_type &state,char const *&begin,char const *end)` - extract first code point from the text in range [begin,end), in case of success begin would point to the next character sequence to be encoded to next code point, in case of incomplete sequence - utf::incomplete shell be returned, and in case of invalid input sequence utf::illegal shell be returned and begin would remain unmodified
  48. /// - `utf::code_point from_unicode(state_type &state,utf::code_point u,char *begin,char const *end)` - convert a unicode code point `u` into a character seqnece at [begin,end). Return the length of the sequence in case of success, utf::incomplete in case of not enough room to encode the code point of utf::illegal in case conversion can not be performed
  49. ///
  50. ///
  51. /// For example implementaion of codecvt for latin1/ISO-8859-1 character set
  52. ///
  53. /// \code
  54. ///
  55. /// template<typename CharType>
  56. /// class latin1_codecvt :boost::locale::generic_codecvt<CharType,latin1_codecvt<CharType> >
  57. /// {
  58. /// public:
  59. ///
  60. /// /* Standard codecvt constructor */
  61. /// latin1_codecvt(size_t refs = 0) : boost::locale::generic_codecvt<CharType,latin1_codecvt<CharType> >(refs)
  62. /// {
  63. /// }
  64. ///
  65. /// /* State is unused but required by generic_codecvt */
  66. /// struct state_type {};
  67. ///
  68. /// state_type initial_state(generic_codecvt_base::initial_convertion_state /*unused*/) const
  69. /// {
  70. /// return state_type();
  71. /// }
  72. ///
  73. /// int max_encoding_length() const
  74. /// {
  75. /// return 1;
  76. /// }
  77. ///
  78. /// boost::locale::utf::code_point to_unicode(state_type &,char const *&begin,char const *end) const
  79. /// {
  80. /// if(begin == end)
  81. /// return boost::locale::utf::incomplete;
  82. /// return *begin++;
  83. /// }
  84. ///
  85. /// boost::locale::utf::code_point from_unicode(state_type &,boost::locale::utf::code_point u,char *begin,char const *end) const
  86. /// {
  87. /// if(u >= 256)
  88. /// return boost::locale::utf::illegal;
  89. /// if(begin == end)
  90. /// return boost::locale::utf::incomplete;
  91. /// *begin = u;
  92. /// return 1;
  93. /// }
  94. /// };
  95. ///
  96. /// \endcode
  97. ///
  98. /// When external tools used for encoding conversion, the `state_type` is useful to save objects used for conversions. For example,
  99. /// icu::UConverter can be saved in such a state for an efficient use:
  100. ///
  101. /// \code
  102. /// template<typename CharType>
  103. /// class icu_codecvt :boost::locale::generic_codecvt<CharType,icu_codecvt<CharType> >
  104. /// {
  105. /// public:
  106. ///
  107. /// /* Standard codecvt constructor */
  108. /// icu_codecvt(std::string const &name,refs = 0) :
  109. /// boost::locale::generic_codecvt<CharType,latin1_codecvt<CharType> >(refs)
  110. /// { ... }
  111. ///
  112. /// /* State is unused but required by generic_codecvt */
  113. /// struct std::unique_ptr<UConverter,void (*)(UConverter*)> state_type;
  114. ///
  115. /// state_type &&initial_state(generic_codecvt_base::initial_convertion_state /*unused*/) const
  116. /// {
  117. /// UErrorCode err = U_ZERO_ERROR;
  118. /// state_type ptr(ucnv_safeClone(converter_,0,0,&err,ucnv_close);
  119. /// return std::move(ptr);
  120. /// }
  121. ///
  122. /// boost::locale::utf::code_point to_unicode(state_type &ptr,char const *&begin,char const *end) const
  123. /// {
  124. /// UErrorCode err = U_ZERO_ERROR;
  125. /// boost::locale::utf::code_point cp = ucnv_getNextUChar(ptr.get(),&begin,end,&err);
  126. /// ...
  127. /// }
  128. /// ...
  129. /// };
  130. /// \endcode
  131. ///
  132. ///
  133. template<typename CharType,typename CodecvtImpl,int CharSize=sizeof(CharType)>
  134. class generic_codecvt;
  135. ///
  136. /// \brief UTF-16 to/from UTF-8 codecvt facet to use with char16_t or wchar_t on Windows
  137. ///
  138. /// Note in order to fit the requirements of usability by std::wfstream it uses mbstate_t
  139. /// to handle intermediate states in handling of variable length UTF-16 sequences
  140. ///
  141. /// Its member functions implement standard virtual functions of basic codecvt
  142. ///
  143. template<typename CharType,typename CodecvtImpl>
  144. class generic_codecvt<CharType,CodecvtImpl,2> : public std::codecvt<CharType,char,std::mbstate_t>, public generic_codecvt_base
  145. {
  146. public:
  147. typedef CharType uchar;
  148. generic_codecvt(size_t refs = 0) :
  149. std::codecvt<CharType,char,std::mbstate_t>(refs)
  150. {
  151. }
  152. CodecvtImpl const &implementation() const
  153. {
  154. return *static_cast<CodecvtImpl const *>(this);
  155. }
  156. protected:
  157. virtual std::codecvt_base::result do_unshift(std::mbstate_t &s,char *from,char * /*to*/,char *&next) const
  158. {
  159. boost::uint16_t &state = *reinterpret_cast<boost::uint16_t *>(&s);
  160. #ifdef DEBUG_CODECVT
  161. std::cout << "Entering unshift " << std::hex << state << std::dec << std::endl;
  162. #endif
  163. if(state != 0)
  164. return std::codecvt_base::error;
  165. next=from;
  166. return std::codecvt_base::ok;
  167. }
  168. virtual int do_encoding() const throw()
  169. {
  170. return 0;
  171. }
  172. virtual int do_max_length() const throw()
  173. {
  174. return implementation().max_encoding_length();
  175. }
  176. virtual bool do_always_noconv() const throw()
  177. {
  178. return false;
  179. }
  180. virtual int
  181. do_length( std::mbstate_t
  182. #ifdef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
  183. const
  184. #endif
  185. &std_state,
  186. char const *from,
  187. char const *from_end,
  188. size_t max) const
  189. {
  190. #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
  191. char const *save_from = from;
  192. boost::uint16_t &state = *reinterpret_cast<boost::uint16_t *>(&std_state);
  193. #else
  194. size_t save_max = max;
  195. boost::uint16_t state = *reinterpret_cast<boost::uint16_t const *>(&std_state);
  196. #endif
  197. typedef typename CodecvtImpl::state_type state_type;
  198. state_type cvt_state = implementation().initial_state(generic_codecvt_base::to_unicode_state);
  199. while(max > 0 && from < from_end){
  200. char const *prev_from = from;
  201. boost::uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
  202. if(ch==boost::locale::utf::incomplete || ch==boost::locale::utf::illegal) {
  203. from = prev_from;
  204. break;
  205. }
  206. max --;
  207. if(ch > 0xFFFF) {
  208. if(state == 0) {
  209. from = prev_from;
  210. state = 1;
  211. }
  212. else {
  213. state = 0;
  214. }
  215. }
  216. }
  217. #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
  218. return from - save_from;
  219. #else
  220. return save_max - max;
  221. #endif
  222. }
  223. virtual std::codecvt_base::result
  224. do_in( std::mbstate_t &std_state,
  225. char const *from,
  226. char const *from_end,
  227. char const *&from_next,
  228. uchar *to,
  229. uchar *to_end,
  230. uchar *&to_next) const
  231. {
  232. std::codecvt_base::result r=std::codecvt_base::ok;
  233. // mbstate_t is POD type and should be initialized to 0 (i.a. state = stateT())
  234. // according to standard. We use it to keep a flag 0/1 for surrogate pair writing
  235. //
  236. // if 0 no code above >0xFFFF observed, of 1 a code above 0xFFFF observerd
  237. // and first pair is written, but no input consumed
  238. boost::uint16_t &state = *reinterpret_cast<boost::uint16_t *>(&std_state);
  239. typedef typename CodecvtImpl::state_type state_type;
  240. state_type cvt_state = implementation().initial_state(generic_codecvt_base::to_unicode_state);
  241. while(to < to_end && from < from_end)
  242. {
  243. #ifdef DEBUG_CODECVT
  244. std::cout << "Entering IN--------------" << std::endl;
  245. std::cout << "State " << std::hex << state <<std::endl;
  246. std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
  247. #endif
  248. char const *from_saved = from;
  249. uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
  250. if(ch==boost::locale::utf::illegal) {
  251. from = from_saved;
  252. r=std::codecvt_base::error;
  253. break;
  254. }
  255. if(ch==boost::locale::utf::incomplete) {
  256. from = from_saved;
  257. r=std::codecvt_base::partial;
  258. break;
  259. }
  260. // Normal codepoints go direcly to stream
  261. if(ch <= 0xFFFF) {
  262. *to++=ch;
  263. }
  264. else {
  265. // for other codepoints we do following
  266. //
  267. // 1. We can't consume our input as we may find ourselfs
  268. // in state where all input consumed but not all output written,i.e. only
  269. // 1st pair is written
  270. // 2. We only write first pair and mark this in the state, we also revert back
  271. // the from pointer in order to make sure this codepoint would be read
  272. // once again and then we would consume our input together with writing
  273. // second surrogate pair
  274. ch-=0x10000;
  275. boost::uint16_t vh = ch >> 10;
  276. boost::uint16_t vl = ch & 0x3FF;
  277. boost::uint16_t w1 = vh + 0xD800;
  278. boost::uint16_t w2 = vl + 0xDC00;
  279. if(state == 0) {
  280. from = from_saved;
  281. *to++ = w1;
  282. state = 1;
  283. }
  284. else {
  285. *to++ = w2;
  286. state = 0;
  287. }
  288. }
  289. }
  290. from_next=from;
  291. to_next=to;
  292. if(r == std::codecvt_base::ok && (from!=from_end || state!=0))
  293. r = std::codecvt_base::partial;
  294. #ifdef DEBUG_CODECVT
  295. std::cout << "Returning ";
  296. switch(r) {
  297. case std::codecvt_base::ok:
  298. std::cout << "ok" << std::endl;
  299. break;
  300. case std::codecvt_base::partial:
  301. std::cout << "partial" << std::endl;
  302. break;
  303. case std::codecvt_base::error:
  304. std::cout << "error" << std::endl;
  305. break;
  306. default:
  307. std::cout << "other" << std::endl;
  308. break;
  309. }
  310. std::cout << "State " << std::hex << state <<std::endl;
  311. std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
  312. #endif
  313. return r;
  314. }
  315. virtual std::codecvt_base::result
  316. do_out( std::mbstate_t &std_state,
  317. uchar const *from,
  318. uchar const *from_end,
  319. uchar const *&from_next,
  320. char *to,
  321. char *to_end,
  322. char *&to_next) const
  323. {
  324. std::codecvt_base::result r=std::codecvt_base::ok;
  325. // mbstate_t is POD type and should be initialized to 0 (i.a. state = stateT())
  326. // according to standard. We assume that sizeof(mbstate_t) >=2 in order
  327. // to be able to store first observerd surrogate pair
  328. //
  329. // State: state!=0 - a first surrogate pair was observerd (state = first pair),
  330. // we expect the second one to come and then zero the state
  331. ///
  332. boost::uint16_t &state = *reinterpret_cast<boost::uint16_t *>(&std_state);
  333. typedef typename CodecvtImpl::state_type state_type;
  334. state_type cvt_state = implementation().initial_state(generic_codecvt_base::from_unicode_state);
  335. while(to < to_end && from < from_end)
  336. {
  337. #ifdef DEBUG_CODECVT
  338. std::cout << "Entering OUT --------------" << std::endl;
  339. std::cout << "State " << std::hex << state <<std::endl;
  340. std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
  341. #endif
  342. boost::uint32_t ch=0;
  343. if(state != 0) {
  344. // if the state idecates that 1st surrogate pair was written
  345. // we should make sure that the second one that comes is actually
  346. // second surrogate
  347. boost::uint16_t w1 = state;
  348. boost::uint16_t w2 = *from;
  349. // we don't forward from as writing may fail to incomplete or
  350. // partial conversion
  351. if(0xDC00 <= w2 && w2<=0xDFFF) {
  352. boost::uint16_t vh = w1 - 0xD800;
  353. boost::uint16_t vl = w2 - 0xDC00;
  354. ch=((uint32_t(vh) << 10) | vl) + 0x10000;
  355. }
  356. else {
  357. // Invalid surrogate
  358. r=std::codecvt_base::error;
  359. break;
  360. }
  361. }
  362. else {
  363. ch = *from;
  364. if(0xD800 <= ch && ch<=0xDBFF) {
  365. // if this is a first surrogate pair we put
  366. // it into the state and consume it, note we don't
  367. // go forward as it should be illegal so we increase
  368. // the from pointer manually
  369. state = ch;
  370. from++;
  371. continue;
  372. }
  373. else if(0xDC00 <= ch && ch<=0xDFFF) {
  374. // if we observe second surrogate pair and
  375. // first only may be expected we should break from the loop with error
  376. // as it is illegal input
  377. r=std::codecvt_base::error;
  378. break;
  379. }
  380. }
  381. if(!boost::locale::utf::is_valid_codepoint(ch)) {
  382. r=std::codecvt_base::error;
  383. break;
  384. }
  385. boost::uint32_t len = implementation().from_unicode(cvt_state,ch,to,to_end);
  386. if(len == boost::locale::utf::incomplete) {
  387. r=std::codecvt_base::partial;
  388. break;
  389. }
  390. else if(len == boost::locale::utf::illegal) {
  391. r=std::codecvt_base::error;
  392. break;
  393. }
  394. else
  395. to+= len;
  396. state = 0;
  397. from++;
  398. }
  399. from_next=from;
  400. to_next=to;
  401. if(r==std::codecvt_base::ok && from!=from_end)
  402. r = std::codecvt_base::partial;
  403. #ifdef DEBUG_CODECVT
  404. std::cout << "Returning ";
  405. switch(r) {
  406. case std::codecvt_base::ok:
  407. std::cout << "ok" << std::endl;
  408. break;
  409. case std::codecvt_base::partial:
  410. std::cout << "partial" << std::endl;
  411. break;
  412. case std::codecvt_base::error:
  413. std::cout << "error" << std::endl;
  414. break;
  415. default:
  416. std::cout << "other" << std::endl;
  417. break;
  418. }
  419. std::cout << "State " << std::hex << state <<std::endl;
  420. std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
  421. #endif
  422. return r;
  423. }
  424. };
  425. ///
  426. /// \brief UTF-32 to/from UTF-8 codecvt facet to use with char32_t or wchar_t on POSIX platforms
  427. ///
  428. /// Its member functions implement standard virtual functions of basic codecvt.
  429. /// mbstate_t is not used for UTF-32 handling due to fixed length encoding
  430. ///
  431. template<typename CharType,typename CodecvtImpl>
  432. class generic_codecvt<CharType,CodecvtImpl,4> : public std::codecvt<CharType,char,std::mbstate_t>, public generic_codecvt_base
  433. {
  434. public:
  435. typedef CharType uchar;
  436. generic_codecvt(size_t refs = 0) :
  437. std::codecvt<CharType,char,std::mbstate_t>(refs)
  438. {
  439. }
  440. CodecvtImpl const &implementation() const
  441. {
  442. return *static_cast<CodecvtImpl const *>(this);
  443. }
  444. protected:
  445. virtual std::codecvt_base::result do_unshift(std::mbstate_t &/*s*/,char *from,char * /*to*/,char *&next) const
  446. {
  447. next=from;
  448. return std::codecvt_base::ok;
  449. }
  450. virtual int do_encoding() const throw()
  451. {
  452. return 0;
  453. }
  454. virtual int do_max_length() const throw()
  455. {
  456. return implementation().max_encoding_length();
  457. }
  458. virtual bool do_always_noconv() const throw()
  459. {
  460. return false;
  461. }
  462. virtual int
  463. do_length( std::mbstate_t
  464. #ifdef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
  465. const
  466. #endif
  467. &/*state*/,
  468. char const *from,
  469. char const *from_end,
  470. size_t max) const
  471. {
  472. #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
  473. char const *start_from = from;
  474. #else
  475. size_t save_max = max;
  476. #endif
  477. typedef typename CodecvtImpl::state_type state_type;
  478. state_type cvt_state = implementation().initial_state(generic_codecvt_base::to_unicode_state);
  479. while(max > 0 && from < from_end){
  480. char const *save_from = from;
  481. boost::uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
  482. if(ch==boost::locale::utf::incomplete || ch==boost::locale::utf::illegal) {
  483. from = save_from;
  484. break;
  485. }
  486. max--;
  487. }
  488. #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
  489. return from - start_from;
  490. #else
  491. return save_max - max;
  492. #endif
  493. }
  494. virtual std::codecvt_base::result
  495. do_in( std::mbstate_t &/*state*/,
  496. char const *from,
  497. char const *from_end,
  498. char const *&from_next,
  499. uchar *to,
  500. uchar *to_end,
  501. uchar *&to_next) const
  502. {
  503. std::codecvt_base::result r=std::codecvt_base::ok;
  504. // mbstate_t is POD type and should be initialized to 0 (i.a. state = stateT())
  505. // according to standard. We use it to keep a flag 0/1 for surrogate pair writing
  506. //
  507. // if 0 no code above >0xFFFF observed, of 1 a code above 0xFFFF observerd
  508. // and first pair is written, but no input consumed
  509. typedef typename CodecvtImpl::state_type state_type;
  510. state_type cvt_state = implementation().initial_state(generic_codecvt_base::to_unicode_state);
  511. while(to < to_end && from < from_end)
  512. {
  513. #ifdef DEBUG_CODECVT
  514. std::cout << "Entering IN--------------" << std::endl;
  515. std::cout << "State " << std::hex << state <<std::endl;
  516. std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
  517. #endif
  518. char const *from_saved = from;
  519. uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
  520. if(ch==boost::locale::utf::illegal) {
  521. r=std::codecvt_base::error;
  522. from = from_saved;
  523. break;
  524. }
  525. if(ch==boost::locale::utf::incomplete) {
  526. r=std::codecvt_base::partial;
  527. from=from_saved;
  528. break;
  529. }
  530. *to++=ch;
  531. }
  532. from_next=from;
  533. to_next=to;
  534. if(r == std::codecvt_base::ok && from!=from_end)
  535. r = std::codecvt_base::partial;
  536. #ifdef DEBUG_CODECVT
  537. std::cout << "Returning ";
  538. switch(r) {
  539. case std::codecvt_base::ok:
  540. std::cout << "ok" << std::endl;
  541. break;
  542. case std::codecvt_base::partial:
  543. std::cout << "partial" << std::endl;
  544. break;
  545. case std::codecvt_base::error:
  546. std::cout << "error" << std::endl;
  547. break;
  548. default:
  549. std::cout << "other" << std::endl;
  550. break;
  551. }
  552. std::cout << "State " << std::hex << state <<std::endl;
  553. std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
  554. #endif
  555. return r;
  556. }
  557. virtual std::codecvt_base::result
  558. do_out( std::mbstate_t &/*std_state*/,
  559. uchar const *from,
  560. uchar const *from_end,
  561. uchar const *&from_next,
  562. char *to,
  563. char *to_end,
  564. char *&to_next) const
  565. {
  566. std::codecvt_base::result r=std::codecvt_base::ok;
  567. typedef typename CodecvtImpl::state_type state_type;
  568. state_type cvt_state = implementation().initial_state(generic_codecvt_base::from_unicode_state);
  569. while(to < to_end && from < from_end)
  570. {
  571. #ifdef DEBUG_CODECVT
  572. std::cout << "Entering OUT --------------" << std::endl;
  573. std::cout << "State " << std::hex << state <<std::endl;
  574. std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
  575. #endif
  576. boost::uint32_t ch=0;
  577. ch = *from;
  578. if(!boost::locale::utf::is_valid_codepoint(ch)) {
  579. r=std::codecvt_base::error;
  580. break;
  581. }
  582. boost::uint32_t len = implementation().from_unicode(cvt_state,ch,to,to_end);
  583. if(len == boost::locale::utf::incomplete) {
  584. r=std::codecvt_base::partial;
  585. break;
  586. }
  587. else if(len == boost::locale::utf::illegal) {
  588. r=std::codecvt_base::error;
  589. break;
  590. }
  591. to+=len;
  592. from++;
  593. }
  594. from_next=from;
  595. to_next=to;
  596. if(r==std::codecvt_base::ok && from!=from_end)
  597. r = std::codecvt_base::partial;
  598. #ifdef DEBUG_CODECVT
  599. std::cout << "Returning ";
  600. switch(r) {
  601. case std::codecvt_base::ok:
  602. std::cout << "ok" << std::endl;
  603. break;
  604. case std::codecvt_base::partial:
  605. std::cout << "partial" << std::endl;
  606. break;
  607. case std::codecvt_base::error:
  608. std::cout << "error" << std::endl;
  609. break;
  610. default:
  611. std::cout << "other" << std::endl;
  612. break;
  613. }
  614. std::cout << "State " << std::hex << state <<std::endl;
  615. std::cout << "Left in " << std::dec << from_end - from << " out " << to_end -to << std::endl;
  616. #endif
  617. return r;
  618. }
  619. };
  620. template<typename CharType,typename CodecvtImpl>
  621. class generic_codecvt<CharType,CodecvtImpl,1> : public std::codecvt<CharType,char,std::mbstate_t>, public generic_codecvt_base
  622. {
  623. public:
  624. typedef CharType uchar;
  625. CodecvtImpl const &implementation() const
  626. {
  627. return *static_cast<CodecvtImpl const *>(this);
  628. }
  629. generic_codecvt(size_t refs = 0) : std::codecvt<char,char,std::mbstate_t>(refs)
  630. {
  631. }
  632. };
  633. } // locale
  634. } // namespace boost
  635. #endif
  636. ///
  637. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4