transcode_iterator.hpp 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486
  1. // Copyright (C) 2020 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_PARSER_DETAIL_TEXT_TRANSCODE_ITERATOR_HPP
  7. #define BOOST_PARSER_DETAIL_TEXT_TRANSCODE_ITERATOR_HPP
  8. #include <boost/parser/detail/debug_assert.hpp>
  9. #include <boost/parser/detail/text/transcode_iterator_fwd.hpp>
  10. #include <boost/parser/detail/text/concepts.hpp>
  11. #include <boost/parser/detail/text/utf.hpp>
  12. #include <boost/parser/detail/text/detail/algorithm.hpp>
  13. #include <boost/parser/detail/stl_interfaces/iterator_interface.hpp>
  14. #include <array>
  15. #include <iterator>
  16. #include <type_traits>
  17. #include <stdexcept>
  18. #include <string_view>
  19. namespace boost::parser::detail { namespace text {
  20. namespace {
  21. constexpr char16_t high_surrogate_base = 0xd7c0;
  22. constexpr char16_t low_surrogate_base = 0xdc00;
  23. constexpr char32_t high_surrogate_min = 0xd800;
  24. constexpr char32_t high_surrogate_max = 0xdbff;
  25. constexpr char32_t low_surrogate_min = 0xdc00;
  26. constexpr char32_t low_surrogate_max = 0xdfff;
  27. constexpr char32_t replacement_character = 0xfffd;
  28. }
  29. namespace detail {
  30. constexpr bool in(unsigned char lo, unsigned char c, unsigned char hi)
  31. {
  32. return lo <= c && c <= hi;
  33. }
  34. struct throw_on_encoding_error
  35. {};
  36. template<typename OutIter>
  37. inline constexpr OutIter read_into_buf(char32_t cp, OutIter buf)
  38. {
  39. if (cp < 0x80) {
  40. *buf = static_cast<char>(cp);
  41. ++buf;
  42. } else if (cp < 0x800) {
  43. *buf = static_cast<char>(0xC0 + (cp >> 6));
  44. ++buf;
  45. *buf = static_cast<char>(0x80 + (cp & 0x3f));
  46. ++buf;
  47. } else if (cp < 0x10000) {
  48. *buf = static_cast<char>(0xe0 + (cp >> 12));
  49. ++buf;
  50. *buf = static_cast<char>(0x80 + ((cp >> 6) & 0x3f));
  51. ++buf;
  52. *buf = static_cast<char>(0x80 + (cp & 0x3f));
  53. ++buf;
  54. } else {
  55. *buf = static_cast<char>(0xf0 + (cp >> 18));
  56. ++buf;
  57. *buf = static_cast<char>(0x80 + ((cp >> 12) & 0x3f));
  58. ++buf;
  59. *buf = static_cast<char>(0x80 + ((cp >> 6) & 0x3f));
  60. ++buf;
  61. *buf = static_cast<char>(0x80 + (cp & 0x3f));
  62. ++buf;
  63. }
  64. return buf;
  65. }
  66. template<typename OutIter>
  67. constexpr OutIter write_cp_utf8(char32_t cp, OutIter out)
  68. {
  69. return detail::read_into_buf(cp, out);
  70. }
  71. template<typename OutIter>
  72. constexpr OutIter write_cp_utf16(char32_t cp, OutIter out)
  73. {
  74. if (cp < 0x10000) {
  75. *out = static_cast<char16_t>(cp);
  76. ++out;
  77. } else {
  78. *out = static_cast<char16_t>(cp >> 10) + high_surrogate_base;
  79. ++out;
  80. *out = static_cast<char16_t>(cp & 0x3ff) + low_surrogate_base;
  81. ++out;
  82. }
  83. return out;
  84. }
  85. inline constexpr char32_t surrogates_to_cp(char16_t hi, char16_t lo)
  86. {
  87. return char32_t((hi - high_surrogate_base) << 10) +
  88. (lo - low_surrogate_base);
  89. }
  90. template<typename T, typename U>
  91. using enable_utf8_cp = std::enable_if<is_char_iter_v<T>, U>;
  92. template<typename T, typename U = T>
  93. using enable_utf8_cp_t = typename enable_utf8_cp<T, U>::type;
  94. template<typename T, typename U>
  95. using enable_utf16_cp = std::enable_if<is_16_iter_v<T>, U>;
  96. template<typename T, typename U = T>
  97. using enable_utf16_cp_t = typename enable_utf16_cp<T, U>::type;
  98. template<typename I>
  99. auto bidirectional_at_most()
  100. {
  101. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  102. if constexpr (std::bidirectional_iterator<I>) {
  103. return std::bidirectional_iterator_tag{};
  104. } else if constexpr (std::forward_iterator<I>) {
  105. return std::forward_iterator_tag{};
  106. } else if constexpr (std::input_iterator<I>) {
  107. return std::input_iterator_tag{};
  108. }
  109. #else
  110. using category =
  111. typename std::iterator_traits<I>::iterator_category;
  112. if constexpr (std::is_base_of_v<
  113. std::bidirectional_iterator_tag,
  114. category>) {
  115. return std::bidirectional_iterator_tag{};
  116. } else {
  117. return category{};
  118. }
  119. #endif
  120. }
  121. template<typename I>
  122. using bidirectional_at_most_t = decltype(bidirectional_at_most<I>());
  123. }
  124. /** Returns true iff `c` is a Unicode surrogate. */
  125. inline constexpr bool surrogate(char32_t c)
  126. {
  127. return high_surrogate_min <= c && c <= low_surrogate_max;
  128. }
  129. /** Returns true iff `c` is a Unicode high surrogate. */
  130. inline constexpr bool high_surrogate(char32_t c)
  131. {
  132. return high_surrogate_min <= c && c <= high_surrogate_max;
  133. }
  134. /** Returns true iff `c` is a Unicode low surrogate. */
  135. inline constexpr bool low_surrogate(char32_t c)
  136. {
  137. return low_surrogate_min <= c && c <= low_surrogate_max;
  138. }
  139. /** Returns true iff `c` is a Unicode reserved noncharacter.
  140. \see Unicode 3.4/D14 */
  141. inline constexpr bool reserved_noncharacter(char32_t c)
  142. {
  143. bool const byte01_reserved = (c & 0xffff) >= 0xfffe;
  144. bool const byte2_at_most_0x10 = ((c & 0xff0000u) >> 16) <= 0x10;
  145. return (byte01_reserved && byte2_at_most_0x10) ||
  146. (0xfdd0 <= c && c <= 0xfdef);
  147. }
  148. /** Returns true iff `c` is a valid Unicode scalar value.
  149. \see Unicode 3.9/D90 */
  150. inline constexpr bool scalar_value(char32_t c)
  151. {
  152. return c <= 0x10ffff && !surrogate(c);
  153. }
  154. /** Returns true iff `c` is a Unicode scalar value not in the reserved
  155. range.
  156. \see Unicode 3.9/D90 */
  157. inline constexpr bool unreserved_scalar_value(char32_t c)
  158. {
  159. return scalar_value(c) && !reserved_noncharacter(c);
  160. }
  161. /** Returns true iff `c` is a UTF-8 lead code unit (which must be followed
  162. by 1-3 following units). */
  163. constexpr bool lead_code_unit(char8_type c)
  164. {
  165. return uint8_t((unsigned char)c - 0xc2) <= 0x32;
  166. }
  167. /** Returns true iff `c` is a UTF-8 continuation code unit. */
  168. constexpr bool continuation(char8_type c) { return (int8_t)c < -0x40; }
  169. /** Given the first (and possibly only) code unit of a UTF-8-encoded code
  170. point, returns the number of bytes occupied by that code point (in the
  171. range `[1, 4]`). Returns a value < 0 if `first_unit` is not a valid
  172. initial UTF-8 code unit. */
  173. inline constexpr int utf8_code_units(char8_type first_unit_)
  174. {
  175. auto first_unit = (unsigned int)first_unit_;
  176. return first_unit <= 0x7f ? 1
  177. : boost::parser::detail::text::lead_code_unit(first_unit)
  178. ? int(0xe0 <= first_unit) + int(0xf0 <= first_unit) + 2
  179. : -1;
  180. }
  181. /** Given the first (and possibly only) code unit of a UTF-16-encoded code
  182. point, returns the number of code units occupied by that code point
  183. (in the range `[1, 2]`). Returns a negative value if `first_unit` is
  184. not a valid initial UTF-16 code unit. */
  185. inline constexpr int utf16_code_units(char16_t first_unit)
  186. {
  187. if (boost::parser::detail::text::low_surrogate(first_unit))
  188. return -1;
  189. if (boost::parser::detail::text::high_surrogate(first_unit))
  190. return 2;
  191. return 1;
  192. }
  193. namespace detail {
  194. // optional is not constexpr friendly.
  195. template<typename Iter>
  196. struct optional_iter
  197. {
  198. constexpr optional_iter() : it_(), valid_(false) {}
  199. constexpr optional_iter(Iter it) : it_(it), valid_(true) {}
  200. constexpr operator bool() const { return valid_; }
  201. constexpr Iter operator*() const
  202. {
  203. BOOST_PARSER_DEBUG_ASSERT(valid_);
  204. return it_;
  205. }
  206. Iter & operator*()
  207. {
  208. BOOST_PARSER_DEBUG_ASSERT(valid_);
  209. return it_;
  210. }
  211. friend BOOST_PARSER_CONSTEXPR bool
  212. operator==(optional_iter lhs, optional_iter rhs)
  213. {
  214. return lhs.valid_ == rhs.valid_ &&
  215. (!lhs.valid_ || lhs.it_ == rhs.it_);
  216. }
  217. friend BOOST_PARSER_CONSTEXPR bool
  218. operator!=(optional_iter lhs, optional_iter rhs)
  219. {
  220. return !(lhs == rhs);
  221. }
  222. private:
  223. Iter it_;
  224. bool valid_;
  225. };
  226. // Follow Table 3-7 in Unicode 3.9/D92
  227. template<typename Iter>
  228. constexpr optional_iter<Iter> end_of_invalid_utf8(Iter it)
  229. {
  230. BOOST_PARSER_DEBUG_ASSERT(!boost::parser::detail::text::continuation(*it));
  231. if (detail::in(0, *it, 0x7f))
  232. return optional_iter<Iter>{};
  233. if (detail::in(0xc2, *it, 0xdf)) {
  234. auto next = it;
  235. if (!boost::parser::detail::text::continuation(*++next))
  236. return next;
  237. return optional_iter<Iter>{};
  238. }
  239. if (detail::in(0xe0, *it, 0xe0)) {
  240. auto next = it;
  241. if (!detail::in(0xa0, *++next, 0xbf))
  242. return next;
  243. if (!boost::parser::detail::text::continuation(*++next))
  244. return next;
  245. return optional_iter<Iter>{};
  246. }
  247. if (detail::in(0xe1, *it, 0xec)) {
  248. auto next = it;
  249. if (!boost::parser::detail::text::continuation(*++next))
  250. return next;
  251. if (!boost::parser::detail::text::continuation(*++next))
  252. return next;
  253. return optional_iter<Iter>{};
  254. }
  255. if (detail::in(0xed, *it, 0xed)) {
  256. auto next = it;
  257. if (!detail::in(0x80, *++next, 0x9f))
  258. return next;
  259. if (!boost::parser::detail::text::continuation(*++next))
  260. return next;
  261. return optional_iter<Iter>{};
  262. }
  263. if (detail::in(0xee, *it, 0xef)) {
  264. auto next = it;
  265. if (!boost::parser::detail::text::continuation(*++next))
  266. return next;
  267. if (!boost::parser::detail::text::continuation(*++next))
  268. return next;
  269. return optional_iter<Iter>{};
  270. }
  271. if (detail::in(0xf0, *it, 0xf0)) {
  272. auto next = it;
  273. if (!detail::in(0x90, *++next, 0xbf))
  274. return next;
  275. if (!boost::parser::detail::text::continuation(*++next))
  276. return next;
  277. if (!boost::parser::detail::text::continuation(*++next))
  278. return next;
  279. return optional_iter<Iter>{};
  280. }
  281. if (detail::in(0xf1, *it, 0xf3)) {
  282. auto next = it;
  283. if (!boost::parser::detail::text::continuation(*++next))
  284. return next;
  285. if (!boost::parser::detail::text::continuation(*++next))
  286. return next;
  287. if (!boost::parser::detail::text::continuation(*++next))
  288. return next;
  289. return optional_iter<Iter>{};
  290. }
  291. if (detail::in(0xf4, *it, 0xf4)) {
  292. auto next = it;
  293. if (!detail::in(0x80, *++next, 0x8f))
  294. return next;
  295. if (!boost::parser::detail::text::continuation(*++next))
  296. return next;
  297. if (!boost::parser::detail::text::continuation(*++next))
  298. return next;
  299. return optional_iter<Iter>{};
  300. }
  301. return it;
  302. }
  303. template<typename Iter>
  304. constexpr Iter decrement(Iter it)
  305. {
  306. Iter retval = it;
  307. int backup = 0;
  308. while (backup < 4 && boost::parser::detail::text::continuation(*--retval)) {
  309. ++backup;
  310. }
  311. backup = it - retval;
  312. if (boost::parser::detail::text::continuation(*retval))
  313. return it - 1;
  314. optional_iter<Iter> first_invalid = end_of_invalid_utf8(retval);
  315. if (first_invalid == retval)
  316. ++*first_invalid;
  317. while (first_invalid && (*first_invalid - retval) < backup) {
  318. backup -= *first_invalid - retval;
  319. retval = *first_invalid;
  320. first_invalid = end_of_invalid_utf8(retval);
  321. if (first_invalid == retval)
  322. ++*first_invalid;
  323. }
  324. if (1 < backup) {
  325. int const cp_bytes = boost::parser::detail::text::utf8_code_units(*retval);
  326. if (cp_bytes < backup)
  327. retval = it - 1;
  328. }
  329. return retval;
  330. }
  331. template<typename Iter>
  332. constexpr Iter decrement(Iter first, Iter it)
  333. {
  334. Iter retval = it;
  335. int backup = 0;
  336. while (backup < 4 && retval != first &&
  337. boost::parser::detail::text::continuation(*--retval)) {
  338. ++backup;
  339. }
  340. backup = (int)std::distance(retval, it);
  341. if (boost::parser::detail::text::continuation(*retval)) {
  342. if (it != first)
  343. --it;
  344. return it;
  345. }
  346. optional_iter<Iter> first_invalid = end_of_invalid_utf8(retval);
  347. if (first_invalid == retval)
  348. ++*first_invalid;
  349. while (first_invalid &&
  350. std::distance(retval, *first_invalid) < backup) {
  351. backup -= (int)std::distance(retval, *first_invalid);
  352. retval = *first_invalid;
  353. first_invalid = end_of_invalid_utf8(retval);
  354. if (first_invalid == retval)
  355. ++*first_invalid;
  356. }
  357. if (1 < backup) {
  358. int const cp_bytes = boost::parser::detail::text::utf8_code_units(*retval);
  359. if (cp_bytes < backup) {
  360. if (it != first)
  361. --it;
  362. retval = it;
  363. }
  364. }
  365. return retval;
  366. }
  367. enum char_class : uint8_t {
  368. ill = 0,
  369. asc = 1,
  370. cr1 = 2,
  371. cr2 = 3,
  372. cr3 = 4,
  373. l2a = 5,
  374. l3a = 6,
  375. l3b = 7,
  376. l3c = 8,
  377. l4a = 9,
  378. l4b = 10,
  379. l4c = 11,
  380. };
  381. enum table_state : uint8_t {
  382. bgn = 0,
  383. e_d = bgn, // "end"
  384. err = 12,
  385. cs1 = 24,
  386. cs2 = 36,
  387. cs3 = 48,
  388. p3a = 60,
  389. p3b = 72,
  390. p4a = 84,
  391. p4b = 96,
  392. invalid_table_state = 200
  393. };
  394. struct first_cu
  395. {
  396. unsigned char initial_octet;
  397. table_state next;
  398. };
  399. namespace {
  400. constexpr first_cu first_cus[256] = {
  401. {0x00, bgn}, {0x01, bgn}, {0x02, bgn}, {0x03, bgn}, {0x04, bgn},
  402. {0x05, bgn}, {0x06, bgn}, {0x07, bgn}, {0x08, bgn}, {0x09, bgn},
  403. {0x0a, bgn}, {0x0b, bgn}, {0x0c, bgn}, {0x0d, bgn}, {0x0e, bgn},
  404. {0x0f, bgn}, {0x10, bgn}, {0x11, bgn}, {0x12, bgn}, {0x13, bgn},
  405. {0x14, bgn}, {0x15, bgn}, {0x16, bgn}, {0x17, bgn}, {0x18, bgn},
  406. {0x19, bgn}, {0x1a, bgn}, {0x1b, bgn}, {0x1c, bgn}, {0x1d, bgn},
  407. {0x1e, bgn}, {0x1f, bgn}, {0x20, bgn}, {0x21, bgn}, {0x22, bgn},
  408. {0x23, bgn}, {0x24, bgn}, {0x25, bgn}, {0x26, bgn}, {0x27, bgn},
  409. {0x28, bgn}, {0x29, bgn}, {0x2a, bgn}, {0x2b, bgn}, {0x2c, bgn},
  410. {0x2d, bgn}, {0x2e, bgn}, {0x2f, bgn}, {0x30, bgn}, {0x31, bgn},
  411. {0x32, bgn}, {0x33, bgn}, {0x34, bgn}, {0x35, bgn}, {0x36, bgn},
  412. {0x37, bgn}, {0x38, bgn}, {0x39, bgn}, {0x3a, bgn}, {0x3b, bgn},
  413. {0x3c, bgn}, {0x3d, bgn}, {0x3e, bgn}, {0x3f, bgn}, {0x40, bgn},
  414. {0x41, bgn}, {0x42, bgn}, {0x43, bgn}, {0x44, bgn}, {0x45, bgn},
  415. {0x46, bgn}, {0x47, bgn}, {0x48, bgn}, {0x49, bgn}, {0x4a, bgn},
  416. {0x4b, bgn}, {0x4c, bgn}, {0x4d, bgn}, {0x4e, bgn}, {0x4f, bgn},
  417. {0x50, bgn}, {0x51, bgn}, {0x52, bgn}, {0x53, bgn}, {0x54, bgn},
  418. {0x55, bgn}, {0x56, bgn}, {0x57, bgn}, {0x58, bgn}, {0x59, bgn},
  419. {0x5a, bgn}, {0x5b, bgn}, {0x5c, bgn}, {0x5d, bgn}, {0x5e, bgn},
  420. {0x5f, bgn}, {0x60, bgn}, {0x61, bgn}, {0x62, bgn}, {0x63, bgn},
  421. {0x64, bgn}, {0x65, bgn}, {0x66, bgn}, {0x67, bgn}, {0x68, bgn},
  422. {0x69, bgn}, {0x6a, bgn}, {0x6b, bgn}, {0x6c, bgn}, {0x6d, bgn},
  423. {0x6e, bgn}, {0x6f, bgn}, {0x70, bgn}, {0x71, bgn}, {0x72, bgn},
  424. {0x73, bgn}, {0x74, bgn}, {0x75, bgn}, {0x76, bgn}, {0x77, bgn},
  425. {0x78, bgn}, {0x79, bgn}, {0x7a, bgn}, {0x7b, bgn}, {0x7c, bgn},
  426. {0x7d, bgn}, {0x7e, bgn}, {0x7f, bgn}, {0x00, err}, {0x01, err},
  427. {0x02, err}, {0x03, err}, {0x04, err}, {0x05, err}, {0x06, err},
  428. {0x07, err}, {0x08, err}, {0x09, err}, {0x0a, err}, {0x0b, err},
  429. {0x0c, err}, {0x0d, err}, {0x0e, err}, {0x0f, err}, {0x10, err},
  430. {0x11, err}, {0x12, err}, {0x13, err}, {0x14, err}, {0x15, err},
  431. {0x16, err}, {0x17, err}, {0x18, err}, {0x19, err}, {0x1a, err},
  432. {0x1b, err}, {0x1c, err}, {0x1d, err}, {0x1e, err}, {0x1f, err},
  433. {0x20, err}, {0x21, err}, {0x22, err}, {0x23, err}, {0x24, err},
  434. {0x25, err}, {0x26, err}, {0x27, err}, {0x28, err}, {0x29, err},
  435. {0x2a, err}, {0x2b, err}, {0x2c, err}, {0x2d, err}, {0x2e, err},
  436. {0x2f, err}, {0x30, err}, {0x31, err}, {0x32, err}, {0x33, err},
  437. {0x34, err}, {0x35, err}, {0x36, err}, {0x37, err}, {0x38, err},
  438. {0x39, err}, {0x3a, err}, {0x3b, err}, {0x3c, err}, {0x3d, err},
  439. {0x3e, err}, {0x3f, err}, {0xc0, err}, {0xc1, err}, {0x02, cs1},
  440. {0x03, cs1}, {0x04, cs1}, {0x05, cs1}, {0x06, cs1}, {0x07, cs1},
  441. {0x08, cs1}, {0x09, cs1}, {0x0a, cs1}, {0x0b, cs1}, {0x0c, cs1},
  442. {0x0d, cs1}, {0x0e, cs1}, {0x0f, cs1}, {0x10, cs1}, {0x11, cs1},
  443. {0x12, cs1}, {0x13, cs1}, {0x14, cs1}, {0x15, cs1}, {0x16, cs1},
  444. {0x17, cs1}, {0x18, cs1}, {0x19, cs1}, {0x1a, cs1}, {0x1b, cs1},
  445. {0x1c, cs1}, {0x1d, cs1}, {0x1e, cs1}, {0x1f, cs1}, {0x00, p3a},
  446. {0x01, cs2}, {0x02, cs2}, {0x03, cs2}, {0x04, cs2}, {0x05, cs2},
  447. {0x06, cs2}, {0x07, cs2}, {0x08, cs2}, {0x09, cs2}, {0x0a, cs2},
  448. {0x0b, cs2}, {0x0c, cs2}, {0x0d, p3b}, {0x0e, cs2}, {0x0f, cs2},
  449. {0x00, p4a}, {0x01, cs3}, {0x02, cs3}, {0x03, cs3}, {0x04, p4b},
  450. {0xf5, err}, {0xf6, err}, {0xf7, err}, {0xf8, err}, {0xf9, err},
  451. {0xfa, err}, {0xfb, err}, {0xfc, err}, {0xfd, err}, {0xfe, err},
  452. {0xff, err},
  453. };
  454. constexpr char_class octet_classes[256] = {
  455. asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
  456. asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
  457. asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
  458. asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
  459. asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
  460. asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
  461. asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
  462. asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
  463. asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
  464. asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, cr1, cr1,
  465. cr1, cr1, cr1, cr1, cr1, cr1, cr1, cr1, cr1, cr1, cr1, cr1, cr1,
  466. cr1, cr2, cr2, cr2, cr2, cr2, cr2, cr2, cr2, cr2, cr2, cr2, cr2,
  467. cr2, cr2, cr2, cr2, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3,
  468. cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3,
  469. cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, ill, ill, l2a,
  470. l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a,
  471. l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a,
  472. l2a, l2a, l2a, l3a, l3b, l3b, l3b, l3b, l3b, l3b, l3b, l3b, l3b,
  473. l3b, l3b, l3b, l3c, l3b, l3b, l4a, l4b, l4b, l4b, l4c, ill, ill,
  474. ill, ill, ill, ill, ill, ill, ill, ill, ill,
  475. };
  476. constexpr table_state transitions[108] = {
  477. err, e_d, err, err, err, cs1, p3a, cs2, p3b, p4a, cs3, p4b,
  478. err, err, err, err, err, err, err, err, err, err, err, err,
  479. err, err, e_d, e_d, e_d, err, err, err, err, err, err, err,
  480. err, err, cs1, cs1, cs1, err, err, err, err, err, err, err,
  481. err, err, cs2, cs2, cs2, err, err, err, err, err, err, err,
  482. err, err, err, err, cs1, err, err, err, err, err, err, err,
  483. err, err, cs1, cs1, err, err, err, err, err, err, err, err,
  484. err, err, err, cs2, cs2, err, err, err, err, err, err, err,
  485. err, err, cs2, err, err, err, err, err, err, err, err, err,
  486. };
  487. }
  488. template<typename InputIter, typename Sentinel>
  489. char32_t advance(InputIter & first, Sentinel last)
  490. {
  491. char32_t retval = 0;
  492. first_cu const info = first_cus[(unsigned char)*first];
  493. ++first;
  494. retval = info.initial_octet;
  495. int state = info.next;
  496. while (state != bgn) {
  497. if (first != last) {
  498. unsigned char const cu = *first;
  499. retval = (retval << 6) | (cu & 0x3f);
  500. char_class const class_ = octet_classes[cu];
  501. state = transitions[state + class_];
  502. if (state == err)
  503. return replacement_character;
  504. ++first;
  505. } else {
  506. return replacement_character;
  507. }
  508. }
  509. return retval;
  510. }
  511. template<typename Derived, typename Iter>
  512. struct trans_ins_iter
  513. {
  514. using value_type = void;
  515. using difference_type =
  516. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  517. std::ptrdiff_t;
  518. #else
  519. void;
  520. #endif
  521. using pointer = void;
  522. using reference = void;
  523. using iterator_category = std::output_iterator_tag;
  524. constexpr trans_ins_iter() {}
  525. constexpr trans_ins_iter(Iter it) : it_(it) {}
  526. constexpr Derived & operator*() { return derived(); }
  527. constexpr Derived & operator++() { return derived(); }
  528. constexpr Derived operator++(int) { return derived(); }
  529. constexpr Iter base() const { return it_; }
  530. protected:
  531. constexpr Iter & iter() { return it_; }
  532. private:
  533. constexpr Derived & derived()
  534. {
  535. return static_cast<Derived &>(*this);
  536. }
  537. Iter it_;
  538. };
  539. template<typename Derived, typename I, typename ValueType>
  540. using trans_iter = stl_interfaces::iterator_interface<
  541. Derived,
  542. bidirectional_at_most_t<I>,
  543. ValueType,
  544. ValueType>;
  545. }
  546. }}
  547. namespace boost::parser::detail { namespace text { BOOST_PARSER_DETAIL_TEXT_NAMESPACE_V1 {
  548. #if defined(BOOST_TEXT_DOXYGEN)
  549. /** Returns the first code unit in `[r.begin(), r.end())` that is not
  550. properly UTF-8 encoded, or `r.begin() + std::distance(r)` if no such
  551. code unit is found. */
  552. template<utf8_range R>
  553. requires std::ranges::forward_range<R>
  554. constexpr std::ranges::borrowed_iterator_t<R> find_invalid_encoding(R && r);
  555. /** Returns the first code unit in `[r.begin(), r.end())` that is not
  556. properly UTF-16 encoded, or `r.begin() + std::distance(r)` if no such
  557. code unit is found. */
  558. template<utf16_range R>
  559. requires std::ranges::forward_range<R>
  560. constexpr std::ranges::borrowed_iterator_t<R> find_invalid_encoding(R && r);
  561. /** Returns true iff `r` is properly UTF-8 encoded. */
  562. template<utf8_range R>
  563. requires std::ranges::forward_range<R>
  564. constexpr bool encoded(R && r);
  565. /** Returns true iff `r` is properly UTF-16 encoded */
  566. template<utf16_range R>
  567. requires std::ranges::forward_range<R>
  568. constexpr bool encoded(R && r);
  569. /** Returns true iff `r` is empty or the initial UTF-8 code units in `r`
  570. form a valid Unicode code point. */
  571. template<utf8_range R>
  572. requires std::ranges::forward_range<R>
  573. constexpr bool starts_encoded(R && r);
  574. /** Returns true iff `r` is empty or the initial UTF-16 code units in `r`
  575. form a valid Unicode code point. */
  576. template<utf16_range R>
  577. requires std::ranges::forward_range<R>
  578. constexpr bool starts_encoded(R && r);
  579. /** Returns true iff `r` is empty or the final UTF-8 code units in `r`
  580. form a valid Unicode code point. */
  581. template<utf8_range R>
  582. requires std::ranges::bidirectional_range<R> &&
  583. std::ranges::common_range<R>
  584. constexpr bool ends_encoded(R && r);
  585. /** Returns true iff `r` is empty or the final UTF-16 code units in `r`
  586. form a valid Unicode code point. */
  587. template<utf8_range R>
  588. requires std::ranges::bidirectional_range<R> &&
  589. std::ranges::common_range<R>
  590. constexpr bool ends_encoded(R && r);
  591. #endif
  592. template<typename Iter>
  593. constexpr detail::enable_utf8_cp_t<Iter>
  594. find_invalid_encoding(Iter first, Iter last)
  595. {
  596. while (first != last) {
  597. int const cp_bytes = boost::parser::detail::text::utf8_code_units(*first);
  598. if (cp_bytes == -1 || last - first < cp_bytes)
  599. return first;
  600. if (detail::end_of_invalid_utf8(first))
  601. return first;
  602. first += cp_bytes;
  603. }
  604. return last;
  605. }
  606. template<typename Iter>
  607. constexpr detail::enable_utf16_cp_t<Iter>
  608. find_invalid_encoding(Iter first, Iter last)
  609. {
  610. while (first != last) {
  611. int const cp_units = boost::parser::detail::text::utf16_code_units(*first);
  612. if (cp_units == -1 || last - first < cp_units)
  613. return first;
  614. if (cp_units == 2 && !boost::parser::detail::text::low_surrogate(*(first + 1)))
  615. return first;
  616. first += cp_units;
  617. }
  618. return last;
  619. }
  620. template<typename Iter>
  621. constexpr detail::enable_utf8_cp_t<Iter, bool> encoded(
  622. Iter first, Iter last)
  623. {
  624. return v1::find_invalid_encoding(first, last) == last;
  625. }
  626. template<typename Iter>
  627. constexpr detail::enable_utf16_cp_t<Iter, bool> encoded(
  628. Iter first, Iter last)
  629. {
  630. return v1::find_invalid_encoding(first, last) == last;
  631. }
  632. template<typename Iter>
  633. constexpr detail::enable_utf8_cp_t<Iter, bool>
  634. starts_encoded(Iter first, Iter last)
  635. {
  636. if (first == last)
  637. return true;
  638. int const cp_bytes = boost::parser::detail::text::utf8_code_units(*first);
  639. if (cp_bytes == -1 || last - first < cp_bytes)
  640. return false;
  641. return !detail::end_of_invalid_utf8(first);
  642. }
  643. template<typename Iter>
  644. constexpr detail::enable_utf16_cp_t<Iter, bool>
  645. starts_encoded(Iter first, Iter last)
  646. {
  647. if (first == last)
  648. return true;
  649. int const cp_units = boost::parser::detail::text::utf16_code_units(*first);
  650. if (cp_units == -1 || last - first < cp_units)
  651. return false;
  652. return cp_units == 1 || boost::parser::detail::text::low_surrogate(*(first + 1));
  653. }
  654. template<typename Iter>
  655. constexpr detail::enable_utf8_cp_t<Iter, bool>
  656. ends_encoded(Iter first, Iter last)
  657. {
  658. if (first == last)
  659. return true;
  660. auto it = last;
  661. while (first != --it && boost::parser::detail::text::continuation(*it))
  662. ;
  663. return v1::starts_encoded(it, last);
  664. }
  665. template<typename Iter>
  666. constexpr detail::enable_utf16_cp_t<Iter, bool>
  667. ends_encoded(Iter first, Iter last)
  668. {
  669. if (first == last)
  670. return true;
  671. auto it = last;
  672. if (boost::parser::detail::text::low_surrogate(*--it))
  673. --it;
  674. return v1::starts_encoded(it, last);
  675. }
  676. }}}
  677. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  678. namespace boost::parser::detail { namespace text { BOOST_PARSER_DETAIL_TEXT_NAMESPACE_V2 {
  679. template<utf8_range R>
  680. requires std::ranges::forward_range<R>
  681. constexpr std::ranges::borrowed_iterator_t<R> find_invalid_encoding(R && r)
  682. {
  683. auto first = std::ranges::begin(r);
  684. auto last = std::ranges::end(r);
  685. while (first != last) {
  686. int const cp_bytes = boost::parser::detail::text::utf8_code_units(*first);
  687. if (cp_bytes == -1 || last - first < cp_bytes)
  688. return first;
  689. if (detail::end_of_invalid_utf8(first))
  690. return first;
  691. first += cp_bytes;
  692. }
  693. if constexpr (std::ranges::borrowed_range<R>) {
  694. return last;
  695. } else {
  696. return std::ranges::dangling{};
  697. }
  698. }
  699. template<utf16_range R>
  700. requires std::ranges::forward_range<R>
  701. constexpr std::ranges::borrowed_iterator_t<R> find_invalid_encoding(R && r)
  702. {
  703. auto first = std::ranges::begin(r);
  704. auto last = std::ranges::end(r);
  705. while (first != last) {
  706. int const cp_units = boost::parser::detail::text::utf16_code_units(*first);
  707. if (cp_units == -1 || last - first < cp_units)
  708. return first;
  709. if (cp_units == 2 && !boost::parser::detail::text::low_surrogate(*(first + 1)))
  710. return first;
  711. first += cp_units;
  712. }
  713. if constexpr (std::ranges::borrowed_range<R>) {
  714. return last;
  715. } else {
  716. return std::ranges::dangling{};
  717. }
  718. }
  719. template<utf8_range R>
  720. requires std::ranges::forward_range<R>
  721. constexpr bool encoded(R && r)
  722. {
  723. return boost::parser::detail::text::v1::find_invalid_encoding(r.begin(), r.end()) ==
  724. r.end();
  725. }
  726. template<utf16_range R>
  727. requires std::ranges::forward_range<R>
  728. constexpr bool encoded(R && r)
  729. {
  730. return boost::parser::detail::text::v1::find_invalid_encoding(r.begin(), r.end()) ==
  731. r.end();
  732. }
  733. template<utf8_range R>
  734. requires std::ranges::forward_range<R>
  735. constexpr bool starts_encoded(R && r)
  736. {
  737. auto first = std::ranges::begin(r);
  738. auto last = std::ranges::end(r);
  739. if (first == last)
  740. return true;
  741. int const cp_bytes = boost::parser::detail::text::utf8_code_units(*first);
  742. if (cp_bytes == -1 || last - first < cp_bytes)
  743. return false;
  744. return !detail::end_of_invalid_utf8(first);
  745. }
  746. template<utf16_range R>
  747. requires std::ranges::forward_range<R>
  748. constexpr bool starts_encoded(R && r)
  749. {
  750. auto first = std::ranges::begin(r);
  751. auto last = std::ranges::end(r);
  752. if (first == last)
  753. return true;
  754. int const cp_units = boost::parser::detail::text::utf16_code_units(*first);
  755. if (cp_units == -1 || last - first < cp_units)
  756. return false;
  757. return cp_units == 1 || boost::parser::detail::text::low_surrogate(*(first + 1));
  758. }
  759. template<utf8_range R>
  760. requires std::ranges::bidirectional_range<R> &&
  761. std::ranges::common_range<R>
  762. constexpr bool ends_encoded(R && r)
  763. {
  764. auto first = std::ranges::begin(r);
  765. auto last = std::ranges::end(r);
  766. if (first == last)
  767. return true;
  768. auto it = last;
  769. while (first != --it && boost::parser::detail::text::continuation(*it))
  770. ;
  771. return boost::parser::detail::text::starts_encoded(it, last);
  772. }
  773. template<utf16_range R>
  774. requires std::ranges::bidirectional_range<R> &&
  775. std::ranges::common_range<R>
  776. constexpr bool ends_encoded(R && r)
  777. {
  778. auto first = std::ranges::begin(r);
  779. auto last = std::ranges::end(r);
  780. if (first == last)
  781. return true;
  782. auto it = last;
  783. if (boost::parser::detail::text::low_surrogate(*--it))
  784. --it;
  785. return boost::parser::detail::text::starts_encoded(it, last);
  786. }
  787. }}}
  788. #endif
  789. namespace boost::parser::detail { namespace text {
  790. /** An error handler type that can be used with the converting iterators;
  791. provides the Unicode replacement character on errors. */
  792. struct use_replacement_character
  793. {
  794. constexpr char32_t operator()(std::string_view) const noexcept
  795. {
  796. return replacement_character;
  797. }
  798. };
  799. /** A sentinel type that compares equal to a pointer to a 1-, 2-, or
  800. 4-byte integral value, iff the pointer is null. */
  801. struct null_sentinel_t
  802. {
  803. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  804. template<std::input_iterator I>
  805. requires std::default_initializable<std::iter_value_t<I>> &&
  806. std::equality_comparable_with<std::iter_reference_t<I>, std::iter_value_t<I>>
  807. #else
  808. template<typename I>
  809. #endif
  810. friend constexpr bool operator==(I it, null_sentinel_t)
  811. {
  812. return *it == detail::iter_value_t<I>{};
  813. }
  814. #if !defined(__cpp_impl_three_way_comparison)
  815. template<typename I>
  816. friend constexpr bool operator==(null_sentinel_t, I it)
  817. {
  818. return *it == detail::iter_value_t<I>{};
  819. }
  820. template<typename I>
  821. friend constexpr bool operator!=(I it, null_sentinel_t)
  822. {
  823. return *it != detail::iter_value_t<I>{};
  824. }
  825. template<typename I>
  826. friend constexpr bool operator!=(null_sentinel_t, I it)
  827. {
  828. return *it != detail::iter_value_t<I>{};
  829. }
  830. #endif
  831. };
  832. #if defined(__cpp_inline_variables)
  833. inline constexpr null_sentinel_t null_sentinel;
  834. #else
  835. namespace {
  836. constexpr null_sentinel_t null_sentinel;
  837. }
  838. #endif
  839. /** An out iterator that converts UTF-32 to UTF-8. */
  840. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  841. template<std::output_iterator<char8_t> Iter>
  842. #else
  843. template<typename Iter>
  844. #endif
  845. struct utf_32_to_8_out_iterator
  846. : detail::trans_ins_iter<utf_32_to_8_out_iterator<Iter>, Iter>
  847. {
  848. constexpr utf_32_to_8_out_iterator() {}
  849. explicit constexpr utf_32_to_8_out_iterator(Iter it) :
  850. detail::trans_ins_iter<utf_32_to_8_out_iterator<Iter>, Iter>(it)
  851. {}
  852. constexpr utf_32_to_8_out_iterator & operator=(char32_t cp)
  853. {
  854. auto & out = this->iter();
  855. out = detail::write_cp_utf8(cp, out);
  856. return *this;
  857. }
  858. };
  859. /** An insert-iterator analogous to std::insert_iterator, that also
  860. converts UTF-32 to UTF-8. */
  861. template<typename Cont>
  862. struct utf_32_to_8_insert_iterator : detail::trans_ins_iter<
  863. utf_32_to_8_insert_iterator<Cont>,
  864. std::insert_iterator<Cont>>
  865. {
  866. constexpr utf_32_to_8_insert_iterator() {}
  867. constexpr utf_32_to_8_insert_iterator(
  868. Cont & c, typename Cont::iterator it) :
  869. detail::trans_ins_iter<
  870. utf_32_to_8_insert_iterator<Cont>,
  871. std::insert_iterator<Cont>>(std::insert_iterator<Cont>(c, it))
  872. {}
  873. constexpr utf_32_to_8_insert_iterator & operator=(char32_t cp)
  874. {
  875. auto & out = this->iter();
  876. out = detail::write_cp_utf8(cp, out);
  877. return *this;
  878. }
  879. };
  880. /** An insert-iterator analogous to std::front_insert_iterator, that also
  881. converts UTF-32 to UTF-8. */
  882. template<typename Cont>
  883. struct utf_32_to_8_front_insert_iterator
  884. : detail::trans_ins_iter<
  885. utf_32_to_8_front_insert_iterator<Cont>,
  886. std::front_insert_iterator<Cont>>
  887. {
  888. constexpr utf_32_to_8_front_insert_iterator() {}
  889. explicit constexpr utf_32_to_8_front_insert_iterator(Cont & c) :
  890. detail::trans_ins_iter<
  891. utf_32_to_8_front_insert_iterator<Cont>,
  892. std::front_insert_iterator<Cont>>(
  893. std::front_insert_iterator<Cont>(c))
  894. {}
  895. constexpr utf_32_to_8_front_insert_iterator & operator=(char32_t cp)
  896. {
  897. auto & out = this->iter();
  898. out = detail::write_cp_utf8(cp, out);
  899. return *this;
  900. }
  901. };
  902. /** An insert-iterator analogous to std::back_insert_iterator, that also
  903. converts UTF-32 to UTF-8. */
  904. template<typename Cont>
  905. struct utf_32_to_8_back_insert_iterator
  906. : detail::trans_ins_iter<
  907. utf_32_to_8_back_insert_iterator<Cont>,
  908. std::back_insert_iterator<Cont>>
  909. {
  910. constexpr utf_32_to_8_back_insert_iterator() {}
  911. explicit constexpr utf_32_to_8_back_insert_iterator(Cont & c) :
  912. detail::trans_ins_iter<
  913. utf_32_to_8_back_insert_iterator<Cont>,
  914. std::back_insert_iterator<Cont>>(
  915. std::back_insert_iterator<Cont>(c))
  916. {}
  917. constexpr utf_32_to_8_back_insert_iterator & operator=(char32_t cp)
  918. {
  919. auto & out = this->iter();
  920. out = detail::write_cp_utf8(cp, out);
  921. return *this;
  922. }
  923. };
  924. namespace detail {
  925. template<typename OutIter>
  926. OutIter assign_8_to_32_insert(
  927. unsigned char cu, char32_t & cp, int & state, OutIter out)
  928. {
  929. auto write = [&] {
  930. *out = cp;
  931. ++out;
  932. state = invalid_table_state;
  933. };
  934. auto start_cp = [&] {
  935. first_cu const info = first_cus[cu];
  936. state = info.next;
  937. cp = info.initial_octet;
  938. if (state == bgn)
  939. write();
  940. };
  941. if (state == invalid_table_state) {
  942. start_cp();
  943. } else {
  944. cp = (cp << 6) | (cu & 0x3f);
  945. char_class const class_ = octet_classes[cu];
  946. state = transitions[state + class_];
  947. if (state == bgn) {
  948. write();
  949. } else if (state == err) {
  950. *out = replacement_character;
  951. ++out;
  952. start_cp();
  953. }
  954. }
  955. return out;
  956. }
  957. }
  958. /** An out iterator that converts UTF-8 to UTF-32. */
  959. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  960. template<std::output_iterator<char32_t> Iter>
  961. #else
  962. template<typename Iter>
  963. #endif
  964. struct utf_8_to_32_out_iterator
  965. : detail::trans_ins_iter<utf_8_to_32_out_iterator<Iter>, Iter>
  966. {
  967. constexpr utf_8_to_32_out_iterator() {}
  968. explicit constexpr utf_8_to_32_out_iterator(Iter it) :
  969. detail::trans_ins_iter<utf_8_to_32_out_iterator<Iter>, Iter>(it),
  970. state_(detail::invalid_table_state)
  971. {}
  972. constexpr utf_8_to_32_out_iterator & operator=(char8_type cu)
  973. {
  974. auto & out = this->iter();
  975. out = detail::assign_8_to_32_insert(cu, cp_, state_, out);
  976. return *this;
  977. }
  978. #ifndef BOOST_TEXT_DOXYGEN
  979. private:
  980. int state_;
  981. char32_t cp_;
  982. #endif
  983. };
  984. /** An insert-iterator analogous to std::insert_iterator, that also
  985. converts UTF-8 to UTF-32. */
  986. template<typename Cont>
  987. struct utf_8_to_32_insert_iterator : detail::trans_ins_iter<
  988. utf_8_to_32_insert_iterator<Cont>,
  989. std::insert_iterator<Cont>>
  990. {
  991. constexpr utf_8_to_32_insert_iterator() {}
  992. constexpr utf_8_to_32_insert_iterator(
  993. Cont & c, typename Cont::iterator it) :
  994. detail::trans_ins_iter<
  995. utf_8_to_32_insert_iterator<Cont>,
  996. std::insert_iterator<Cont>>(std::insert_iterator<Cont>(c, it)),
  997. state_(detail::invalid_table_state)
  998. {}
  999. constexpr utf_8_to_32_insert_iterator & operator=(char16_t cu)
  1000. {
  1001. auto & out = this->iter();
  1002. out = detail::assign_8_to_32_insert(cu, cp_, state_, out);
  1003. return *this;
  1004. }
  1005. #ifndef BOOST_TEXT_DOXYGEN
  1006. private:
  1007. int state_;
  1008. char32_t cp_;
  1009. #endif
  1010. };
  1011. /** An insert-iterator analogous to std::front_insert_iterator, that also
  1012. converts UTF-8 to UTF-32. */
  1013. template<typename Cont>
  1014. struct utf_8_to_32_front_insert_iterator
  1015. : detail::trans_ins_iter<
  1016. utf_8_to_32_front_insert_iterator<Cont>,
  1017. std::front_insert_iterator<Cont>>
  1018. {
  1019. constexpr utf_8_to_32_front_insert_iterator() {}
  1020. explicit constexpr utf_8_to_32_front_insert_iterator(Cont & c) :
  1021. detail::trans_ins_iter<
  1022. utf_8_to_32_front_insert_iterator<Cont>,
  1023. std::front_insert_iterator<Cont>>(
  1024. std::front_insert_iterator<Cont>(c)),
  1025. state_(detail::invalid_table_state)
  1026. {}
  1027. constexpr utf_8_to_32_front_insert_iterator & operator=(char16_t cu)
  1028. {
  1029. auto & out = this->iter();
  1030. out = detail::assign_8_to_32_insert(cu, cp_, state_, out);
  1031. return *this;
  1032. }
  1033. #ifndef BOOST_TEXT_DOXYGEN
  1034. private:
  1035. int state_;
  1036. char32_t cp_;
  1037. #endif
  1038. };
  1039. /** An insert-iterator analogous to std::back_insert_iterator, that also
  1040. converts UTF-8 to UTF-32. */
  1041. template<typename Cont>
  1042. struct utf_8_to_32_back_insert_iterator
  1043. : detail::trans_ins_iter<
  1044. utf_8_to_32_back_insert_iterator<Cont>,
  1045. std::back_insert_iterator<Cont>>
  1046. {
  1047. constexpr utf_8_to_32_back_insert_iterator() {}
  1048. explicit constexpr utf_8_to_32_back_insert_iterator(Cont & c) :
  1049. detail::trans_ins_iter<
  1050. utf_8_to_32_back_insert_iterator<Cont>,
  1051. std::back_insert_iterator<Cont>>(
  1052. std::back_insert_iterator<Cont>(c)),
  1053. state_(detail::invalid_table_state)
  1054. {}
  1055. constexpr utf_8_to_32_back_insert_iterator & operator=(char16_t cu)
  1056. {
  1057. auto & out = this->iter();
  1058. out = detail::assign_8_to_32_insert(cu, cp_, state_, out);
  1059. return *this;
  1060. }
  1061. #ifndef BOOST_TEXT_DOXYGEN
  1062. private:
  1063. int state_;
  1064. char32_t cp_;
  1065. #endif
  1066. };
  1067. /** An out iterator that converts UTF-8 to UTF-16. */
  1068. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  1069. template<std::output_iterator<char16_t> Iter>
  1070. #else
  1071. template<typename Iter>
  1072. #endif
  1073. struct utf_32_to_16_out_iterator
  1074. : detail::trans_ins_iter<utf_32_to_16_out_iterator<Iter>, Iter>
  1075. {
  1076. constexpr utf_32_to_16_out_iterator() {}
  1077. explicit constexpr utf_32_to_16_out_iterator(Iter it) :
  1078. detail::trans_ins_iter<utf_32_to_16_out_iterator<Iter>, Iter>(it)
  1079. {}
  1080. constexpr utf_32_to_16_out_iterator & operator=(char32_t cp)
  1081. {
  1082. auto & out = this->iter();
  1083. out = detail::write_cp_utf16(cp, out);
  1084. return *this;
  1085. }
  1086. };
  1087. /** An insert-iterator analogous to std::insert_iterator, that also
  1088. converts UTF-32 to UTF-16. */
  1089. template<typename Cont>
  1090. struct utf_32_to_16_insert_iterator
  1091. : detail::trans_ins_iter<
  1092. utf_32_to_16_insert_iterator<Cont>,
  1093. std::insert_iterator<Cont>>
  1094. {
  1095. constexpr utf_32_to_16_insert_iterator() {}
  1096. constexpr utf_32_to_16_insert_iterator(
  1097. Cont & c, typename Cont::iterator it) :
  1098. detail::trans_ins_iter<
  1099. utf_32_to_16_insert_iterator<Cont>,
  1100. std::insert_iterator<Cont>>(std::insert_iterator<Cont>(c, it))
  1101. {}
  1102. constexpr utf_32_to_16_insert_iterator & operator=(char32_t cp)
  1103. {
  1104. auto & out = this->iter();
  1105. out = detail::write_cp_utf16(cp, out);
  1106. return *this;
  1107. }
  1108. };
  1109. /** An insert-iterator analogous to std::front_insert_iterator, that also
  1110. converts UTF-32 to UTF-16. */
  1111. template<typename Cont>
  1112. struct utf_32_to_16_front_insert_iterator
  1113. : detail::trans_ins_iter<
  1114. utf_32_to_16_front_insert_iterator<Cont>,
  1115. std::front_insert_iterator<Cont>>
  1116. {
  1117. constexpr utf_32_to_16_front_insert_iterator() {}
  1118. explicit constexpr utf_32_to_16_front_insert_iterator(Cont & c) :
  1119. detail::trans_ins_iter<
  1120. utf_32_to_16_front_insert_iterator<Cont>,
  1121. std::front_insert_iterator<Cont>>(
  1122. std::front_insert_iterator<Cont>(c))
  1123. {}
  1124. constexpr utf_32_to_16_front_insert_iterator & operator=(char32_t cp)
  1125. {
  1126. auto & out = this->iter();
  1127. out = detail::write_cp_utf16(cp, out);
  1128. return *this;
  1129. }
  1130. };
  1131. /** An insert-iterator analogous to std::back_insert_iterator, that also
  1132. converts UTF-32 to UTF-16. */
  1133. template<typename Cont>
  1134. struct utf_32_to_16_back_insert_iterator
  1135. : detail::trans_ins_iter<
  1136. utf_32_to_16_back_insert_iterator<Cont>,
  1137. std::back_insert_iterator<Cont>>
  1138. {
  1139. constexpr utf_32_to_16_back_insert_iterator() {}
  1140. explicit constexpr utf_32_to_16_back_insert_iterator(Cont & c) :
  1141. detail::trans_ins_iter<
  1142. utf_32_to_16_back_insert_iterator<Cont>,
  1143. std::back_insert_iterator<Cont>>(
  1144. std::back_insert_iterator<Cont>(c))
  1145. {}
  1146. constexpr utf_32_to_16_back_insert_iterator & operator=(char32_t cp)
  1147. {
  1148. auto & out = this->iter();
  1149. out = detail::write_cp_utf16(cp, out);
  1150. return *this;
  1151. }
  1152. };
  1153. namespace detail {
  1154. template<typename OutIter>
  1155. OutIter
  1156. assign_16_to_32_insert(char16_t & prev_cu, char16_t cu, OutIter out)
  1157. {
  1158. if (high_surrogate(cu)) {
  1159. if (prev_cu) {
  1160. *out = replacement_character;
  1161. ++out;
  1162. }
  1163. prev_cu = cu;
  1164. } else if (low_surrogate(cu)) {
  1165. if (prev_cu) {
  1166. *out = detail::surrogates_to_cp(prev_cu, cu);
  1167. ++out;
  1168. } else {
  1169. *out = replacement_character;
  1170. ++out;
  1171. }
  1172. prev_cu = 0;
  1173. } else {
  1174. if (prev_cu) {
  1175. *out = replacement_character;
  1176. ++out;
  1177. }
  1178. *out = cu;
  1179. ++out;
  1180. prev_cu = 0;
  1181. }
  1182. return out;
  1183. }
  1184. }
  1185. /** An out iterator that converts UTF-16 to UTF-32. */
  1186. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  1187. template<std::output_iterator<char32_t> Iter>
  1188. #else
  1189. template<typename Iter>
  1190. #endif
  1191. struct utf_16_to_32_out_iterator
  1192. : detail::trans_ins_iter<utf_16_to_32_out_iterator<Iter>, Iter>
  1193. {
  1194. constexpr utf_16_to_32_out_iterator() {}
  1195. explicit constexpr utf_16_to_32_out_iterator(Iter it) :
  1196. detail::trans_ins_iter<utf_16_to_32_out_iterator<Iter>, Iter>(it),
  1197. prev_cu_(0)
  1198. {}
  1199. constexpr utf_16_to_32_out_iterator & operator=(char16_t cu)
  1200. {
  1201. auto & out = this->iter();
  1202. out = detail::assign_16_to_32_insert(prev_cu_, cu, out);
  1203. return *this;
  1204. }
  1205. #ifndef BOOST_TEXT_DOXYGEN
  1206. private:
  1207. char16_t prev_cu_;
  1208. #endif
  1209. };
  1210. /** An insert-iterator analogous to std::insert_iterator, that also
  1211. converts UTF-16 to UTF-32. */
  1212. template<typename Cont>
  1213. struct utf_16_to_32_insert_iterator
  1214. : detail::trans_ins_iter<
  1215. utf_16_to_32_insert_iterator<Cont>,
  1216. std::insert_iterator<Cont>>
  1217. {
  1218. constexpr utf_16_to_32_insert_iterator() {}
  1219. constexpr utf_16_to_32_insert_iterator(
  1220. Cont & c, typename Cont::iterator it) :
  1221. detail::trans_ins_iter<
  1222. utf_16_to_32_insert_iterator<Cont>,
  1223. std::insert_iterator<Cont>>(std::insert_iterator<Cont>(c, it)),
  1224. prev_cu_(0)
  1225. {}
  1226. constexpr utf_16_to_32_insert_iterator & operator=(char16_t cu)
  1227. {
  1228. auto & out = this->iter();
  1229. out = detail::assign_16_to_32_insert(prev_cu_, cu, out);
  1230. return *this;
  1231. }
  1232. #ifndef BOOST_TEXT_DOXYGEN
  1233. private:
  1234. char16_t prev_cu_;
  1235. #endif
  1236. };
  1237. /** An insert-iterator analogous to std::front_insert_iterator, that also
  1238. converts UTF-16 to UTF-32. */
  1239. template<typename Cont>
  1240. struct utf_16_to_32_front_insert_iterator
  1241. : detail::trans_ins_iter<
  1242. utf_16_to_32_front_insert_iterator<Cont>,
  1243. std::front_insert_iterator<Cont>>
  1244. {
  1245. constexpr utf_16_to_32_front_insert_iterator() {}
  1246. explicit constexpr utf_16_to_32_front_insert_iterator(Cont & c) :
  1247. detail::trans_ins_iter<
  1248. utf_16_to_32_front_insert_iterator<Cont>,
  1249. std::front_insert_iterator<Cont>>(
  1250. std::front_insert_iterator<Cont>(c)),
  1251. prev_cu_(0)
  1252. {}
  1253. constexpr utf_16_to_32_front_insert_iterator & operator=(char16_t cu)
  1254. {
  1255. auto & out = this->iter();
  1256. out = detail::assign_16_to_32_insert(prev_cu_, cu, out);
  1257. return *this;
  1258. }
  1259. #ifndef BOOST_TEXT_DOXYGEN
  1260. private:
  1261. char16_t prev_cu_;
  1262. #endif
  1263. };
  1264. /** An insert-iterator analogous to std::back_insert_iterator, that also
  1265. converts UTF-16 to UTF-32. */
  1266. template<typename Cont>
  1267. struct utf_16_to_32_back_insert_iterator
  1268. : detail::trans_ins_iter<
  1269. utf_16_to_32_back_insert_iterator<Cont>,
  1270. std::back_insert_iterator<Cont>>
  1271. {
  1272. constexpr utf_16_to_32_back_insert_iterator() {}
  1273. explicit constexpr utf_16_to_32_back_insert_iterator(Cont & c) :
  1274. detail::trans_ins_iter<
  1275. utf_16_to_32_back_insert_iterator<Cont>,
  1276. std::back_insert_iterator<Cont>>(
  1277. std::back_insert_iterator<Cont>(c)),
  1278. prev_cu_(0)
  1279. {}
  1280. constexpr utf_16_to_32_back_insert_iterator & operator=(char16_t cu)
  1281. {
  1282. auto & out = this->iter();
  1283. out = detail::assign_16_to_32_insert(prev_cu_, cu, out);
  1284. return *this;
  1285. }
  1286. #ifndef BOOST_TEXT_DOXYGEN
  1287. private:
  1288. char16_t prev_cu_;
  1289. #endif
  1290. };
  1291. namespace detail {
  1292. template<typename OutIter>
  1293. OutIter
  1294. assign_16_to_8_insert(char16_t & prev_cu, char16_t cu, OutIter out)
  1295. {
  1296. if (high_surrogate(cu)) {
  1297. if (prev_cu)
  1298. out = detail::write_cp_utf8(replacement_character, out);
  1299. prev_cu = cu;
  1300. } else if (low_surrogate(cu)) {
  1301. if (prev_cu) {
  1302. auto const cp = detail::surrogates_to_cp(prev_cu, cu);
  1303. out = detail::write_cp_utf8(cp, out);
  1304. } else {
  1305. out = detail::write_cp_utf8(replacement_character, out);
  1306. }
  1307. prev_cu = 0;
  1308. } else {
  1309. if (prev_cu)
  1310. out = detail::write_cp_utf8(replacement_character, out);
  1311. out = detail::write_cp_utf8(cu, out);
  1312. prev_cu = 0;
  1313. }
  1314. return out;
  1315. }
  1316. }
  1317. /** An out iterator that converts UTF-16 to UTF-8. */
  1318. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  1319. template<std::output_iterator<char8_t> Iter>
  1320. #else
  1321. template<typename Iter>
  1322. #endif
  1323. struct utf_16_to_8_out_iterator
  1324. : detail::trans_ins_iter<utf_16_to_8_out_iterator<Iter>, Iter>
  1325. {
  1326. constexpr utf_16_to_8_out_iterator() {}
  1327. explicit constexpr utf_16_to_8_out_iterator(Iter it) :
  1328. detail::trans_ins_iter<utf_16_to_8_out_iterator<Iter>, Iter>(it),
  1329. prev_cu_(0)
  1330. {}
  1331. constexpr utf_16_to_8_out_iterator & operator=(char16_t cu)
  1332. {
  1333. auto & out = this->iter();
  1334. out = detail::assign_16_to_8_insert(prev_cu_, cu, out);
  1335. return *this;
  1336. }
  1337. #ifndef BOOST_TEXT_DOXYGEN
  1338. private:
  1339. char16_t prev_cu_;
  1340. #endif
  1341. };
  1342. /** An insert-iterator analogous to std::insert_iterator, that also
  1343. converts UTF-16 to UTF-8. */
  1344. template<typename Cont>
  1345. struct utf_16_to_8_insert_iterator : detail::trans_ins_iter<
  1346. utf_16_to_8_insert_iterator<Cont>,
  1347. std::insert_iterator<Cont>>
  1348. {
  1349. constexpr utf_16_to_8_insert_iterator() {}
  1350. constexpr utf_16_to_8_insert_iterator(
  1351. Cont & c, typename Cont::iterator it) :
  1352. detail::trans_ins_iter<
  1353. utf_16_to_8_insert_iterator<Cont>,
  1354. std::insert_iterator<Cont>>(std::insert_iterator<Cont>(c, it)),
  1355. prev_cu_(0)
  1356. {}
  1357. constexpr utf_16_to_8_insert_iterator & operator=(char16_t cu)
  1358. {
  1359. auto & out = this->iter();
  1360. out = detail::assign_16_to_8_insert(prev_cu_, cu, out);
  1361. return *this;
  1362. }
  1363. #ifndef BOOST_TEXT_DOXYGEN
  1364. private:
  1365. char16_t prev_cu_;
  1366. #endif
  1367. };
  1368. /** An insert-iterator analogous to std::front_insert_iterator, that also
  1369. converts UTF-16 to UTF-8. */
  1370. template<typename Cont>
  1371. struct utf_16_to_8_front_insert_iterator
  1372. : detail::trans_ins_iter<
  1373. utf_16_to_8_front_insert_iterator<Cont>,
  1374. std::front_insert_iterator<Cont>>
  1375. {
  1376. constexpr utf_16_to_8_front_insert_iterator() {}
  1377. explicit constexpr utf_16_to_8_front_insert_iterator(Cont & c) :
  1378. detail::trans_ins_iter<
  1379. utf_16_to_8_front_insert_iterator<Cont>,
  1380. std::front_insert_iterator<Cont>>(
  1381. std::front_insert_iterator<Cont>(c)),
  1382. prev_cu_(0)
  1383. {}
  1384. constexpr utf_16_to_8_front_insert_iterator & operator=(char16_t cu)
  1385. {
  1386. auto & out = this->iter();
  1387. out = detail::assign_16_to_8_insert(prev_cu_, cu, out);
  1388. return *this;
  1389. }
  1390. #ifndef BOOST_TEXT_DOXYGEN
  1391. private:
  1392. char16_t prev_cu_;
  1393. #endif
  1394. };
  1395. /** An insert-iterator analogous to std::back_insert_iterator, that also
  1396. converts UTF-16 to UTF-8. */
  1397. template<typename Cont>
  1398. struct utf_16_to_8_back_insert_iterator
  1399. : detail::trans_ins_iter<
  1400. utf_16_to_8_back_insert_iterator<Cont>,
  1401. std::back_insert_iterator<Cont>>
  1402. {
  1403. constexpr utf_16_to_8_back_insert_iterator() {}
  1404. explicit constexpr utf_16_to_8_back_insert_iterator(Cont & c) :
  1405. detail::trans_ins_iter<
  1406. utf_16_to_8_back_insert_iterator<Cont>,
  1407. std::back_insert_iterator<Cont>>(
  1408. std::back_insert_iterator<Cont>(c)),
  1409. prev_cu_(0)
  1410. {}
  1411. constexpr utf_16_to_8_back_insert_iterator & operator=(char16_t cu)
  1412. {
  1413. auto & out = this->iter();
  1414. out = detail::assign_16_to_8_insert(prev_cu_, cu, out);
  1415. return *this;
  1416. }
  1417. #ifndef BOOST_TEXT_DOXYGEN
  1418. private:
  1419. char16_t prev_cu_;
  1420. #endif
  1421. };
  1422. namespace detail {
  1423. template<typename OutIter>
  1424. OutIter assign_8_to_16_insert(
  1425. unsigned char cu, char32_t & cp, int & state, OutIter out)
  1426. {
  1427. auto write = [&] {
  1428. out = detail::write_cp_utf16(cp, out);
  1429. state = invalid_table_state;
  1430. };
  1431. auto start_cp = [&] {
  1432. first_cu const info = first_cus[cu];
  1433. state = info.next;
  1434. cp = info.initial_octet;
  1435. if (state == bgn)
  1436. write();
  1437. };
  1438. if (state == invalid_table_state) {
  1439. start_cp();
  1440. } else {
  1441. cp = (cp << 6) | (cu & 0x3f);
  1442. char_class const class_ = octet_classes[cu];
  1443. state = transitions[state + class_];
  1444. if (state == bgn) {
  1445. write();
  1446. } else if (state == err) {
  1447. out = detail::write_cp_utf16(replacement_character, out);
  1448. start_cp();
  1449. }
  1450. }
  1451. return out;
  1452. }
  1453. }
  1454. /** An out iterator that converts UTF-8 to UTF-16. */
  1455. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  1456. template<std::output_iterator<char16_t> Iter>
  1457. #else
  1458. template<typename Iter>
  1459. #endif
  1460. struct utf_8_to_16_out_iterator
  1461. : detail::trans_ins_iter<utf_8_to_16_out_iterator<Iter>, Iter>
  1462. {
  1463. constexpr utf_8_to_16_out_iterator() {}
  1464. explicit constexpr utf_8_to_16_out_iterator(Iter it) :
  1465. detail::trans_ins_iter<utf_8_to_16_out_iterator<Iter>, Iter>(it),
  1466. state_(detail::invalid_table_state)
  1467. {}
  1468. constexpr utf_8_to_16_out_iterator & operator=(char8_type cu)
  1469. {
  1470. auto & out = this->iter();
  1471. out = detail::assign_8_to_16_insert(cu, cp_, state_, out);
  1472. return *this;
  1473. }
  1474. #ifndef BOOST_TEXT_DOXYGEN
  1475. private:
  1476. int state_;
  1477. char32_t cp_;
  1478. #endif
  1479. };
  1480. /** An insert-iterator analogous to std::insert_iterator, that also
  1481. converts UTF-8 to UTF-16. */
  1482. template<typename Cont>
  1483. struct utf_8_to_16_insert_iterator : detail::trans_ins_iter<
  1484. utf_8_to_16_insert_iterator<Cont>,
  1485. std::insert_iterator<Cont>>
  1486. {
  1487. constexpr utf_8_to_16_insert_iterator() {}
  1488. constexpr utf_8_to_16_insert_iterator(
  1489. Cont & c, typename Cont::iterator it) :
  1490. detail::trans_ins_iter<
  1491. utf_8_to_16_insert_iterator<Cont>,
  1492. std::insert_iterator<Cont>>(std::insert_iterator<Cont>(c, it)),
  1493. state_(detail::invalid_table_state)
  1494. {}
  1495. constexpr utf_8_to_16_insert_iterator & operator=(char16_t cu)
  1496. {
  1497. auto & out = this->iter();
  1498. out = detail::assign_8_to_16_insert(cu, cp_, state_, out);
  1499. return *this;
  1500. }
  1501. #ifndef BOOST_TEXT_DOXYGEN
  1502. private:
  1503. int state_;
  1504. char32_t cp_;
  1505. #endif
  1506. };
  1507. /** An insert-iterator analogous to std::front_insert_iterator, that also
  1508. converts UTF-8 to UTF-16. */
  1509. template<typename Cont>
  1510. struct utf_8_to_16_front_insert_iterator
  1511. : detail::trans_ins_iter<
  1512. utf_8_to_16_front_insert_iterator<Cont>,
  1513. std::front_insert_iterator<Cont>>
  1514. {
  1515. constexpr utf_8_to_16_front_insert_iterator() {}
  1516. explicit constexpr utf_8_to_16_front_insert_iterator(Cont & c) :
  1517. detail::trans_ins_iter<
  1518. utf_8_to_16_front_insert_iterator<Cont>,
  1519. std::front_insert_iterator<Cont>>(
  1520. std::front_insert_iterator<Cont>(c)),
  1521. state_(detail::invalid_table_state)
  1522. {}
  1523. constexpr utf_8_to_16_front_insert_iterator & operator=(char16_t cu)
  1524. {
  1525. auto & out = this->iter();
  1526. out = detail::assign_8_to_16_insert(cu, cp_, state_, out);
  1527. return *this;
  1528. }
  1529. #ifndef BOOST_TEXT_DOXYGEN
  1530. private:
  1531. int state_;
  1532. char32_t cp_;
  1533. #endif
  1534. };
  1535. /** An insert-iterator analogous to std::back_insert_iterator, that also
  1536. converts UTF-8 to UTF-16. */
  1537. template<typename Cont>
  1538. struct utf_8_to_16_back_insert_iterator
  1539. : detail::trans_ins_iter<
  1540. utf_8_to_16_back_insert_iterator<Cont>,
  1541. std::back_insert_iterator<Cont>>
  1542. {
  1543. constexpr utf_8_to_16_back_insert_iterator() {}
  1544. explicit constexpr utf_8_to_16_back_insert_iterator(Cont & c) :
  1545. detail::trans_ins_iter<
  1546. utf_8_to_16_back_insert_iterator<Cont>,
  1547. std::back_insert_iterator<Cont>>(
  1548. std::back_insert_iterator<Cont>(c)),
  1549. state_(detail::invalid_table_state)
  1550. {}
  1551. constexpr utf_8_to_16_back_insert_iterator & operator=(char16_t cu)
  1552. {
  1553. auto & out = this->iter();
  1554. out = detail::assign_8_to_16_insert(cu, cp_, state_, out);
  1555. return *this;
  1556. }
  1557. #ifndef BOOST_TEXT_DOXYGEN
  1558. private:
  1559. int state_;
  1560. char32_t cp_;
  1561. #endif
  1562. };
  1563. }}
  1564. #include <boost/parser/detail/text/unpack.hpp>
  1565. namespace boost::parser::detail { namespace text { namespace detail {
  1566. template<format Tag>
  1567. struct make_utf8_dispatch;
  1568. template<>
  1569. struct make_utf8_dispatch<format::utf8>
  1570. {
  1571. template<typename Iter, typename Sentinel>
  1572. static constexpr Iter call(Iter first, Iter it, Sentinel last)
  1573. {
  1574. return it;
  1575. }
  1576. };
  1577. template<>
  1578. struct make_utf8_dispatch<format::utf16>
  1579. {
  1580. template<typename Iter, typename Sentinel>
  1581. static constexpr utf_iterator<
  1582. format::utf16,
  1583. format::utf8,
  1584. Iter,
  1585. Sentinel>
  1586. call(Iter first, Iter it, Sentinel last)
  1587. {
  1588. return {first, it, last};
  1589. }
  1590. };
  1591. template<>
  1592. struct make_utf8_dispatch<format::utf32>
  1593. {
  1594. template<typename Iter, typename Sentinel>
  1595. static constexpr utf_iterator<
  1596. format::utf32,
  1597. format::utf8,
  1598. Iter,
  1599. Sentinel>
  1600. call(Iter first, Iter it, Sentinel last)
  1601. {
  1602. return {first, it, last};
  1603. }
  1604. };
  1605. template<format Tag>
  1606. struct make_utf16_dispatch;
  1607. template<>
  1608. struct make_utf16_dispatch<format::utf8>
  1609. {
  1610. template<typename Iter, typename Sentinel>
  1611. static constexpr utf_iterator<
  1612. format::utf8,
  1613. format::utf16,
  1614. Iter,
  1615. Sentinel>
  1616. call(Iter first, Iter it, Sentinel last)
  1617. {
  1618. return {first, it, last};
  1619. }
  1620. };
  1621. template<>
  1622. struct make_utf16_dispatch<format::utf16>
  1623. {
  1624. template<typename Iter, typename Sentinel>
  1625. static constexpr Iter call(Iter first, Iter it, Sentinel last)
  1626. {
  1627. return it;
  1628. }
  1629. };
  1630. template<>
  1631. struct make_utf16_dispatch<format::utf32>
  1632. {
  1633. template<typename Iter, typename Sentinel>
  1634. static constexpr utf_iterator<
  1635. format::utf32,
  1636. format::utf16,
  1637. Iter,
  1638. Sentinel>
  1639. call(Iter first, Iter it, Sentinel last)
  1640. {
  1641. return {first, it, last};
  1642. }
  1643. };
  1644. template<format Tag>
  1645. struct make_utf32_dispatch;
  1646. template<>
  1647. struct make_utf32_dispatch<format::utf8>
  1648. {
  1649. template<typename Iter, typename Sentinel>
  1650. static constexpr utf_iterator<
  1651. format::utf8,
  1652. format::utf32,
  1653. Iter,
  1654. Sentinel>
  1655. call(Iter first, Iter it, Sentinel last)
  1656. {
  1657. return {first, it, last};
  1658. }
  1659. };
  1660. template<>
  1661. struct make_utf32_dispatch<format::utf16>
  1662. {
  1663. template<typename Iter, typename Sentinel>
  1664. static constexpr utf_iterator<
  1665. format::utf16,
  1666. format::utf32,
  1667. Iter,
  1668. Sentinel>
  1669. call(Iter first, Iter it, Sentinel last)
  1670. {
  1671. return {first, it, last};
  1672. }
  1673. };
  1674. template<>
  1675. struct make_utf32_dispatch<format::utf32>
  1676. {
  1677. template<typename Iter, typename Sentinel>
  1678. static constexpr Iter call(Iter first, Iter it, Sentinel last)
  1679. {
  1680. return it;
  1681. }
  1682. };
  1683. template<
  1684. typename Cont,
  1685. typename UTF8,
  1686. typename UTF16,
  1687. typename UTF32,
  1688. int Bytes = sizeof(typename Cont::value_type)>
  1689. struct from_utf8_dispatch
  1690. {
  1691. using type = UTF8;
  1692. };
  1693. template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
  1694. struct from_utf8_dispatch<Cont, UTF8, UTF16, UTF32, 2>
  1695. {
  1696. using type = UTF16;
  1697. };
  1698. template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
  1699. struct from_utf8_dispatch<Cont, UTF8, UTF16, UTF32, 4>
  1700. {
  1701. using type = UTF32;
  1702. };
  1703. template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
  1704. using from_utf8_dispatch_t =
  1705. typename from_utf8_dispatch<Cont, UTF8, UTF16, UTF32>::type;
  1706. template<
  1707. typename Cont,
  1708. typename UTF8,
  1709. typename UTF16,
  1710. typename UTF32,
  1711. int Bytes = sizeof(typename Cont::value_type)>
  1712. struct from_utf16_dispatch
  1713. {
  1714. using type = UTF16;
  1715. };
  1716. template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
  1717. struct from_utf16_dispatch<Cont, UTF8, UTF16, UTF32, 1>
  1718. {
  1719. using type = UTF8;
  1720. };
  1721. template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
  1722. struct from_utf16_dispatch<Cont, UTF8, UTF16, UTF32, 4>
  1723. {
  1724. using type = UTF32;
  1725. };
  1726. template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
  1727. using from_utf16_dispatch_t =
  1728. typename from_utf16_dispatch<Cont, UTF8, UTF16, UTF32>::type;
  1729. template<
  1730. typename Cont,
  1731. typename UTF8,
  1732. typename UTF16,
  1733. typename UTF32,
  1734. int Bytes = sizeof(typename Cont::value_type)>
  1735. struct from_utf32_dispatch
  1736. {
  1737. using type = UTF32;
  1738. };
  1739. template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
  1740. struct from_utf32_dispatch<Cont, UTF8, UTF16, UTF32, 1>
  1741. {
  1742. using type = UTF8;
  1743. };
  1744. template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
  1745. struct from_utf32_dispatch<Cont, UTF8, UTF16, UTF32, 2>
  1746. {
  1747. using type = UTF16;
  1748. };
  1749. template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
  1750. using from_utf32_dispatch_t =
  1751. typename from_utf32_dispatch<Cont, UTF8, UTF16, UTF32>::type;
  1752. }}}
  1753. namespace boost::parser::detail { namespace text { BOOST_PARSER_DETAIL_TEXT_NAMESPACE_V1 {
  1754. #if defined(BOOST_TEXT_DOXYGEN)
  1755. /** Returns a `utf_32_to_8_out_iterator<O>` constructed from the given
  1756. iterator. */
  1757. template<std::output_iterator<char8_t> O>
  1758. utf_32_to_8_out_iterator<O> utf_32_to_8_out(O it);
  1759. /** Returns a `utf_8_to_32_out_iterator<O>` constructed from the given
  1760. iterator. */
  1761. template<std::output_iterator<char32_t> O>
  1762. utf_8_to_32_out_iterator<O> utf_8_to_32_out(O it);
  1763. /** Returns a `utf_32_to_16_out_iterator<O>` constructed from the given
  1764. iterator. */
  1765. template<std::output_iterator<char16_t> O>
  1766. utf_32_to_16_out_iterator<O> utf_32_to_16_out(O it);
  1767. /** Returns a `utf_16_to_32_out_iterator<O>` constructed from the given
  1768. iterator. */
  1769. template<std::output_iterator<char32_t> O>
  1770. utf_16_to_32_out_iterator<O> utf_16_to_32_out(O it);
  1771. /** Returns a `utf_16_to_8_out_iterator<O>` constructed from the given
  1772. iterator. */
  1773. template<std::output_iterator<char8_t> O>
  1774. utf_16_to_8_out_iterator<O> utf_16_to_8_out(O it);
  1775. /** Returns a `utf_8_to_16_out_iterator<O>` constructed from the given
  1776. iterator. */
  1777. template<std::output_iterator<char16_t> O>
  1778. utf_8_to_16_out_iterator<O> utf_8_to_16_out(O it);
  1779. /** Returns an iterator equivalent to `it` that transcodes `[first, last)`
  1780. to UTF-8. */
  1781. template<std::input_iterator I, std::sentinel_for<I> S>
  1782. auto utf8_iterator(I first, I it, S last);
  1783. /** Returns an iterator equivalent to `it` that transcodes `[first, last)`
  1784. to UTF-16. */
  1785. template<std::input_iterator I, std::sentinel_for<I> S>
  1786. auto utf16_iterator(I first, I it, S last);
  1787. /** Returns an iterator equivalent to `it` that transcodes `[first, last)`
  1788. to UTF-32. */
  1789. template<std::input_iterator I, std::sentinel_for<I> S>
  1790. auto utf32_iterator(I first, I it, S last);
  1791. /** Returns a inserting iterator that transcodes from UTF-8 to UTF-8,
  1792. UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
  1793. `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
  1794. any other size implies UTF-32. */
  1795. template<typename Cont>
  1796. requires requires { typename Cont::value_type; } &&
  1797. std::is_integral_v<typename Cont::value_type>
  1798. auto from_utf8_inserter(Cont & c, typename Cont::iterator it);
  1799. /** Returns a inserting iterator that transcodes from UTF-16 to UTF-8,
  1800. UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
  1801. `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
  1802. any other size implies UTF-32. */
  1803. template<typename Cont>
  1804. requires requires { typename Cont::value_type; } &&
  1805. std::is_integral_v<typename Cont::value_type>
  1806. auto from_utf16_inserter(Cont & c, typename Cont::iterator it);
  1807. /** Returns a inserting iterator that transcodes from UTF-32 to UTF-8,
  1808. UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
  1809. `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
  1810. any other size implies UTF-32. */
  1811. template<typename Cont>
  1812. requires requires { typename Cont::value_type; } &&
  1813. std::is_integral_v<typename Cont::value_type>
  1814. auto from_utf32_inserter(Cont & c, typename Cont::iterator it);
  1815. /** Returns a back-inserting iterator that transcodes from UTF-8 to UTF-8,
  1816. UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
  1817. `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
  1818. any other size implies UTF-32. */
  1819. template<typename Cont>
  1820. requires requires { typename Cont::value_type; } &&
  1821. std::is_integral_v<typename Cont::value_type>
  1822. auto from_utf8_back_inserter(Cont & c);
  1823. /** Returns a back-inserting iterator that transcodes from UTF-16 to UTF-8,
  1824. UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
  1825. `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
  1826. any other size implies UTF-32. */
  1827. template<typename Cont>
  1828. requires requires { typename Cont::value_type; } &&
  1829. std::is_integral_v<typename Cont::value_type>
  1830. auto from_utf16_back_inserter(Cont & c);
  1831. /** Returns a back-inserting iterator that transcodes from UTF-32 to UTF-8,
  1832. UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
  1833. `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
  1834. any other size implies UTF-32. */
  1835. template<typename Cont>
  1836. requires requires { typename Cont::value_type; } &&
  1837. std::is_integral_v<typename Cont::value_type>
  1838. auto from_utf32_back_inserter(Cont & c);
  1839. /** Returns a front-inserting iterator that transcodes from UTF-8 to UTF-8,
  1840. UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
  1841. `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
  1842. any other size implies UTF-32. */
  1843. template<typename Cont>
  1844. requires requires { typename Cont::value_type; } &&
  1845. std::is_integral_v<typename Cont::value_type>
  1846. auto from_utf8_front_inserter(Cont & c);
  1847. /** Returns a front-inserting iterator that transcodes from UTF-16 to UTF-8,
  1848. UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
  1849. `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
  1850. any other size implies UTF-32. */
  1851. template<typename Cont>
  1852. requires requires { typename Cont::value_type; } &&
  1853. std::is_integral_v<typename Cont::value_type>
  1854. auto from_utf16_front_inserter(Cont & c);
  1855. /** Returns a front-inserting iterator that transcodes from UTF-32 to UTF-8,
  1856. UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
  1857. `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
  1858. any other size implies UTF-32. */
  1859. template<typename Cont>
  1860. requires requires { typename Cont::value_type; } &&
  1861. std::is_integral_v<typename Cont::value_type>
  1862. auto from_utf32_front_inserter(Cont & c);
  1863. #endif
  1864. template<typename Iter>
  1865. utf_32_to_8_out_iterator<Iter> utf_32_to_8_out(Iter it)
  1866. {
  1867. return utf_32_to_8_out_iterator<Iter>(it);
  1868. }
  1869. template<typename Iter>
  1870. utf_8_to_32_out_iterator<Iter> utf_8_to_32_out(Iter it)
  1871. {
  1872. return utf_8_to_32_out_iterator<Iter>(it);
  1873. }
  1874. template<typename Iter>
  1875. utf_32_to_16_out_iterator<Iter> utf_32_to_16_out(Iter it)
  1876. {
  1877. return utf_32_to_16_out_iterator<Iter>(it);
  1878. }
  1879. template<typename Iter>
  1880. utf_16_to_32_out_iterator<Iter> utf_16_to_32_out(Iter it)
  1881. {
  1882. return utf_16_to_32_out_iterator<Iter>(it);
  1883. }
  1884. template<typename Iter>
  1885. utf_16_to_8_out_iterator<Iter> utf_16_to_8_out(Iter it)
  1886. {
  1887. return utf_16_to_8_out_iterator<Iter>(it);
  1888. }
  1889. template<typename Iter>
  1890. utf_8_to_16_out_iterator<Iter> utf_8_to_16_out(Iter it)
  1891. {
  1892. return utf_8_to_16_out_iterator<Iter>(it);
  1893. }
  1894. template<typename Iter, typename Sentinel>
  1895. auto utf8_iterator(Iter first, Iter it, Sentinel last)
  1896. {
  1897. auto const unpacked = text::unpack_iterator_and_sentinel(first, last);
  1898. auto const unpacked_it =
  1899. text::unpack_iterator_and_sentinel(it, last).first;
  1900. constexpr format tag = unpacked.format_tag;
  1901. return detail::make_utf8_dispatch<tag>::call(
  1902. unpacked.first, unpacked_it, unpacked.last);
  1903. }
  1904. template<typename Iter, typename Sentinel>
  1905. auto utf16_iterator(Iter first, Iter it, Sentinel last)
  1906. {
  1907. auto const unpacked = text::unpack_iterator_and_sentinel(first, last);
  1908. auto const unpacked_it =
  1909. text::unpack_iterator_and_sentinel(it, last).first;
  1910. constexpr format tag = unpacked.format_tag;
  1911. return detail::make_utf16_dispatch<tag>::call(
  1912. unpacked.first, unpacked_it, unpacked.last);
  1913. }
  1914. template<typename Iter, typename Sentinel>
  1915. auto utf32_iterator(Iter first, Iter it, Sentinel last)
  1916. {
  1917. auto const unpacked = text::unpack_iterator_and_sentinel(first, last);
  1918. auto const unpacked_it =
  1919. text::unpack_iterator_and_sentinel(it, last).first;
  1920. constexpr format tag = unpacked.format_tag;
  1921. return detail::make_utf32_dispatch<tag>::call(
  1922. unpacked.first, unpacked_it, unpacked.last);
  1923. }
  1924. template<typename Cont>
  1925. auto from_utf8_inserter(Cont & c, typename Cont::iterator it)
  1926. {
  1927. using result_type = detail::from_utf8_dispatch_t<
  1928. Cont,
  1929. std::insert_iterator<Cont>,
  1930. utf_8_to_16_insert_iterator<Cont>,
  1931. utf_8_to_32_insert_iterator<Cont>>;
  1932. return result_type(c, it);
  1933. }
  1934. template<typename Cont>
  1935. auto from_utf16_inserter(Cont & c, typename Cont::iterator it)
  1936. {
  1937. using result_type = detail::from_utf16_dispatch_t<
  1938. Cont,
  1939. utf_16_to_8_insert_iterator<Cont>,
  1940. std::insert_iterator<Cont>,
  1941. utf_16_to_32_insert_iterator<Cont>>;
  1942. return result_type(c, it);
  1943. }
  1944. template<typename Cont>
  1945. auto from_utf32_inserter(Cont & c, typename Cont::iterator it)
  1946. {
  1947. using result_type = detail::from_utf32_dispatch_t<
  1948. Cont,
  1949. utf_32_to_8_insert_iterator<Cont>,
  1950. utf_32_to_16_insert_iterator<Cont>,
  1951. std::insert_iterator<Cont>>;
  1952. return result_type(c, it);
  1953. }
  1954. template<typename Cont>
  1955. auto from_utf8_back_inserter(Cont & c)
  1956. {
  1957. using result_type = detail::from_utf8_dispatch_t<
  1958. Cont,
  1959. std::back_insert_iterator<Cont>,
  1960. utf_8_to_16_back_insert_iterator<Cont>,
  1961. utf_8_to_32_back_insert_iterator<Cont>>;
  1962. return result_type(c);
  1963. }
  1964. template<typename Cont>
  1965. auto from_utf16_back_inserter(Cont & c)
  1966. {
  1967. using result_type = detail::from_utf16_dispatch_t<
  1968. Cont,
  1969. utf_16_to_8_back_insert_iterator<Cont>,
  1970. std::back_insert_iterator<Cont>,
  1971. utf_16_to_32_back_insert_iterator<Cont>>;
  1972. return result_type(c);
  1973. }
  1974. template<typename Cont>
  1975. auto from_utf32_back_inserter(Cont & c)
  1976. {
  1977. using result_type = detail::from_utf32_dispatch_t<
  1978. Cont,
  1979. utf_32_to_8_back_insert_iterator<Cont>,
  1980. utf_32_to_16_back_insert_iterator<Cont>,
  1981. std::back_insert_iterator<Cont>>;
  1982. return result_type(c);
  1983. }
  1984. template<typename Cont>
  1985. auto from_utf8_front_inserter(Cont & c)
  1986. {
  1987. using result_type = detail::from_utf8_dispatch_t<
  1988. Cont,
  1989. std::front_insert_iterator<Cont>,
  1990. utf_8_to_16_front_insert_iterator<Cont>,
  1991. utf_8_to_32_front_insert_iterator<Cont>>;
  1992. return result_type(c);
  1993. }
  1994. template<typename Cont>
  1995. auto from_utf16_front_inserter(Cont & c)
  1996. {
  1997. using result_type = detail::from_utf16_dispatch_t<
  1998. Cont,
  1999. utf_16_to_8_front_insert_iterator<Cont>,
  2000. std::front_insert_iterator<Cont>,
  2001. utf_16_to_32_front_insert_iterator<Cont>>;
  2002. return result_type(c);
  2003. }
  2004. template<typename Cont>
  2005. auto from_utf32_front_inserter(Cont & c)
  2006. {
  2007. using result_type = detail::from_utf32_dispatch_t<
  2008. Cont,
  2009. utf_32_to_8_front_insert_iterator<Cont>,
  2010. utf_32_to_16_front_insert_iterator<Cont>,
  2011. std::front_insert_iterator<Cont>>;
  2012. return result_type(c);
  2013. }
  2014. }}}
  2015. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2016. namespace boost::parser::detail { namespace text { BOOST_PARSER_DETAIL_TEXT_NAMESPACE_V2 {
  2017. template<std::output_iterator<char8_t> O>
  2018. constexpr utf_32_to_8_out_iterator<O> utf_32_to_8_out(O it)
  2019. {
  2020. return utf_32_to_8_out_iterator<O>(it);
  2021. }
  2022. template<std::output_iterator<char32_t> O>
  2023. constexpr utf_8_to_32_out_iterator<O> utf_8_to_32_out(O it)
  2024. {
  2025. return utf_8_to_32_out_iterator<O>(it);
  2026. }
  2027. template<std::output_iterator<char16_t> O>
  2028. constexpr utf_32_to_16_out_iterator<O> utf_32_to_16_out(O it)
  2029. {
  2030. return utf_32_to_16_out_iterator<O>(it);
  2031. }
  2032. template<std::output_iterator<char32_t> O>
  2033. constexpr utf_16_to_32_out_iterator<O> utf_16_to_32_out(O it)
  2034. {
  2035. return utf_16_to_32_out_iterator<O>(it);
  2036. }
  2037. template<std::output_iterator<char8_t> O>
  2038. constexpr utf_16_to_8_out_iterator<O> utf_16_to_8_out(O it)
  2039. {
  2040. return utf_16_to_8_out_iterator<O>(it);
  2041. }
  2042. template<std::output_iterator<char16_t> O>
  2043. constexpr utf_8_to_16_out_iterator<O> utf_8_to_16_out(O it)
  2044. {
  2045. return utf_8_to_16_out_iterator<O>(it);
  2046. }
  2047. template<std::input_iterator I, std::sentinel_for<I> S>
  2048. constexpr auto utf8_iterator(I first, I it, S last)
  2049. {
  2050. return v1::utf8_iterator(first, it, last);
  2051. }
  2052. template<std::input_iterator I, std::sentinel_for<I> S>
  2053. constexpr auto utf16_iterator(I first, I it, S last)
  2054. {
  2055. return v1::utf16_iterator(first, it, last);
  2056. }
  2057. template<std::input_iterator I, std::sentinel_for<I> S>
  2058. constexpr auto utf32_iterator(I first, I it, S last)
  2059. {
  2060. return v1::utf32_iterator(first, it, last);
  2061. }
  2062. template<typename Cont>
  2063. requires requires { typename Cont::value_type; } &&
  2064. utf_code_unit<typename Cont::value_type>
  2065. constexpr auto from_utf8_inserter(Cont & c, typename Cont::iterator it)
  2066. {
  2067. if constexpr (sizeof(typename Cont::value_type) == 1) {
  2068. return std::insert_iterator<Cont>(c, it);
  2069. } else if constexpr (sizeof(typename Cont::value_type) == 2) {
  2070. return utf_8_to_16_insert_iterator<Cont>(c, it);
  2071. } else {
  2072. return utf_8_to_32_insert_iterator<Cont>(c, it);
  2073. }
  2074. }
  2075. template<typename Cont>
  2076. requires requires { typename Cont::value_type; } &&
  2077. utf_code_unit<typename Cont::value_type>
  2078. constexpr auto from_utf16_inserter(Cont & c, typename Cont::iterator it)
  2079. {
  2080. if constexpr (sizeof(typename Cont::value_type) == 1) {
  2081. return utf_16_to_8_insert_iterator<Cont>(c, it);
  2082. } else if constexpr (sizeof(typename Cont::value_type) == 2) {
  2083. return std::insert_iterator<Cont>(c, it);
  2084. } else {
  2085. return utf_16_to_32_insert_iterator<Cont>(c, it);
  2086. }
  2087. }
  2088. template<typename Cont>
  2089. requires requires { typename Cont::value_type; } &&
  2090. utf_code_unit<typename Cont::value_type>
  2091. constexpr auto from_utf32_inserter(Cont & c, typename Cont::iterator it)
  2092. {
  2093. if constexpr (sizeof(typename Cont::value_type) == 1) {
  2094. return utf_32_to_8_insert_iterator<Cont>(c, it);
  2095. } else if constexpr (sizeof(typename Cont::value_type) == 2) {
  2096. return utf_32_to_16_insert_iterator<Cont>(c, it);
  2097. } else {
  2098. return std::insert_iterator<Cont>(c, it);
  2099. }
  2100. }
  2101. template<typename Cont>
  2102. requires requires { typename Cont::value_type; } &&
  2103. utf_code_unit<typename Cont::value_type>
  2104. constexpr auto from_utf8_back_inserter(Cont & c)
  2105. {
  2106. if constexpr (sizeof(typename Cont::value_type) == 1) {
  2107. return std::back_insert_iterator<Cont>(c);
  2108. } else if constexpr (sizeof(typename Cont::value_type) == 2) {
  2109. return utf_8_to_16_back_insert_iterator<Cont>(c);
  2110. } else {
  2111. return utf_8_to_32_back_insert_iterator<Cont>(c);
  2112. }
  2113. }
  2114. template<typename Cont>
  2115. requires requires { typename Cont::value_type; } &&
  2116. utf_code_unit<typename Cont::value_type>
  2117. constexpr auto from_utf16_back_inserter(Cont & c)
  2118. {
  2119. if constexpr (sizeof(typename Cont::value_type) == 1) {
  2120. return utf_16_to_8_back_insert_iterator<Cont>(c);
  2121. } else if constexpr (sizeof(typename Cont::value_type) == 2) {
  2122. return std::back_insert_iterator<Cont>(c);
  2123. } else {
  2124. return utf_16_to_32_back_insert_iterator<Cont>(c);
  2125. }
  2126. }
  2127. template<typename Cont>
  2128. requires requires { typename Cont::value_type; } &&
  2129. utf_code_unit<typename Cont::value_type>
  2130. constexpr auto from_utf32_back_inserter(Cont & c)
  2131. {
  2132. if constexpr (sizeof(typename Cont::value_type) == 1) {
  2133. return utf_32_to_8_back_insert_iterator<Cont>(c);
  2134. } else if constexpr (sizeof(typename Cont::value_type) == 2) {
  2135. return utf_32_to_16_back_insert_iterator<Cont>(c);
  2136. } else {
  2137. return std::back_insert_iterator<Cont>(c);
  2138. }
  2139. }
  2140. template<typename Cont>
  2141. requires requires { typename Cont::value_type; } &&
  2142. utf_code_unit<typename Cont::value_type>
  2143. constexpr auto from_utf8_front_inserter(Cont & c)
  2144. {
  2145. if constexpr (sizeof(typename Cont::value_type) == 1) {
  2146. return std::front_insert_iterator<Cont>(c);
  2147. } else if constexpr (sizeof(typename Cont::value_type) == 2) {
  2148. return utf_8_to_16_front_insert_iterator<Cont>(c);
  2149. } else {
  2150. return utf_8_to_32_front_insert_iterator<Cont>(c);
  2151. }
  2152. }
  2153. template<typename Cont>
  2154. requires requires { typename Cont::value_type; } &&
  2155. utf_code_unit<typename Cont::value_type>
  2156. constexpr auto from_utf16_front_inserter(Cont & c)
  2157. {
  2158. if constexpr (sizeof(typename Cont::value_type) == 1) {
  2159. return utf_16_to_8_front_insert_iterator<Cont>(c);
  2160. } else if constexpr (sizeof(typename Cont::value_type) == 2) {
  2161. return std::front_insert_iterator<Cont>(c);
  2162. } else {
  2163. return utf_16_to_32_front_insert_iterator<Cont>(c);
  2164. }
  2165. }
  2166. template<typename Cont>
  2167. requires requires { typename Cont::value_type; } &&
  2168. utf_code_unit<typename Cont::value_type>
  2169. constexpr auto from_utf32_front_inserter(Cont & c)
  2170. {
  2171. if constexpr (sizeof(typename Cont::value_type) == 1) {
  2172. return utf_32_to_8_front_insert_iterator<Cont>(c);
  2173. } else if constexpr (sizeof(typename Cont::value_type) == 2) {
  2174. return utf_32_to_16_front_insert_iterator<Cont>(c);
  2175. } else {
  2176. return std::front_insert_iterator<Cont>(c);
  2177. }
  2178. }
  2179. }}}
  2180. #endif
  2181. namespace boost::parser::detail { namespace text {
  2182. namespace detail {
  2183. template<format Format>
  2184. constexpr auto format_to_type()
  2185. {
  2186. if constexpr (Format == format::utf8) {
  2187. return char8_type{};
  2188. } else if constexpr (Format == format::utf16) {
  2189. return char16_t{};
  2190. } else {
  2191. return char32_t{};
  2192. }
  2193. }
  2194. template<typename I>
  2195. constexpr bool is_bidi =
  2196. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2197. std::bidirectional_iterator<I>
  2198. #else
  2199. std::is_base_of_v<
  2200. std::bidirectional_iterator_tag,
  2201. typename std::iterator_traits<I>::iterator_category>
  2202. #endif
  2203. ;
  2204. template<typename I, bool SupportReverse = is_bidi<I>>
  2205. struct first_and_curr
  2206. {
  2207. first_and_curr() = default;
  2208. first_and_curr(I curr) : curr{curr} {}
  2209. first_and_curr(const first_and_curr & other) = default;
  2210. template<
  2211. class I2
  2212. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2213. ,
  2214. typename Enable = std::enable_if_t<std::is_convertible_v<I2, I>>
  2215. #endif
  2216. >
  2217. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2218. requires std::convertible_to<I2, I>
  2219. #endif
  2220. first_and_curr(const first_and_curr<I2> & other) : curr{other.curr}
  2221. {}
  2222. I curr;
  2223. };
  2224. template<typename I>
  2225. struct first_and_curr<I, true>
  2226. {
  2227. first_and_curr() = default;
  2228. first_and_curr(I first, I curr) : first{first}, curr{curr} {}
  2229. first_and_curr(const first_and_curr & other) = default;
  2230. template<
  2231. class I2
  2232. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2233. ,
  2234. typename Enable = std::enable_if_t<std::is_convertible_v<I2, I>>
  2235. #endif
  2236. >
  2237. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2238. requires std::convertible_to<I2, I>
  2239. #endif
  2240. first_and_curr(const first_and_curr<I2> & other) :
  2241. first{other.first}, curr{other.curr}
  2242. {}
  2243. I first;
  2244. I curr;
  2245. };
  2246. }
  2247. namespace detail {
  2248. struct iter_access
  2249. {
  2250. template<typename T>
  2251. static auto & buf(T & it)
  2252. {
  2253. return it.buf_;
  2254. }
  2255. template<typename T>
  2256. static auto & first_and_curr(T & it)
  2257. {
  2258. return it.first_and_curr_;
  2259. }
  2260. template<typename T>
  2261. static auto & buf_index(T & it)
  2262. {
  2263. return it.buf_index_;
  2264. }
  2265. template<typename T>
  2266. static auto & buf_last(T & it)
  2267. {
  2268. return it.buf_last_;
  2269. }
  2270. template<typename T>
  2271. static auto & to_increment(T & it)
  2272. {
  2273. return it.to_increment_;
  2274. }
  2275. template<typename T>
  2276. static auto & last(T & it)
  2277. {
  2278. return it.last_;
  2279. }
  2280. };
  2281. }
  2282. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2283. template<
  2284. format FromFormat,
  2285. format ToFormat,
  2286. std::input_iterator I,
  2287. std::sentinel_for<I> S,
  2288. transcoding_error_handler ErrorHandler>
  2289. requires std::convertible_to<std::iter_value_t<I>, detail::format_to_type_t<FromFormat>>
  2290. #else
  2291. template<
  2292. format FromFormat,
  2293. format ToFormat,
  2294. typename I,
  2295. typename S,
  2296. typename ErrorHandler>
  2297. #endif
  2298. class utf_iterator
  2299. : public stl_interfaces::iterator_interface<
  2300. utf_iterator<FromFormat, ToFormat, I, S, ErrorHandler>,
  2301. detail::bidirectional_at_most_t<I>,
  2302. detail::format_to_type_t<ToFormat>,
  2303. detail::format_to_type_t<ToFormat>>
  2304. {
  2305. static_assert(
  2306. FromFormat == format::utf8 || FromFormat == format::utf16 ||
  2307. FromFormat == format::utf32);
  2308. static_assert(
  2309. ToFormat == format::utf8 || ToFormat == format::utf16 ||
  2310. ToFormat == format::utf32);
  2311. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2312. template<typename T>
  2313. constexpr static bool is_bidirectional = std::is_base_of_v<
  2314. std::bidirectional_iterator_tag,
  2315. detail::bidirectional_at_most_t<T>>;
  2316. template<typename T>
  2317. constexpr static bool is_forward = std::is_base_of_v<
  2318. std::forward_iterator_tag,
  2319. detail::bidirectional_at_most_t<T>>;
  2320. template<typename T>
  2321. constexpr static bool is_input = !is_bidirectional<T> && !is_forward<T>;
  2322. #endif
  2323. static_assert(
  2324. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2325. std::forward_iterator<I>
  2326. #else
  2327. is_forward<I>
  2328. #endif
  2329. || noexcept(ErrorHandler{}("")));
  2330. public:
  2331. using value_type = detail::format_to_type_t<ToFormat>;
  2332. constexpr utf_iterator() = default;
  2333. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2334. template<
  2335. typename J = I,
  2336. typename Enable = std::enable_if_t<is_bidirectional<J>>>
  2337. #endif
  2338. constexpr utf_iterator(I first, I it, S last)
  2339. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2340. requires std::bidirectional_iterator<I>
  2341. #endif
  2342. : first_and_curr_{first, it}, last_(last)
  2343. {
  2344. if (curr() != last_)
  2345. read();
  2346. }
  2347. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2348. template<
  2349. typename J = I,
  2350. typename Enable = std::enable_if_t<!is_bidirectional<J>>>
  2351. #endif
  2352. constexpr utf_iterator(I it, S last)
  2353. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2354. requires(!std::bidirectional_iterator<I>)
  2355. #endif
  2356. :
  2357. first_and_curr_{it}, last_(last)
  2358. {
  2359. if (curr() != last_)
  2360. read();
  2361. }
  2362. template<
  2363. class I2,
  2364. class S2
  2365. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2366. ,
  2367. typename Enable = std::enable_if_t<
  2368. std::is_convertible_v<I2, I> && std::is_convertible_v<S2, S>>
  2369. #endif
  2370. >
  2371. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2372. requires std::convertible_to<I2, I> && std::convertible_to<S2, S>
  2373. #endif
  2374. constexpr utf_iterator(
  2375. utf_iterator<FromFormat, ToFormat, I2, S2, ErrorHandler> const &
  2376. other) :
  2377. buf_(detail::iter_access::buf(other)),
  2378. first_and_curr_(detail::iter_access::first_and_curr(other)),
  2379. buf_index_(detail::iter_access::buf_index(other)),
  2380. buf_last_(detail::iter_access::buf_last(other)),
  2381. to_increment_(detail::iter_access::to_increment(other)),
  2382. last_(detail::iter_access::last(other))
  2383. {}
  2384. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2385. template<
  2386. typename J = I,
  2387. typename Enable = std::enable_if_t<is_bidirectional<J>>>
  2388. #endif
  2389. constexpr I begin() const
  2390. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2391. requires std::bidirectional_iterator<I>
  2392. #endif
  2393. {
  2394. return first();
  2395. }
  2396. constexpr S end() const { return last_; }
  2397. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2398. template<
  2399. typename J = I,
  2400. typename Enable = std::enable_if_t<is_forward<J>>>
  2401. #endif
  2402. constexpr I base() const
  2403. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2404. requires std::forward_iterator<I>
  2405. #endif
  2406. {
  2407. return curr();
  2408. }
  2409. constexpr value_type operator*() const
  2410. {
  2411. BOOST_PARSER_DEBUG_ASSERT(buf_index_ < buf_last_);
  2412. return buf_[buf_index_];
  2413. }
  2414. constexpr utf_iterator & operator++()
  2415. {
  2416. BOOST_PARSER_DEBUG_ASSERT(buf_index_ != buf_last_ || curr() != last_);
  2417. if (buf_index_ + 1 == buf_last_ && curr() != last_) {
  2418. if constexpr (
  2419. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2420. std::forward_iterator<I>
  2421. #else
  2422. is_forward<I>
  2423. #endif
  2424. ) {
  2425. std::advance(curr(), to_increment_);
  2426. }
  2427. if (curr() == last_)
  2428. buf_index_ = 0;
  2429. else
  2430. read();
  2431. } else if (buf_index_ + 1 <= buf_last_) {
  2432. ++buf_index_;
  2433. }
  2434. return *this;
  2435. }
  2436. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2437. template<
  2438. typename J = I,
  2439. typename Enable = std::enable_if_t<is_bidirectional<J>>>
  2440. #endif
  2441. constexpr utf_iterator & operator--()
  2442. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2443. requires std::bidirectional_iterator<I>
  2444. #endif
  2445. {
  2446. BOOST_PARSER_DEBUG_ASSERT(buf_index_ || curr() != first());
  2447. if (!buf_index_ && curr() != first())
  2448. read_reverse();
  2449. else if (buf_index_)
  2450. --buf_index_;
  2451. return *this;
  2452. }
  2453. friend constexpr bool operator==(
  2454. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2455. utf_iterator
  2456. #else
  2457. std::enable_if_t<is_forward<I>, utf_iterator>
  2458. #endif
  2459. lhs, utf_iterator rhs)
  2460. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2461. requires std::forward_iterator<I> || requires(I i) { i == i; }
  2462. #endif
  2463. {
  2464. if constexpr (
  2465. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2466. std::forward_iterator<I>
  2467. #else
  2468. is_forward<I>
  2469. #endif
  2470. ) {
  2471. return lhs.curr() == rhs.curr() && lhs.buf_index_ == rhs.buf_index_;
  2472. } else {
  2473. if (lhs.curr() != rhs.curr())
  2474. return false;
  2475. if (lhs.buf_index_ == rhs.buf_index_ &&
  2476. lhs.buf_last_ == rhs.buf_last_) {
  2477. return true;
  2478. }
  2479. return lhs.buf_index_ == lhs.buf_last_ &&
  2480. rhs.buf_index_ == rhs.buf_last_;
  2481. }
  2482. }
  2483. #if !defined(__cpp_impl_three_way_comparison)
  2484. friend BOOST_PARSER_CONSTEXPR bool operator!=(
  2485. std::enable_if_t<is_forward<I>, utf_iterator> lhs, utf_iterator rhs)
  2486. { return !(lhs == rhs); }
  2487. #endif
  2488. friend constexpr bool operator==(utf_iterator lhs, S rhs)
  2489. {
  2490. if constexpr (
  2491. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2492. std::forward_iterator<I>
  2493. #else
  2494. is_forward<I>
  2495. #endif
  2496. ) {
  2497. return lhs.curr() == rhs;
  2498. } else {
  2499. return lhs.curr() == rhs && lhs.buf_index_ == lhs.buf_last_;
  2500. }
  2501. }
  2502. #if !defined(__cpp_impl_three_way_comparison)
  2503. friend BOOST_PARSER_CONSTEXPR bool operator!=(utf_iterator lhs, S rhs)
  2504. { return !(lhs == rhs); }
  2505. #endif
  2506. // exposition only
  2507. using base_type = stl_interfaces::iterator_interface<
  2508. utf_iterator<FromFormat, ToFormat, I, S, ErrorHandler>,
  2509. detail::bidirectional_at_most_t<I>,
  2510. value_type,
  2511. value_type>;
  2512. using base_type::operator++;
  2513. using base_type::operator--;
  2514. private:
  2515. constexpr char32_t decode_code_point()
  2516. {
  2517. if constexpr (FromFormat == format::utf8) {
  2518. char32_t cp = *curr();
  2519. ++curr();
  2520. to_increment_ = 1;
  2521. if (cp < 0x80)
  2522. return cp;
  2523. // clang-format off
  2524. // It turns out that this naive implementation is faster than
  2525. // the table implementation for the converting iterators.
  2526. /*
  2527. Unicode 3.9/D92
  2528. Table 3-7. Well-Formed UTF-8 Byte Sequences
  2529. Code Points First Byte Second Byte Third Byte Fourth Byte
  2530. =========== ========== =========== ========== ===========
  2531. U+0000..U+007F 00..7F
  2532. U+0080..U+07FF C2..DF 80..BF
  2533. U+0800..U+0FFF E0 A0..BF 80..BF
  2534. U+1000..U+CFFF E1..EC 80..BF 80..BF
  2535. U+D000..U+D7FF ED 80..9F 80..BF
  2536. U+E000..U+FFFF EE..EF 80..BF 80..BF
  2537. U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
  2538. U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
  2539. U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
  2540. */
  2541. // clang-format on
  2542. unsigned char curr_c = (unsigned char)cp;
  2543. auto error = [&]() {
  2544. return ErrorHandler{}("Ill-formed UTF-8.");
  2545. };
  2546. auto next = [&]() {
  2547. ++curr();
  2548. ++to_increment_;
  2549. };
  2550. // One-byte case handled above
  2551. // Two-byte
  2552. if (detail::in(0xc2, curr_c, 0xdf)) {
  2553. cp = curr_c & 0b00011111;
  2554. if (curr() == last_)
  2555. return error();
  2556. curr_c = *curr();
  2557. if (!detail::in(0x80, curr_c, 0xbf))
  2558. return error();
  2559. cp = (cp << 6) + (curr_c & 0b00111111);
  2560. next();
  2561. // Three-byte
  2562. } else if (curr_c == 0xe0) {
  2563. cp = curr_c & 0b00001111;
  2564. if (curr() == last_)
  2565. return error();
  2566. curr_c = *curr();
  2567. if (!detail::in(0xa0, curr_c, 0xbf))
  2568. return error();
  2569. cp = (cp << 6) + (curr_c & 0b00111111);
  2570. next();
  2571. if (curr() == last_)
  2572. return error();
  2573. curr_c = *curr();
  2574. if (!detail::in(0x80, curr_c, 0xbf))
  2575. return error();
  2576. cp = (cp << 6) + (curr_c & 0b00111111);
  2577. next();
  2578. } else if (detail::in(0xe1, curr_c, 0xec)) {
  2579. cp = curr_c & 0b00001111;
  2580. if (curr() == last_)
  2581. return error();
  2582. curr_c = *curr();
  2583. if (!detail::in(0x80, curr_c, 0xbf))
  2584. return error();
  2585. cp = (cp << 6) + (curr_c & 0b00111111);
  2586. next();
  2587. if (curr() == last_)
  2588. return error();
  2589. curr_c = *curr();
  2590. if (!detail::in(0x80, curr_c, 0xbf))
  2591. return error();
  2592. cp = (cp << 6) + (curr_c & 0b00111111);
  2593. next();
  2594. } else if (curr_c == 0xed) {
  2595. cp = curr_c & 0b00001111;
  2596. if (curr() == last_)
  2597. return error();
  2598. curr_c = *curr();
  2599. if (!detail::in(0x80, curr_c, 0x9f))
  2600. return error();
  2601. cp = (cp << 6) + (curr_c & 0b00111111);
  2602. next();
  2603. if (curr() == last_)
  2604. return error();
  2605. curr_c = *curr();
  2606. if (!detail::in(0x80, curr_c, 0xbf))
  2607. return error();
  2608. cp = (cp << 6) + (curr_c & 0b00111111);
  2609. next();
  2610. } else if (detail::in(0xee, curr_c, 0xef)) {
  2611. cp = curr_c & 0b00001111;
  2612. if (curr() == last_)
  2613. return error();
  2614. curr_c = *curr();
  2615. if (!detail::in(0x80, curr_c, 0xbf))
  2616. return error();
  2617. cp = (cp << 6) + (curr_c & 0b00111111);
  2618. next();
  2619. if (curr() == last_)
  2620. return error();
  2621. curr_c = *curr();
  2622. if (!detail::in(0x80, curr_c, 0xbf))
  2623. return error();
  2624. cp = (cp << 6) + (curr_c & 0b00111111);
  2625. next();
  2626. // Four-byte
  2627. } else if (curr_c == 0xf0) {
  2628. cp = curr_c & 0b00000111;
  2629. if (curr() == last_)
  2630. return error();
  2631. curr_c = *curr();
  2632. if (!detail::in(0x90, curr_c, 0xbf))
  2633. return error();
  2634. cp = (cp << 6) + (curr_c & 0b00111111);
  2635. next();
  2636. if (curr() == last_)
  2637. return error();
  2638. curr_c = *curr();
  2639. if (!detail::in(0x80, curr_c, 0xbf))
  2640. return error();
  2641. cp = (cp << 6) + (curr_c & 0b00111111);
  2642. next();
  2643. if (curr() == last_)
  2644. return error();
  2645. curr_c = *curr();
  2646. if (!detail::in(0x80, curr_c, 0xbf))
  2647. return error();
  2648. cp = (cp << 6) + (curr_c & 0b00111111);
  2649. next();
  2650. } else if (detail::in(0xf1, curr_c, 0xf3)) {
  2651. cp = curr_c & 0b00000111;
  2652. if (curr() == last_)
  2653. return error();
  2654. curr_c = *curr();
  2655. if (!detail::in(0x80, curr_c, 0xbf))
  2656. return error();
  2657. cp = (cp << 6) + (curr_c & 0b00111111);
  2658. next();
  2659. if (curr() == last_)
  2660. return error();
  2661. curr_c = *curr();
  2662. if (!detail::in(0x80, curr_c, 0xbf))
  2663. return error();
  2664. cp = (cp << 6) + (curr_c & 0b00111111);
  2665. next();
  2666. if (curr() == last_)
  2667. return error();
  2668. curr_c = *curr();
  2669. if (!detail::in(0x80, curr_c, 0xbf))
  2670. return error();
  2671. cp = (cp << 6) + (curr_c & 0b00111111);
  2672. next();
  2673. } else if (curr_c == 0xf4) {
  2674. cp = curr_c & 0b00000111;
  2675. if (curr() == last_)
  2676. return error();
  2677. curr_c = *curr();
  2678. if (!detail::in(0x80, curr_c, 0x8f))
  2679. return error();
  2680. cp = (cp << 6) + (curr_c & 0b00111111);
  2681. next();
  2682. if (curr() == last_)
  2683. return error();
  2684. curr_c = *curr();
  2685. if (!detail::in(0x80, curr_c, 0xbf))
  2686. return error();
  2687. cp = (cp << 6) + (curr_c & 0b00111111);
  2688. next();
  2689. if (curr() == last_)
  2690. return error();
  2691. curr_c = *curr();
  2692. if (!detail::in(0x80, curr_c, 0xbf))
  2693. return error();
  2694. cp = (cp << 6) + (curr_c & 0b00111111);
  2695. next();
  2696. } else {
  2697. return error();
  2698. }
  2699. return cp;
  2700. } else if constexpr (FromFormat == format::utf16) {
  2701. char16_t hi = *curr();
  2702. ++curr();
  2703. to_increment_ = 1;
  2704. if (!boost::parser::detail::text::surrogate(hi))
  2705. return hi;
  2706. if (boost::parser::detail::text::low_surrogate(hi)) {
  2707. return ErrorHandler{}(
  2708. "Invalid UTF-16 sequence; lone trailing surrogate.");
  2709. }
  2710. // high surrogate
  2711. if (curr() == last_) {
  2712. return ErrorHandler{}(
  2713. "Invalid UTF-16 sequence; lone leading surrogate.");
  2714. }
  2715. char16_t lo = *curr();
  2716. ++curr();
  2717. ++to_increment_;
  2718. if (!boost::parser::detail::text::low_surrogate(lo)) {
  2719. return ErrorHandler{}(
  2720. "Invalid UTF-16 sequence; lone leading surrogate.");
  2721. }
  2722. return char32_t((hi - high_surrogate_base) << 10) +
  2723. (lo - low_surrogate_base);
  2724. } else {
  2725. char32_t retval = *curr();
  2726. ++curr();
  2727. to_increment_ = 1;
  2728. return retval;
  2729. }
  2730. }
  2731. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2732. template<
  2733. typename J = I,
  2734. typename Enable = std::enable_if_t<is_bidirectional<J>>>
  2735. #endif
  2736. constexpr char32_t decode_code_point_reverse()
  2737. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2738. requires std::bidirectional_iterator<I>
  2739. #endif
  2740. {
  2741. if constexpr (FromFormat == format::utf8) {
  2742. curr() = detail::decrement(first(), curr());
  2743. auto initial = curr();
  2744. char32_t cp = decode_code_point();
  2745. curr() = initial;
  2746. return cp;
  2747. } else if constexpr (FromFormat == format::utf16) {
  2748. char16_t lo = *--curr();
  2749. if (!boost::parser::detail::text::surrogate(lo))
  2750. return lo;
  2751. if (boost::parser::detail::text::high_surrogate(lo)) {
  2752. return ErrorHandler{}(
  2753. "Invalid UTF-16 sequence; lone leading surrogate.");
  2754. }
  2755. // low surrogate
  2756. if (curr() == first()) {
  2757. return ErrorHandler{}(
  2758. "Invalid UTF-16 sequence; lone trailing surrogate.");
  2759. }
  2760. char16_t hi = *detail::prev(curr());
  2761. if (!boost::parser::detail::text::high_surrogate(hi)) {
  2762. return ErrorHandler{}(
  2763. "Invalid UTF-16 sequence; lone trailing surrogate.");
  2764. }
  2765. --curr();
  2766. return char32_t((hi - high_surrogate_base) << 10) +
  2767. (lo - low_surrogate_base);
  2768. } else {
  2769. return *--curr();
  2770. }
  2771. }
  2772. template<class Out>
  2773. static constexpr Out encode_code_point(char32_t cp, Out out)
  2774. {
  2775. if constexpr (ToFormat == format::utf8) {
  2776. if (cp < 0x80) {
  2777. *out++ = static_cast<char8_type>(cp);
  2778. } else if (cp < 0x800) {
  2779. *out++ = static_cast<char8_type>(0xC0 + (cp >> 6));
  2780. *out++ = static_cast<char8_type>(0x80 + (cp & 0x3f));
  2781. } else if (cp < 0x10000) {
  2782. *out++ = static_cast<char8_type>(0xe0 + (cp >> 12));
  2783. *out++ = static_cast<char8_type>(0x80 + ((cp >> 6) & 0x3f));
  2784. *out++ = static_cast<char8_type>(0x80 + (cp & 0x3f));
  2785. } else {
  2786. *out++ = static_cast<char8_type>(0xf0 + (cp >> 18));
  2787. *out++ = static_cast<char8_type>(0x80 + ((cp >> 12) & 0x3f));
  2788. *out++ = static_cast<char8_type>(0x80 + ((cp >> 6) & 0x3f));
  2789. *out++ = static_cast<char8_type>(0x80 + (cp & 0x3f));
  2790. }
  2791. } else if constexpr (ToFormat == format::utf16) {
  2792. if (cp < 0x10000) {
  2793. *out++ = static_cast<char16_t>(cp);
  2794. } else {
  2795. *out++ =
  2796. static_cast<char16_t>(cp >> 10) + high_surrogate_base;
  2797. *out++ =
  2798. static_cast<char16_t>(cp & 0x3ff) + low_surrogate_base;
  2799. }
  2800. } else {
  2801. *out++ = cp;
  2802. }
  2803. return out;
  2804. }
  2805. constexpr void read()
  2806. {
  2807. I initial;
  2808. if constexpr (
  2809. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2810. std::forward_iterator<I>
  2811. #else
  2812. is_forward<I>
  2813. #endif
  2814. ) {
  2815. initial = curr();
  2816. }
  2817. if constexpr (noexcept(ErrorHandler{}(""))) {
  2818. char32_t cp = decode_code_point();
  2819. auto it = encode_code_point(cp, buf_.begin());
  2820. buf_index_ = 0;
  2821. buf_last_ = uint8_t(it - buf_.begin());
  2822. } else {
  2823. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2824. auto buf = buf_;
  2825. try {
  2826. #endif
  2827. char32_t cp = decode_code_point();
  2828. auto it = encode_code_point(cp, buf_.begin());
  2829. buf_index_ = 0;
  2830. buf_last_ = it - buf_.begin();
  2831. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2832. } catch (...) {
  2833. buf_ = buf;
  2834. curr() = initial;
  2835. throw;
  2836. }
  2837. #endif
  2838. }
  2839. if constexpr (
  2840. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2841. std::forward_iterator<I>
  2842. #else
  2843. is_forward<I>
  2844. #endif
  2845. ) {
  2846. curr() = initial;
  2847. }
  2848. }
  2849. constexpr void read_reverse()
  2850. {
  2851. auto initial = curr();
  2852. if constexpr (noexcept(ErrorHandler{}(""))) {
  2853. char32_t cp = decode_code_point_reverse();
  2854. auto it = encode_code_point(cp, buf_.begin());
  2855. buf_last_ = uint8_t(it - buf_.begin());
  2856. buf_index_ = buf_last_ - 1;
  2857. to_increment_ = (int)std::distance(curr(), initial);
  2858. } else {
  2859. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2860. auto buf = buf_;
  2861. try {
  2862. #endif
  2863. char32_t cp = decode_code_point_reverse();
  2864. auto it = encode_code_point(cp, buf_.begin());
  2865. buf_last_ = it - buf_.begin();
  2866. buf_index_ = buf_last_ - 1;
  2867. to_increment_ = std::distance(curr(), initial);
  2868. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2869. } catch (...) {
  2870. buf_ = buf;
  2871. curr() = initial;
  2872. throw;
  2873. }
  2874. #endif
  2875. }
  2876. }
  2877. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2878. template<
  2879. typename J = I,
  2880. typename Enable = std::enable_if_t<is_bidirectional<J>>>
  2881. #endif
  2882. constexpr I first() const
  2883. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2884. requires std::bidirectional_iterator<I>
  2885. #endif
  2886. {
  2887. return first_and_curr_.first;
  2888. }
  2889. constexpr I & curr() { return first_and_curr_.curr; }
  2890. constexpr I curr() const { return first_and_curr_.curr; }
  2891. std::array<value_type, 4 / static_cast<int>(ToFormat)> buf_ = {};
  2892. detail::first_and_curr<I> first_and_curr_ = {};
  2893. uint8_t buf_index_ = 0;
  2894. uint8_t buf_last_ = 0;
  2895. uint8_t to_increment_ = 0;
  2896. [[no_unique_address]] S last_ = {};
  2897. friend struct detail::iter_access;
  2898. };
  2899. }}
  2900. namespace boost::parser::detail { namespace text { namespace detail {
  2901. template<class T>
  2902. constexpr bool is_utf_iter = false;
  2903. template<
  2904. format FromFormat,
  2905. format ToFormat,
  2906. class I,
  2907. class S,
  2908. class ErrorHandler>
  2909. constexpr bool
  2910. is_utf_iter<utf_iterator<FromFormat, ToFormat, I, S, ErrorHandler>> =
  2911. true;
  2912. // These are here because so many downstream views that use
  2913. // utf_iterator use them.
  2914. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2915. template<typename V>
  2916. constexpr bool common_range_v = std::ranges::common_range<V>;
  2917. template<typename V>
  2918. constexpr bool forward_range_v = std::ranges::forward_range<V>;
  2919. template<typename V>
  2920. constexpr bool bidirectional_range_v = std::ranges::bidirectional_range<V>;
  2921. template<typename T>
  2922. constexpr bool default_initializable_v = std::default_initializable<T>;
  2923. template<typename V>
  2924. constexpr bool utf32_range_v = utf32_range<V>;
  2925. #else
  2926. template<typename T>
  2927. using range_expr =
  2928. decltype(detail::begin(std::declval<T &>()) == detail::end(std::declval<T &>()));
  2929. template<typename T>
  2930. constexpr bool is_range_v = is_detected_v<range_expr, T>;
  2931. template<typename V>
  2932. constexpr bool common_range_v =
  2933. is_range_v<V> && std::is_same_v<iterator_t<V>, sentinel_t<V>>;
  2934. template<typename V>
  2935. constexpr bool input_range_v = is_range_v<V> && std::is_base_of_v<
  2936. std::input_iterator_tag,
  2937. typename std::iterator_traits<iterator_t<V>>::iterator_category>;
  2938. template<typename V>
  2939. constexpr bool forward_range_v = is_range_v<V> && std::is_base_of_v<
  2940. std::forward_iterator_tag,
  2941. typename std::iterator_traits<iterator_t<V>>::iterator_category>;
  2942. template<typename V>
  2943. constexpr bool bidirectional_range_v = is_range_v<V> && std::is_base_of_v<
  2944. std::bidirectional_iterator_tag,
  2945. typename std::iterator_traits<iterator_t<V>>::iterator_category>;
  2946. template<typename T>
  2947. constexpr bool default_initializable_v = std::is_default_constructible_v<T>;
  2948. template<typename V>
  2949. constexpr bool utf_range_v = is_range_v<V> && code_unit_v<range_value_t<V>>;
  2950. template<typename V>
  2951. constexpr bool
  2952. utf32_range_v = is_range_v<V> &&
  2953. (
  2954. #if !defined(_MSC_VER)
  2955. std::is_same_v<range_value_t<V>, wchar_t> ||
  2956. #endif
  2957. std::is_same_v<range_value_t<V>, char32_t>);
  2958. #endif
  2959. template<typename I>
  2960. constexpr bool random_access_iterator_v = std::is_base_of_v<
  2961. std::random_access_iterator_tag,
  2962. typename std::iterator_traits<I>::iterator_category>;
  2963. template<typename I>
  2964. constexpr bool bidirectional_iterator_v = std::is_base_of_v<
  2965. std::bidirectional_iterator_tag,
  2966. typename std::iterator_traits<I>::iterator_category>;
  2967. template<typename I>
  2968. constexpr bool forward_iterator_v = std::is_base_of_v<
  2969. std::forward_iterator_tag,
  2970. typename std::iterator_traits<I>::iterator_category>;
  2971. template<
  2972. class V,
  2973. bool StoreFirst = !is_utf_iter<iterator_t<V>> && common_range_v<V> &&
  2974. bidirectional_range_v<V>,
  2975. bool StoreLast = !is_utf_iter<iterator_t<V>>>
  2976. struct first_last_storage
  2977. {
  2978. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2979. template<
  2980. typename Enable = std::enable_if_t<
  2981. default_initializable_v<iterator_t<V>> &&
  2982. default_initializable_v<sentinel_t<V>>>>
  2983. #endif
  2984. constexpr first_last_storage()
  2985. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  2986. requires default_initializable_v<iterator_t<V>> &&
  2987. default_initializable_v<sentinel_t<V>>
  2988. #endif
  2989. {}
  2990. constexpr first_last_storage(V & base) :
  2991. first_{detail::begin(base)}, last_{detail::end(base)}
  2992. {}
  2993. constexpr auto begin(iterator_t<V> & it) const { return first_; }
  2994. constexpr auto end(iterator_t<V> & it) const { return last_; }
  2995. iterator_t<V> first_;
  2996. sentinel_t<V> last_;
  2997. };
  2998. template<typename I>
  2999. using trinary_iter_ctor = decltype(I(
  3000. std::declval<I>().begin(),
  3001. std::declval<I>().end(),
  3002. std::declval<I>().end()));
  3003. template<class V>
  3004. struct first_last_storage<V, true, false>
  3005. {
  3006. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  3007. template<
  3008. typename Enable =
  3009. std::enable_if_t<default_initializable_v<iterator_t<V>>>>
  3010. #endif
  3011. constexpr first_last_storage()
  3012. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  3013. requires default_initializable_v<iterator_t<V>>
  3014. #endif
  3015. {}
  3016. constexpr first_last_storage(V & base) : first_{detail::begin(base)} {}
  3017. constexpr auto begin(iterator_t<V> & it) const { return first_; }
  3018. constexpr auto end(iterator_t<V> & it) const {
  3019. if constexpr (
  3020. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  3021. requires { iterator_t<V>(it.begin(), it.end(), it.end()); }
  3022. #else
  3023. is_detected_v<trinary_iter_ctor, iterator_t<V>>
  3024. #endif
  3025. ) {
  3026. return iterator_t<V>(it.begin(), it.end(), it.end());
  3027. } else {
  3028. return it.end();
  3029. }
  3030. }
  3031. iterator_t<V> first_;
  3032. };
  3033. template<class V>
  3034. struct first_last_storage<V, false, true>
  3035. {
  3036. #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  3037. template<
  3038. typename Enable =
  3039. std::enable_if_t<default_initializable_v<sentinel_t<V>>>>
  3040. #endif
  3041. constexpr first_last_storage()
  3042. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  3043. requires default_initializable_v<sentinel_t<V>>
  3044. #endif
  3045. {}
  3046. constexpr first_last_storage(V & base) : last_{detail::end(base)} {}
  3047. constexpr auto begin(iterator_t<V> & it) const {
  3048. if constexpr (is_utf_iter<iterator_t<V>>) {
  3049. return iterator_t<V>(it.begin(), it.begin(), it.end());
  3050. } else {
  3051. return;
  3052. }
  3053. }
  3054. constexpr auto end(iterator_t<V> & it) const { return last_; }
  3055. sentinel_t<V> last_;
  3056. };
  3057. template<class V>
  3058. struct first_last_storage<V, false, false>
  3059. {
  3060. constexpr first_last_storage() = default;
  3061. constexpr first_last_storage(V & base) {}
  3062. constexpr auto begin(iterator_t<V> & it) const {
  3063. if constexpr (is_utf_iter<iterator_t<V>>) {
  3064. return iterator_t<V>(it.begin(), it.begin(), it.end());
  3065. } else {
  3066. return;
  3067. }
  3068. }
  3069. constexpr auto end(iterator_t<V> & it) const {
  3070. if constexpr (
  3071. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  3072. requires { iterator_t<V>(it.begin(), it.end(), it.end()); }
  3073. #else
  3074. is_detected_v<trinary_iter_ctor, iterator_t<V>>
  3075. #endif
  3076. ) {
  3077. return iterator_t<V>(it.begin(), it.end(), it.end());
  3078. } else {
  3079. return it.end();
  3080. }
  3081. }
  3082. };
  3083. template<class V>
  3084. constexpr auto uc_view_category() {
  3085. if constexpr (common_range_v<V> && bidirectional_range_v<V>) {
  3086. return std::bidirectional_iterator_tag{};
  3087. } else {
  3088. return std::forward_iterator_tag{};
  3089. }
  3090. }
  3091. template<class V>
  3092. using uc_view_category_t = decltype(uc_view_category<V>());
  3093. template<bool Const, class T>
  3094. using maybe_const = std::conditional_t<Const, const T, T>;
  3095. template<class T>
  3096. constexpr bool is_empty_view = false;
  3097. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  3098. template<class T>
  3099. constexpr bool is_empty_view<std::ranges::empty_view<T>> = true;
  3100. #endif
  3101. }}}
  3102. #endif