utf8cpp.html 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta name="generator" content=
  5. "HTML Tidy for Linux/x86 (vers 1st November 2002), see www.w3.org">
  6. <meta name="description" content=
  7. "A simple, portable and lightweigt C++ library for easy handling of UTF-8 encoded strings">
  8. <meta name="keywords" content="UTF-8 C++ portable utf8 unicode generic templates">
  9. <meta name="author" content="Nemanja Trifunovic">
  10. <title>
  11. UTF8-CPP: UTF-8 with C++ in a Portable Way
  12. </title>
  13. <style type="text/css">
  14. <!--
  15. span.return_value {
  16. color: brown;
  17. }
  18. span.keyword {
  19. color: blue;
  20. }
  21. span.preprocessor {
  22. color: navy;
  23. }
  24. span.literal {
  25. color: olive;
  26. }
  27. span.comment {
  28. color: green;
  29. }
  30. code {
  31. font-weight: bold;
  32. }
  33. ul.toc {
  34. list-style-type: none;
  35. }
  36. p.version {
  37. font-size: small;
  38. font-style: italic;
  39. }
  40. -->
  41. </style>
  42. </head>
  43. <body>
  44. <h1>
  45. UTF8-CPP: UTF-8 with C++ in a Portable Way
  46. </h1>
  47. <p>
  48. <a href="https://sourceforge.net/projects/utfcpp">The Sourceforge project page</a>
  49. </p>
  50. <div id="toc">
  51. <h2>
  52. Table of Contents
  53. </h2>
  54. <ul class="toc">
  55. <li>
  56. <a href="#introduction">Introduction</a>
  57. </li>
  58. <li>
  59. <a href="#examples">Examples of Use</a>
  60. <ul class="toc">
  61. <li>
  62. <a href=#introsample>Introductionary Sample </a>
  63. </li>
  64. <li>
  65. <a href=#validfile>Checking if a file contains valid UTF-8 text</a>
  66. </li>
  67. <li>
  68. <a href=#fixinvalid>Ensure that a string contains valid UTF-8 text</a>
  69. </li>
  70. </ul>
  71. <li>
  72. <a href="#reference">Reference</a>
  73. <ul class="toc">
  74. <li>
  75. <a href="#funutf8">Functions From utf8 Namespace </a>
  76. </li>
  77. <li>
  78. <a href="#typesutf8">Types From utf8 Namespace </a>
  79. </li>
  80. <li>
  81. <a href="#fununchecked">Functions From utf8::unchecked Namespace </a>
  82. </li>
  83. <li>
  84. <a href="#typesunchecked">Types From utf8::unchecked Namespace </a>
  85. </li>
  86. </ul>
  87. </li>
  88. <li>
  89. <a href="#points">Points of Interest</a>
  90. </li>
  91. <li>
  92. <a href="#links">Links</a>
  93. </li>
  94. </ul>
  95. </div>
  96. <h2 id="introduction">
  97. Introduction
  98. </h2>
  99. <p>
  100. Many C++ developers miss an easy and portable way of handling Unicode encoded
  101. strings. The original C++ Standard (known as C++98 or C++03) is Unicode agnostic.
  102. C++11 provides some support for Unicode on core language and library level:
  103. u8, u, and U character and string literals, char16_t and char32_t character types,
  104. u16string and u32string library classes, and codecvt support for conversions
  105. between Unicode encoding forms.
  106. In the meantime, developers use third party libraries like ICU, OS specific capabilities, or simply
  107. roll out their own solutions.
  108. </p>
  109. <p>
  110. In order to easily handle UTF-8 encoded Unicode strings, I came up with a small
  111. generic library. For anybody used to work with STL algorithms and iterators, it should be
  112. easy and natural to use. The code is freely available for any purpose - check out
  113. the license at the beginning of the utf8.h file. If you run into
  114. bugs or performance issues, please let me know and I'll do my best to address them.
  115. </p>
  116. <p>
  117. The purpose of this article is not to offer an introduction to Unicode in general,
  118. and UTF-8 in particular. If you are not familiar with Unicode, be sure to check out
  119. <a href="http://www.unicode.org/">Unicode Home Page</a> or some other source of
  120. information for Unicode. Also, it is not my aim to advocate the use of UTF-8
  121. encoded strings in C++ programs; if you want to handle UTF-8 encoded strings from
  122. C++, I am sure you have good reasons for it.
  123. </p>
  124. <h2 id="examples">
  125. Examples of use
  126. </h2>
  127. <h3 id="introsample">
  128. Introductionary Sample
  129. </h3>
  130. <p>
  131. To illustrate the use of the library, let's start with a small but complete program
  132. that opens a file containing UTF-8 encoded text, reads it line by line, checks each line
  133. for invalid UTF-8 byte sequences, and converts it to UTF-16 encoding and back to UTF-8:
  134. </p>
  135. <pre>
  136. <span class="preprocessor">#include &lt;fstream&gt;</span>
  137. <span class="preprocessor">#include &lt;iostream&gt;</span>
  138. <span class="preprocessor">#include &lt;string&gt;</span>
  139. <span class="preprocessor">#include &lt;vector&gt;</span>
  140. <span class="preprocessor">#include "utf8.h"</span>
  141. <span class="keyword">using namespace</span> std;
  142. <span class="keyword">int</span> main(<span class="keyword">int</span> argc, <span class="keyword">char</span>** argv)
  143. {
  144. <span class="keyword">if</span> (argc != <span class="literal">2</span>) {
  145. cout &lt;&lt; <span class="literal">"\nUsage: docsample filename\n"</span>;
  146. <span class="keyword">return</span> <span class="literal">0</span>;
  147. }
  148. <span class="keyword">const char</span>* test_file_path = argv[1];
  149. <span class="comment">// Open the test file (contains UTF-8 encoded text)</span>
  150. ifstream fs8(test_file_path);
  151. <span class="keyword">if</span> (!fs8.is_open()) {
  152. cout &lt;&lt; <span class=
  153. "literal">"Could not open "</span> &lt;&lt; test_file_path &lt;&lt; endl;
  154. <span class="keyword">return</span> <span class="literal">0</span>;
  155. }
  156. <span class="keyword">unsigned</span> line_count = <span class="literal">1</span>;
  157. string line;
  158. <span class="comment">// Play with all the lines in the file</span>
  159. <span class="keyword">while</span> (getline(fs8, line)) {
  160. <span class="comment">// check for invalid utf-8 (for a simple yes/no check, there is also utf8::is_valid function)</span>
  161. string::iterator end_it = utf8::find_invalid(line.begin(), line.end());
  162. <span class="keyword">if</span> (end_it != line.end()) {
  163. cout &lt;&lt; <span class=
  164. "literal">"Invalid UTF-8 encoding detected at line "</span> &lt;&lt; line_count &lt;&lt; <span
  165. class="literal">"\n"</span>;
  166. cout &lt;&lt; <span class=
  167. "literal">"This part is fine: "</span> &lt;&lt; string(line.begin(), end_it) &lt;&lt; <span
  168. class="literal">"\n"</span>;
  169. }
  170. <span class="comment">// Get the line length (at least for the valid part)</span>
  171. <span class="keyword">int</span> length = utf8::distance(line.begin(), end_it);
  172. cout &lt;&lt; <span class=
  173. "literal">"Length of line "</span> &lt;&lt; line_count &lt;&lt; <span class=
  174. "literal">" is "</span> &lt;&lt; length &lt;&lt; <span class="literal">"\n"</span>;
  175. <span class="comment">// Convert it to utf-16</span>
  176. vector&lt;unsigned short&gt; utf16line;
  177. utf8::utf8to16(line.begin(), end_it, back_inserter(utf16line));
  178. <span class="comment">// And back to utf-8</span>
  179. string utf8line;
  180. utf8::utf16to8(utf16line.begin(), utf16line.end(), back_inserter(utf8line));
  181. <span class="comment">// Confirm that the conversion went OK:</span>
  182. <span class="keyword">if</span> (utf8line != string(line.begin(), end_it))
  183. cout &lt;&lt; <span class=
  184. "literal">"Error in UTF-16 conversion at line: "</span> &lt;&lt; line_count &lt;&lt; <span
  185. class="literal">"\n"</span>;
  186. line_count++;
  187. }
  188. <span class="keyword">return</span> <span class="literal">0</span>;
  189. }
  190. </pre>
  191. <p>
  192. In the previous code sample, for each line we performed
  193. a detection of invalid UTF-8 sequences with <code>find_invalid</code>; the number
  194. of characters (more precisely - the number of Unicode code points, including the end
  195. of line and even BOM if there is one) in each line was
  196. determined with a use of <code>utf8::distance</code>; finally, we have converted
  197. each line to UTF-16 encoding with <code>utf8to16</code> and back to UTF-8 with
  198. <code>utf16to8</code>.
  199. </p>
  200. <h3 id="validfile">Checking if a file contains valid UTF-8 text</h3>
  201. <p>
  202. Here is a function that checks whether the content of a file is valid UTF-8 encoded text without
  203. reading the content into the memory:
  204. </p>
  205. <pre>
  206. <span class="keyword">bool</span> valid_utf8_file(i<span class="keyword">const char</span>* file_name)
  207. {
  208. ifstream ifs(file_name);
  209. <span class="keyword">if</span> (!ifs)
  210. <span class="keyword">return false</span>; <span class="comment">// even better, throw here</span>
  211. istreambuf_iterator&lt;<span class="keyword">char</span>&gt; it(ifs.rdbuf());
  212. istreambuf_iterator&lt;<span class="keyword">char</span>&gt; eos;
  213. <span class="keyword">return</span> utf8::is_valid(it, eos);
  214. }
  215. </pre>
  216. <p>
  217. Because the function <code>utf8::is_valid()</code> works with input iterators, we were able
  218. to pass an <code>istreambuf_iterator</code> to it and read the content of the file directly
  219. without loading it to the memory first.</p>
  220. <p>
  221. Note that other functions that take input iterator arguments can be used in a similar way. For
  222. instance, to read the content of a UTF-8 encoded text file and convert the text to UTF-16, just
  223. do something like:
  224. </p>
  225. <pre>
  226. utf8::utf8to16(it, eos, back_inserter(u16string));
  227. </pre>
  228. <h3 id="fixinvalid">Ensure that a string contains valid UTF-8 text</h3>
  229. <p>
  230. If we have some text that "probably" contains UTF-8 encoded text and we want to
  231. replace any invalid UTF-8 sequence with a replacement character, something like
  232. the following function may be used:
  233. </p>
  234. <pre>
  235. <span class="keyword">void</span> fix_utf8_string(std::string&amp; str)
  236. {
  237. std::string temp;
  238. utf8::replace_invalid(str.begin(), str.end(), back_inserter(temp));
  239. str = temp;
  240. }
  241. </pre>
  242. <p>The function will replace any invalid UTF-8 sequence with a Unicode replacement character.
  243. There is an overloaded function that enables the caller to supply their own replacement character.
  244. </p>
  245. <h2 id="reference">
  246. Reference
  247. </h2>
  248. <h3 id="funutf8">
  249. Functions From utf8 Namespace
  250. </h3>
  251. <h4>
  252. utf8::append
  253. </h4>
  254. <p class="version">
  255. Available in version 1.0 and later.
  256. </p>
  257. <p>
  258. Encodes a 32 bit code point as a UTF-8 sequence of octets and appends the sequence
  259. to a UTF-8 string.
  260. </p>
  261. <pre>
  262. <span class="keyword">template</span> &lt;<span class=
  263. "keyword">typename</span> octet_iterator&gt;
  264. octet_iterator append(uint32_t cp, octet_iterator result);
  265. </pre>
  266. <p>
  267. <code>octet_iterator</code>: an output iterator.<br>
  268. <code>cp</code>: a 32 bit integer representing a code point to append to the
  269. sequence.<br>
  270. <code>result</code>: an output iterator to the place in the sequence where to
  271. append the code point.<br>
  272. <span class="return_value">Return value</span>: an iterator pointing to the place
  273. after the newly appended sequence.
  274. </p>
  275. <p>
  276. Example of use:
  277. </p>
  278. <pre>
  279. <span class="keyword">unsigned char</span> u[<span class="literal">5</span>] = {<span
  280. class="literal">0</span>,<span class="literal">0</span>,<span class=
  281. "literal">0</span>,<span class="literal">0</span>,<span class="literal">0</span>};
  282. <span class="keyword">unsigned char</span>* end = append(<span class=
  283. "literal">0x0448</span>, u);
  284. assert (u[<span class="literal">0</span>] == <span class=
  285. "literal">0xd1</span> &amp;&amp; u[<span class="literal">1</span>] == <span class=
  286. "literal">0x88</span> &amp;&amp; u[<span class="literal">2</span>] == <span class=
  287. "literal">0</span> &amp;&amp; u[<span class="literal">3</span>] == <span class=
  288. "literal">0</span> &amp;&amp; u[<span class="literal">4</span>] == <span class=
  289. "literal">0</span>);
  290. </pre>
  291. <p>
  292. Note that <code>append</code> does not allocate any memory - it is the burden of
  293. the caller to make sure there is enough memory allocated for the operation. To make
  294. things more interesting, <code>append</code> can add anywhere between 1 and 4
  295. octets to the sequence. In practice, you would most often want to use
  296. <code>std::back_inserter</code> to ensure that the necessary memory is allocated.
  297. </p>
  298. <p>
  299. In case of an invalid code point, a <code>utf8::invalid_code_point</code> exception
  300. is thrown.
  301. </p>
  302. <h4>
  303. utf8::next
  304. </h4>
  305. <p class="version">
  306. Available in version 1.0 and later.
  307. </p>
  308. <p>
  309. Given the iterator to the beginning of the UTF-8 sequence, it returns the code
  310. point and moves the iterator to the next position.
  311. </p>
  312. <pre>
  313. <span class="keyword">template</span> &lt;<span class=
  314. "keyword">typename</span> octet_iterator&gt;
  315. uint32_t next(octet_iterator&amp; it, octet_iterator end);
  316. </pre>
  317. <p>
  318. <code>octet_iterator</code>: an input iterator.<br>
  319. <code>it</code>: a reference to an iterator pointing to the beginning of an UTF-8
  320. encoded code point. After the function returns, it is incremented to point to the
  321. beginning of the next code point.<br>
  322. <code>end</code>: end of the UTF-8 sequence to be processed. If <code>it</code>
  323. gets equal to <code>end</code> during the extraction of a code point, an
  324. <code>utf8::not_enough_room</code> exception is thrown.<br>
  325. <span class="return_value">Return value</span>: the 32 bit representation of the
  326. processed UTF-8 code point.
  327. </p>
  328. <p>
  329. Example of use:
  330. </p>
  331. <pre>
  332. <span class="keyword">char</span>* twochars = <span class=
  333. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  334. <span class="keyword">char</span>* w = twochars;
  335. <span class="keyword">int</span> cp = next(w, twochars + <span class="literal">6</span>);
  336. assert (cp == <span class="literal">0x65e5</span>);
  337. assert (w == twochars + <span class="literal">3</span>);
  338. </pre>
  339. <p>
  340. This function is typically used to iterate through a UTF-8 encoded string.
  341. </p>
  342. <p>
  343. In case of an invalid UTF-8 seqence, a <code>utf8::invalid_utf8</code> exception is
  344. thrown.
  345. </p>
  346. <h4>
  347. utf8::peek_next
  348. </h4>
  349. <p class="version">
  350. Available in version 2.1 and later.
  351. </p>
  352. <p>
  353. Given the iterator to the beginning of the UTF-8 sequence, it returns the code
  354. point for the following sequence without changing the value of the iterator.
  355. </p>
  356. <pre>
  357. <span class="keyword">template</span> &lt;<span class=
  358. "keyword">typename</span> octet_iterator&gt;
  359. uint32_t peek_next(octet_iterator it, octet_iterator end);
  360. </pre>
  361. <p>
  362. <code>octet_iterator</code>: an input iterator.<br>
  363. <code>it</code>: an iterator pointing to the beginning of an UTF-8
  364. encoded code point.<br>
  365. <code>end</code>: end of the UTF-8 sequence to be processed. If <code>it</code>
  366. gets equal to <code>end</code> during the extraction of a code point, an
  367. <code>utf8::not_enough_room</code> exception is thrown.<br>
  368. <span class="return_value">Return value</span>: the 32 bit representation of the
  369. processed UTF-8 code point.
  370. </p>
  371. <p>
  372. Example of use:
  373. </p>
  374. <pre>
  375. <span class="keyword">char</span>* twochars = <span class=
  376. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  377. <span class="keyword">char</span>* w = twochars;
  378. <span class="keyword">int</span> cp = peek_next(w, twochars + <span class="literal">6</span>);
  379. assert (cp == <span class="literal">0x65e5</span>);
  380. assert (w == twochars);
  381. </pre>
  382. <p>
  383. In case of an invalid UTF-8 seqence, a <code>utf8::invalid_utf8</code> exception is
  384. thrown.
  385. </p>
  386. <h4>
  387. utf8::prior
  388. </h4>
  389. <p class="version">
  390. Available in version 1.02 and later.
  391. </p>
  392. <p>
  393. Given a reference to an iterator pointing to an octet in a UTF-8 sequence, it
  394. decreases the iterator until it hits the beginning of the previous UTF-8 encoded
  395. code point and returns the 32 bits representation of the code point.
  396. </p>
  397. <pre>
  398. <span class="keyword">template</span> &lt;<span class=
  399. "keyword">typename</span> octet_iterator&gt;
  400. uint32_t prior(octet_iterator&amp; it, octet_iterator start);
  401. </pre>
  402. <p>
  403. <code>octet_iterator</code>: a bidirectional iterator.<br>
  404. <code>it</code>: a reference pointing to an octet within a UTF-8 encoded string.
  405. After the function returns, it is decremented to point to the beginning of the
  406. previous code point.<br>
  407. <code>start</code>: an iterator to the beginning of the sequence where the search
  408. for the beginning of a code point is performed. It is a
  409. safety measure to prevent passing the beginning of the string in the search for a
  410. UTF-8 lead octet.<br>
  411. <span class="return_value">Return value</span>: the 32 bit representation of the
  412. previous code point.
  413. </p>
  414. <p>
  415. Example of use:
  416. </p>
  417. <pre>
  418. <span class="keyword">char</span>* twochars = <span class=
  419. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  420. <span class="keyword">unsigned char</span>* w = twochars + <span class=
  421. "literal">3</span>;
  422. <span class="keyword">int</span> cp = prior (w, twochars);
  423. assert (cp == <span class="literal">0x65e5</span>);
  424. assert (w == twochars);
  425. </pre>
  426. <p>
  427. This function has two purposes: one is two iterate backwards through a UTF-8
  428. encoded string. Note that it is usually a better idea to iterate forward instead,
  429. since <code>utf8::next</code> is faster. The second purpose is to find a beginning
  430. of a UTF-8 sequence if we have a random position within a string. Note that in that
  431. case <code>utf8::prior</code> may not detect an invalid UTF-8 sequence in some scenarios:
  432. for instance if there are superfluous trail octets, it will just skip them.
  433. </p>
  434. <p>
  435. <code>it</code> will typically point to the beginning of
  436. a code point, and <code>start</code> will point to the
  437. beginning of the string to ensure we don't go backwards too far. <code>it</code> is
  438. decreased until it points to a lead UTF-8 octet, and then the UTF-8 sequence
  439. beginning with that octet is decoded to a 32 bit representation and returned.
  440. </p>
  441. <p>
  442. In case <code>start</code> is reached before a UTF-8 lead octet is hit, or if an
  443. invalid UTF-8 sequence is started by the lead octet, an <code>invalid_utf8</code>
  444. exception is thrown.
  445. </p>
  446. <p>In case <code>start</code> equals <code>it</code>, a <code>not_enough_room</code>
  447. exception is thrown.
  448. <h4>
  449. utf8::previous
  450. </h4>
  451. <p class="version">
  452. Deprecated in version 1.02 and later.
  453. </p>
  454. <p>
  455. Given a reference to an iterator pointing to an octet in a UTF-8 seqence, it
  456. decreases the iterator until it hits the beginning of the previous UTF-8 encoded
  457. code point and returns the 32 bits representation of the code point.
  458. </p>
  459. <pre>
  460. <span class="keyword">template</span> &lt;<span class=
  461. "keyword">typename</span> octet_iterator&gt;
  462. uint32_t previous(octet_iterator&amp; it, octet_iterator pass_start);
  463. </pre>
  464. <p>
  465. <code>octet_iterator</code>: a random access iterator.<br>
  466. <code>it</code>: a reference pointing to an octet within a UTF-8 encoded string.
  467. After the function returns, it is decremented to point to the beginning of the
  468. previous code point.<br>
  469. <code>pass_start</code>: an iterator to the point in the sequence where the search
  470. for the beginning of a code point is aborted if no result was reached. It is a
  471. safety measure to prevent passing the beginning of the string in the search for a
  472. UTF-8 lead octet.<br>
  473. <span class="return_value">Return value</span>: the 32 bit representation of the
  474. previous code point.
  475. </p>
  476. <p>
  477. Example of use:
  478. </p>
  479. <pre>
  480. <span class="keyword">char</span>* twochars = <span class=
  481. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  482. <span class="keyword">unsigned char</span>* w = twochars + <span class=
  483. "literal">3</span>;
  484. <span class="keyword">int</span> cp = previous (w, twochars - <span class=
  485. "literal">1</span>);
  486. assert (cp == <span class="literal">0x65e5</span>);
  487. assert (w == twochars);
  488. </pre>
  489. <p>
  490. <code>utf8::previous</code> is deprecated, and <code>utf8::prior</code> should
  491. be used instead, although the existing code can continue using this function.
  492. The problem is the parameter <code>pass_start</code> that points to the position
  493. just before the beginning of the sequence. Standard containers don't have the
  494. concept of "pass start" and the function can not be used with their iterators.
  495. </p>
  496. <p>
  497. <code>it</code> will typically point to the beginning of
  498. a code point, and <code>pass_start</code> will point to the octet just before the
  499. beginning of the string to ensure we don't go backwards too far. <code>it</code> is
  500. decreased until it points to a lead UTF-8 octet, and then the UTF-8 sequence
  501. beginning with that octet is decoded to a 32 bit representation and returned.
  502. </p>
  503. <p>
  504. In case <code>pass_start</code> is reached before a UTF-8 lead octet is hit, or if an
  505. invalid UTF-8 sequence is started by the lead octet, an <code>invalid_utf8</code>
  506. exception is thrown
  507. </p>
  508. <h4>
  509. utf8::advance
  510. </h4>
  511. <p class="version">
  512. Available in version 1.0 and later.
  513. </p>
  514. <p>
  515. Advances an iterator by the specified number of code points within an UTF-8
  516. sequence.
  517. </p>
  518. <pre>
  519. <span class="keyword">template</span> &lt;<span class=
  520. "keyword">typename</span> octet_iterator, typename distance_type&gt;
  521. <span class=
  522. "keyword">void</span> advance (octet_iterator&amp; it, distance_type n, octet_iterator end);
  523. </pre>
  524. <p>
  525. <code>octet_iterator</code>: an input iterator.<br>
  526. <code>distance_type</code>: an integral type convertible to <code>octet_iterator</code>'s difference type.<br>
  527. <code>it</code>: a reference to an iterator pointing to the beginning of an UTF-8
  528. encoded code point. After the function returns, it is incremented to point to the
  529. nth following code point.<br>
  530. <code>n</code>: a positive integer that shows how many code points we want to
  531. advance.<br>
  532. <code>end</code>: end of the UTF-8 sequence to be processed. If <code>it</code>
  533. gets equal to <code>end</code> during the extraction of a code point, an
  534. <code>utf8::not_enough_room</code> exception is thrown.<br>
  535. </p>
  536. <p>
  537. Example of use:
  538. </p>
  539. <pre>
  540. <span class="keyword">char</span>* twochars = <span class=
  541. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  542. <span class="keyword">unsigned char</span>* w = twochars;
  543. advance (w, <span class="literal">2</span>, twochars + <span class="literal">6</span>);
  544. assert (w == twochars + <span class="literal">5</span>);
  545. </pre>
  546. <p>
  547. This function works only "forward". In case of a negative <code>n</code>, there is
  548. no effect.
  549. </p>
  550. <p>
  551. In case of an invalid code point, a <code>utf8::invalid_code_point</code> exception
  552. is thrown.
  553. </p>
  554. <h4>
  555. utf8::distance
  556. </h4>
  557. <p class="version">
  558. Available in version 1.0 and later.
  559. </p>
  560. <p>
  561. Given the iterators to two UTF-8 encoded code points in a seqence, returns the
  562. number of code points between them.
  563. </p>
  564. <pre>
  565. <span class="keyword">template</span> &lt;<span class=
  566. "keyword">typename</span> octet_iterator&gt;
  567. <span class=
  568. "keyword">typename</span> std::iterator_traits&lt;octet_iterator&gt;::difference_type distance (octet_iterator first, octet_iterator last);
  569. </pre>
  570. <p>
  571. <code>octet_iterator</code>: an input iterator.<br>
  572. <code>first</code>: an iterator to a beginning of a UTF-8 encoded code point.<br>
  573. <code>last</code>: an iterator to a "post-end" of the last UTF-8 encoded code
  574. point in the sequence we are trying to determine the length. It can be the
  575. beginning of a new code point, or not.<br>
  576. <span class="return_value">Return value</span> the distance between the iterators,
  577. in code points.
  578. </p>
  579. <p>
  580. Example of use:
  581. </p>
  582. <pre>
  583. <span class="keyword">char</span>* twochars = <span class=
  584. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  585. size_t dist = utf8::distance(twochars, twochars + <span class="literal">5</span>);
  586. assert (dist == <span class="literal">2</span>);
  587. </pre>
  588. <p>
  589. This function is used to find the length (in code points) of a UTF-8 encoded
  590. string. The reason it is called <em>distance</em>, rather than, say,
  591. <em>length</em> is mainly because developers are used that <em>length</em> is an
  592. O(1) function. Computing the length of an UTF-8 string is a linear operation, and
  593. it looked better to model it after <code>std::distance</code> algorithm.
  594. </p>
  595. <p>
  596. In case of an invalid UTF-8 seqence, a <code>utf8::invalid_utf8</code> exception is
  597. thrown. If <code>last</code> does not point to the past-of-end of a UTF-8 seqence,
  598. a <code>utf8::not_enough_room</code> exception is thrown.
  599. </p>
  600. <h4>
  601. utf8::utf16to8
  602. </h4>
  603. <p class="version">
  604. Available in version 1.0 and later.
  605. </p>
  606. <p>
  607. Converts a UTF-16 encoded string to UTF-8.
  608. </p>
  609. <pre>
  610. <span class="keyword">template</span> &lt;<span class=
  611. "keyword">typename</span> u16bit_iterator, <span class=
  612. "keyword">typename</span> octet_iterator&gt;
  613. octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result);
  614. </pre>
  615. <p>
  616. <code>u16bit_iterator</code>: an input iterator.<br>
  617. <code>octet_iterator</code>: an output iterator.<br>
  618. <code>start</code>: an iterator pointing to the beginning of the UTF-16 encoded
  619. string to convert.<br>
  620. <code>end</code>: an iterator pointing to pass-the-end of the UTF-16 encoded
  621. string to convert.<br>
  622. <code>result</code>: an output iterator to the place in the UTF-8 string where to
  623. append the result of conversion.<br>
  624. <span class="return_value">Return value</span>: An iterator pointing to the place
  625. after the appended UTF-8 string.
  626. </p>
  627. <p>
  628. Example of use:
  629. </p>
  630. <pre>
  631. <span class="keyword">unsigned short</span> utf16string[] = {<span class=
  632. "literal">0x41</span>, <span class="literal">0x0448</span>, <span class=
  633. "literal">0x65e5</span>, <span class="literal">0xd834</span>, <span class=
  634. "literal">0xdd1e</span>};
  635. vector&lt;<span class="keyword">unsigned char</span>&gt; utf8result;
  636. utf16to8(utf16string, utf16string + <span class=
  637. "literal">5</span>, back_inserter(utf8result));
  638. assert (utf8result.size() == <span class="literal">10</span>);
  639. </pre>
  640. <p>
  641. In case of invalid UTF-16 sequence, a <code>utf8::invalid_utf16</code> exception is
  642. thrown.
  643. </p>
  644. <h4>
  645. utf8::utf8to16
  646. </h4>
  647. <p class="version">
  648. Available in version 1.0 and later.
  649. </p>
  650. <p>
  651. Converts an UTF-8 encoded string to UTF-16
  652. </p>
  653. <pre>
  654. <span class="keyword">template</span> &lt;<span class=
  655. "keyword">typename</span> u16bit_iterator, typename octet_iterator&gt;
  656. u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result);
  657. </pre>
  658. <p>
  659. <code>octet_iterator</code>: an input iterator.<br>
  660. <code>u16bit_iterator</code>: an output iterator.<br>
  661. <code>start</code>: an iterator pointing to the beginning of the UTF-8 encoded
  662. string to convert. &lt; br /&gt; <code>end</code>: an iterator pointing to
  663. pass-the-end of the UTF-8 encoded string to convert.<br>
  664. <code>result</code>: an output iterator to the place in the UTF-16 string where to
  665. append the result of conversion.<br>
  666. <span class="return_value">Return value</span>: An iterator pointing to the place
  667. after the appended UTF-16 string.
  668. </p>
  669. <p>
  670. Example of use:
  671. </p>
  672. <pre>
  673. <span class="keyword">char</span> utf8_with_surrogates[] = <span class=
  674. "literal">"\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"</span>;
  675. vector &lt;<span class="keyword">unsigned short</span>&gt; utf16result;
  676. utf8to16(utf8_with_surrogates, utf8_with_surrogates + <span class=
  677. "literal">9</span>, back_inserter(utf16result));
  678. assert (utf16result.size() == <span class="literal">4</span>);
  679. assert (utf16result[<span class="literal">2</span>] == <span class=
  680. "literal">0xd834</span>);
  681. assert (utf16result[<span class="literal">3</span>] == <span class=
  682. "literal">0xdd1e</span>);
  683. </pre>
  684. <p>
  685. In case of an invalid UTF-8 seqence, a <code>utf8::invalid_utf8</code> exception is
  686. thrown. If <code>end</code> does not point to the past-of-end of a UTF-8 seqence, a
  687. <code>utf8::not_enough_room</code> exception is thrown.
  688. </p>
  689. <h4>
  690. utf8::utf32to8
  691. </h4>
  692. <p class="version">
  693. Available in version 1.0 and later.
  694. </p>
  695. <p>
  696. Converts a UTF-32 encoded string to UTF-8.
  697. </p>
  698. <pre>
  699. <span class="keyword">template</span> &lt;<span class=
  700. "keyword">typename</span> octet_iterator, typename u32bit_iterator&gt;
  701. octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result);
  702. </pre>
  703. <p>
  704. <code>octet_iterator</code>: an output iterator.<br>
  705. <code>u32bit_iterator</code>: an input iterator.<br>
  706. <code>start</code>: an iterator pointing to the beginning of the UTF-32 encoded
  707. string to convert.<br>
  708. <code>end</code>: an iterator pointing to pass-the-end of the UTF-32 encoded
  709. string to convert.<br>
  710. <code>result</code>: an output iterator to the place in the UTF-8 string where to
  711. append the result of conversion.<br>
  712. <span class="return_value">Return value</span>: An iterator pointing to the place
  713. after the appended UTF-8 string.
  714. </p>
  715. <p>
  716. Example of use:
  717. </p>
  718. <pre>
  719. <span class="keyword">int</span> utf32string[] = {<span class=
  720. "literal">0x448</span>, <span class="literal">0x65E5</span>, <span class=
  721. "literal">0x10346</span>, <span class="literal">0</span>};
  722. vector&lt;<span class="keyword">unsigned char</span>&gt; utf8result;
  723. utf32to8(utf32string, utf32string + <span class=
  724. "literal">3</span>, back_inserter(utf8result));
  725. assert (utf8result.size() == <span class="literal">9</span>);
  726. </pre>
  727. <p>
  728. In case of invalid UTF-32 string, a <code>utf8::invalid_code_point</code> exception
  729. is thrown.
  730. </p>
  731. <h4>
  732. utf8::utf8to32
  733. </h4>
  734. <p class="version">
  735. Available in version 1.0 and later.
  736. </p>
  737. <p>
  738. Converts a UTF-8 encoded string to UTF-32.
  739. </p>
  740. <pre>
  741. <span class="keyword">template</span> &lt;<span class=
  742. "keyword">typename</span> octet_iterator, <span class=
  743. "keyword">typename</span> u32bit_iterator&gt;
  744. u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result);
  745. </pre>
  746. <p>
  747. <code>octet_iterator</code>: an input iterator.<br>
  748. <code>u32bit_iterator</code>: an output iterator.<br>
  749. <code>start</code>: an iterator pointing to the beginning of the UTF-8 encoded
  750. string to convert.<br>
  751. <code>end</code>: an iterator pointing to pass-the-end of the UTF-8 encoded string
  752. to convert.<br>
  753. <code>result</code>: an output iterator to the place in the UTF-32 string where to
  754. append the result of conversion.<br>
  755. <span class="return_value">Return value</span>: An iterator pointing to the place
  756. after the appended UTF-32 string.
  757. </p>
  758. <p>
  759. Example of use:
  760. </p>
  761. <pre>
  762. <span class="keyword">char</span>* twochars = <span class=
  763. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  764. vector&lt;<span class="keyword">int</span>&gt; utf32result;
  765. utf8to32(twochars, twochars + <span class=
  766. "literal">5</span>, back_inserter(utf32result));
  767. assert (utf32result.size() == <span class="literal">2</span>);
  768. </pre>
  769. <p>
  770. In case of an invalid UTF-8 seqence, a <code>utf8::invalid_utf8</code> exception is
  771. thrown. If <code>end</code> does not point to the past-of-end of a UTF-8 seqence, a
  772. <code>utf8::not_enough_room</code> exception is thrown.
  773. </p>
  774. <h4>
  775. utf8::find_invalid
  776. </h4>
  777. <p class="version">
  778. Available in version 1.0 and later.
  779. </p>
  780. <p>
  781. Detects an invalid sequence within a UTF-8 string.
  782. </p>
  783. <pre>
  784. <span class="keyword">template</span> &lt;<span class=
  785. "keyword">typename</span> octet_iterator&gt;
  786. octet_iterator find_invalid(octet_iterator start, octet_iterator end);
  787. </pre>
  788. <p>
  789. <code>octet_iterator</code>: an input iterator.<br>
  790. <code>start</code>: an iterator pointing to the beginning of the UTF-8 string to
  791. test for validity.<br>
  792. <code>end</code>: an iterator pointing to pass-the-end of the UTF-8 string to test
  793. for validity.<br>
  794. <span class="return_value">Return value</span>: an iterator pointing to the first
  795. invalid octet in the UTF-8 string. In case none were found, equals
  796. <code>end</code>.
  797. </p>
  798. <p>
  799. Example of use:
  800. </p>
  801. <pre>
  802. <span class="keyword">char</span> utf_invalid[] = <span class=
  803. "literal">"\xe6\x97\xa5\xd1\x88\xfa"</span>;
  804. <span class=
  805. "keyword">char</span>* invalid = find_invalid(utf_invalid, utf_invalid + <span class=
  806. "literal">6</span>);
  807. assert (invalid == utf_invalid + <span class="literal">5</span>);
  808. </pre>
  809. <p>
  810. This function is typically used to make sure a UTF-8 string is valid before
  811. processing it with other functions. It is especially important to call it if before
  812. doing any of the <em>unchecked</em> operations on it.
  813. </p>
  814. <h4>
  815. utf8::is_valid
  816. </h4>
  817. <p class="version">
  818. Available in version 1.0 and later.
  819. </p>
  820. <p>
  821. Checks whether a sequence of octets is a valid UTF-8 string.
  822. </p>
  823. <pre>
  824. <span class="keyword">template</span> &lt;<span class=
  825. "keyword">typename</span> octet_iterator&gt;
  826. <span class="keyword">bool</span> is_valid(octet_iterator start, octet_iterator end);
  827. </pre>
  828. <p>
  829. <code>octet_iterator</code>: an input iterator.<br>
  830. <code>start</code>: an iterator pointing to the beginning of the UTF-8 string to
  831. test for validity.<br>
  832. <code>end</code>: an iterator pointing to pass-the-end of the UTF-8 string to test
  833. for validity.<br>
  834. <span class="return_value">Return value</span>: <code>true</code> if the sequence
  835. is a valid UTF-8 string; <code>false</code> if not.
  836. </p>
  837. Example of use:
  838. <pre>
  839. <span class="keyword">char</span> utf_invalid[] = <span class=
  840. "literal">"\xe6\x97\xa5\xd1\x88\xfa"</span>;
  841. <span class="keyword">bool</span> bvalid = is_valid(utf_invalid, utf_invalid + <span
  842. class="literal">6</span>);
  843. assert (bvalid == false);
  844. </pre>
  845. <p>
  846. <code>is_valid</code> is a shorthand for <code>find_invalid(start, end) ==
  847. end;</code>. You may want to use it to make sure that a byte seqence is a valid
  848. UTF-8 string without the need to know where it fails if it is not valid.
  849. </p>
  850. <h4>
  851. utf8::replace_invalid
  852. </h4>
  853. <p class="version">
  854. Available in version 2.0 and later.
  855. </p>
  856. <p>
  857. Replaces all invalid UTF-8 sequences within a string with a replacement marker.
  858. </p>
  859. <pre>
  860. <span class="keyword">template</span> &lt;<span class=
  861. "keyword">typename</span> octet_iterator, <span class=
  862. "keyword">typename</span> output_iterator&gt;
  863. output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement);
  864. <span class="keyword">template</span> &lt;<span class=
  865. "keyword">typename</span> octet_iterator, <span class=
  866. "keyword">typename</span> output_iterator&gt;
  867. output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out);
  868. </pre>
  869. <p>
  870. <code>octet_iterator</code>: an input iterator.<br>
  871. <code>output_iterator</code>: an output iterator.<br>
  872. <code>start</code>: an iterator pointing to the beginning of the UTF-8 string to
  873. look for invalid UTF-8 sequences.<br>
  874. <code>end</code>: an iterator pointing to pass-the-end of the UTF-8 string to look
  875. for invalid UTF-8 sequences.<br>
  876. <code>out</code>: An output iterator to the range where the result of replacement
  877. is stored.<br>
  878. <code>replacement</code>: A Unicode code point for the replacement marker. The
  879. version without this parameter assumes the value <code>0xfffd</code><br>
  880. <span class="return_value">Return value</span>: An iterator pointing to the place
  881. after the UTF-8 string with replaced invalid sequences.
  882. </p>
  883. <p>
  884. Example of use:
  885. </p>
  886. <pre>
  887. <span class="keyword">char</span> invalid_sequence[] = <span class=
  888. "literal">"a\x80\xe0\xa0\xc0\xaf\xed\xa0\x80z"</span>;
  889. vector&lt;<span class="keyword">char</span>&gt; replace_invalid_result;
  890. replace_invalid (invalid_sequence, invalid_sequence + sizeof(invalid_sequence), back_inserter(replace_invalid_result), <span
  891. class="literal">'?'</span>);
  892. bvalid = is_valid(replace_invalid_result.begin(), replace_invalid_result.end());
  893. assert (bvalid);
  894. <span class="keyword">char</span>* fixed_invalid_sequence = <span class=
  895. "literal">"a????z"</span>;
  896. assert (std::equal(replace_invalid_result.begin(), replace_invalid_result.end(), fixed_invalid_sequence));
  897. </pre>
  898. <p>
  899. <code>replace_invalid</code> does not perform in-place replacement of invalid
  900. sequences. Rather, it produces a copy of the original string with the invalid
  901. sequences replaced with a replacement marker. Therefore, <code>out</code> must not
  902. be in the <code>[start, end]</code> range.
  903. </p>
  904. <p>
  905. If <code>end</code> does not point to the past-of-end of a UTF-8 sequence, a
  906. <code>utf8::not_enough_room</code> exception is thrown.
  907. </p>
  908. <h4>
  909. utf8::starts_with_bom
  910. </h4>
  911. <p class="version">
  912. Available in version 2.3 and later. Relaces deprecated <code>is_bom()</code> function.
  913. </p>
  914. <p>
  915. Checks whether an octet sequence starts with a UTF-8 byte order mark (BOM)
  916. </p>
  917. <pre>
  918. <span class="keyword">template</span> &lt;<span class=
  919. "keyword">typename</span> octet_iterator&gt;
  920. <span class="keyword">bool</span> starts_with_bom (octet_iterator it, octet_iterator end);
  921. </pre>
  922. <p>
  923. <code>octet_iterator</code>: an input iterator.<br>
  924. <code>it</code>: beginning of the octet sequence to check<br>
  925. <code>end</code>: pass-end of the sequence to check<br>
  926. <span class="return_value">Return value</span>: <code>true</code> if the sequence
  927. starts with a UTF-8 byte order mark; <code>false</code> if not.
  928. </p>
  929. <p>
  930. Example of use:
  931. </p>
  932. <pre>
  933. <span class="keyword">unsigned char</span> byte_order_mark[] = {<span class=
  934. "literal">0xef</span>, <span class="literal">0xbb</span>, <span class=
  935. "literal">0xbf</span>};
  936. <span class="keyword">bool</span> bbom = starts_with_bom(byte_order_mark, byte_order_mark + <span class="keyword">sizeof</span>(byte_order_mark));
  937. assert (bbom == <span class="literal">true</span>);
  938. </pre>
  939. <p>
  940. The typical use of this function is to check the first three bytes of a file. If
  941. they form the UTF-8 BOM, we want to skip them before processing the actual UTF-8
  942. encoded text.
  943. </p>
  944. <h4>
  945. utf8::is_bom
  946. </h4>
  947. <p class="version">
  948. Available in version 1.0 and later. Deprecated in version 2.3. <code>starts_with_bom()</code> should be used
  949. instead.
  950. </p>
  951. <p>
  952. Checks whether a sequence of three octets is a UTF-8 byte order mark (BOM)
  953. </p>
  954. <pre>
  955. <span class="keyword">template</span> &lt;<span class=
  956. "keyword">typename</span> octet_iterator&gt;
  957. <span class="keyword">bool</span> is_bom (octet_iterator it); <span class="comment"> // Deprecated</span>
  958. </pre>
  959. <p>
  960. <code>octet_iterator</code>: an input iterator.<br>
  961. <code>it</code>: beginning of the 3-octet sequence to check<br>
  962. <span class="return_value">Return value</span>: <code>true</code> if the sequence
  963. is UTF-8 byte order mark; <code>false</code> if not.
  964. </p>
  965. <p>
  966. Example of use:
  967. </p>
  968. <pre>
  969. <span class="keyword">unsigned char</span> byte_order_mark[] = {<span class=
  970. "literal">0xef</span>, <span class="literal">0xbb</span>, <span class=
  971. "literal">0xbf</span>};
  972. <span class="keyword">bool</span> bbom = is_bom(byte_order_mark);
  973. assert (bbom == <span class="literal">true</span>);
  974. </pre>
  975. <p>
  976. The typical use of this function is to check the first three bytes of a file. If
  977. they form the UTF-8 BOM, we want to skip them before processing the actual UTF-8
  978. encoded text.
  979. </p>
  980. <p>
  981. If a sequence is
  982. shorter than three bytes, an invalid iterator will be dereferenced. Therefore, this function is deprecated
  983. in favor of <code>starts_with_bom()</code>that takes the end of sequence as an argument.
  984. </p>
  985. <h3 id="typesutf8">
  986. Types From utf8 Namespace
  987. </h3>
  988. <h4>utf8::exception
  989. </h4>
  990. <p class="version">
  991. Available in version 2.3 and later.
  992. </p>
  993. <p>
  994. Base class for the exceptions thrown by UTF CPP library functions.
  995. </p>
  996. <pre>
  997. <span class="keyword">class</span> exception : <span class="keyword">public</span> std::exception {};
  998. </pre>
  999. <p>
  1000. Example of use:
  1001. </p>
  1002. <pre>
  1003. <span class="keyword">try</span> {
  1004. code_that_uses_utf_cpp_library();
  1005. }
  1006. <span class="keyword">catch</span>(<span class="keyword">const</span> utf8::exception&amp; utfcpp_ex) {
  1007. cerr &lt;&lt; utfcpp_ex.what();
  1008. }
  1009. </pre>
  1010. <h4>utf8::invalid_code_point
  1011. </h4>
  1012. <p class="version">
  1013. Available in version 1.0 and later.
  1014. </p>
  1015. <p>
  1016. Thrown by UTF8 CPP functions such as <code>advance</code> and <code>next</code> if an UTF-8 sequence represents and invalid code point.
  1017. </p>
  1018. <pre>
  1019. <span class="keyword">class</span> invalid_code_point : <span class="keyword">public</span> exception {
  1020. <span class="keyword">public</span>:
  1021. uint32_t code_point() <span class="keyword">const</span>;
  1022. };
  1023. </pre>
  1024. <p>
  1025. Member function <code>code_point()</code> can be used to determine the invalid code point that
  1026. caused the exception to be thrown.
  1027. </p>
  1028. <h4>utf8::invalid_utf8
  1029. </h4>
  1030. <p class="version">
  1031. Available in version 1.0 and later.
  1032. </p>
  1033. <p>
  1034. Thrown by UTF8 CPP functions such as <code>next</code> and <code>prior</code> if an invalid UTF-8 sequence
  1035. is detected during decoding.
  1036. </p>
  1037. <pre>
  1038. <span class="keyword">class</span> invalid_utf8 : <span class="keyword">public</span> exception {
  1039. <span class="keyword">public</span>:
  1040. uint8_t utf8_octet() <span class="keyword">const</span>;
  1041. };
  1042. </pre>
  1043. <p>
  1044. Member function <code>utf8_octet()</code> can be used to determine the beginning of the byte
  1045. sequence that caused the exception to be thrown.
  1046. </p>
  1047. </pre>
  1048. <h4>utf8::invalid_utf16
  1049. </h4>
  1050. <p class="version">
  1051. Available in version 1.0 and later.
  1052. </p>
  1053. <p>
  1054. Thrown by UTF8 CPP function <code>utf16to8</code> if an invalid UTF-16 sequence
  1055. is detected during decoding.
  1056. </p>
  1057. <pre>
  1058. <span class="keyword">class</span> invalid_utf16 : <span class="keyword">public</span> exception {
  1059. <span class="keyword">public</span>:
  1060. uint16_t utf16_word() <span class="keyword">const</span>;
  1061. };
  1062. </pre>
  1063. <p>
  1064. Member function <code>utf16_word()</code> can be used to determine the UTF-16 code unit
  1065. that caused the exception to be thrown.
  1066. </p>
  1067. <h4>utf8::not_enough_room
  1068. </h4>
  1069. <p class="version">
  1070. Available in version 1.0 and later.
  1071. </p>
  1072. <p>
  1073. Thrown by UTF8 CPP functions such as <code>next</code> if the end of the decoded UTF-8 sequence
  1074. was reached before the code point was decoded.
  1075. </p>
  1076. <pre>
  1077. <span class="keyword">class</span> not_enough_room : <span class="keyword">public</span> exception {};
  1078. </pre>
  1079. <h4>
  1080. utf8::iterator
  1081. </h4>
  1082. <p class="version">
  1083. Available in version 2.0 and later.
  1084. </p>
  1085. <p>
  1086. Adapts the underlying octet iterator to iterate over the sequence of code points,
  1087. rather than raw octets.
  1088. </p>
  1089. <pre>
  1090. <span class="keyword">template</span> &lt;<span class="keyword">typename</span> octet_iterator&gt;
  1091. <span class="keyword">class</span> iterator;
  1092. </pre>
  1093. <h5>Member functions</h5>
  1094. <dl>
  1095. <dt><code>iterator();</code> <dd> the deafult constructor; the underlying <code>octet_iterator</code> is
  1096. constructed with its default constructor.
  1097. <dt><code><span class="keyword">explicit</span> iterator (const octet_iterator&amp; octet_it,
  1098. const octet_iterator&amp; range_start,
  1099. const octet_iterator&amp; range_end);</code> <dd> a constructor
  1100. that initializes the underlying <code>octet_iterator</code> with <code>octet_it</code>
  1101. and sets the range in which the iterator is considered valid.
  1102. <dt><code>octet_iterator base () <span class="keyword">const</span>;</code> <dd> returns the
  1103. underlying <code>octet_iterator</code>.
  1104. <dt><code>uint32_t operator * () <span class="keyword">const</span>;</code> <dd> decodes the utf-8 sequence
  1105. the underlying <code>octet_iterator</code> is pointing to and returns the code point.
  1106. <dt><code><span class="keyword">bool operator</span> == (const iterator&amp; rhs)
  1107. <span class="keyword">const</span>;</code> <dd> returns <span class="keyword">true</span>
  1108. if the two underlaying iterators are equal.
  1109. <dt><code><span class="keyword">bool operator</span> != (const iterator&amp; rhs)
  1110. <span class="keyword">const</span>;</code> <dd> returns <span class="keyword">true</span>
  1111. if the two underlaying iterators are not equal.
  1112. <dt><code>iterator&amp; <span class="keyword">operator</span> ++ (); </code> <dd> the prefix increment - moves
  1113. the iterator to the next UTF-8 encoded code point.
  1114. <dt><code>iterator <span class="keyword">operator</span> ++ (<span class="keyword">int</span>); </code> <dd>
  1115. the postfix increment - moves the iterator to the next UTF-8 encoded code point and returns the current one.
  1116. <dt><code>iterator&amp; <span class="keyword">operator</span> -- (); </code> <dd> the prefix decrement - moves
  1117. the iterator to the previous UTF-8 encoded code point.
  1118. <dt><code>iterator <span class="keyword">operator</span> -- (<span class="keyword">int</span>); </code> <dd>
  1119. the postfix decrement - moves the iterator to the previous UTF-8 encoded code point and returns the current one.
  1120. </dl>
  1121. <p>
  1122. Example of use:
  1123. </p>
  1124. <pre>
  1125. <span class="keyword">char</span>* threechars = <span class="literal">"\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"</span>;
  1126. utf8::iterator&lt;<span class="keyword">char</span>*&gt; it(threechars, threechars, threechars + <span class="literal">9</span>);
  1127. utf8::iterator&lt;<span class="keyword">char</span>*&gt; it2 = it;
  1128. assert (it2 == it);
  1129. assert (*it == <span class="literal">0x10346</span>);
  1130. assert (*(++it) == <span class="literal">0x65e5</span>);
  1131. assert ((*it++) == <span class="literal">0x65e5</span>);
  1132. assert (*it == <span class="literal">0x0448</span>);
  1133. assert (it != it2);
  1134. utf8::iterator&lt;<span class="keyword">char</span>*&gt; endit (threechars + <span class="literal">9</span>, threechars, threechars + <span class="literal">9</span>);
  1135. assert (++it == endit);
  1136. assert (*(--it) == <span class="literal">0x0448</span>);
  1137. assert ((*it--) == <span class="literal">0x0448</span>);
  1138. assert (*it == <span class="literal">0x65e5</span>);
  1139. assert (--it == utf8::iterator&lt;<span class="keyword">char</span>*&gt;(threechars, threechars, threechars + <span class="literal">9</span>));
  1140. assert (*it == <span class="literal">0x10346</span>);
  1141. </pre>
  1142. <p>
  1143. The purpose of <code>utf8::iterator</code> adapter is to enable easy iteration as well as the use of STL
  1144. algorithms with UTF-8 encoded strings. Increment and decrement operators are implemented in terms of
  1145. <code>utf8::next()</code> and <code>utf8::prior()</code> functions.
  1146. </p>
  1147. <p>
  1148. Note that <code>utf8::iterator</code> adapter is a checked iterator. It operates on the range specified in
  1149. the constructor; any attempt to go out of that range will result in an exception. Even the comparison operators
  1150. require both iterator object to be constructed against the same range - otherwise an exception is thrown. Typically,
  1151. the range will be determined by sequence container functions <code>begin</code> and <code>end</code>, i.e.:
  1152. </p>
  1153. <pre>
  1154. std::string s = <span class="literal">"example"</span>;
  1155. utf8::iterator i (s.begin(), s.begin(), s.end());
  1156. </pre>
  1157. <h3 id="fununchecked">
  1158. Functions From utf8::unchecked Namespace
  1159. </h3>
  1160. <h4>
  1161. utf8::unchecked::append
  1162. </h4>
  1163. <p class="version">
  1164. Available in version 1.0 and later.
  1165. </p>
  1166. <p>
  1167. Encodes a 32 bit code point as a UTF-8 sequence of octets and appends the sequence
  1168. to a UTF-8 string.
  1169. </p>
  1170. <pre>
  1171. <span class="keyword">template</span> &lt;<span class=
  1172. "keyword">typename</span> octet_iterator&gt;
  1173. octet_iterator append(uint32_t cp, octet_iterator result);
  1174. </pre>
  1175. <p>
  1176. <code>cp</code>: A 32 bit integer representing a code point to append to the
  1177. sequence.<br>
  1178. <code>result</code>: An output iterator to the place in the sequence where to
  1179. append the code point.<br>
  1180. <span class="return_value">Return value</span>: An iterator pointing to the place
  1181. after the newly appended sequence.
  1182. </p>
  1183. <p>
  1184. Example of use:
  1185. </p>
  1186. <pre>
  1187. <span class="keyword">unsigned char</span> u[<span class="literal">5</span>] = {<span
  1188. class="literal">0</span>,<span class="literal">0</span>,<span class=
  1189. "literal">0</span>,<span class="literal">0</span>,<span class="literal">0</span>};
  1190. <span class="keyword">unsigned char</span>* end = unchecked::append(<span class=
  1191. "literal">0x0448</span>, u);
  1192. assert (u[<span class="literal">0</span>] == <span class=
  1193. "literal">0xd1</span> &amp;&amp; u[<span class="literal">1</span>] == <span class=
  1194. "literal">0x88</span> &amp;&amp; u[<span class="literal">2</span>] == <span class=
  1195. "literal">0</span> &amp;&amp; u[<span class="literal">3</span>] == <span class=
  1196. "literal">0</span> &amp;&amp; u[<span class="literal">4</span>] == <span class=
  1197. "literal">0</span>);
  1198. </pre>
  1199. <p>
  1200. This is a faster but less safe version of <code>utf8::append</code>. It does not
  1201. check for validity of the supplied code point, and may produce an invalid UTF-8
  1202. sequence.
  1203. </p>
  1204. <h4>
  1205. utf8::unchecked::next
  1206. </h4>
  1207. <p class="version">
  1208. Available in version 1.0 and later.
  1209. </p>
  1210. <p>
  1211. Given the iterator to the beginning of a UTF-8 sequence, it returns the code point
  1212. and moves the iterator to the next position.
  1213. </p>
  1214. <pre>
  1215. <span class="keyword">template</span> &lt;<span class=
  1216. "keyword">typename</span> octet_iterator&gt;
  1217. uint32_t next(octet_iterator&amp; it);
  1218. </pre>
  1219. <p>
  1220. <code>it</code>: a reference to an iterator pointing to the beginning of an UTF-8
  1221. encoded code point. After the function returns, it is incremented to point to the
  1222. beginning of the next code point.<br>
  1223. <span class="return_value">Return value</span>: the 32 bit representation of the
  1224. processed UTF-8 code point.
  1225. </p>
  1226. <p>
  1227. Example of use:
  1228. </p>
  1229. <pre>
  1230. <span class="keyword">char</span>* twochars = <span class=
  1231. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  1232. <span class="keyword">char</span>* w = twochars;
  1233. <span class="keyword">int</span> cp = unchecked::next(w);
  1234. assert (cp == <span class="literal">0x65e5</span>);
  1235. assert (w == twochars + <span class="literal">3</span>);
  1236. </pre>
  1237. <p>
  1238. This is a faster but less safe version of <code>utf8::next</code>. It does not
  1239. check for validity of the supplied UTF-8 sequence.
  1240. </p>
  1241. <h4>
  1242. utf8::unchecked::peek_next
  1243. </h4>
  1244. <p class="version">
  1245. Available in version 2.1 and later.
  1246. </p>
  1247. <p>
  1248. Given the iterator to the beginning of a UTF-8 sequence, it returns the code point.
  1249. </p>
  1250. <pre>
  1251. <span class="keyword">template</span> &lt;<span class=
  1252. "keyword">typename</span> octet_iterator&gt;
  1253. uint32_t peek_next(octet_iterator it);
  1254. </pre>
  1255. <p>
  1256. <code>it</code>: an iterator pointing to the beginning of an UTF-8
  1257. encoded code point.<br>
  1258. <span class="return_value">Return value</span>: the 32 bit representation of the
  1259. processed UTF-8 code point.
  1260. </p>
  1261. <p>
  1262. Example of use:
  1263. </p>
  1264. <pre>
  1265. <span class="keyword">char</span>* twochars = <span class=
  1266. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  1267. <span class="keyword">char</span>* w = twochars;
  1268. <span class="keyword">int</span> cp = unchecked::peek_next(w);
  1269. assert (cp == <span class="literal">0x65e5</span>);
  1270. assert (w == twochars);
  1271. </pre>
  1272. <p>
  1273. This is a faster but less safe version of <code>utf8::peek_next</code>. It does not
  1274. check for validity of the supplied UTF-8 sequence.
  1275. </p>
  1276. <h4>
  1277. utf8::unchecked::prior
  1278. </h4>
  1279. <p class="version">
  1280. Available in version 1.02 and later.
  1281. </p>
  1282. <p>
  1283. Given a reference to an iterator pointing to an octet in a UTF-8 seqence, it
  1284. decreases the iterator until it hits the beginning of the previous UTF-8 encoded
  1285. code point and returns the 32 bits representation of the code point.
  1286. </p>
  1287. <pre>
  1288. <span class="keyword">template</span> &lt;<span class=
  1289. "keyword">typename</span> octet_iterator&gt;
  1290. uint32_t prior(octet_iterator&amp; it);
  1291. </pre>
  1292. <p>
  1293. <code>it</code>: a reference pointing to an octet within a UTF-8 encoded string.
  1294. After the function returns, it is decremented to point to the beginning of the
  1295. previous code point.<br>
  1296. <span class="return_value">Return value</span>: the 32 bit representation of the
  1297. previous code point.
  1298. </p>
  1299. <p>
  1300. Example of use:
  1301. </p>
  1302. <pre>
  1303. <span class="keyword">char</span>* twochars = <span class=
  1304. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  1305. <span class="keyword">char</span>* w = twochars + <span class="literal">3</span>;
  1306. <span class="keyword">int</span> cp = unchecked::prior (w);
  1307. assert (cp == <span class="literal">0x65e5</span>);
  1308. assert (w == twochars);
  1309. </pre>
  1310. <p>
  1311. This is a faster but less safe version of <code>utf8::prior</code>. It does not
  1312. check for validity of the supplied UTF-8 sequence and offers no boundary checking.
  1313. </p>
  1314. <h4>
  1315. utf8::unchecked::previous (deprecated, see utf8::unchecked::prior)
  1316. </h4>
  1317. <p class="version">
  1318. Deprecated in version 1.02 and later.
  1319. </p>
  1320. <p>
  1321. Given a reference to an iterator pointing to an octet in a UTF-8 seqence, it
  1322. decreases the iterator until it hits the beginning of the previous UTF-8 encoded
  1323. code point and returns the 32 bits representation of the code point.
  1324. </p>
  1325. <pre>
  1326. <span class="keyword">template</span> &lt;<span class=
  1327. "keyword">typename</span> octet_iterator&gt;
  1328. uint32_t previous(octet_iterator&amp; it);
  1329. </pre>
  1330. <p>
  1331. <code>it</code>: a reference pointing to an octet within a UTF-8 encoded string.
  1332. After the function returns, it is decremented to point to the beginning of the
  1333. previous code point.<br>
  1334. <span class="return_value">Return value</span>: the 32 bit representation of the
  1335. previous code point.
  1336. </p>
  1337. <p>
  1338. Example of use:
  1339. </p>
  1340. <pre>
  1341. <span class="keyword">char</span>* twochars = <span class=
  1342. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  1343. <span class="keyword">char</span>* w = twochars + <span class="literal">3</span>;
  1344. <span class="keyword">int</span> cp = unchecked::previous (w);
  1345. assert (cp == <span class="literal">0x65e5</span>);
  1346. assert (w == twochars);
  1347. </pre>
  1348. <p>
  1349. The reason this function is deprecated is just the consistency with the "checked"
  1350. versions, where <code>prior</code> should be used instead of <code>previous</code>.
  1351. In fact, <code>unchecked::previous</code> behaves exactly the same as <code>
  1352. unchecked::prior</code>
  1353. </p>
  1354. <p>
  1355. This is a faster but less safe version of <code>utf8::previous</code>. It does not
  1356. check for validity of the supplied UTF-8 sequence and offers no boundary checking.
  1357. </p>
  1358. <h4>
  1359. utf8::unchecked::advance
  1360. </h4>
  1361. <p class="version">
  1362. Available in version 1.0 and later.
  1363. </p>
  1364. <p>
  1365. Advances an iterator by the specified number of code points within an UTF-8
  1366. sequence.
  1367. </p>
  1368. <pre>
  1369. <span class="keyword">template</span> &lt;<span class=
  1370. "keyword">typename</span> octet_iterator, typename distance_type&gt;
  1371. <span class="keyword">void</span> advance (octet_iterator&amp; it, distance_type n);
  1372. </pre>
  1373. <p>
  1374. <code>it</code>: a reference to an iterator pointing to the beginning of an UTF-8
  1375. encoded code point. After the function returns, it is incremented to point to the
  1376. nth following code point.<br>
  1377. <code>n</code>: a positive integer that shows how many code points we want to
  1378. advance.<br>
  1379. </p>
  1380. <p>
  1381. Example of use:
  1382. </p>
  1383. <pre>
  1384. <span class="keyword">char</span>* twochars = <span class=
  1385. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  1386. <span class="keyword">char</span>* w = twochars;
  1387. unchecked::advance (w, <span class="literal">2</span>);
  1388. assert (w == twochars + <span class="literal">5</span>);
  1389. </pre>
  1390. <p>
  1391. This function works only "forward". In case of a negative <code>n</code>, there is
  1392. no effect.
  1393. </p>
  1394. <p>
  1395. This is a faster but less safe version of <code>utf8::advance</code>. It does not
  1396. check for validity of the supplied UTF-8 sequence and offers no boundary checking.
  1397. </p>
  1398. <h4>
  1399. utf8::unchecked::distance
  1400. </h4>
  1401. <p class="version">
  1402. Available in version 1.0 and later.
  1403. </p>
  1404. <p>
  1405. Given the iterators to two UTF-8 encoded code points in a seqence, returns the
  1406. number of code points between them.
  1407. </p>
  1408. <pre>
  1409. <span class="keyword">template</span> &lt;<span class=
  1410. "keyword">typename</span> octet_iterator&gt;
  1411. <span class=
  1412. "keyword">typename</span> std::iterator_traits&lt;octet_iterator&gt;::difference_type distance (octet_iterator first, octet_iterator last);
  1413. </pre>
  1414. <p>
  1415. <code>first</code>: an iterator to a beginning of a UTF-8 encoded code point.<br>
  1416. <code>last</code>: an iterator to a "post-end" of the last UTF-8 encoded code
  1417. point in the sequence we are trying to determine the length. It can be the
  1418. beginning of a new code point, or not.<br>
  1419. <span class="return_value">Return value</span> the distance between the iterators,
  1420. in code points.
  1421. </p>
  1422. <p>
  1423. Example of use:
  1424. </p>
  1425. <pre>
  1426. <span class="keyword">char</span>* twochars = <span class=
  1427. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  1428. size_t dist = utf8::unchecked::distance(twochars, twochars + <span class=
  1429. "literal">5</span>);
  1430. assert (dist == <span class="literal">2</span>);
  1431. </pre>
  1432. <p>
  1433. This is a faster but less safe version of <code>utf8::distance</code>. It does not
  1434. check for validity of the supplied UTF-8 sequence.
  1435. </p>
  1436. <h4>
  1437. utf8::unchecked::utf16to8
  1438. </h4>
  1439. <p class="version">
  1440. Available in version 1.0 and later.
  1441. </p>
  1442. <p>
  1443. Converts a UTF-16 encoded string to UTF-8.
  1444. </p>
  1445. <pre>
  1446. <span class="keyword">template</span> &lt;<span class=
  1447. "keyword">typename</span> u16bit_iterator, <span class=
  1448. "keyword">typename</span> octet_iterator&gt;
  1449. octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result);
  1450. </pre>
  1451. <p>
  1452. <code>start</code>: an iterator pointing to the beginning of the UTF-16 encoded
  1453. string to convert.<br>
  1454. <code>end</code>: an iterator pointing to pass-the-end of the UTF-16 encoded
  1455. string to convert.<br>
  1456. <code>result</code>: an output iterator to the place in the UTF-8 string where to
  1457. append the result of conversion.<br>
  1458. <span class="return_value">Return value</span>: An iterator pointing to the place
  1459. after the appended UTF-8 string.
  1460. </p>
  1461. <p>
  1462. Example of use:
  1463. </p>
  1464. <pre>
  1465. <span class="keyword">unsigned short</span> utf16string[] = {<span class=
  1466. "literal">0x41</span>, <span class="literal">0x0448</span>, <span class=
  1467. "literal">0x65e5</span>, <span class="literal">0xd834</span>, <span class=
  1468. "literal">0xdd1e</span>};
  1469. vector&lt;<span class="keyword">unsigned char</span>&gt; utf8result;
  1470. unchecked::utf16to8(utf16string, utf16string + <span class=
  1471. "literal">5</span>, back_inserter(utf8result));
  1472. assert (utf8result.size() == <span class="literal">10</span>);
  1473. </pre>
  1474. <p>
  1475. This is a faster but less safe version of <code>utf8::utf16to8</code>. It does not
  1476. check for validity of the supplied UTF-16 sequence.
  1477. </p>
  1478. <h4>
  1479. utf8::unchecked::utf8to16
  1480. </h4>
  1481. <p class="version">
  1482. Available in version 1.0 and later.
  1483. </p>
  1484. <p>
  1485. Converts an UTF-8 encoded string to UTF-16
  1486. </p>
  1487. <pre>
  1488. <span class="keyword">template</span> &lt;<span class=
  1489. "keyword">typename</span> u16bit_iterator, typename octet_iterator&gt;
  1490. u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result);
  1491. </pre>
  1492. <p>
  1493. <code>start</code>: an iterator pointing to the beginning of the UTF-8 encoded
  1494. string to convert. &lt; br /&gt; <code>end</code>: an iterator pointing to
  1495. pass-the-end of the UTF-8 encoded string to convert.<br>
  1496. <code>result</code>: an output iterator to the place in the UTF-16 string where to
  1497. append the result of conversion.<br>
  1498. <span class="return_value">Return value</span>: An iterator pointing to the place
  1499. after the appended UTF-16 string.
  1500. </p>
  1501. <p>
  1502. Example of use:
  1503. </p>
  1504. <pre>
  1505. <span class="keyword">char</span> utf8_with_surrogates[] = <span class=
  1506. "literal">"\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"</span>;
  1507. vector &lt;<span class="keyword">unsigned short</span>&gt; utf16result;
  1508. unchecked::utf8to16(utf8_with_surrogates, utf8_with_surrogates + <span class=
  1509. "literal">9</span>, back_inserter(utf16result));
  1510. assert (utf16result.size() == <span class="literal">4</span>);
  1511. assert (utf16result[<span class="literal">2</span>] == <span class=
  1512. "literal">0xd834</span>);
  1513. assert (utf16result[<span class="literal">3</span>] == <span class=
  1514. "literal">0xdd1e</span>);
  1515. </pre>
  1516. <p>
  1517. This is a faster but less safe version of <code>utf8::utf8to16</code>. It does not
  1518. check for validity of the supplied UTF-8 sequence.
  1519. </p>
  1520. <h4>
  1521. utf8::unchecked::utf32to8
  1522. </h4>
  1523. <p class="version">
  1524. Available in version 1.0 and later.
  1525. </p>
  1526. <p>
  1527. Converts a UTF-32 encoded string to UTF-8.
  1528. </p>
  1529. <pre>
  1530. <span class="keyword">template</span> &lt;<span class=
  1531. "keyword">typename</span> octet_iterator, <span class=
  1532. "keyword">typename</span> u32bit_iterator&gt;
  1533. octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result);
  1534. </pre>
  1535. <p>
  1536. <code>start</code>: an iterator pointing to the beginning of the UTF-32 encoded
  1537. string to convert.<br>
  1538. <code>end</code>: an iterator pointing to pass-the-end of the UTF-32 encoded
  1539. string to convert.<br>
  1540. <code>result</code>: an output iterator to the place in the UTF-8 string where to
  1541. append the result of conversion.<br>
  1542. <span class="return_value">Return value</span>: An iterator pointing to the place
  1543. after the appended UTF-8 string.
  1544. </p>
  1545. <p>
  1546. Example of use:
  1547. </p>
  1548. <pre>
  1549. <span class="keyword">int</span> utf32string[] = {<span class=
  1550. "literal">0x448</span>, <span class="literal">0x65e5</span>, <span class=
  1551. "literal">0x10346</span>, <span class="literal">0</span>};
  1552. vector&lt;<span class="keyword">unsigned char</span>&gt; utf8result;
  1553. utf32to8(utf32string, utf32string + <span class=
  1554. "literal">3</span>, back_inserter(utf8result));
  1555. assert (utf8result.size() == <span class="literal">9</span>);
  1556. </pre>
  1557. <p>
  1558. This is a faster but less safe version of <code>utf8::utf32to8</code>. It does not
  1559. check for validity of the supplied UTF-32 sequence.
  1560. </p>
  1561. <h4>
  1562. utf8::unchecked::utf8to32
  1563. </h4>
  1564. <p class="version">
  1565. Available in version 1.0 and later.
  1566. </p>
  1567. <p>
  1568. Converts a UTF-8 encoded string to UTF-32.
  1569. </p>
  1570. <pre>
  1571. <span class="keyword">template</span> &lt;<span class=
  1572. "keyword">typename</span> octet_iterator, typename u32bit_iterator&gt;
  1573. u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result);
  1574. </pre>
  1575. <p>
  1576. <code>start</code>: an iterator pointing to the beginning of the UTF-8 encoded
  1577. string to convert.<br>
  1578. <code>end</code>: an iterator pointing to pass-the-end of the UTF-8 encoded string
  1579. to convert.<br>
  1580. <code>result</code>: an output iterator to the place in the UTF-32 string where to
  1581. append the result of conversion.<br>
  1582. <span class="return_value">Return value</span>: An iterator pointing to the place
  1583. after the appended UTF-32 string.
  1584. </p>
  1585. <p>
  1586. Example of use:
  1587. </p>
  1588. <pre>
  1589. <span class="keyword">char</span>* twochars = <span class=
  1590. "literal">"\xe6\x97\xa5\xd1\x88"</span>;
  1591. vector&lt;<span class="keyword">int</span>&gt; utf32result;
  1592. unchecked::utf8to32(twochars, twochars + <span class=
  1593. "literal">5</span>, back_inserter(utf32result));
  1594. assert (utf32result.size() == <span class="literal">2</span>);
  1595. </pre>
  1596. <p>
  1597. This is a faster but less safe version of <code>utf8::utf8to32</code>. It does not
  1598. check for validity of the supplied UTF-8 sequence.
  1599. </p>
  1600. <h3 id="typesunchecked">
  1601. Types From utf8::unchecked Namespace
  1602. </h3>
  1603. <h4>
  1604. utf8::iterator
  1605. </h4>
  1606. <p class="version">
  1607. Available in version 2.0 and later.
  1608. </p>
  1609. <p>
  1610. Adapts the underlying octet iterator to iterate over the sequence of code points,
  1611. rather than raw octets.
  1612. </p>
  1613. <pre>
  1614. <span class="keyword">template</span> &lt;<span class="keyword">typename</span> octet_iterator&gt;
  1615. <span class="keyword">class</span> iterator;
  1616. </pre>
  1617. <h5>Member functions</h5>
  1618. <dl>
  1619. <dt><code>iterator();</code> <dd> the deafult constructor; the underlying <code>octet_iterator</code> is
  1620. constructed with its default constructor.
  1621. <dt><code><span class="keyword">explicit</span> iterator (const octet_iterator&amp; octet_it);
  1622. </code> <dd> a constructor
  1623. that initializes the underlying <code>octet_iterator</code> with <code>octet_it</code>
  1624. <dt><code>octet_iterator base () <span class="keyword">const</span>;</code> <dd> returns the
  1625. underlying <code>octet_iterator</code>.
  1626. <dt><code>uint32_t operator * () <span class="keyword">const</span>;</code> <dd> decodes the utf-8 sequence
  1627. the underlying <code>octet_iterator</code> is pointing to and returns the code point.
  1628. <dt><code><span class="keyword">bool operator</span> == (const iterator&amp; rhs)
  1629. <span class="keyword">const</span>;</code> <dd> returns <span class="keyword">true</span>
  1630. if the two underlaying iterators are equal.
  1631. <dt><code><span class="keyword">bool operator</span> != (const iterator&amp; rhs)
  1632. <span class="keyword">const</span>;</code> <dd> returns <span class="keyword">true</span>
  1633. if the two underlaying iterators are not equal.
  1634. <dt><code>iterator&amp; <span class="keyword">operator</span> ++ (); </code> <dd> the prefix increment - moves
  1635. the iterator to the next UTF-8 encoded code point.
  1636. <dt><code>iterator <span class="keyword">operator</span> ++ (<span class="keyword">int</span>); </code> <dd>
  1637. the postfix increment - moves the iterator to the next UTF-8 encoded code point and returns the current one.
  1638. <dt><code>iterator&amp; <span class="keyword">operator</span> -- (); </code> <dd> the prefix decrement - moves
  1639. the iterator to the previous UTF-8 encoded code point.
  1640. <dt><code>iterator <span class="keyword">operator</span> -- (<span class="keyword">int</span>); </code> <dd>
  1641. the postfix decrement - moves the iterator to the previous UTF-8 encoded code point and returns the current one.
  1642. </dl>
  1643. <p>
  1644. Example of use:
  1645. </p>
  1646. <pre>
  1647. <span class="keyword">char</span>* threechars = <span class="literal">"\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"</span>;
  1648. utf8::unchecked::iterator&lt;<span class="keyword">char</span>*&gt; un_it(threechars);
  1649. utf8::unchecked::iterator&lt;<span class="keyword">char</span>*&gt; un_it2 = un_it;
  1650. assert (un_it2 == un_it);
  1651. assert (*un_it == <span class="literal">0x10346</span>);
  1652. assert (*(++un_it) == <span class="literal">0x65e5</span>);
  1653. assert ((*un_it++) == <span class="literal">0x65e5</span>);
  1654. assert (*un_it == <span class="literal">0x0448</span>);
  1655. assert (un_it != un_it2);
  1656. utf8::::unchecked::iterator&lt;<span class="keyword">char</span>*&gt; un_endit (threechars + <span class="literal">9</span>);
  1657. assert (++un_it == un_endit);
  1658. assert (*(--un_it) == <span class="literal">0x0448</span>);
  1659. assert ((*un_it--) == <span class="literal">0x0448</span>);
  1660. assert (*un_it == <span class="literal">0x65e5</span>);
  1661. assert (--un_it == utf8::unchecked::iterator&lt;<span class="keyword">char</span>*&gt;(threechars));
  1662. assert (*un_it == <span class="literal">0x10346</span>);
  1663. </pre>
  1664. <p>
  1665. This is an unchecked version of <code>utf8::iterator</code>. It is faster in many cases, but offers
  1666. no validity or range checks.
  1667. </p>
  1668. <h2 id="points">
  1669. Points of interest
  1670. </h2>
  1671. <h4>
  1672. Design goals and decisions
  1673. </h4>
  1674. <p>
  1675. The library was designed to be:
  1676. </p>
  1677. <ol>
  1678. <li>
  1679. Generic: for better or worse, there are many C++ string classes out there, and
  1680. the library should work with as many of them as possible.
  1681. </li>
  1682. <li>
  1683. Portable: the library should be portable both accross different platforms and
  1684. compilers. The only non-portable code is a small section that declares unsigned
  1685. integers of different sizes: three typedefs. They can be changed by the users of
  1686. the library if they don't match their platform. The default setting should work
  1687. for Windows (both 32 and 64 bit), and most 32 bit and 64 bit Unix derivatives.
  1688. </li>
  1689. <li>
  1690. Lightweight: follow the "pay only for what you use" guideline.
  1691. </li>
  1692. <li>
  1693. Unintrusive: avoid forcing any particular design or even programming style on the
  1694. user. This is a library, not a framework.
  1695. </li>
  1696. </ol>
  1697. <h4>
  1698. Alternatives
  1699. </h4>
  1700. <p>
  1701. In case you want to look into other means of working with UTF-8 strings from C++,
  1702. here is the list of solutions I am aware of:
  1703. </p>
  1704. <ol>
  1705. <li>
  1706. <a href="http://icu.sourceforge.net/">ICU Library</a>. It is very powerful,
  1707. complete, feature-rich, mature, and widely used. Also big, intrusive,
  1708. non-generic, and doesn't play well with the Standard Library. I definitelly
  1709. recommend looking at ICU even if you don't plan to use it.
  1710. </li>
  1711. <li>
  1712. C++11 language and library features. Still far from complete, and not widely
  1713. supported by compiler vendors.
  1714. </li>
  1715. <li>
  1716. <a href=
  1717. "http://www.gtkmm.org/gtkmm2/docs/tutorial/html/ch03s04.html">Glib::ustring</a>.
  1718. A class specifically made to work with UTF-8 strings, and also feel like
  1719. <code>std::string</code>. If you prefer to have yet another string class in your
  1720. code, it may be worth a look. Be aware of the licensing issues, though.
  1721. </li>
  1722. <li>
  1723. Platform dependent solutions: Windows and POSIX have functions to convert strings
  1724. from one encoding to another. That is only a subset of what my library offers,
  1725. but if that is all you need it may be good enough.
  1726. </li>
  1727. </ol>
  1728. <h2 id="links">
  1729. Links
  1730. </h2>
  1731. <ol>
  1732. <li>
  1733. <a href="http://www.unicode.org/">The Unicode Consortium</a>.
  1734. </li>
  1735. <li>
  1736. <a href="http://icu.sourceforge.net/">ICU Library</a>.
  1737. </li>
  1738. <li>
  1739. <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8 at Wikipedia</a>
  1740. </li>
  1741. <li>
  1742. <a href="http://www.cl.cam.ac.uk/~mgk25/unicode.html">UTF-8 and Unicode FAQ for
  1743. Unix/Linux</a>
  1744. </li>
  1745. </ol>
  1746. </body>
  1747. </html>