| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486 |
- // Copyright (C) 2020 T. Zachary Laine
- //
- // Distributed under the Boost Software License, Version 1.0. (See
- // accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- #ifndef BOOST_PARSER_DETAIL_TEXT_TRANSCODE_ITERATOR_HPP
- #define BOOST_PARSER_DETAIL_TEXT_TRANSCODE_ITERATOR_HPP
- #include <boost/parser/detail/debug_assert.hpp>
- #include <boost/parser/detail/text/transcode_iterator_fwd.hpp>
- #include <boost/parser/detail/text/concepts.hpp>
- #include <boost/parser/detail/text/utf.hpp>
- #include <boost/parser/detail/text/detail/algorithm.hpp>
- #include <boost/parser/detail/stl_interfaces/iterator_interface.hpp>
- #include <array>
- #include <iterator>
- #include <type_traits>
- #include <stdexcept>
- #include <string_view>
- namespace boost::parser::detail { namespace text {
- namespace {
- constexpr char16_t high_surrogate_base = 0xd7c0;
- constexpr char16_t low_surrogate_base = 0xdc00;
- constexpr char32_t high_surrogate_min = 0xd800;
- constexpr char32_t high_surrogate_max = 0xdbff;
- constexpr char32_t low_surrogate_min = 0xdc00;
- constexpr char32_t low_surrogate_max = 0xdfff;
- constexpr char32_t replacement_character = 0xfffd;
- }
- namespace detail {
- constexpr bool in(unsigned char lo, unsigned char c, unsigned char hi)
- {
- return lo <= c && c <= hi;
- }
- struct throw_on_encoding_error
- {};
- template<typename OutIter>
- inline constexpr OutIter read_into_buf(char32_t cp, OutIter buf)
- {
- if (cp < 0x80) {
- *buf = static_cast<char>(cp);
- ++buf;
- } else if (cp < 0x800) {
- *buf = static_cast<char>(0xC0 + (cp >> 6));
- ++buf;
- *buf = static_cast<char>(0x80 + (cp & 0x3f));
- ++buf;
- } else if (cp < 0x10000) {
- *buf = static_cast<char>(0xe0 + (cp >> 12));
- ++buf;
- *buf = static_cast<char>(0x80 + ((cp >> 6) & 0x3f));
- ++buf;
- *buf = static_cast<char>(0x80 + (cp & 0x3f));
- ++buf;
- } else {
- *buf = static_cast<char>(0xf0 + (cp >> 18));
- ++buf;
- *buf = static_cast<char>(0x80 + ((cp >> 12) & 0x3f));
- ++buf;
- *buf = static_cast<char>(0x80 + ((cp >> 6) & 0x3f));
- ++buf;
- *buf = static_cast<char>(0x80 + (cp & 0x3f));
- ++buf;
- }
- return buf;
- }
- template<typename OutIter>
- constexpr OutIter write_cp_utf8(char32_t cp, OutIter out)
- {
- return detail::read_into_buf(cp, out);
- }
- template<typename OutIter>
- constexpr OutIter write_cp_utf16(char32_t cp, OutIter out)
- {
- if (cp < 0x10000) {
- *out = static_cast<char16_t>(cp);
- ++out;
- } else {
- *out = static_cast<char16_t>(cp >> 10) + high_surrogate_base;
- ++out;
- *out = static_cast<char16_t>(cp & 0x3ff) + low_surrogate_base;
- ++out;
- }
- return out;
- }
- inline constexpr char32_t surrogates_to_cp(char16_t hi, char16_t lo)
- {
- return char32_t((hi - high_surrogate_base) << 10) +
- (lo - low_surrogate_base);
- }
- template<typename T, typename U>
- using enable_utf8_cp = std::enable_if<is_char_iter_v<T>, U>;
- template<typename T, typename U = T>
- using enable_utf8_cp_t = typename enable_utf8_cp<T, U>::type;
- template<typename T, typename U>
- using enable_utf16_cp = std::enable_if<is_16_iter_v<T>, U>;
- template<typename T, typename U = T>
- using enable_utf16_cp_t = typename enable_utf16_cp<T, U>::type;
- template<typename I>
- auto bidirectional_at_most()
- {
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- if constexpr (std::bidirectional_iterator<I>) {
- return std::bidirectional_iterator_tag{};
- } else if constexpr (std::forward_iterator<I>) {
- return std::forward_iterator_tag{};
- } else if constexpr (std::input_iterator<I>) {
- return std::input_iterator_tag{};
- }
- #else
- using category =
- typename std::iterator_traits<I>::iterator_category;
- if constexpr (std::is_base_of_v<
- std::bidirectional_iterator_tag,
- category>) {
- return std::bidirectional_iterator_tag{};
- } else {
- return category{};
- }
- #endif
- }
- template<typename I>
- using bidirectional_at_most_t = decltype(bidirectional_at_most<I>());
- }
- /** Returns true iff `c` is a Unicode surrogate. */
- inline constexpr bool surrogate(char32_t c)
- {
- return high_surrogate_min <= c && c <= low_surrogate_max;
- }
- /** Returns true iff `c` is a Unicode high surrogate. */
- inline constexpr bool high_surrogate(char32_t c)
- {
- return high_surrogate_min <= c && c <= high_surrogate_max;
- }
- /** Returns true iff `c` is a Unicode low surrogate. */
- inline constexpr bool low_surrogate(char32_t c)
- {
- return low_surrogate_min <= c && c <= low_surrogate_max;
- }
- /** Returns true iff `c` is a Unicode reserved noncharacter.
- \see Unicode 3.4/D14 */
- inline constexpr bool reserved_noncharacter(char32_t c)
- {
- bool const byte01_reserved = (c & 0xffff) >= 0xfffe;
- bool const byte2_at_most_0x10 = ((c & 0xff0000u) >> 16) <= 0x10;
- return (byte01_reserved && byte2_at_most_0x10) ||
- (0xfdd0 <= c && c <= 0xfdef);
- }
- /** Returns true iff `c` is a valid Unicode scalar value.
- \see Unicode 3.9/D90 */
- inline constexpr bool scalar_value(char32_t c)
- {
- return c <= 0x10ffff && !surrogate(c);
- }
- /** Returns true iff `c` is a Unicode scalar value not in the reserved
- range.
- \see Unicode 3.9/D90 */
- inline constexpr bool unreserved_scalar_value(char32_t c)
- {
- return scalar_value(c) && !reserved_noncharacter(c);
- }
- /** Returns true iff `c` is a UTF-8 lead code unit (which must be followed
- by 1-3 following units). */
- constexpr bool lead_code_unit(char8_type c)
- {
- return uint8_t((unsigned char)c - 0xc2) <= 0x32;
- }
- /** Returns true iff `c` is a UTF-8 continuation code unit. */
- constexpr bool continuation(char8_type c) { return (int8_t)c < -0x40; }
- /** Given the first (and possibly only) code unit of a UTF-8-encoded code
- point, returns the number of bytes occupied by that code point (in the
- range `[1, 4]`). Returns a value < 0 if `first_unit` is not a valid
- initial UTF-8 code unit. */
- inline constexpr int utf8_code_units(char8_type first_unit_)
- {
- auto first_unit = (unsigned int)first_unit_;
- return first_unit <= 0x7f ? 1
- : boost::parser::detail::text::lead_code_unit(first_unit)
- ? int(0xe0 <= first_unit) + int(0xf0 <= first_unit) + 2
- : -1;
- }
- /** Given the first (and possibly only) code unit of a UTF-16-encoded code
- point, returns the number of code units occupied by that code point
- (in the range `[1, 2]`). Returns a negative value if `first_unit` is
- not a valid initial UTF-16 code unit. */
- inline constexpr int utf16_code_units(char16_t first_unit)
- {
- if (boost::parser::detail::text::low_surrogate(first_unit))
- return -1;
- if (boost::parser::detail::text::high_surrogate(first_unit))
- return 2;
- return 1;
- }
- namespace detail {
- // optional is not constexpr friendly.
- template<typename Iter>
- struct optional_iter
- {
- constexpr optional_iter() : it_(), valid_(false) {}
- constexpr optional_iter(Iter it) : it_(it), valid_(true) {}
- constexpr operator bool() const { return valid_; }
- constexpr Iter operator*() const
- {
- BOOST_PARSER_DEBUG_ASSERT(valid_);
- return it_;
- }
- Iter & operator*()
- {
- BOOST_PARSER_DEBUG_ASSERT(valid_);
- return it_;
- }
- friend BOOST_PARSER_CONSTEXPR bool
- operator==(optional_iter lhs, optional_iter rhs)
- {
- return lhs.valid_ == rhs.valid_ &&
- (!lhs.valid_ || lhs.it_ == rhs.it_);
- }
- friend BOOST_PARSER_CONSTEXPR bool
- operator!=(optional_iter lhs, optional_iter rhs)
- {
- return !(lhs == rhs);
- }
- private:
- Iter it_;
- bool valid_;
- };
- // Follow Table 3-7 in Unicode 3.9/D92
- template<typename Iter>
- constexpr optional_iter<Iter> end_of_invalid_utf8(Iter it)
- {
- BOOST_PARSER_DEBUG_ASSERT(!boost::parser::detail::text::continuation(*it));
- if (detail::in(0, *it, 0x7f))
- return optional_iter<Iter>{};
- if (detail::in(0xc2, *it, 0xdf)) {
- auto next = it;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- return optional_iter<Iter>{};
- }
- if (detail::in(0xe0, *it, 0xe0)) {
- auto next = it;
- if (!detail::in(0xa0, *++next, 0xbf))
- return next;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- return optional_iter<Iter>{};
- }
- if (detail::in(0xe1, *it, 0xec)) {
- auto next = it;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- return optional_iter<Iter>{};
- }
- if (detail::in(0xed, *it, 0xed)) {
- auto next = it;
- if (!detail::in(0x80, *++next, 0x9f))
- return next;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- return optional_iter<Iter>{};
- }
- if (detail::in(0xee, *it, 0xef)) {
- auto next = it;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- return optional_iter<Iter>{};
- }
- if (detail::in(0xf0, *it, 0xf0)) {
- auto next = it;
- if (!detail::in(0x90, *++next, 0xbf))
- return next;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- return optional_iter<Iter>{};
- }
- if (detail::in(0xf1, *it, 0xf3)) {
- auto next = it;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- return optional_iter<Iter>{};
- }
- if (detail::in(0xf4, *it, 0xf4)) {
- auto next = it;
- if (!detail::in(0x80, *++next, 0x8f))
- return next;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- if (!boost::parser::detail::text::continuation(*++next))
- return next;
- return optional_iter<Iter>{};
- }
- return it;
- }
- template<typename Iter>
- constexpr Iter decrement(Iter it)
- {
- Iter retval = it;
- int backup = 0;
- while (backup < 4 && boost::parser::detail::text::continuation(*--retval)) {
- ++backup;
- }
- backup = it - retval;
- if (boost::parser::detail::text::continuation(*retval))
- return it - 1;
- optional_iter<Iter> first_invalid = end_of_invalid_utf8(retval);
- if (first_invalid == retval)
- ++*first_invalid;
- while (first_invalid && (*first_invalid - retval) < backup) {
- backup -= *first_invalid - retval;
- retval = *first_invalid;
- first_invalid = end_of_invalid_utf8(retval);
- if (first_invalid == retval)
- ++*first_invalid;
- }
- if (1 < backup) {
- int const cp_bytes = boost::parser::detail::text::utf8_code_units(*retval);
- if (cp_bytes < backup)
- retval = it - 1;
- }
- return retval;
- }
- template<typename Iter>
- constexpr Iter decrement(Iter first, Iter it)
- {
- Iter retval = it;
- int backup = 0;
- while (backup < 4 && retval != first &&
- boost::parser::detail::text::continuation(*--retval)) {
- ++backup;
- }
- backup = (int)std::distance(retval, it);
- if (boost::parser::detail::text::continuation(*retval)) {
- if (it != first)
- --it;
- return it;
- }
- optional_iter<Iter> first_invalid = end_of_invalid_utf8(retval);
- if (first_invalid == retval)
- ++*first_invalid;
- while (first_invalid &&
- std::distance(retval, *first_invalid) < backup) {
- backup -= (int)std::distance(retval, *first_invalid);
- retval = *first_invalid;
- first_invalid = end_of_invalid_utf8(retval);
- if (first_invalid == retval)
- ++*first_invalid;
- }
- if (1 < backup) {
- int const cp_bytes = boost::parser::detail::text::utf8_code_units(*retval);
- if (cp_bytes < backup) {
- if (it != first)
- --it;
- retval = it;
- }
- }
- return retval;
- }
- enum char_class : uint8_t {
- ill = 0,
- asc = 1,
- cr1 = 2,
- cr2 = 3,
- cr3 = 4,
- l2a = 5,
- l3a = 6,
- l3b = 7,
- l3c = 8,
- l4a = 9,
- l4b = 10,
- l4c = 11,
- };
- enum table_state : uint8_t {
- bgn = 0,
- e_d = bgn, // "end"
- err = 12,
- cs1 = 24,
- cs2 = 36,
- cs3 = 48,
- p3a = 60,
- p3b = 72,
- p4a = 84,
- p4b = 96,
- invalid_table_state = 200
- };
- struct first_cu
- {
- unsigned char initial_octet;
- table_state next;
- };
- namespace {
- constexpr first_cu first_cus[256] = {
- {0x00, bgn}, {0x01, bgn}, {0x02, bgn}, {0x03, bgn}, {0x04, bgn},
- {0x05, bgn}, {0x06, bgn}, {0x07, bgn}, {0x08, bgn}, {0x09, bgn},
- {0x0a, bgn}, {0x0b, bgn}, {0x0c, bgn}, {0x0d, bgn}, {0x0e, bgn},
- {0x0f, bgn}, {0x10, bgn}, {0x11, bgn}, {0x12, bgn}, {0x13, bgn},
- {0x14, bgn}, {0x15, bgn}, {0x16, bgn}, {0x17, bgn}, {0x18, bgn},
- {0x19, bgn}, {0x1a, bgn}, {0x1b, bgn}, {0x1c, bgn}, {0x1d, bgn},
- {0x1e, bgn}, {0x1f, bgn}, {0x20, bgn}, {0x21, bgn}, {0x22, bgn},
- {0x23, bgn}, {0x24, bgn}, {0x25, bgn}, {0x26, bgn}, {0x27, bgn},
- {0x28, bgn}, {0x29, bgn}, {0x2a, bgn}, {0x2b, bgn}, {0x2c, bgn},
- {0x2d, bgn}, {0x2e, bgn}, {0x2f, bgn}, {0x30, bgn}, {0x31, bgn},
- {0x32, bgn}, {0x33, bgn}, {0x34, bgn}, {0x35, bgn}, {0x36, bgn},
- {0x37, bgn}, {0x38, bgn}, {0x39, bgn}, {0x3a, bgn}, {0x3b, bgn},
- {0x3c, bgn}, {0x3d, bgn}, {0x3e, bgn}, {0x3f, bgn}, {0x40, bgn},
- {0x41, bgn}, {0x42, bgn}, {0x43, bgn}, {0x44, bgn}, {0x45, bgn},
- {0x46, bgn}, {0x47, bgn}, {0x48, bgn}, {0x49, bgn}, {0x4a, bgn},
- {0x4b, bgn}, {0x4c, bgn}, {0x4d, bgn}, {0x4e, bgn}, {0x4f, bgn},
- {0x50, bgn}, {0x51, bgn}, {0x52, bgn}, {0x53, bgn}, {0x54, bgn},
- {0x55, bgn}, {0x56, bgn}, {0x57, bgn}, {0x58, bgn}, {0x59, bgn},
- {0x5a, bgn}, {0x5b, bgn}, {0x5c, bgn}, {0x5d, bgn}, {0x5e, bgn},
- {0x5f, bgn}, {0x60, bgn}, {0x61, bgn}, {0x62, bgn}, {0x63, bgn},
- {0x64, bgn}, {0x65, bgn}, {0x66, bgn}, {0x67, bgn}, {0x68, bgn},
- {0x69, bgn}, {0x6a, bgn}, {0x6b, bgn}, {0x6c, bgn}, {0x6d, bgn},
- {0x6e, bgn}, {0x6f, bgn}, {0x70, bgn}, {0x71, bgn}, {0x72, bgn},
- {0x73, bgn}, {0x74, bgn}, {0x75, bgn}, {0x76, bgn}, {0x77, bgn},
- {0x78, bgn}, {0x79, bgn}, {0x7a, bgn}, {0x7b, bgn}, {0x7c, bgn},
- {0x7d, bgn}, {0x7e, bgn}, {0x7f, bgn}, {0x00, err}, {0x01, err},
- {0x02, err}, {0x03, err}, {0x04, err}, {0x05, err}, {0x06, err},
- {0x07, err}, {0x08, err}, {0x09, err}, {0x0a, err}, {0x0b, err},
- {0x0c, err}, {0x0d, err}, {0x0e, err}, {0x0f, err}, {0x10, err},
- {0x11, err}, {0x12, err}, {0x13, err}, {0x14, err}, {0x15, err},
- {0x16, err}, {0x17, err}, {0x18, err}, {0x19, err}, {0x1a, err},
- {0x1b, err}, {0x1c, err}, {0x1d, err}, {0x1e, err}, {0x1f, err},
- {0x20, err}, {0x21, err}, {0x22, err}, {0x23, err}, {0x24, err},
- {0x25, err}, {0x26, err}, {0x27, err}, {0x28, err}, {0x29, err},
- {0x2a, err}, {0x2b, err}, {0x2c, err}, {0x2d, err}, {0x2e, err},
- {0x2f, err}, {0x30, err}, {0x31, err}, {0x32, err}, {0x33, err},
- {0x34, err}, {0x35, err}, {0x36, err}, {0x37, err}, {0x38, err},
- {0x39, err}, {0x3a, err}, {0x3b, err}, {0x3c, err}, {0x3d, err},
- {0x3e, err}, {0x3f, err}, {0xc0, err}, {0xc1, err}, {0x02, cs1},
- {0x03, cs1}, {0x04, cs1}, {0x05, cs1}, {0x06, cs1}, {0x07, cs1},
- {0x08, cs1}, {0x09, cs1}, {0x0a, cs1}, {0x0b, cs1}, {0x0c, cs1},
- {0x0d, cs1}, {0x0e, cs1}, {0x0f, cs1}, {0x10, cs1}, {0x11, cs1},
- {0x12, cs1}, {0x13, cs1}, {0x14, cs1}, {0x15, cs1}, {0x16, cs1},
- {0x17, cs1}, {0x18, cs1}, {0x19, cs1}, {0x1a, cs1}, {0x1b, cs1},
- {0x1c, cs1}, {0x1d, cs1}, {0x1e, cs1}, {0x1f, cs1}, {0x00, p3a},
- {0x01, cs2}, {0x02, cs2}, {0x03, cs2}, {0x04, cs2}, {0x05, cs2},
- {0x06, cs2}, {0x07, cs2}, {0x08, cs2}, {0x09, cs2}, {0x0a, cs2},
- {0x0b, cs2}, {0x0c, cs2}, {0x0d, p3b}, {0x0e, cs2}, {0x0f, cs2},
- {0x00, p4a}, {0x01, cs3}, {0x02, cs3}, {0x03, cs3}, {0x04, p4b},
- {0xf5, err}, {0xf6, err}, {0xf7, err}, {0xf8, err}, {0xf9, err},
- {0xfa, err}, {0xfb, err}, {0xfc, err}, {0xfd, err}, {0xfe, err},
- {0xff, err},
- };
- constexpr char_class octet_classes[256] = {
- asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
- asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
- asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
- asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
- asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
- asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
- asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
- asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
- asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc,
- asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, asc, cr1, cr1,
- cr1, cr1, cr1, cr1, cr1, cr1, cr1, cr1, cr1, cr1, cr1, cr1, cr1,
- cr1, cr2, cr2, cr2, cr2, cr2, cr2, cr2, cr2, cr2, cr2, cr2, cr2,
- cr2, cr2, cr2, cr2, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3,
- cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3,
- cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, cr3, ill, ill, l2a,
- l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a,
- l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a, l2a,
- l2a, l2a, l2a, l3a, l3b, l3b, l3b, l3b, l3b, l3b, l3b, l3b, l3b,
- l3b, l3b, l3b, l3c, l3b, l3b, l4a, l4b, l4b, l4b, l4c, ill, ill,
- ill, ill, ill, ill, ill, ill, ill, ill, ill,
- };
- constexpr table_state transitions[108] = {
- err, e_d, err, err, err, cs1, p3a, cs2, p3b, p4a, cs3, p4b,
- err, err, err, err, err, err, err, err, err, err, err, err,
- err, err, e_d, e_d, e_d, err, err, err, err, err, err, err,
- err, err, cs1, cs1, cs1, err, err, err, err, err, err, err,
- err, err, cs2, cs2, cs2, err, err, err, err, err, err, err,
- err, err, err, err, cs1, err, err, err, err, err, err, err,
- err, err, cs1, cs1, err, err, err, err, err, err, err, err,
- err, err, err, cs2, cs2, err, err, err, err, err, err, err,
- err, err, cs2, err, err, err, err, err, err, err, err, err,
- };
- }
- template<typename InputIter, typename Sentinel>
- char32_t advance(InputIter & first, Sentinel last)
- {
- char32_t retval = 0;
- first_cu const info = first_cus[(unsigned char)*first];
- ++first;
- retval = info.initial_octet;
- int state = info.next;
- while (state != bgn) {
- if (first != last) {
- unsigned char const cu = *first;
- retval = (retval << 6) | (cu & 0x3f);
- char_class const class_ = octet_classes[cu];
- state = transitions[state + class_];
- if (state == err)
- return replacement_character;
- ++first;
- } else {
- return replacement_character;
- }
- }
- return retval;
- }
- template<typename Derived, typename Iter>
- struct trans_ins_iter
- {
- using value_type = void;
- using difference_type =
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- std::ptrdiff_t;
- #else
- void;
- #endif
- using pointer = void;
- using reference = void;
- using iterator_category = std::output_iterator_tag;
- constexpr trans_ins_iter() {}
- constexpr trans_ins_iter(Iter it) : it_(it) {}
- constexpr Derived & operator*() { return derived(); }
- constexpr Derived & operator++() { return derived(); }
- constexpr Derived operator++(int) { return derived(); }
- constexpr Iter base() const { return it_; }
- protected:
- constexpr Iter & iter() { return it_; }
- private:
- constexpr Derived & derived()
- {
- return static_cast<Derived &>(*this);
- }
- Iter it_;
- };
- template<typename Derived, typename I, typename ValueType>
- using trans_iter = stl_interfaces::iterator_interface<
- Derived,
- bidirectional_at_most_t<I>,
- ValueType,
- ValueType>;
- }
- }}
- namespace boost::parser::detail { namespace text { BOOST_PARSER_DETAIL_TEXT_NAMESPACE_V1 {
- #if defined(BOOST_TEXT_DOXYGEN)
- /** Returns the first code unit in `[r.begin(), r.end())` that is not
- properly UTF-8 encoded, or `r.begin() + std::distance(r)` if no such
- code unit is found. */
- template<utf8_range R>
- requires std::ranges::forward_range<R>
- constexpr std::ranges::borrowed_iterator_t<R> find_invalid_encoding(R && r);
- /** Returns the first code unit in `[r.begin(), r.end())` that is not
- properly UTF-16 encoded, or `r.begin() + std::distance(r)` if no such
- code unit is found. */
- template<utf16_range R>
- requires std::ranges::forward_range<R>
- constexpr std::ranges::borrowed_iterator_t<R> find_invalid_encoding(R && r);
- /** Returns true iff `r` is properly UTF-8 encoded. */
- template<utf8_range R>
- requires std::ranges::forward_range<R>
- constexpr bool encoded(R && r);
- /** Returns true iff `r` is properly UTF-16 encoded */
- template<utf16_range R>
- requires std::ranges::forward_range<R>
- constexpr bool encoded(R && r);
- /** Returns true iff `r` is empty or the initial UTF-8 code units in `r`
- form a valid Unicode code point. */
- template<utf8_range R>
- requires std::ranges::forward_range<R>
- constexpr bool starts_encoded(R && r);
- /** Returns true iff `r` is empty or the initial UTF-16 code units in `r`
- form a valid Unicode code point. */
- template<utf16_range R>
- requires std::ranges::forward_range<R>
- constexpr bool starts_encoded(R && r);
- /** Returns true iff `r` is empty or the final UTF-8 code units in `r`
- form a valid Unicode code point. */
- template<utf8_range R>
- requires std::ranges::bidirectional_range<R> &&
- std::ranges::common_range<R>
- constexpr bool ends_encoded(R && r);
- /** Returns true iff `r` is empty or the final UTF-16 code units in `r`
- form a valid Unicode code point. */
- template<utf8_range R>
- requires std::ranges::bidirectional_range<R> &&
- std::ranges::common_range<R>
- constexpr bool ends_encoded(R && r);
- #endif
- template<typename Iter>
- constexpr detail::enable_utf8_cp_t<Iter>
- find_invalid_encoding(Iter first, Iter last)
- {
- while (first != last) {
- int const cp_bytes = boost::parser::detail::text::utf8_code_units(*first);
- if (cp_bytes == -1 || last - first < cp_bytes)
- return first;
- if (detail::end_of_invalid_utf8(first))
- return first;
- first += cp_bytes;
- }
- return last;
- }
- template<typename Iter>
- constexpr detail::enable_utf16_cp_t<Iter>
- find_invalid_encoding(Iter first, Iter last)
- {
- while (first != last) {
- int const cp_units = boost::parser::detail::text::utf16_code_units(*first);
- if (cp_units == -1 || last - first < cp_units)
- return first;
- if (cp_units == 2 && !boost::parser::detail::text::low_surrogate(*(first + 1)))
- return first;
- first += cp_units;
- }
- return last;
- }
- template<typename Iter>
- constexpr detail::enable_utf8_cp_t<Iter, bool> encoded(
- Iter first, Iter last)
- {
- return v1::find_invalid_encoding(first, last) == last;
- }
- template<typename Iter>
- constexpr detail::enable_utf16_cp_t<Iter, bool> encoded(
- Iter first, Iter last)
- {
- return v1::find_invalid_encoding(first, last) == last;
- }
- template<typename Iter>
- constexpr detail::enable_utf8_cp_t<Iter, bool>
- starts_encoded(Iter first, Iter last)
- {
- if (first == last)
- return true;
- int const cp_bytes = boost::parser::detail::text::utf8_code_units(*first);
- if (cp_bytes == -1 || last - first < cp_bytes)
- return false;
- return !detail::end_of_invalid_utf8(first);
- }
- template<typename Iter>
- constexpr detail::enable_utf16_cp_t<Iter, bool>
- starts_encoded(Iter first, Iter last)
- {
- if (first == last)
- return true;
- int const cp_units = boost::parser::detail::text::utf16_code_units(*first);
- if (cp_units == -1 || last - first < cp_units)
- return false;
- return cp_units == 1 || boost::parser::detail::text::low_surrogate(*(first + 1));
- }
- template<typename Iter>
- constexpr detail::enable_utf8_cp_t<Iter, bool>
- ends_encoded(Iter first, Iter last)
- {
- if (first == last)
- return true;
- auto it = last;
- while (first != --it && boost::parser::detail::text::continuation(*it))
- ;
- return v1::starts_encoded(it, last);
- }
- template<typename Iter>
- constexpr detail::enable_utf16_cp_t<Iter, bool>
- ends_encoded(Iter first, Iter last)
- {
- if (first == last)
- return true;
- auto it = last;
- if (boost::parser::detail::text::low_surrogate(*--it))
- --it;
- return v1::starts_encoded(it, last);
- }
- }}}
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- namespace boost::parser::detail { namespace text { BOOST_PARSER_DETAIL_TEXT_NAMESPACE_V2 {
- template<utf8_range R>
- requires std::ranges::forward_range<R>
- constexpr std::ranges::borrowed_iterator_t<R> find_invalid_encoding(R && r)
- {
- auto first = std::ranges::begin(r);
- auto last = std::ranges::end(r);
- while (first != last) {
- int const cp_bytes = boost::parser::detail::text::utf8_code_units(*first);
- if (cp_bytes == -1 || last - first < cp_bytes)
- return first;
- if (detail::end_of_invalid_utf8(first))
- return first;
- first += cp_bytes;
- }
- if constexpr (std::ranges::borrowed_range<R>) {
- return last;
- } else {
- return std::ranges::dangling{};
- }
- }
- template<utf16_range R>
- requires std::ranges::forward_range<R>
- constexpr std::ranges::borrowed_iterator_t<R> find_invalid_encoding(R && r)
- {
- auto first = std::ranges::begin(r);
- auto last = std::ranges::end(r);
- while (first != last) {
- int const cp_units = boost::parser::detail::text::utf16_code_units(*first);
- if (cp_units == -1 || last - first < cp_units)
- return first;
- if (cp_units == 2 && !boost::parser::detail::text::low_surrogate(*(first + 1)))
- return first;
- first += cp_units;
- }
- if constexpr (std::ranges::borrowed_range<R>) {
- return last;
- } else {
- return std::ranges::dangling{};
- }
- }
- template<utf8_range R>
- requires std::ranges::forward_range<R>
- constexpr bool encoded(R && r)
- {
- return boost::parser::detail::text::v1::find_invalid_encoding(r.begin(), r.end()) ==
- r.end();
- }
- template<utf16_range R>
- requires std::ranges::forward_range<R>
- constexpr bool encoded(R && r)
- {
- return boost::parser::detail::text::v1::find_invalid_encoding(r.begin(), r.end()) ==
- r.end();
- }
- template<utf8_range R>
- requires std::ranges::forward_range<R>
- constexpr bool starts_encoded(R && r)
- {
- auto first = std::ranges::begin(r);
- auto last = std::ranges::end(r);
- if (first == last)
- return true;
- int const cp_bytes = boost::parser::detail::text::utf8_code_units(*first);
- if (cp_bytes == -1 || last - first < cp_bytes)
- return false;
- return !detail::end_of_invalid_utf8(first);
- }
- template<utf16_range R>
- requires std::ranges::forward_range<R>
- constexpr bool starts_encoded(R && r)
- {
- auto first = std::ranges::begin(r);
- auto last = std::ranges::end(r);
- if (first == last)
- return true;
- int const cp_units = boost::parser::detail::text::utf16_code_units(*first);
- if (cp_units == -1 || last - first < cp_units)
- return false;
- return cp_units == 1 || boost::parser::detail::text::low_surrogate(*(first + 1));
- }
- template<utf8_range R>
- requires std::ranges::bidirectional_range<R> &&
- std::ranges::common_range<R>
- constexpr bool ends_encoded(R && r)
- {
- auto first = std::ranges::begin(r);
- auto last = std::ranges::end(r);
- if (first == last)
- return true;
- auto it = last;
- while (first != --it && boost::parser::detail::text::continuation(*it))
- ;
- return boost::parser::detail::text::starts_encoded(it, last);
- }
- template<utf16_range R>
- requires std::ranges::bidirectional_range<R> &&
- std::ranges::common_range<R>
- constexpr bool ends_encoded(R && r)
- {
- auto first = std::ranges::begin(r);
- auto last = std::ranges::end(r);
- if (first == last)
- return true;
- auto it = last;
- if (boost::parser::detail::text::low_surrogate(*--it))
- --it;
- return boost::parser::detail::text::starts_encoded(it, last);
- }
- }}}
- #endif
- namespace boost::parser::detail { namespace text {
- /** An error handler type that can be used with the converting iterators;
- provides the Unicode replacement character on errors. */
- struct use_replacement_character
- {
- constexpr char32_t operator()(std::string_view) const noexcept
- {
- return replacement_character;
- }
- };
- /** A sentinel type that compares equal to a pointer to a 1-, 2-, or
- 4-byte integral value, iff the pointer is null. */
- struct null_sentinel_t
- {
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<std::input_iterator I>
- requires std::default_initializable<std::iter_value_t<I>> &&
- std::equality_comparable_with<std::iter_reference_t<I>, std::iter_value_t<I>>
- #else
- template<typename I>
- #endif
- friend constexpr bool operator==(I it, null_sentinel_t)
- {
- return *it == detail::iter_value_t<I>{};
- }
- #if !defined(__cpp_impl_three_way_comparison)
- template<typename I>
- friend constexpr bool operator==(null_sentinel_t, I it)
- {
- return *it == detail::iter_value_t<I>{};
- }
- template<typename I>
- friend constexpr bool operator!=(I it, null_sentinel_t)
- {
- return *it != detail::iter_value_t<I>{};
- }
- template<typename I>
- friend constexpr bool operator!=(null_sentinel_t, I it)
- {
- return *it != detail::iter_value_t<I>{};
- }
- #endif
- };
- #if defined(__cpp_inline_variables)
- inline constexpr null_sentinel_t null_sentinel;
- #else
- namespace {
- constexpr null_sentinel_t null_sentinel;
- }
- #endif
- /** An out iterator that converts UTF-32 to UTF-8. */
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<std::output_iterator<char8_t> Iter>
- #else
- template<typename Iter>
- #endif
- struct utf_32_to_8_out_iterator
- : detail::trans_ins_iter<utf_32_to_8_out_iterator<Iter>, Iter>
- {
- constexpr utf_32_to_8_out_iterator() {}
- explicit constexpr utf_32_to_8_out_iterator(Iter it) :
- detail::trans_ins_iter<utf_32_to_8_out_iterator<Iter>, Iter>(it)
- {}
- constexpr utf_32_to_8_out_iterator & operator=(char32_t cp)
- {
- auto & out = this->iter();
- out = detail::write_cp_utf8(cp, out);
- return *this;
- }
- };
- /** An insert-iterator analogous to std::insert_iterator, that also
- converts UTF-32 to UTF-8. */
- template<typename Cont>
- struct utf_32_to_8_insert_iterator : detail::trans_ins_iter<
- utf_32_to_8_insert_iterator<Cont>,
- std::insert_iterator<Cont>>
- {
- constexpr utf_32_to_8_insert_iterator() {}
- constexpr utf_32_to_8_insert_iterator(
- Cont & c, typename Cont::iterator it) :
- detail::trans_ins_iter<
- utf_32_to_8_insert_iterator<Cont>,
- std::insert_iterator<Cont>>(std::insert_iterator<Cont>(c, it))
- {}
- constexpr utf_32_to_8_insert_iterator & operator=(char32_t cp)
- {
- auto & out = this->iter();
- out = detail::write_cp_utf8(cp, out);
- return *this;
- }
- };
- /** An insert-iterator analogous to std::front_insert_iterator, that also
- converts UTF-32 to UTF-8. */
- template<typename Cont>
- struct utf_32_to_8_front_insert_iterator
- : detail::trans_ins_iter<
- utf_32_to_8_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>>
- {
- constexpr utf_32_to_8_front_insert_iterator() {}
- explicit constexpr utf_32_to_8_front_insert_iterator(Cont & c) :
- detail::trans_ins_iter<
- utf_32_to_8_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>>(
- std::front_insert_iterator<Cont>(c))
- {}
- constexpr utf_32_to_8_front_insert_iterator & operator=(char32_t cp)
- {
- auto & out = this->iter();
- out = detail::write_cp_utf8(cp, out);
- return *this;
- }
- };
- /** An insert-iterator analogous to std::back_insert_iterator, that also
- converts UTF-32 to UTF-8. */
- template<typename Cont>
- struct utf_32_to_8_back_insert_iterator
- : detail::trans_ins_iter<
- utf_32_to_8_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>>
- {
- constexpr utf_32_to_8_back_insert_iterator() {}
- explicit constexpr utf_32_to_8_back_insert_iterator(Cont & c) :
- detail::trans_ins_iter<
- utf_32_to_8_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>>(
- std::back_insert_iterator<Cont>(c))
- {}
- constexpr utf_32_to_8_back_insert_iterator & operator=(char32_t cp)
- {
- auto & out = this->iter();
- out = detail::write_cp_utf8(cp, out);
- return *this;
- }
- };
- namespace detail {
- template<typename OutIter>
- OutIter assign_8_to_32_insert(
- unsigned char cu, char32_t & cp, int & state, OutIter out)
- {
- auto write = [&] {
- *out = cp;
- ++out;
- state = invalid_table_state;
- };
- auto start_cp = [&] {
- first_cu const info = first_cus[cu];
- state = info.next;
- cp = info.initial_octet;
- if (state == bgn)
- write();
- };
- if (state == invalid_table_state) {
- start_cp();
- } else {
- cp = (cp << 6) | (cu & 0x3f);
- char_class const class_ = octet_classes[cu];
- state = transitions[state + class_];
- if (state == bgn) {
- write();
- } else if (state == err) {
- *out = replacement_character;
- ++out;
- start_cp();
- }
- }
- return out;
- }
- }
- /** An out iterator that converts UTF-8 to UTF-32. */
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<std::output_iterator<char32_t> Iter>
- #else
- template<typename Iter>
- #endif
- struct utf_8_to_32_out_iterator
- : detail::trans_ins_iter<utf_8_to_32_out_iterator<Iter>, Iter>
- {
- constexpr utf_8_to_32_out_iterator() {}
- explicit constexpr utf_8_to_32_out_iterator(Iter it) :
- detail::trans_ins_iter<utf_8_to_32_out_iterator<Iter>, Iter>(it),
- state_(detail::invalid_table_state)
- {}
- constexpr utf_8_to_32_out_iterator & operator=(char8_type cu)
- {
- auto & out = this->iter();
- out = detail::assign_8_to_32_insert(cu, cp_, state_, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- int state_;
- char32_t cp_;
- #endif
- };
- /** An insert-iterator analogous to std::insert_iterator, that also
- converts UTF-8 to UTF-32. */
- template<typename Cont>
- struct utf_8_to_32_insert_iterator : detail::trans_ins_iter<
- utf_8_to_32_insert_iterator<Cont>,
- std::insert_iterator<Cont>>
- {
- constexpr utf_8_to_32_insert_iterator() {}
- constexpr utf_8_to_32_insert_iterator(
- Cont & c, typename Cont::iterator it) :
- detail::trans_ins_iter<
- utf_8_to_32_insert_iterator<Cont>,
- std::insert_iterator<Cont>>(std::insert_iterator<Cont>(c, it)),
- state_(detail::invalid_table_state)
- {}
- constexpr utf_8_to_32_insert_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_8_to_32_insert(cu, cp_, state_, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- int state_;
- char32_t cp_;
- #endif
- };
- /** An insert-iterator analogous to std::front_insert_iterator, that also
- converts UTF-8 to UTF-32. */
- template<typename Cont>
- struct utf_8_to_32_front_insert_iterator
- : detail::trans_ins_iter<
- utf_8_to_32_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>>
- {
- constexpr utf_8_to_32_front_insert_iterator() {}
- explicit constexpr utf_8_to_32_front_insert_iterator(Cont & c) :
- detail::trans_ins_iter<
- utf_8_to_32_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>>(
- std::front_insert_iterator<Cont>(c)),
- state_(detail::invalid_table_state)
- {}
- constexpr utf_8_to_32_front_insert_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_8_to_32_insert(cu, cp_, state_, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- int state_;
- char32_t cp_;
- #endif
- };
- /** An insert-iterator analogous to std::back_insert_iterator, that also
- converts UTF-8 to UTF-32. */
- template<typename Cont>
- struct utf_8_to_32_back_insert_iterator
- : detail::trans_ins_iter<
- utf_8_to_32_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>>
- {
- constexpr utf_8_to_32_back_insert_iterator() {}
- explicit constexpr utf_8_to_32_back_insert_iterator(Cont & c) :
- detail::trans_ins_iter<
- utf_8_to_32_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>>(
- std::back_insert_iterator<Cont>(c)),
- state_(detail::invalid_table_state)
- {}
- constexpr utf_8_to_32_back_insert_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_8_to_32_insert(cu, cp_, state_, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- int state_;
- char32_t cp_;
- #endif
- };
- /** An out iterator that converts UTF-8 to UTF-16. */
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<std::output_iterator<char16_t> Iter>
- #else
- template<typename Iter>
- #endif
- struct utf_32_to_16_out_iterator
- : detail::trans_ins_iter<utf_32_to_16_out_iterator<Iter>, Iter>
- {
- constexpr utf_32_to_16_out_iterator() {}
- explicit constexpr utf_32_to_16_out_iterator(Iter it) :
- detail::trans_ins_iter<utf_32_to_16_out_iterator<Iter>, Iter>(it)
- {}
- constexpr utf_32_to_16_out_iterator & operator=(char32_t cp)
- {
- auto & out = this->iter();
- out = detail::write_cp_utf16(cp, out);
- return *this;
- }
- };
- /** An insert-iterator analogous to std::insert_iterator, that also
- converts UTF-32 to UTF-16. */
- template<typename Cont>
- struct utf_32_to_16_insert_iterator
- : detail::trans_ins_iter<
- utf_32_to_16_insert_iterator<Cont>,
- std::insert_iterator<Cont>>
- {
- constexpr utf_32_to_16_insert_iterator() {}
- constexpr utf_32_to_16_insert_iterator(
- Cont & c, typename Cont::iterator it) :
- detail::trans_ins_iter<
- utf_32_to_16_insert_iterator<Cont>,
- std::insert_iterator<Cont>>(std::insert_iterator<Cont>(c, it))
- {}
- constexpr utf_32_to_16_insert_iterator & operator=(char32_t cp)
- {
- auto & out = this->iter();
- out = detail::write_cp_utf16(cp, out);
- return *this;
- }
- };
- /** An insert-iterator analogous to std::front_insert_iterator, that also
- converts UTF-32 to UTF-16. */
- template<typename Cont>
- struct utf_32_to_16_front_insert_iterator
- : detail::trans_ins_iter<
- utf_32_to_16_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>>
- {
- constexpr utf_32_to_16_front_insert_iterator() {}
- explicit constexpr utf_32_to_16_front_insert_iterator(Cont & c) :
- detail::trans_ins_iter<
- utf_32_to_16_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>>(
- std::front_insert_iterator<Cont>(c))
- {}
- constexpr utf_32_to_16_front_insert_iterator & operator=(char32_t cp)
- {
- auto & out = this->iter();
- out = detail::write_cp_utf16(cp, out);
- return *this;
- }
- };
- /** An insert-iterator analogous to std::back_insert_iterator, that also
- converts UTF-32 to UTF-16. */
- template<typename Cont>
- struct utf_32_to_16_back_insert_iterator
- : detail::trans_ins_iter<
- utf_32_to_16_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>>
- {
- constexpr utf_32_to_16_back_insert_iterator() {}
- explicit constexpr utf_32_to_16_back_insert_iterator(Cont & c) :
- detail::trans_ins_iter<
- utf_32_to_16_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>>(
- std::back_insert_iterator<Cont>(c))
- {}
- constexpr utf_32_to_16_back_insert_iterator & operator=(char32_t cp)
- {
- auto & out = this->iter();
- out = detail::write_cp_utf16(cp, out);
- return *this;
- }
- };
- namespace detail {
- template<typename OutIter>
- OutIter
- assign_16_to_32_insert(char16_t & prev_cu, char16_t cu, OutIter out)
- {
- if (high_surrogate(cu)) {
- if (prev_cu) {
- *out = replacement_character;
- ++out;
- }
- prev_cu = cu;
- } else if (low_surrogate(cu)) {
- if (prev_cu) {
- *out = detail::surrogates_to_cp(prev_cu, cu);
- ++out;
- } else {
- *out = replacement_character;
- ++out;
- }
- prev_cu = 0;
- } else {
- if (prev_cu) {
- *out = replacement_character;
- ++out;
- }
- *out = cu;
- ++out;
- prev_cu = 0;
- }
- return out;
- }
- }
- /** An out iterator that converts UTF-16 to UTF-32. */
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<std::output_iterator<char32_t> Iter>
- #else
- template<typename Iter>
- #endif
- struct utf_16_to_32_out_iterator
- : detail::trans_ins_iter<utf_16_to_32_out_iterator<Iter>, Iter>
- {
- constexpr utf_16_to_32_out_iterator() {}
- explicit constexpr utf_16_to_32_out_iterator(Iter it) :
- detail::trans_ins_iter<utf_16_to_32_out_iterator<Iter>, Iter>(it),
- prev_cu_(0)
- {}
- constexpr utf_16_to_32_out_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_16_to_32_insert(prev_cu_, cu, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- char16_t prev_cu_;
- #endif
- };
- /** An insert-iterator analogous to std::insert_iterator, that also
- converts UTF-16 to UTF-32. */
- template<typename Cont>
- struct utf_16_to_32_insert_iterator
- : detail::trans_ins_iter<
- utf_16_to_32_insert_iterator<Cont>,
- std::insert_iterator<Cont>>
- {
- constexpr utf_16_to_32_insert_iterator() {}
- constexpr utf_16_to_32_insert_iterator(
- Cont & c, typename Cont::iterator it) :
- detail::trans_ins_iter<
- utf_16_to_32_insert_iterator<Cont>,
- std::insert_iterator<Cont>>(std::insert_iterator<Cont>(c, it)),
- prev_cu_(0)
- {}
- constexpr utf_16_to_32_insert_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_16_to_32_insert(prev_cu_, cu, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- char16_t prev_cu_;
- #endif
- };
- /** An insert-iterator analogous to std::front_insert_iterator, that also
- converts UTF-16 to UTF-32. */
- template<typename Cont>
- struct utf_16_to_32_front_insert_iterator
- : detail::trans_ins_iter<
- utf_16_to_32_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>>
- {
- constexpr utf_16_to_32_front_insert_iterator() {}
- explicit constexpr utf_16_to_32_front_insert_iterator(Cont & c) :
- detail::trans_ins_iter<
- utf_16_to_32_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>>(
- std::front_insert_iterator<Cont>(c)),
- prev_cu_(0)
- {}
- constexpr utf_16_to_32_front_insert_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_16_to_32_insert(prev_cu_, cu, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- char16_t prev_cu_;
- #endif
- };
- /** An insert-iterator analogous to std::back_insert_iterator, that also
- converts UTF-16 to UTF-32. */
- template<typename Cont>
- struct utf_16_to_32_back_insert_iterator
- : detail::trans_ins_iter<
- utf_16_to_32_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>>
- {
- constexpr utf_16_to_32_back_insert_iterator() {}
- explicit constexpr utf_16_to_32_back_insert_iterator(Cont & c) :
- detail::trans_ins_iter<
- utf_16_to_32_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>>(
- std::back_insert_iterator<Cont>(c)),
- prev_cu_(0)
- {}
- constexpr utf_16_to_32_back_insert_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_16_to_32_insert(prev_cu_, cu, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- char16_t prev_cu_;
- #endif
- };
- namespace detail {
- template<typename OutIter>
- OutIter
- assign_16_to_8_insert(char16_t & prev_cu, char16_t cu, OutIter out)
- {
- if (high_surrogate(cu)) {
- if (prev_cu)
- out = detail::write_cp_utf8(replacement_character, out);
- prev_cu = cu;
- } else if (low_surrogate(cu)) {
- if (prev_cu) {
- auto const cp = detail::surrogates_to_cp(prev_cu, cu);
- out = detail::write_cp_utf8(cp, out);
- } else {
- out = detail::write_cp_utf8(replacement_character, out);
- }
- prev_cu = 0;
- } else {
- if (prev_cu)
- out = detail::write_cp_utf8(replacement_character, out);
- out = detail::write_cp_utf8(cu, out);
- prev_cu = 0;
- }
- return out;
- }
- }
- /** An out iterator that converts UTF-16 to UTF-8. */
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<std::output_iterator<char8_t> Iter>
- #else
- template<typename Iter>
- #endif
- struct utf_16_to_8_out_iterator
- : detail::trans_ins_iter<utf_16_to_8_out_iterator<Iter>, Iter>
- {
- constexpr utf_16_to_8_out_iterator() {}
- explicit constexpr utf_16_to_8_out_iterator(Iter it) :
- detail::trans_ins_iter<utf_16_to_8_out_iterator<Iter>, Iter>(it),
- prev_cu_(0)
- {}
- constexpr utf_16_to_8_out_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_16_to_8_insert(prev_cu_, cu, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- char16_t prev_cu_;
- #endif
- };
- /** An insert-iterator analogous to std::insert_iterator, that also
- converts UTF-16 to UTF-8. */
- template<typename Cont>
- struct utf_16_to_8_insert_iterator : detail::trans_ins_iter<
- utf_16_to_8_insert_iterator<Cont>,
- std::insert_iterator<Cont>>
- {
- constexpr utf_16_to_8_insert_iterator() {}
- constexpr utf_16_to_8_insert_iterator(
- Cont & c, typename Cont::iterator it) :
- detail::trans_ins_iter<
- utf_16_to_8_insert_iterator<Cont>,
- std::insert_iterator<Cont>>(std::insert_iterator<Cont>(c, it)),
- prev_cu_(0)
- {}
- constexpr utf_16_to_8_insert_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_16_to_8_insert(prev_cu_, cu, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- char16_t prev_cu_;
- #endif
- };
- /** An insert-iterator analogous to std::front_insert_iterator, that also
- converts UTF-16 to UTF-8. */
- template<typename Cont>
- struct utf_16_to_8_front_insert_iterator
- : detail::trans_ins_iter<
- utf_16_to_8_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>>
- {
- constexpr utf_16_to_8_front_insert_iterator() {}
- explicit constexpr utf_16_to_8_front_insert_iterator(Cont & c) :
- detail::trans_ins_iter<
- utf_16_to_8_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>>(
- std::front_insert_iterator<Cont>(c)),
- prev_cu_(0)
- {}
- constexpr utf_16_to_8_front_insert_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_16_to_8_insert(prev_cu_, cu, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- char16_t prev_cu_;
- #endif
- };
- /** An insert-iterator analogous to std::back_insert_iterator, that also
- converts UTF-16 to UTF-8. */
- template<typename Cont>
- struct utf_16_to_8_back_insert_iterator
- : detail::trans_ins_iter<
- utf_16_to_8_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>>
- {
- constexpr utf_16_to_8_back_insert_iterator() {}
- explicit constexpr utf_16_to_8_back_insert_iterator(Cont & c) :
- detail::trans_ins_iter<
- utf_16_to_8_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>>(
- std::back_insert_iterator<Cont>(c)),
- prev_cu_(0)
- {}
- constexpr utf_16_to_8_back_insert_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_16_to_8_insert(prev_cu_, cu, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- char16_t prev_cu_;
- #endif
- };
- namespace detail {
- template<typename OutIter>
- OutIter assign_8_to_16_insert(
- unsigned char cu, char32_t & cp, int & state, OutIter out)
- {
- auto write = [&] {
- out = detail::write_cp_utf16(cp, out);
- state = invalid_table_state;
- };
- auto start_cp = [&] {
- first_cu const info = first_cus[cu];
- state = info.next;
- cp = info.initial_octet;
- if (state == bgn)
- write();
- };
- if (state == invalid_table_state) {
- start_cp();
- } else {
- cp = (cp << 6) | (cu & 0x3f);
- char_class const class_ = octet_classes[cu];
- state = transitions[state + class_];
- if (state == bgn) {
- write();
- } else if (state == err) {
- out = detail::write_cp_utf16(replacement_character, out);
- start_cp();
- }
- }
- return out;
- }
- }
- /** An out iterator that converts UTF-8 to UTF-16. */
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<std::output_iterator<char16_t> Iter>
- #else
- template<typename Iter>
- #endif
- struct utf_8_to_16_out_iterator
- : detail::trans_ins_iter<utf_8_to_16_out_iterator<Iter>, Iter>
- {
- constexpr utf_8_to_16_out_iterator() {}
- explicit constexpr utf_8_to_16_out_iterator(Iter it) :
- detail::trans_ins_iter<utf_8_to_16_out_iterator<Iter>, Iter>(it),
- state_(detail::invalid_table_state)
- {}
- constexpr utf_8_to_16_out_iterator & operator=(char8_type cu)
- {
- auto & out = this->iter();
- out = detail::assign_8_to_16_insert(cu, cp_, state_, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- int state_;
- char32_t cp_;
- #endif
- };
- /** An insert-iterator analogous to std::insert_iterator, that also
- converts UTF-8 to UTF-16. */
- template<typename Cont>
- struct utf_8_to_16_insert_iterator : detail::trans_ins_iter<
- utf_8_to_16_insert_iterator<Cont>,
- std::insert_iterator<Cont>>
- {
- constexpr utf_8_to_16_insert_iterator() {}
- constexpr utf_8_to_16_insert_iterator(
- Cont & c, typename Cont::iterator it) :
- detail::trans_ins_iter<
- utf_8_to_16_insert_iterator<Cont>,
- std::insert_iterator<Cont>>(std::insert_iterator<Cont>(c, it)),
- state_(detail::invalid_table_state)
- {}
- constexpr utf_8_to_16_insert_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_8_to_16_insert(cu, cp_, state_, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- int state_;
- char32_t cp_;
- #endif
- };
- /** An insert-iterator analogous to std::front_insert_iterator, that also
- converts UTF-8 to UTF-16. */
- template<typename Cont>
- struct utf_8_to_16_front_insert_iterator
- : detail::trans_ins_iter<
- utf_8_to_16_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>>
- {
- constexpr utf_8_to_16_front_insert_iterator() {}
- explicit constexpr utf_8_to_16_front_insert_iterator(Cont & c) :
- detail::trans_ins_iter<
- utf_8_to_16_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>>(
- std::front_insert_iterator<Cont>(c)),
- state_(detail::invalid_table_state)
- {}
- constexpr utf_8_to_16_front_insert_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_8_to_16_insert(cu, cp_, state_, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- int state_;
- char32_t cp_;
- #endif
- };
- /** An insert-iterator analogous to std::back_insert_iterator, that also
- converts UTF-8 to UTF-16. */
- template<typename Cont>
- struct utf_8_to_16_back_insert_iterator
- : detail::trans_ins_iter<
- utf_8_to_16_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>>
- {
- constexpr utf_8_to_16_back_insert_iterator() {}
- explicit constexpr utf_8_to_16_back_insert_iterator(Cont & c) :
- detail::trans_ins_iter<
- utf_8_to_16_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>>(
- std::back_insert_iterator<Cont>(c)),
- state_(detail::invalid_table_state)
- {}
- constexpr utf_8_to_16_back_insert_iterator & operator=(char16_t cu)
- {
- auto & out = this->iter();
- out = detail::assign_8_to_16_insert(cu, cp_, state_, out);
- return *this;
- }
- #ifndef BOOST_TEXT_DOXYGEN
- private:
- int state_;
- char32_t cp_;
- #endif
- };
- }}
- #include <boost/parser/detail/text/unpack.hpp>
- namespace boost::parser::detail { namespace text { namespace detail {
- template<format Tag>
- struct make_utf8_dispatch;
- template<>
- struct make_utf8_dispatch<format::utf8>
- {
- template<typename Iter, typename Sentinel>
- static constexpr Iter call(Iter first, Iter it, Sentinel last)
- {
- return it;
- }
- };
- template<>
- struct make_utf8_dispatch<format::utf16>
- {
- template<typename Iter, typename Sentinel>
- static constexpr utf_iterator<
- format::utf16,
- format::utf8,
- Iter,
- Sentinel>
- call(Iter first, Iter it, Sentinel last)
- {
- return {first, it, last};
- }
- };
- template<>
- struct make_utf8_dispatch<format::utf32>
- {
- template<typename Iter, typename Sentinel>
- static constexpr utf_iterator<
- format::utf32,
- format::utf8,
- Iter,
- Sentinel>
- call(Iter first, Iter it, Sentinel last)
- {
- return {first, it, last};
- }
- };
- template<format Tag>
- struct make_utf16_dispatch;
- template<>
- struct make_utf16_dispatch<format::utf8>
- {
- template<typename Iter, typename Sentinel>
- static constexpr utf_iterator<
- format::utf8,
- format::utf16,
- Iter,
- Sentinel>
- call(Iter first, Iter it, Sentinel last)
- {
- return {first, it, last};
- }
- };
- template<>
- struct make_utf16_dispatch<format::utf16>
- {
- template<typename Iter, typename Sentinel>
- static constexpr Iter call(Iter first, Iter it, Sentinel last)
- {
- return it;
- }
- };
- template<>
- struct make_utf16_dispatch<format::utf32>
- {
- template<typename Iter, typename Sentinel>
- static constexpr utf_iterator<
- format::utf32,
- format::utf16,
- Iter,
- Sentinel>
- call(Iter first, Iter it, Sentinel last)
- {
- return {first, it, last};
- }
- };
- template<format Tag>
- struct make_utf32_dispatch;
- template<>
- struct make_utf32_dispatch<format::utf8>
- {
- template<typename Iter, typename Sentinel>
- static constexpr utf_iterator<
- format::utf8,
- format::utf32,
- Iter,
- Sentinel>
- call(Iter first, Iter it, Sentinel last)
- {
- return {first, it, last};
- }
- };
- template<>
- struct make_utf32_dispatch<format::utf16>
- {
- template<typename Iter, typename Sentinel>
- static constexpr utf_iterator<
- format::utf16,
- format::utf32,
- Iter,
- Sentinel>
- call(Iter first, Iter it, Sentinel last)
- {
- return {first, it, last};
- }
- };
- template<>
- struct make_utf32_dispatch<format::utf32>
- {
- template<typename Iter, typename Sentinel>
- static constexpr Iter call(Iter first, Iter it, Sentinel last)
- {
- return it;
- }
- };
- template<
- typename Cont,
- typename UTF8,
- typename UTF16,
- typename UTF32,
- int Bytes = sizeof(typename Cont::value_type)>
- struct from_utf8_dispatch
- {
- using type = UTF8;
- };
- template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
- struct from_utf8_dispatch<Cont, UTF8, UTF16, UTF32, 2>
- {
- using type = UTF16;
- };
- template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
- struct from_utf8_dispatch<Cont, UTF8, UTF16, UTF32, 4>
- {
- using type = UTF32;
- };
- template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
- using from_utf8_dispatch_t =
- typename from_utf8_dispatch<Cont, UTF8, UTF16, UTF32>::type;
- template<
- typename Cont,
- typename UTF8,
- typename UTF16,
- typename UTF32,
- int Bytes = sizeof(typename Cont::value_type)>
- struct from_utf16_dispatch
- {
- using type = UTF16;
- };
- template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
- struct from_utf16_dispatch<Cont, UTF8, UTF16, UTF32, 1>
- {
- using type = UTF8;
- };
- template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
- struct from_utf16_dispatch<Cont, UTF8, UTF16, UTF32, 4>
- {
- using type = UTF32;
- };
- template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
- using from_utf16_dispatch_t =
- typename from_utf16_dispatch<Cont, UTF8, UTF16, UTF32>::type;
- template<
- typename Cont,
- typename UTF8,
- typename UTF16,
- typename UTF32,
- int Bytes = sizeof(typename Cont::value_type)>
- struct from_utf32_dispatch
- {
- using type = UTF32;
- };
- template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
- struct from_utf32_dispatch<Cont, UTF8, UTF16, UTF32, 1>
- {
- using type = UTF8;
- };
- template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
- struct from_utf32_dispatch<Cont, UTF8, UTF16, UTF32, 2>
- {
- using type = UTF16;
- };
- template<typename Cont, typename UTF8, typename UTF16, typename UTF32>
- using from_utf32_dispatch_t =
- typename from_utf32_dispatch<Cont, UTF8, UTF16, UTF32>::type;
- }}}
- namespace boost::parser::detail { namespace text { BOOST_PARSER_DETAIL_TEXT_NAMESPACE_V1 {
- #if defined(BOOST_TEXT_DOXYGEN)
- /** Returns a `utf_32_to_8_out_iterator<O>` constructed from the given
- iterator. */
- template<std::output_iterator<char8_t> O>
- utf_32_to_8_out_iterator<O> utf_32_to_8_out(O it);
- /** Returns a `utf_8_to_32_out_iterator<O>` constructed from the given
- iterator. */
- template<std::output_iterator<char32_t> O>
- utf_8_to_32_out_iterator<O> utf_8_to_32_out(O it);
- /** Returns a `utf_32_to_16_out_iterator<O>` constructed from the given
- iterator. */
- template<std::output_iterator<char16_t> O>
- utf_32_to_16_out_iterator<O> utf_32_to_16_out(O it);
- /** Returns a `utf_16_to_32_out_iterator<O>` constructed from the given
- iterator. */
- template<std::output_iterator<char32_t> O>
- utf_16_to_32_out_iterator<O> utf_16_to_32_out(O it);
- /** Returns a `utf_16_to_8_out_iterator<O>` constructed from the given
- iterator. */
- template<std::output_iterator<char8_t> O>
- utf_16_to_8_out_iterator<O> utf_16_to_8_out(O it);
- /** Returns a `utf_8_to_16_out_iterator<O>` constructed from the given
- iterator. */
- template<std::output_iterator<char16_t> O>
- utf_8_to_16_out_iterator<O> utf_8_to_16_out(O it);
- /** Returns an iterator equivalent to `it` that transcodes `[first, last)`
- to UTF-8. */
- template<std::input_iterator I, std::sentinel_for<I> S>
- auto utf8_iterator(I first, I it, S last);
- /** Returns an iterator equivalent to `it` that transcodes `[first, last)`
- to UTF-16. */
- template<std::input_iterator I, std::sentinel_for<I> S>
- auto utf16_iterator(I first, I it, S last);
- /** Returns an iterator equivalent to `it` that transcodes `[first, last)`
- to UTF-32. */
- template<std::input_iterator I, std::sentinel_for<I> S>
- auto utf32_iterator(I first, I it, S last);
- /** Returns a inserting iterator that transcodes from UTF-8 to UTF-8,
- UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
- `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
- any other size implies UTF-32. */
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- std::is_integral_v<typename Cont::value_type>
- auto from_utf8_inserter(Cont & c, typename Cont::iterator it);
- /** Returns a inserting iterator that transcodes from UTF-16 to UTF-8,
- UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
- `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
- any other size implies UTF-32. */
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- std::is_integral_v<typename Cont::value_type>
- auto from_utf16_inserter(Cont & c, typename Cont::iterator it);
- /** Returns a inserting iterator that transcodes from UTF-32 to UTF-8,
- UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
- `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
- any other size implies UTF-32. */
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- std::is_integral_v<typename Cont::value_type>
- auto from_utf32_inserter(Cont & c, typename Cont::iterator it);
- /** Returns a back-inserting iterator that transcodes from UTF-8 to UTF-8,
- UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
- `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
- any other size implies UTF-32. */
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- std::is_integral_v<typename Cont::value_type>
- auto from_utf8_back_inserter(Cont & c);
- /** Returns a back-inserting iterator that transcodes from UTF-16 to UTF-8,
- UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
- `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
- any other size implies UTF-32. */
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- std::is_integral_v<typename Cont::value_type>
- auto from_utf16_back_inserter(Cont & c);
- /** Returns a back-inserting iterator that transcodes from UTF-32 to UTF-8,
- UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
- `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
- any other size implies UTF-32. */
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- std::is_integral_v<typename Cont::value_type>
- auto from_utf32_back_inserter(Cont & c);
- /** Returns a front-inserting iterator that transcodes from UTF-8 to UTF-8,
- UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
- `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
- any other size implies UTF-32. */
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- std::is_integral_v<typename Cont::value_type>
- auto from_utf8_front_inserter(Cont & c);
- /** Returns a front-inserting iterator that transcodes from UTF-16 to UTF-8,
- UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
- `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
- any other size implies UTF-32. */
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- std::is_integral_v<typename Cont::value_type>
- auto from_utf16_front_inserter(Cont & c);
- /** Returns a front-inserting iterator that transcodes from UTF-32 to UTF-8,
- UTF-16, or UTF-32. Which UTF the iterator transcodes to depends on
- `sizeof(Cont::value_type)`: `1` implies UTF-8; `2` implies UTF-16; and
- any other size implies UTF-32. */
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- std::is_integral_v<typename Cont::value_type>
- auto from_utf32_front_inserter(Cont & c);
- #endif
- template<typename Iter>
- utf_32_to_8_out_iterator<Iter> utf_32_to_8_out(Iter it)
- {
- return utf_32_to_8_out_iterator<Iter>(it);
- }
- template<typename Iter>
- utf_8_to_32_out_iterator<Iter> utf_8_to_32_out(Iter it)
- {
- return utf_8_to_32_out_iterator<Iter>(it);
- }
- template<typename Iter>
- utf_32_to_16_out_iterator<Iter> utf_32_to_16_out(Iter it)
- {
- return utf_32_to_16_out_iterator<Iter>(it);
- }
- template<typename Iter>
- utf_16_to_32_out_iterator<Iter> utf_16_to_32_out(Iter it)
- {
- return utf_16_to_32_out_iterator<Iter>(it);
- }
- template<typename Iter>
- utf_16_to_8_out_iterator<Iter> utf_16_to_8_out(Iter it)
- {
- return utf_16_to_8_out_iterator<Iter>(it);
- }
- template<typename Iter>
- utf_8_to_16_out_iterator<Iter> utf_8_to_16_out(Iter it)
- {
- return utf_8_to_16_out_iterator<Iter>(it);
- }
- template<typename Iter, typename Sentinel>
- auto utf8_iterator(Iter first, Iter it, Sentinel last)
- {
- auto const unpacked = text::unpack_iterator_and_sentinel(first, last);
- auto const unpacked_it =
- text::unpack_iterator_and_sentinel(it, last).first;
- constexpr format tag = unpacked.format_tag;
- return detail::make_utf8_dispatch<tag>::call(
- unpacked.first, unpacked_it, unpacked.last);
- }
- template<typename Iter, typename Sentinel>
- auto utf16_iterator(Iter first, Iter it, Sentinel last)
- {
- auto const unpacked = text::unpack_iterator_and_sentinel(first, last);
- auto const unpacked_it =
- text::unpack_iterator_and_sentinel(it, last).first;
- constexpr format tag = unpacked.format_tag;
- return detail::make_utf16_dispatch<tag>::call(
- unpacked.first, unpacked_it, unpacked.last);
- }
- template<typename Iter, typename Sentinel>
- auto utf32_iterator(Iter first, Iter it, Sentinel last)
- {
- auto const unpacked = text::unpack_iterator_and_sentinel(first, last);
- auto const unpacked_it =
- text::unpack_iterator_and_sentinel(it, last).first;
- constexpr format tag = unpacked.format_tag;
- return detail::make_utf32_dispatch<tag>::call(
- unpacked.first, unpacked_it, unpacked.last);
- }
- template<typename Cont>
- auto from_utf8_inserter(Cont & c, typename Cont::iterator it)
- {
- using result_type = detail::from_utf8_dispatch_t<
- Cont,
- std::insert_iterator<Cont>,
- utf_8_to_16_insert_iterator<Cont>,
- utf_8_to_32_insert_iterator<Cont>>;
- return result_type(c, it);
- }
- template<typename Cont>
- auto from_utf16_inserter(Cont & c, typename Cont::iterator it)
- {
- using result_type = detail::from_utf16_dispatch_t<
- Cont,
- utf_16_to_8_insert_iterator<Cont>,
- std::insert_iterator<Cont>,
- utf_16_to_32_insert_iterator<Cont>>;
- return result_type(c, it);
- }
- template<typename Cont>
- auto from_utf32_inserter(Cont & c, typename Cont::iterator it)
- {
- using result_type = detail::from_utf32_dispatch_t<
- Cont,
- utf_32_to_8_insert_iterator<Cont>,
- utf_32_to_16_insert_iterator<Cont>,
- std::insert_iterator<Cont>>;
- return result_type(c, it);
- }
- template<typename Cont>
- auto from_utf8_back_inserter(Cont & c)
- {
- using result_type = detail::from_utf8_dispatch_t<
- Cont,
- std::back_insert_iterator<Cont>,
- utf_8_to_16_back_insert_iterator<Cont>,
- utf_8_to_32_back_insert_iterator<Cont>>;
- return result_type(c);
- }
- template<typename Cont>
- auto from_utf16_back_inserter(Cont & c)
- {
- using result_type = detail::from_utf16_dispatch_t<
- Cont,
- utf_16_to_8_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>,
- utf_16_to_32_back_insert_iterator<Cont>>;
- return result_type(c);
- }
- template<typename Cont>
- auto from_utf32_back_inserter(Cont & c)
- {
- using result_type = detail::from_utf32_dispatch_t<
- Cont,
- utf_32_to_8_back_insert_iterator<Cont>,
- utf_32_to_16_back_insert_iterator<Cont>,
- std::back_insert_iterator<Cont>>;
- return result_type(c);
- }
- template<typename Cont>
- auto from_utf8_front_inserter(Cont & c)
- {
- using result_type = detail::from_utf8_dispatch_t<
- Cont,
- std::front_insert_iterator<Cont>,
- utf_8_to_16_front_insert_iterator<Cont>,
- utf_8_to_32_front_insert_iterator<Cont>>;
- return result_type(c);
- }
- template<typename Cont>
- auto from_utf16_front_inserter(Cont & c)
- {
- using result_type = detail::from_utf16_dispatch_t<
- Cont,
- utf_16_to_8_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>,
- utf_16_to_32_front_insert_iterator<Cont>>;
- return result_type(c);
- }
- template<typename Cont>
- auto from_utf32_front_inserter(Cont & c)
- {
- using result_type = detail::from_utf32_dispatch_t<
- Cont,
- utf_32_to_8_front_insert_iterator<Cont>,
- utf_32_to_16_front_insert_iterator<Cont>,
- std::front_insert_iterator<Cont>>;
- return result_type(c);
- }
- }}}
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- namespace boost::parser::detail { namespace text { BOOST_PARSER_DETAIL_TEXT_NAMESPACE_V2 {
- template<std::output_iterator<char8_t> O>
- constexpr utf_32_to_8_out_iterator<O> utf_32_to_8_out(O it)
- {
- return utf_32_to_8_out_iterator<O>(it);
- }
- template<std::output_iterator<char32_t> O>
- constexpr utf_8_to_32_out_iterator<O> utf_8_to_32_out(O it)
- {
- return utf_8_to_32_out_iterator<O>(it);
- }
- template<std::output_iterator<char16_t> O>
- constexpr utf_32_to_16_out_iterator<O> utf_32_to_16_out(O it)
- {
- return utf_32_to_16_out_iterator<O>(it);
- }
- template<std::output_iterator<char32_t> O>
- constexpr utf_16_to_32_out_iterator<O> utf_16_to_32_out(O it)
- {
- return utf_16_to_32_out_iterator<O>(it);
- }
- template<std::output_iterator<char8_t> O>
- constexpr utf_16_to_8_out_iterator<O> utf_16_to_8_out(O it)
- {
- return utf_16_to_8_out_iterator<O>(it);
- }
- template<std::output_iterator<char16_t> O>
- constexpr utf_8_to_16_out_iterator<O> utf_8_to_16_out(O it)
- {
- return utf_8_to_16_out_iterator<O>(it);
- }
- template<std::input_iterator I, std::sentinel_for<I> S>
- constexpr auto utf8_iterator(I first, I it, S last)
- {
- return v1::utf8_iterator(first, it, last);
- }
- template<std::input_iterator I, std::sentinel_for<I> S>
- constexpr auto utf16_iterator(I first, I it, S last)
- {
- return v1::utf16_iterator(first, it, last);
- }
- template<std::input_iterator I, std::sentinel_for<I> S>
- constexpr auto utf32_iterator(I first, I it, S last)
- {
- return v1::utf32_iterator(first, it, last);
- }
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- utf_code_unit<typename Cont::value_type>
- constexpr auto from_utf8_inserter(Cont & c, typename Cont::iterator it)
- {
- if constexpr (sizeof(typename Cont::value_type) == 1) {
- return std::insert_iterator<Cont>(c, it);
- } else if constexpr (sizeof(typename Cont::value_type) == 2) {
- return utf_8_to_16_insert_iterator<Cont>(c, it);
- } else {
- return utf_8_to_32_insert_iterator<Cont>(c, it);
- }
- }
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- utf_code_unit<typename Cont::value_type>
- constexpr auto from_utf16_inserter(Cont & c, typename Cont::iterator it)
- {
- if constexpr (sizeof(typename Cont::value_type) == 1) {
- return utf_16_to_8_insert_iterator<Cont>(c, it);
- } else if constexpr (sizeof(typename Cont::value_type) == 2) {
- return std::insert_iterator<Cont>(c, it);
- } else {
- return utf_16_to_32_insert_iterator<Cont>(c, it);
- }
- }
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- utf_code_unit<typename Cont::value_type>
- constexpr auto from_utf32_inserter(Cont & c, typename Cont::iterator it)
- {
- if constexpr (sizeof(typename Cont::value_type) == 1) {
- return utf_32_to_8_insert_iterator<Cont>(c, it);
- } else if constexpr (sizeof(typename Cont::value_type) == 2) {
- return utf_32_to_16_insert_iterator<Cont>(c, it);
- } else {
- return std::insert_iterator<Cont>(c, it);
- }
- }
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- utf_code_unit<typename Cont::value_type>
- constexpr auto from_utf8_back_inserter(Cont & c)
- {
- if constexpr (sizeof(typename Cont::value_type) == 1) {
- return std::back_insert_iterator<Cont>(c);
- } else if constexpr (sizeof(typename Cont::value_type) == 2) {
- return utf_8_to_16_back_insert_iterator<Cont>(c);
- } else {
- return utf_8_to_32_back_insert_iterator<Cont>(c);
- }
- }
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- utf_code_unit<typename Cont::value_type>
- constexpr auto from_utf16_back_inserter(Cont & c)
- {
- if constexpr (sizeof(typename Cont::value_type) == 1) {
- return utf_16_to_8_back_insert_iterator<Cont>(c);
- } else if constexpr (sizeof(typename Cont::value_type) == 2) {
- return std::back_insert_iterator<Cont>(c);
- } else {
- return utf_16_to_32_back_insert_iterator<Cont>(c);
- }
- }
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- utf_code_unit<typename Cont::value_type>
- constexpr auto from_utf32_back_inserter(Cont & c)
- {
- if constexpr (sizeof(typename Cont::value_type) == 1) {
- return utf_32_to_8_back_insert_iterator<Cont>(c);
- } else if constexpr (sizeof(typename Cont::value_type) == 2) {
- return utf_32_to_16_back_insert_iterator<Cont>(c);
- } else {
- return std::back_insert_iterator<Cont>(c);
- }
- }
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- utf_code_unit<typename Cont::value_type>
- constexpr auto from_utf8_front_inserter(Cont & c)
- {
- if constexpr (sizeof(typename Cont::value_type) == 1) {
- return std::front_insert_iterator<Cont>(c);
- } else if constexpr (sizeof(typename Cont::value_type) == 2) {
- return utf_8_to_16_front_insert_iterator<Cont>(c);
- } else {
- return utf_8_to_32_front_insert_iterator<Cont>(c);
- }
- }
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- utf_code_unit<typename Cont::value_type>
- constexpr auto from_utf16_front_inserter(Cont & c)
- {
- if constexpr (sizeof(typename Cont::value_type) == 1) {
- return utf_16_to_8_front_insert_iterator<Cont>(c);
- } else if constexpr (sizeof(typename Cont::value_type) == 2) {
- return std::front_insert_iterator<Cont>(c);
- } else {
- return utf_16_to_32_front_insert_iterator<Cont>(c);
- }
- }
- template<typename Cont>
- requires requires { typename Cont::value_type; } &&
- utf_code_unit<typename Cont::value_type>
- constexpr auto from_utf32_front_inserter(Cont & c)
- {
- if constexpr (sizeof(typename Cont::value_type) == 1) {
- return utf_32_to_8_front_insert_iterator<Cont>(c);
- } else if constexpr (sizeof(typename Cont::value_type) == 2) {
- return utf_32_to_16_front_insert_iterator<Cont>(c);
- } else {
- return std::front_insert_iterator<Cont>(c);
- }
- }
- }}}
- #endif
- namespace boost::parser::detail { namespace text {
- namespace detail {
- template<format Format>
- constexpr auto format_to_type()
- {
- if constexpr (Format == format::utf8) {
- return char8_type{};
- } else if constexpr (Format == format::utf16) {
- return char16_t{};
- } else {
- return char32_t{};
- }
- }
- template<typename I>
- constexpr bool is_bidi =
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- std::bidirectional_iterator<I>
- #else
- std::is_base_of_v<
- std::bidirectional_iterator_tag,
- typename std::iterator_traits<I>::iterator_category>
- #endif
- ;
- template<typename I, bool SupportReverse = is_bidi<I>>
- struct first_and_curr
- {
- first_and_curr() = default;
- first_and_curr(I curr) : curr{curr} {}
- first_and_curr(const first_and_curr & other) = default;
- template<
- class I2
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- ,
- typename Enable = std::enable_if_t<std::is_convertible_v<I2, I>>
- #endif
- >
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires std::convertible_to<I2, I>
- #endif
- first_and_curr(const first_and_curr<I2> & other) : curr{other.curr}
- {}
- I curr;
- };
- template<typename I>
- struct first_and_curr<I, true>
- {
- first_and_curr() = default;
- first_and_curr(I first, I curr) : first{first}, curr{curr} {}
- first_and_curr(const first_and_curr & other) = default;
- template<
- class I2
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- ,
- typename Enable = std::enable_if_t<std::is_convertible_v<I2, I>>
- #endif
- >
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires std::convertible_to<I2, I>
- #endif
- first_and_curr(const first_and_curr<I2> & other) :
- first{other.first}, curr{other.curr}
- {}
- I first;
- I curr;
- };
- }
- namespace detail {
- struct iter_access
- {
- template<typename T>
- static auto & buf(T & it)
- {
- return it.buf_;
- }
- template<typename T>
- static auto & first_and_curr(T & it)
- {
- return it.first_and_curr_;
- }
- template<typename T>
- static auto & buf_index(T & it)
- {
- return it.buf_index_;
- }
- template<typename T>
- static auto & buf_last(T & it)
- {
- return it.buf_last_;
- }
- template<typename T>
- static auto & to_increment(T & it)
- {
- return it.to_increment_;
- }
- template<typename T>
- static auto & last(T & it)
- {
- return it.last_;
- }
- };
- }
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<
- format FromFormat,
- format ToFormat,
- std::input_iterator I,
- std::sentinel_for<I> S,
- transcoding_error_handler ErrorHandler>
- requires std::convertible_to<std::iter_value_t<I>, detail::format_to_type_t<FromFormat>>
- #else
- template<
- format FromFormat,
- format ToFormat,
- typename I,
- typename S,
- typename ErrorHandler>
- #endif
- class utf_iterator
- : public stl_interfaces::iterator_interface<
- utf_iterator<FromFormat, ToFormat, I, S, ErrorHandler>,
- detail::bidirectional_at_most_t<I>,
- detail::format_to_type_t<ToFormat>,
- detail::format_to_type_t<ToFormat>>
- {
- static_assert(
- FromFormat == format::utf8 || FromFormat == format::utf16 ||
- FromFormat == format::utf32);
- static_assert(
- ToFormat == format::utf8 || ToFormat == format::utf16 ||
- ToFormat == format::utf32);
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<typename T>
- constexpr static bool is_bidirectional = std::is_base_of_v<
- std::bidirectional_iterator_tag,
- detail::bidirectional_at_most_t<T>>;
- template<typename T>
- constexpr static bool is_forward = std::is_base_of_v<
- std::forward_iterator_tag,
- detail::bidirectional_at_most_t<T>>;
- template<typename T>
- constexpr static bool is_input = !is_bidirectional<T> && !is_forward<T>;
- #endif
- static_assert(
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- std::forward_iterator<I>
- #else
- is_forward<I>
- #endif
- || noexcept(ErrorHandler{}("")));
- public:
- using value_type = detail::format_to_type_t<ToFormat>;
- constexpr utf_iterator() = default;
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<
- typename J = I,
- typename Enable = std::enable_if_t<is_bidirectional<J>>>
- #endif
- constexpr utf_iterator(I first, I it, S last)
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires std::bidirectional_iterator<I>
- #endif
- : first_and_curr_{first, it}, last_(last)
- {
- if (curr() != last_)
- read();
- }
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<
- typename J = I,
- typename Enable = std::enable_if_t<!is_bidirectional<J>>>
- #endif
- constexpr utf_iterator(I it, S last)
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires(!std::bidirectional_iterator<I>)
- #endif
- :
- first_and_curr_{it}, last_(last)
- {
- if (curr() != last_)
- read();
- }
- template<
- class I2,
- class S2
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- ,
- typename Enable = std::enable_if_t<
- std::is_convertible_v<I2, I> && std::is_convertible_v<S2, S>>
- #endif
- >
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires std::convertible_to<I2, I> && std::convertible_to<S2, S>
- #endif
- constexpr utf_iterator(
- utf_iterator<FromFormat, ToFormat, I2, S2, ErrorHandler> const &
- other) :
- buf_(detail::iter_access::buf(other)),
- first_and_curr_(detail::iter_access::first_and_curr(other)),
- buf_index_(detail::iter_access::buf_index(other)),
- buf_last_(detail::iter_access::buf_last(other)),
- to_increment_(detail::iter_access::to_increment(other)),
- last_(detail::iter_access::last(other))
- {}
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<
- typename J = I,
- typename Enable = std::enable_if_t<is_bidirectional<J>>>
- #endif
- constexpr I begin() const
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires std::bidirectional_iterator<I>
- #endif
- {
- return first();
- }
- constexpr S end() const { return last_; }
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<
- typename J = I,
- typename Enable = std::enable_if_t<is_forward<J>>>
- #endif
- constexpr I base() const
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires std::forward_iterator<I>
- #endif
- {
- return curr();
- }
- constexpr value_type operator*() const
- {
- BOOST_PARSER_DEBUG_ASSERT(buf_index_ < buf_last_);
- return buf_[buf_index_];
- }
- constexpr utf_iterator & operator++()
- {
- BOOST_PARSER_DEBUG_ASSERT(buf_index_ != buf_last_ || curr() != last_);
- if (buf_index_ + 1 == buf_last_ && curr() != last_) {
- if constexpr (
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- std::forward_iterator<I>
- #else
- is_forward<I>
- #endif
- ) {
- std::advance(curr(), to_increment_);
- }
- if (curr() == last_)
- buf_index_ = 0;
- else
- read();
- } else if (buf_index_ + 1 <= buf_last_) {
- ++buf_index_;
- }
- return *this;
- }
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<
- typename J = I,
- typename Enable = std::enable_if_t<is_bidirectional<J>>>
- #endif
- constexpr utf_iterator & operator--()
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires std::bidirectional_iterator<I>
- #endif
- {
- BOOST_PARSER_DEBUG_ASSERT(buf_index_ || curr() != first());
- if (!buf_index_ && curr() != first())
- read_reverse();
- else if (buf_index_)
- --buf_index_;
- return *this;
- }
- friend constexpr bool operator==(
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- utf_iterator
- #else
- std::enable_if_t<is_forward<I>, utf_iterator>
- #endif
- lhs, utf_iterator rhs)
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires std::forward_iterator<I> || requires(I i) { i == i; }
- #endif
- {
- if constexpr (
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- std::forward_iterator<I>
- #else
- is_forward<I>
- #endif
- ) {
- return lhs.curr() == rhs.curr() && lhs.buf_index_ == rhs.buf_index_;
- } else {
- if (lhs.curr() != rhs.curr())
- return false;
- if (lhs.buf_index_ == rhs.buf_index_ &&
- lhs.buf_last_ == rhs.buf_last_) {
- return true;
- }
- return lhs.buf_index_ == lhs.buf_last_ &&
- rhs.buf_index_ == rhs.buf_last_;
- }
- }
- #if !defined(__cpp_impl_three_way_comparison)
- friend BOOST_PARSER_CONSTEXPR bool operator!=(
- std::enable_if_t<is_forward<I>, utf_iterator> lhs, utf_iterator rhs)
- { return !(lhs == rhs); }
- #endif
- friend constexpr bool operator==(utf_iterator lhs, S rhs)
- {
- if constexpr (
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- std::forward_iterator<I>
- #else
- is_forward<I>
- #endif
- ) {
- return lhs.curr() == rhs;
- } else {
- return lhs.curr() == rhs && lhs.buf_index_ == lhs.buf_last_;
- }
- }
- #if !defined(__cpp_impl_three_way_comparison)
- friend BOOST_PARSER_CONSTEXPR bool operator!=(utf_iterator lhs, S rhs)
- { return !(lhs == rhs); }
- #endif
- // exposition only
- using base_type = stl_interfaces::iterator_interface<
- utf_iterator<FromFormat, ToFormat, I, S, ErrorHandler>,
- detail::bidirectional_at_most_t<I>,
- value_type,
- value_type>;
- using base_type::operator++;
- using base_type::operator--;
- private:
- constexpr char32_t decode_code_point()
- {
- if constexpr (FromFormat == format::utf8) {
- char32_t cp = *curr();
- ++curr();
- to_increment_ = 1;
- if (cp < 0x80)
- return cp;
- // clang-format off
- // It turns out that this naive implementation is faster than
- // the table implementation for the converting iterators.
- /*
- Unicode 3.9/D92
- Table 3-7. Well-Formed UTF-8 Byte Sequences
- Code Points First Byte Second Byte Third Byte Fourth Byte
- =========== ========== =========== ========== ===========
- U+0000..U+007F 00..7F
- U+0080..U+07FF C2..DF 80..BF
- U+0800..U+0FFF E0 A0..BF 80..BF
- U+1000..U+CFFF E1..EC 80..BF 80..BF
- U+D000..U+D7FF ED 80..9F 80..BF
- U+E000..U+FFFF EE..EF 80..BF 80..BF
- U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
- U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
- U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
- */
- // clang-format on
- unsigned char curr_c = (unsigned char)cp;
- auto error = [&]() {
- return ErrorHandler{}("Ill-formed UTF-8.");
- };
- auto next = [&]() {
- ++curr();
- ++to_increment_;
- };
- // One-byte case handled above
- // Two-byte
- if (detail::in(0xc2, curr_c, 0xdf)) {
- cp = curr_c & 0b00011111;
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- // Three-byte
- } else if (curr_c == 0xe0) {
- cp = curr_c & 0b00001111;
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0xa0, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- } else if (detail::in(0xe1, curr_c, 0xec)) {
- cp = curr_c & 0b00001111;
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- } else if (curr_c == 0xed) {
- cp = curr_c & 0b00001111;
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0x9f))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- } else if (detail::in(0xee, curr_c, 0xef)) {
- cp = curr_c & 0b00001111;
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- // Four-byte
- } else if (curr_c == 0xf0) {
- cp = curr_c & 0b00000111;
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x90, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- } else if (detail::in(0xf1, curr_c, 0xf3)) {
- cp = curr_c & 0b00000111;
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- } else if (curr_c == 0xf4) {
- cp = curr_c & 0b00000111;
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0x8f))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- if (curr() == last_)
- return error();
- curr_c = *curr();
- if (!detail::in(0x80, curr_c, 0xbf))
- return error();
- cp = (cp << 6) + (curr_c & 0b00111111);
- next();
- } else {
- return error();
- }
- return cp;
- } else if constexpr (FromFormat == format::utf16) {
- char16_t hi = *curr();
- ++curr();
- to_increment_ = 1;
- if (!boost::parser::detail::text::surrogate(hi))
- return hi;
- if (boost::parser::detail::text::low_surrogate(hi)) {
- return ErrorHandler{}(
- "Invalid UTF-16 sequence; lone trailing surrogate.");
- }
- // high surrogate
- if (curr() == last_) {
- return ErrorHandler{}(
- "Invalid UTF-16 sequence; lone leading surrogate.");
- }
- char16_t lo = *curr();
- ++curr();
- ++to_increment_;
- if (!boost::parser::detail::text::low_surrogate(lo)) {
- return ErrorHandler{}(
- "Invalid UTF-16 sequence; lone leading surrogate.");
- }
- return char32_t((hi - high_surrogate_base) << 10) +
- (lo - low_surrogate_base);
- } else {
- char32_t retval = *curr();
- ++curr();
- to_increment_ = 1;
- return retval;
- }
- }
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<
- typename J = I,
- typename Enable = std::enable_if_t<is_bidirectional<J>>>
- #endif
- constexpr char32_t decode_code_point_reverse()
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires std::bidirectional_iterator<I>
- #endif
- {
- if constexpr (FromFormat == format::utf8) {
- curr() = detail::decrement(first(), curr());
- auto initial = curr();
- char32_t cp = decode_code_point();
- curr() = initial;
- return cp;
- } else if constexpr (FromFormat == format::utf16) {
- char16_t lo = *--curr();
- if (!boost::parser::detail::text::surrogate(lo))
- return lo;
- if (boost::parser::detail::text::high_surrogate(lo)) {
- return ErrorHandler{}(
- "Invalid UTF-16 sequence; lone leading surrogate.");
- }
- // low surrogate
- if (curr() == first()) {
- return ErrorHandler{}(
- "Invalid UTF-16 sequence; lone trailing surrogate.");
- }
- char16_t hi = *detail::prev(curr());
- if (!boost::parser::detail::text::high_surrogate(hi)) {
- return ErrorHandler{}(
- "Invalid UTF-16 sequence; lone trailing surrogate.");
- }
- --curr();
- return char32_t((hi - high_surrogate_base) << 10) +
- (lo - low_surrogate_base);
- } else {
- return *--curr();
- }
- }
- template<class Out>
- static constexpr Out encode_code_point(char32_t cp, Out out)
- {
- if constexpr (ToFormat == format::utf8) {
- if (cp < 0x80) {
- *out++ = static_cast<char8_type>(cp);
- } else if (cp < 0x800) {
- *out++ = static_cast<char8_type>(0xC0 + (cp >> 6));
- *out++ = static_cast<char8_type>(0x80 + (cp & 0x3f));
- } else if (cp < 0x10000) {
- *out++ = static_cast<char8_type>(0xe0 + (cp >> 12));
- *out++ = static_cast<char8_type>(0x80 + ((cp >> 6) & 0x3f));
- *out++ = static_cast<char8_type>(0x80 + (cp & 0x3f));
- } else {
- *out++ = static_cast<char8_type>(0xf0 + (cp >> 18));
- *out++ = static_cast<char8_type>(0x80 + ((cp >> 12) & 0x3f));
- *out++ = static_cast<char8_type>(0x80 + ((cp >> 6) & 0x3f));
- *out++ = static_cast<char8_type>(0x80 + (cp & 0x3f));
- }
- } else if constexpr (ToFormat == format::utf16) {
- if (cp < 0x10000) {
- *out++ = static_cast<char16_t>(cp);
- } else {
- *out++ =
- static_cast<char16_t>(cp >> 10) + high_surrogate_base;
- *out++ =
- static_cast<char16_t>(cp & 0x3ff) + low_surrogate_base;
- }
- } else {
- *out++ = cp;
- }
- return out;
- }
- constexpr void read()
- {
- I initial;
- if constexpr (
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- std::forward_iterator<I>
- #else
- is_forward<I>
- #endif
- ) {
- initial = curr();
- }
- if constexpr (noexcept(ErrorHandler{}(""))) {
- char32_t cp = decode_code_point();
- auto it = encode_code_point(cp, buf_.begin());
- buf_index_ = 0;
- buf_last_ = uint8_t(it - buf_.begin());
- } else {
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- auto buf = buf_;
- try {
- #endif
- char32_t cp = decode_code_point();
- auto it = encode_code_point(cp, buf_.begin());
- buf_index_ = 0;
- buf_last_ = it - buf_.begin();
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- } catch (...) {
- buf_ = buf;
- curr() = initial;
- throw;
- }
- #endif
- }
- if constexpr (
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- std::forward_iterator<I>
- #else
- is_forward<I>
- #endif
- ) {
- curr() = initial;
- }
- }
- constexpr void read_reverse()
- {
- auto initial = curr();
- if constexpr (noexcept(ErrorHandler{}(""))) {
- char32_t cp = decode_code_point_reverse();
- auto it = encode_code_point(cp, buf_.begin());
- buf_last_ = uint8_t(it - buf_.begin());
- buf_index_ = buf_last_ - 1;
- to_increment_ = (int)std::distance(curr(), initial);
- } else {
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- auto buf = buf_;
- try {
- #endif
- char32_t cp = decode_code_point_reverse();
- auto it = encode_code_point(cp, buf_.begin());
- buf_last_ = it - buf_.begin();
- buf_index_ = buf_last_ - 1;
- to_increment_ = std::distance(curr(), initial);
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- } catch (...) {
- buf_ = buf;
- curr() = initial;
- throw;
- }
- #endif
- }
- }
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<
- typename J = I,
- typename Enable = std::enable_if_t<is_bidirectional<J>>>
- #endif
- constexpr I first() const
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires std::bidirectional_iterator<I>
- #endif
- {
- return first_and_curr_.first;
- }
- constexpr I & curr() { return first_and_curr_.curr; }
- constexpr I curr() const { return first_and_curr_.curr; }
- std::array<value_type, 4 / static_cast<int>(ToFormat)> buf_ = {};
- detail::first_and_curr<I> first_and_curr_ = {};
- uint8_t buf_index_ = 0;
- uint8_t buf_last_ = 0;
- uint8_t to_increment_ = 0;
- [[no_unique_address]] S last_ = {};
- friend struct detail::iter_access;
- };
- }}
- namespace boost::parser::detail { namespace text { namespace detail {
- template<class T>
- constexpr bool is_utf_iter = false;
- template<
- format FromFormat,
- format ToFormat,
- class I,
- class S,
- class ErrorHandler>
- constexpr bool
- is_utf_iter<utf_iterator<FromFormat, ToFormat, I, S, ErrorHandler>> =
- true;
- // These are here because so many downstream views that use
- // utf_iterator use them.
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<typename V>
- constexpr bool common_range_v = std::ranges::common_range<V>;
- template<typename V>
- constexpr bool forward_range_v = std::ranges::forward_range<V>;
- template<typename V>
- constexpr bool bidirectional_range_v = std::ranges::bidirectional_range<V>;
- template<typename T>
- constexpr bool default_initializable_v = std::default_initializable<T>;
- template<typename V>
- constexpr bool utf32_range_v = utf32_range<V>;
- #else
- template<typename T>
- using range_expr =
- decltype(detail::begin(std::declval<T &>()) == detail::end(std::declval<T &>()));
- template<typename T>
- constexpr bool is_range_v = is_detected_v<range_expr, T>;
- template<typename V>
- constexpr bool common_range_v =
- is_range_v<V> && std::is_same_v<iterator_t<V>, sentinel_t<V>>;
- template<typename V>
- constexpr bool input_range_v = is_range_v<V> && std::is_base_of_v<
- std::input_iterator_tag,
- typename std::iterator_traits<iterator_t<V>>::iterator_category>;
- template<typename V>
- constexpr bool forward_range_v = is_range_v<V> && std::is_base_of_v<
- std::forward_iterator_tag,
- typename std::iterator_traits<iterator_t<V>>::iterator_category>;
- template<typename V>
- constexpr bool bidirectional_range_v = is_range_v<V> && std::is_base_of_v<
- std::bidirectional_iterator_tag,
- typename std::iterator_traits<iterator_t<V>>::iterator_category>;
- template<typename T>
- constexpr bool default_initializable_v = std::is_default_constructible_v<T>;
- template<typename V>
- constexpr bool utf_range_v = is_range_v<V> && code_unit_v<range_value_t<V>>;
- template<typename V>
- constexpr bool
- utf32_range_v = is_range_v<V> &&
- (
- #if !defined(_MSC_VER)
- std::is_same_v<range_value_t<V>, wchar_t> ||
- #endif
- std::is_same_v<range_value_t<V>, char32_t>);
- #endif
- template<typename I>
- constexpr bool random_access_iterator_v = std::is_base_of_v<
- std::random_access_iterator_tag,
- typename std::iterator_traits<I>::iterator_category>;
- template<typename I>
- constexpr bool bidirectional_iterator_v = std::is_base_of_v<
- std::bidirectional_iterator_tag,
- typename std::iterator_traits<I>::iterator_category>;
- template<typename I>
- constexpr bool forward_iterator_v = std::is_base_of_v<
- std::forward_iterator_tag,
- typename std::iterator_traits<I>::iterator_category>;
- template<
- class V,
- bool StoreFirst = !is_utf_iter<iterator_t<V>> && common_range_v<V> &&
- bidirectional_range_v<V>,
- bool StoreLast = !is_utf_iter<iterator_t<V>>>
- struct first_last_storage
- {
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<
- typename Enable = std::enable_if_t<
- default_initializable_v<iterator_t<V>> &&
- default_initializable_v<sentinel_t<V>>>>
- #endif
- constexpr first_last_storage()
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires default_initializable_v<iterator_t<V>> &&
- default_initializable_v<sentinel_t<V>>
- #endif
- {}
- constexpr first_last_storage(V & base) :
- first_{detail::begin(base)}, last_{detail::end(base)}
- {}
- constexpr auto begin(iterator_t<V> & it) const { return first_; }
- constexpr auto end(iterator_t<V> & it) const { return last_; }
- iterator_t<V> first_;
- sentinel_t<V> last_;
- };
- template<typename I>
- using trinary_iter_ctor = decltype(I(
- std::declval<I>().begin(),
- std::declval<I>().end(),
- std::declval<I>().end()));
- template<class V>
- struct first_last_storage<V, true, false>
- {
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<
- typename Enable =
- std::enable_if_t<default_initializable_v<iterator_t<V>>>>
- #endif
- constexpr first_last_storage()
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires default_initializable_v<iterator_t<V>>
- #endif
- {}
- constexpr first_last_storage(V & base) : first_{detail::begin(base)} {}
- constexpr auto begin(iterator_t<V> & it) const { return first_; }
- constexpr auto end(iterator_t<V> & it) const {
- if constexpr (
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires { iterator_t<V>(it.begin(), it.end(), it.end()); }
- #else
- is_detected_v<trinary_iter_ctor, iterator_t<V>>
- #endif
- ) {
- return iterator_t<V>(it.begin(), it.end(), it.end());
- } else {
- return it.end();
- }
- }
- iterator_t<V> first_;
- };
- template<class V>
- struct first_last_storage<V, false, true>
- {
- #if !BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<
- typename Enable =
- std::enable_if_t<default_initializable_v<sentinel_t<V>>>>
- #endif
- constexpr first_last_storage()
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires default_initializable_v<sentinel_t<V>>
- #endif
- {}
- constexpr first_last_storage(V & base) : last_{detail::end(base)} {}
- constexpr auto begin(iterator_t<V> & it) const {
- if constexpr (is_utf_iter<iterator_t<V>>) {
- return iterator_t<V>(it.begin(), it.begin(), it.end());
- } else {
- return;
- }
- }
- constexpr auto end(iterator_t<V> & it) const { return last_; }
- sentinel_t<V> last_;
- };
- template<class V>
- struct first_last_storage<V, false, false>
- {
- constexpr first_last_storage() = default;
- constexpr first_last_storage(V & base) {}
- constexpr auto begin(iterator_t<V> & it) const {
- if constexpr (is_utf_iter<iterator_t<V>>) {
- return iterator_t<V>(it.begin(), it.begin(), it.end());
- } else {
- return;
- }
- }
- constexpr auto end(iterator_t<V> & it) const {
- if constexpr (
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- requires { iterator_t<V>(it.begin(), it.end(), it.end()); }
- #else
- is_detected_v<trinary_iter_ctor, iterator_t<V>>
- #endif
- ) {
- return iterator_t<V>(it.begin(), it.end(), it.end());
- } else {
- return it.end();
- }
- }
- };
- template<class V>
- constexpr auto uc_view_category() {
- if constexpr (common_range_v<V> && bidirectional_range_v<V>) {
- return std::bidirectional_iterator_tag{};
- } else {
- return std::forward_iterator_tag{};
- }
- }
- template<class V>
- using uc_view_category_t = decltype(uc_view_category<V>());
- template<bool Const, class T>
- using maybe_const = std::conditional_t<Const, const T, T>;
- template<class T>
- constexpr bool is_empty_view = false;
- #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
- template<class T>
- constexpr bool is_empty_view<std::ranges::empty_view<T>> = true;
- #endif
- }}}
- #endif
|