toml.hpp 475 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748
  1. //----------------------------------------------------------------------------------------------------------------------
  2. //
  3. // toml++ v3.4.0
  4. // https://github.com/marzer/tomlplusplus
  5. // SPDX-License-Identifier: MIT
  6. //
  7. //----------------------------------------------------------------------------------------------------------------------
  8. //
  9. // - THIS FILE WAS ASSEMBLED FROM MULTIPLE HEADER FILES BY A SCRIPT - PLEASE DON'T EDIT IT DIRECTLY -
  10. //
  11. // If you wish to submit a contribution to toml++, hooray and thanks! Before you crack on, please be aware that this
  12. // file was assembled from a number of smaller files by a python script, and code contributions should not be made
  13. // against it directly. You should instead make your changes in the relevant source file(s). The file names of the files
  14. // that contributed to this header can be found at the beginnings and ends of the corresponding sections of this file.
  15. //
  16. //----------------------------------------------------------------------------------------------------------------------
  17. //
  18. // TOML Language Specifications:
  19. // latest: https://github.com/toml-lang/toml/blob/master/README.md
  20. // v1.0.0: https://toml.io/en/v1.0.0
  21. // v0.5.0: https://toml.io/en/v0.5.0
  22. // changelog: https://github.com/toml-lang/toml/blob/master/CHANGELOG.md
  23. //
  24. //----------------------------------------------------------------------------------------------------------------------
  25. //
  26. // MIT License
  27. //
  28. // Copyright (c) Mark Gillard <[email protected]>
  29. //
  30. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  31. // documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  32. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  33. // permit persons to whom the Software is furnished to do so, subject to the following conditions:
  34. //
  35. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  36. // Software.
  37. //
  38. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  39. // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  40. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  41. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  42. //
  43. //----------------------------------------------------------------------------------------------------------------------
  44. #ifndef TOMLPLUSPLUS_HPP
  45. #define TOMLPLUSPLUS_HPP
  46. #define INCLUDE_TOMLPLUSPLUS_H // old guard name used pre-v3
  47. #define TOMLPLUSPLUS_H // guard name used in the legacy toml.h
  48. //******** impl/preprocessor.hpp *************************************************************************************
  49. #ifndef __cplusplus
  50. #error toml++ is a C++ library.
  51. #endif
  52. #ifndef TOML_CPP
  53. #ifdef _MSVC_LANG
  54. #if _MSVC_LANG > __cplusplus
  55. #define TOML_CPP _MSVC_LANG
  56. #endif
  57. #endif
  58. #ifndef TOML_CPP
  59. #define TOML_CPP __cplusplus
  60. #endif
  61. #if TOML_CPP >= 202900L
  62. #undef TOML_CPP
  63. #define TOML_CPP 29
  64. #elif TOML_CPP >= 202600L
  65. #undef TOML_CPP
  66. #define TOML_CPP 26
  67. #elif TOML_CPP >= 202302L
  68. #undef TOML_CPP
  69. #define TOML_CPP 23
  70. #elif TOML_CPP >= 202002L
  71. #undef TOML_CPP
  72. #define TOML_CPP 20
  73. #elif TOML_CPP >= 201703L
  74. #undef TOML_CPP
  75. #define TOML_CPP 17
  76. #elif TOML_CPP >= 201402L
  77. #undef TOML_CPP
  78. #define TOML_CPP 14
  79. #elif TOML_CPP >= 201103L
  80. #undef TOML_CPP
  81. #define TOML_CPP 11
  82. #else
  83. #undef TOML_CPP
  84. #define TOML_CPP 0
  85. #endif
  86. #endif
  87. #if !TOML_CPP
  88. #error toml++ requires C++17 or higher. For a pre-C++11 TOML library see https://github.com/ToruNiina/Boost.toml
  89. #elif TOML_CPP < 17
  90. #error toml++ requires C++17 or higher. For a C++11 TOML library see https://github.com/ToruNiina/toml11
  91. #endif
  92. #ifndef TOML_MAKE_VERSION
  93. #define TOML_MAKE_VERSION(major, minor, patch) (((major)*10000) + ((minor)*100) + ((patch)))
  94. #endif
  95. #ifndef TOML_INTELLISENSE
  96. #ifdef __INTELLISENSE__
  97. #define TOML_INTELLISENSE 1
  98. #else
  99. #define TOML_INTELLISENSE 0
  100. #endif
  101. #endif
  102. #ifndef TOML_DOXYGEN
  103. #if defined(DOXYGEN) || defined(__DOXYGEN) || defined(__DOXYGEN__) || defined(__doxygen__) || defined(__POXY__) \
  104. || defined(__poxy__)
  105. #define TOML_DOXYGEN 1
  106. #else
  107. #define TOML_DOXYGEN 0
  108. #endif
  109. #endif
  110. #ifndef TOML_CLANG
  111. #ifdef __clang__
  112. #define TOML_CLANG __clang_major__
  113. #else
  114. #define TOML_CLANG 0
  115. #endif
  116. // special handling for apple clang; see:
  117. // - https://github.com/marzer/tomlplusplus/issues/189
  118. // - https://en.wikipedia.org/wiki/Xcode
  119. // -
  120. // https://stackoverflow.com/questions/19387043/how-can-i-reliably-detect-the-version-of-clang-at-preprocessing-time
  121. #if TOML_CLANG && defined(__apple_build_version__)
  122. #undef TOML_CLANG
  123. #define TOML_CLANG_VERSION TOML_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
  124. #if TOML_CLANG_VERSION >= TOML_MAKE_VERSION(15, 0, 0)
  125. #define TOML_CLANG 16
  126. #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(14, 3, 0)
  127. #define TOML_CLANG 15
  128. #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(14, 0, 0)
  129. #define TOML_CLANG 14
  130. #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(13, 1, 6)
  131. #define TOML_CLANG 13
  132. #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(13, 0, 0)
  133. #define TOML_CLANG 12
  134. #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(12, 0, 5)
  135. #define TOML_CLANG 11
  136. #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(12, 0, 0)
  137. #define TOML_CLANG 10
  138. #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(11, 0, 3)
  139. #define TOML_CLANG 9
  140. #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(11, 0, 0)
  141. #define TOML_CLANG 8
  142. #elif TOML_CLANG_VERSION >= TOML_MAKE_VERSION(10, 0, 1)
  143. #define TOML_CLANG 7
  144. #else
  145. #define TOML_CLANG 6 // not strictly correct but doesn't matter below this
  146. #endif
  147. #undef TOML_CLANG_VERSION
  148. #endif
  149. #endif
  150. #ifndef TOML_ICC
  151. #ifdef __INTEL_COMPILER
  152. #define TOML_ICC __INTEL_COMPILER
  153. #ifdef __ICL
  154. #define TOML_ICC_CL TOML_ICC
  155. #else
  156. #define TOML_ICC_CL 0
  157. #endif
  158. #else
  159. #define TOML_ICC 0
  160. #define TOML_ICC_CL 0
  161. #endif
  162. #endif
  163. #ifndef TOML_MSVC_LIKE
  164. #ifdef _MSC_VER
  165. #define TOML_MSVC_LIKE _MSC_VER
  166. #else
  167. #define TOML_MSVC_LIKE 0
  168. #endif
  169. #endif
  170. #ifndef TOML_MSVC
  171. #if TOML_MSVC_LIKE && !TOML_CLANG && !TOML_ICC
  172. #define TOML_MSVC TOML_MSVC_LIKE
  173. #else
  174. #define TOML_MSVC 0
  175. #endif
  176. #endif
  177. #ifndef TOML_GCC_LIKE
  178. #ifdef __GNUC__
  179. #define TOML_GCC_LIKE __GNUC__
  180. #else
  181. #define TOML_GCC_LIKE 0
  182. #endif
  183. #endif
  184. #ifndef TOML_GCC
  185. #if TOML_GCC_LIKE && !TOML_CLANG && !TOML_ICC
  186. #define TOML_GCC TOML_GCC_LIKE
  187. #else
  188. #define TOML_GCC 0
  189. #endif
  190. #endif
  191. #ifndef TOML_CUDA
  192. #if defined(__CUDACC__) || defined(__CUDA_ARCH__) || defined(__CUDA_LIBDEVICE__)
  193. #define TOML_CUDA 1
  194. #else
  195. #define TOML_CUDA 0
  196. #endif
  197. #endif
  198. #ifndef TOML_ARCH_ITANIUM
  199. #if defined(__ia64__) || defined(__ia64) || defined(_IA64) || defined(__IA64__) || defined(_M_IA64)
  200. #define TOML_ARCH_ITANIUM 1
  201. #define TOML_ARCH_BITNESS 64
  202. #else
  203. #define TOML_ARCH_ITANIUM 0
  204. #endif
  205. #endif
  206. #ifndef TOML_ARCH_AMD64
  207. #if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_AMD64)
  208. #define TOML_ARCH_AMD64 1
  209. #define TOML_ARCH_BITNESS 64
  210. #else
  211. #define TOML_ARCH_AMD64 0
  212. #endif
  213. #endif
  214. #ifndef TOML_ARCH_X86
  215. #if defined(__i386__) || defined(_M_IX86)
  216. #define TOML_ARCH_X86 1
  217. #define TOML_ARCH_BITNESS 32
  218. #else
  219. #define TOML_ARCH_X86 0
  220. #endif
  221. #endif
  222. #ifndef TOML_ARCH_ARM
  223. #if defined(__aarch64__) || defined(__ARM_ARCH_ISA_A64) || defined(_M_ARM64) || defined(__ARM_64BIT_STATE) \
  224. || defined(_M_ARM64EC)
  225. #define TOML_ARCH_ARM32 0
  226. #define TOML_ARCH_ARM64 1
  227. #define TOML_ARCH_ARM 1
  228. #define TOML_ARCH_BITNESS 64
  229. #elif defined(__arm__) || defined(_M_ARM) || defined(__ARM_32BIT_STATE)
  230. #define TOML_ARCH_ARM32 1
  231. #define TOML_ARCH_ARM64 0
  232. #define TOML_ARCH_ARM 1
  233. #define TOML_ARCH_BITNESS 32
  234. #else
  235. #define TOML_ARCH_ARM32 0
  236. #define TOML_ARCH_ARM64 0
  237. #define TOML_ARCH_ARM 0
  238. #endif
  239. #endif
  240. #ifndef TOML_ARCH_BITNESS
  241. #define TOML_ARCH_BITNESS 0
  242. #endif
  243. #ifndef TOML_ARCH_X64
  244. #if TOML_ARCH_BITNESS == 64
  245. #define TOML_ARCH_X64 1
  246. #else
  247. #define TOML_ARCH_X64 0
  248. #endif
  249. #endif
  250. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__CYGWIN__)
  251. #define TOML_WINDOWS 1
  252. #else
  253. #define TOML_WINDOWS 0
  254. #endif
  255. #ifdef __unix__
  256. #define TOML_UNIX 1
  257. #else
  258. #define TOML_UNIX 0
  259. #endif
  260. #ifdef __linux__
  261. #define TOML_LINUX 1
  262. #else
  263. #define TOML_LINUX 0
  264. #endif
  265. // TOML_HAS_INCLUDE
  266. #ifndef TOML_HAS_INCLUDE
  267. #ifdef __has_include
  268. #define TOML_HAS_INCLUDE(header) __has_include(header)
  269. #else
  270. #define TOML_HAS_INCLUDE(header) 0
  271. #endif
  272. #endif
  273. // TOML_HAS_BUILTIN
  274. #ifndef TOML_HAS_BUILTIN
  275. #ifdef __has_builtin
  276. #define TOML_HAS_BUILTIN(name) __has_builtin(name)
  277. #else
  278. #define TOML_HAS_BUILTIN(name) 0
  279. #endif
  280. #endif
  281. // TOML_HAS_FEATURE
  282. #ifndef TOML_HAS_FEATURE
  283. #ifdef __has_feature
  284. #define TOML_HAS_FEATURE(name) __has_feature(name)
  285. #else
  286. #define TOML_HAS_FEATURE(name) 0
  287. #endif
  288. #endif
  289. // TOML_HAS_ATTR
  290. #ifndef TOML_HAS_ATTR
  291. #ifdef __has_attribute
  292. #define TOML_HAS_ATTR(attr) __has_attribute(attr)
  293. #else
  294. #define TOML_HAS_ATTR(attr) 0
  295. #endif
  296. #endif
  297. // TOML_HAS_CPP_ATTR
  298. #ifndef TOML_HAS_CPP_ATTR
  299. #ifdef __has_cpp_attribute
  300. #define TOML_HAS_CPP_ATTR(attr) __has_cpp_attribute(attr)
  301. #else
  302. #define TOML_HAS_CPP_ATTR(attr) 0
  303. #endif
  304. #endif
  305. // TOML_ATTR (gnu attributes)
  306. #ifndef TOML_ATTR
  307. #if TOML_CLANG || TOML_GCC_LIKE
  308. #define TOML_ATTR(...) __attribute__((__VA_ARGS__))
  309. #else
  310. #define TOML_ATTR(...)
  311. #endif
  312. #endif
  313. // TOML_DECLSPEC (msvc attributes)
  314. #ifndef TOML_DECLSPEC
  315. #if TOML_MSVC_LIKE
  316. #define TOML_DECLSPEC(...) __declspec(__VA_ARGS__)
  317. #else
  318. #define TOML_DECLSPEC(...)
  319. #endif
  320. #endif
  321. // TOML_COMPILER_HAS_EXCEPTIONS
  322. #ifndef TOML_COMPILER_HAS_EXCEPTIONS
  323. #if defined(__EXCEPTIONS) || defined(_CPPUNWIND) || defined(__cpp_exceptions)
  324. #define TOML_COMPILER_HAS_EXCEPTIONS 1
  325. #else
  326. #define TOML_COMPILER_HAS_EXCEPTIONS 0
  327. #endif
  328. #endif
  329. // TOML_COMPILER_HAS_RTTI
  330. #ifndef TOML_COMPILER_HAS_RTTI
  331. #if defined(_CPPRTTI) || defined(__GXX_RTTI) || TOML_HAS_FEATURE(cxx_rtti)
  332. #define TOML_COMPILER_HAS_RTTI 1
  333. #else
  334. #define TOML_COMPILER_HAS_RTTI 0
  335. #endif
  336. #endif
  337. // TOML_CONCAT
  338. #define TOML_CONCAT_1(x, y) x##y
  339. #define TOML_CONCAT(x, y) TOML_CONCAT_1(x, y)
  340. // TOML_MAKE_STRING
  341. #define TOML_MAKE_STRING_1(s) #s
  342. #define TOML_MAKE_STRING(s) TOML_MAKE_STRING_1(s)
  343. // TOML_PRAGMA_XXXX (compiler-specific pragmas)
  344. #if TOML_CLANG
  345. #define TOML_PRAGMA_CLANG(decl) _Pragma(TOML_MAKE_STRING(clang decl))
  346. #else
  347. #define TOML_PRAGMA_CLANG(decl)
  348. #endif
  349. #if TOML_CLANG >= 8
  350. #define TOML_PRAGMA_CLANG_GE_8(decl) TOML_PRAGMA_CLANG(decl)
  351. #else
  352. #define TOML_PRAGMA_CLANG_GE_8(decl)
  353. #endif
  354. #if TOML_CLANG >= 9
  355. #define TOML_PRAGMA_CLANG_GE_9(decl) TOML_PRAGMA_CLANG(decl)
  356. #else
  357. #define TOML_PRAGMA_CLANG_GE_9(decl)
  358. #endif
  359. #if TOML_CLANG >= 10
  360. #define TOML_PRAGMA_CLANG_GE_10(decl) TOML_PRAGMA_CLANG(decl)
  361. #else
  362. #define TOML_PRAGMA_CLANG_GE_10(decl)
  363. #endif
  364. #if TOML_CLANG >= 11
  365. #define TOML_PRAGMA_CLANG_GE_11(decl) TOML_PRAGMA_CLANG(decl)
  366. #else
  367. #define TOML_PRAGMA_CLANG_GE_11(decl)
  368. #endif
  369. #if TOML_GCC
  370. #define TOML_PRAGMA_GCC(decl) _Pragma(TOML_MAKE_STRING(GCC decl))
  371. #else
  372. #define TOML_PRAGMA_GCC(decl)
  373. #endif
  374. #if TOML_MSVC
  375. #define TOML_PRAGMA_MSVC(...) __pragma(__VA_ARGS__)
  376. #else
  377. #define TOML_PRAGMA_MSVC(...)
  378. #endif
  379. #if TOML_ICC
  380. #define TOML_PRAGMA_ICC(...) __pragma(__VA_ARGS__)
  381. #else
  382. #define TOML_PRAGMA_ICC(...)
  383. #endif
  384. // TOML_ALWAYS_INLINE
  385. #ifdef _MSC_VER
  386. #define TOML_ALWAYS_INLINE __forceinline
  387. #elif TOML_GCC || TOML_CLANG || TOML_HAS_ATTR(__always_inline__)
  388. #define TOML_ALWAYS_INLINE \
  389. TOML_ATTR(__always_inline__) \
  390. inline
  391. #else
  392. #define TOML_ALWAYS_INLINE inline
  393. #endif
  394. // TOML_NEVER_INLINE
  395. #ifdef _MSC_VER
  396. #define TOML_NEVER_INLINE TOML_DECLSPEC(noinline)
  397. #elif TOML_CUDA // https://gitlab.gnome.org/GNOME/glib/-/issues/2555
  398. #define TOML_NEVER_INLINE TOML_ATTR(noinline)
  399. #else
  400. #if TOML_GCC || TOML_CLANG || TOML_HAS_ATTR(__noinline__)
  401. #define TOML_NEVER_INLINE TOML_ATTR(__noinline__)
  402. #endif
  403. #endif
  404. #ifndef TOML_NEVER_INLINE
  405. #define TOML_NEVER_INLINE
  406. #endif
  407. // MSVC attributes
  408. #define TOML_ABSTRACT_INTERFACE TOML_DECLSPEC(novtable)
  409. #define TOML_EMPTY_BASES TOML_DECLSPEC(empty_bases)
  410. // TOML_TRIVIAL_ABI
  411. #if TOML_CLANG || TOML_HAS_ATTR(__trivial_abi__)
  412. #define TOML_TRIVIAL_ABI TOML_ATTR(__trivial_abi__)
  413. #else
  414. #define TOML_TRIVIAL_ABI
  415. #endif
  416. // TOML_NODISCARD
  417. #if TOML_CPP >= 17 && TOML_HAS_CPP_ATTR(nodiscard) >= 201603
  418. #define TOML_NODISCARD [[nodiscard]]
  419. #elif TOML_CLANG || TOML_GCC || TOML_HAS_ATTR(__warn_unused_result__)
  420. #define TOML_NODISCARD TOML_ATTR(__warn_unused_result__)
  421. #else
  422. #define TOML_NODISCARD
  423. #endif
  424. // TOML_NODISCARD_CTOR
  425. #if TOML_CPP >= 17 && TOML_HAS_CPP_ATTR(nodiscard) >= 201907
  426. #define TOML_NODISCARD_CTOR [[nodiscard]]
  427. #else
  428. #define TOML_NODISCARD_CTOR
  429. #endif
  430. // pure + const
  431. #ifndef TOML_PURE
  432. #ifdef NDEBUG
  433. #define TOML_PURE \
  434. TOML_DECLSPEC(noalias) \
  435. TOML_ATTR(pure)
  436. #else
  437. #define TOML_PURE
  438. #endif
  439. #endif
  440. #ifndef TOML_CONST
  441. #ifdef NDEBUG
  442. #define TOML_CONST \
  443. TOML_DECLSPEC(noalias) \
  444. TOML_ATTR(const)
  445. #else
  446. #define TOML_CONST
  447. #endif
  448. #endif
  449. #ifndef TOML_INLINE_GETTER
  450. #define TOML_INLINE_GETTER \
  451. TOML_NODISCARD \
  452. TOML_ALWAYS_INLINE
  453. #endif
  454. #ifndef TOML_PURE_GETTER
  455. #define TOML_PURE_GETTER \
  456. TOML_NODISCARD \
  457. TOML_PURE
  458. #endif
  459. #ifndef TOML_PURE_INLINE_GETTER
  460. #define TOML_PURE_INLINE_GETTER \
  461. TOML_NODISCARD \
  462. TOML_ALWAYS_INLINE \
  463. TOML_PURE
  464. #endif
  465. #ifndef TOML_CONST_GETTER
  466. #define TOML_CONST_GETTER \
  467. TOML_NODISCARD \
  468. TOML_CONST
  469. #endif
  470. #ifndef TOML_CONST_INLINE_GETTER
  471. #define TOML_CONST_INLINE_GETTER \
  472. TOML_NODISCARD \
  473. TOML_ALWAYS_INLINE \
  474. TOML_CONST
  475. #endif
  476. // TOML_ASSUME
  477. #ifdef _MSC_VER
  478. #define TOML_ASSUME(expr) __assume(expr)
  479. #elif TOML_ICC || TOML_CLANG || TOML_HAS_BUILTIN(__builtin_assume)
  480. #define TOML_ASSUME(expr) __builtin_assume(expr)
  481. #elif TOML_HAS_CPP_ATTR(assume) >= 202207
  482. #define TOML_ASSUME(expr) [[assume(expr)]]
  483. #elif TOML_HAS_ATTR(__assume__)
  484. #define TOML_ASSUME(expr) __attribute__((__assume__(expr)))
  485. #else
  486. #define TOML_ASSUME(expr) static_cast<void>(0)
  487. #endif
  488. // TOML_UNREACHABLE
  489. #ifdef _MSC_VER
  490. #define TOML_UNREACHABLE __assume(0)
  491. #elif TOML_ICC || TOML_CLANG || TOML_GCC || TOML_HAS_BUILTIN(__builtin_unreachable)
  492. #define TOML_UNREACHABLE __builtin_unreachable()
  493. #else
  494. #define TOML_UNREACHABLE static_cast<void>(0)
  495. #endif
  496. // TOML_LIKELY
  497. #if TOML_CPP >= 20 && TOML_HAS_CPP_ATTR(likely) >= 201803
  498. #define TOML_LIKELY(...) (__VA_ARGS__) [[likely]]
  499. #define TOML_LIKELY_CASE [[likely]]
  500. #elif TOML_GCC || TOML_CLANG || TOML_HAS_BUILTIN(__builtin_expect)
  501. #define TOML_LIKELY(...) (__builtin_expect(!!(__VA_ARGS__), 1))
  502. #else
  503. #define TOML_LIKELY(...) (__VA_ARGS__)
  504. #endif
  505. #ifndef TOML_LIKELY_CASE
  506. #define TOML_LIKELY_CASE
  507. #endif
  508. // TOML_UNLIKELY
  509. #if TOML_CPP >= 20 && TOML_HAS_CPP_ATTR(unlikely) >= 201803
  510. #define TOML_UNLIKELY(...) (__VA_ARGS__) [[unlikely]]
  511. #define TOML_UNLIKELY_CASE [[unlikely]]
  512. #elif TOML_GCC || TOML_CLANG || TOML_HAS_BUILTIN(__builtin_expect)
  513. #define TOML_UNLIKELY(...) (__builtin_expect(!!(__VA_ARGS__), 0))
  514. #else
  515. #define TOML_UNLIKELY(...) (__VA_ARGS__)
  516. #endif
  517. #ifndef TOML_UNLIKELY_CASE
  518. #define TOML_UNLIKELY_CASE
  519. #endif
  520. // TOML_FLAGS_ENUM
  521. #if TOML_CLANG || TOML_HAS_ATTR(flag_enum)
  522. #define TOML_FLAGS_ENUM __attribute__((flag_enum))
  523. #else
  524. #define TOML_FLAGS_ENUM
  525. #endif
  526. // TOML_OPEN_ENUM + TOML_CLOSED_ENUM
  527. #if TOML_CLANG || TOML_HAS_ATTR(enum_extensibility)
  528. #define TOML_OPEN_ENUM __attribute__((enum_extensibility(open)))
  529. #define TOML_CLOSED_ENUM __attribute__((enum_extensibility(closed)))
  530. #else
  531. #define TOML_OPEN_ENUM
  532. #define TOML_CLOSED_ENUM
  533. #endif
  534. // TOML_OPEN_FLAGS_ENUM + TOML_CLOSED_FLAGS_ENUM
  535. #define TOML_OPEN_FLAGS_ENUM TOML_OPEN_ENUM TOML_FLAGS_ENUM
  536. #define TOML_CLOSED_FLAGS_ENUM TOML_CLOSED_ENUM TOML_FLAGS_ENUM
  537. // TOML_MAKE_FLAGS
  538. #define TOML_MAKE_FLAGS_2(T, op, linkage) \
  539. TOML_CONST_INLINE_GETTER \
  540. linkage constexpr T operator op(T lhs, T rhs) noexcept \
  541. { \
  542. using under = std::underlying_type_t<T>; \
  543. return static_cast<T>(static_cast<under>(lhs) op static_cast<under>(rhs)); \
  544. } \
  545. \
  546. linkage constexpr T& operator TOML_CONCAT(op, =)(T & lhs, T rhs) noexcept \
  547. { \
  548. return lhs = (lhs op rhs); \
  549. } \
  550. \
  551. static_assert(true)
  552. #define TOML_MAKE_FLAGS_1(T, linkage) \
  553. static_assert(std::is_enum_v<T>); \
  554. \
  555. TOML_MAKE_FLAGS_2(T, &, linkage); \
  556. TOML_MAKE_FLAGS_2(T, |, linkage); \
  557. TOML_MAKE_FLAGS_2(T, ^, linkage); \
  558. \
  559. TOML_CONST_INLINE_GETTER \
  560. linkage constexpr T operator~(T val) noexcept \
  561. { \
  562. using under = std::underlying_type_t<T>; \
  563. return static_cast<T>(~static_cast<under>(val)); \
  564. } \
  565. \
  566. TOML_CONST_INLINE_GETTER \
  567. linkage constexpr bool operator!(T val) noexcept \
  568. { \
  569. using under = std::underlying_type_t<T>; \
  570. return !static_cast<under>(val); \
  571. } \
  572. \
  573. static_assert(true)
  574. #define TOML_MAKE_FLAGS(T) TOML_MAKE_FLAGS_1(T, )
  575. #define TOML_UNUSED(...) static_cast<void>(__VA_ARGS__)
  576. #define TOML_DELETE_DEFAULTS(T) \
  577. T(const T&) = delete; \
  578. T(T&&) = delete; \
  579. T& operator=(const T&) = delete; \
  580. T& operator=(T&&) = delete
  581. #define TOML_ASYMMETRICAL_EQUALITY_OPS(LHS, RHS, ...) \
  582. __VA_ARGS__ TOML_NODISCARD \
  583. friend bool operator==(RHS rhs, LHS lhs) noexcept \
  584. { \
  585. return lhs == rhs; \
  586. } \
  587. __VA_ARGS__ TOML_NODISCARD \
  588. friend bool operator!=(LHS lhs, RHS rhs) noexcept \
  589. { \
  590. return !(lhs == rhs); \
  591. } \
  592. __VA_ARGS__ TOML_NODISCARD \
  593. friend bool operator!=(RHS rhs, LHS lhs) noexcept \
  594. { \
  595. return !(lhs == rhs); \
  596. } \
  597. static_assert(true)
  598. #define TOML_EVAL_BOOL_1(T, F) T
  599. #define TOML_EVAL_BOOL_0(T, F) F
  600. #if !defined(__POXY__) && !defined(POXY_IMPLEMENTATION_DETAIL)
  601. #define POXY_IMPLEMENTATION_DETAIL(...) __VA_ARGS__
  602. #endif
  603. // COMPILER-SPECIFIC WARNING MANAGEMENT
  604. #if TOML_CLANG
  605. #define TOML_PUSH_WARNINGS \
  606. TOML_PRAGMA_CLANG(diagnostic push) \
  607. TOML_PRAGMA_CLANG(diagnostic ignored "-Wunknown-warning-option") \
  608. static_assert(true)
  609. #define TOML_DISABLE_SWITCH_WARNINGS \
  610. TOML_PRAGMA_CLANG(diagnostic ignored "-Wswitch") \
  611. static_assert(true)
  612. #define TOML_DISABLE_ARITHMETIC_WARNINGS \
  613. TOML_PRAGMA_CLANG_GE_10(diagnostic ignored "-Wimplicit-int-float-conversion") \
  614. TOML_PRAGMA_CLANG(diagnostic ignored "-Wfloat-equal") \
  615. TOML_PRAGMA_CLANG(diagnostic ignored "-Wdouble-promotion") \
  616. TOML_PRAGMA_CLANG(diagnostic ignored "-Wchar-subscripts") \
  617. TOML_PRAGMA_CLANG(diagnostic ignored "-Wshift-sign-overflow") \
  618. static_assert(true)
  619. #define TOML_DISABLE_SPAM_WARNINGS \
  620. TOML_PRAGMA_CLANG_GE_8(diagnostic ignored "-Wdefaulted-function-deleted") \
  621. TOML_PRAGMA_CLANG_GE_9(diagnostic ignored "-Wctad-maybe-unsupported") \
  622. TOML_PRAGMA_CLANG_GE_10(diagnostic ignored "-Wzero-as-null-pointer-constant") \
  623. TOML_PRAGMA_CLANG_GE_11(diagnostic ignored "-Wsuggest-destructor-override") \
  624. TOML_PRAGMA_CLANG(diagnostic ignored "-Wweak-vtables") \
  625. TOML_PRAGMA_CLANG(diagnostic ignored "-Wweak-template-vtables") \
  626. TOML_PRAGMA_CLANG(diagnostic ignored "-Wdouble-promotion") \
  627. TOML_PRAGMA_CLANG(diagnostic ignored "-Wchar-subscripts") \
  628. TOML_PRAGMA_CLANG(diagnostic ignored "-Wmissing-field-initializers") \
  629. TOML_PRAGMA_CLANG(diagnostic ignored "-Wpadded") \
  630. static_assert(true)
  631. #define TOML_POP_WARNINGS \
  632. TOML_PRAGMA_CLANG(diagnostic pop) \
  633. static_assert(true)
  634. #define TOML_DISABLE_WARNINGS \
  635. TOML_PRAGMA_CLANG(diagnostic push) \
  636. TOML_PRAGMA_CLANG(diagnostic ignored "-Weverything") \
  637. static_assert(true, "")
  638. #define TOML_ENABLE_WARNINGS \
  639. TOML_PRAGMA_CLANG(diagnostic pop) \
  640. static_assert(true)
  641. #define TOML_SIMPLE_STATIC_ASSERT_MESSAGES 1
  642. #elif TOML_MSVC
  643. #define TOML_PUSH_WARNINGS \
  644. __pragma(warning(push)) \
  645. static_assert(true)
  646. #if TOML_HAS_INCLUDE(<CodeAnalysis/Warnings.h>)
  647. #pragma warning(push, 0)
  648. #include <CodeAnalysis/Warnings.h>
  649. #pragma warning(pop)
  650. #define TOML_DISABLE_CODE_ANALYSIS_WARNINGS \
  651. __pragma(warning(disable : ALL_CODE_ANALYSIS_WARNINGS)) \
  652. static_assert(true)
  653. #else
  654. #define TOML_DISABLE_CODE_ANALYSIS_WARNINGS static_assert(true)
  655. #endif
  656. #define TOML_DISABLE_SWITCH_WARNINGS \
  657. __pragma(warning(disable : 4061)) \
  658. __pragma(warning(disable : 4062)) \
  659. __pragma(warning(disable : 4063)) \
  660. __pragma(warning(disable : 5262)) /* switch-case implicit fallthrough (false-positive) */ \
  661. __pragma(warning(disable : 26819)) /* cg: unannotated fallthrough */ \
  662. static_assert(true)
  663. #define TOML_DISABLE_SPAM_WARNINGS \
  664. __pragma(warning(disable : 4127)) /* conditional expr is constant */ \
  665. __pragma(warning(disable : 4324)) /* structure was padded due to alignment specifier */ \
  666. __pragma(warning(disable : 4348)) \
  667. __pragma(warning(disable : 4464)) /* relative include path contains '..' */ \
  668. __pragma(warning(disable : 4505)) /* unreferenced local function removed */ \
  669. __pragma(warning(disable : 4514)) /* unreferenced inline function has been removed */ \
  670. __pragma(warning(disable : 4582)) /* constructor is not implicitly called */ \
  671. __pragma(warning(disable : 4619)) /* there is no warning number 'XXXX' */ \
  672. __pragma(warning(disable : 4623)) /* default constructor was implicitly defined as deleted */ \
  673. __pragma(warning(disable : 4625)) /* copy constructor was implicitly defined as deleted */ \
  674. __pragma(warning(disable : 4626)) /* assignment operator was implicitly defined as deleted */ \
  675. __pragma(warning(disable : 4710)) /* function not inlined */ \
  676. __pragma(warning(disable : 4711)) /* function selected for automatic expansion */ \
  677. __pragma(warning(disable : 4820)) /* N bytes padding added */ \
  678. __pragma(warning(disable : 4946)) /* reinterpret_cast used between related classes */ \
  679. __pragma(warning(disable : 5026)) /* move constructor was implicitly defined as deleted */ \
  680. __pragma(warning(disable : 5027)) /* move assignment operator was implicitly defined as deleted */ \
  681. __pragma(warning(disable : 5039)) /* potentially throwing function passed to 'extern "C"' function */ \
  682. __pragma(warning(disable : 5045)) /* Compiler will insert Spectre mitigation */ \
  683. __pragma(warning(disable : 5264)) /* const variable is not used (false-positive) */ \
  684. __pragma(warning(disable : 26451)) \
  685. __pragma(warning(disable : 26490)) \
  686. __pragma(warning(disable : 26495)) \
  687. __pragma(warning(disable : 26812)) \
  688. __pragma(warning(disable : 26819)) \
  689. static_assert(true)
  690. #define TOML_DISABLE_ARITHMETIC_WARNINGS \
  691. __pragma(warning(disable : 4365)) /* argument signed/unsigned mismatch */ \
  692. __pragma(warning(disable : 4738)) /* storing 32-bit float result in memory */ \
  693. __pragma(warning(disable : 5219)) /* implicit conversion from integral to float */ \
  694. static_assert(true)
  695. #define TOML_POP_WARNINGS \
  696. __pragma(warning(pop)) \
  697. static_assert(true)
  698. #define TOML_DISABLE_WARNINGS \
  699. __pragma(warning(push, 0)) \
  700. __pragma(warning(disable : 4348)) \
  701. __pragma(warning(disable : 4668)) \
  702. __pragma(warning(disable : 5105)) \
  703. __pragma(warning(disable : 5264)) \
  704. TOML_DISABLE_CODE_ANALYSIS_WARNINGS; \
  705. TOML_DISABLE_SWITCH_WARNINGS; \
  706. TOML_DISABLE_SPAM_WARNINGS; \
  707. TOML_DISABLE_ARITHMETIC_WARNINGS; \
  708. static_assert(true)
  709. #define TOML_ENABLE_WARNINGS TOML_POP_WARNINGS
  710. #elif TOML_ICC
  711. #define TOML_PUSH_WARNINGS \
  712. __pragma(warning(push)) \
  713. static_assert(true)
  714. #define TOML_DISABLE_SPAM_WARNINGS \
  715. __pragma(warning(disable : 82)) /* storage class is not first */ \
  716. __pragma(warning(disable : 111)) /* statement unreachable (false-positive) */ \
  717. __pragma(warning(disable : 869)) /* unreferenced parameter */ \
  718. __pragma(warning(disable : 1011)) /* missing return (false-positive) */ \
  719. __pragma(warning(disable : 2261)) /* assume expr side-effects discarded */ \
  720. static_assert(true)
  721. #define TOML_POP_WARNINGS \
  722. __pragma(warning(pop)) \
  723. static_assert(true)
  724. #define TOML_DISABLE_WARNINGS \
  725. __pragma(warning(push, 0)) \
  726. TOML_DISABLE_SPAM_WARNINGS
  727. #define TOML_ENABLE_WARNINGS \
  728. __pragma(warning(pop)) \
  729. static_assert(true)
  730. #elif TOML_GCC
  731. #define TOML_PUSH_WARNINGS \
  732. TOML_PRAGMA_GCC(diagnostic push) \
  733. static_assert(true)
  734. #define TOML_DISABLE_SWITCH_WARNINGS \
  735. TOML_PRAGMA_GCC(diagnostic ignored "-Wswitch") \
  736. TOML_PRAGMA_GCC(diagnostic ignored "-Wswitch-enum") \
  737. TOML_PRAGMA_GCC(diagnostic ignored "-Wswitch-default") \
  738. static_assert(true)
  739. #define TOML_DISABLE_ARITHMETIC_WARNINGS \
  740. TOML_PRAGMA_GCC(diagnostic ignored "-Wfloat-equal") \
  741. TOML_PRAGMA_GCC(diagnostic ignored "-Wsign-conversion") \
  742. TOML_PRAGMA_GCC(diagnostic ignored "-Wchar-subscripts") \
  743. static_assert(true)
  744. #define TOML_DISABLE_SUGGEST_ATTR_WARNINGS \
  745. TOML_PRAGMA_GCC(diagnostic ignored "-Wsuggest-attribute=const") \
  746. TOML_PRAGMA_GCC(diagnostic ignored "-Wsuggest-attribute=pure") \
  747. static_assert(true)
  748. #define TOML_DISABLE_SPAM_WARNINGS \
  749. TOML_PRAGMA_GCC(diagnostic ignored "-Wpadded") \
  750. TOML_PRAGMA_GCC(diagnostic ignored "-Wcast-align") \
  751. TOML_PRAGMA_GCC(diagnostic ignored "-Wcomment") \
  752. TOML_PRAGMA_GCC(diagnostic ignored "-Wtype-limits") \
  753. TOML_PRAGMA_GCC(diagnostic ignored "-Wuseless-cast") \
  754. TOML_PRAGMA_GCC(diagnostic ignored "-Wchar-subscripts") \
  755. TOML_PRAGMA_GCC(diagnostic ignored "-Wsubobject-linkage") \
  756. TOML_PRAGMA_GCC(diagnostic ignored "-Wmissing-field-initializers") \
  757. TOML_PRAGMA_GCC(diagnostic ignored "-Wmaybe-uninitialized") \
  758. TOML_PRAGMA_GCC(diagnostic ignored "-Wnoexcept") \
  759. TOML_PRAGMA_GCC(diagnostic ignored "-Wnull-dereference") \
  760. TOML_PRAGMA_GCC(diagnostic ignored "-Wduplicated-branches") \
  761. static_assert(true)
  762. #define TOML_POP_WARNINGS \
  763. TOML_PRAGMA_GCC(diagnostic pop) \
  764. static_assert(true)
  765. #define TOML_DISABLE_WARNINGS \
  766. TOML_PRAGMA_GCC(diagnostic push) \
  767. TOML_PRAGMA_GCC(diagnostic ignored "-Wall") \
  768. TOML_PRAGMA_GCC(diagnostic ignored "-Wextra") \
  769. TOML_PRAGMA_GCC(diagnostic ignored "-Wpedantic") \
  770. TOML_DISABLE_SWITCH_WARNINGS; \
  771. TOML_DISABLE_ARITHMETIC_WARNINGS; \
  772. TOML_DISABLE_SUGGEST_ATTR_WARNINGS; \
  773. TOML_DISABLE_SPAM_WARNINGS; \
  774. static_assert(true)
  775. #define TOML_ENABLE_WARNINGS \
  776. TOML_PRAGMA_GCC(diagnostic pop) \
  777. static_assert(true)
  778. #endif
  779. #ifndef TOML_PUSH_WARNINGS
  780. #define TOML_PUSH_WARNINGS static_assert(true)
  781. #endif
  782. #ifndef TOML_DISABLE_CODE_ANALYSIS_WARNINGS
  783. #define TOML_DISABLE_CODE_ANALYSIS_WARNINGS static_assert(true)
  784. #endif
  785. #ifndef TOML_DISABLE_SWITCH_WARNINGS
  786. #define TOML_DISABLE_SWITCH_WARNINGS static_assert(true)
  787. #endif
  788. #ifndef TOML_DISABLE_SUGGEST_ATTR_WARNINGS
  789. #define TOML_DISABLE_SUGGEST_ATTR_WARNINGS static_assert(true)
  790. #endif
  791. #ifndef TOML_DISABLE_SPAM_WARNINGS
  792. #define TOML_DISABLE_SPAM_WARNINGS static_assert(true)
  793. #endif
  794. #ifndef TOML_DISABLE_ARITHMETIC_WARNINGS
  795. #define TOML_DISABLE_ARITHMETIC_WARNINGS static_assert(true)
  796. #endif
  797. #ifndef TOML_POP_WARNINGS
  798. #define TOML_POP_WARNINGS static_assert(true)
  799. #endif
  800. #ifndef TOML_DISABLE_WARNINGS
  801. #define TOML_DISABLE_WARNINGS static_assert(true)
  802. #endif
  803. #ifndef TOML_ENABLE_WARNINGS
  804. #define TOML_ENABLE_WARNINGS static_assert(true)
  805. #endif
  806. #ifndef TOML_SIMPLE_STATIC_ASSERT_MESSAGES
  807. #define TOML_SIMPLE_STATIC_ASSERT_MESSAGES 0
  808. #endif
  809. #ifdef TOML_CONFIG_HEADER
  810. #include TOML_CONFIG_HEADER
  811. #endif
  812. // is the library being built as a shared lib/dll using meson and friends?
  813. #ifndef TOML_SHARED_LIB
  814. #define TOML_SHARED_LIB 0
  815. #endif
  816. // header-only mode
  817. #if !defined(TOML_HEADER_ONLY) && defined(TOML_ALL_INLINE) // was TOML_ALL_INLINE pre-2.0
  818. #define TOML_HEADER_ONLY TOML_ALL_INLINE
  819. #endif
  820. #if !defined(TOML_HEADER_ONLY) || (defined(TOML_HEADER_ONLY) && TOML_HEADER_ONLY) || TOML_INTELLISENSE
  821. #undef TOML_HEADER_ONLY
  822. #define TOML_HEADER_ONLY 1
  823. #endif
  824. #if TOML_DOXYGEN || TOML_SHARED_LIB
  825. #undef TOML_HEADER_ONLY
  826. #define TOML_HEADER_ONLY 0
  827. #endif
  828. // internal implementation switch
  829. #if defined(TOML_IMPLEMENTATION) || TOML_HEADER_ONLY
  830. #undef TOML_IMPLEMENTATION
  831. #define TOML_IMPLEMENTATION 1
  832. #else
  833. #define TOML_IMPLEMENTATION 0
  834. #endif
  835. // dll/shared lib function exports (legacy - TOML_API was the old name for this setting)
  836. #if !defined(TOML_EXPORTED_MEMBER_FUNCTION) && !defined(TOML_EXPORTED_STATIC_FUNCTION) \
  837. && !defined(TOML_EXPORTED_FREE_FUNCTION) && !defined(TOML_EXPORTED_CLASS) && defined(TOML_API)
  838. #define TOML_EXPORTED_MEMBER_FUNCTION TOML_API
  839. #define TOML_EXPORTED_STATIC_FUNCTION TOML_API
  840. #define TOML_EXPORTED_FREE_FUNCTION TOML_API
  841. #endif
  842. // dll/shared lib exports
  843. #if TOML_SHARED_LIB
  844. #undef TOML_API
  845. #undef TOML_EXPORTED_CLASS
  846. #undef TOML_EXPORTED_MEMBER_FUNCTION
  847. #undef TOML_EXPORTED_STATIC_FUNCTION
  848. #undef TOML_EXPORTED_FREE_FUNCTION
  849. #if TOML_WINDOWS
  850. #if TOML_IMPLEMENTATION
  851. #define TOML_EXPORTED_CLASS __declspec(dllexport)
  852. #define TOML_EXPORTED_FREE_FUNCTION __declspec(dllexport)
  853. #else
  854. #define TOML_EXPORTED_CLASS __declspec(dllimport)
  855. #define TOML_EXPORTED_FREE_FUNCTION __declspec(dllimport)
  856. #endif
  857. #ifndef TOML_CALLCONV
  858. #define TOML_CALLCONV __cdecl
  859. #endif
  860. #elif defined(__GNUC__) && __GNUC__ >= 4
  861. #define TOML_EXPORTED_CLASS __attribute__((visibility("default")))
  862. #define TOML_EXPORTED_MEMBER_FUNCTION __attribute__((visibility("default")))
  863. #define TOML_EXPORTED_STATIC_FUNCTION __attribute__((visibility("default")))
  864. #define TOML_EXPORTED_FREE_FUNCTION __attribute__((visibility("default")))
  865. #endif
  866. #endif
  867. #ifndef TOML_EXPORTED_CLASS
  868. #define TOML_EXPORTED_CLASS
  869. #endif
  870. #ifndef TOML_EXPORTED_MEMBER_FUNCTION
  871. #define TOML_EXPORTED_MEMBER_FUNCTION
  872. #endif
  873. #ifndef TOML_EXPORTED_STATIC_FUNCTION
  874. #define TOML_EXPORTED_STATIC_FUNCTION
  875. #endif
  876. #ifndef TOML_EXPORTED_FREE_FUNCTION
  877. #define TOML_EXPORTED_FREE_FUNCTION
  878. #endif
  879. // experimental language features
  880. #if !defined(TOML_ENABLE_UNRELEASED_FEATURES) && defined(TOML_UNRELEASED_FEATURES) // was TOML_UNRELEASED_FEATURES
  881. // pre-3.0
  882. #define TOML_ENABLE_UNRELEASED_FEATURES TOML_UNRELEASED_FEATURES
  883. #endif
  884. #if (defined(TOML_ENABLE_UNRELEASED_FEATURES) && TOML_ENABLE_UNRELEASED_FEATURES) || TOML_INTELLISENSE
  885. #undef TOML_ENABLE_UNRELEASED_FEATURES
  886. #define TOML_ENABLE_UNRELEASED_FEATURES 1
  887. #endif
  888. #ifndef TOML_ENABLE_UNRELEASED_FEATURES
  889. #define TOML_ENABLE_UNRELEASED_FEATURES 0
  890. #endif
  891. // parser
  892. #if !defined(TOML_ENABLE_PARSER) && defined(TOML_PARSER) // was TOML_PARSER pre-3.0
  893. #define TOML_ENABLE_PARSER TOML_PARSER
  894. #endif
  895. #if !defined(TOML_ENABLE_PARSER) || (defined(TOML_ENABLE_PARSER) && TOML_ENABLE_PARSER) || TOML_INTELLISENSE
  896. #undef TOML_ENABLE_PARSER
  897. #define TOML_ENABLE_PARSER 1
  898. #endif
  899. // formatters
  900. #if !defined(TOML_ENABLE_FORMATTERS) || (defined(TOML_ENABLE_FORMATTERS) && TOML_ENABLE_FORMATTERS) || TOML_INTELLISENSE
  901. #undef TOML_ENABLE_FORMATTERS
  902. #define TOML_ENABLE_FORMATTERS 1
  903. #endif
  904. // SIMD
  905. #if !defined(TOML_ENABLE_SIMD) || (defined(TOML_ENABLE_SIMD) && TOML_ENABLE_SIMD) || TOML_INTELLISENSE
  906. #undef TOML_ENABLE_SIMD
  907. #define TOML_ENABLE_SIMD 1
  908. #endif
  909. // windows compat
  910. #if !defined(TOML_ENABLE_WINDOWS_COMPAT) && defined(TOML_WINDOWS_COMPAT) // was TOML_WINDOWS_COMPAT pre-3.0
  911. #define TOML_ENABLE_WINDOWS_COMPAT TOML_WINDOWS_COMPAT
  912. #endif
  913. #if !defined(TOML_ENABLE_WINDOWS_COMPAT) || (defined(TOML_ENABLE_WINDOWS_COMPAT) && TOML_ENABLE_WINDOWS_COMPAT) \
  914. || TOML_INTELLISENSE
  915. #undef TOML_ENABLE_WINDOWS_COMPAT
  916. #define TOML_ENABLE_WINDOWS_COMPAT 1
  917. #endif
  918. #if !TOML_WINDOWS
  919. #undef TOML_ENABLE_WINDOWS_COMPAT
  920. #define TOML_ENABLE_WINDOWS_COMPAT 0
  921. #endif
  922. #ifndef TOML_INCLUDE_WINDOWS_H
  923. #define TOML_INCLUDE_WINDOWS_H 0
  924. #endif
  925. // custom optional
  926. #ifdef TOML_OPTIONAL_TYPE
  927. #define TOML_HAS_CUSTOM_OPTIONAL_TYPE 1
  928. #else
  929. #define TOML_HAS_CUSTOM_OPTIONAL_TYPE 0
  930. #endif
  931. // exceptions (library use)
  932. #if TOML_COMPILER_HAS_EXCEPTIONS
  933. #if !defined(TOML_EXCEPTIONS) || (defined(TOML_EXCEPTIONS) && TOML_EXCEPTIONS)
  934. #undef TOML_EXCEPTIONS
  935. #define TOML_EXCEPTIONS 1
  936. #endif
  937. #else
  938. #if defined(TOML_EXCEPTIONS) && TOML_EXCEPTIONS
  939. #error TOML_EXCEPTIONS was explicitly enabled but exceptions are disabled/unsupported by the compiler.
  940. #endif
  941. #undef TOML_EXCEPTIONS
  942. #define TOML_EXCEPTIONS 0
  943. #endif
  944. // calling convention for static/free/friend functions
  945. #ifndef TOML_CALLCONV
  946. #define TOML_CALLCONV
  947. #endif
  948. #ifndef TOML_UNDEF_MACROS
  949. #define TOML_UNDEF_MACROS 1
  950. #endif
  951. #ifndef TOML_MAX_NESTED_VALUES
  952. #define TOML_MAX_NESTED_VALUES 256
  953. // this refers to the depth of nested values, e.g. inline tables and arrays.
  954. // 256 is crazy high! if you're hitting this limit with real input, TOML is probably the wrong tool for the job...
  955. #endif
  956. #ifdef TOML_CHAR_8_STRINGS
  957. #if TOML_CHAR_8_STRINGS
  958. #error TOML_CHAR_8_STRINGS was removed in toml++ 2.0.0; all value setters and getters now work with char8_t strings implicitly.
  959. #endif
  960. #endif
  961. #ifdef TOML_LARGE_FILES
  962. #if !TOML_LARGE_FILES
  963. #error Support for !TOML_LARGE_FILES (i.e. 'small files') was removed in toml++ 3.0.0.
  964. #endif
  965. #endif
  966. #ifndef TOML_LIFETIME_HOOKS
  967. #define TOML_LIFETIME_HOOKS 0
  968. #endif
  969. #ifdef NDEBUG
  970. #undef TOML_ASSERT
  971. #define TOML_ASSERT(expr) static_assert(true)
  972. #endif
  973. #ifndef TOML_ASSERT
  974. #ifndef assert
  975. TOML_DISABLE_WARNINGS;
  976. #include <cassert>
  977. TOML_ENABLE_WARNINGS;
  978. #endif
  979. #define TOML_ASSERT(expr) assert(expr)
  980. #endif
  981. #ifdef NDEBUG
  982. #define TOML_ASSERT_ASSUME(expr) TOML_ASSUME(expr)
  983. #else
  984. #define TOML_ASSERT_ASSUME(expr) TOML_ASSERT(expr)
  985. #endif
  986. #ifndef TOML_ENABLE_FLOAT16
  987. #define TOML_ENABLE_FLOAT16 0
  988. #endif
  989. #if !defined(TOML_FLOAT_CHARCONV) && (TOML_GCC || TOML_CLANG || (TOML_ICC && !TOML_ICC_CL))
  990. // not supported by any version of GCC or Clang as of 26/11/2020
  991. // not supported by any version of ICC on Linux as of 11/01/2021
  992. #define TOML_FLOAT_CHARCONV 0
  993. #endif
  994. #if !defined(TOML_INT_CHARCONV) && (defined(__EMSCRIPTEN__) || defined(__APPLE__))
  995. // causes link errors on emscripten
  996. // causes Mac OS SDK version errors on some versions of Apple Clang
  997. #define TOML_INT_CHARCONV 0
  998. #endif
  999. #ifndef TOML_INT_CHARCONV
  1000. #define TOML_INT_CHARCONV 1
  1001. #endif
  1002. #ifndef TOML_FLOAT_CHARCONV
  1003. #define TOML_FLOAT_CHARCONV 1
  1004. #endif
  1005. #if (TOML_INT_CHARCONV || TOML_FLOAT_CHARCONV) && !TOML_HAS_INCLUDE(<charconv>)
  1006. #undef TOML_INT_CHARCONV
  1007. #undef TOML_FLOAT_CHARCONV
  1008. #define TOML_INT_CHARCONV 0
  1009. #define TOML_FLOAT_CHARCONV 0
  1010. #endif
  1011. #if defined(__cpp_concepts) && __cpp_concepts >= 201907
  1012. #define TOML_REQUIRES(...) requires(__VA_ARGS__)
  1013. #else
  1014. #define TOML_REQUIRES(...)
  1015. #endif
  1016. #define TOML_ENABLE_IF(...) , typename std::enable_if<(__VA_ARGS__), int>::type = 0
  1017. #define TOML_CONSTRAINED_TEMPLATE(condition, ...) \
  1018. template <__VA_ARGS__ TOML_ENABLE_IF(condition)> \
  1019. TOML_REQUIRES(condition)
  1020. #define TOML_HIDDEN_CONSTRAINT(condition, ...) TOML_CONSTRAINED_TEMPLATE(condition, __VA_ARGS__)
  1021. #if defined(__SIZEOF_FLOAT128__) && defined(__FLT128_MANT_DIG__) && defined(__LDBL_MANT_DIG__) \
  1022. && __FLT128_MANT_DIG__ > __LDBL_MANT_DIG__
  1023. #define TOML_FLOAT128 __float128
  1024. #endif
  1025. #ifdef __SIZEOF_INT128__
  1026. #define TOML_INT128 __int128_t
  1027. #define TOML_UINT128 __uint128_t
  1028. #endif
  1029. // clang-format off
  1030. //******** impl/version.hpp ******************************************************************************************
  1031. #define TOML_LIB_MAJOR 3
  1032. #define TOML_LIB_MINOR 4
  1033. #define TOML_LIB_PATCH 0
  1034. #define TOML_LANG_MAJOR 1
  1035. #define TOML_LANG_MINOR 0
  1036. #define TOML_LANG_PATCH 0
  1037. //******** impl/preprocessor.hpp *************************************************************************************
  1038. #define TOML_LIB_SINGLE_HEADER 1
  1039. #if TOML_ENABLE_UNRELEASED_FEATURES
  1040. #define TOML_LANG_EFFECTIVE_VERSION \
  1041. TOML_MAKE_VERSION(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH+1)
  1042. #else
  1043. #define TOML_LANG_EFFECTIVE_VERSION \
  1044. TOML_MAKE_VERSION(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH)
  1045. #endif
  1046. #define TOML_LANG_HIGHER_THAN(major, minor, patch) \
  1047. (TOML_LANG_EFFECTIVE_VERSION > TOML_MAKE_VERSION(major, minor, patch))
  1048. #define TOML_LANG_AT_LEAST(major, minor, patch) \
  1049. (TOML_LANG_EFFECTIVE_VERSION >= TOML_MAKE_VERSION(major, minor, patch))
  1050. #define TOML_LANG_UNRELEASED \
  1051. TOML_LANG_HIGHER_THAN(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH)
  1052. #ifndef TOML_ABI_NAMESPACES
  1053. #if TOML_DOXYGEN
  1054. #define TOML_ABI_NAMESPACES 0
  1055. #else
  1056. #define TOML_ABI_NAMESPACES 1
  1057. #endif
  1058. #endif
  1059. #if TOML_ABI_NAMESPACES
  1060. #define TOML_NAMESPACE_START namespace toml { inline namespace TOML_CONCAT(v, TOML_LIB_MAJOR)
  1061. #define TOML_NAMESPACE_END } static_assert(true)
  1062. #define TOML_NAMESPACE ::toml::TOML_CONCAT(v, TOML_LIB_MAJOR)
  1063. #define TOML_ABI_NAMESPACE_START(name) inline namespace name { static_assert(true)
  1064. #define TOML_ABI_NAMESPACE_BOOL(cond, T, F) TOML_ABI_NAMESPACE_START(TOML_CONCAT(TOML_EVAL_BOOL_, cond)(T, F))
  1065. #define TOML_ABI_NAMESPACE_END } static_assert(true)
  1066. #else
  1067. #define TOML_NAMESPACE_START namespace toml
  1068. #define TOML_NAMESPACE_END static_assert(true)
  1069. #define TOML_NAMESPACE toml
  1070. #define TOML_ABI_NAMESPACE_START(...) static_assert(true)
  1071. #define TOML_ABI_NAMESPACE_BOOL(...) static_assert(true)
  1072. #define TOML_ABI_NAMESPACE_END static_assert(true)
  1073. #endif
  1074. #define TOML_IMPL_NAMESPACE_START TOML_NAMESPACE_START { namespace impl
  1075. #define TOML_IMPL_NAMESPACE_END } TOML_NAMESPACE_END
  1076. #if TOML_HEADER_ONLY
  1077. #define TOML_ANON_NAMESPACE_START static_assert(TOML_IMPLEMENTATION); TOML_IMPL_NAMESPACE_START
  1078. #define TOML_ANON_NAMESPACE_END TOML_IMPL_NAMESPACE_END
  1079. #define TOML_ANON_NAMESPACE TOML_NAMESPACE::impl
  1080. #define TOML_EXTERNAL_LINKAGE inline
  1081. #define TOML_INTERNAL_LINKAGE inline
  1082. #else
  1083. #define TOML_ANON_NAMESPACE_START static_assert(TOML_IMPLEMENTATION); \
  1084. using namespace toml; \
  1085. namespace
  1086. #define TOML_ANON_NAMESPACE_END static_assert(true)
  1087. #define TOML_ANON_NAMESPACE
  1088. #define TOML_EXTERNAL_LINKAGE
  1089. #define TOML_INTERNAL_LINKAGE static
  1090. #endif
  1091. // clang-format on
  1092. // clang-format off
  1093. #if TOML_SIMPLE_STATIC_ASSERT_MESSAGES
  1094. #define TOML_SA_NEWLINE " "
  1095. #define TOML_SA_LIST_SEP ", "
  1096. #define TOML_SA_LIST_BEG " ("
  1097. #define TOML_SA_LIST_END ")"
  1098. #define TOML_SA_LIST_NEW " "
  1099. #define TOML_SA_LIST_NXT ", "
  1100. #else
  1101. #define TOML_SA_NEWLINE "\n| "
  1102. #define TOML_SA_LIST_SEP TOML_SA_NEWLINE " - "
  1103. #define TOML_SA_LIST_BEG TOML_SA_LIST_SEP
  1104. #define TOML_SA_LIST_END
  1105. #define TOML_SA_LIST_NEW TOML_SA_NEWLINE TOML_SA_NEWLINE
  1106. #define TOML_SA_LIST_NXT TOML_SA_LIST_NEW
  1107. #endif
  1108. #define TOML_SA_NATIVE_VALUE_TYPE_LIST \
  1109. TOML_SA_LIST_BEG "std::string" \
  1110. TOML_SA_LIST_SEP "int64_t" \
  1111. TOML_SA_LIST_SEP "double" \
  1112. TOML_SA_LIST_SEP "bool" \
  1113. TOML_SA_LIST_SEP "toml::date" \
  1114. TOML_SA_LIST_SEP "toml::time" \
  1115. TOML_SA_LIST_SEP "toml::date_time" \
  1116. TOML_SA_LIST_END
  1117. #define TOML_SA_NODE_TYPE_LIST \
  1118. TOML_SA_LIST_BEG "toml::table" \
  1119. TOML_SA_LIST_SEP "toml::array" \
  1120. TOML_SA_LIST_SEP "toml::value<std::string>" \
  1121. TOML_SA_LIST_SEP "toml::value<int64_t>" \
  1122. TOML_SA_LIST_SEP "toml::value<double>" \
  1123. TOML_SA_LIST_SEP "toml::value<bool>" \
  1124. TOML_SA_LIST_SEP "toml::value<toml::date>" \
  1125. TOML_SA_LIST_SEP "toml::value<toml::time>" \
  1126. TOML_SA_LIST_SEP "toml::value<toml::date_time>" \
  1127. TOML_SA_LIST_END
  1128. #define TOML_SA_UNWRAPPED_NODE_TYPE_LIST \
  1129. TOML_SA_LIST_NEW "A native TOML value type" \
  1130. TOML_SA_NATIVE_VALUE_TYPE_LIST \
  1131. \
  1132. TOML_SA_LIST_NXT "A TOML node type" \
  1133. TOML_SA_NODE_TYPE_LIST
  1134. // clang-format on
  1135. TOML_PUSH_WARNINGS;
  1136. TOML_DISABLE_SPAM_WARNINGS;
  1137. TOML_DISABLE_SWITCH_WARNINGS;
  1138. TOML_DISABLE_SUGGEST_ATTR_WARNINGS;
  1139. // misc warning false-positives
  1140. #if TOML_MSVC
  1141. #pragma warning(disable : 5031) // #pragma warning(pop): likely mismatch
  1142. #if TOML_SHARED_LIB
  1143. #pragma warning(disable : 4251) // dll exports for std lib types
  1144. #endif
  1145. #elif TOML_CLANG
  1146. TOML_PRAGMA_CLANG(diagnostic ignored "-Wheader-hygiene")
  1147. #if TOML_CLANG >= 12
  1148. TOML_PRAGMA_CLANG(diagnostic ignored "-Wc++20-extensions")
  1149. #endif
  1150. #if TOML_CLANG == 13
  1151. TOML_PRAGMA_CLANG(diagnostic ignored "-Wreserved-identifier")
  1152. #endif
  1153. #endif
  1154. //******** impl/std_new.hpp ******************************************************************************************
  1155. TOML_DISABLE_WARNINGS;
  1156. #include <new>
  1157. TOML_ENABLE_WARNINGS;
  1158. #if (!defined(__apple_build_version__) && TOML_CLANG >= 8) || TOML_GCC >= 7 || TOML_ICC >= 1910 || TOML_MSVC >= 1914
  1159. #define TOML_LAUNDER(x) __builtin_launder(x)
  1160. #elif defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
  1161. #define TOML_LAUNDER(x) std::launder(x)
  1162. #else
  1163. #define TOML_LAUNDER(x) x
  1164. #endif
  1165. //******** impl/std_string.hpp ***************************************************************************************
  1166. TOML_DISABLE_WARNINGS;
  1167. #include <string_view>
  1168. #include <string>
  1169. TOML_ENABLE_WARNINGS;
  1170. #if TOML_DOXYGEN \
  1171. || (defined(__cpp_char8_t) && __cpp_char8_t >= 201811 && defined(__cpp_lib_char8_t) \
  1172. && __cpp_lib_char8_t >= 201907)
  1173. #define TOML_HAS_CHAR8 1
  1174. #else
  1175. #define TOML_HAS_CHAR8 0
  1176. #endif
  1177. namespace toml // non-abi namespace; this is not an error
  1178. {
  1179. using namespace std::string_literals;
  1180. using namespace std::string_view_literals;
  1181. }
  1182. #if TOML_ENABLE_WINDOWS_COMPAT
  1183. TOML_IMPL_NAMESPACE_START
  1184. {
  1185. TOML_NODISCARD
  1186. TOML_EXPORTED_FREE_FUNCTION
  1187. std::string narrow(std::wstring_view);
  1188. TOML_NODISCARD
  1189. TOML_EXPORTED_FREE_FUNCTION
  1190. std::wstring widen(std::string_view);
  1191. #if TOML_HAS_CHAR8
  1192. TOML_NODISCARD
  1193. TOML_EXPORTED_FREE_FUNCTION
  1194. std::wstring widen(std::u8string_view);
  1195. #endif
  1196. }
  1197. TOML_IMPL_NAMESPACE_END;
  1198. #endif // TOML_ENABLE_WINDOWS_COMPAT
  1199. //******** impl/std_optional.hpp *************************************************************************************
  1200. TOML_DISABLE_WARNINGS;
  1201. #if !TOML_HAS_CUSTOM_OPTIONAL_TYPE
  1202. #include <optional>
  1203. #endif
  1204. TOML_ENABLE_WARNINGS;
  1205. TOML_NAMESPACE_START
  1206. {
  1207. #if TOML_HAS_CUSTOM_OPTIONAL_TYPE
  1208. template <typename T>
  1209. using optional = TOML_OPTIONAL_TYPE<T>;
  1210. #else
  1211. template <typename T>
  1212. using optional = std::optional<T>;
  1213. #endif
  1214. }
  1215. TOML_NAMESPACE_END;
  1216. //******** impl/forward_declarations.hpp *****************************************************************************
  1217. TOML_DISABLE_WARNINGS;
  1218. #include <cstdint>
  1219. #include <cstddef>
  1220. #include <cstring>
  1221. #include <cfloat>
  1222. #include <climits>
  1223. #include <cmath>
  1224. #include <limits>
  1225. #include <memory>
  1226. #include <iosfwd>
  1227. #include <type_traits>
  1228. TOML_ENABLE_WARNINGS;
  1229. TOML_PUSH_WARNINGS;
  1230. #ifdef _MSC_VER
  1231. #ifndef __clang__
  1232. #pragma inline_recursion(on)
  1233. #endif
  1234. #pragma push_macro("min")
  1235. #pragma push_macro("max")
  1236. #undef min
  1237. #undef max
  1238. #endif
  1239. #ifndef TOML_DISABLE_ENVIRONMENT_CHECKS
  1240. #define TOML_ENV_MESSAGE \
  1241. "If you're seeing this error it's because you're building toml++ for an environment that doesn't conform to " \
  1242. "one of the 'ground truths' assumed by the library. Essentially this just means that I don't have the " \
  1243. "resources to test on more platforms, but I wish I did! You can try disabling the checks by defining " \
  1244. "TOML_DISABLE_ENVIRONMENT_CHECKS, but your mileage may vary. Please consider filing an issue at " \
  1245. "https://github.com/marzer/tomlplusplus/issues to help me improve support for your target environment. " \
  1246. "Thanks!"
  1247. static_assert(CHAR_BIT == 8, TOML_ENV_MESSAGE);
  1248. #ifdef FLT_RADIX
  1249. static_assert(FLT_RADIX == 2, TOML_ENV_MESSAGE);
  1250. #endif
  1251. static_assert('A' == 65, TOML_ENV_MESSAGE);
  1252. static_assert(sizeof(double) == 8, TOML_ENV_MESSAGE);
  1253. static_assert(std::numeric_limits<double>::is_iec559, TOML_ENV_MESSAGE);
  1254. static_assert(std::numeric_limits<double>::digits == 53, TOML_ENV_MESSAGE);
  1255. static_assert(std::numeric_limits<double>::digits10 == 15, TOML_ENV_MESSAGE);
  1256. static_assert(std::numeric_limits<double>::radix == 2, TOML_ENV_MESSAGE);
  1257. #undef TOML_ENV_MESSAGE
  1258. #endif // !TOML_DISABLE_ENVIRONMENT_CHECKS
  1259. // undocumented forward declarations are hidden from doxygen because they fuck it up =/
  1260. namespace toml // non-abi namespace; this is not an error
  1261. {
  1262. using ::std::size_t;
  1263. using ::std::intptr_t;
  1264. using ::std::uintptr_t;
  1265. using ::std::ptrdiff_t;
  1266. using ::std::nullptr_t;
  1267. using ::std::int8_t;
  1268. using ::std::int16_t;
  1269. using ::std::int32_t;
  1270. using ::std::int64_t;
  1271. using ::std::uint8_t;
  1272. using ::std::uint16_t;
  1273. using ::std::uint32_t;
  1274. using ::std::uint64_t;
  1275. using ::std::uint_least32_t;
  1276. using ::std::uint_least64_t;
  1277. }
  1278. TOML_NAMESPACE_START
  1279. {
  1280. struct date;
  1281. struct time;
  1282. struct time_offset;
  1283. TOML_ABI_NAMESPACE_BOOL(TOML_HAS_CUSTOM_OPTIONAL_TYPE, custopt, stdopt);
  1284. struct date_time;
  1285. TOML_ABI_NAMESPACE_END;
  1286. struct source_position;
  1287. struct source_region;
  1288. class node;
  1289. template <typename>
  1290. class node_view;
  1291. class key;
  1292. class array;
  1293. class table;
  1294. template <typename>
  1295. class value;
  1296. class path;
  1297. class toml_formatter;
  1298. class json_formatter;
  1299. class yaml_formatter;
  1300. TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, ex, noex);
  1301. #if TOML_EXCEPTIONS
  1302. using parse_result = table;
  1303. #else
  1304. class parse_result;
  1305. #endif
  1306. TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
  1307. }
  1308. TOML_NAMESPACE_END;
  1309. TOML_IMPL_NAMESPACE_START
  1310. {
  1311. using node_ptr = std::unique_ptr<node>;
  1312. TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, impl_ex, impl_noex);
  1313. class parser;
  1314. TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
  1315. // clang-format off
  1316. inline constexpr std::string_view control_char_escapes[] =
  1317. {
  1318. "\\u0000"sv,
  1319. "\\u0001"sv,
  1320. "\\u0002"sv,
  1321. "\\u0003"sv,
  1322. "\\u0004"sv,
  1323. "\\u0005"sv,
  1324. "\\u0006"sv,
  1325. "\\u0007"sv,
  1326. "\\b"sv,
  1327. "\\t"sv,
  1328. "\\n"sv,
  1329. "\\u000B"sv,
  1330. "\\f"sv,
  1331. "\\r"sv,
  1332. "\\u000E"sv,
  1333. "\\u000F"sv,
  1334. "\\u0010"sv,
  1335. "\\u0011"sv,
  1336. "\\u0012"sv,
  1337. "\\u0013"sv,
  1338. "\\u0014"sv,
  1339. "\\u0015"sv,
  1340. "\\u0016"sv,
  1341. "\\u0017"sv,
  1342. "\\u0018"sv,
  1343. "\\u0019"sv,
  1344. "\\u001A"sv,
  1345. "\\u001B"sv,
  1346. "\\u001C"sv,
  1347. "\\u001D"sv,
  1348. "\\u001E"sv,
  1349. "\\u001F"sv,
  1350. };
  1351. inline constexpr std::string_view node_type_friendly_names[] =
  1352. {
  1353. "none"sv,
  1354. "table"sv,
  1355. "array"sv,
  1356. "string"sv,
  1357. "integer"sv,
  1358. "floating-point"sv,
  1359. "boolean"sv,
  1360. "date"sv,
  1361. "time"sv,
  1362. "date-time"sv
  1363. };
  1364. // clang-format on
  1365. }
  1366. TOML_IMPL_NAMESPACE_END;
  1367. #if TOML_ABI_NAMESPACES
  1368. #if TOML_EXCEPTIONS
  1369. #define TOML_PARSER_TYPENAME TOML_NAMESPACE::impl::impl_ex::parser
  1370. #else
  1371. #define TOML_PARSER_TYPENAME TOML_NAMESPACE::impl::impl_noex::parser
  1372. #endif
  1373. #else
  1374. #define TOML_PARSER_TYPENAME TOML_NAMESPACE::impl::parser
  1375. #endif
  1376. namespace toml
  1377. {
  1378. }
  1379. TOML_NAMESPACE_START // abi namespace
  1380. {
  1381. inline namespace literals
  1382. {
  1383. }
  1384. enum class TOML_CLOSED_ENUM node_type : uint8_t
  1385. {
  1386. none,
  1387. table,
  1388. array,
  1389. string,
  1390. integer,
  1391. floating_point,
  1392. boolean,
  1393. date,
  1394. time,
  1395. date_time
  1396. };
  1397. template <typename Char>
  1398. inline std::basic_ostream<Char>& operator<<(std::basic_ostream<Char>& lhs, node_type rhs)
  1399. {
  1400. const auto str = impl::node_type_friendly_names[static_cast<std::underlying_type_t<node_type>>(rhs)];
  1401. using str_char_t = decltype(str)::value_type;
  1402. if constexpr (std::is_same_v<Char, str_char_t>)
  1403. return lhs << str;
  1404. else
  1405. {
  1406. if constexpr (sizeof(Char) == sizeof(str_char_t))
  1407. return lhs << std::basic_string_view<Char>{ reinterpret_cast<const Char*>(str.data()), str.length() };
  1408. else
  1409. return lhs << str.data();
  1410. }
  1411. }
  1412. enum class TOML_OPEN_FLAGS_ENUM value_flags : uint16_t // being an "OPEN" flags enum is not an error
  1413. {
  1414. none,
  1415. format_as_binary = 1,
  1416. format_as_octal = 2,
  1417. format_as_hexadecimal = 3,
  1418. };
  1419. TOML_MAKE_FLAGS(value_flags);
  1420. inline constexpr value_flags preserve_source_value_flags =
  1421. POXY_IMPLEMENTATION_DETAIL(value_flags{ static_cast<std::underlying_type_t<value_flags>>(-1) });
  1422. enum class TOML_CLOSED_FLAGS_ENUM format_flags : uint64_t
  1423. {
  1424. none,
  1425. quote_dates_and_times = (1ull << 0),
  1426. quote_infinities_and_nans = (1ull << 1),
  1427. allow_literal_strings = (1ull << 2),
  1428. allow_multi_line_strings = (1ull << 3),
  1429. allow_real_tabs_in_strings = (1ull << 4),
  1430. allow_unicode_strings = (1ull << 5),
  1431. allow_binary_integers = (1ull << 6),
  1432. allow_octal_integers = (1ull << 7),
  1433. allow_hexadecimal_integers = (1ull << 8),
  1434. indent_sub_tables = (1ull << 9),
  1435. indent_array_elements = (1ull << 10),
  1436. indentation = indent_sub_tables | indent_array_elements,
  1437. relaxed_float_precision = (1ull << 11),
  1438. terse_key_value_pairs = (1ull << 12),
  1439. };
  1440. TOML_MAKE_FLAGS(format_flags);
  1441. template <typename T>
  1442. struct TOML_TRIVIAL_ABI inserter
  1443. {
  1444. static_assert(std::is_reference_v<T>);
  1445. T value;
  1446. };
  1447. template <typename T>
  1448. inserter(T&&) -> inserter<T&&>;
  1449. template <typename T>
  1450. inserter(T&) -> inserter<T&>;
  1451. using default_formatter = toml_formatter;
  1452. }
  1453. TOML_NAMESPACE_END;
  1454. TOML_IMPL_NAMESPACE_START
  1455. {
  1456. template <typename T>
  1457. using remove_cvref = std::remove_cv_t<std::remove_reference_t<T>>;
  1458. template <typename... T>
  1459. using common_signed_type = std::common_type_t<std::make_signed_t<T>...>;
  1460. template <typename T, typename... U>
  1461. inline constexpr bool is_one_of = (false || ... || std::is_same_v<T, U>);
  1462. template <typename... T>
  1463. inline constexpr bool all_integral = (std::is_integral_v<T> && ...);
  1464. template <typename T>
  1465. inline constexpr bool is_cvref = std::is_reference_v<T> || std::is_const_v<T> || std::is_volatile_v<T>;
  1466. template <typename T>
  1467. inline constexpr bool is_wide_string =
  1468. is_one_of<std::decay_t<T>, const wchar_t*, wchar_t*, std::wstring_view, std::wstring>;
  1469. template <typename T>
  1470. inline constexpr bool value_retrieval_is_nothrow = !std::is_same_v<remove_cvref<T>, std::string>
  1471. #if TOML_HAS_CHAR8
  1472. && !std::is_same_v<remove_cvref<T>, std::u8string>
  1473. #endif
  1474. && !is_wide_string<T>;
  1475. template <typename, typename>
  1476. struct copy_ref_;
  1477. template <typename Dest, typename Src>
  1478. using copy_ref = typename copy_ref_<Dest, Src>::type;
  1479. template <typename Dest, typename Src>
  1480. struct copy_ref_
  1481. {
  1482. using type = Dest;
  1483. };
  1484. template <typename Dest, typename Src>
  1485. struct copy_ref_<Dest, Src&>
  1486. {
  1487. using type = std::add_lvalue_reference_t<Dest>;
  1488. };
  1489. template <typename Dest, typename Src>
  1490. struct copy_ref_<Dest, Src&&>
  1491. {
  1492. using type = std::add_rvalue_reference_t<Dest>;
  1493. };
  1494. template <typename, typename>
  1495. struct copy_cv_;
  1496. template <typename Dest, typename Src>
  1497. using copy_cv = typename copy_cv_<Dest, Src>::type;
  1498. template <typename Dest, typename Src>
  1499. struct copy_cv_
  1500. {
  1501. using type = Dest;
  1502. };
  1503. template <typename Dest, typename Src>
  1504. struct copy_cv_<Dest, const Src>
  1505. {
  1506. using type = std::add_const_t<Dest>;
  1507. };
  1508. template <typename Dest, typename Src>
  1509. struct copy_cv_<Dest, volatile Src>
  1510. {
  1511. using type = std::add_volatile_t<Dest>;
  1512. };
  1513. template <typename Dest, typename Src>
  1514. struct copy_cv_<Dest, const volatile Src>
  1515. {
  1516. using type = std::add_cv_t<Dest>;
  1517. };
  1518. template <typename Dest, typename Src>
  1519. using copy_cvref =
  1520. copy_ref<copy_ref<copy_cv<std::remove_reference_t<Dest>, std::remove_reference_t<Src>>, Dest>, Src>;
  1521. template <typename...>
  1522. inline constexpr bool always_false = false;
  1523. template <typename T, typename... U>
  1524. inline constexpr bool first_is_same = false;
  1525. template <typename T, typename... U>
  1526. inline constexpr bool first_is_same<T, T, U...> = true;
  1527. template <typename T, bool = std::is_enum_v<T>>
  1528. struct underlying_type_
  1529. {
  1530. using type = std::underlying_type_t<T>;
  1531. };
  1532. template <typename T>
  1533. struct underlying_type_<T, false>
  1534. {
  1535. using type = T;
  1536. };
  1537. template <typename T>
  1538. using underlying_type = typename underlying_type_<T>::type;
  1539. // general value traits
  1540. // (as they relate to their equivalent native TOML type)
  1541. struct default_value_traits
  1542. {
  1543. using native_type = void;
  1544. static constexpr bool is_native = false;
  1545. static constexpr bool is_losslessly_convertible_to_native = false;
  1546. static constexpr bool can_represent_native = false;
  1547. static constexpr bool can_partially_represent_native = false;
  1548. static constexpr auto type = node_type::none;
  1549. };
  1550. template <typename T>
  1551. struct value_traits;
  1552. template <typename T, bool = std::is_enum_v<T>>
  1553. struct value_traits_base_selector
  1554. {
  1555. static_assert(!is_cvref<T>);
  1556. using type = default_value_traits;
  1557. };
  1558. template <typename T>
  1559. struct value_traits_base_selector<T, true>
  1560. {
  1561. static_assert(!is_cvref<T>);
  1562. using type = value_traits<underlying_type<T>>;
  1563. };
  1564. template <typename T>
  1565. struct value_traits : value_traits_base_selector<T>::type
  1566. {};
  1567. template <typename T>
  1568. struct value_traits<const T> : value_traits<T>
  1569. {};
  1570. template <typename T>
  1571. struct value_traits<volatile T> : value_traits<T>
  1572. {};
  1573. template <typename T>
  1574. struct value_traits<const volatile T> : value_traits<T>
  1575. {};
  1576. template <typename T>
  1577. struct value_traits<T&> : value_traits<T>
  1578. {};
  1579. template <typename T>
  1580. struct value_traits<T&&> : value_traits<T>
  1581. {};
  1582. // integer value_traits specializations - standard types
  1583. template <typename T>
  1584. struct integer_limits
  1585. {
  1586. static constexpr T min = T{ (std::numeric_limits<underlying_type<T>>::min)() };
  1587. static constexpr T max = T{ (std::numeric_limits<underlying_type<T>>::max)() };
  1588. };
  1589. template <typename T>
  1590. struct integer_traits_base : integer_limits<T>
  1591. {
  1592. using native_type = int64_t;
  1593. static constexpr bool is_native = std::is_same_v<underlying_type<T>, native_type>;
  1594. static constexpr bool is_signed = static_cast<underlying_type<T>>(-1) < underlying_type<T>{};
  1595. static constexpr auto type = node_type::integer;
  1596. static constexpr bool can_partially_represent_native = true;
  1597. };
  1598. template <typename T>
  1599. struct unsigned_integer_traits : integer_traits_base<T>
  1600. {
  1601. static constexpr bool is_losslessly_convertible_to_native =
  1602. integer_limits<underlying_type<T>>::max <= 9223372036854775807ULL;
  1603. static constexpr bool can_represent_native = false;
  1604. };
  1605. template <typename T>
  1606. struct signed_integer_traits : integer_traits_base<T>
  1607. {
  1608. using native_type = int64_t;
  1609. static constexpr bool is_losslessly_convertible_to_native =
  1610. integer_limits<underlying_type<T>>::min >= (-9223372036854775807LL - 1LL)
  1611. && integer_limits<underlying_type<T>>::max <= 9223372036854775807LL;
  1612. static constexpr bool can_represent_native =
  1613. integer_limits<underlying_type<T>>::min <= (-9223372036854775807LL - 1LL)
  1614. && integer_limits<underlying_type<T>>::max >= 9223372036854775807LL;
  1615. };
  1616. template <typename T, bool S = integer_traits_base<T>::is_signed>
  1617. struct integer_traits : signed_integer_traits<T>
  1618. {};
  1619. template <typename T>
  1620. struct integer_traits<T, false> : unsigned_integer_traits<T>
  1621. {};
  1622. template <>
  1623. struct value_traits<signed char> : integer_traits<signed char>
  1624. {};
  1625. template <>
  1626. struct value_traits<unsigned char> : integer_traits<unsigned char>
  1627. {};
  1628. template <>
  1629. struct value_traits<signed short> : integer_traits<signed short>
  1630. {};
  1631. template <>
  1632. struct value_traits<unsigned short> : integer_traits<unsigned short>
  1633. {};
  1634. template <>
  1635. struct value_traits<signed int> : integer_traits<signed int>
  1636. {};
  1637. template <>
  1638. struct value_traits<unsigned int> : integer_traits<unsigned int>
  1639. {};
  1640. template <>
  1641. struct value_traits<signed long> : integer_traits<signed long>
  1642. {};
  1643. template <>
  1644. struct value_traits<unsigned long> : integer_traits<unsigned long>
  1645. {};
  1646. template <>
  1647. struct value_traits<signed long long> : integer_traits<signed long long>
  1648. {};
  1649. template <>
  1650. struct value_traits<unsigned long long> : integer_traits<unsigned long long>
  1651. {};
  1652. static_assert(value_traits<int64_t>::is_native);
  1653. static_assert(value_traits<int64_t>::is_signed);
  1654. static_assert(value_traits<int64_t>::is_losslessly_convertible_to_native);
  1655. static_assert(value_traits<int64_t>::can_represent_native);
  1656. static_assert(value_traits<int64_t>::can_partially_represent_native);
  1657. // integer value_traits specializations - non-standard types
  1658. #ifdef TOML_INT128
  1659. template <>
  1660. struct integer_limits<TOML_INT128>
  1661. {
  1662. static constexpr TOML_INT128 max =
  1663. static_cast<TOML_INT128>((TOML_UINT128{ 1u } << ((__SIZEOF_INT128__ * CHAR_BIT) - 1)) - 1);
  1664. static constexpr TOML_INT128 min = -max - TOML_INT128{ 1 };
  1665. };
  1666. template <>
  1667. struct integer_limits<TOML_UINT128>
  1668. {
  1669. static constexpr TOML_UINT128 min = TOML_UINT128{};
  1670. static constexpr TOML_UINT128 max = (2u * static_cast<TOML_UINT128>(integer_limits<TOML_INT128>::max)) + 1u;
  1671. };
  1672. template <>
  1673. struct value_traits<TOML_INT128> : integer_traits<TOML_INT128>
  1674. {};
  1675. template <>
  1676. struct value_traits<TOML_UINT128> : integer_traits<TOML_UINT128>
  1677. {};
  1678. #endif
  1679. #ifdef TOML_SMALL_INT_TYPE
  1680. template <>
  1681. struct value_traits<TOML_SMALL_INT_TYPE> : signed_integer_traits<TOML_SMALL_INT_TYPE>
  1682. {};
  1683. #endif
  1684. // floating-point traits base
  1685. template <typename T, int MantissaDigits, int DecimalDigits>
  1686. struct float_traits_base
  1687. {
  1688. static constexpr auto type = node_type::floating_point;
  1689. using native_type = double;
  1690. static constexpr bool is_native = std::is_same_v<T, native_type>;
  1691. static constexpr bool is_signed = true;
  1692. static constexpr int bits = static_cast<int>(sizeof(T) * CHAR_BIT);
  1693. static constexpr int digits = MantissaDigits;
  1694. static constexpr int digits10 = DecimalDigits;
  1695. static constexpr bool is_losslessly_convertible_to_native = bits <= 64 //
  1696. && digits <= 53 // DBL_MANT_DIG
  1697. && digits10 <= 15; // DBL_DIG
  1698. static constexpr bool can_represent_native = digits >= 53 // DBL_MANT_DIG
  1699. && digits10 >= 15; // DBL_DIG
  1700. static constexpr bool can_partially_represent_native = digits > 0 && digits10 > 0;
  1701. };
  1702. template <typename T>
  1703. struct float_traits : float_traits_base<T, std::numeric_limits<T>::digits, std::numeric_limits<T>::digits10>
  1704. {};
  1705. #if TOML_ENABLE_FLOAT16
  1706. template <>
  1707. struct float_traits<_Float16> : float_traits_base<_Float16, __FLT16_MANT_DIG__, __FLT16_DIG__>
  1708. {};
  1709. #endif
  1710. #ifdef TOML_FLOAT128
  1711. template <>
  1712. struct float_traits<TOML_FLOAT128> : float_traits_base<TOML_FLOAT128, __FLT128_MANT_DIG__, __FLT128_DIG__>
  1713. {};
  1714. #endif
  1715. // floating-point traits
  1716. template <>
  1717. struct value_traits<float> : float_traits<float>
  1718. {};
  1719. template <>
  1720. struct value_traits<double> : float_traits<double>
  1721. {};
  1722. template <>
  1723. struct value_traits<long double> : float_traits<long double>
  1724. {};
  1725. #if TOML_ENABLE_FLOAT16
  1726. template <>
  1727. struct value_traits<_Float16> : float_traits<_Float16>
  1728. {};
  1729. #endif
  1730. #ifdef TOML_FLOAT128
  1731. template <>
  1732. struct value_traits<TOML_FLOAT128> : float_traits<TOML_FLOAT128>
  1733. {};
  1734. #endif
  1735. #ifdef TOML_SMALL_FLOAT_TYPE
  1736. template <>
  1737. struct value_traits<TOML_SMALL_FLOAT_TYPE> : float_traits<TOML_SMALL_FLOAT_TYPE>
  1738. {};
  1739. #endif
  1740. static_assert(value_traits<double>::is_native);
  1741. static_assert(value_traits<double>::is_losslessly_convertible_to_native);
  1742. static_assert(value_traits<double>::can_represent_native);
  1743. static_assert(value_traits<double>::can_partially_represent_native);
  1744. // string value_traits specializations - char-based strings
  1745. template <typename T>
  1746. struct string_traits
  1747. {
  1748. using native_type = std::string;
  1749. static constexpr bool is_native = std::is_same_v<T, native_type>;
  1750. static constexpr bool is_losslessly_convertible_to_native = true;
  1751. static constexpr bool can_represent_native =
  1752. !std::is_array_v<T> && (!std::is_pointer_v<T> || std::is_const_v<std::remove_pointer_t<T>>);
  1753. static constexpr bool can_partially_represent_native = can_represent_native;
  1754. static constexpr auto type = node_type::string;
  1755. };
  1756. template <>
  1757. struct value_traits<std::string> : string_traits<std::string>
  1758. {};
  1759. template <>
  1760. struct value_traits<std::string_view> : string_traits<std::string_view>
  1761. {};
  1762. template <>
  1763. struct value_traits<const char*> : string_traits<const char*>
  1764. {};
  1765. template <size_t N>
  1766. struct value_traits<const char[N]> : string_traits<const char[N]>
  1767. {};
  1768. template <>
  1769. struct value_traits<char*> : string_traits<char*>
  1770. {};
  1771. template <size_t N>
  1772. struct value_traits<char[N]> : string_traits<char[N]>
  1773. {};
  1774. // string value_traits specializations - char8_t-based strings
  1775. #if TOML_HAS_CHAR8
  1776. template <>
  1777. struct value_traits<std::u8string> : string_traits<std::u8string>
  1778. {};
  1779. template <>
  1780. struct value_traits<std::u8string_view> : string_traits<std::u8string_view>
  1781. {};
  1782. template <>
  1783. struct value_traits<const char8_t*> : string_traits<const char8_t*>
  1784. {};
  1785. template <size_t N>
  1786. struct value_traits<const char8_t[N]> : string_traits<const char8_t[N]>
  1787. {};
  1788. template <>
  1789. struct value_traits<char8_t*> : string_traits<char8_t*>
  1790. {};
  1791. template <size_t N>
  1792. struct value_traits<char8_t[N]> : string_traits<char8_t[N]>
  1793. {};
  1794. #endif
  1795. // string value_traits specializations - wchar_t-based strings on Windows
  1796. #if TOML_ENABLE_WINDOWS_COMPAT
  1797. template <typename T>
  1798. struct wstring_traits
  1799. {
  1800. using native_type = std::string;
  1801. static constexpr bool is_native = false;
  1802. static constexpr bool is_losslessly_convertible_to_native = true; // narrow
  1803. static constexpr bool can_represent_native = std::is_same_v<T, std::wstring>; // widen
  1804. static constexpr bool can_partially_represent_native = can_represent_native;
  1805. static constexpr auto type = node_type::string;
  1806. };
  1807. template <>
  1808. struct value_traits<std::wstring> : wstring_traits<std::wstring>
  1809. {};
  1810. template <>
  1811. struct value_traits<std::wstring_view> : wstring_traits<std::wstring_view>
  1812. {};
  1813. template <>
  1814. struct value_traits<const wchar_t*> : wstring_traits<const wchar_t*>
  1815. {};
  1816. template <size_t N>
  1817. struct value_traits<const wchar_t[N]> : wstring_traits<const wchar_t[N]>
  1818. {};
  1819. template <>
  1820. struct value_traits<wchar_t*> : wstring_traits<wchar_t*>
  1821. {};
  1822. template <size_t N>
  1823. struct value_traits<wchar_t[N]> : wstring_traits<wchar_t[N]>
  1824. {};
  1825. #endif
  1826. // other 'native' value_traits specializations
  1827. template <typename T, node_type NodeType>
  1828. struct native_value_traits
  1829. {
  1830. using native_type = T;
  1831. static constexpr bool is_native = true;
  1832. static constexpr bool is_losslessly_convertible_to_native = true;
  1833. static constexpr bool can_represent_native = true;
  1834. static constexpr bool can_partially_represent_native = true;
  1835. static constexpr auto type = NodeType;
  1836. };
  1837. template <>
  1838. struct value_traits<bool> : native_value_traits<bool, node_type::boolean>
  1839. {};
  1840. template <>
  1841. struct value_traits<date> : native_value_traits<date, node_type::date>
  1842. {};
  1843. template <>
  1844. struct value_traits<time> : native_value_traits<time, node_type::time>
  1845. {};
  1846. template <>
  1847. struct value_traits<date_time> : native_value_traits<date_time, node_type::date_time>
  1848. {};
  1849. // native value category queries
  1850. template <typename T>
  1851. using native_type_of = typename value_traits<T>::native_type;
  1852. template <typename T>
  1853. inline constexpr bool is_native = value_traits<T>::is_native;
  1854. template <typename T>
  1855. inline constexpr bool can_represent_native = value_traits<T>::can_represent_native;
  1856. template <typename T>
  1857. inline constexpr bool can_partially_represent_native = value_traits<T>::can_partially_represent_native;
  1858. template <typename T>
  1859. inline constexpr bool is_losslessly_convertible_to_native = value_traits<T>::is_losslessly_convertible_to_native;
  1860. template <typename T, typename... U>
  1861. inline constexpr bool is_natively_one_of = is_one_of<native_type_of<T>, U...>;
  1862. // native value types => nodes
  1863. template <typename T>
  1864. struct node_wrapper
  1865. {
  1866. using type = T;
  1867. };
  1868. template <typename T>
  1869. struct node_wrapper<const T>
  1870. {
  1871. using type = std::add_const_t<typename node_wrapper<T>::type>;
  1872. };
  1873. template <typename T>
  1874. struct node_wrapper<volatile T>
  1875. {
  1876. using type = std::add_volatile_t<typename node_wrapper<T>::type>;
  1877. };
  1878. template <typename T>
  1879. struct node_wrapper<const volatile T>
  1880. {
  1881. using type = std::add_const_t<std::add_volatile_t<typename node_wrapper<T>::type>>;
  1882. };
  1883. template <>
  1884. struct node_wrapper<std::string>
  1885. {
  1886. using type = value<std::string>;
  1887. };
  1888. template <>
  1889. struct node_wrapper<int64_t>
  1890. {
  1891. using type = value<int64_t>;
  1892. };
  1893. template <>
  1894. struct node_wrapper<double>
  1895. {
  1896. using type = value<double>;
  1897. };
  1898. template <>
  1899. struct node_wrapper<bool>
  1900. {
  1901. using type = value<bool>;
  1902. };
  1903. template <>
  1904. struct node_wrapper<date>
  1905. {
  1906. using type = value<date>;
  1907. };
  1908. template <>
  1909. struct node_wrapper<time>
  1910. {
  1911. using type = value<time>;
  1912. };
  1913. template <>
  1914. struct node_wrapper<date_time>
  1915. {
  1916. using type = value<date_time>;
  1917. };
  1918. template <typename T>
  1919. using wrap_node = typename node_wrapper<std::remove_reference_t<T>>::type;
  1920. // nodes => native value types
  1921. template <typename T>
  1922. struct node_unwrapper
  1923. {
  1924. using type = T;
  1925. };
  1926. template <typename T>
  1927. struct node_unwrapper<value<T>>
  1928. {
  1929. using type = T;
  1930. };
  1931. template <typename T>
  1932. struct node_unwrapper<const value<T>>
  1933. {
  1934. using type = std::add_const_t<T>;
  1935. };
  1936. template <typename T>
  1937. struct node_unwrapper<volatile value<T>>
  1938. {
  1939. using type = std::add_volatile_t<T>;
  1940. };
  1941. template <typename T>
  1942. struct node_unwrapper<const volatile value<T>>
  1943. {
  1944. using type = std::add_volatile_t<std::add_const_t<T>>;
  1945. };
  1946. template <typename T>
  1947. using unwrap_node = typename node_unwrapper<std::remove_reference_t<T>>::type;
  1948. template <typename T>
  1949. struct node_type_getter
  1950. {
  1951. static constexpr auto value = value_traits<T>::type;
  1952. };
  1953. template <>
  1954. struct node_type_getter<table>
  1955. {
  1956. static constexpr auto value = node_type::table;
  1957. };
  1958. template <>
  1959. struct node_type_getter<array>
  1960. {
  1961. static constexpr auto value = node_type::array;
  1962. };
  1963. template <>
  1964. struct node_type_getter<void>
  1965. {
  1966. static constexpr auto value = node_type::none;
  1967. };
  1968. template <typename T>
  1969. inline constexpr node_type node_type_of = node_type_getter<unwrap_node<remove_cvref<T>>>::value;
  1970. template <typename T, typename ConvertFrom>
  1971. inline constexpr bool is_constructible_or_convertible = std::is_constructible_v<T, ConvertFrom> //
  1972. || std::is_convertible_v<ConvertFrom, T>;
  1973. }
  1974. TOML_IMPL_NAMESPACE_END;
  1975. TOML_NAMESPACE_START
  1976. {
  1977. template <typename T>
  1978. inline constexpr bool is_table = std::is_same_v<impl::remove_cvref<T>, table>;
  1979. template <typename T>
  1980. inline constexpr bool is_array = std::is_same_v<impl::remove_cvref<T>, array>;
  1981. template <typename T>
  1982. inline constexpr bool is_container = is_table<T> || is_array<T>;
  1983. template <typename T>
  1984. inline constexpr bool is_string = std::is_same_v< //
  1985. impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
  1986. value<std::string>>;
  1987. template <typename T>
  1988. inline constexpr bool is_integer = std::is_same_v< //
  1989. impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
  1990. value<int64_t>>;
  1991. template <typename T>
  1992. inline constexpr bool is_floating_point = std::is_same_v< //
  1993. impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
  1994. value<double>>;
  1995. template <typename T>
  1996. inline constexpr bool is_number = is_integer<T> || is_floating_point<T>;
  1997. template <typename T>
  1998. inline constexpr bool is_boolean = std::is_same_v< //
  1999. impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
  2000. value<bool>>;
  2001. template <typename T>
  2002. inline constexpr bool is_date = std::is_same_v< //
  2003. impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
  2004. value<date>>;
  2005. template <typename T>
  2006. inline constexpr bool is_time = std::is_same_v< //
  2007. impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
  2008. value<time>>;
  2009. template <typename T>
  2010. inline constexpr bool is_date_time = std::is_same_v< //
  2011. impl::remove_cvref<impl::wrap_node<impl::remove_cvref<T>>>, //
  2012. value<date_time>>;
  2013. template <typename T>
  2014. inline constexpr bool is_chronological = is_date<T> || is_time<T> || is_date_time<T>;
  2015. template <typename T>
  2016. inline constexpr bool is_value = is_string<T> || is_number<T> || is_boolean<T> || is_chronological<T>;
  2017. template <typename T>
  2018. inline constexpr bool is_node = std::is_same_v<toml::node, impl::remove_cvref<T>> //
  2019. || std::is_base_of_v<toml::node, impl::remove_cvref<T>>;
  2020. template <typename T>
  2021. inline constexpr bool is_node_view = impl::is_one_of<impl::remove_cvref<T>, node_view<node>, node_view<const node>>;
  2022. }
  2023. TOML_NAMESPACE_END;
  2024. TOML_IMPL_NAMESPACE_START
  2025. {
  2026. template <typename T>
  2027. TOML_CONST_INLINE_GETTER
  2028. constexpr std::underlying_type_t<T> unwrap_enum(T val) noexcept
  2029. {
  2030. return static_cast<std::underlying_type_t<T>>(val);
  2031. }
  2032. // Q: "why not use std::fpclassify?"
  2033. // A: Because it gets broken by -ffast-math and friends
  2034. enum class TOML_CLOSED_ENUM fp_class : unsigned
  2035. {
  2036. ok,
  2037. neg_inf,
  2038. pos_inf,
  2039. nan
  2040. };
  2041. TOML_PURE_GETTER
  2042. inline fp_class fpclassify(const double& val) noexcept
  2043. {
  2044. static_assert(sizeof(uint64_t) == sizeof(double));
  2045. static constexpr uint64_t sign = 0b1000000000000000000000000000000000000000000000000000000000000000ull;
  2046. static constexpr uint64_t exponent = 0b0111111111110000000000000000000000000000000000000000000000000000ull;
  2047. static constexpr uint64_t mantissa = 0b0000000000001111111111111111111111111111111111111111111111111111ull;
  2048. uint64_t val_bits;
  2049. std::memcpy(&val_bits, &val, sizeof(val));
  2050. if ((val_bits & exponent) != exponent)
  2051. return fp_class::ok;
  2052. if ((val_bits & mantissa))
  2053. return fp_class::nan;
  2054. return (val_bits & sign) ? fp_class::neg_inf : fp_class::pos_inf;
  2055. }
  2056. // Q: "why not use std::find and std::min?"
  2057. // A: Because <algorithm> is _huge_ and these would be the only things I used from it.
  2058. // I don't want to impose such a heavy compile-time burden on users.
  2059. template <typename Iterator, typename T>
  2060. TOML_PURE_GETTER
  2061. inline auto find(Iterator start, Iterator end, const T& needle) noexcept //
  2062. ->decltype(&(*start))
  2063. {
  2064. for (; start != end; start++)
  2065. if (*start == needle)
  2066. return &(*start);
  2067. return nullptr;
  2068. }
  2069. template <typename T>
  2070. TOML_PURE_INLINE_GETTER
  2071. constexpr const T& min(const T& a, const T& b) noexcept //
  2072. {
  2073. return a < b ? a : b;
  2074. }
  2075. }
  2076. TOML_IMPL_NAMESPACE_END;
  2077. #ifdef _MSC_VER
  2078. #pragma pop_macro("min")
  2079. #pragma pop_macro("max")
  2080. #ifndef __clang__
  2081. #pragma inline_recursion(off)
  2082. #endif
  2083. #endif
  2084. TOML_POP_WARNINGS;
  2085. //******** impl/print_to_stream.hpp **********************************************************************************
  2086. TOML_PUSH_WARNINGS;
  2087. #ifdef _MSC_VER
  2088. #ifndef __clang__
  2089. #pragma inline_recursion(on)
  2090. #endif
  2091. #pragma push_macro("min")
  2092. #pragma push_macro("max")
  2093. #undef min
  2094. #undef max
  2095. #endif
  2096. TOML_IMPL_NAMESPACE_START
  2097. {
  2098. // Q: "why does print_to_stream() exist? why not just use ostream::write(), ostream::put() etc?"
  2099. // A: - I'm using <charconv> to format numerics. Faster and locale-independent.
  2100. // - I can (potentially) avoid forcing users to drag in <sstream> and <iomanip>.
  2101. // - Strings in C++. Honestly.
  2102. TOML_EXPORTED_FREE_FUNCTION
  2103. TOML_ATTR(nonnull)
  2104. void TOML_CALLCONV print_to_stream(std::ostream&, const char*, size_t);
  2105. TOML_EXPORTED_FREE_FUNCTION
  2106. void TOML_CALLCONV print_to_stream(std::ostream&, std::string_view);
  2107. TOML_EXPORTED_FREE_FUNCTION
  2108. void TOML_CALLCONV print_to_stream(std::ostream&, const std::string&);
  2109. TOML_EXPORTED_FREE_FUNCTION
  2110. void TOML_CALLCONV print_to_stream(std::ostream&, char);
  2111. TOML_EXPORTED_FREE_FUNCTION
  2112. void TOML_CALLCONV print_to_stream(std::ostream&, signed char, value_flags = {}, size_t min_digits = 0);
  2113. TOML_EXPORTED_FREE_FUNCTION
  2114. void TOML_CALLCONV print_to_stream(std::ostream&, signed short, value_flags = {}, size_t min_digits = 0);
  2115. TOML_EXPORTED_FREE_FUNCTION
  2116. void TOML_CALLCONV print_to_stream(std::ostream&, signed int, value_flags = {}, size_t min_digits = 0);
  2117. TOML_EXPORTED_FREE_FUNCTION
  2118. void TOML_CALLCONV print_to_stream(std::ostream&, signed long, value_flags = {}, size_t min_digits = 0);
  2119. TOML_EXPORTED_FREE_FUNCTION
  2120. void TOML_CALLCONV print_to_stream(std::ostream&, signed long long, value_flags = {}, size_t min_digits = 0);
  2121. TOML_EXPORTED_FREE_FUNCTION
  2122. void TOML_CALLCONV print_to_stream(std::ostream&, unsigned char, value_flags = {}, size_t min_digits = 0);
  2123. TOML_EXPORTED_FREE_FUNCTION
  2124. void TOML_CALLCONV print_to_stream(std::ostream&, unsigned short, value_flags = {}, size_t min_digits = 0);
  2125. TOML_EXPORTED_FREE_FUNCTION
  2126. void TOML_CALLCONV print_to_stream(std::ostream&, unsigned int, value_flags = {}, size_t min_digits = 0);
  2127. TOML_EXPORTED_FREE_FUNCTION
  2128. void TOML_CALLCONV print_to_stream(std::ostream&, unsigned long, value_flags = {}, size_t min_digits = 0);
  2129. TOML_EXPORTED_FREE_FUNCTION
  2130. void TOML_CALLCONV print_to_stream(std::ostream&, unsigned long long, value_flags = {}, size_t min_digits = 0);
  2131. TOML_EXPORTED_FREE_FUNCTION
  2132. void TOML_CALLCONV print_to_stream(std::ostream&, float, value_flags = {}, bool relaxed_precision = false);
  2133. TOML_EXPORTED_FREE_FUNCTION
  2134. void TOML_CALLCONV print_to_stream(std::ostream&, double, value_flags = {}, bool relaxed_precision = false);
  2135. TOML_EXPORTED_FREE_FUNCTION
  2136. void TOML_CALLCONV print_to_stream(std::ostream&, bool);
  2137. TOML_EXPORTED_FREE_FUNCTION
  2138. void TOML_CALLCONV print_to_stream(std::ostream&, const toml::date&);
  2139. TOML_EXPORTED_FREE_FUNCTION
  2140. void TOML_CALLCONV print_to_stream(std::ostream&, const toml::time&);
  2141. TOML_EXPORTED_FREE_FUNCTION
  2142. void TOML_CALLCONV print_to_stream(std::ostream&, const toml::time_offset&);
  2143. TOML_EXPORTED_FREE_FUNCTION
  2144. void TOML_CALLCONV print_to_stream(std::ostream&, const toml::date_time&);
  2145. TOML_EXPORTED_FREE_FUNCTION
  2146. void TOML_CALLCONV print_to_stream(std::ostream&, const source_position&);
  2147. TOML_EXPORTED_FREE_FUNCTION
  2148. void TOML_CALLCONV print_to_stream(std::ostream&, const source_region&);
  2149. #if TOML_ENABLE_FORMATTERS
  2150. TOML_EXPORTED_FREE_FUNCTION
  2151. void TOML_CALLCONV print_to_stream(std::ostream&, const array&);
  2152. TOML_EXPORTED_FREE_FUNCTION
  2153. void TOML_CALLCONV print_to_stream(std::ostream&, const table&);
  2154. TOML_EXPORTED_FREE_FUNCTION
  2155. void TOML_CALLCONV print_to_stream(std::ostream&, const value<std::string>&);
  2156. TOML_EXPORTED_FREE_FUNCTION
  2157. void TOML_CALLCONV print_to_stream(std::ostream&, const value<int64_t>&);
  2158. TOML_EXPORTED_FREE_FUNCTION
  2159. void TOML_CALLCONV print_to_stream(std::ostream&, const value<double>&);
  2160. TOML_EXPORTED_FREE_FUNCTION
  2161. void TOML_CALLCONV print_to_stream(std::ostream&, const value<bool>&);
  2162. TOML_EXPORTED_FREE_FUNCTION
  2163. void TOML_CALLCONV print_to_stream(std::ostream&, const value<date>&);
  2164. TOML_EXPORTED_FREE_FUNCTION
  2165. void TOML_CALLCONV print_to_stream(std::ostream&, const value<time>&);
  2166. TOML_EXPORTED_FREE_FUNCTION
  2167. void TOML_CALLCONV print_to_stream(std::ostream&, const value<date_time>&);
  2168. #endif
  2169. template <typename T, typename U>
  2170. inline void print_to_stream_bookended(std::ostream & stream, const T& val, const U& bookend)
  2171. {
  2172. print_to_stream(stream, bookend);
  2173. print_to_stream(stream, val);
  2174. print_to_stream(stream, bookend);
  2175. }
  2176. }
  2177. TOML_IMPL_NAMESPACE_END;
  2178. #ifdef _MSC_VER
  2179. #pragma pop_macro("min")
  2180. #pragma pop_macro("max")
  2181. #ifndef __clang__
  2182. #pragma inline_recursion(off)
  2183. #endif
  2184. #endif
  2185. TOML_POP_WARNINGS;
  2186. //******** impl/source_region.hpp ************************************************************************************
  2187. TOML_PUSH_WARNINGS;
  2188. #ifdef _MSC_VER
  2189. #ifndef __clang__
  2190. #pragma inline_recursion(on)
  2191. #endif
  2192. #pragma push_macro("min")
  2193. #pragma push_macro("max")
  2194. #undef min
  2195. #undef max
  2196. #endif
  2197. TOML_NAMESPACE_START
  2198. {
  2199. using source_index = uint32_t;
  2200. using source_path_ptr = std::shared_ptr<const std::string>;
  2201. struct TOML_TRIVIAL_ABI source_position
  2202. {
  2203. source_index line;
  2204. source_index column;
  2205. TOML_PURE_GETTER
  2206. explicit constexpr operator bool() const noexcept
  2207. {
  2208. return line > source_index{} //
  2209. && column > source_index{};
  2210. }
  2211. TOML_PURE_GETTER
  2212. friend constexpr bool operator==(const source_position& lhs, const source_position& rhs) noexcept
  2213. {
  2214. return lhs.line == rhs.line //
  2215. && lhs.column == rhs.column;
  2216. }
  2217. TOML_PURE_INLINE_GETTER
  2218. friend constexpr bool operator!=(const source_position& lhs, const source_position& rhs) noexcept
  2219. {
  2220. return !(lhs == rhs);
  2221. }
  2222. private:
  2223. TOML_PURE_GETTER
  2224. static constexpr uint64_t pack(const source_position& pos) noexcept
  2225. {
  2226. return static_cast<uint64_t>(pos.line) << 32 | static_cast<uint64_t>(pos.column);
  2227. }
  2228. public:
  2229. TOML_PURE_GETTER
  2230. friend constexpr bool operator<(const source_position& lhs, const source_position& rhs) noexcept
  2231. {
  2232. return pack(lhs) < pack(rhs);
  2233. }
  2234. TOML_PURE_GETTER
  2235. friend constexpr bool operator<=(const source_position& lhs, const source_position& rhs) noexcept
  2236. {
  2237. return pack(lhs) <= pack(rhs);
  2238. }
  2239. TOML_PURE_GETTER
  2240. friend constexpr bool operator>(const source_position& lhs, const source_position& rhs) noexcept
  2241. {
  2242. return pack(lhs) > pack(rhs);
  2243. }
  2244. TOML_PURE_GETTER
  2245. friend constexpr bool operator>=(const source_position& lhs, const source_position& rhs) noexcept
  2246. {
  2247. return pack(lhs) >= pack(rhs);
  2248. }
  2249. friend std::ostream& operator<<(std::ostream& lhs, const source_position& rhs)
  2250. {
  2251. impl::print_to_stream(lhs, rhs);
  2252. return lhs;
  2253. }
  2254. };
  2255. struct source_region
  2256. {
  2257. source_position begin;
  2258. source_position end;
  2259. source_path_ptr path;
  2260. #if TOML_ENABLE_WINDOWS_COMPAT
  2261. TOML_NODISCARD
  2262. optional<std::wstring> wide_path() const
  2263. {
  2264. if (!path || path->empty())
  2265. return {};
  2266. return { impl::widen(*path) };
  2267. }
  2268. #endif
  2269. friend std::ostream& operator<<(std::ostream& lhs, const source_region& rhs)
  2270. {
  2271. impl::print_to_stream(lhs, rhs);
  2272. return lhs;
  2273. }
  2274. };
  2275. }
  2276. TOML_NAMESPACE_END;
  2277. #ifdef _MSC_VER
  2278. #pragma pop_macro("min")
  2279. #pragma pop_macro("max")
  2280. #ifndef __clang__
  2281. #pragma inline_recursion(off)
  2282. #endif
  2283. #endif
  2284. TOML_POP_WARNINGS;
  2285. //******** impl/date_time.hpp ****************************************************************************************
  2286. TOML_PUSH_WARNINGS;
  2287. #ifdef _MSC_VER
  2288. #ifndef __clang__
  2289. #pragma inline_recursion(on)
  2290. #endif
  2291. #pragma push_macro("min")
  2292. #pragma push_macro("max")
  2293. #undef min
  2294. #undef max
  2295. #endif
  2296. TOML_NAMESPACE_START
  2297. {
  2298. struct TOML_TRIVIAL_ABI date
  2299. {
  2300. uint16_t year;
  2301. uint8_t month;
  2302. uint8_t day;
  2303. TOML_NODISCARD_CTOR
  2304. date() noexcept = default;
  2305. TOML_CONSTRAINED_TEMPLATE((impl::all_integral<Y, M, D>), typename Y, typename M, typename D)
  2306. TOML_NODISCARD_CTOR
  2307. constexpr date(Y y, M m, D d) noexcept //
  2308. : year{ static_cast<uint16_t>(y) },
  2309. month{ static_cast<uint8_t>(m) },
  2310. day{ static_cast<uint8_t>(d) }
  2311. {}
  2312. TOML_PURE_GETTER
  2313. friend constexpr bool operator==(const date& lhs, const date& rhs) noexcept
  2314. {
  2315. return lhs.year == rhs.year //
  2316. && lhs.month == rhs.month //
  2317. && lhs.day == rhs.day;
  2318. }
  2319. TOML_PURE_INLINE_GETTER
  2320. friend constexpr bool operator!=(const date& lhs, const date& rhs) noexcept
  2321. {
  2322. return !(lhs == rhs);
  2323. }
  2324. private:
  2325. TOML_PURE_GETTER
  2326. static constexpr uint32_t pack(const date& d) noexcept
  2327. {
  2328. return (static_cast<uint32_t>(d.year) << 16) | (static_cast<uint32_t>(d.month) << 8)
  2329. | static_cast<uint32_t>(d.day);
  2330. }
  2331. public:
  2332. TOML_PURE_GETTER
  2333. friend constexpr bool operator<(const date& lhs, const date& rhs) noexcept
  2334. {
  2335. return pack(lhs) < pack(rhs);
  2336. }
  2337. TOML_PURE_GETTER
  2338. friend constexpr bool operator<=(const date& lhs, const date& rhs) noexcept
  2339. {
  2340. return pack(lhs) <= pack(rhs);
  2341. }
  2342. TOML_PURE_GETTER
  2343. friend constexpr bool operator>(const date& lhs, const date& rhs) noexcept
  2344. {
  2345. return pack(lhs) > pack(rhs);
  2346. }
  2347. TOML_PURE_GETTER
  2348. friend constexpr bool operator>=(const date& lhs, const date& rhs) noexcept
  2349. {
  2350. return pack(lhs) >= pack(rhs);
  2351. }
  2352. friend std::ostream& operator<<(std::ostream& lhs, const date& rhs)
  2353. {
  2354. impl::print_to_stream(lhs, rhs);
  2355. return lhs;
  2356. }
  2357. };
  2358. struct TOML_TRIVIAL_ABI time
  2359. {
  2360. uint8_t hour;
  2361. uint8_t minute;
  2362. uint8_t second;
  2363. uint32_t nanosecond;
  2364. TOML_NODISCARD_CTOR
  2365. time() noexcept = default;
  2366. TOML_CONSTRAINED_TEMPLATE((impl::all_integral<H, M, S, NS>),
  2367. typename H,
  2368. typename M,
  2369. typename S = uint8_t,
  2370. typename NS = uint32_t)
  2371. TOML_NODISCARD_CTOR
  2372. constexpr time(H h, M m, S s = S{}, NS ns = NS{}) noexcept //
  2373. : hour{ static_cast<uint8_t>(h) },
  2374. minute{ static_cast<uint8_t>(m) },
  2375. second{ static_cast<uint8_t>(s) },
  2376. nanosecond{ static_cast<uint32_t>(ns) }
  2377. {}
  2378. TOML_PURE_GETTER
  2379. friend constexpr bool operator==(const time& lhs, const time& rhs) noexcept
  2380. {
  2381. return lhs.hour == rhs.hour //
  2382. && lhs.minute == rhs.minute //
  2383. && lhs.second == rhs.second //
  2384. && lhs.nanosecond == rhs.nanosecond;
  2385. }
  2386. TOML_PURE_INLINE_GETTER
  2387. friend constexpr bool operator!=(const time& lhs, const time& rhs) noexcept
  2388. {
  2389. return !(lhs == rhs);
  2390. }
  2391. private:
  2392. TOML_PURE_GETTER
  2393. static constexpr uint64_t pack(const time& t) noexcept
  2394. {
  2395. return static_cast<uint64_t>(t.hour) << 48 | static_cast<uint64_t>(t.minute) << 40
  2396. | static_cast<uint64_t>(t.second) << 32 | static_cast<uint64_t>(t.nanosecond);
  2397. }
  2398. public:
  2399. TOML_PURE_GETTER
  2400. friend constexpr bool operator<(const time& lhs, const time& rhs) noexcept
  2401. {
  2402. return pack(lhs) < pack(rhs);
  2403. }
  2404. TOML_PURE_GETTER
  2405. friend constexpr bool operator<=(const time& lhs, const time& rhs) noexcept
  2406. {
  2407. return pack(lhs) <= pack(rhs);
  2408. }
  2409. TOML_PURE_GETTER
  2410. friend constexpr bool operator>(const time& lhs, const time& rhs) noexcept
  2411. {
  2412. return pack(lhs) > pack(rhs);
  2413. }
  2414. TOML_PURE_GETTER
  2415. friend constexpr bool operator>=(const time& lhs, const time& rhs) noexcept
  2416. {
  2417. return pack(lhs) >= pack(rhs);
  2418. }
  2419. friend std::ostream& operator<<(std::ostream& lhs, const time& rhs)
  2420. {
  2421. impl::print_to_stream(lhs, rhs);
  2422. return lhs;
  2423. }
  2424. };
  2425. struct TOML_TRIVIAL_ABI time_offset
  2426. {
  2427. int16_t minutes;
  2428. TOML_NODISCARD_CTOR
  2429. time_offset() noexcept = default;
  2430. TOML_CONSTRAINED_TEMPLATE((impl::all_integral<H, M>), typename H, typename M)
  2431. TOML_NODISCARD_CTOR
  2432. constexpr time_offset(H h, M m) noexcept //
  2433. : minutes{ static_cast<int16_t>(static_cast<impl::common_signed_type<H, M>>(h)
  2434. * impl::common_signed_type<H, M>{ 60 }
  2435. + static_cast<impl::common_signed_type<H, M>>(m)) }
  2436. {}
  2437. TOML_PURE_INLINE_GETTER
  2438. friend constexpr bool operator==(time_offset lhs, time_offset rhs) noexcept
  2439. {
  2440. return lhs.minutes == rhs.minutes;
  2441. }
  2442. TOML_PURE_INLINE_GETTER
  2443. friend constexpr bool operator!=(time_offset lhs, time_offset rhs) noexcept
  2444. {
  2445. return lhs.minutes != rhs.minutes;
  2446. }
  2447. TOML_PURE_INLINE_GETTER
  2448. friend constexpr bool operator<(time_offset lhs, time_offset rhs) noexcept
  2449. {
  2450. return lhs.minutes < rhs.minutes;
  2451. }
  2452. TOML_PURE_INLINE_GETTER
  2453. friend constexpr bool operator<=(time_offset lhs, time_offset rhs) noexcept
  2454. {
  2455. return lhs.minutes <= rhs.minutes;
  2456. }
  2457. TOML_PURE_INLINE_GETTER
  2458. friend constexpr bool operator>(time_offset lhs, time_offset rhs) noexcept
  2459. {
  2460. return lhs.minutes > rhs.minutes;
  2461. }
  2462. TOML_PURE_INLINE_GETTER
  2463. friend constexpr bool operator>=(time_offset lhs, time_offset rhs) noexcept
  2464. {
  2465. return lhs.minutes >= rhs.minutes;
  2466. }
  2467. friend std::ostream& operator<<(std::ostream& lhs, const time_offset& rhs)
  2468. {
  2469. impl::print_to_stream(lhs, rhs);
  2470. return lhs;
  2471. }
  2472. };
  2473. TOML_ABI_NAMESPACE_BOOL(TOML_HAS_CUSTOM_OPTIONAL_TYPE, custopt, stdopt);
  2474. struct date_time
  2475. {
  2476. toml::date date;
  2477. toml::time time;
  2478. optional<toml::time_offset> offset;
  2479. TOML_NODISCARD_CTOR
  2480. date_time() noexcept = default;
  2481. TOML_NODISCARD_CTOR
  2482. constexpr date_time(const toml::date& d, const toml::time& t) noexcept //
  2483. : date{ d },
  2484. time{ t },
  2485. offset{} // TINAE - icc bugfix
  2486. {}
  2487. TOML_NODISCARD_CTOR
  2488. explicit constexpr date_time(const toml::date& d) noexcept //
  2489. : date{ d },
  2490. time{},
  2491. offset{} // TINAE - icc bugfix
  2492. {}
  2493. TOML_NODISCARD_CTOR
  2494. explicit constexpr date_time(const toml::time& t) noexcept //
  2495. : date{},
  2496. time{ t },
  2497. offset{} // TINAE - icc bugfix
  2498. {}
  2499. TOML_NODISCARD_CTOR
  2500. constexpr date_time(const toml::date& d, const toml::time& t, const toml::time_offset& off) noexcept
  2501. : date{ d },
  2502. time{ t },
  2503. offset{ off }
  2504. {}
  2505. TOML_PURE_INLINE_GETTER
  2506. constexpr bool is_local() const noexcept
  2507. {
  2508. return !offset.has_value();
  2509. }
  2510. TOML_PURE_GETTER
  2511. friend constexpr bool operator==(const date_time& lhs, const date_time& rhs) noexcept
  2512. {
  2513. return lhs.date == rhs.date //
  2514. && lhs.time == rhs.time //
  2515. && lhs.offset == rhs.offset;
  2516. }
  2517. TOML_PURE_INLINE_GETTER
  2518. friend constexpr bool operator!=(const date_time& lhs, const date_time& rhs) noexcept
  2519. {
  2520. return !(lhs == rhs);
  2521. }
  2522. TOML_PURE_GETTER
  2523. friend constexpr bool operator<(const date_time& lhs, const date_time& rhs) noexcept
  2524. {
  2525. if (lhs.date != rhs.date)
  2526. return lhs.date < rhs.date;
  2527. if (lhs.time != rhs.time)
  2528. return lhs.time < rhs.time;
  2529. return lhs.offset < rhs.offset;
  2530. }
  2531. TOML_PURE_GETTER
  2532. friend constexpr bool operator<=(const date_time& lhs, const date_time& rhs) noexcept
  2533. {
  2534. if (lhs.date != rhs.date)
  2535. return lhs.date < rhs.date;
  2536. if (lhs.time != rhs.time)
  2537. return lhs.time < rhs.time;
  2538. return lhs.offset <= rhs.offset;
  2539. }
  2540. TOML_PURE_INLINE_GETTER
  2541. friend constexpr bool operator>(const date_time& lhs, const date_time& rhs) noexcept
  2542. {
  2543. return !(lhs <= rhs);
  2544. }
  2545. TOML_PURE_INLINE_GETTER
  2546. friend constexpr bool operator>=(const date_time& lhs, const date_time& rhs) noexcept
  2547. {
  2548. return !(lhs < rhs);
  2549. }
  2550. friend std::ostream& operator<<(std::ostream& lhs, const date_time& rhs)
  2551. {
  2552. impl::print_to_stream(lhs, rhs);
  2553. return lhs;
  2554. }
  2555. };
  2556. TOML_ABI_NAMESPACE_END; // TOML_HAS_CUSTOM_OPTIONAL_TYPE
  2557. }
  2558. TOML_NAMESPACE_END;
  2559. #ifdef _MSC_VER
  2560. #pragma pop_macro("min")
  2561. #pragma pop_macro("max")
  2562. #ifndef __clang__
  2563. #pragma inline_recursion(off)
  2564. #endif
  2565. #endif
  2566. TOML_POP_WARNINGS;
  2567. //******** impl/at_path.hpp ******************************************************************************************
  2568. TOML_IMPL_NAMESPACE_START
  2569. {
  2570. template <typename T>
  2571. using parse_path_callback = bool(TOML_CALLCONV*)(void*, T);
  2572. TOML_NODISCARD
  2573. bool TOML_CALLCONV parse_path(std::string_view,
  2574. void*,
  2575. parse_path_callback<std::string_view>,
  2576. parse_path_callback<size_t>);
  2577. }
  2578. TOML_IMPL_NAMESPACE_END;
  2579. TOML_NAMESPACE_START
  2580. {
  2581. TOML_NODISCARD
  2582. TOML_EXPORTED_FREE_FUNCTION
  2583. node_view<node> TOML_CALLCONV at_path(node & root, std::string_view path) noexcept;
  2584. TOML_NODISCARD
  2585. TOML_EXPORTED_FREE_FUNCTION
  2586. node_view<const node> TOML_CALLCONV at_path(const node& root, std::string_view path) noexcept;
  2587. #if TOML_ENABLE_WINDOWS_COMPAT
  2588. TOML_NODISCARD
  2589. TOML_EXPORTED_FREE_FUNCTION
  2590. node_view<node> TOML_CALLCONV at_path(node & root, std::wstring_view path);
  2591. TOML_NODISCARD
  2592. TOML_EXPORTED_FREE_FUNCTION
  2593. node_view<const node> TOML_CALLCONV at_path(const node& root, std::wstring_view path);
  2594. #endif
  2595. }
  2596. TOML_NAMESPACE_END;
  2597. //******** impl/std_vector.hpp ***************************************************************************************
  2598. TOML_DISABLE_WARNINGS;
  2599. #include <vector>
  2600. #include <iterator>
  2601. TOML_ENABLE_WARNINGS;
  2602. //******** impl/path.hpp *********************************************************************************************
  2603. TOML_PUSH_WARNINGS;
  2604. #ifdef _MSC_VER
  2605. #ifndef __clang__
  2606. #pragma inline_recursion(on)
  2607. #endif
  2608. #pragma push_macro("min")
  2609. #pragma push_macro("max")
  2610. #undef min
  2611. #undef max
  2612. #endif
  2613. TOML_NAMESPACE_START
  2614. {
  2615. enum class TOML_CLOSED_ENUM path_component_type : uint8_t
  2616. {
  2617. key = 0x1,
  2618. array_index = 0x2
  2619. };
  2620. class TOML_EXPORTED_CLASS path_component
  2621. {
  2622. struct storage_t
  2623. {
  2624. static constexpr size_t size =
  2625. (sizeof(size_t) < sizeof(std::string) ? sizeof(std::string) : sizeof(size_t));
  2626. static constexpr size_t align =
  2627. (alignof(size_t) < alignof(std::string) ? alignof(std::string) : alignof(size_t));
  2628. alignas(align) unsigned char bytes[size];
  2629. };
  2630. alignas(storage_t::align) mutable storage_t value_storage_;
  2631. path_component_type type_;
  2632. TOML_PURE_GETTER
  2633. TOML_EXPORTED_STATIC_FUNCTION
  2634. static bool TOML_CALLCONV equal(const path_component&, const path_component&) noexcept;
  2635. template <typename Type>
  2636. TOML_PURE_INLINE_GETTER
  2637. static Type* get_as(storage_t& s) noexcept
  2638. {
  2639. return TOML_LAUNDER(reinterpret_cast<Type*>(s.bytes));
  2640. }
  2641. static void store_key(std::string_view key, storage_t& storage_)
  2642. {
  2643. ::new (static_cast<void*>(storage_.bytes)) std::string{ key };
  2644. }
  2645. static void store_index(size_t index, storage_t& storage_) noexcept
  2646. {
  2647. ::new (static_cast<void*>(storage_.bytes)) std::size_t{ index };
  2648. }
  2649. void destroy() noexcept
  2650. {
  2651. if (type_ == path_component_type::key)
  2652. get_as<std::string>(value_storage_)->~basic_string();
  2653. }
  2654. TOML_NODISCARD
  2655. size_t& index_ref() noexcept
  2656. {
  2657. TOML_ASSERT_ASSUME(type_ == path_component_type::array_index);
  2658. return *get_as<size_t>(value_storage_);
  2659. }
  2660. TOML_NODISCARD
  2661. std::string& key_ref() noexcept
  2662. {
  2663. TOML_ASSERT_ASSUME(type_ == path_component_type::key);
  2664. return *get_as<std::string>(value_storage_);
  2665. }
  2666. public:
  2667. TOML_NODISCARD_CTOR
  2668. TOML_EXPORTED_MEMBER_FUNCTION
  2669. path_component();
  2670. TOML_NODISCARD_CTOR
  2671. TOML_EXPORTED_MEMBER_FUNCTION
  2672. path_component(size_t index) noexcept;
  2673. TOML_NODISCARD_CTOR
  2674. TOML_EXPORTED_MEMBER_FUNCTION
  2675. path_component(std::string_view key);
  2676. #if TOML_ENABLE_WINDOWS_COMPAT
  2677. TOML_NODISCARD_CTOR
  2678. TOML_EXPORTED_MEMBER_FUNCTION
  2679. path_component(std::wstring_view key);
  2680. #endif
  2681. TOML_NODISCARD_CTOR
  2682. TOML_EXPORTED_MEMBER_FUNCTION
  2683. path_component(const path_component& pc);
  2684. TOML_NODISCARD_CTOR
  2685. TOML_EXPORTED_MEMBER_FUNCTION
  2686. path_component(path_component&& pc) noexcept;
  2687. TOML_EXPORTED_MEMBER_FUNCTION
  2688. path_component& operator=(const path_component& rhs);
  2689. TOML_EXPORTED_MEMBER_FUNCTION
  2690. path_component& operator=(path_component&& rhs) noexcept;
  2691. TOML_EXPORTED_MEMBER_FUNCTION
  2692. path_component& operator=(size_t new_index) noexcept;
  2693. TOML_EXPORTED_MEMBER_FUNCTION
  2694. path_component& operator=(std::string_view new_key);
  2695. #if TOML_ENABLE_WINDOWS_COMPAT
  2696. TOML_EXPORTED_MEMBER_FUNCTION
  2697. path_component& operator=(std::wstring_view new_key);
  2698. #endif
  2699. ~path_component() noexcept
  2700. {
  2701. destroy();
  2702. }
  2703. TOML_PURE_GETTER
  2704. size_t index() const noexcept
  2705. {
  2706. TOML_ASSERT_ASSUME(type_ == path_component_type::array_index);
  2707. return *get_as<const size_t>(value_storage_);
  2708. }
  2709. TOML_PURE_INLINE_GETTER
  2710. explicit operator size_t() const noexcept
  2711. {
  2712. return index();
  2713. }
  2714. TOML_PURE_GETTER
  2715. const std::string& key() const noexcept
  2716. {
  2717. TOML_ASSERT_ASSUME(type_ == path_component_type::key);
  2718. return *get_as<const std::string>(value_storage_);
  2719. }
  2720. TOML_PURE_INLINE_GETTER
  2721. explicit operator const std::string&() const noexcept
  2722. {
  2723. return key();
  2724. }
  2725. TOML_PURE_INLINE_GETTER
  2726. path_component_type type() const noexcept
  2727. {
  2728. return type_;
  2729. }
  2730. TOML_PURE_INLINE_GETTER
  2731. friend bool operator==(const path_component& lhs, const path_component& rhs) noexcept
  2732. {
  2733. return equal(lhs, rhs);
  2734. }
  2735. TOML_PURE_INLINE_GETTER
  2736. friend bool operator!=(const path_component& lhs, const path_component& rhs) noexcept
  2737. {
  2738. return !equal(lhs, rhs);
  2739. }
  2740. };
  2741. class TOML_EXPORTED_CLASS path
  2742. {
  2743. private:
  2744. std::vector<path_component> components_;
  2745. TOML_EXPORTED_MEMBER_FUNCTION
  2746. void print_to(std::ostream&) const;
  2747. TOML_PURE_GETTER
  2748. TOML_EXPORTED_STATIC_FUNCTION
  2749. static bool TOML_CALLCONV equal(const path&, const path&) noexcept;
  2750. public:
  2751. TOML_NODISCARD_CTOR
  2752. path() noexcept = default;
  2753. TOML_NODISCARD_CTOR
  2754. TOML_EXPORTED_MEMBER_FUNCTION
  2755. explicit path(std::string_view);
  2756. #if TOML_ENABLE_WINDOWS_COMPAT
  2757. TOML_NODISCARD_CTOR
  2758. TOML_EXPORTED_MEMBER_FUNCTION
  2759. explicit path(std::wstring_view);
  2760. #endif
  2761. ~path() noexcept = default;
  2762. TOML_NODISCARD_CTOR
  2763. path(const path&) = default;
  2764. TOML_NODISCARD_CTOR
  2765. path(path&&) noexcept = default;
  2766. TOML_PURE_INLINE_GETTER
  2767. size_t size() const noexcept
  2768. {
  2769. return components_.size();
  2770. }
  2771. TOML_PURE_INLINE_GETTER
  2772. explicit operator bool() const noexcept
  2773. {
  2774. return !components_.empty();
  2775. }
  2776. TOML_PURE_INLINE_GETTER
  2777. bool empty() const noexcept
  2778. {
  2779. return components_.empty();
  2780. }
  2781. TOML_PURE_INLINE_GETTER
  2782. path_component& operator[](size_t index) noexcept
  2783. {
  2784. TOML_ASSERT(index < size());
  2785. return components_[index];
  2786. }
  2787. TOML_PURE_INLINE_GETTER
  2788. const path_component& operator[](size_t index) const noexcept
  2789. {
  2790. TOML_ASSERT(index < size());
  2791. return components_[index];
  2792. }
  2793. path& operator=(const path&) = default;
  2794. path& operator=(path&&) noexcept = default;
  2795. TOML_EXPORTED_MEMBER_FUNCTION
  2796. path& operator=(std::string_view);
  2797. #if TOML_ENABLE_WINDOWS_COMPAT
  2798. TOML_EXPORTED_MEMBER_FUNCTION
  2799. path& operator=(std::wstring_view);
  2800. #endif
  2801. TOML_ALWAYS_INLINE
  2802. path& assign(const path& p)
  2803. {
  2804. return *this = p;
  2805. }
  2806. TOML_ALWAYS_INLINE
  2807. path& assign(path&& p) noexcept
  2808. {
  2809. return *this = std::move(p);
  2810. }
  2811. TOML_ALWAYS_INLINE
  2812. path& assign(std::string_view str)
  2813. {
  2814. return *this = str;
  2815. }
  2816. #if TOML_ENABLE_WINDOWS_COMPAT
  2817. TOML_ALWAYS_INLINE
  2818. path& assign(std::wstring_view str)
  2819. {
  2820. return *this = str;
  2821. }
  2822. #endif
  2823. TOML_EXPORTED_MEMBER_FUNCTION
  2824. path& operator+=(const path&);
  2825. TOML_EXPORTED_MEMBER_FUNCTION
  2826. path& operator+=(path&&);
  2827. TOML_EXPORTED_MEMBER_FUNCTION
  2828. path& operator+=(std::string_view);
  2829. #if TOML_ENABLE_WINDOWS_COMPAT
  2830. TOML_EXPORTED_MEMBER_FUNCTION
  2831. path& operator+=(std::wstring_view);
  2832. #endif
  2833. TOML_ALWAYS_INLINE
  2834. path& append(const path& p)
  2835. {
  2836. return *this += p;
  2837. }
  2838. TOML_ALWAYS_INLINE
  2839. path& append(path&& p)
  2840. {
  2841. return *this += std::move(p);
  2842. }
  2843. TOML_ALWAYS_INLINE
  2844. path& append(std::string_view str)
  2845. {
  2846. return *this += str;
  2847. }
  2848. #if TOML_ENABLE_WINDOWS_COMPAT
  2849. TOML_ALWAYS_INLINE
  2850. path& append(std::wstring_view str)
  2851. {
  2852. return *this += str;
  2853. }
  2854. #endif
  2855. TOML_EXPORTED_MEMBER_FUNCTION
  2856. path& prepend(const path&);
  2857. TOML_EXPORTED_MEMBER_FUNCTION
  2858. path& prepend(path&&);
  2859. TOML_EXPORTED_MEMBER_FUNCTION
  2860. path& prepend(std::string_view);
  2861. #if TOML_ENABLE_WINDOWS_COMPAT
  2862. TOML_EXPORTED_MEMBER_FUNCTION
  2863. path& prepend(std::wstring_view);
  2864. #endif
  2865. TOML_NODISCARD
  2866. friend path operator+(const path& lhs, const path& rhs)
  2867. {
  2868. path result = lhs;
  2869. result += rhs;
  2870. return result;
  2871. }
  2872. TOML_NODISCARD
  2873. friend path operator+(const path& lhs, std::string_view rhs)
  2874. {
  2875. path result = lhs;
  2876. result += rhs;
  2877. return result;
  2878. }
  2879. TOML_NODISCARD
  2880. friend path operator+(std::string_view lhs, const path& rhs)
  2881. {
  2882. path result = rhs;
  2883. result.prepend(lhs);
  2884. return result;
  2885. }
  2886. #if TOML_ENABLE_WINDOWS_COMPAT
  2887. TOML_NODISCARD
  2888. friend path operator+(const path& lhs, std::wstring_view rhs)
  2889. {
  2890. path result = lhs;
  2891. result += rhs;
  2892. return result;
  2893. }
  2894. TOML_NODISCARD
  2895. friend path operator+(std::wstring_view lhs, const path& rhs)
  2896. {
  2897. path result = rhs;
  2898. result.prepend(lhs);
  2899. return result;
  2900. }
  2901. #endif
  2902. TOML_ALWAYS_INLINE
  2903. friend std::ostream& operator<<(std::ostream& os, const path& rhs)
  2904. {
  2905. rhs.print_to(os);
  2906. return os;
  2907. }
  2908. TOML_NODISCARD
  2909. TOML_EXPORTED_MEMBER_FUNCTION
  2910. std::string str() const;
  2911. TOML_NODISCARD
  2912. TOML_ALWAYS_INLINE
  2913. explicit operator std::string() const
  2914. {
  2915. return str();
  2916. }
  2917. #if TOML_ENABLE_WINDOWS_COMPAT
  2918. TOML_NODISCARD
  2919. TOML_EXPORTED_MEMBER_FUNCTION
  2920. std::wstring wide_str() const;
  2921. TOML_NODISCARD
  2922. TOML_ALWAYS_INLINE
  2923. explicit operator std::wstring() const
  2924. {
  2925. return wide_str();
  2926. }
  2927. #endif
  2928. TOML_PURE_INLINE_GETTER
  2929. friend bool operator==(const path& lhs, const path& rhs) noexcept
  2930. {
  2931. return equal(lhs, rhs);
  2932. }
  2933. TOML_PURE_INLINE_GETTER
  2934. friend bool operator!=(const path& lhs, const path& rhs) noexcept
  2935. {
  2936. return !equal(lhs, rhs);
  2937. }
  2938. TOML_NODISCARD
  2939. TOML_ALWAYS_INLINE
  2940. friend bool operator==(const path& lhs, std::string_view rhs)
  2941. {
  2942. return lhs == path{ rhs };
  2943. }
  2944. TOML_NODISCARD
  2945. TOML_ALWAYS_INLINE
  2946. friend bool operator==(std::string_view lhs, const path& rhs)
  2947. {
  2948. return rhs == lhs;
  2949. }
  2950. TOML_NODISCARD
  2951. TOML_ALWAYS_INLINE
  2952. friend bool operator!=(const path& lhs, std::string_view rhs)
  2953. {
  2954. return lhs != path{ rhs };
  2955. }
  2956. TOML_NODISCARD
  2957. TOML_ALWAYS_INLINE
  2958. friend bool operator!=(std::string_view lhs, const path& rhs)
  2959. {
  2960. return rhs != lhs;
  2961. }
  2962. #if TOML_ENABLE_WINDOWS_COMPAT
  2963. TOML_NODISCARD
  2964. TOML_ALWAYS_INLINE
  2965. friend bool operator==(const path& lhs, std::wstring_view rhs)
  2966. {
  2967. return lhs == path{ rhs };
  2968. }
  2969. TOML_NODISCARD
  2970. TOML_ALWAYS_INLINE
  2971. friend bool operator==(std::wstring_view lhs, const path& rhs)
  2972. {
  2973. return rhs == lhs;
  2974. }
  2975. TOML_NODISCARD
  2976. TOML_ALWAYS_INLINE
  2977. friend bool operator!=(const path& lhs, std::wstring_view rhs)
  2978. {
  2979. return lhs != path{ rhs };
  2980. }
  2981. TOML_NODISCARD
  2982. TOML_ALWAYS_INLINE
  2983. friend bool operator!=(std::wstring_view lhs, const path& rhs)
  2984. {
  2985. return rhs != lhs;
  2986. }
  2987. #endif // TOML_ENABLE_WINDOWS_COMPAT
  2988. using iterator = std::vector<path_component>::iterator;
  2989. using const_iterator = std::vector<path_component>::const_iterator;
  2990. TOML_PURE_INLINE_GETTER
  2991. iterator begin() noexcept
  2992. {
  2993. return components_.begin();
  2994. }
  2995. TOML_PURE_INLINE_GETTER
  2996. iterator end() noexcept
  2997. {
  2998. return components_.end();
  2999. }
  3000. TOML_PURE_INLINE_GETTER
  3001. const_iterator begin() const noexcept
  3002. {
  3003. return components_.begin();
  3004. }
  3005. TOML_PURE_INLINE_GETTER
  3006. const_iterator end() const noexcept
  3007. {
  3008. return components_.end();
  3009. }
  3010. TOML_PURE_INLINE_GETTER
  3011. const_iterator cbegin() const noexcept
  3012. {
  3013. return components_.begin();
  3014. }
  3015. TOML_PURE_INLINE_GETTER
  3016. const_iterator cend() const noexcept
  3017. {
  3018. return components_.end();
  3019. }
  3020. TOML_EXPORTED_MEMBER_FUNCTION
  3021. void clear() noexcept;
  3022. TOML_EXPORTED_MEMBER_FUNCTION
  3023. path& truncate(size_t n);
  3024. TOML_NODISCARD
  3025. TOML_EXPORTED_MEMBER_FUNCTION
  3026. path truncated(size_t n) const;
  3027. TOML_NODISCARD
  3028. TOML_EXPORTED_MEMBER_FUNCTION
  3029. path parent() const;
  3030. TOML_NODISCARD
  3031. TOML_EXPORTED_MEMBER_FUNCTION
  3032. path leaf(size_t n = 1) const;
  3033. TOML_NODISCARD
  3034. TOML_EXPORTED_MEMBER_FUNCTION
  3035. path subpath(const_iterator start, const_iterator end) const;
  3036. TOML_NODISCARD
  3037. TOML_EXPORTED_MEMBER_FUNCTION
  3038. path subpath(size_t start, size_t length) const;
  3039. };
  3040. inline namespace literals
  3041. {
  3042. TOML_NODISCARD
  3043. TOML_ALWAYS_INLINE
  3044. path operator"" _tpath(const char* str, size_t len)
  3045. {
  3046. return path(std::string_view{ str, len });
  3047. }
  3048. }
  3049. TOML_NODISCARD
  3050. TOML_EXPORTED_FREE_FUNCTION
  3051. node_view<node> TOML_CALLCONV at_path(node & root, const toml::path& path) noexcept;
  3052. TOML_NODISCARD
  3053. TOML_EXPORTED_FREE_FUNCTION
  3054. node_view<const node> TOML_CALLCONV at_path(const node& root, const toml::path& path) noexcept;
  3055. }
  3056. TOML_NAMESPACE_END;
  3057. #ifdef _MSC_VER
  3058. #pragma pop_macro("min")
  3059. #pragma pop_macro("max")
  3060. #ifndef __clang__
  3061. #pragma inline_recursion(off)
  3062. #endif
  3063. #endif
  3064. TOML_POP_WARNINGS;
  3065. //******** impl/std_utility.hpp **************************************************************************************
  3066. TOML_DISABLE_WARNINGS;
  3067. #include <utility>
  3068. TOML_ENABLE_WARNINGS;
  3069. //******** impl/node.hpp *********************************************************************************************
  3070. TOML_PUSH_WARNINGS;
  3071. #ifdef _MSC_VER
  3072. #ifndef __clang__
  3073. #pragma inline_recursion(on)
  3074. #endif
  3075. #pragma push_macro("min")
  3076. #pragma push_macro("max")
  3077. #undef min
  3078. #undef max
  3079. #endif
  3080. TOML_NAMESPACE_START
  3081. {
  3082. class TOML_ABSTRACT_INTERFACE TOML_EXPORTED_CLASS node
  3083. {
  3084. private:
  3085. friend class TOML_PARSER_TYPENAME;
  3086. source_region source_{};
  3087. template <typename T>
  3088. TOML_NODISCARD
  3089. decltype(auto) get_value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>);
  3090. template <typename T, typename N>
  3091. using ref_type_ = std::conditional_t< //
  3092. std::is_reference_v<T>, //
  3093. impl::copy_ref<impl::copy_cv<impl::unwrap_node<T>, std::remove_reference_t<N>>, T>, //
  3094. impl::copy_cvref<impl::unwrap_node<T>, N> //
  3095. >;
  3096. template <typename T, typename N>
  3097. using ref_type = std::conditional_t< //
  3098. std::is_reference_v<N>, //
  3099. ref_type_<T, N>, //
  3100. ref_type_<T, std::add_lvalue_reference_t<N>> //
  3101. >;
  3102. template <typename T, typename N>
  3103. TOML_PURE_GETTER
  3104. static ref_type<T, N&&> do_ref(N&& n) noexcept
  3105. {
  3106. using unwrapped_type = impl::unwrap_node<T>;
  3107. static_assert(toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>,
  3108. "The template type argument of node::ref() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
  3109. TOML_ASSERT_ASSUME(
  3110. n.template is<unwrapped_type>()
  3111. && "template type argument provided to toml::node::ref() didn't match the node's actual type");
  3112. using node_ref = std::remove_volatile_t<std::remove_reference_t<N>>&;
  3113. using val_type = std::remove_volatile_t<unwrapped_type>;
  3114. using out_ref = ref_type<T, N&&>;
  3115. static_assert(std::is_reference_v<out_ref>);
  3116. if constexpr (toml::is_value<unwrapped_type>)
  3117. return static_cast<out_ref>(const_cast<node_ref>(n).template ref_cast<val_type>().get());
  3118. else
  3119. return static_cast<out_ref>(const_cast<node_ref>(n).template ref_cast<val_type>());
  3120. }
  3121. protected:
  3122. TOML_EXPORTED_MEMBER_FUNCTION
  3123. node() noexcept;
  3124. TOML_EXPORTED_MEMBER_FUNCTION
  3125. node(const node&) noexcept;
  3126. TOML_EXPORTED_MEMBER_FUNCTION
  3127. node(node&&) noexcept;
  3128. TOML_EXPORTED_MEMBER_FUNCTION
  3129. node& operator=(const node&) noexcept;
  3130. TOML_EXPORTED_MEMBER_FUNCTION
  3131. node& operator=(node&&) noexcept;
  3132. template <typename T, typename N>
  3133. using ref_cast_type_ = std::conditional_t< //
  3134. std::is_reference_v<T>, //
  3135. impl::copy_ref<impl::copy_cv<impl::wrap_node<T>, std::remove_reference_t<N>>, T>, //
  3136. impl::copy_cvref<impl::wrap_node<T>, N> //
  3137. >;
  3138. template <typename T, typename N>
  3139. using ref_cast_type = std::conditional_t< //
  3140. std::is_reference_v<N>, //
  3141. ref_cast_type_<T, N>, //
  3142. ref_cast_type_<T, std::add_lvalue_reference_t<N>> //
  3143. >;
  3144. template <typename T>
  3145. TOML_PURE_INLINE_GETTER
  3146. ref_cast_type<T, node&> ref_cast() & noexcept
  3147. {
  3148. using out_ref = ref_cast_type<T, node&>;
  3149. using out_type = std::remove_reference_t<out_ref>;
  3150. return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
  3151. }
  3152. template <typename T>
  3153. TOML_PURE_INLINE_GETTER
  3154. ref_cast_type<T, node&&> ref_cast() && noexcept
  3155. {
  3156. using out_ref = ref_cast_type<T, node&&>;
  3157. using out_type = std::remove_reference_t<out_ref>;
  3158. return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
  3159. }
  3160. template <typename T>
  3161. TOML_PURE_INLINE_GETTER
  3162. ref_cast_type<T, const node&> ref_cast() const& noexcept
  3163. {
  3164. using out_ref = ref_cast_type<T, const node&>;
  3165. using out_type = std::remove_reference_t<out_ref>;
  3166. return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
  3167. }
  3168. template <typename T>
  3169. TOML_PURE_INLINE_GETTER
  3170. ref_cast_type<T, const node&&> ref_cast() const&& noexcept
  3171. {
  3172. using out_ref = ref_cast_type<T, const node&&>;
  3173. using out_type = std::remove_reference_t<out_ref>;
  3174. return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
  3175. }
  3176. public:
  3177. TOML_EXPORTED_MEMBER_FUNCTION
  3178. virtual ~node() noexcept;
  3179. TOML_PURE_GETTER
  3180. virtual bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept = 0;
  3181. TOML_PURE_GETTER
  3182. virtual bool is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept = 0;
  3183. TOML_PURE_GETTER
  3184. virtual bool is_homogeneous(node_type ntype) const noexcept = 0;
  3185. template <typename ElemType = void>
  3186. TOML_PURE_GETTER
  3187. bool is_homogeneous() const noexcept
  3188. {
  3189. using type = impl::remove_cvref<impl::unwrap_node<ElemType>>;
  3190. static_assert(std::is_void_v<type> || toml::is_value<type> || toml::is_container<type>,
  3191. "The template type argument of node::is_homogeneous() must be void or one "
  3192. "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
  3193. return is_homogeneous(impl::node_type_of<type>);
  3194. }
  3195. TOML_PURE_GETTER
  3196. virtual node_type type() const noexcept = 0;
  3197. TOML_PURE_GETTER
  3198. virtual bool is_table() const noexcept = 0;
  3199. TOML_PURE_GETTER
  3200. virtual bool is_array() const noexcept = 0;
  3201. TOML_PURE_GETTER
  3202. virtual bool is_array_of_tables() const noexcept = 0;
  3203. TOML_PURE_GETTER
  3204. virtual bool is_value() const noexcept = 0;
  3205. TOML_PURE_GETTER
  3206. virtual bool is_string() const noexcept = 0;
  3207. TOML_PURE_GETTER
  3208. virtual bool is_integer() const noexcept = 0;
  3209. TOML_PURE_GETTER
  3210. virtual bool is_floating_point() const noexcept = 0;
  3211. TOML_PURE_GETTER
  3212. virtual bool is_number() const noexcept = 0;
  3213. TOML_PURE_GETTER
  3214. virtual bool is_boolean() const noexcept = 0;
  3215. TOML_PURE_GETTER
  3216. virtual bool is_date() const noexcept = 0;
  3217. TOML_PURE_GETTER
  3218. virtual bool is_time() const noexcept = 0;
  3219. TOML_PURE_GETTER
  3220. virtual bool is_date_time() const noexcept = 0;
  3221. template <typename T>
  3222. TOML_PURE_INLINE_GETTER
  3223. bool is() const noexcept
  3224. {
  3225. using type = impl::remove_cvref<impl::unwrap_node<T>>;
  3226. static_assert(toml::is_value<type> || toml::is_container<type>,
  3227. "The template type argument of node::is() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
  3228. if constexpr (std::is_same_v<type, table>)
  3229. return is_table();
  3230. else if constexpr (std::is_same_v<type, array>)
  3231. return is_array();
  3232. else if constexpr (std::is_same_v<type, std::string>)
  3233. return is_string();
  3234. else if constexpr (std::is_same_v<type, int64_t>)
  3235. return is_integer();
  3236. else if constexpr (std::is_same_v<type, double>)
  3237. return is_floating_point();
  3238. else if constexpr (std::is_same_v<type, bool>)
  3239. return is_boolean();
  3240. else if constexpr (std::is_same_v<type, date>)
  3241. return is_date();
  3242. else if constexpr (std::is_same_v<type, time>)
  3243. return is_time();
  3244. else if constexpr (std::is_same_v<type, date_time>)
  3245. return is_date_time();
  3246. }
  3247. TOML_PURE_GETTER
  3248. virtual table* as_table() noexcept = 0;
  3249. TOML_PURE_GETTER
  3250. virtual array* as_array() noexcept = 0;
  3251. TOML_PURE_GETTER
  3252. virtual toml::value<std::string>* as_string() noexcept = 0;
  3253. TOML_PURE_GETTER
  3254. virtual toml::value<int64_t>* as_integer() noexcept = 0;
  3255. TOML_PURE_GETTER
  3256. virtual toml::value<double>* as_floating_point() noexcept = 0;
  3257. TOML_PURE_GETTER
  3258. virtual toml::value<bool>* as_boolean() noexcept = 0;
  3259. TOML_PURE_GETTER
  3260. virtual toml::value<date>* as_date() noexcept = 0;
  3261. TOML_PURE_GETTER
  3262. virtual toml::value<time>* as_time() noexcept = 0;
  3263. TOML_PURE_GETTER
  3264. virtual toml::value<date_time>* as_date_time() noexcept = 0;
  3265. TOML_PURE_GETTER
  3266. virtual const table* as_table() const noexcept = 0;
  3267. TOML_PURE_GETTER
  3268. virtual const array* as_array() const noexcept = 0;
  3269. TOML_PURE_GETTER
  3270. virtual const toml::value<std::string>* as_string() const noexcept = 0;
  3271. TOML_PURE_GETTER
  3272. virtual const toml::value<int64_t>* as_integer() const noexcept = 0;
  3273. TOML_PURE_GETTER
  3274. virtual const toml::value<double>* as_floating_point() const noexcept = 0;
  3275. TOML_PURE_GETTER
  3276. virtual const toml::value<bool>* as_boolean() const noexcept = 0;
  3277. TOML_PURE_GETTER
  3278. virtual const toml::value<date>* as_date() const noexcept = 0;
  3279. TOML_PURE_GETTER
  3280. virtual const toml::value<time>* as_time() const noexcept = 0;
  3281. TOML_PURE_GETTER
  3282. virtual const toml::value<date_time>* as_date_time() const noexcept = 0;
  3283. template <typename T>
  3284. TOML_PURE_INLINE_GETTER
  3285. impl::wrap_node<T>* as() noexcept
  3286. {
  3287. using unwrapped_type = impl::unwrap_node<impl::remove_cvref<T>>;
  3288. static_assert(toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>,
  3289. "The template type argument of node::as() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
  3290. if constexpr (std::is_same_v<unwrapped_type, table>)
  3291. return as_table();
  3292. else if constexpr (std::is_same_v<unwrapped_type, array>)
  3293. return as_array();
  3294. else if constexpr (std::is_same_v<unwrapped_type, std::string>)
  3295. return as_string();
  3296. else if constexpr (std::is_same_v<unwrapped_type, int64_t>)
  3297. return as_integer();
  3298. else if constexpr (std::is_same_v<unwrapped_type, double>)
  3299. return as_floating_point();
  3300. else if constexpr (std::is_same_v<unwrapped_type, bool>)
  3301. return as_boolean();
  3302. else if constexpr (std::is_same_v<unwrapped_type, date>)
  3303. return as_date();
  3304. else if constexpr (std::is_same_v<unwrapped_type, time>)
  3305. return as_time();
  3306. else if constexpr (std::is_same_v<unwrapped_type, date_time>)
  3307. return as_date_time();
  3308. }
  3309. template <typename T>
  3310. TOML_PURE_INLINE_GETTER
  3311. const impl::wrap_node<T>* as() const noexcept
  3312. {
  3313. using unwrapped_type = impl::unwrap_node<impl::remove_cvref<T>>;
  3314. static_assert(toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>,
  3315. "The template type argument of node::as() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
  3316. if constexpr (std::is_same_v<unwrapped_type, table>)
  3317. return as_table();
  3318. else if constexpr (std::is_same_v<unwrapped_type, array>)
  3319. return as_array();
  3320. else if constexpr (std::is_same_v<unwrapped_type, std::string>)
  3321. return as_string();
  3322. else if constexpr (std::is_same_v<unwrapped_type, int64_t>)
  3323. return as_integer();
  3324. else if constexpr (std::is_same_v<unwrapped_type, double>)
  3325. return as_floating_point();
  3326. else if constexpr (std::is_same_v<unwrapped_type, bool>)
  3327. return as_boolean();
  3328. else if constexpr (std::is_same_v<unwrapped_type, date>)
  3329. return as_date();
  3330. else if constexpr (std::is_same_v<unwrapped_type, time>)
  3331. return as_time();
  3332. else if constexpr (std::is_same_v<unwrapped_type, date_time>)
  3333. return as_date_time();
  3334. }
  3335. template <typename T>
  3336. TOML_NODISCARD
  3337. optional<T> value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>);
  3338. template <typename T>
  3339. TOML_NODISCARD
  3340. optional<T> value() const noexcept(impl::value_retrieval_is_nothrow<T>);
  3341. template <typename T>
  3342. TOML_NODISCARD
  3343. auto value_or(T&& default_value) const noexcept(impl::value_retrieval_is_nothrow<T>);
  3344. template <typename T>
  3345. TOML_PURE_GETTER
  3346. decltype(auto) ref() & noexcept
  3347. {
  3348. return do_ref<T>(*this);
  3349. }
  3350. template <typename T>
  3351. TOML_PURE_GETTER
  3352. decltype(auto) ref() && noexcept
  3353. {
  3354. return do_ref<T>(std::move(*this));
  3355. }
  3356. template <typename T>
  3357. TOML_PURE_GETTER
  3358. decltype(auto) ref() const& noexcept
  3359. {
  3360. return do_ref<T>(*this);
  3361. }
  3362. template <typename T>
  3363. TOML_PURE_GETTER
  3364. decltype(auto) ref() const&& noexcept
  3365. {
  3366. return do_ref<T>(std::move(*this));
  3367. }
  3368. TOML_PURE_INLINE_GETTER
  3369. const source_region& source() const noexcept
  3370. {
  3371. return source_;
  3372. }
  3373. private:
  3374. template <typename Func, typename Node, typename T>
  3375. static constexpr bool can_visit = std::is_invocable_v<Func, ref_cast_type<T, Node>>;
  3376. template <typename Func, typename Node, typename T>
  3377. static constexpr bool can_visit_nothrow = std::is_nothrow_invocable_v<Func, ref_cast_type<T, Node>>;
  3378. template <typename Func, typename Node>
  3379. static constexpr bool can_visit_any = can_visit<Func, Node, table> //
  3380. || can_visit<Func, Node, array> //
  3381. || can_visit<Func, Node, std::string> //
  3382. || can_visit<Func, Node, int64_t> //
  3383. || can_visit<Func, Node, double> //
  3384. || can_visit<Func, Node, bool> //
  3385. || can_visit<Func, Node, date> //
  3386. || can_visit<Func, Node, time> //
  3387. || can_visit<Func, Node, date_time>;
  3388. // clang-format off
  3389. template <typename Func, typename Node>
  3390. static constexpr bool can_visit_all = can_visit<Func, Node, table> //
  3391. && can_visit<Func, Node, array> //
  3392. && can_visit<Func, Node, std::string> //
  3393. && can_visit<Func, Node, int64_t> //
  3394. && can_visit<Func, Node, double> //
  3395. && can_visit<Func, Node, bool> //
  3396. && can_visit<Func, Node, date> //
  3397. && can_visit<Func, Node, time> //
  3398. && can_visit<Func, Node, date_time>;
  3399. template <typename Func, typename Node, typename T>
  3400. static constexpr bool visit_is_nothrow_one = !can_visit<Func, Node, T> || can_visit_nothrow<Func, Node, T>;
  3401. template <typename Func, typename Node>
  3402. static constexpr bool visit_is_nothrow = visit_is_nothrow_one<Func, Node, table> //
  3403. && visit_is_nothrow_one<Func, Node, array> //
  3404. && visit_is_nothrow_one<Func, Node, std::string> //
  3405. && visit_is_nothrow_one<Func, Node, int64_t> //
  3406. && visit_is_nothrow_one<Func, Node, double> //
  3407. && visit_is_nothrow_one<Func, Node, bool> //
  3408. && visit_is_nothrow_one<Func, Node, date> //
  3409. && visit_is_nothrow_one<Func, Node, time> //
  3410. && visit_is_nothrow_one<Func, Node, date_time>;
  3411. // clang-format on
  3412. template <typename Func, typename Node, typename T, bool = can_visit<Func, Node, T>>
  3413. struct visit_return_type_
  3414. {
  3415. using type = decltype(std::declval<Func>()(std::declval<ref_cast_type<T, Node>>()));
  3416. };
  3417. template <typename Func, typename Node, typename T>
  3418. struct visit_return_type_<Func, Node, T, false>
  3419. {
  3420. using type = void;
  3421. };
  3422. template <typename Func, typename Node, typename T>
  3423. using visit_return_type = typename visit_return_type_<Func, Node, T>::type;
  3424. template <typename A, typename B>
  3425. using nonvoid = std::conditional_t<std::is_void_v<A>, B, A>;
  3426. template <typename Func, typename Node>
  3427. static decltype(auto) do_visit(Func&& visitor, Node&& n) noexcept(visit_is_nothrow<Func&&, Node&&>)
  3428. {
  3429. static_assert(can_visit_any<Func&&, Node&&>,
  3430. "TOML node visitors must be invocable for at least one of the toml::node "
  3431. "specializations:" TOML_SA_NODE_TYPE_LIST);
  3432. switch (n.type())
  3433. {
  3434. case node_type::table:
  3435. if constexpr (can_visit<Func&&, Node&&, table>)
  3436. return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<table>());
  3437. break;
  3438. case node_type::array:
  3439. if constexpr (can_visit<Func&&, Node&&, array>)
  3440. return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<array>());
  3441. break;
  3442. case node_type::string:
  3443. if constexpr (can_visit<Func&&, Node&&, std::string>)
  3444. return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<std::string>());
  3445. break;
  3446. case node_type::integer:
  3447. if constexpr (can_visit<Func&&, Node&&, int64_t>)
  3448. return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<int64_t>());
  3449. break;
  3450. case node_type::floating_point:
  3451. if constexpr (can_visit<Func&&, Node&&, double>)
  3452. return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<double>());
  3453. break;
  3454. case node_type::boolean:
  3455. if constexpr (can_visit<Func&&, Node&&, bool>)
  3456. return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<bool>());
  3457. break;
  3458. case node_type::date:
  3459. if constexpr (can_visit<Func&&, Node&&, date>)
  3460. return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<date>());
  3461. break;
  3462. case node_type::time:
  3463. if constexpr (can_visit<Func&&, Node&&, time>)
  3464. return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<time>());
  3465. break;
  3466. case node_type::date_time:
  3467. if constexpr (can_visit<Func&&, Node&&, date_time>)
  3468. return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<date_time>());
  3469. break;
  3470. case node_type::none: TOML_UNREACHABLE;
  3471. default: TOML_UNREACHABLE;
  3472. }
  3473. if constexpr (!can_visit_all<Func&&, Node&&>)
  3474. {
  3475. // clang-format off
  3476. using return_type =
  3477. nonvoid<visit_return_type<Func&&, Node&&, table>,
  3478. nonvoid<visit_return_type<Func&&, Node&&, array>,
  3479. nonvoid<visit_return_type<Func&&, Node&&, std::string>,
  3480. nonvoid<visit_return_type<Func&&, Node&&, int64_t>,
  3481. nonvoid<visit_return_type<Func&&, Node&&, double>,
  3482. nonvoid<visit_return_type<Func&&, Node&&, bool>,
  3483. nonvoid<visit_return_type<Func&&, Node&&, date>,
  3484. nonvoid<visit_return_type<Func&&, Node&&, time>,
  3485. visit_return_type<Func&&, Node&&, date_time>
  3486. >>>>>>>>;
  3487. // clang-format on
  3488. if constexpr (!std::is_void_v<return_type>)
  3489. {
  3490. static_assert(std::is_default_constructible_v<return_type>,
  3491. "Non-exhaustive visitors must return a default-constructible type, or void");
  3492. return return_type{};
  3493. }
  3494. }
  3495. }
  3496. public:
  3497. template <typename Func>
  3498. decltype(auto) visit(Func&& visitor) & noexcept(visit_is_nothrow<Func&&, node&>)
  3499. {
  3500. return do_visit(static_cast<Func&&>(visitor), *this);
  3501. }
  3502. template <typename Func>
  3503. decltype(auto) visit(Func&& visitor) && noexcept(visit_is_nothrow<Func&&, node&&>)
  3504. {
  3505. return do_visit(static_cast<Func&&>(visitor), static_cast<node&&>(*this));
  3506. }
  3507. template <typename Func>
  3508. decltype(auto) visit(Func&& visitor) const& noexcept(visit_is_nothrow<Func&&, const node&>)
  3509. {
  3510. return do_visit(static_cast<Func&&>(visitor), *this);
  3511. }
  3512. template <typename Func>
  3513. decltype(auto) visit(Func&& visitor) const&& noexcept(visit_is_nothrow<Func&&, const node&&>)
  3514. {
  3515. return do_visit(static_cast<Func&&>(visitor), static_cast<const node&&>(*this));
  3516. }
  3517. TOML_NODISCARD
  3518. explicit operator node_view<node>() noexcept;
  3519. TOML_NODISCARD
  3520. explicit operator node_view<const node>() const noexcept;
  3521. TOML_NODISCARD
  3522. TOML_EXPORTED_MEMBER_FUNCTION
  3523. node_view<node> at_path(std::string_view path) noexcept;
  3524. TOML_NODISCARD
  3525. TOML_EXPORTED_MEMBER_FUNCTION
  3526. node_view<const node> at_path(std::string_view path) const noexcept;
  3527. TOML_NODISCARD
  3528. TOML_EXPORTED_MEMBER_FUNCTION
  3529. node_view<node> at_path(const toml::path& path) noexcept;
  3530. TOML_NODISCARD
  3531. TOML_EXPORTED_MEMBER_FUNCTION
  3532. node_view<const node> at_path(const toml::path& path) const noexcept;
  3533. #if TOML_ENABLE_WINDOWS_COMPAT
  3534. TOML_NODISCARD
  3535. TOML_EXPORTED_MEMBER_FUNCTION
  3536. node_view<node> at_path(std::wstring_view path);
  3537. TOML_NODISCARD
  3538. TOML_EXPORTED_MEMBER_FUNCTION
  3539. node_view<const node> at_path(std::wstring_view path) const;
  3540. #endif // TOML_ENABLE_WINDOWS_COMPAT
  3541. TOML_NODISCARD
  3542. TOML_EXPORTED_MEMBER_FUNCTION
  3543. node_view<node> operator[](const toml::path& path) noexcept;
  3544. TOML_NODISCARD
  3545. TOML_EXPORTED_MEMBER_FUNCTION
  3546. node_view<const node> operator[](const toml::path& path) const noexcept;
  3547. };
  3548. }
  3549. TOML_NAMESPACE_END;
  3550. TOML_IMPL_NAMESPACE_START
  3551. {
  3552. TOML_PURE_GETTER
  3553. TOML_EXPORTED_FREE_FUNCTION
  3554. bool TOML_CALLCONV node_deep_equality(const node*, const node*) noexcept;
  3555. }
  3556. TOML_IMPL_NAMESPACE_END;
  3557. #ifdef _MSC_VER
  3558. #pragma pop_macro("min")
  3559. #pragma pop_macro("max")
  3560. #ifndef __clang__
  3561. #pragma inline_recursion(off)
  3562. #endif
  3563. #endif
  3564. TOML_POP_WARNINGS;
  3565. //******** impl/std_initializer_list.hpp *****************************************************************************
  3566. TOML_DISABLE_WARNINGS;
  3567. #include <initializer_list>
  3568. TOML_ENABLE_WARNINGS;
  3569. //******** impl/node_view.hpp ****************************************************************************************
  3570. TOML_PUSH_WARNINGS;
  3571. #ifdef _MSC_VER
  3572. #ifndef __clang__
  3573. #pragma inline_recursion(on)
  3574. #endif
  3575. #pragma push_macro("min")
  3576. #pragma push_macro("max")
  3577. #undef min
  3578. #undef max
  3579. #endif
  3580. TOML_DISABLE_ARITHMETIC_WARNINGS;
  3581. TOML_NAMESPACE_START
  3582. {
  3583. template <typename ViewedType>
  3584. class TOML_TRIVIAL_ABI node_view
  3585. {
  3586. static_assert(impl::is_one_of<ViewedType, toml::node, const toml::node>,
  3587. "A toml::node_view<> must wrap toml::node or const toml::node.");
  3588. public:
  3589. using viewed_type = ViewedType;
  3590. private:
  3591. template <typename T>
  3592. friend class node_view;
  3593. mutable viewed_type* node_ = nullptr;
  3594. public:
  3595. TOML_NODISCARD_CTOR
  3596. node_view() noexcept = default;
  3597. TOML_NODISCARD_CTOR
  3598. explicit node_view(viewed_type* node) noexcept //
  3599. : node_{ node }
  3600. {}
  3601. TOML_NODISCARD_CTOR
  3602. explicit node_view(viewed_type& node) noexcept //
  3603. : node_{ &node }
  3604. {}
  3605. TOML_NODISCARD_CTOR
  3606. node_view(const node_view&) noexcept = default;
  3607. TOML_NODISCARD_CTOR
  3608. node_view(node_view&&) noexcept = default;
  3609. node_view& operator=(const node_view&) & noexcept = default;
  3610. node_view& operator=(node_view&&) & noexcept = default;
  3611. TOML_PURE_INLINE_GETTER
  3612. explicit operator bool() const noexcept
  3613. {
  3614. return node_ != nullptr;
  3615. }
  3616. TOML_PURE_INLINE_GETTER
  3617. viewed_type* node() const noexcept
  3618. {
  3619. return node_;
  3620. }
  3621. TOML_PURE_GETTER
  3622. node_type type() const noexcept
  3623. {
  3624. return node_ ? node_->type() : node_type::none;
  3625. }
  3626. TOML_PURE_GETTER
  3627. bool is_table() const noexcept
  3628. {
  3629. return node_ && node_->is_table();
  3630. }
  3631. TOML_PURE_GETTER
  3632. bool is_array() const noexcept
  3633. {
  3634. return node_ && node_->is_array();
  3635. }
  3636. TOML_PURE_GETTER
  3637. bool is_value() const noexcept
  3638. {
  3639. return node_ && node_->is_value();
  3640. }
  3641. TOML_PURE_GETTER
  3642. bool is_string() const noexcept
  3643. {
  3644. return node_ && node_->is_string();
  3645. }
  3646. TOML_PURE_GETTER
  3647. bool is_integer() const noexcept
  3648. {
  3649. return node_ && node_->is_integer();
  3650. }
  3651. TOML_PURE_GETTER
  3652. bool is_floating_point() const noexcept
  3653. {
  3654. return node_ && node_->is_floating_point();
  3655. }
  3656. TOML_PURE_GETTER
  3657. bool is_number() const noexcept
  3658. {
  3659. return node_ && node_->is_number();
  3660. }
  3661. TOML_PURE_GETTER
  3662. bool is_boolean() const noexcept
  3663. {
  3664. return node_ && node_->is_boolean();
  3665. }
  3666. TOML_PURE_GETTER
  3667. bool is_date() const noexcept
  3668. {
  3669. return node_ && node_->is_date();
  3670. }
  3671. TOML_PURE_GETTER
  3672. bool is_time() const noexcept
  3673. {
  3674. return node_ && node_->is_time();
  3675. }
  3676. TOML_PURE_GETTER
  3677. bool is_date_time() const noexcept
  3678. {
  3679. return node_ && node_->is_date_time();
  3680. }
  3681. TOML_PURE_GETTER
  3682. bool is_array_of_tables() const noexcept
  3683. {
  3684. return node_ && node_->is_array_of_tables();
  3685. }
  3686. template <typename T>
  3687. TOML_PURE_GETTER
  3688. bool is() const noexcept
  3689. {
  3690. return node_ ? node_->template is<impl::unwrap_node<impl::remove_cvref<T>>>() : false;
  3691. }
  3692. TOML_NODISCARD
  3693. bool is_homogeneous(node_type ntype, viewed_type*& first_nonmatch) const noexcept
  3694. {
  3695. if (!node_)
  3696. {
  3697. first_nonmatch = {};
  3698. return false;
  3699. }
  3700. return node_->is_homogeneous(ntype, first_nonmatch);
  3701. }
  3702. TOML_NODISCARD
  3703. bool is_homogeneous(node_type ntype) const noexcept
  3704. {
  3705. return node_ ? node_->is_homogeneous(ntype) : false;
  3706. }
  3707. template <typename ElemType = void>
  3708. TOML_PURE_GETTER
  3709. bool is_homogeneous() const noexcept
  3710. {
  3711. return node_ ? node_->template is_homogeneous<impl::unwrap_node<impl::remove_cvref<ElemType>>>() : false;
  3712. }
  3713. template <typename T>
  3714. TOML_PURE_GETTER
  3715. auto* as() const noexcept
  3716. {
  3717. return node_ ? node_->template as<T>() : nullptr;
  3718. }
  3719. TOML_PURE_GETTER
  3720. auto* as_table() const noexcept
  3721. {
  3722. return as<table>();
  3723. }
  3724. TOML_PURE_GETTER
  3725. auto* as_array() const noexcept
  3726. {
  3727. return as<array>();
  3728. }
  3729. TOML_PURE_GETTER
  3730. auto* as_string() const noexcept
  3731. {
  3732. return as<std::string>();
  3733. }
  3734. TOML_PURE_GETTER
  3735. auto* as_integer() const noexcept
  3736. {
  3737. return as<int64_t>();
  3738. }
  3739. TOML_PURE_GETTER
  3740. auto* as_floating_point() const noexcept
  3741. {
  3742. return as<double>();
  3743. }
  3744. TOML_PURE_GETTER
  3745. auto* as_boolean() const noexcept
  3746. {
  3747. return as<bool>();
  3748. }
  3749. TOML_PURE_GETTER
  3750. auto* as_date() const noexcept
  3751. {
  3752. return as<date>();
  3753. }
  3754. TOML_PURE_GETTER
  3755. auto* as_time() const noexcept
  3756. {
  3757. return as<time>();
  3758. }
  3759. TOML_PURE_GETTER
  3760. auto* as_date_time() const noexcept
  3761. {
  3762. return as<date_time>();
  3763. }
  3764. template <typename T>
  3765. TOML_NODISCARD
  3766. optional<T> value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>)
  3767. {
  3768. if (node_)
  3769. return node_->template value_exact<T>();
  3770. return {};
  3771. }
  3772. template <typename T>
  3773. TOML_NODISCARD
  3774. optional<T> value() const noexcept(impl::value_retrieval_is_nothrow<T>)
  3775. {
  3776. if (node_)
  3777. return node_->template value<T>();
  3778. return {};
  3779. }
  3780. template <typename T>
  3781. TOML_NODISCARD
  3782. auto value_or(T&& default_value) const noexcept(impl::value_retrieval_is_nothrow<T>)
  3783. {
  3784. using namespace ::toml::impl;
  3785. static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
  3786. "Retrieving values as wide-character strings is only "
  3787. "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
  3788. if constexpr (is_wide_string<T>)
  3789. {
  3790. #if TOML_ENABLE_WINDOWS_COMPAT
  3791. if (node_)
  3792. return node_->value_or(static_cast<T&&>(default_value));
  3793. return std::wstring{ static_cast<T&&>(default_value) };
  3794. #else
  3795. static_assert(impl::always_false<T>, "Evaluated unreachable branch!");
  3796. #endif
  3797. }
  3798. else
  3799. {
  3800. using value_type =
  3801. std::conditional_t<std::is_pointer_v<std::decay_t<T>>,
  3802. std::add_pointer_t<std::add_const_t<std::remove_pointer_t<std::decay_t<T>>>>,
  3803. std::decay_t<T>>;
  3804. if (node_)
  3805. return node_->value_or(static_cast<T&&>(default_value));
  3806. if constexpr (std::is_pointer_v<value_type>)
  3807. return value_type{ default_value };
  3808. else
  3809. return static_cast<T&&>(default_value);
  3810. }
  3811. }
  3812. template <typename T>
  3813. TOML_PURE_INLINE_GETTER
  3814. decltype(auto) ref() const noexcept
  3815. {
  3816. TOML_ASSERT_ASSUME(node_ && "toml::node_view::ref() called on a node_view that did not reference a node");
  3817. return node_->template ref<T>();
  3818. }
  3819. private:
  3820. template <typename Func>
  3821. static constexpr bool visit_is_nothrow = noexcept(std::declval<viewed_type*>()->visit(std::declval<Func>()));
  3822. public:
  3823. template <typename Func>
  3824. decltype(auto) visit(Func&& visitor) const noexcept(visit_is_nothrow<Func&&>)
  3825. {
  3826. using return_type = decltype(node_->visit(static_cast<Func&&>(visitor)));
  3827. if (node_)
  3828. return node_->visit(static_cast<Func&&>(visitor));
  3829. if constexpr (!std::is_void_v<return_type>)
  3830. return return_type{};
  3831. }
  3832. public:
  3833. template <typename T>
  3834. TOML_PURE_GETTER
  3835. friend bool operator==(const node_view& lhs, const node_view<T>& rhs) noexcept
  3836. {
  3837. return impl::node_deep_equality(lhs.node_, rhs.node_);
  3838. }
  3839. template <typename T>
  3840. TOML_PURE_GETTER
  3841. friend bool operator!=(const node_view& lhs, const node_view<T>& rhs) noexcept
  3842. {
  3843. return !impl::node_deep_equality(lhs.node_, rhs.node_);
  3844. }
  3845. TOML_NODISCARD
  3846. friend bool operator==(const node_view& lhs, const table& rhs) noexcept
  3847. {
  3848. if (lhs.node_ == &rhs)
  3849. return true;
  3850. const auto tbl = lhs.as<table>();
  3851. return tbl && *tbl == rhs;
  3852. }
  3853. TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const table&, );
  3854. TOML_NODISCARD
  3855. friend bool operator==(const node_view& lhs, const array& rhs) noexcept
  3856. {
  3857. if (lhs.node_ == &rhs)
  3858. return true;
  3859. const auto arr = lhs.as<array>();
  3860. return arr && *arr == rhs;
  3861. }
  3862. TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const array&, );
  3863. template <typename T>
  3864. TOML_NODISCARD
  3865. friend bool operator==(const node_view& lhs, const toml::value<T>& rhs) noexcept
  3866. {
  3867. if (lhs.node_ == &rhs)
  3868. return true;
  3869. const auto val = lhs.as<T>();
  3870. return val && *val == rhs;
  3871. }
  3872. TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const toml::value<T>&, template <typename T>);
  3873. TOML_CONSTRAINED_TEMPLATE(impl::is_losslessly_convertible_to_native<T>, typename T)
  3874. TOML_NODISCARD
  3875. friend bool operator==(const node_view& lhs, const T& rhs) noexcept(!impl::is_wide_string<T>)
  3876. {
  3877. static_assert(!impl::is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
  3878. "Comparison with wide-character strings is only "
  3879. "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
  3880. if constexpr (impl::is_wide_string<T>)
  3881. {
  3882. #if TOML_ENABLE_WINDOWS_COMPAT
  3883. return lhs == impl::narrow(rhs);
  3884. #else
  3885. static_assert(impl::always_false<T>, "Evaluated unreachable branch!");
  3886. #endif
  3887. }
  3888. else
  3889. {
  3890. const auto val = lhs.as<impl::native_type_of<T>>();
  3891. return val && *val == rhs;
  3892. }
  3893. }
  3894. TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&,
  3895. const T&,
  3896. TOML_CONSTRAINED_TEMPLATE(impl::is_losslessly_convertible_to_native<T>,
  3897. typename T));
  3898. template <typename T>
  3899. TOML_NODISCARD
  3900. friend bool operator==(const node_view& lhs,
  3901. const std::initializer_list<T>& rhs) noexcept(!impl::is_wide_string<T>)
  3902. {
  3903. const auto arr = lhs.as<array>();
  3904. return arr && *arr == rhs;
  3905. }
  3906. TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const std::initializer_list<T>&, template <typename T>);
  3907. template <typename T>
  3908. TOML_NODISCARD
  3909. friend bool operator==(const node_view& lhs, const std::vector<T>& rhs) noexcept(!impl::is_wide_string<T>)
  3910. {
  3911. const auto arr = lhs.as<array>();
  3912. return arr && *arr == rhs;
  3913. }
  3914. TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const std::vector<T>&, template <typename T>);
  3915. TOML_NODISCARD
  3916. node_view operator[](std::string_view key) const noexcept
  3917. {
  3918. if (auto tbl = this->as_table())
  3919. return node_view{ tbl->get(key) };
  3920. return {};
  3921. }
  3922. TOML_NODISCARD
  3923. node_view operator[](const toml::path& path) const noexcept
  3924. {
  3925. return node_ ? node_->at_path(path) : node_view{};
  3926. }
  3927. TOML_NODISCARD
  3928. node_view at_path(std::string_view path) const noexcept
  3929. {
  3930. return node_ ? node_->at_path(path) : node_view{};
  3931. }
  3932. TOML_NODISCARD
  3933. node_view at_path(const toml::path& path) const noexcept
  3934. {
  3935. return node_ ? node_->at_path(path) : node_view{};
  3936. }
  3937. #if TOML_ENABLE_WINDOWS_COMPAT
  3938. TOML_NODISCARD
  3939. node_view operator[](std::wstring_view key) const
  3940. {
  3941. if (auto tbl = this->as_table())
  3942. return node_view{ tbl->get(key) };
  3943. return {};
  3944. }
  3945. TOML_NODISCARD
  3946. node_view at_path(std::wstring_view path) const
  3947. {
  3948. return node_ ? node_->at_path(path) : node_view{};
  3949. }
  3950. #endif // TOML_ENABLE_WINDOWS_COMPAT
  3951. TOML_NODISCARD
  3952. node_view operator[](size_t index) const noexcept
  3953. {
  3954. if (auto arr = this->as_array())
  3955. return node_view{ arr->get(index) };
  3956. return {};
  3957. }
  3958. #if TOML_ENABLE_FORMATTERS
  3959. friend std::ostream& operator<<(std::ostream& os, const node_view& nv)
  3960. {
  3961. if (nv.node_)
  3962. nv.node_->visit([&os](const auto& n) { os << n; });
  3963. return os;
  3964. }
  3965. #endif
  3966. };
  3967. template <typename T>
  3968. node_view(const T&) -> node_view<const node>;
  3969. template <typename T>
  3970. node_view(const T*) -> node_view<const node>;
  3971. template <typename T>
  3972. node_view(T&) -> node_view<node>;
  3973. template <typename T>
  3974. node_view(T*) -> node_view<node>;
  3975. }
  3976. TOML_NAMESPACE_END;
  3977. TOML_NAMESPACE_START
  3978. {
  3979. inline node::operator node_view<node>() noexcept
  3980. {
  3981. return node_view<node>{ this };
  3982. }
  3983. inline node::operator node_view<const node>() const noexcept
  3984. {
  3985. return node_view<const node>{ this };
  3986. }
  3987. }
  3988. TOML_NAMESPACE_END;
  3989. #ifdef _MSC_VER
  3990. #pragma pop_macro("min")
  3991. #pragma pop_macro("max")
  3992. #ifndef __clang__
  3993. #pragma inline_recursion(off)
  3994. #endif
  3995. #endif
  3996. TOML_POP_WARNINGS;
  3997. //******** impl/value.hpp ********************************************************************************************
  3998. TOML_PUSH_WARNINGS;
  3999. #ifdef _MSC_VER
  4000. #ifndef __clang__
  4001. #pragma inline_recursion(on)
  4002. #endif
  4003. #pragma push_macro("min")
  4004. #pragma push_macro("max")
  4005. #undef min
  4006. #undef max
  4007. #endif
  4008. TOML_DISABLE_ARITHMETIC_WARNINGS;
  4009. // clang-format off
  4010. #if TOML_ENABLE_WINDOWS_COMPAT
  4011. #define TOML_SA_VALUE_MESSAGE_WSTRING TOML_SA_LIST_SEP "std::wstring"
  4012. #else
  4013. #define TOML_SA_VALUE_MESSAGE_WSTRING
  4014. #endif
  4015. #if TOML_HAS_CHAR8
  4016. #define TOML_SA_VALUE_MESSAGE_U8STRING_VIEW TOML_SA_LIST_SEP "std::u8string_view"
  4017. #define TOML_SA_VALUE_MESSAGE_CONST_CHAR8 TOML_SA_LIST_SEP "const char8_t*"
  4018. #else
  4019. #define TOML_SA_VALUE_MESSAGE_U8STRING_VIEW
  4020. #define TOML_SA_VALUE_MESSAGE_CONST_CHAR8
  4021. #endif
  4022. #define TOML_SA_VALUE_EXACT_FUNC_MESSAGE(type_arg) \
  4023. "The " type_arg " must be one of:" \
  4024. TOML_SA_LIST_NEW "A native TOML value type" \
  4025. TOML_SA_NATIVE_VALUE_TYPE_LIST \
  4026. \
  4027. TOML_SA_LIST_NXT "A non-view type capable of losslessly representing a native TOML value type" \
  4028. TOML_SA_LIST_BEG "std::string" \
  4029. TOML_SA_VALUE_MESSAGE_WSTRING \
  4030. TOML_SA_LIST_SEP "any signed integer type >= 64 bits" \
  4031. TOML_SA_LIST_SEP "any floating-point type >= 64 bits" \
  4032. TOML_SA_LIST_END \
  4033. \
  4034. TOML_SA_LIST_NXT "An immutable view type not requiring additional temporary storage" \
  4035. TOML_SA_LIST_BEG "std::string_view" \
  4036. TOML_SA_VALUE_MESSAGE_U8STRING_VIEW \
  4037. TOML_SA_LIST_SEP "const char*" \
  4038. TOML_SA_VALUE_MESSAGE_CONST_CHAR8 \
  4039. TOML_SA_LIST_END
  4040. #define TOML_SA_VALUE_FUNC_MESSAGE(type_arg) \
  4041. "The " type_arg " must be one of:" \
  4042. TOML_SA_LIST_NEW "A native TOML value type" \
  4043. TOML_SA_NATIVE_VALUE_TYPE_LIST \
  4044. \
  4045. TOML_SA_LIST_NXT "A non-view type capable of losslessly representing a native TOML value type" \
  4046. TOML_SA_LIST_BEG "std::string" \
  4047. TOML_SA_VALUE_MESSAGE_WSTRING \
  4048. TOML_SA_LIST_SEP "any signed integer type >= 64 bits" \
  4049. TOML_SA_LIST_SEP "any floating-point type >= 64 bits" \
  4050. TOML_SA_LIST_END \
  4051. \
  4052. TOML_SA_LIST_NXT "A non-view type capable of (reasonably) representing a native TOML value type" \
  4053. TOML_SA_LIST_BEG "any other integral type" \
  4054. TOML_SA_LIST_SEP "any floating-point type" \
  4055. TOML_SA_LIST_END \
  4056. \
  4057. TOML_SA_LIST_NXT "An immutable view type not requiring additional temporary storage" \
  4058. TOML_SA_LIST_BEG "std::string_view" \
  4059. TOML_SA_VALUE_MESSAGE_U8STRING_VIEW \
  4060. TOML_SA_LIST_SEP "const char*" \
  4061. TOML_SA_VALUE_MESSAGE_CONST_CHAR8 \
  4062. TOML_SA_LIST_END
  4063. // clang-format on
  4064. TOML_IMPL_NAMESPACE_START
  4065. {
  4066. template <typename T, typename...>
  4067. struct native_value_maker
  4068. {
  4069. template <typename... Args>
  4070. TOML_NODISCARD
  4071. static T make(Args&&... args) noexcept(std::is_nothrow_constructible_v<T, Args&&...>)
  4072. {
  4073. if constexpr (std::is_aggregate_v<T>)
  4074. return T{ static_cast<Args&&>(args)... };
  4075. else
  4076. return T(static_cast<Args&&>(args)...);
  4077. }
  4078. };
  4079. template <typename T>
  4080. struct native_value_maker<T, T>
  4081. {
  4082. template <typename U>
  4083. TOML_NODISCARD
  4084. TOML_ALWAYS_INLINE
  4085. static U&& make(U&& val) noexcept
  4086. {
  4087. return static_cast<U&&>(val);
  4088. }
  4089. };
  4090. #if TOML_HAS_CHAR8 || TOML_ENABLE_WINDOWS_COMPAT
  4091. struct string_maker
  4092. {
  4093. template <typename T>
  4094. TOML_NODISCARD
  4095. static std::string make(T&& arg)
  4096. {
  4097. using arg_type = std::decay_t<T>;
  4098. #if TOML_HAS_CHAR8
  4099. if constexpr (is_one_of<arg_type, char8_t*, const char8_t*>)
  4100. {
  4101. return std::string(reinterpret_cast<const char*>(static_cast<const char8_t*>(arg)));
  4102. }
  4103. if constexpr (is_one_of<arg_type, std::u8string, std::u8string_view>)
  4104. {
  4105. return std::string(reinterpret_cast<const char*>(static_cast<const char8_t*>(arg.data())),
  4106. arg.length());
  4107. }
  4108. #endif
  4109. #if TOML_ENABLE_WINDOWS_COMPAT
  4110. if constexpr (is_wide_string<arg_type>)
  4111. {
  4112. return narrow(static_cast<T&&>(arg));
  4113. }
  4114. #endif
  4115. }
  4116. };
  4117. #if TOML_HAS_CHAR8
  4118. template <>
  4119. struct native_value_maker<std::string, char8_t*> : string_maker
  4120. {};
  4121. template <>
  4122. struct native_value_maker<std::string, const char8_t*> : string_maker
  4123. {};
  4124. template <>
  4125. struct native_value_maker<std::string, std::u8string> : string_maker
  4126. {};
  4127. template <>
  4128. struct native_value_maker<std::string, std::u8string_view> : string_maker
  4129. {};
  4130. #endif // TOML_HAS_CHAR8
  4131. #if TOML_ENABLE_WINDOWS_COMPAT
  4132. template <>
  4133. struct native_value_maker<std::string, wchar_t*> : string_maker
  4134. {};
  4135. template <>
  4136. struct native_value_maker<std::string, const wchar_t*> : string_maker
  4137. {};
  4138. template <>
  4139. struct native_value_maker<std::string, std::wstring> : string_maker
  4140. {};
  4141. template <>
  4142. struct native_value_maker<std::string, std::wstring_view> : string_maker
  4143. {};
  4144. #endif // TOML_ENABLE_WINDOWS_COMPAT
  4145. #endif // TOML_HAS_CHAR8 || TOML_ENABLE_WINDOWS_COMPAT
  4146. template <typename T>
  4147. TOML_CONST_GETTER
  4148. inline optional<T> node_integer_cast(int64_t val) noexcept
  4149. {
  4150. static_assert(node_type_of<T> == node_type::integer);
  4151. static_assert(!is_cvref<T>);
  4152. using traits = value_traits<T>;
  4153. if constexpr (!traits::is_signed)
  4154. {
  4155. if constexpr ((sizeof(T) * CHAR_BIT) < 63) // 63 bits == int64_max
  4156. {
  4157. using common_t = decltype(int64_t{} + T{});
  4158. if (val < int64_t{} || static_cast<common_t>(val) > static_cast<common_t>(traits::max))
  4159. return {};
  4160. }
  4161. else
  4162. {
  4163. if (val < int64_t{})
  4164. return {};
  4165. }
  4166. }
  4167. else
  4168. {
  4169. if (val < traits::min || val > traits::max)
  4170. return {};
  4171. }
  4172. return { static_cast<T>(val) };
  4173. }
  4174. template <typename...>
  4175. struct value_variadic_ctor_allowed : std::true_type
  4176. {};
  4177. template <typename T, typename... Args>
  4178. struct value_variadic_ctor_allowed<value<T>, value<T>, Args...> : std::false_type
  4179. {};
  4180. }
  4181. TOML_IMPL_NAMESPACE_END;
  4182. TOML_NAMESPACE_START
  4183. {
  4184. template <typename ValueType>
  4185. class value : public node
  4186. {
  4187. static_assert(impl::is_native<ValueType> && !impl::is_cvref<ValueType>,
  4188. "A toml::value<> must model one of the native TOML value types:" TOML_SA_NATIVE_VALUE_TYPE_LIST);
  4189. private:
  4190. friend class TOML_PARSER_TYPENAME;
  4191. template <typename T, typename U>
  4192. TOML_CONST_INLINE_GETTER
  4193. static auto as_value([[maybe_unused]] U* ptr) noexcept
  4194. {
  4195. if constexpr (std::is_same_v<value_type, T>)
  4196. return ptr;
  4197. else
  4198. return nullptr;
  4199. }
  4200. ValueType val_;
  4201. value_flags flags_ = value_flags::none;
  4202. public:
  4203. using value_type = ValueType;
  4204. using value_arg = POXY_IMPLEMENTATION_DETAIL(
  4205. std::conditional_t<
  4206. std::is_same_v<value_type, std::string>,
  4207. std::string_view,
  4208. std::conditional_t<impl::is_one_of<value_type, double, int64_t, bool>, value_type, const value_type&>>);
  4209. TOML_HIDDEN_CONSTRAINT(
  4210. (impl::value_variadic_ctor_allowed<value<ValueType>, impl::remove_cvref<Args>...>::value),
  4211. typename... Args)
  4212. TOML_NODISCARD_CTOR
  4213. explicit value(Args&&... args) noexcept(noexcept(value_type(
  4214. impl::native_value_maker<value_type, std::decay_t<Args>...>::make(static_cast<Args&&>(args)...))))
  4215. : val_(impl::native_value_maker<value_type, std::decay_t<Args>...>::make(static_cast<Args&&>(args)...))
  4216. {
  4217. #if TOML_LIFETIME_HOOKS
  4218. TOML_VALUE_CREATED;
  4219. #endif
  4220. }
  4221. TOML_NODISCARD_CTOR
  4222. value(const value& other) noexcept //
  4223. : node(other),
  4224. val_{ other.val_ },
  4225. flags_{ other.flags_ }
  4226. {
  4227. #if TOML_LIFETIME_HOOKS
  4228. TOML_VALUE_CREATED;
  4229. #endif
  4230. }
  4231. TOML_NODISCARD_CTOR
  4232. value(const value& other, value_flags flags) noexcept //
  4233. : node(other),
  4234. val_{ other.val_ },
  4235. flags_{ flags == preserve_source_value_flags ? other.flags_ : flags }
  4236. {
  4237. #if TOML_LIFETIME_HOOKS
  4238. TOML_VALUE_CREATED;
  4239. #endif
  4240. }
  4241. TOML_NODISCARD_CTOR
  4242. value(value&& other) noexcept //
  4243. : node(std::move(other)),
  4244. val_{ std::move(other.val_) },
  4245. flags_{ std::exchange(other.flags_, value_flags{}) }
  4246. {
  4247. #if TOML_LIFETIME_HOOKS
  4248. TOML_VALUE_CREATED;
  4249. #endif
  4250. }
  4251. TOML_NODISCARD_CTOR
  4252. value(value&& other, value_flags flags) noexcept //
  4253. : node(std::move(other)),
  4254. val_{ std::move(other.val_) },
  4255. flags_{ flags == preserve_source_value_flags ? other.flags_ : flags }
  4256. {
  4257. #if TOML_LIFETIME_HOOKS
  4258. TOML_VALUE_CREATED;
  4259. #endif
  4260. other.flags_ = {};
  4261. }
  4262. value& operator=(const value& rhs) noexcept
  4263. {
  4264. node::operator=(rhs);
  4265. val_ = rhs.val_;
  4266. flags_ = rhs.flags_;
  4267. return *this;
  4268. }
  4269. value& operator=(value&& rhs) noexcept
  4270. {
  4271. if (&rhs != this)
  4272. {
  4273. node::operator=(std::move(rhs));
  4274. val_ = std::move(rhs.val_);
  4275. flags_ = std::exchange(rhs.flags_, value_flags{});
  4276. }
  4277. return *this;
  4278. }
  4279. #if TOML_LIFETIME_HOOKS
  4280. ~value() noexcept
  4281. {
  4282. TOML_VALUE_DESTROYED;
  4283. }
  4284. #endif
  4285. TOML_CONST_INLINE_GETTER
  4286. node_type type() const noexcept final
  4287. {
  4288. return impl::node_type_of<value_type>;
  4289. }
  4290. TOML_PURE_GETTER
  4291. bool is_homogeneous(node_type ntype) const noexcept final
  4292. {
  4293. return ntype == node_type::none || ntype == impl::node_type_of<value_type>;
  4294. }
  4295. TOML_PURE_GETTER
  4296. bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept final
  4297. {
  4298. if (ntype != node_type::none && ntype != impl::node_type_of<value_type>)
  4299. {
  4300. first_nonmatch = this;
  4301. return false;
  4302. }
  4303. return true;
  4304. }
  4305. TOML_PURE_GETTER
  4306. bool is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept final
  4307. {
  4308. if (ntype != node_type::none && ntype != impl::node_type_of<value_type>)
  4309. {
  4310. first_nonmatch = this;
  4311. return false;
  4312. }
  4313. return true;
  4314. }
  4315. template <typename ElemType = void>
  4316. TOML_PURE_GETTER
  4317. bool is_homogeneous() const noexcept
  4318. {
  4319. using type = impl::remove_cvref<impl::unwrap_node<ElemType>>;
  4320. static_assert(std::is_void_v<type> || toml::is_value<type> || toml::is_container<type>,
  4321. "The template type argument of value::is_homogeneous() must be void or one "
  4322. "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
  4323. if constexpr (std::is_void_v<type>)
  4324. return true;
  4325. else
  4326. return impl::node_type_of<type> == impl::node_type_of<value_type>;
  4327. }
  4328. TOML_CONST_INLINE_GETTER
  4329. bool is_table() const noexcept final
  4330. {
  4331. return false;
  4332. }
  4333. TOML_CONST_INLINE_GETTER
  4334. bool is_array() const noexcept final
  4335. {
  4336. return false;
  4337. }
  4338. TOML_CONST_INLINE_GETTER
  4339. bool is_array_of_tables() const noexcept final
  4340. {
  4341. return false;
  4342. }
  4343. TOML_CONST_INLINE_GETTER
  4344. bool is_value() const noexcept final
  4345. {
  4346. return true;
  4347. }
  4348. TOML_CONST_INLINE_GETTER
  4349. bool is_string() const noexcept final
  4350. {
  4351. return std::is_same_v<value_type, std::string>;
  4352. }
  4353. TOML_CONST_INLINE_GETTER
  4354. bool is_integer() const noexcept final
  4355. {
  4356. return std::is_same_v<value_type, int64_t>;
  4357. }
  4358. TOML_CONST_INLINE_GETTER
  4359. bool is_floating_point() const noexcept final
  4360. {
  4361. return std::is_same_v<value_type, double>;
  4362. }
  4363. TOML_CONST_INLINE_GETTER
  4364. bool is_number() const noexcept final
  4365. {
  4366. return impl::is_one_of<value_type, int64_t, double>;
  4367. }
  4368. TOML_CONST_INLINE_GETTER
  4369. bool is_boolean() const noexcept final
  4370. {
  4371. return std::is_same_v<value_type, bool>;
  4372. }
  4373. TOML_CONST_INLINE_GETTER
  4374. bool is_date() const noexcept final
  4375. {
  4376. return std::is_same_v<value_type, date>;
  4377. }
  4378. TOML_CONST_INLINE_GETTER
  4379. bool is_time() const noexcept final
  4380. {
  4381. return std::is_same_v<value_type, time>;
  4382. }
  4383. TOML_CONST_INLINE_GETTER
  4384. bool is_date_time() const noexcept final
  4385. {
  4386. return std::is_same_v<value_type, date_time>;
  4387. }
  4388. TOML_CONST_INLINE_GETTER
  4389. table* as_table() noexcept final
  4390. {
  4391. return nullptr;
  4392. }
  4393. TOML_CONST_INLINE_GETTER
  4394. array* as_array() noexcept final
  4395. {
  4396. return nullptr;
  4397. }
  4398. TOML_CONST_INLINE_GETTER
  4399. value<std::string>* as_string() noexcept final
  4400. {
  4401. return as_value<std::string>(this);
  4402. }
  4403. TOML_CONST_INLINE_GETTER
  4404. value<int64_t>* as_integer() noexcept final
  4405. {
  4406. return as_value<int64_t>(this);
  4407. }
  4408. TOML_CONST_INLINE_GETTER
  4409. value<double>* as_floating_point() noexcept final
  4410. {
  4411. return as_value<double>(this);
  4412. }
  4413. TOML_CONST_INLINE_GETTER
  4414. value<bool>* as_boolean() noexcept final
  4415. {
  4416. return as_value<bool>(this);
  4417. }
  4418. TOML_CONST_INLINE_GETTER
  4419. value<date>* as_date() noexcept final
  4420. {
  4421. return as_value<date>(this);
  4422. }
  4423. TOML_CONST_INLINE_GETTER
  4424. value<time>* as_time() noexcept final
  4425. {
  4426. return as_value<time>(this);
  4427. }
  4428. TOML_CONST_INLINE_GETTER
  4429. value<date_time>* as_date_time() noexcept final
  4430. {
  4431. return as_value<date_time>(this);
  4432. }
  4433. TOML_CONST_INLINE_GETTER
  4434. const table* as_table() const noexcept final
  4435. {
  4436. return nullptr;
  4437. }
  4438. TOML_CONST_INLINE_GETTER
  4439. const array* as_array() const noexcept final
  4440. {
  4441. return nullptr;
  4442. }
  4443. TOML_CONST_INLINE_GETTER
  4444. const value<std::string>* as_string() const noexcept final
  4445. {
  4446. return as_value<std::string>(this);
  4447. }
  4448. TOML_CONST_INLINE_GETTER
  4449. const value<int64_t>* as_integer() const noexcept final
  4450. {
  4451. return as_value<int64_t>(this);
  4452. }
  4453. TOML_CONST_INLINE_GETTER
  4454. const value<double>* as_floating_point() const noexcept final
  4455. {
  4456. return as_value<double>(this);
  4457. }
  4458. TOML_CONST_INLINE_GETTER
  4459. const value<bool>* as_boolean() const noexcept final
  4460. {
  4461. return as_value<bool>(this);
  4462. }
  4463. TOML_CONST_INLINE_GETTER
  4464. const value<date>* as_date() const noexcept final
  4465. {
  4466. return as_value<date>(this);
  4467. }
  4468. TOML_CONST_INLINE_GETTER
  4469. const value<time>* as_time() const noexcept final
  4470. {
  4471. return as_value<time>(this);
  4472. }
  4473. TOML_CONST_INLINE_GETTER
  4474. const value<date_time>* as_date_time() const noexcept final
  4475. {
  4476. return as_value<date_time>(this);
  4477. }
  4478. TOML_PURE_INLINE_GETTER
  4479. value_type& get() & noexcept
  4480. {
  4481. return val_;
  4482. }
  4483. TOML_PURE_INLINE_GETTER
  4484. value_type&& get() && noexcept
  4485. {
  4486. return static_cast<value_type&&>(val_);
  4487. }
  4488. TOML_PURE_INLINE_GETTER
  4489. const value_type& get() const& noexcept
  4490. {
  4491. return val_;
  4492. }
  4493. TOML_PURE_INLINE_GETTER
  4494. const value_type&& get() const&& noexcept
  4495. {
  4496. return static_cast<const value_type&&>(val_);
  4497. }
  4498. TOML_PURE_INLINE_GETTER
  4499. value_type& operator*() & noexcept
  4500. {
  4501. return val_;
  4502. }
  4503. TOML_PURE_INLINE_GETTER
  4504. value_type&& operator*() && noexcept
  4505. {
  4506. return static_cast<value_type&&>(val_);
  4507. }
  4508. TOML_PURE_INLINE_GETTER
  4509. const value_type& operator*() const& noexcept
  4510. {
  4511. return val_;
  4512. }
  4513. TOML_PURE_INLINE_GETTER
  4514. const value_type&& operator*() const&& noexcept
  4515. {
  4516. return static_cast<const value_type&&>(val_);
  4517. }
  4518. TOML_PURE_INLINE_GETTER
  4519. explicit operator value_type&() & noexcept
  4520. {
  4521. return val_;
  4522. }
  4523. TOML_PURE_INLINE_GETTER
  4524. explicit operator value_type&&() && noexcept
  4525. {
  4526. return static_cast<value_type&&>(val_);
  4527. }
  4528. TOML_PURE_INLINE_GETTER
  4529. explicit operator const value_type&() const& noexcept
  4530. {
  4531. return val_;
  4532. }
  4533. TOML_PURE_INLINE_GETTER
  4534. explicit operator const value_type&&() && noexcept
  4535. {
  4536. return static_cast<const value_type&&>(val_);
  4537. }
  4538. TOML_HIDDEN_CONSTRAINT(std::is_class_v<T>, typename T = value_type)
  4539. TOML_PURE_INLINE_GETTER
  4540. value_type* operator->() noexcept
  4541. {
  4542. return &val_;
  4543. }
  4544. TOML_HIDDEN_CONSTRAINT(std::is_class_v<T>, typename T = value_type)
  4545. TOML_PURE_INLINE_GETTER
  4546. const value_type* operator->() const noexcept
  4547. {
  4548. return &val_;
  4549. }
  4550. TOML_NODISCARD
  4551. value_flags flags() const noexcept
  4552. {
  4553. return flags_;
  4554. }
  4555. value& flags(value_flags new_flags) noexcept
  4556. {
  4557. flags_ = new_flags;
  4558. return *this;
  4559. }
  4560. value& operator=(value_arg rhs) noexcept
  4561. {
  4562. if constexpr (std::is_same_v<value_type, std::string>)
  4563. val_.assign(rhs);
  4564. else
  4565. val_ = rhs;
  4566. return *this;
  4567. }
  4568. TOML_CONSTRAINED_TEMPLATE((std::is_same_v<T, std::string>), typename T = value_type)
  4569. value& operator=(std::string&& rhs) noexcept
  4570. {
  4571. val_ = std::move(rhs);
  4572. return *this;
  4573. }
  4574. TOML_PURE_GETTER
  4575. friend bool operator==(const value& lhs, value_arg rhs) noexcept
  4576. {
  4577. if constexpr (std::is_same_v<value_type, double>)
  4578. {
  4579. const auto lhs_nan = impl::fpclassify(lhs.val_) == impl::fp_class::nan;
  4580. const auto rhs_nan = impl::fpclassify(rhs) == impl::fp_class::nan;
  4581. if (lhs_nan != rhs_nan)
  4582. return false;
  4583. if (lhs_nan)
  4584. return true;
  4585. }
  4586. return lhs.val_ == rhs;
  4587. }
  4588. TOML_ASYMMETRICAL_EQUALITY_OPS(const value&, value_arg, );
  4589. TOML_PURE_GETTER
  4590. friend bool operator<(const value& lhs, value_arg rhs) noexcept
  4591. {
  4592. return lhs.val_ < rhs;
  4593. }
  4594. TOML_PURE_GETTER
  4595. friend bool operator<(value_arg lhs, const value& rhs) noexcept
  4596. {
  4597. return lhs < rhs.val_;
  4598. }
  4599. TOML_PURE_GETTER
  4600. friend bool operator<=(const value& lhs, value_arg rhs) noexcept
  4601. {
  4602. return lhs.val_ <= rhs;
  4603. }
  4604. TOML_PURE_GETTER
  4605. friend bool operator<=(value_arg lhs, const value& rhs) noexcept
  4606. {
  4607. return lhs <= rhs.val_;
  4608. }
  4609. TOML_PURE_GETTER
  4610. friend bool operator>(const value& lhs, value_arg rhs) noexcept
  4611. {
  4612. return lhs.val_ > rhs;
  4613. }
  4614. TOML_PURE_GETTER
  4615. friend bool operator>(value_arg lhs, const value& rhs) noexcept
  4616. {
  4617. return lhs > rhs.val_;
  4618. }
  4619. TOML_PURE_GETTER
  4620. friend bool operator>=(const value& lhs, value_arg rhs) noexcept
  4621. {
  4622. return lhs.val_ >= rhs;
  4623. }
  4624. TOML_PURE_GETTER
  4625. friend bool operator>=(value_arg lhs, const value& rhs) noexcept
  4626. {
  4627. return lhs >= rhs.val_;
  4628. }
  4629. template <typename T>
  4630. TOML_PURE_GETTER
  4631. friend bool operator==(const value& lhs, const value<T>& rhs) noexcept
  4632. {
  4633. if constexpr (std::is_same_v<value_type, T>)
  4634. return lhs == rhs.val_; // calls asymmetrical value-equality operator defined above
  4635. else
  4636. return false;
  4637. }
  4638. template <typename T>
  4639. TOML_PURE_INLINE_GETTER
  4640. friend bool operator!=(const value& lhs, const value<T>& rhs) noexcept
  4641. {
  4642. return !(lhs == rhs);
  4643. }
  4644. template <typename T>
  4645. TOML_PURE_GETTER
  4646. friend bool operator<(const value& lhs, const value<T>& rhs) noexcept
  4647. {
  4648. if constexpr (std::is_same_v<value_type, T>)
  4649. return lhs.val_ < rhs.val_;
  4650. else
  4651. return impl::node_type_of<value_type> < impl::node_type_of<T>;
  4652. }
  4653. template <typename T>
  4654. TOML_PURE_GETTER
  4655. friend bool operator<=(const value& lhs, const value<T>& rhs) noexcept
  4656. {
  4657. if constexpr (std::is_same_v<value_type, T>)
  4658. return lhs.val_ <= rhs.val_;
  4659. else
  4660. return impl::node_type_of<value_type> <= impl::node_type_of<T>;
  4661. }
  4662. template <typename T>
  4663. TOML_PURE_GETTER
  4664. friend bool operator>(const value& lhs, const value<T>& rhs) noexcept
  4665. {
  4666. if constexpr (std::is_same_v<value_type, T>)
  4667. return lhs.val_ > rhs.val_;
  4668. else
  4669. return impl::node_type_of<value_type> > impl::node_type_of<T>;
  4670. }
  4671. template <typename T>
  4672. TOML_PURE_GETTER
  4673. friend bool operator>=(const value& lhs, const value<T>& rhs) noexcept
  4674. {
  4675. if constexpr (std::is_same_v<value_type, T>)
  4676. return lhs.val_ >= rhs.val_;
  4677. else
  4678. return impl::node_type_of<value_type> >= impl::node_type_of<T>;
  4679. }
  4680. #if TOML_ENABLE_FORMATTERS
  4681. friend std::ostream& operator<<(std::ostream& lhs, const value& rhs)
  4682. {
  4683. impl::print_to_stream(lhs, rhs);
  4684. return lhs;
  4685. }
  4686. #endif
  4687. };
  4688. template <typename T>
  4689. value(T) -> value<impl::native_type_of<impl::remove_cvref<T>>>;
  4690. template <typename T>
  4691. value(T, value_flags) -> value<impl::native_type_of<impl::remove_cvref<T>>>;
  4692. template <typename T>
  4693. TOML_NODISCARD
  4694. inline decltype(auto) node::get_value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>)
  4695. {
  4696. using namespace impl;
  4697. static_assert(node_type_of<T> != node_type::none);
  4698. static_assert(node_type_of<T> != node_type::table);
  4699. static_assert(node_type_of<T> != node_type::array);
  4700. static_assert(is_native<T> || can_represent_native<T>);
  4701. static_assert(!is_cvref<T>);
  4702. TOML_ASSERT(this->type() == node_type_of<T>);
  4703. if constexpr (node_type_of<T> == node_type::string)
  4704. {
  4705. const auto& str = *ref_cast<std::string>();
  4706. if constexpr (std::is_same_v<T, std::string>)
  4707. return str;
  4708. else if constexpr (std::is_same_v<T, std::string_view>)
  4709. return T{ str };
  4710. else if constexpr (std::is_same_v<T, const char*>)
  4711. return str.c_str();
  4712. else if constexpr (std::is_same_v<T, std::wstring>)
  4713. {
  4714. #if TOML_ENABLE_WINDOWS_COMPAT
  4715. return widen(str);
  4716. #else
  4717. static_assert(always_false<T>, "Evaluated unreachable branch!");
  4718. #endif
  4719. }
  4720. #if TOML_HAS_CHAR8
  4721. // char -> char8_t (potentially unsafe - the feature is 'experimental'!)
  4722. else if constexpr (is_one_of<T, std::u8string, std::u8string_view>)
  4723. return T(reinterpret_cast<const char8_t*>(str.c_str()), str.length());
  4724. else if constexpr (std::is_same_v<T, const char8_t*>)
  4725. return reinterpret_cast<const char8_t*>(str.c_str());
  4726. else
  4727. static_assert(always_false<T>, "Evaluated unreachable branch!");
  4728. #endif
  4729. }
  4730. else
  4731. return static_cast<T>(*ref_cast<native_type_of<T>>());
  4732. }
  4733. template <typename T>
  4734. TOML_NODISCARD
  4735. inline optional<T> node::value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>)
  4736. {
  4737. using namespace impl;
  4738. static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
  4739. "Retrieving values as wide-character strings with node::value_exact() is only "
  4740. "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
  4741. static_assert((is_native<T> || can_represent_native<T>)&&!is_cvref<T>,
  4742. TOML_SA_VALUE_EXACT_FUNC_MESSAGE("return type of node::value_exact()"));
  4743. // prevent additional compiler error spam when the static_assert fails by gating behind if constexpr
  4744. if constexpr ((is_native<T> || can_represent_native<T>)&&!is_cvref<T>)
  4745. {
  4746. if (type() == node_type_of<T>)
  4747. return { this->get_value_exact<T>() };
  4748. else
  4749. return {};
  4750. }
  4751. }
  4752. template <typename T>
  4753. TOML_NODISCARD
  4754. inline optional<T> node::value() const noexcept(impl::value_retrieval_is_nothrow<T>)
  4755. {
  4756. using namespace impl;
  4757. static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
  4758. "Retrieving values as wide-character strings with node::value() is only "
  4759. "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
  4760. static_assert((is_native<T> || can_represent_native<T> || can_partially_represent_native<T>)&&!is_cvref<T>,
  4761. TOML_SA_VALUE_FUNC_MESSAGE("return type of node::value()"));
  4762. // when asking for strings, dates, times and date_times there's no 'fuzzy' conversion
  4763. // semantics to be mindful of so the exact retrieval is enough.
  4764. if constexpr (is_natively_one_of<T, std::string, time, date, date_time>)
  4765. {
  4766. if (type() == node_type_of<T>)
  4767. return { this->get_value_exact<T>() };
  4768. else
  4769. return {};
  4770. }
  4771. // everything else requires a bit of logicking.
  4772. else
  4773. {
  4774. switch (type())
  4775. {
  4776. // int -> *
  4777. case node_type::integer:
  4778. {
  4779. // int -> int
  4780. if constexpr (is_natively_one_of<T, int64_t>)
  4781. {
  4782. if constexpr (is_native<T> || can_represent_native<T>)
  4783. return static_cast<T>(*ref_cast<int64_t>());
  4784. else
  4785. return node_integer_cast<T>(*ref_cast<int64_t>());
  4786. }
  4787. // int -> float
  4788. else if constexpr (is_natively_one_of<T, double>)
  4789. {
  4790. const int64_t val = *ref_cast<int64_t>();
  4791. if constexpr (std::numeric_limits<T>::digits < 64)
  4792. {
  4793. constexpr auto largest_whole_float = (int64_t{ 1 } << std::numeric_limits<T>::digits);
  4794. if (val < -largest_whole_float || val > largest_whole_float)
  4795. return {};
  4796. }
  4797. return static_cast<T>(val);
  4798. }
  4799. // int -> bool
  4800. else if constexpr (is_natively_one_of<T, bool>)
  4801. return static_cast<bool>(*ref_cast<int64_t>());
  4802. // int -> anything else
  4803. else
  4804. return {};
  4805. }
  4806. // float -> *
  4807. case node_type::floating_point:
  4808. {
  4809. // float -> float
  4810. if constexpr (is_natively_one_of<T, double>)
  4811. {
  4812. if constexpr (is_native<T> || can_represent_native<T>)
  4813. return { static_cast<T>(*ref_cast<double>()) };
  4814. else
  4815. {
  4816. const double val = *ref_cast<double>();
  4817. if (impl::fpclassify(val) == fp_class::ok
  4818. && (val < (std::numeric_limits<T>::lowest)() || val > (std::numeric_limits<T>::max)()))
  4819. return {};
  4820. return { static_cast<T>(val) };
  4821. }
  4822. }
  4823. // float -> int
  4824. else if constexpr (is_natively_one_of<T, int64_t>)
  4825. {
  4826. const double val = *ref_cast<double>();
  4827. if (impl::fpclassify(val) == fp_class::ok
  4828. && static_cast<double>(static_cast<int64_t>(val)) == val)
  4829. return node_integer_cast<T>(static_cast<int64_t>(val));
  4830. else
  4831. return {};
  4832. }
  4833. // float -> anything else
  4834. else
  4835. return {};
  4836. }
  4837. // bool -> *
  4838. case node_type::boolean:
  4839. {
  4840. // bool -> bool
  4841. if constexpr (is_natively_one_of<T, bool>)
  4842. return { *ref_cast<bool>() };
  4843. // bool -> int
  4844. else if constexpr (is_natively_one_of<T, int64_t>)
  4845. return { static_cast<T>(*ref_cast<bool>()) };
  4846. // bool -> anything else
  4847. else
  4848. return {};
  4849. }
  4850. }
  4851. // non-values, or 'exact' types covered above
  4852. return {};
  4853. }
  4854. }
  4855. template <typename T>
  4856. TOML_NODISCARD
  4857. inline auto node::value_or(T && default_value) const noexcept(impl::value_retrieval_is_nothrow<T>)
  4858. {
  4859. using namespace impl;
  4860. static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
  4861. "Retrieving values as wide-character strings with node::value_or() is only "
  4862. "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
  4863. if constexpr (is_wide_string<T>)
  4864. {
  4865. #if TOML_ENABLE_WINDOWS_COMPAT
  4866. if (type() == node_type::string)
  4867. return widen(*ref_cast<std::string>());
  4868. return std::wstring{ static_cast<T&&>(default_value) };
  4869. #else
  4870. static_assert(always_false<T>, "Evaluated unreachable branch!");
  4871. #endif
  4872. }
  4873. else
  4874. {
  4875. using value_type =
  4876. std::conditional_t<std::is_pointer_v<std::decay_t<T>>,
  4877. std::add_pointer_t<std::add_const_t<std::remove_pointer_t<std::decay_t<T>>>>,
  4878. std::decay_t<T>>;
  4879. using traits = value_traits<value_type>;
  4880. // clang-format off
  4881. static_assert(
  4882. traits::is_native || traits::can_represent_native || traits::can_partially_represent_native,
  4883. "The default value type of node::value_or() must be one of:"
  4884. TOML_SA_LIST_NEW "A native TOML value type"
  4885. TOML_SA_NATIVE_VALUE_TYPE_LIST
  4886. TOML_SA_LIST_NXT "A non-view type capable of losslessly representing a native TOML value type"
  4887. TOML_SA_LIST_BEG "std::string"
  4888. #if TOML_ENABLE_WINDOWS_COMPAT
  4889. TOML_SA_LIST_SEP "std::wstring"
  4890. #endif
  4891. TOML_SA_LIST_SEP "any signed integer type >= 64 bits"
  4892. TOML_SA_LIST_SEP "any floating-point type >= 64 bits"
  4893. TOML_SA_LIST_END
  4894. TOML_SA_LIST_NXT "A non-view type capable of (reasonably) representing a native TOML value type"
  4895. TOML_SA_LIST_BEG "any other integral type"
  4896. TOML_SA_LIST_SEP "any floating-point type"
  4897. TOML_SA_LIST_END
  4898. TOML_SA_LIST_NXT "A compatible view type"
  4899. TOML_SA_LIST_BEG "std::string_view"
  4900. #if TOML_HAS_CHAR8
  4901. TOML_SA_LIST_SEP "std::u8string_view"
  4902. #endif
  4903. #if TOML_ENABLE_WINDOWS_COMPAT
  4904. TOML_SA_LIST_SEP "std::wstring_view"
  4905. #endif
  4906. TOML_SA_LIST_SEP "const char*"
  4907. #if TOML_HAS_CHAR8
  4908. TOML_SA_LIST_SEP "const char8_t*"
  4909. #endif
  4910. #if TOML_ENABLE_WINDOWS_COMPAT
  4911. TOML_SA_LIST_SEP "const wchar_t*"
  4912. #endif
  4913. TOML_SA_LIST_END
  4914. );
  4915. // clang-format on
  4916. // prevent additional compiler error spam when the static_assert fails by gating behind if constexpr
  4917. if constexpr (traits::is_native || traits::can_represent_native || traits::can_partially_represent_native)
  4918. {
  4919. if constexpr (traits::is_native)
  4920. {
  4921. if (type() == node_type_of<value_type>)
  4922. return *ref_cast<typename traits::native_type>();
  4923. }
  4924. if (auto val = this->value<value_type>())
  4925. return *val;
  4926. if constexpr (std::is_pointer_v<value_type>)
  4927. return value_type{ default_value };
  4928. else
  4929. return static_cast<T&&>(default_value);
  4930. }
  4931. }
  4932. }
  4933. }
  4934. TOML_NAMESPACE_END;
  4935. #ifdef _MSC_VER
  4936. #pragma pop_macro("min")
  4937. #pragma pop_macro("max")
  4938. #ifndef __clang__
  4939. #pragma inline_recursion(off)
  4940. #endif
  4941. #endif
  4942. TOML_POP_WARNINGS;
  4943. //******** impl/make_node.hpp ****************************************************************************************
  4944. TOML_PUSH_WARNINGS;
  4945. #ifdef _MSC_VER
  4946. #ifndef __clang__
  4947. #pragma inline_recursion(on)
  4948. #endif
  4949. #pragma push_macro("min")
  4950. #pragma push_macro("max")
  4951. #undef min
  4952. #undef max
  4953. #endif
  4954. TOML_IMPL_NAMESPACE_START
  4955. {
  4956. template <typename T>
  4957. TOML_NODISCARD
  4958. TOML_ATTR(returns_nonnull)
  4959. auto* make_node_impl_specialized(T && val, [[maybe_unused]] value_flags flags)
  4960. {
  4961. using unwrapped_type = unwrap_node<remove_cvref<T>>;
  4962. static_assert(!std::is_same_v<unwrapped_type, node>);
  4963. static_assert(!is_node_view<unwrapped_type>);
  4964. // arrays + tables - invoke copy/move ctor
  4965. if constexpr (is_one_of<unwrapped_type, array, table>)
  4966. {
  4967. return new unwrapped_type(static_cast<T&&>(val));
  4968. }
  4969. // values
  4970. else
  4971. {
  4972. using native_type = native_type_of<unwrapped_type>;
  4973. using value_type = value<native_type>;
  4974. value_type* out;
  4975. // copy/move ctor
  4976. if constexpr (std::is_same_v<remove_cvref<T>, value_type>)
  4977. {
  4978. out = new value_type{ static_cast<T&&>(val), flags };
  4979. }
  4980. // creating from raw value
  4981. else
  4982. {
  4983. static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
  4984. "Instantiating values from wide-character strings is only "
  4985. "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
  4986. if constexpr (!is_losslessly_convertible_to_native<unwrapped_type>)
  4987. {
  4988. if constexpr (std::is_same_v<native_type, int64_t>)
  4989. static_assert(always_false<T>,
  4990. "Integral value initializers must be losslessly convertible to int64_t");
  4991. else if constexpr (std::is_same_v<native_type, double>)
  4992. static_assert(always_false<T>,
  4993. "Floating-point value initializers must be losslessly convertible to double");
  4994. else
  4995. static_assert(
  4996. always_false<T>,
  4997. "Value initializers must be losslessly convertible to one of the TOML value types");
  4998. }
  4999. if constexpr (is_wide_string<T>)
  5000. {
  5001. #if TOML_ENABLE_WINDOWS_COMPAT
  5002. out = new value_type{ narrow(static_cast<T&&>(val)) };
  5003. #else
  5004. static_assert(always_false<T>, "Evaluated unreachable branch!");
  5005. #endif
  5006. }
  5007. else
  5008. out = new value_type{ static_cast<T&&>(val) };
  5009. if (flags != preserve_source_value_flags)
  5010. out->flags(flags);
  5011. }
  5012. return out;
  5013. }
  5014. }
  5015. template <typename T>
  5016. TOML_NODISCARD
  5017. auto* make_node_impl(T && val, value_flags flags = preserve_source_value_flags)
  5018. {
  5019. using unwrapped_type = unwrap_node<remove_cvref<T>>;
  5020. if constexpr (std::is_same_v<unwrapped_type, node> || is_node_view<unwrapped_type>)
  5021. {
  5022. if constexpr (is_node_view<unwrapped_type>)
  5023. {
  5024. if (!val)
  5025. return static_cast<toml::node*>(nullptr);
  5026. }
  5027. return static_cast<T&&>(val).visit(
  5028. [flags](auto&& concrete) {
  5029. return static_cast<toml::node*>(
  5030. make_node_impl_specialized(static_cast<decltype(concrete)&&>(concrete), flags));
  5031. });
  5032. }
  5033. else
  5034. return make_node_impl_specialized(static_cast<T&&>(val), flags);
  5035. }
  5036. template <typename T>
  5037. TOML_NODISCARD
  5038. auto* make_node_impl(inserter<T> && val, value_flags flags = preserve_source_value_flags)
  5039. {
  5040. return make_node_impl(static_cast<T&&>(val.value), flags);
  5041. }
  5042. template <typename T, bool = (is_node<T> || is_node_view<T> || is_value<T> || can_partially_represent_native<T>)>
  5043. struct inserted_type_of_
  5044. {
  5045. using type = std::remove_pointer_t<decltype(make_node_impl(std::declval<T>()))>;
  5046. };
  5047. template <typename T>
  5048. struct inserted_type_of_<inserter<T>, false>
  5049. {
  5050. using type = typename inserted_type_of_<remove_cvref<T>>::type;
  5051. };
  5052. template <typename T>
  5053. struct inserted_type_of_<T, false>
  5054. {
  5055. using type = void;
  5056. };
  5057. template <typename T>
  5058. TOML_NODISCARD
  5059. node_ptr make_node(T && val, value_flags flags = preserve_source_value_flags)
  5060. {
  5061. return node_ptr{ make_node_impl(static_cast<T&&>(val), flags) };
  5062. }
  5063. template <typename... T>
  5064. struct emplaced_type_of_
  5065. {
  5066. using type = void;
  5067. };
  5068. template <typename T>
  5069. struct emplaced_type_of_<T>
  5070. {
  5071. using type = std::conditional_t<is_one_of<T, node, node_view<node>, node_view<const node>>,
  5072. void,
  5073. typename inserted_type_of_<T>::type>;
  5074. };
  5075. template <typename T>
  5076. struct emplaced_type_of_<inserter<T>>
  5077. {
  5078. using type = typename emplaced_type_of_<remove_cvref<T>>::type;
  5079. };
  5080. template <typename... T>
  5081. using emplaced_type_of = typename emplaced_type_of_<remove_cvref<T>...>::type;
  5082. }
  5083. TOML_IMPL_NAMESPACE_END;
  5084. TOML_NAMESPACE_START
  5085. {
  5086. template <typename T>
  5087. using inserted_type_of = POXY_IMPLEMENTATION_DETAIL(typename impl::inserted_type_of_<impl::remove_cvref<T>>::type);
  5088. }
  5089. TOML_NAMESPACE_END;
  5090. #ifdef _MSC_VER
  5091. #pragma pop_macro("min")
  5092. #pragma pop_macro("max")
  5093. #ifndef __clang__
  5094. #pragma inline_recursion(off)
  5095. #endif
  5096. #endif
  5097. TOML_POP_WARNINGS;
  5098. //******** impl/array.hpp ********************************************************************************************
  5099. TOML_PUSH_WARNINGS;
  5100. #ifdef _MSC_VER
  5101. #ifndef __clang__
  5102. #pragma inline_recursion(on)
  5103. #endif
  5104. #pragma push_macro("min")
  5105. #pragma push_macro("max")
  5106. #undef min
  5107. #undef max
  5108. #endif
  5109. #ifndef TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN
  5110. #if TOML_GCC && TOML_GCC <= 7
  5111. #define TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN 1
  5112. #else
  5113. #define TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN 0
  5114. #endif
  5115. #endif
  5116. #if TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN && !defined(TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_ACKNOWLEDGED)
  5117. #define TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_MESSAGE \
  5118. "If you're seeing this error it's because you're using one of toml++'s for_each() functions on a compiler with " \
  5119. "known bugs in that area (e.g. GCC 7). On these compilers returning a bool (or bool-convertible) value from the " \
  5120. "for_each() callable causes spurious compilation failures, while returning nothing (void) works fine. " \
  5121. "If you believe this message is incorrect for your compiler, you can try your luck by #defining " \
  5122. "TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN as 0 and recompiling - if it works, great! Let me know at " \
  5123. "https://github.com/marzer/tomlplusplus/issues. Alternatively, if you don't have any need for early-exiting from " \
  5124. "for_each(), you can suppress this error by #defining TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_ACKNOWLEDGED " \
  5125. "and moving on with your life."
  5126. #endif
  5127. TOML_IMPL_NAMESPACE_START
  5128. {
  5129. template <bool IsConst>
  5130. class TOML_TRIVIAL_ABI array_iterator
  5131. {
  5132. private:
  5133. template <bool>
  5134. friend class array_iterator;
  5135. using mutable_vector_iterator = std::vector<node_ptr>::iterator;
  5136. using const_vector_iterator = std::vector<node_ptr>::const_iterator;
  5137. using vector_iterator = std::conditional_t<IsConst, const_vector_iterator, mutable_vector_iterator>;
  5138. mutable vector_iterator iter_;
  5139. public:
  5140. using value_type = std::conditional_t<IsConst, const node, node>;
  5141. using reference = value_type&;
  5142. using pointer = value_type*;
  5143. using difference_type = ptrdiff_t;
  5144. using iterator_category = typename std::iterator_traits<vector_iterator>::iterator_category;
  5145. TOML_NODISCARD_CTOR
  5146. array_iterator() noexcept = default;
  5147. TOML_NODISCARD_CTOR
  5148. explicit array_iterator(mutable_vector_iterator iter) noexcept //
  5149. : iter_{ iter }
  5150. {}
  5151. TOML_CONSTRAINED_TEMPLATE(C, bool C = IsConst)
  5152. TOML_NODISCARD_CTOR
  5153. explicit array_iterator(const_vector_iterator iter) noexcept //
  5154. : iter_{ iter }
  5155. {}
  5156. TOML_CONSTRAINED_TEMPLATE(C, bool C = IsConst)
  5157. TOML_NODISCARD_CTOR
  5158. array_iterator(const array_iterator<false>& other) noexcept //
  5159. : iter_{ other.iter_ }
  5160. {}
  5161. TOML_NODISCARD_CTOR
  5162. array_iterator(const array_iterator&) noexcept = default;
  5163. array_iterator& operator=(const array_iterator&) noexcept = default;
  5164. array_iterator& operator++() noexcept // ++pre
  5165. {
  5166. ++iter_;
  5167. return *this;
  5168. }
  5169. array_iterator operator++(int) noexcept // post++
  5170. {
  5171. array_iterator out{ iter_ };
  5172. ++iter_;
  5173. return out;
  5174. }
  5175. array_iterator& operator--() noexcept // --pre
  5176. {
  5177. --iter_;
  5178. return *this;
  5179. }
  5180. array_iterator operator--(int) noexcept // post--
  5181. {
  5182. array_iterator out{ iter_ };
  5183. --iter_;
  5184. return out;
  5185. }
  5186. TOML_PURE_INLINE_GETTER
  5187. reference operator*() const noexcept
  5188. {
  5189. return *iter_->get();
  5190. }
  5191. TOML_PURE_INLINE_GETTER
  5192. pointer operator->() const noexcept
  5193. {
  5194. return iter_->get();
  5195. }
  5196. TOML_PURE_INLINE_GETTER
  5197. explicit operator const vector_iterator&() const noexcept
  5198. {
  5199. return iter_;
  5200. }
  5201. TOML_CONSTRAINED_TEMPLATE(!C, bool C = IsConst)
  5202. TOML_PURE_INLINE_GETTER
  5203. explicit operator const const_vector_iterator() const noexcept
  5204. {
  5205. return iter_;
  5206. }
  5207. array_iterator& operator+=(ptrdiff_t rhs) noexcept
  5208. {
  5209. iter_ += rhs;
  5210. return *this;
  5211. }
  5212. array_iterator& operator-=(ptrdiff_t rhs) noexcept
  5213. {
  5214. iter_ -= rhs;
  5215. return *this;
  5216. }
  5217. TOML_NODISCARD
  5218. friend array_iterator operator+(const array_iterator& lhs, ptrdiff_t rhs) noexcept
  5219. {
  5220. return array_iterator{ lhs.iter_ + rhs };
  5221. }
  5222. TOML_NODISCARD
  5223. friend array_iterator operator+(ptrdiff_t lhs, const array_iterator& rhs) noexcept
  5224. {
  5225. return array_iterator{ rhs.iter_ + lhs };
  5226. }
  5227. TOML_NODISCARD
  5228. friend array_iterator operator-(const array_iterator& lhs, ptrdiff_t rhs) noexcept
  5229. {
  5230. return array_iterator{ lhs.iter_ - rhs };
  5231. }
  5232. TOML_PURE_INLINE_GETTER
  5233. friend ptrdiff_t operator-(const array_iterator& lhs, const array_iterator& rhs) noexcept
  5234. {
  5235. return lhs.iter_ - rhs.iter_;
  5236. }
  5237. TOML_PURE_INLINE_GETTER
  5238. friend bool operator==(const array_iterator& lhs, const array_iterator& rhs) noexcept
  5239. {
  5240. return lhs.iter_ == rhs.iter_;
  5241. }
  5242. TOML_PURE_INLINE_GETTER
  5243. friend bool operator!=(const array_iterator& lhs, const array_iterator& rhs) noexcept
  5244. {
  5245. return lhs.iter_ != rhs.iter_;
  5246. }
  5247. TOML_PURE_INLINE_GETTER
  5248. friend bool operator<(const array_iterator& lhs, const array_iterator& rhs) noexcept
  5249. {
  5250. return lhs.iter_ < rhs.iter_;
  5251. }
  5252. TOML_PURE_INLINE_GETTER
  5253. friend bool operator<=(const array_iterator& lhs, const array_iterator& rhs) noexcept
  5254. {
  5255. return lhs.iter_ <= rhs.iter_;
  5256. }
  5257. TOML_PURE_INLINE_GETTER
  5258. friend bool operator>(const array_iterator& lhs, const array_iterator& rhs) noexcept
  5259. {
  5260. return lhs.iter_ > rhs.iter_;
  5261. }
  5262. TOML_PURE_INLINE_GETTER
  5263. friend bool operator>=(const array_iterator& lhs, const array_iterator& rhs) noexcept
  5264. {
  5265. return lhs.iter_ >= rhs.iter_;
  5266. }
  5267. TOML_PURE_INLINE_GETTER
  5268. reference operator[](ptrdiff_t idx) const noexcept
  5269. {
  5270. return *(iter_ + idx)->get();
  5271. }
  5272. };
  5273. struct array_init_elem
  5274. {
  5275. mutable node_ptr value;
  5276. template <typename T>
  5277. TOML_NODISCARD_CTOR
  5278. array_init_elem(T&& val, value_flags flags = preserve_source_value_flags) //
  5279. : value{ make_node(static_cast<T&&>(val), flags) }
  5280. {}
  5281. };
  5282. }
  5283. TOML_IMPL_NAMESPACE_END;
  5284. TOML_NAMESPACE_START
  5285. {
  5286. using array_iterator = POXY_IMPLEMENTATION_DETAIL(impl::array_iterator<false>);
  5287. using const_array_iterator = POXY_IMPLEMENTATION_DETAIL(impl::array_iterator<true>);
  5288. class TOML_EXPORTED_CLASS array : public node
  5289. {
  5290. private:
  5291. using vector_type = std::vector<impl::node_ptr>;
  5292. using vector_iterator = typename vector_type::iterator;
  5293. using const_vector_iterator = typename vector_type::const_iterator;
  5294. vector_type elems_;
  5295. TOML_NODISCARD_CTOR
  5296. TOML_EXPORTED_MEMBER_FUNCTION
  5297. array(const impl::array_init_elem*, const impl::array_init_elem*);
  5298. TOML_NODISCARD_CTOR
  5299. array(std::false_type, std::initializer_list<impl::array_init_elem> elems) //
  5300. : array{ elems.begin(), elems.end() }
  5301. {}
  5302. TOML_EXPORTED_MEMBER_FUNCTION
  5303. void preinsertion_resize(size_t idx, size_t count);
  5304. TOML_EXPORTED_MEMBER_FUNCTION
  5305. void insert_at_back(impl::node_ptr&&);
  5306. TOML_EXPORTED_MEMBER_FUNCTION
  5307. vector_iterator insert_at(const_vector_iterator, impl::node_ptr&&);
  5308. template <typename T>
  5309. void emplace_back_if_not_empty_view(T&& val, value_flags flags)
  5310. {
  5311. if constexpr (is_node_view<T>)
  5312. {
  5313. if (!val)
  5314. return;
  5315. }
  5316. insert_at_back(impl::make_node(static_cast<T&&>(val), flags));
  5317. }
  5318. TOML_NODISCARD
  5319. TOML_EXPORTED_MEMBER_FUNCTION
  5320. size_t total_leaf_count() const noexcept;
  5321. TOML_EXPORTED_MEMBER_FUNCTION
  5322. void flatten_child(array&& child, size_t& dest_index) noexcept;
  5323. public:
  5324. using value_type = node;
  5325. using size_type = size_t;
  5326. using difference_type = ptrdiff_t;
  5327. using reference = node&;
  5328. using const_reference = const node&;
  5329. TOML_NODISCARD_CTOR
  5330. TOML_EXPORTED_MEMBER_FUNCTION
  5331. array() noexcept;
  5332. TOML_EXPORTED_MEMBER_FUNCTION
  5333. ~array() noexcept;
  5334. TOML_NODISCARD_CTOR
  5335. TOML_EXPORTED_MEMBER_FUNCTION
  5336. array(const array&);
  5337. TOML_NODISCARD_CTOR
  5338. TOML_EXPORTED_MEMBER_FUNCTION
  5339. array(array&& other) noexcept;
  5340. TOML_CONSTRAINED_TEMPLATE((sizeof...(ElemTypes) > 0 || !std::is_same_v<impl::remove_cvref<ElemType>, array>),
  5341. typename ElemType,
  5342. typename... ElemTypes)
  5343. TOML_NODISCARD_CTOR
  5344. explicit array(ElemType&& val, ElemTypes&&... vals)
  5345. : array{ std::false_type{},
  5346. std::initializer_list<impl::array_init_elem>{ static_cast<ElemType&&>(val),
  5347. static_cast<ElemTypes&&>(vals)... } }
  5348. {}
  5349. TOML_EXPORTED_MEMBER_FUNCTION
  5350. array& operator=(const array&);
  5351. TOML_EXPORTED_MEMBER_FUNCTION
  5352. array& operator=(array&& rhs) noexcept;
  5353. TOML_CONST_INLINE_GETTER
  5354. node_type type() const noexcept final
  5355. {
  5356. return node_type::array;
  5357. }
  5358. TOML_PURE_GETTER
  5359. TOML_EXPORTED_MEMBER_FUNCTION
  5360. bool is_homogeneous(node_type ntype) const noexcept final;
  5361. TOML_PURE_GETTER
  5362. TOML_EXPORTED_MEMBER_FUNCTION
  5363. bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept final;
  5364. TOML_PURE_GETTER
  5365. TOML_EXPORTED_MEMBER_FUNCTION
  5366. bool is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept final;
  5367. template <typename ElemType = void>
  5368. TOML_PURE_GETTER
  5369. bool is_homogeneous() const noexcept
  5370. {
  5371. using type = impl::remove_cvref<impl::unwrap_node<ElemType>>;
  5372. static_assert(std::is_void_v<type> || toml::is_value<type> || toml::is_container<type>,
  5373. "The template type argument of array::is_homogeneous() must be void or one "
  5374. "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
  5375. return is_homogeneous(impl::node_type_of<type>);
  5376. }
  5377. TOML_CONST_INLINE_GETTER
  5378. bool is_table() const noexcept final
  5379. {
  5380. return false;
  5381. }
  5382. TOML_CONST_INLINE_GETTER
  5383. bool is_array() const noexcept final
  5384. {
  5385. return true;
  5386. }
  5387. TOML_PURE_GETTER
  5388. bool is_array_of_tables() const noexcept final
  5389. {
  5390. return is_homogeneous(node_type::table);
  5391. }
  5392. TOML_CONST_INLINE_GETTER
  5393. bool is_value() const noexcept final
  5394. {
  5395. return false;
  5396. }
  5397. TOML_CONST_INLINE_GETTER
  5398. bool is_string() const noexcept final
  5399. {
  5400. return false;
  5401. }
  5402. TOML_CONST_INLINE_GETTER
  5403. bool is_integer() const noexcept final
  5404. {
  5405. return false;
  5406. }
  5407. TOML_CONST_INLINE_GETTER
  5408. bool is_floating_point() const noexcept final
  5409. {
  5410. return false;
  5411. }
  5412. TOML_CONST_INLINE_GETTER
  5413. bool is_number() const noexcept final
  5414. {
  5415. return false;
  5416. }
  5417. TOML_CONST_INLINE_GETTER
  5418. bool is_boolean() const noexcept final
  5419. {
  5420. return false;
  5421. }
  5422. TOML_CONST_INLINE_GETTER
  5423. bool is_date() const noexcept final
  5424. {
  5425. return false;
  5426. }
  5427. TOML_CONST_INLINE_GETTER
  5428. bool is_time() const noexcept final
  5429. {
  5430. return false;
  5431. }
  5432. TOML_CONST_INLINE_GETTER
  5433. bool is_date_time() const noexcept final
  5434. {
  5435. return false;
  5436. }
  5437. TOML_CONST_INLINE_GETTER
  5438. table* as_table() noexcept final
  5439. {
  5440. return nullptr;
  5441. }
  5442. TOML_CONST_INLINE_GETTER
  5443. array* as_array() noexcept final
  5444. {
  5445. return this;
  5446. }
  5447. TOML_CONST_INLINE_GETTER
  5448. toml::value<std::string>* as_string() noexcept final
  5449. {
  5450. return nullptr;
  5451. }
  5452. TOML_CONST_INLINE_GETTER
  5453. toml::value<int64_t>* as_integer() noexcept final
  5454. {
  5455. return nullptr;
  5456. }
  5457. TOML_CONST_INLINE_GETTER
  5458. toml::value<double>* as_floating_point() noexcept final
  5459. {
  5460. return nullptr;
  5461. }
  5462. TOML_CONST_INLINE_GETTER
  5463. toml::value<bool>* as_boolean() noexcept final
  5464. {
  5465. return nullptr;
  5466. }
  5467. TOML_CONST_INLINE_GETTER
  5468. toml::value<date>* as_date() noexcept final
  5469. {
  5470. return nullptr;
  5471. }
  5472. TOML_CONST_INLINE_GETTER
  5473. toml::value<time>* as_time() noexcept final
  5474. {
  5475. return nullptr;
  5476. }
  5477. TOML_CONST_INLINE_GETTER
  5478. toml::value<date_time>* as_date_time() noexcept final
  5479. {
  5480. return nullptr;
  5481. }
  5482. TOML_CONST_INLINE_GETTER
  5483. const table* as_table() const noexcept final
  5484. {
  5485. return nullptr;
  5486. }
  5487. TOML_CONST_INLINE_GETTER
  5488. const array* as_array() const noexcept final
  5489. {
  5490. return this;
  5491. }
  5492. TOML_CONST_INLINE_GETTER
  5493. const toml::value<std::string>* as_string() const noexcept final
  5494. {
  5495. return nullptr;
  5496. }
  5497. TOML_CONST_INLINE_GETTER
  5498. const toml::value<int64_t>* as_integer() const noexcept final
  5499. {
  5500. return nullptr;
  5501. }
  5502. TOML_CONST_INLINE_GETTER
  5503. const toml::value<double>* as_floating_point() const noexcept final
  5504. {
  5505. return nullptr;
  5506. }
  5507. TOML_CONST_INLINE_GETTER
  5508. const toml::value<bool>* as_boolean() const noexcept final
  5509. {
  5510. return nullptr;
  5511. }
  5512. TOML_CONST_INLINE_GETTER
  5513. const toml::value<date>* as_date() const noexcept final
  5514. {
  5515. return nullptr;
  5516. }
  5517. TOML_CONST_INLINE_GETTER
  5518. const toml::value<time>* as_time() const noexcept final
  5519. {
  5520. return nullptr;
  5521. }
  5522. TOML_CONST_INLINE_GETTER
  5523. const toml::value<date_time>* as_date_time() const noexcept final
  5524. {
  5525. return nullptr;
  5526. }
  5527. TOML_PURE_INLINE_GETTER
  5528. node* get(size_t index) noexcept
  5529. {
  5530. return index < elems_.size() ? elems_[index].get() : nullptr;
  5531. }
  5532. TOML_PURE_INLINE_GETTER
  5533. const node* get(size_t index) const noexcept
  5534. {
  5535. return const_cast<array&>(*this).get(index);
  5536. }
  5537. template <typename ElemType>
  5538. TOML_NODISCARD
  5539. impl::wrap_node<ElemType>* get_as(size_t index) noexcept
  5540. {
  5541. if (auto val = get(index))
  5542. return val->template as<ElemType>();
  5543. return nullptr;
  5544. }
  5545. template <typename ElemType>
  5546. TOML_NODISCARD
  5547. const impl::wrap_node<ElemType>* get_as(size_t index) const noexcept
  5548. {
  5549. return const_cast<array&>(*this).template get_as<ElemType>(index);
  5550. }
  5551. using node::operator[]; // inherit operator[toml::path]
  5552. TOML_NODISCARD
  5553. node& operator[](size_t index) noexcept
  5554. {
  5555. return *elems_[index];
  5556. }
  5557. TOML_NODISCARD
  5558. const node& operator[](size_t index) const noexcept
  5559. {
  5560. return *elems_[index];
  5561. }
  5562. TOML_NODISCARD
  5563. TOML_EXPORTED_MEMBER_FUNCTION
  5564. node& at(size_t index);
  5565. TOML_NODISCARD
  5566. const node& at(size_t index) const
  5567. {
  5568. return const_cast<array&>(*this).at(index);
  5569. }
  5570. TOML_NODISCARD
  5571. node& front() noexcept
  5572. {
  5573. return *elems_.front();
  5574. }
  5575. TOML_NODISCARD
  5576. const node& front() const noexcept
  5577. {
  5578. return *elems_.front();
  5579. }
  5580. TOML_NODISCARD
  5581. node& back() noexcept
  5582. {
  5583. return *elems_.back();
  5584. }
  5585. TOML_NODISCARD
  5586. const node& back() const noexcept
  5587. {
  5588. return *elems_.back();
  5589. }
  5590. using iterator = array_iterator;
  5591. using const_iterator = const_array_iterator;
  5592. TOML_NODISCARD
  5593. iterator begin() noexcept
  5594. {
  5595. return iterator{ elems_.begin() };
  5596. }
  5597. TOML_NODISCARD
  5598. const_iterator begin() const noexcept
  5599. {
  5600. return const_iterator{ elems_.cbegin() };
  5601. }
  5602. TOML_NODISCARD
  5603. const_iterator cbegin() const noexcept
  5604. {
  5605. return const_iterator{ elems_.cbegin() };
  5606. }
  5607. TOML_NODISCARD
  5608. iterator end() noexcept
  5609. {
  5610. return iterator{ elems_.end() };
  5611. }
  5612. TOML_NODISCARD
  5613. const_iterator end() const noexcept
  5614. {
  5615. return const_iterator{ elems_.cend() };
  5616. }
  5617. TOML_NODISCARD
  5618. const_iterator cend() const noexcept
  5619. {
  5620. return const_iterator{ elems_.cend() };
  5621. }
  5622. private:
  5623. template <typename T, typename Array>
  5624. using for_each_elem_ref = impl::copy_cvref<impl::wrap_node<impl::remove_cvref<impl::unwrap_node<T>>>, Array>;
  5625. template <typename Func, typename Array, typename T>
  5626. using can_for_each = std::disjunction<std::is_invocable<Func, for_each_elem_ref<T, Array>, size_t>,
  5627. std::is_invocable<Func, size_t, for_each_elem_ref<T, Array>>,
  5628. std::is_invocable<Func, for_each_elem_ref<T, Array>>>;
  5629. template <typename Func, typename Array, typename T>
  5630. using can_for_each_nothrow = std::conditional_t<
  5631. // first form
  5632. std::is_invocable_v<Func, for_each_elem_ref<T, Array>, size_t>,
  5633. std::is_nothrow_invocable<Func, for_each_elem_ref<T, Array>, size_t>,
  5634. std::conditional_t<
  5635. // second form
  5636. std::is_invocable_v<Func, size_t, for_each_elem_ref<T, Array>>,
  5637. std::is_nothrow_invocable<Func, size_t, for_each_elem_ref<T, Array>>,
  5638. std::conditional_t<
  5639. // third form
  5640. std::is_invocable_v<Func, for_each_elem_ref<T, Array>>,
  5641. std::is_nothrow_invocable<Func, for_each_elem_ref<T, Array>>,
  5642. std::false_type>>>;
  5643. template <typename Func, typename Array>
  5644. using can_for_each_any = std::disjunction<can_for_each<Func, Array, table>,
  5645. can_for_each<Func, Array, array>,
  5646. can_for_each<Func, Array, std::string>,
  5647. can_for_each<Func, Array, int64_t>,
  5648. can_for_each<Func, Array, double>,
  5649. can_for_each<Func, Array, bool>,
  5650. can_for_each<Func, Array, date>,
  5651. can_for_each<Func, Array, time>,
  5652. can_for_each<Func, Array, date_time>>;
  5653. template <typename Func, typename Array, typename T>
  5654. using for_each_is_nothrow_one = std::disjunction<std::negation<can_for_each<Func, Array, T>>, //
  5655. can_for_each_nothrow<Func, Array, T>>;
  5656. template <typename Func, typename Array>
  5657. using for_each_is_nothrow = std::conjunction<for_each_is_nothrow_one<Func, Array, table>,
  5658. for_each_is_nothrow_one<Func, Array, array>,
  5659. for_each_is_nothrow_one<Func, Array, std::string>,
  5660. for_each_is_nothrow_one<Func, Array, int64_t>,
  5661. for_each_is_nothrow_one<Func, Array, double>,
  5662. for_each_is_nothrow_one<Func, Array, bool>,
  5663. for_each_is_nothrow_one<Func, Array, date>,
  5664. for_each_is_nothrow_one<Func, Array, time>,
  5665. for_each_is_nothrow_one<Func, Array, date_time>>;
  5666. template <typename Func, typename Array>
  5667. static void do_for_each(Func&& visitor, Array&& arr) //
  5668. noexcept(for_each_is_nothrow<Func&&, Array&&>::value)
  5669. {
  5670. static_assert(can_for_each_any<Func&&, Array&&>::value,
  5671. "TOML array for_each visitors must be invocable for at least one of the toml::node "
  5672. "specializations:" TOML_SA_NODE_TYPE_LIST);
  5673. for (size_t i = 0; i < arr.size(); i++)
  5674. {
  5675. using node_ref = impl::copy_cvref<toml::node, Array&&>;
  5676. static_assert(std::is_reference_v<node_ref>);
  5677. #if TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN
  5678. #ifndef TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_ACKNOWLEDGED
  5679. static_assert(impl::always_false<Func, Array, node_ref>, //
  5680. TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_MESSAGE);
  5681. #endif
  5682. static_cast<node_ref>(static_cast<Array&&>(arr)[i])
  5683. .visit(
  5684. [&]([[maybe_unused]] auto&& elem) //
  5685. noexcept(for_each_is_nothrow_one<Func&&, Array&&, decltype(elem)>::value)
  5686. {
  5687. using elem_ref = for_each_elem_ref<decltype(elem), Array&&>;
  5688. static_assert(std::is_reference_v<elem_ref>);
  5689. // func(elem, i)
  5690. if constexpr (std::is_invocable_v<Func&&, elem_ref, size_t>)
  5691. {
  5692. static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem), i);
  5693. }
  5694. // func(i, elem)
  5695. else if constexpr (std::is_invocable_v<Func&&, size_t, elem_ref>)
  5696. {
  5697. static_cast<Func&&>(visitor)(i, static_cast<elem_ref>(elem));
  5698. }
  5699. // func(elem)
  5700. else if constexpr (std::is_invocable_v<Func&&, elem_ref>)
  5701. {
  5702. static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem));
  5703. }
  5704. });
  5705. #else
  5706. const auto keep_going =
  5707. static_cast<node_ref>(static_cast<Array&&>(arr)[i])
  5708. .visit(
  5709. [&]([[maybe_unused]] auto&& elem) //
  5710. noexcept(for_each_is_nothrow_one<Func&&, Array&&, decltype(elem)>::value)
  5711. {
  5712. using elem_ref = for_each_elem_ref<decltype(elem), Array&&>;
  5713. static_assert(std::is_reference_v<elem_ref>);
  5714. // func(elem, i)
  5715. if constexpr (std::is_invocable_v<Func&&, elem_ref, size_t>)
  5716. {
  5717. using return_type =
  5718. decltype(static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem), i));
  5719. if constexpr (impl::is_constructible_or_convertible<bool, return_type>)
  5720. {
  5721. return static_cast<bool>(
  5722. static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem), i));
  5723. }
  5724. else
  5725. {
  5726. static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem), i);
  5727. return true;
  5728. }
  5729. }
  5730. // func(i, elem)
  5731. else if constexpr (std::is_invocable_v<Func&&, size_t, elem_ref>)
  5732. {
  5733. using return_type =
  5734. decltype(static_cast<Func&&>(visitor)(i, static_cast<elem_ref>(elem)));
  5735. if constexpr (impl::is_constructible_or_convertible<bool, return_type>)
  5736. {
  5737. return static_cast<bool>(
  5738. static_cast<Func&&>(visitor)(i, static_cast<elem_ref>(elem)));
  5739. }
  5740. else
  5741. {
  5742. static_cast<Func&&>(visitor)(i, static_cast<elem_ref>(elem));
  5743. return true;
  5744. }
  5745. }
  5746. // func(elem)
  5747. else if constexpr (std::is_invocable_v<Func&&, elem_ref>)
  5748. {
  5749. using return_type =
  5750. decltype(static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem)));
  5751. if constexpr (impl::is_constructible_or_convertible<bool, return_type>)
  5752. {
  5753. return static_cast<bool>(
  5754. static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem)));
  5755. }
  5756. else
  5757. {
  5758. static_cast<Func&&>(visitor)(static_cast<elem_ref>(elem));
  5759. return true;
  5760. }
  5761. }
  5762. // visitor not compatible with this particular type
  5763. else
  5764. return true;
  5765. });
  5766. if (!keep_going)
  5767. return;
  5768. #endif
  5769. }
  5770. }
  5771. public:
  5772. template <typename Func>
  5773. array& for_each(Func&& visitor) & //
  5774. noexcept(for_each_is_nothrow<Func&&, array&>::value)
  5775. {
  5776. do_for_each(static_cast<Func&&>(visitor), *this);
  5777. return *this;
  5778. }
  5779. template <typename Func>
  5780. array&& for_each(Func&& visitor) && //
  5781. noexcept(for_each_is_nothrow<Func&&, array&&>::value)
  5782. {
  5783. do_for_each(static_cast<Func&&>(visitor), static_cast<array&&>(*this));
  5784. return static_cast<array&&>(*this);
  5785. }
  5786. template <typename Func>
  5787. const array& for_each(Func&& visitor) const& //
  5788. noexcept(for_each_is_nothrow<Func&&, const array&>::value)
  5789. {
  5790. do_for_each(static_cast<Func&&>(visitor), *this);
  5791. return *this;
  5792. }
  5793. template <typename Func>
  5794. const array&& for_each(Func&& visitor) const&& //
  5795. noexcept(for_each_is_nothrow<Func&&, const array&&>::value)
  5796. {
  5797. do_for_each(static_cast<Func&&>(visitor), static_cast<const array&&>(*this));
  5798. return static_cast<const array&&>(*this);
  5799. }
  5800. TOML_NODISCARD
  5801. bool empty() const noexcept
  5802. {
  5803. return elems_.empty();
  5804. }
  5805. TOML_NODISCARD
  5806. size_t size() const noexcept
  5807. {
  5808. return elems_.size();
  5809. }
  5810. TOML_NODISCARD
  5811. size_t max_size() const noexcept
  5812. {
  5813. return elems_.max_size();
  5814. }
  5815. TOML_NODISCARD
  5816. size_t capacity() const noexcept
  5817. {
  5818. return elems_.capacity();
  5819. }
  5820. TOML_EXPORTED_MEMBER_FUNCTION
  5821. void reserve(size_t new_capacity);
  5822. TOML_EXPORTED_MEMBER_FUNCTION
  5823. void shrink_to_fit();
  5824. TOML_EXPORTED_MEMBER_FUNCTION
  5825. void truncate(size_t new_size);
  5826. template <typename ElemType>
  5827. void resize(size_t new_size,
  5828. ElemType&& default_init_val,
  5829. value_flags default_init_flags = preserve_source_value_flags)
  5830. {
  5831. static_assert(!is_node_view<ElemType>,
  5832. "The default element type argument to toml::array::resize may not be toml::node_view.");
  5833. if (!new_size)
  5834. clear();
  5835. else if (new_size > elems_.size())
  5836. insert(cend(), new_size - elems_.size(), static_cast<ElemType&&>(default_init_val), default_init_flags);
  5837. else
  5838. truncate(new_size);
  5839. }
  5840. TOML_EXPORTED_MEMBER_FUNCTION
  5841. iterator erase(const_iterator pos) noexcept;
  5842. TOML_EXPORTED_MEMBER_FUNCTION
  5843. iterator erase(const_iterator first, const_iterator last) noexcept;
  5844. TOML_EXPORTED_MEMBER_FUNCTION
  5845. array& flatten() &;
  5846. array&& flatten() &&
  5847. {
  5848. return static_cast<toml::array&&>(this->flatten());
  5849. }
  5850. TOML_EXPORTED_MEMBER_FUNCTION
  5851. array& prune(bool recursive = true) & noexcept;
  5852. array&& prune(bool recursive = true) && noexcept
  5853. {
  5854. return static_cast<toml::array&&>(this->prune(recursive));
  5855. }
  5856. TOML_EXPORTED_MEMBER_FUNCTION
  5857. void pop_back() noexcept;
  5858. TOML_EXPORTED_MEMBER_FUNCTION
  5859. void clear() noexcept;
  5860. template <typename ElemType>
  5861. iterator insert(const_iterator pos, ElemType&& val, value_flags flags = preserve_source_value_flags)
  5862. {
  5863. if constexpr (is_node_view<ElemType>)
  5864. {
  5865. if (!val)
  5866. return end();
  5867. }
  5868. return iterator{ insert_at(const_vector_iterator{ pos },
  5869. impl::make_node(static_cast<ElemType&&>(val), flags)) };
  5870. }
  5871. template <typename ElemType>
  5872. iterator insert(const_iterator pos,
  5873. size_t count,
  5874. ElemType&& val,
  5875. value_flags flags = preserve_source_value_flags)
  5876. {
  5877. if constexpr (is_node_view<ElemType>)
  5878. {
  5879. if (!val)
  5880. return end();
  5881. }
  5882. switch (count)
  5883. {
  5884. case 0: return iterator{ elems_.begin() + (const_vector_iterator{ pos } - elems_.cbegin()) };
  5885. case 1: return insert(pos, static_cast<ElemType&&>(val), flags);
  5886. default:
  5887. {
  5888. const auto start_idx = static_cast<size_t>(const_vector_iterator{ pos } - elems_.cbegin());
  5889. preinsertion_resize(start_idx, count);
  5890. size_t i = start_idx;
  5891. for (size_t e = start_idx + count - 1u; i < e; i++)
  5892. elems_[i] = impl::make_node(val, flags);
  5893. elems_[i] = impl::make_node(static_cast<ElemType&&>(val), flags);
  5894. return iterator{ elems_.begin() + static_cast<ptrdiff_t>(start_idx) };
  5895. }
  5896. }
  5897. }
  5898. template <typename Iter>
  5899. iterator insert(const_iterator pos, Iter first, Iter last, value_flags flags = preserve_source_value_flags)
  5900. {
  5901. const auto distance = std::distance(first, last);
  5902. if (distance <= 0)
  5903. return iterator{ elems_.begin() + (const_vector_iterator{ pos } - elems_.cbegin()) };
  5904. else
  5905. {
  5906. auto count = distance;
  5907. using deref_type = decltype(*first);
  5908. if constexpr (is_node_view<deref_type>)
  5909. {
  5910. for (auto it = first; it != last; it++)
  5911. if (!(*it))
  5912. count--;
  5913. if (!count)
  5914. return iterator{ elems_.begin() + (const_vector_iterator{ pos } - elems_.cbegin()) };
  5915. }
  5916. const auto start_idx = static_cast<size_t>(const_vector_iterator{ pos } - elems_.cbegin());
  5917. preinsertion_resize(start_idx, static_cast<size_t>(count));
  5918. size_t i = start_idx;
  5919. for (auto it = first; it != last; it++)
  5920. {
  5921. if constexpr (is_node_view<deref_type>)
  5922. {
  5923. if (!(*it))
  5924. continue;
  5925. }
  5926. if constexpr (std::is_rvalue_reference_v<deref_type>)
  5927. elems_[i++] = impl::make_node(std::move(*it), flags);
  5928. else
  5929. elems_[i++] = impl::make_node(*it, flags);
  5930. }
  5931. return iterator{ elems_.begin() + static_cast<ptrdiff_t>(start_idx) };
  5932. }
  5933. }
  5934. template <typename ElemType>
  5935. iterator insert(const_iterator pos,
  5936. std::initializer_list<ElemType> ilist,
  5937. value_flags flags = preserve_source_value_flags)
  5938. {
  5939. return insert(pos, ilist.begin(), ilist.end(), flags);
  5940. }
  5941. template <typename ElemType = void, typename... Args>
  5942. iterator emplace(const_iterator pos, Args&&... args)
  5943. {
  5944. using raw_elem_type = impl::remove_cvref<ElemType>;
  5945. using elem_type = std::conditional_t<std::is_void_v<raw_elem_type>, //
  5946. impl::emplaced_type_of<Args&&...>,
  5947. raw_elem_type>;
  5948. using type = impl::remove_cvref<impl::unwrap_node<elem_type>>;
  5949. static_assert(impl::is_native<type> || impl::is_one_of<type, table, array>,
  5950. "Emplacement type parameter must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
  5951. return iterator{ insert_at(const_vector_iterator{ pos },
  5952. impl::node_ptr{ new impl::wrap_node<type>{ static_cast<Args&&>(args)... } }) };
  5953. }
  5954. template <typename ElemType>
  5955. iterator replace(const_iterator pos, ElemType&& val, value_flags flags = preserve_source_value_flags)
  5956. {
  5957. TOML_ASSERT(pos >= cbegin() && pos < cend());
  5958. if constexpr (is_node_view<ElemType>)
  5959. {
  5960. if (!val)
  5961. return end();
  5962. }
  5963. const auto it = elems_.begin() + (const_vector_iterator{ pos } - elems_.cbegin());
  5964. *it = impl::make_node(static_cast<ElemType&&>(val), flags);
  5965. return iterator{ it };
  5966. }
  5967. template <typename ElemType>
  5968. void push_back(ElemType&& val, value_flags flags = preserve_source_value_flags)
  5969. {
  5970. emplace_back_if_not_empty_view(static_cast<ElemType&&>(val), flags);
  5971. }
  5972. template <typename ElemType = void, typename... Args>
  5973. decltype(auto) emplace_back(Args&&... args)
  5974. {
  5975. using raw_elem_type = impl::remove_cvref<ElemType>;
  5976. using elem_type = std::conditional_t<std::is_void_v<raw_elem_type>, //
  5977. impl::emplaced_type_of<Args&&...>,
  5978. raw_elem_type>;
  5979. static constexpr auto moving_node_ptr = std::is_same_v<elem_type, impl::node_ptr> //
  5980. && sizeof...(Args) == 1u //
  5981. && impl::first_is_same<impl::node_ptr&&, Args&&...>;
  5982. using unwrapped_type = impl::remove_cvref<impl::unwrap_node<elem_type>>;
  5983. static_assert(
  5984. moving_node_ptr //
  5985. || impl::is_native<unwrapped_type> //
  5986. || impl::is_one_of<unwrapped_type, table, array>, //
  5987. "ElemType argument of array::emplace_back() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
  5988. if constexpr (moving_node_ptr)
  5989. {
  5990. insert_at_back(static_cast<Args&&>(args)...);
  5991. return *elems_.back();
  5992. }
  5993. else
  5994. {
  5995. auto ptr = new impl::wrap_node<unwrapped_type>{ static_cast<Args&&>(args)... };
  5996. insert_at_back(impl::node_ptr{ ptr });
  5997. return *ptr;
  5998. }
  5999. }
  6000. private:
  6001. TOML_NODISCARD
  6002. TOML_EXPORTED_STATIC_FUNCTION
  6003. static bool TOML_CALLCONV equal(const array&, const array&) noexcept;
  6004. template <typename T>
  6005. TOML_NODISCARD
  6006. static bool equal_to_container(const array& lhs, const T& rhs) noexcept
  6007. {
  6008. using element_type = std::remove_const_t<typename T::value_type>;
  6009. static_assert(impl::is_losslessly_convertible_to_native<element_type>,
  6010. "Container element type must be losslessly convertible one of the native TOML value types");
  6011. if (lhs.size() != rhs.size())
  6012. return false;
  6013. if (rhs.size() == 0u)
  6014. return true;
  6015. size_t i{};
  6016. for (auto& list_elem : rhs)
  6017. {
  6018. const auto elem = lhs.get_as<impl::native_type_of<element_type>>(i++);
  6019. if (!elem || *elem != list_elem)
  6020. return false;
  6021. }
  6022. return true;
  6023. }
  6024. public:
  6025. TOML_NODISCARD
  6026. friend bool operator==(const array& lhs, const array& rhs) noexcept
  6027. {
  6028. return equal(lhs, rhs);
  6029. }
  6030. TOML_NODISCARD
  6031. friend bool operator!=(const array& lhs, const array& rhs) noexcept
  6032. {
  6033. return !equal(lhs, rhs);
  6034. }
  6035. template <typename T>
  6036. TOML_NODISCARD
  6037. friend bool operator==(const array& lhs, const std::initializer_list<T>& rhs) noexcept
  6038. {
  6039. return equal_to_container(lhs, rhs);
  6040. }
  6041. TOML_ASYMMETRICAL_EQUALITY_OPS(const array&, const std::initializer_list<T>&, template <typename T>);
  6042. template <typename T>
  6043. TOML_NODISCARD
  6044. friend bool operator==(const array& lhs, const std::vector<T>& rhs) noexcept
  6045. {
  6046. return equal_to_container(lhs, rhs);
  6047. }
  6048. TOML_ASYMMETRICAL_EQUALITY_OPS(const array&, const std::vector<T>&, template <typename T>);
  6049. #if TOML_ENABLE_FORMATTERS
  6050. friend std::ostream& operator<<(std::ostream& lhs, const array& rhs)
  6051. {
  6052. impl::print_to_stream(lhs, rhs);
  6053. return lhs;
  6054. }
  6055. #endif
  6056. };
  6057. }
  6058. TOML_NAMESPACE_END;
  6059. #ifdef _MSC_VER
  6060. #pragma pop_macro("min")
  6061. #pragma pop_macro("max")
  6062. #ifndef __clang__
  6063. #pragma inline_recursion(off)
  6064. #endif
  6065. #endif
  6066. TOML_POP_WARNINGS;
  6067. //******** impl/key.hpp **********************************************************************************************
  6068. TOML_PUSH_WARNINGS;
  6069. #ifdef _MSC_VER
  6070. #ifndef __clang__
  6071. #pragma inline_recursion(on)
  6072. #endif
  6073. #pragma push_macro("min")
  6074. #pragma push_macro("max")
  6075. #undef min
  6076. #undef max
  6077. #endif
  6078. TOML_NAMESPACE_START
  6079. {
  6080. class key
  6081. {
  6082. private:
  6083. std::string key_;
  6084. source_region source_;
  6085. public:
  6086. TOML_NODISCARD_CTOR
  6087. key() noexcept = default;
  6088. TOML_NODISCARD_CTOR
  6089. explicit key(std::string_view k, source_region&& src = {}) //
  6090. : key_{ k },
  6091. source_{ std::move(src) }
  6092. {}
  6093. TOML_NODISCARD_CTOR
  6094. explicit key(std::string_view k, const source_region& src) //
  6095. : key_{ k },
  6096. source_{ src }
  6097. {}
  6098. TOML_NODISCARD_CTOR
  6099. explicit key(std::string&& k, source_region&& src = {}) noexcept //
  6100. : key_{ std::move(k) },
  6101. source_{ std::move(src) }
  6102. {}
  6103. TOML_NODISCARD_CTOR
  6104. explicit key(std::string&& k, const source_region& src) noexcept //
  6105. : key_{ std::move(k) },
  6106. source_{ src }
  6107. {}
  6108. TOML_NODISCARD_CTOR
  6109. explicit key(const char* k, source_region&& src = {}) //
  6110. : key_{ k },
  6111. source_{ std::move(src) }
  6112. {}
  6113. TOML_NODISCARD_CTOR
  6114. explicit key(const char* k, const source_region& src) //
  6115. : key_{ k },
  6116. source_{ src }
  6117. {}
  6118. #if TOML_ENABLE_WINDOWS_COMPAT
  6119. TOML_NODISCARD_CTOR
  6120. explicit key(std::wstring_view k, source_region&& src = {}) //
  6121. : key_{ impl::narrow(k) },
  6122. source_{ std::move(src) }
  6123. {}
  6124. TOML_NODISCARD_CTOR
  6125. explicit key(std::wstring_view k, const source_region& src) //
  6126. : key_{ impl::narrow(k) },
  6127. source_{ src }
  6128. {}
  6129. #endif
  6130. TOML_PURE_INLINE_GETTER
  6131. std::string_view str() const noexcept
  6132. {
  6133. return std::string_view{ key_ };
  6134. }
  6135. TOML_PURE_INLINE_GETTER
  6136. /*implicit*/ operator std::string_view() const noexcept
  6137. {
  6138. return str();
  6139. }
  6140. TOML_PURE_INLINE_GETTER
  6141. bool empty() const noexcept
  6142. {
  6143. return key_.empty();
  6144. }
  6145. TOML_PURE_INLINE_GETTER
  6146. const char* data() const noexcept
  6147. {
  6148. return key_.data();
  6149. }
  6150. TOML_PURE_INLINE_GETTER
  6151. size_t length() const noexcept
  6152. {
  6153. return key_.length();
  6154. }
  6155. TOML_PURE_INLINE_GETTER
  6156. const source_region& source() const noexcept
  6157. {
  6158. return source_;
  6159. }
  6160. TOML_PURE_INLINE_GETTER
  6161. friend bool operator==(const key& lhs, const key& rhs) noexcept
  6162. {
  6163. return lhs.key_ == rhs.key_;
  6164. }
  6165. TOML_PURE_INLINE_GETTER
  6166. friend bool operator!=(const key& lhs, const key& rhs) noexcept
  6167. {
  6168. return lhs.key_ != rhs.key_;
  6169. }
  6170. TOML_PURE_INLINE_GETTER
  6171. friend bool operator<(const key& lhs, const key& rhs) noexcept
  6172. {
  6173. return lhs.key_ < rhs.key_;
  6174. }
  6175. TOML_PURE_INLINE_GETTER
  6176. friend bool operator<=(const key& lhs, const key& rhs) noexcept
  6177. {
  6178. return lhs.key_ <= rhs.key_;
  6179. }
  6180. TOML_PURE_INLINE_GETTER
  6181. friend bool operator>(const key& lhs, const key& rhs) noexcept
  6182. {
  6183. return lhs.key_ > rhs.key_;
  6184. }
  6185. TOML_PURE_INLINE_GETTER
  6186. friend bool operator>=(const key& lhs, const key& rhs) noexcept
  6187. {
  6188. return lhs.key_ >= rhs.key_;
  6189. }
  6190. TOML_PURE_INLINE_GETTER
  6191. friend bool operator==(const key& lhs, std::string_view rhs) noexcept
  6192. {
  6193. return lhs.key_ == rhs;
  6194. }
  6195. TOML_PURE_INLINE_GETTER
  6196. friend bool operator!=(const key& lhs, std::string_view rhs) noexcept
  6197. {
  6198. return lhs.key_ != rhs;
  6199. }
  6200. TOML_PURE_INLINE_GETTER
  6201. friend bool operator<(const key& lhs, std::string_view rhs) noexcept
  6202. {
  6203. return lhs.key_ < rhs;
  6204. }
  6205. TOML_PURE_INLINE_GETTER
  6206. friend bool operator<=(const key& lhs, std::string_view rhs) noexcept
  6207. {
  6208. return lhs.key_ <= rhs;
  6209. }
  6210. TOML_PURE_INLINE_GETTER
  6211. friend bool operator>(const key& lhs, std::string_view rhs) noexcept
  6212. {
  6213. return lhs.key_ > rhs;
  6214. }
  6215. TOML_PURE_INLINE_GETTER
  6216. friend bool operator>=(const key& lhs, std::string_view rhs) noexcept
  6217. {
  6218. return lhs.key_ >= rhs;
  6219. }
  6220. TOML_PURE_INLINE_GETTER
  6221. friend bool operator==(std::string_view lhs, const key& rhs) noexcept
  6222. {
  6223. return lhs == rhs.key_;
  6224. }
  6225. TOML_PURE_INLINE_GETTER
  6226. friend bool operator!=(std::string_view lhs, const key& rhs) noexcept
  6227. {
  6228. return lhs != rhs.key_;
  6229. }
  6230. TOML_PURE_INLINE_GETTER
  6231. friend bool operator<(std::string_view lhs, const key& rhs) noexcept
  6232. {
  6233. return lhs < rhs.key_;
  6234. }
  6235. TOML_PURE_INLINE_GETTER
  6236. friend bool operator<=(std::string_view lhs, const key& rhs) noexcept
  6237. {
  6238. return lhs <= rhs.key_;
  6239. }
  6240. TOML_PURE_INLINE_GETTER
  6241. friend bool operator>(std::string_view lhs, const key& rhs) noexcept
  6242. {
  6243. return lhs > rhs.key_;
  6244. }
  6245. TOML_PURE_INLINE_GETTER
  6246. friend bool operator>=(std::string_view lhs, const key& rhs) noexcept
  6247. {
  6248. return lhs >= rhs.key_;
  6249. }
  6250. using const_iterator = const char*;
  6251. using iterator = const_iterator;
  6252. TOML_PURE_INLINE_GETTER
  6253. const_iterator begin() const noexcept
  6254. {
  6255. return key_.data();
  6256. }
  6257. TOML_PURE_INLINE_GETTER
  6258. const_iterator end() const noexcept
  6259. {
  6260. return key_.data() + key_.length();
  6261. }
  6262. friend std::ostream& operator<<(std::ostream& lhs, const key& rhs)
  6263. {
  6264. impl::print_to_stream(lhs, rhs.key_);
  6265. return lhs;
  6266. }
  6267. };
  6268. template <typename T>
  6269. inline constexpr bool is_key = std::is_same_v<impl::remove_cvref<T>, toml::key>;
  6270. template <typename T>
  6271. inline constexpr bool is_key_or_convertible = is_key<T> //
  6272. || impl::is_constructible_or_convertible<toml::key, T>;
  6273. }
  6274. TOML_NAMESPACE_END;
  6275. #ifdef _MSC_VER
  6276. #pragma pop_macro("min")
  6277. #pragma pop_macro("max")
  6278. #ifndef __clang__
  6279. #pragma inline_recursion(off)
  6280. #endif
  6281. #endif
  6282. TOML_POP_WARNINGS;
  6283. //******** impl/std_map.hpp ******************************************************************************************
  6284. TOML_DISABLE_WARNINGS;
  6285. #include <map>
  6286. #include <iterator>
  6287. TOML_ENABLE_WARNINGS;
  6288. //******** impl/table.hpp ********************************************************************************************
  6289. TOML_PUSH_WARNINGS;
  6290. #ifdef _MSC_VER
  6291. #ifndef __clang__
  6292. #pragma inline_recursion(on)
  6293. #endif
  6294. #pragma push_macro("min")
  6295. #pragma push_macro("max")
  6296. #undef min
  6297. #undef max
  6298. #endif
  6299. TOML_IMPL_NAMESPACE_START
  6300. {
  6301. template <bool IsConst>
  6302. struct table_proxy_pair
  6303. {
  6304. using value_type = std::conditional_t<IsConst, const node, node>;
  6305. const toml::key& first;
  6306. value_type& second;
  6307. };
  6308. template <bool IsConst>
  6309. class table_iterator
  6310. {
  6311. private:
  6312. template <bool>
  6313. friend class table_iterator;
  6314. using proxy_type = table_proxy_pair<IsConst>;
  6315. using mutable_map_iterator = std::map<toml::key, node_ptr, std::less<>>::iterator;
  6316. using const_map_iterator = std::map<toml::key, node_ptr, std::less<>>::const_iterator;
  6317. using map_iterator = std::conditional_t<IsConst, const_map_iterator, mutable_map_iterator>;
  6318. mutable map_iterator iter_;
  6319. alignas(proxy_type) mutable unsigned char proxy_[sizeof(proxy_type)];
  6320. mutable bool proxy_instantiated_ = false;
  6321. TOML_NODISCARD
  6322. proxy_type* get_proxy() const noexcept
  6323. {
  6324. if (!proxy_instantiated_)
  6325. {
  6326. auto p = ::new (static_cast<void*>(proxy_)) proxy_type{ iter_->first, *iter_->second.get() };
  6327. proxy_instantiated_ = true;
  6328. return p;
  6329. }
  6330. else
  6331. return TOML_LAUNDER(reinterpret_cast<proxy_type*>(proxy_));
  6332. }
  6333. public:
  6334. TOML_NODISCARD_CTOR
  6335. table_iterator() noexcept = default;
  6336. TOML_NODISCARD_CTOR
  6337. explicit table_iterator(mutable_map_iterator iter) noexcept //
  6338. : iter_{ iter }
  6339. {}
  6340. TOML_CONSTRAINED_TEMPLATE(C, bool C = IsConst)
  6341. TOML_NODISCARD_CTOR
  6342. explicit table_iterator(const_map_iterator iter) noexcept //
  6343. : iter_{ iter }
  6344. {}
  6345. TOML_CONSTRAINED_TEMPLATE(C, bool C = IsConst)
  6346. TOML_NODISCARD_CTOR
  6347. table_iterator(const table_iterator<false>& other) noexcept //
  6348. : iter_{ other.iter_ }
  6349. {}
  6350. TOML_NODISCARD_CTOR
  6351. table_iterator(const table_iterator& other) noexcept //
  6352. : iter_{ other.iter_ }
  6353. {}
  6354. table_iterator& operator=(const table_iterator& rhs) noexcept
  6355. {
  6356. iter_ = rhs.iter_;
  6357. proxy_instantiated_ = false;
  6358. return *this;
  6359. }
  6360. using value_type = table_proxy_pair<IsConst>;
  6361. using reference = value_type&;
  6362. using pointer = value_type*;
  6363. using difference_type = typename std::iterator_traits<map_iterator>::difference_type;
  6364. using iterator_category = typename std::iterator_traits<map_iterator>::iterator_category;
  6365. table_iterator& operator++() noexcept // ++pre
  6366. {
  6367. ++iter_;
  6368. proxy_instantiated_ = false;
  6369. return *this;
  6370. }
  6371. table_iterator operator++(int) noexcept // post++
  6372. {
  6373. table_iterator out{ iter_ };
  6374. ++iter_;
  6375. proxy_instantiated_ = false;
  6376. return out;
  6377. }
  6378. table_iterator& operator--() noexcept // --pre
  6379. {
  6380. --iter_;
  6381. proxy_instantiated_ = false;
  6382. return *this;
  6383. }
  6384. table_iterator operator--(int) noexcept // post--
  6385. {
  6386. table_iterator out{ iter_ };
  6387. --iter_;
  6388. proxy_instantiated_ = false;
  6389. return out;
  6390. }
  6391. TOML_PURE_INLINE_GETTER
  6392. reference operator*() const noexcept
  6393. {
  6394. return *get_proxy();
  6395. }
  6396. TOML_PURE_INLINE_GETTER
  6397. pointer operator->() const noexcept
  6398. {
  6399. return get_proxy();
  6400. }
  6401. TOML_PURE_INLINE_GETTER
  6402. explicit operator const map_iterator&() const noexcept
  6403. {
  6404. return iter_;
  6405. }
  6406. TOML_CONSTRAINED_TEMPLATE(!C, bool C = IsConst)
  6407. TOML_PURE_INLINE_GETTER
  6408. explicit operator const const_map_iterator() const noexcept
  6409. {
  6410. return iter_;
  6411. }
  6412. TOML_PURE_INLINE_GETTER
  6413. friend bool operator==(const table_iterator& lhs, const table_iterator& rhs) noexcept
  6414. {
  6415. return lhs.iter_ == rhs.iter_;
  6416. }
  6417. TOML_PURE_INLINE_GETTER
  6418. friend bool operator!=(const table_iterator& lhs, const table_iterator& rhs) noexcept
  6419. {
  6420. return lhs.iter_ != rhs.iter_;
  6421. }
  6422. };
  6423. struct table_init_pair
  6424. {
  6425. mutable toml::key key;
  6426. mutable node_ptr value;
  6427. template <typename K, typename V>
  6428. TOML_NODISCARD_CTOR
  6429. table_init_pair(K&& k, V&& v, value_flags flags = preserve_source_value_flags) //
  6430. : key{ static_cast<K&&>(k) },
  6431. value{ make_node(static_cast<V&&>(v), flags) }
  6432. {}
  6433. };
  6434. }
  6435. TOML_IMPL_NAMESPACE_END;
  6436. TOML_NAMESPACE_START
  6437. {
  6438. using table_iterator = POXY_IMPLEMENTATION_DETAIL(impl::table_iterator<false>);
  6439. using const_table_iterator = POXY_IMPLEMENTATION_DETAIL(impl::table_iterator<true>);
  6440. class TOML_EXPORTED_CLASS table : public node
  6441. {
  6442. private:
  6443. using map_type = std::map<toml::key, impl::node_ptr, std::less<>>;
  6444. using map_pair = std::pair<const toml::key, impl::node_ptr>;
  6445. using map_iterator = typename map_type::iterator;
  6446. using const_map_iterator = typename map_type::const_iterator;
  6447. map_type map_;
  6448. bool inline_ = false;
  6449. TOML_NODISCARD_CTOR
  6450. TOML_EXPORTED_MEMBER_FUNCTION
  6451. table(const impl::table_init_pair*, const impl::table_init_pair*);
  6452. public:
  6453. TOML_NODISCARD_CTOR
  6454. TOML_EXPORTED_MEMBER_FUNCTION
  6455. table() noexcept;
  6456. TOML_EXPORTED_MEMBER_FUNCTION
  6457. ~table() noexcept;
  6458. TOML_NODISCARD_CTOR
  6459. TOML_EXPORTED_MEMBER_FUNCTION
  6460. table(const table&);
  6461. TOML_NODISCARD_CTOR
  6462. TOML_EXPORTED_MEMBER_FUNCTION
  6463. table(table&& other) noexcept;
  6464. TOML_NODISCARD_CTOR
  6465. TOML_EXPORTED_MEMBER_FUNCTION
  6466. explicit table(std::initializer_list<impl::table_init_pair> kvps) //
  6467. : table(kvps.begin(), kvps.end())
  6468. {}
  6469. TOML_EXPORTED_MEMBER_FUNCTION
  6470. table& operator=(const table&);
  6471. TOML_EXPORTED_MEMBER_FUNCTION
  6472. table& operator=(table&& rhs) noexcept;
  6473. TOML_CONST_INLINE_GETTER
  6474. node_type type() const noexcept final
  6475. {
  6476. return node_type::table;
  6477. }
  6478. TOML_PURE_GETTER
  6479. TOML_EXPORTED_MEMBER_FUNCTION
  6480. bool is_homogeneous(node_type ntype) const noexcept final;
  6481. TOML_PURE_GETTER
  6482. TOML_EXPORTED_MEMBER_FUNCTION
  6483. bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept final;
  6484. TOML_PURE_GETTER
  6485. TOML_EXPORTED_MEMBER_FUNCTION
  6486. bool is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept final;
  6487. template <typename ElemType = void>
  6488. TOML_PURE_GETTER
  6489. bool is_homogeneous() const noexcept
  6490. {
  6491. using type = impl::remove_cvref<impl::unwrap_node<ElemType>>;
  6492. static_assert(std::is_void_v<type> || toml::is_value<type> || toml::is_container<type>,
  6493. "The template type argument of table::is_homogeneous() must be void or one "
  6494. "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
  6495. return is_homogeneous(impl::node_type_of<type>);
  6496. }
  6497. TOML_CONST_INLINE_GETTER
  6498. bool is_table() const noexcept final
  6499. {
  6500. return true;
  6501. }
  6502. TOML_CONST_INLINE_GETTER
  6503. bool is_array() const noexcept final
  6504. {
  6505. return false;
  6506. }
  6507. TOML_PURE_GETTER
  6508. bool is_array_of_tables() const noexcept final
  6509. {
  6510. return false;
  6511. }
  6512. TOML_CONST_INLINE_GETTER
  6513. bool is_value() const noexcept final
  6514. {
  6515. return false;
  6516. }
  6517. TOML_CONST_INLINE_GETTER
  6518. bool is_string() const noexcept final
  6519. {
  6520. return false;
  6521. }
  6522. TOML_CONST_INLINE_GETTER
  6523. bool is_integer() const noexcept final
  6524. {
  6525. return false;
  6526. }
  6527. TOML_CONST_INLINE_GETTER
  6528. bool is_floating_point() const noexcept final
  6529. {
  6530. return false;
  6531. }
  6532. TOML_CONST_INLINE_GETTER
  6533. bool is_number() const noexcept final
  6534. {
  6535. return false;
  6536. }
  6537. TOML_CONST_INLINE_GETTER
  6538. bool is_boolean() const noexcept final
  6539. {
  6540. return false;
  6541. }
  6542. TOML_CONST_INLINE_GETTER
  6543. bool is_date() const noexcept final
  6544. {
  6545. return false;
  6546. }
  6547. TOML_CONST_INLINE_GETTER
  6548. bool is_time() const noexcept final
  6549. {
  6550. return false;
  6551. }
  6552. TOML_CONST_INLINE_GETTER
  6553. bool is_date_time() const noexcept final
  6554. {
  6555. return false;
  6556. }
  6557. TOML_CONST_INLINE_GETTER
  6558. table* as_table() noexcept final
  6559. {
  6560. return this;
  6561. }
  6562. TOML_CONST_INLINE_GETTER
  6563. array* as_array() noexcept final
  6564. {
  6565. return nullptr;
  6566. }
  6567. TOML_CONST_INLINE_GETTER
  6568. toml::value<std::string>* as_string() noexcept final
  6569. {
  6570. return nullptr;
  6571. }
  6572. TOML_CONST_INLINE_GETTER
  6573. toml::value<int64_t>* as_integer() noexcept final
  6574. {
  6575. return nullptr;
  6576. }
  6577. TOML_CONST_INLINE_GETTER
  6578. toml::value<double>* as_floating_point() noexcept final
  6579. {
  6580. return nullptr;
  6581. }
  6582. TOML_CONST_INLINE_GETTER
  6583. toml::value<bool>* as_boolean() noexcept final
  6584. {
  6585. return nullptr;
  6586. }
  6587. TOML_CONST_INLINE_GETTER
  6588. toml::value<date>* as_date() noexcept final
  6589. {
  6590. return nullptr;
  6591. }
  6592. TOML_CONST_INLINE_GETTER
  6593. toml::value<time>* as_time() noexcept final
  6594. {
  6595. return nullptr;
  6596. }
  6597. TOML_CONST_INLINE_GETTER
  6598. toml::value<date_time>* as_date_time() noexcept final
  6599. {
  6600. return nullptr;
  6601. }
  6602. TOML_CONST_INLINE_GETTER
  6603. const table* as_table() const noexcept final
  6604. {
  6605. return this;
  6606. }
  6607. TOML_CONST_INLINE_GETTER
  6608. const array* as_array() const noexcept final
  6609. {
  6610. return nullptr;
  6611. }
  6612. TOML_CONST_INLINE_GETTER
  6613. const toml::value<std::string>* as_string() const noexcept final
  6614. {
  6615. return nullptr;
  6616. }
  6617. TOML_CONST_INLINE_GETTER
  6618. const toml::value<int64_t>* as_integer() const noexcept final
  6619. {
  6620. return nullptr;
  6621. }
  6622. TOML_CONST_INLINE_GETTER
  6623. const toml::value<double>* as_floating_point() const noexcept final
  6624. {
  6625. return nullptr;
  6626. }
  6627. TOML_CONST_INLINE_GETTER
  6628. const toml::value<bool>* as_boolean() const noexcept final
  6629. {
  6630. return nullptr;
  6631. }
  6632. TOML_CONST_INLINE_GETTER
  6633. const toml::value<date>* as_date() const noexcept final
  6634. {
  6635. return nullptr;
  6636. }
  6637. TOML_CONST_INLINE_GETTER
  6638. const toml::value<time>* as_time() const noexcept final
  6639. {
  6640. return nullptr;
  6641. }
  6642. TOML_CONST_INLINE_GETTER
  6643. const toml::value<date_time>* as_date_time() const noexcept final
  6644. {
  6645. return nullptr;
  6646. }
  6647. TOML_PURE_INLINE_GETTER
  6648. bool is_inline() const noexcept
  6649. {
  6650. return inline_;
  6651. }
  6652. void is_inline(bool val) noexcept
  6653. {
  6654. inline_ = val;
  6655. }
  6656. TOML_PURE_GETTER
  6657. TOML_EXPORTED_MEMBER_FUNCTION
  6658. node* get(std::string_view key) noexcept;
  6659. TOML_PURE_INLINE_GETTER
  6660. const node* get(std::string_view key) const noexcept
  6661. {
  6662. return const_cast<table&>(*this).get(key);
  6663. }
  6664. #if TOML_ENABLE_WINDOWS_COMPAT
  6665. TOML_NODISCARD
  6666. node* get(std::wstring_view key)
  6667. {
  6668. if (empty())
  6669. return nullptr;
  6670. return get(impl::narrow(key));
  6671. }
  6672. TOML_NODISCARD
  6673. const node* get(std::wstring_view key) const
  6674. {
  6675. return const_cast<table&>(*this).get(key);
  6676. }
  6677. #endif // TOML_ENABLE_WINDOWS_COMPAT
  6678. template <typename T>
  6679. TOML_PURE_GETTER
  6680. impl::wrap_node<T>* get_as(std::string_view key) noexcept
  6681. {
  6682. const auto n = this->get(key);
  6683. return n ? n->template as<T>() : nullptr;
  6684. }
  6685. template <typename T>
  6686. TOML_PURE_GETTER
  6687. const impl::wrap_node<T>* get_as(std::string_view key) const noexcept
  6688. {
  6689. return const_cast<table&>(*this).template get_as<T>(key);
  6690. }
  6691. #if TOML_ENABLE_WINDOWS_COMPAT
  6692. template <typename T>
  6693. TOML_NODISCARD
  6694. impl::wrap_node<T>* get_as(std::wstring_view key)
  6695. {
  6696. if (empty())
  6697. return nullptr;
  6698. return get_as<T>(impl::narrow(key));
  6699. }
  6700. template <typename T>
  6701. TOML_NODISCARD
  6702. const impl::wrap_node<T>* get_as(std::wstring_view key) const
  6703. {
  6704. return const_cast<table&>(*this).template get_as<T>(key);
  6705. }
  6706. #endif // TOML_ENABLE_WINDOWS_COMPAT
  6707. TOML_NODISCARD
  6708. TOML_EXPORTED_MEMBER_FUNCTION
  6709. node& at(std::string_view key);
  6710. TOML_NODISCARD
  6711. const node& at(std::string_view key) const
  6712. {
  6713. return const_cast<table&>(*this).at(key);
  6714. }
  6715. #if TOML_ENABLE_WINDOWS_COMPAT
  6716. TOML_NODISCARD
  6717. node& at(std::wstring_view key)
  6718. {
  6719. return at(impl::narrow(key));
  6720. }
  6721. TOML_NODISCARD
  6722. const node& at(std::wstring_view key) const
  6723. {
  6724. return const_cast<table&>(*this).at(key);
  6725. }
  6726. #endif // TOML_ENABLE_WINDOWS_COMPAT
  6727. using iterator = toml::table_iterator;
  6728. using const_iterator = toml::const_table_iterator;
  6729. TOML_PURE_INLINE_GETTER
  6730. iterator begin() noexcept
  6731. {
  6732. return iterator{ map_.begin() };
  6733. }
  6734. TOML_PURE_INLINE_GETTER
  6735. const_iterator begin() const noexcept
  6736. {
  6737. return const_iterator{ map_.cbegin() };
  6738. }
  6739. TOML_PURE_INLINE_GETTER
  6740. const_iterator cbegin() const noexcept
  6741. {
  6742. return const_iterator{ map_.cbegin() };
  6743. }
  6744. TOML_PURE_INLINE_GETTER
  6745. iterator end() noexcept
  6746. {
  6747. return iterator{ map_.end() };
  6748. }
  6749. TOML_PURE_INLINE_GETTER
  6750. const_iterator end() const noexcept
  6751. {
  6752. return const_iterator{ map_.cend() };
  6753. }
  6754. TOML_PURE_INLINE_GETTER
  6755. const_iterator cend() const noexcept
  6756. {
  6757. return const_iterator{ map_.cend() };
  6758. }
  6759. private:
  6760. template <typename T, typename Table>
  6761. using for_each_value_ref = impl::copy_cvref<impl::wrap_node<impl::remove_cvref<impl::unwrap_node<T>>>, Table>;
  6762. template <typename Func, typename Table, typename T>
  6763. using can_for_each = std::disjunction<std::is_invocable<Func, const key&, for_each_value_ref<T, Table>>, //
  6764. std::is_invocable<Func, for_each_value_ref<T, Table>>>;
  6765. template <typename Func, typename Table, typename T>
  6766. using can_for_each_nothrow = std::conditional_t<
  6767. // first form
  6768. std::is_invocable_v<Func, const key&, for_each_value_ref<T, Table>>,
  6769. std::is_nothrow_invocable<Func, const key&, for_each_value_ref<T, Table>>,
  6770. std::conditional_t<
  6771. // second form
  6772. std::is_invocable_v<Func, for_each_value_ref<T, Table>>,
  6773. std::is_nothrow_invocable<Func, for_each_value_ref<T, Table>>,
  6774. std::false_type>>;
  6775. template <typename Func, typename Table>
  6776. using can_for_each_any = std::disjunction<can_for_each<Func, Table, table>,
  6777. can_for_each<Func, Table, array>,
  6778. can_for_each<Func, Table, std::string>,
  6779. can_for_each<Func, Table, int64_t>,
  6780. can_for_each<Func, Table, double>,
  6781. can_for_each<Func, Table, bool>,
  6782. can_for_each<Func, Table, date>,
  6783. can_for_each<Func, Table, time>,
  6784. can_for_each<Func, Table, date_time>>;
  6785. template <typename Func, typename Table, typename T>
  6786. using for_each_is_nothrow_one = std::disjunction<std::negation<can_for_each<Func, Table, T>>, //
  6787. can_for_each_nothrow<Func, Table, T>>;
  6788. template <typename Func, typename Table>
  6789. using for_each_is_nothrow = std::conjunction<for_each_is_nothrow_one<Func, Table, table>,
  6790. for_each_is_nothrow_one<Func, Table, array>,
  6791. for_each_is_nothrow_one<Func, Table, std::string>,
  6792. for_each_is_nothrow_one<Func, Table, int64_t>,
  6793. for_each_is_nothrow_one<Func, Table, double>,
  6794. for_each_is_nothrow_one<Func, Table, bool>,
  6795. for_each_is_nothrow_one<Func, Table, date>,
  6796. for_each_is_nothrow_one<Func, Table, time>,
  6797. for_each_is_nothrow_one<Func, Table, date_time>>;
  6798. template <typename Func, typename Table>
  6799. static void do_for_each(Func&& visitor, Table&& tbl) //
  6800. noexcept(for_each_is_nothrow<Func&&, Table&&>::value)
  6801. {
  6802. static_assert(can_for_each_any<Func&&, Table&&>::value,
  6803. "TOML table for_each visitors must be invocable for at least one of the toml::node "
  6804. "specializations:" TOML_SA_NODE_TYPE_LIST);
  6805. using kvp_type = impl::copy_cv<map_pair, std::remove_reference_t<Table>>;
  6806. for (kvp_type& kvp : tbl.map_)
  6807. {
  6808. using node_ref = impl::copy_cvref<toml::node, Table&&>;
  6809. static_assert(std::is_reference_v<node_ref>);
  6810. #if TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN
  6811. #ifndef TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_ACKNOWLEDGED
  6812. static_assert(impl::always_false<Func, Table, kvp_type, node_ref>, //
  6813. TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_MESSAGE);
  6814. #endif
  6815. static_cast<node_ref>(*kvp.second)
  6816. .visit(
  6817. [&]([[maybe_unused]] auto&& v) //
  6818. noexcept(for_each_is_nothrow_one<Func&&, Table&&, decltype(v)>::value)
  6819. {
  6820. using value_ref = for_each_value_ref<decltype(v), Table&&>;
  6821. static_assert(std::is_reference_v<value_ref>);
  6822. // func(key, val)
  6823. if constexpr (std::is_invocable_v<Func&&, const key&, value_ref>)
  6824. {
  6825. static_cast<Func&&>(visitor)(static_cast<const key&>(kvp.first),
  6826. static_cast<value_ref>(v));
  6827. }
  6828. // func(val)
  6829. else if constexpr (std::is_invocable_v<Func&&, value_ref>)
  6830. {
  6831. static_cast<Func&&>(visitor)(static_cast<value_ref>(v));
  6832. }
  6833. });
  6834. #else
  6835. const auto keep_going =
  6836. static_cast<node_ref>(*kvp.second)
  6837. .visit(
  6838. [&]([[maybe_unused]] auto&& v) //
  6839. noexcept(for_each_is_nothrow_one<Func&&, Table&&, decltype(v)>::value)
  6840. {
  6841. using value_ref = for_each_value_ref<decltype(v), Table&&>;
  6842. static_assert(std::is_reference_v<value_ref>);
  6843. // func(key, val)
  6844. if constexpr (std::is_invocable_v<Func&&, const key&, value_ref>)
  6845. {
  6846. using return_type = decltype(static_cast<Func&&>(
  6847. visitor)(static_cast<const key&>(kvp.first), static_cast<value_ref>(v)));
  6848. if constexpr (impl::is_constructible_or_convertible<bool, return_type>)
  6849. {
  6850. return static_cast<bool>(static_cast<Func&&>(
  6851. visitor)(static_cast<const key&>(kvp.first), static_cast<value_ref>(v)));
  6852. }
  6853. else
  6854. {
  6855. static_cast<Func&&>(visitor)(static_cast<const key&>(kvp.first),
  6856. static_cast<value_ref>(v));
  6857. return true;
  6858. }
  6859. }
  6860. // func(val)
  6861. else if constexpr (std::is_invocable_v<Func&&, value_ref>)
  6862. {
  6863. using return_type =
  6864. decltype(static_cast<Func&&>(visitor)(static_cast<value_ref>(v)));
  6865. if constexpr (impl::is_constructible_or_convertible<bool, return_type>)
  6866. {
  6867. return static_cast<bool>(
  6868. static_cast<Func&&>(visitor)(static_cast<value_ref>(v)));
  6869. }
  6870. else
  6871. {
  6872. static_cast<Func&&>(visitor)(static_cast<value_ref>(v));
  6873. return true;
  6874. }
  6875. }
  6876. // visitor not compatible with this particular type
  6877. else
  6878. return true;
  6879. });
  6880. if (!keep_going)
  6881. return;
  6882. #endif
  6883. }
  6884. }
  6885. public:
  6886. template <typename Func>
  6887. table& for_each(Func&& visitor) & //
  6888. noexcept(for_each_is_nothrow<Func&&, table&>::value)
  6889. {
  6890. do_for_each(static_cast<Func&&>(visitor), *this);
  6891. return *this;
  6892. }
  6893. template <typename Func>
  6894. table&& for_each(Func&& visitor) && //
  6895. noexcept(for_each_is_nothrow<Func&&, table&&>::value)
  6896. {
  6897. do_for_each(static_cast<Func&&>(visitor), static_cast<table&&>(*this));
  6898. return static_cast<table&&>(*this);
  6899. }
  6900. template <typename Func>
  6901. const table& for_each(Func&& visitor) const& //
  6902. noexcept(for_each_is_nothrow<Func&&, const table&>::value)
  6903. {
  6904. do_for_each(static_cast<Func&&>(visitor), *this);
  6905. return *this;
  6906. }
  6907. template <typename Func>
  6908. const table&& for_each(Func&& visitor) const&& //
  6909. noexcept(for_each_is_nothrow<Func&&, const table&&>::value)
  6910. {
  6911. do_for_each(static_cast<Func&&>(visitor), static_cast<const table&&>(*this));
  6912. return static_cast<const table&&>(*this);
  6913. }
  6914. TOML_PURE_INLINE_GETTER
  6915. bool empty() const noexcept
  6916. {
  6917. return map_.empty();
  6918. }
  6919. TOML_PURE_INLINE_GETTER
  6920. size_t size() const noexcept
  6921. {
  6922. return map_.size();
  6923. }
  6924. private:
  6925. TOML_PURE_GETTER
  6926. TOML_EXPORTED_MEMBER_FUNCTION
  6927. map_iterator get_lower_bound(std::string_view) noexcept;
  6928. public:
  6929. TOML_PURE_GETTER
  6930. iterator lower_bound(std::string_view key) noexcept
  6931. {
  6932. return iterator{ get_lower_bound(key) };
  6933. }
  6934. TOML_PURE_GETTER
  6935. const_iterator lower_bound(std::string_view key) const noexcept
  6936. {
  6937. return const_iterator{ const_cast<table&>(*this).get_lower_bound(key) };
  6938. }
  6939. #if TOML_ENABLE_WINDOWS_COMPAT
  6940. TOML_NODISCARD
  6941. iterator lower_bound(std::wstring_view key)
  6942. {
  6943. if (empty())
  6944. return end();
  6945. return lower_bound(impl::narrow(key));
  6946. }
  6947. TOML_NODISCARD
  6948. const_iterator lower_bound(std::wstring_view key) const
  6949. {
  6950. if (empty())
  6951. return end();
  6952. return lower_bound(impl::narrow(key));
  6953. }
  6954. #endif // TOML_ENABLE_WINDOWS_COMPAT
  6955. TOML_PURE_GETTER
  6956. TOML_EXPORTED_MEMBER_FUNCTION
  6957. iterator find(std::string_view key) noexcept;
  6958. TOML_PURE_GETTER
  6959. TOML_EXPORTED_MEMBER_FUNCTION
  6960. const_iterator find(std::string_view key) const noexcept;
  6961. TOML_PURE_GETTER
  6962. bool contains(std::string_view key) const noexcept
  6963. {
  6964. return get(key) != nullptr;
  6965. }
  6966. #if TOML_ENABLE_WINDOWS_COMPAT
  6967. TOML_NODISCARD
  6968. iterator find(std::wstring_view key)
  6969. {
  6970. if (empty())
  6971. return end();
  6972. return find(impl::narrow(key));
  6973. }
  6974. TOML_NODISCARD
  6975. const_iterator find(std::wstring_view key) const
  6976. {
  6977. return find(impl::narrow(key));
  6978. }
  6979. TOML_NODISCARD
  6980. bool contains(std::wstring_view key) const
  6981. {
  6982. return contains(impl::narrow(key));
  6983. }
  6984. #endif // TOML_ENABLE_WINDOWS_COMPAT
  6985. private:
  6986. TOML_EXPORTED_MEMBER_FUNCTION
  6987. map_iterator erase(const_map_iterator) noexcept;
  6988. TOML_EXPORTED_MEMBER_FUNCTION
  6989. map_iterator erase(const_map_iterator, const_map_iterator) noexcept;
  6990. public:
  6991. iterator erase(iterator pos) noexcept
  6992. {
  6993. return iterator{ erase(const_map_iterator{ pos }) };
  6994. }
  6995. iterator erase(const_iterator pos) noexcept
  6996. {
  6997. return iterator{ erase(const_map_iterator{ pos }) };
  6998. }
  6999. iterator erase(const_iterator begin, const_iterator end) noexcept
  7000. {
  7001. return iterator{ erase(const_map_iterator{ begin }, const_map_iterator{ end }) };
  7002. }
  7003. TOML_EXPORTED_MEMBER_FUNCTION
  7004. size_t erase(std::string_view key) noexcept;
  7005. #if TOML_ENABLE_WINDOWS_COMPAT
  7006. size_t erase(std::wstring_view key)
  7007. {
  7008. if (empty())
  7009. return false;
  7010. return erase(impl::narrow(key));
  7011. }
  7012. #endif // TOML_ENABLE_WINDOWS_COMPAT
  7013. TOML_EXPORTED_MEMBER_FUNCTION
  7014. table& prune(bool recursive = true) & noexcept;
  7015. table&& prune(bool recursive = true) && noexcept
  7016. {
  7017. return static_cast<toml::table&&>(this->prune(recursive));
  7018. }
  7019. TOML_EXPORTED_MEMBER_FUNCTION
  7020. void clear() noexcept;
  7021. private:
  7022. TOML_EXPORTED_MEMBER_FUNCTION
  7023. map_iterator insert_with_hint(const_iterator, key&&, impl::node_ptr&&);
  7024. public:
  7025. TOML_CONSTRAINED_TEMPLATE((is_key_or_convertible<KeyType&&> || impl::is_wide_string<KeyType>),
  7026. typename ValueType = void,
  7027. typename KeyType,
  7028. typename... ValueArgs)
  7029. iterator emplace_hint(const_iterator hint, KeyType&& key, ValueArgs&&... args)
  7030. {
  7031. static_assert(!impl::is_wide_string<KeyType> || TOML_ENABLE_WINDOWS_COMPAT,
  7032. "Emplacement using wide-character keys is only supported on Windows with "
  7033. "TOML_ENABLE_WINDOWS_COMPAT enabled.");
  7034. using raw_value_type = impl::remove_cvref<ValueType>;
  7035. using value_type = std::
  7036. conditional_t<std::is_void_v<raw_value_type>, impl::emplaced_type_of<ValueArgs&&...>, raw_value_type>;
  7037. if constexpr (impl::is_wide_string<KeyType>)
  7038. {
  7039. #if TOML_ENABLE_WINDOWS_COMPAT
  7040. return emplace_hint<value_type>(hint,
  7041. impl::narrow(static_cast<KeyType&&>(key)),
  7042. static_cast<ValueArgs&&>(args)...);
  7043. #else
  7044. static_assert(impl::always_false<KeyType>, "Evaluated unreachable branch!");
  7045. #endif
  7046. }
  7047. else
  7048. {
  7049. static constexpr auto moving_node_ptr = std::is_same_v<value_type, impl::node_ptr> //
  7050. && sizeof...(ValueArgs) == 1u //
  7051. && impl::first_is_same<impl::node_ptr&&, ValueArgs&&...>;
  7052. using unwrapped_type = impl::remove_cvref<impl::unwrap_node<value_type>>;
  7053. static_assert(moving_node_ptr //
  7054. || impl::is_native<unwrapped_type> //
  7055. || impl::is_one_of<unwrapped_type, table, array>, //
  7056. "ValueType argument of table::emplace_hint() must be one "
  7057. "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
  7058. map_iterator ipos = insert_with_hint(hint, toml::key{ static_cast<KeyType&&>(key) }, nullptr);
  7059. // if second is nullptr then we successully claimed the key and inserted the empty sentinel,
  7060. // so now we have to construct the actual value
  7061. if (!ipos->second)
  7062. {
  7063. if constexpr (moving_node_ptr)
  7064. ipos->second = std::move(static_cast<ValueArgs&&>(args)...);
  7065. else
  7066. {
  7067. #if TOML_COMPILER_HAS_EXCEPTIONS
  7068. try
  7069. {
  7070. #endif
  7071. ipos->second.reset(
  7072. new impl::wrap_node<unwrapped_type>{ static_cast<ValueArgs&&>(args)... });
  7073. #if TOML_COMPILER_HAS_EXCEPTIONS
  7074. }
  7075. catch (...)
  7076. {
  7077. erase(const_map_iterator{ ipos }); // strong exception guarantee
  7078. throw;
  7079. }
  7080. #endif
  7081. }
  7082. }
  7083. return iterator{ ipos };
  7084. }
  7085. }
  7086. TOML_CONSTRAINED_TEMPLATE((is_key_or_convertible<KeyType&&> || impl::is_wide_string<KeyType>),
  7087. typename KeyType,
  7088. typename ValueType)
  7089. std::pair<iterator, bool> insert(KeyType&& key,
  7090. ValueType&& val,
  7091. value_flags flags = preserve_source_value_flags)
  7092. {
  7093. static_assert(!impl::is_wide_string<KeyType> || TOML_ENABLE_WINDOWS_COMPAT,
  7094. "Insertion using wide-character keys is only supported on Windows with "
  7095. "TOML_ENABLE_WINDOWS_COMPAT enabled.");
  7096. if constexpr (is_node_view<ValueType>)
  7097. {
  7098. if (!val)
  7099. return { end(), false };
  7100. }
  7101. if constexpr (impl::is_wide_string<KeyType>)
  7102. {
  7103. #if TOML_ENABLE_WINDOWS_COMPAT
  7104. return insert(impl::narrow(static_cast<KeyType&&>(key)), static_cast<ValueType&&>(val), flags);
  7105. #else
  7106. static_assert(impl::always_false<KeyType>, "Evaluated unreachable branch!");
  7107. #endif
  7108. }
  7109. else
  7110. {
  7111. const auto key_view = std::string_view{ key };
  7112. map_iterator ipos = get_lower_bound(key_view);
  7113. if (ipos == map_.end() || ipos->first != key_view)
  7114. {
  7115. ipos = insert_with_hint(const_iterator{ ipos },
  7116. toml::key{ static_cast<KeyType&&>(key) },
  7117. impl::make_node(static_cast<ValueType&&>(val), flags));
  7118. return { iterator{ ipos }, true };
  7119. }
  7120. return { iterator{ ipos }, false };
  7121. }
  7122. }
  7123. TOML_CONSTRAINED_TEMPLATE((!is_key_or_convertible<Iter> && !impl::is_wide_string<Iter>), typename Iter)
  7124. void insert(Iter begin, Iter end, value_flags flags = preserve_source_value_flags)
  7125. {
  7126. if (begin == end)
  7127. return;
  7128. for (auto it = begin; it != end; it++)
  7129. {
  7130. if constexpr (std::is_rvalue_reference_v<decltype(*it)>)
  7131. insert(std::move((*it).first), std::move((*it).second), flags);
  7132. else
  7133. insert((*it).first, (*it).second, flags);
  7134. }
  7135. }
  7136. TOML_CONSTRAINED_TEMPLATE((is_key_or_convertible<KeyType&&> || impl::is_wide_string<KeyType>),
  7137. typename KeyType,
  7138. typename ValueType)
  7139. std::pair<iterator, bool> insert_or_assign(KeyType&& key,
  7140. ValueType&& val,
  7141. value_flags flags = preserve_source_value_flags)
  7142. {
  7143. static_assert(!impl::is_wide_string<KeyType> || TOML_ENABLE_WINDOWS_COMPAT,
  7144. "Insertion using wide-character keys is only supported on Windows with "
  7145. "TOML_ENABLE_WINDOWS_COMPAT enabled.");
  7146. if constexpr (is_node_view<ValueType>)
  7147. {
  7148. if (!val)
  7149. return { end(), false };
  7150. }
  7151. if constexpr (impl::is_wide_string<KeyType>)
  7152. {
  7153. #if TOML_ENABLE_WINDOWS_COMPAT
  7154. return insert_or_assign(impl::narrow(static_cast<KeyType&&>(key)),
  7155. static_cast<ValueType&&>(val),
  7156. flags);
  7157. #else
  7158. static_assert(impl::always_false<KeyType>, "Evaluated unreachable branch!");
  7159. #endif
  7160. }
  7161. else
  7162. {
  7163. const auto key_view = std::string_view{ key };
  7164. map_iterator ipos = get_lower_bound(key_view);
  7165. if (ipos == map_.end() || ipos->first != key_view)
  7166. {
  7167. ipos = insert_with_hint(const_iterator{ ipos },
  7168. toml::key{ static_cast<KeyType&&>(key) },
  7169. impl::make_node(static_cast<ValueType&&>(val), flags));
  7170. return { iterator{ ipos }, true };
  7171. }
  7172. else
  7173. {
  7174. (*ipos).second = impl::make_node(static_cast<ValueType&&>(val), flags);
  7175. return { iterator{ ipos }, false };
  7176. }
  7177. }
  7178. }
  7179. TOML_CONSTRAINED_TEMPLATE((is_key_or_convertible<KeyType&&> || impl::is_wide_string<KeyType>),
  7180. typename ValueType = void,
  7181. typename KeyType,
  7182. typename... ValueArgs)
  7183. std::pair<iterator, bool> emplace(KeyType&& key, ValueArgs&&... args)
  7184. {
  7185. static_assert(!impl::is_wide_string<KeyType> || TOML_ENABLE_WINDOWS_COMPAT,
  7186. "Emplacement using wide-character keys is only supported on Windows with "
  7187. "TOML_ENABLE_WINDOWS_COMPAT enabled.");
  7188. using raw_value_type = impl::remove_cvref<ValueType>;
  7189. using value_type = std::
  7190. conditional_t<std::is_void_v<raw_value_type>, impl::emplaced_type_of<ValueArgs&&...>, raw_value_type>;
  7191. if constexpr (impl::is_wide_string<KeyType>)
  7192. {
  7193. #if TOML_ENABLE_WINDOWS_COMPAT
  7194. return emplace<value_type>(impl::narrow(static_cast<KeyType&&>(key)),
  7195. static_cast<ValueArgs&&>(args)...);
  7196. #else
  7197. static_assert(impl::always_false<KeyType>, "Evaluated unreachable branch!");
  7198. #endif
  7199. }
  7200. else
  7201. {
  7202. using unwrapped_type = impl::remove_cvref<impl::unwrap_node<value_type>>;
  7203. static_assert((impl::is_native<unwrapped_type> || impl::is_one_of<unwrapped_type, table, array>),
  7204. "ValueType argument of table::emplace() must be one "
  7205. "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
  7206. const auto key_view = std::string_view{ key };
  7207. auto ipos = get_lower_bound(key_view);
  7208. if (ipos == map_.end() || ipos->first != key_view)
  7209. {
  7210. ipos = insert_with_hint(
  7211. const_iterator{ ipos },
  7212. toml::key{ static_cast<KeyType&&>(key) },
  7213. impl::node_ptr{ new impl::wrap_node<unwrapped_type>{ static_cast<ValueArgs&&>(args)... } });
  7214. return { iterator{ ipos }, true };
  7215. }
  7216. return { iterator{ ipos }, false };
  7217. }
  7218. }
  7219. using node::operator[]; // inherit operator[toml::path]
  7220. TOML_NODISCARD
  7221. node_view<node> operator[](std::string_view key) noexcept
  7222. {
  7223. return node_view<node>{ get(key) };
  7224. }
  7225. TOML_NODISCARD
  7226. node_view<const node> operator[](std::string_view key) const noexcept
  7227. {
  7228. return node_view<const node>{ get(key) };
  7229. }
  7230. #if TOML_ENABLE_WINDOWS_COMPAT
  7231. TOML_NODISCARD
  7232. node_view<node> operator[](std::wstring_view key)
  7233. {
  7234. return node_view<node>{ get(key) };
  7235. }
  7236. TOML_NODISCARD
  7237. node_view<const node> operator[](std::wstring_view key) const
  7238. {
  7239. return node_view<const node>{ get(key) };
  7240. }
  7241. #endif // TOML_ENABLE_WINDOWS_COMPAT
  7242. private:
  7243. TOML_PURE_GETTER
  7244. TOML_EXPORTED_STATIC_FUNCTION
  7245. static bool TOML_CALLCONV equal(const table&, const table&) noexcept;
  7246. public:
  7247. TOML_NODISCARD
  7248. friend bool operator==(const table& lhs, const table& rhs) noexcept
  7249. {
  7250. return equal(lhs, rhs);
  7251. }
  7252. TOML_NODISCARD
  7253. friend bool operator!=(const table& lhs, const table& rhs) noexcept
  7254. {
  7255. return !equal(lhs, rhs);
  7256. }
  7257. #if TOML_ENABLE_FORMATTERS
  7258. friend std::ostream& operator<<(std::ostream& lhs, const table& rhs)
  7259. {
  7260. impl::print_to_stream(lhs, rhs);
  7261. return lhs;
  7262. }
  7263. #endif
  7264. };
  7265. }
  7266. TOML_NAMESPACE_END;
  7267. #ifdef _MSC_VER
  7268. #pragma pop_macro("min")
  7269. #pragma pop_macro("max")
  7270. #ifndef __clang__
  7271. #pragma inline_recursion(off)
  7272. #endif
  7273. #endif
  7274. TOML_POP_WARNINGS;
  7275. //******** impl/unicode_autogenerated.hpp ****************************************************************************
  7276. TOML_PUSH_WARNINGS;
  7277. #ifdef _MSC_VER
  7278. #ifndef __clang__
  7279. #pragma inline_recursion(on)
  7280. #endif
  7281. #pragma push_macro("min")
  7282. #pragma push_macro("max")
  7283. #undef min
  7284. #undef max
  7285. #endif
  7286. #if TOML_GCC && TOML_GCC < 9
  7287. #pragma GCC push_options
  7288. #pragma GCC optimize("O1") // codegen bugs
  7289. #endif
  7290. // the functions in this namespace block are automatically generated by a tool - they are not meant to be hand-edited
  7291. TOML_IMPL_NAMESPACE_START
  7292. {
  7293. TOML_CONST_GETTER
  7294. constexpr bool is_ascii_horizontal_whitespace(char32_t c) noexcept
  7295. {
  7296. return c == U'\t' || c == U' ';
  7297. }
  7298. TOML_CONST_GETTER
  7299. constexpr bool is_non_ascii_horizontal_whitespace(char32_t c) noexcept
  7300. {
  7301. // 20 code units from 8 ranges (spanning a search area of 65120)
  7302. if (c < U'\xA0' || c > U'\uFEFF')
  7303. return false;
  7304. const auto child_index_0 = (static_cast<uint_least64_t>(c) - 0xA0ull) / 0x3FAull;
  7305. if ((1ull << child_index_0) & 0x7FFFFFFFFFFFF75Eull)
  7306. return false;
  7307. if (c == U'\xA0' || c == U'\u3000' || c == U'\uFEFF')
  7308. return true;
  7309. switch (child_index_0)
  7310. {
  7311. case 0x05: return c == U'\u1680' || c == U'\u180E';
  7312. case 0x07:
  7313. return (U'\u2000' <= c && c <= U'\u200B') || (U'\u205F' <= c && c <= U'\u2060') || c == U'\u202F';
  7314. default: TOML_UNREACHABLE;
  7315. }
  7316. TOML_UNREACHABLE;
  7317. }
  7318. TOML_CONST_GETTER
  7319. constexpr bool is_ascii_vertical_whitespace(char32_t c) noexcept
  7320. {
  7321. return c >= U'\n' && c <= U'\r';
  7322. }
  7323. TOML_CONST_GETTER
  7324. constexpr bool is_non_ascii_vertical_whitespace(char32_t c) noexcept
  7325. {
  7326. return (U'\u2028' <= c && c <= U'\u2029') || c == U'\x85';
  7327. }
  7328. TOML_CONST_GETTER
  7329. constexpr bool is_ascii_bare_key_character(char32_t c) noexcept
  7330. {
  7331. #if TOML_LANG_UNRELEASED // toml/issues/644 ('+' in bare keys)
  7332. if TOML_UNLIKELY(c == U'+')
  7333. return true;
  7334. #endif
  7335. // 64 code units from 5 ranges (spanning a search area of 78)
  7336. if (c < U'-' || c > U'z')
  7337. return false;
  7338. return (((static_cast<uint_least64_t>(c) - 0x2Dull) / 0x40ull) != 0ull)
  7339. || ((1ull << (static_cast<uint_least64_t>(c) - 0x2Dull)) & 0xFFF43FFFFFF01FF9ull);
  7340. }
  7341. #if TOML_LANG_UNRELEASED // toml/pull/891 (unicode bare keys)
  7342. TOML_CONST_GETTER
  7343. constexpr bool is_non_ascii_bare_key_character(char32_t c) noexcept
  7344. {
  7345. // 971732 code units from 16 ranges (spanning a search area of 982862)
  7346. if (c < U'\xB2' || c > U'\U000EFFFF')
  7347. return false;
  7348. const auto child_index_0 = (static_cast<uint_least64_t>(c) - 0xB2ull) / 0x3BFEull;
  7349. if ((1ull << child_index_0) & 0xFFFFFFFFFFFFFFE6ull)
  7350. return true;
  7351. switch (child_index_0)
  7352. {
  7353. case 0x00: // [0] 00B2 - 3CAF
  7354. {
  7355. // 12710 code units from 13 ranges (spanning a search area of 15358)
  7356. TOML_ASSUME(c >= U'\xB2' && c <= U'\u3CAF');
  7357. constexpr uint_least64_t bitmask_table_1[] = {
  7358. 0xFFFFFFDFFFFFDC83u, 0xFFFFFFFFFFFFFFDFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7359. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7360. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFEFFFu,
  7361. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7362. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7363. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7364. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7365. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7366. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7367. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7368. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7369. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7370. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7371. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7372. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7373. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7374. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7375. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7376. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7377. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7378. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7379. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7380. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7381. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7382. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7383. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7384. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7385. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7386. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7387. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7388. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7389. 0xFFFFFFFFFFFFFFFFu, 0x000000000C003FFFu, 0xC000000000006000u, 0xFFFFFFFFFFFFFFFFu,
  7390. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0x000000003FFFFFFFu,
  7391. 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
  7392. 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
  7393. 0x0000000000000000u, 0x0000000000000000u, 0xFFFFC00000000000u, 0xFFFFFFFFFFFFFFFFu,
  7394. 0xFFFFFFFFFFFFFFFFu, 0x0000000000003FFFu, 0x0000000000000000u, 0x0000000000000000u,
  7395. 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
  7396. 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
  7397. 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
  7398. 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
  7399. 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
  7400. 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u, 0x0000000000000000u,
  7401. 0x0000000000000000u, 0xFFFFFFFFFFFFC000u, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7402. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7403. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7404. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7405. 0x3FFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFF8000u, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7406. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7407. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7408. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7409. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7410. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7411. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7412. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7413. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7414. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7415. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7416. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu,
  7417. 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0xFFFFFFFFFFFFFFFFu, 0x3FFFFFFFFFFFFFFFu,
  7418. };
  7419. return bitmask_table_1[(static_cast<uint_least64_t>(c) - 0xB2ull) / 0x40ull]
  7420. & (0x1ull << ((static_cast<uint_least64_t>(c) - 0xB2ull) % 0x40ull));
  7421. }
  7422. case 0x03: return c <= U'\uD7FF';
  7423. case 0x04:
  7424. return (U'\uF900' <= c && c <= U'\uFDCF') || (U'\uFDF0' <= c && c <= U'\uFFFD') || U'\U00010000' <= c;
  7425. default: TOML_UNREACHABLE;
  7426. }
  7427. TOML_UNREACHABLE;
  7428. }
  7429. #endif // TOML_LANG_UNRELEASED
  7430. }
  7431. TOML_IMPL_NAMESPACE_END;
  7432. #if TOML_GCC && TOML_GCC < 9
  7433. #pragma GCC pop_options
  7434. #endif
  7435. #ifdef _MSC_VER
  7436. #pragma pop_macro("min")
  7437. #pragma pop_macro("max")
  7438. #ifndef __clang__
  7439. #pragma inline_recursion(off)
  7440. #endif
  7441. #endif
  7442. TOML_POP_WARNINGS;
  7443. //******** impl/unicode.hpp ******************************************************************************************
  7444. TOML_PUSH_WARNINGS;
  7445. #ifdef _MSC_VER
  7446. #ifndef __clang__
  7447. #pragma inline_recursion(on)
  7448. #endif
  7449. #pragma push_macro("min")
  7450. #pragma push_macro("max")
  7451. #undef min
  7452. #undef max
  7453. #endif
  7454. TOML_IMPL_NAMESPACE_START
  7455. {
  7456. TOML_CONST_GETTER
  7457. constexpr bool is_string_delimiter(char32_t c) noexcept
  7458. {
  7459. return c == U'"' || c == U'\'';
  7460. }
  7461. TOML_CONST_GETTER
  7462. constexpr bool is_ascii_letter(char32_t c) noexcept
  7463. {
  7464. return (c >= U'a' && c <= U'z') || (c >= U'A' && c <= U'Z');
  7465. }
  7466. TOML_CONST_GETTER
  7467. constexpr bool is_binary_digit(char32_t c) noexcept
  7468. {
  7469. return c == U'0' || c == U'1';
  7470. }
  7471. TOML_CONST_GETTER
  7472. constexpr bool is_octal_digit(char32_t c) noexcept
  7473. {
  7474. return (c >= U'0' && c <= U'7');
  7475. }
  7476. TOML_CONST_GETTER
  7477. constexpr bool is_decimal_digit(char32_t c) noexcept
  7478. {
  7479. return (c >= U'0' && c <= U'9');
  7480. }
  7481. TOML_CONST_GETTER
  7482. constexpr bool is_hexadecimal_digit(char32_t c) noexcept
  7483. {
  7484. return U'0' <= c && c <= U'f' && (1ull << (static_cast<uint_least64_t>(c) - 0x30u)) & 0x7E0000007E03FFull;
  7485. }
  7486. template <typename T>
  7487. TOML_CONST_GETTER
  7488. constexpr uint_least32_t hex_to_dec(const T c) noexcept
  7489. {
  7490. if constexpr (std::is_same_v<remove_cvref<T>, uint_least32_t>)
  7491. return c >= 0x41u // >= 'A'
  7492. ? 10u + (c | 0x20u) - 0x61u // - 'a'
  7493. : c - 0x30u // - '0'
  7494. ;
  7495. else
  7496. return hex_to_dec(static_cast<uint_least32_t>(c));
  7497. }
  7498. TOML_CONST_GETTER
  7499. constexpr bool is_horizontal_whitespace(char32_t c) noexcept
  7500. {
  7501. return is_ascii_horizontal_whitespace(c) || is_non_ascii_horizontal_whitespace(c);
  7502. }
  7503. TOML_CONST_GETTER
  7504. constexpr bool is_vertical_whitespace(char32_t c) noexcept
  7505. {
  7506. return is_ascii_vertical_whitespace(c) || is_non_ascii_vertical_whitespace(c);
  7507. }
  7508. TOML_CONST_GETTER
  7509. constexpr bool is_whitespace(char32_t c) noexcept
  7510. {
  7511. return is_horizontal_whitespace(c) || is_vertical_whitespace(c);
  7512. }
  7513. TOML_CONST_GETTER
  7514. constexpr bool is_bare_key_character(char32_t c) noexcept
  7515. {
  7516. return is_ascii_bare_key_character(c)
  7517. #if TOML_LANG_UNRELEASED // toml/pull/891 (unicode bare keys)
  7518. || is_non_ascii_bare_key_character(c)
  7519. #endif
  7520. ;
  7521. }
  7522. TOML_CONST_GETTER
  7523. constexpr bool is_value_terminator(char32_t c) noexcept
  7524. {
  7525. return is_whitespace(c) || c == U']' || c == U'}' || c == U',' || c == U'#';
  7526. }
  7527. TOML_CONST_GETTER
  7528. constexpr bool is_control_character(char c) noexcept
  7529. {
  7530. return c <= '\u001F' || c == '\u007F';
  7531. }
  7532. TOML_CONST_GETTER
  7533. constexpr bool is_control_character(char32_t c) noexcept
  7534. {
  7535. return c <= U'\u001F' || c == U'\u007F';
  7536. }
  7537. TOML_CONST_GETTER
  7538. constexpr bool is_nontab_control_character(char32_t c) noexcept
  7539. {
  7540. return c <= U'\u0008' || (c >= U'\u000A' && c <= U'\u001F') || c == U'\u007F';
  7541. }
  7542. TOML_CONST_GETTER
  7543. constexpr bool is_unicode_surrogate(char32_t c) noexcept
  7544. {
  7545. return c >= 0xD800u && c <= 0xDFFF;
  7546. }
  7547. struct utf8_decoder
  7548. {
  7549. // utf8_decoder based on this: https://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  7550. // Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]>
  7551. uint_least32_t state{};
  7552. char32_t codepoint{};
  7553. static constexpr uint8_t state_table[]{
  7554. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  7555. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  7556. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  7557. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  7558. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
  7559. 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7,
  7560. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7561. 7, 7, 7, 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  7562. 2, 2, 2, 2, 2, 2, 2, 2, 10, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 11, 6, 6,
  7563. 6, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  7564. 0, 12, 24, 36, 60, 96, 84, 12, 12, 12, 48, 72, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 0, 12,
  7565. 12, 12, 12, 12, 0, 12, 0, 12, 12, 12, 24, 12, 12, 12, 12, 12, 24, 12, 24, 12, 12, 12, 12, 12, 12, 12, 12,
  7566. 12, 24, 12, 12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 12, 12, 12, 12, 36, 12,
  7567. 36, 12, 12, 12, 36, 12, 12, 12, 12, 12, 36, 12, 36, 12, 12, 12, 36, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12
  7568. };
  7569. TOML_PURE_INLINE_GETTER
  7570. constexpr bool error() const noexcept
  7571. {
  7572. return state == uint_least32_t{ 12u };
  7573. }
  7574. TOML_PURE_INLINE_GETTER
  7575. constexpr bool has_code_point() const noexcept
  7576. {
  7577. return state == uint_least32_t{};
  7578. }
  7579. TOML_PURE_INLINE_GETTER
  7580. constexpr bool needs_more_input() const noexcept
  7581. {
  7582. return !has_code_point() && !error();
  7583. }
  7584. constexpr void operator()(uint8_t byte) noexcept
  7585. {
  7586. TOML_ASSERT_ASSUME(!error());
  7587. const auto type = state_table[byte];
  7588. codepoint = static_cast<char32_t>(has_code_point() ? (uint_least32_t{ 255u } >> type) & byte
  7589. : (byte & uint_least32_t{ 63u })
  7590. | (static_cast<uint_least32_t>(codepoint) << 6));
  7591. state = state_table[state + uint_least32_t{ 256u } + type];
  7592. }
  7593. TOML_ALWAYS_INLINE
  7594. constexpr void operator()(char c) noexcept
  7595. {
  7596. operator()(static_cast<uint8_t>(c));
  7597. }
  7598. TOML_ALWAYS_INLINE
  7599. constexpr void reset() noexcept
  7600. {
  7601. state = {};
  7602. }
  7603. };
  7604. TOML_PURE_GETTER
  7605. TOML_ATTR(nonnull)
  7606. bool is_ascii(const char* str, size_t len) noexcept;
  7607. }
  7608. TOML_IMPL_NAMESPACE_END;
  7609. #ifdef _MSC_VER
  7610. #pragma pop_macro("min")
  7611. #pragma pop_macro("max")
  7612. #ifndef __clang__
  7613. #pragma inline_recursion(off)
  7614. #endif
  7615. #endif
  7616. TOML_POP_WARNINGS;
  7617. //******** impl/parse_error.hpp **************************************************************************************
  7618. #if TOML_ENABLE_PARSER
  7619. //******** impl/std_except.hpp ***************************************************************************************
  7620. TOML_DISABLE_WARNINGS;
  7621. #if TOML_EXCEPTIONS
  7622. #include <stdexcept>
  7623. #endif
  7624. TOML_ENABLE_WARNINGS;
  7625. //******** impl/parse_error.hpp **************************************************************************************
  7626. TOML_PUSH_WARNINGS;
  7627. #ifdef _MSC_VER
  7628. #ifndef __clang__
  7629. #pragma inline_recursion(on)
  7630. #endif
  7631. #pragma push_macro("min")
  7632. #pragma push_macro("max")
  7633. #undef min
  7634. #undef max
  7635. #endif
  7636. #if TOML_DOXYGEN || !TOML_EXCEPTIONS
  7637. #define TOML_PARSE_ERROR_BASE
  7638. #else
  7639. #define TOML_PARSE_ERROR_BASE : public std::runtime_error
  7640. #endif
  7641. TOML_NAMESPACE_START
  7642. {
  7643. TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, ex, noex);
  7644. class parse_error TOML_PARSE_ERROR_BASE
  7645. {
  7646. private:
  7647. #if !TOML_EXCEPTIONS
  7648. std::string description_;
  7649. #endif
  7650. source_region source_;
  7651. public:
  7652. #if TOML_EXCEPTIONS
  7653. TOML_NODISCARD_CTOR
  7654. TOML_ATTR(nonnull)
  7655. parse_error(const char* desc, source_region&& src) noexcept //
  7656. : std::runtime_error{ desc },
  7657. source_{ std::move(src) }
  7658. {}
  7659. TOML_NODISCARD_CTOR
  7660. TOML_ATTR(nonnull)
  7661. parse_error(const char* desc, const source_region& src) noexcept //
  7662. : parse_error{ desc, source_region{ src } }
  7663. {}
  7664. TOML_NODISCARD_CTOR
  7665. TOML_ATTR(nonnull)
  7666. parse_error(const char* desc, const source_position& position, const source_path_ptr& path = {}) noexcept
  7667. : parse_error{ desc, source_region{ position, position, path } }
  7668. {}
  7669. #else
  7670. TOML_NODISCARD_CTOR
  7671. parse_error(std::string&& desc, source_region&& src) noexcept //
  7672. : description_{ std::move(desc) },
  7673. source_{ std::move(src) }
  7674. {}
  7675. TOML_NODISCARD_CTOR
  7676. parse_error(std::string&& desc, const source_region& src) noexcept //
  7677. : parse_error{ std::move(desc), source_region{ src } }
  7678. {}
  7679. TOML_NODISCARD_CTOR
  7680. parse_error(std::string&& desc, const source_position& position, const source_path_ptr& path = {}) noexcept
  7681. : parse_error{ std::move(desc), source_region{ position, position, path } }
  7682. {}
  7683. #endif
  7684. TOML_NODISCARD
  7685. std::string_view description() const noexcept
  7686. {
  7687. #if TOML_EXCEPTIONS
  7688. return std::string_view{ what() };
  7689. #else
  7690. return description_;
  7691. #endif
  7692. }
  7693. TOML_NODISCARD
  7694. const source_region& source() const noexcept
  7695. {
  7696. return source_;
  7697. }
  7698. friend std::ostream& operator<<(std::ostream& lhs, const parse_error& rhs)
  7699. {
  7700. impl::print_to_stream(lhs, rhs.description());
  7701. impl::print_to_stream(lhs, "\n\t(error occurred at "sv);
  7702. impl::print_to_stream(lhs, rhs.source());
  7703. impl::print_to_stream(lhs, ")"sv);
  7704. return lhs;
  7705. }
  7706. };
  7707. TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
  7708. }
  7709. TOML_NAMESPACE_END;
  7710. #undef TOML_PARSE_ERROR_BASE
  7711. #ifdef _MSC_VER
  7712. #pragma pop_macro("min")
  7713. #pragma pop_macro("max")
  7714. #ifndef __clang__
  7715. #pragma inline_recursion(off)
  7716. #endif
  7717. #endif
  7718. TOML_POP_WARNINGS;
  7719. #endif // TOML_ENABLE_PARSER
  7720. //******** impl/parse_result.hpp *************************************************************************************
  7721. #if TOML_DOXYGEN || (TOML_ENABLE_PARSER && !TOML_EXCEPTIONS)
  7722. TOML_PUSH_WARNINGS;
  7723. #ifdef _MSC_VER
  7724. #ifndef __clang__
  7725. #pragma inline_recursion(on)
  7726. #endif
  7727. #pragma push_macro("min")
  7728. #pragma push_macro("max")
  7729. #undef min
  7730. #undef max
  7731. #endif
  7732. TOML_NAMESPACE_START
  7733. {
  7734. TOML_ABI_NAMESPACE_START(noex);
  7735. class parse_result
  7736. {
  7737. private:
  7738. struct storage_t
  7739. {
  7740. static constexpr size_t size =
  7741. (sizeof(toml::table) < sizeof(parse_error) ? sizeof(parse_error) : sizeof(toml::table));
  7742. static constexpr size_t align =
  7743. (alignof(toml::table) < alignof(parse_error) ? alignof(parse_error) : alignof(toml::table));
  7744. alignas(align) unsigned char bytes[size];
  7745. };
  7746. alignas(storage_t::align) mutable storage_t storage_;
  7747. bool err_;
  7748. template <typename Type>
  7749. TOML_NODISCARD
  7750. TOML_ALWAYS_INLINE
  7751. static Type* get_as(storage_t& s) noexcept
  7752. {
  7753. return TOML_LAUNDER(reinterpret_cast<Type*>(s.bytes));
  7754. }
  7755. void destroy() noexcept
  7756. {
  7757. if (err_)
  7758. get_as<parse_error>(storage_)->~parse_error();
  7759. else
  7760. get_as<toml::table>(storage_)->~table();
  7761. }
  7762. public:
  7763. TOML_NODISCARD_CTOR
  7764. parse_result() noexcept //
  7765. : err_{ true }
  7766. {
  7767. ::new (static_cast<void*>(storage_.bytes)) parse_error{ std::string{}, source_region{} };
  7768. }
  7769. TOML_NODISCARD_CTOR
  7770. explicit parse_result(toml::table&& tbl) noexcept //
  7771. : err_{ false }
  7772. {
  7773. ::new (static_cast<void*>(storage_.bytes)) toml::table{ std::move(tbl) };
  7774. }
  7775. TOML_NODISCARD_CTOR
  7776. explicit parse_result(parse_error&& err) noexcept //
  7777. : err_{ true }
  7778. {
  7779. ::new (static_cast<void*>(storage_.bytes)) parse_error{ std::move(err) };
  7780. }
  7781. TOML_NODISCARD_CTOR
  7782. parse_result(parse_result&& res) noexcept //
  7783. : err_{ res.err_ }
  7784. {
  7785. if (err_)
  7786. ::new (static_cast<void*>(storage_.bytes)) parse_error{ std::move(res).error() };
  7787. else
  7788. ::new (static_cast<void*>(storage_.bytes)) toml::table{ std::move(res).table() };
  7789. }
  7790. parse_result& operator=(parse_result&& rhs) noexcept
  7791. {
  7792. if (err_ != rhs.err_)
  7793. {
  7794. destroy();
  7795. err_ = rhs.err_;
  7796. if (err_)
  7797. ::new (static_cast<void*>(storage_.bytes)) parse_error{ std::move(rhs).error() };
  7798. else
  7799. ::new (static_cast<void*>(storage_.bytes)) toml::table{ std::move(rhs).table() };
  7800. }
  7801. else
  7802. {
  7803. if (err_)
  7804. error() = std::move(rhs).error();
  7805. else
  7806. table() = std::move(rhs).table();
  7807. }
  7808. return *this;
  7809. }
  7810. ~parse_result() noexcept
  7811. {
  7812. destroy();
  7813. }
  7814. TOML_NODISCARD
  7815. bool succeeded() const noexcept
  7816. {
  7817. return !err_;
  7818. }
  7819. TOML_NODISCARD
  7820. bool failed() const noexcept
  7821. {
  7822. return err_;
  7823. }
  7824. TOML_NODISCARD
  7825. explicit operator bool() const noexcept
  7826. {
  7827. return !err_;
  7828. }
  7829. TOML_NODISCARD
  7830. toml::table& table() & noexcept
  7831. {
  7832. TOML_ASSERT_ASSUME(!err_);
  7833. return *get_as<toml::table>(storage_);
  7834. }
  7835. TOML_NODISCARD
  7836. toml::table&& table() && noexcept
  7837. {
  7838. TOML_ASSERT_ASSUME(!err_);
  7839. return static_cast<toml::table&&>(*get_as<toml::table>(storage_));
  7840. }
  7841. TOML_NODISCARD
  7842. const toml::table& table() const& noexcept
  7843. {
  7844. TOML_ASSERT_ASSUME(!err_);
  7845. return *get_as<const toml::table>(storage_);
  7846. }
  7847. TOML_NODISCARD
  7848. /* implicit */ operator toml::table&() noexcept
  7849. {
  7850. return table();
  7851. }
  7852. TOML_NODISCARD
  7853. /* implicit */ operator toml::table&&() noexcept
  7854. {
  7855. return std::move(table());
  7856. }
  7857. TOML_NODISCARD
  7858. /* implicit */ operator const toml::table&() const noexcept
  7859. {
  7860. return table();
  7861. }
  7862. TOML_NODISCARD
  7863. parse_error& error() & noexcept
  7864. {
  7865. TOML_ASSERT_ASSUME(err_);
  7866. return *get_as<parse_error>(storage_);
  7867. }
  7868. TOML_NODISCARD
  7869. parse_error&& error() && noexcept
  7870. {
  7871. TOML_ASSERT_ASSUME(err_);
  7872. return static_cast<parse_error&&>(*get_as<parse_error>(storage_));
  7873. }
  7874. TOML_NODISCARD
  7875. const parse_error& error() const& noexcept
  7876. {
  7877. TOML_ASSERT_ASSUME(err_);
  7878. return *get_as<const parse_error>(storage_);
  7879. }
  7880. TOML_NODISCARD
  7881. explicit operator parse_error&() noexcept
  7882. {
  7883. return error();
  7884. }
  7885. TOML_NODISCARD
  7886. explicit operator parse_error&&() noexcept
  7887. {
  7888. return std::move(error());
  7889. }
  7890. TOML_NODISCARD
  7891. explicit operator const parse_error&() const noexcept
  7892. {
  7893. return error();
  7894. }
  7895. using iterator = table_iterator;
  7896. using const_iterator = const_table_iterator;
  7897. TOML_NODISCARD
  7898. table_iterator begin() noexcept
  7899. {
  7900. return err_ ? table_iterator{} : table().begin();
  7901. }
  7902. TOML_NODISCARD
  7903. const_table_iterator begin() const noexcept
  7904. {
  7905. return err_ ? const_table_iterator{} : table().begin();
  7906. }
  7907. TOML_NODISCARD
  7908. const_table_iterator cbegin() const noexcept
  7909. {
  7910. return err_ ? const_table_iterator{} : table().cbegin();
  7911. }
  7912. TOML_NODISCARD
  7913. table_iterator end() noexcept
  7914. {
  7915. return err_ ? table_iterator{} : table().end();
  7916. }
  7917. TOML_NODISCARD
  7918. const_table_iterator end() const noexcept
  7919. {
  7920. return err_ ? const_table_iterator{} : table().end();
  7921. }
  7922. TOML_NODISCARD
  7923. const_table_iterator cend() const noexcept
  7924. {
  7925. return err_ ? const_table_iterator{} : table().cend();
  7926. }
  7927. TOML_NODISCARD
  7928. node_view<node> at_path(std::string_view path) noexcept
  7929. {
  7930. return err_ ? node_view<node>{} : table().at_path(path);
  7931. }
  7932. TOML_NODISCARD
  7933. node_view<const node> at_path(std::string_view path) const noexcept
  7934. {
  7935. return err_ ? node_view<const node>{} : table().at_path(path);
  7936. }
  7937. TOML_NODISCARD
  7938. node_view<node> at_path(const toml::path& path) noexcept
  7939. {
  7940. return err_ ? node_view<node>{} : table().at_path(path);
  7941. }
  7942. TOML_NODISCARD
  7943. node_view<const node> at_path(const toml::path& path) const noexcept
  7944. {
  7945. return err_ ? node_view<const node>{} : table().at_path(path);
  7946. }
  7947. #if TOML_ENABLE_WINDOWS_COMPAT
  7948. TOML_NODISCARD
  7949. node_view<node> at_path(std::wstring_view path)
  7950. {
  7951. return err_ ? node_view<node>{} : table().at_path(path);
  7952. }
  7953. TOML_NODISCARD
  7954. node_view<const node> at_path(std::wstring_view path) const
  7955. {
  7956. return err_ ? node_view<const node>{} : table().at_path(path);
  7957. }
  7958. #endif
  7959. TOML_NODISCARD
  7960. node_view<node> operator[](const toml::path& path) noexcept
  7961. {
  7962. return err_ ? node_view<node>{} : table()[path];
  7963. }
  7964. TOML_NODISCARD
  7965. node_view<const node> operator[](const toml::path& path) const noexcept
  7966. {
  7967. return err_ ? node_view<const node>{} : table()[path];
  7968. }
  7969. TOML_NODISCARD
  7970. node_view<node> operator[](std::string_view key) noexcept
  7971. {
  7972. return err_ ? node_view<node>{} : table()[key];
  7973. }
  7974. TOML_NODISCARD
  7975. node_view<const node> operator[](std::string_view key) const noexcept
  7976. {
  7977. return err_ ? node_view<const node>{} : table()[key];
  7978. }
  7979. #if TOML_ENABLE_WINDOWS_COMPAT
  7980. TOML_NODISCARD
  7981. node_view<node> operator[](std::wstring_view key)
  7982. {
  7983. return err_ ? node_view<node>{} : table()[key];
  7984. }
  7985. TOML_NODISCARD
  7986. node_view<const node> operator[](std::wstring_view key) const
  7987. {
  7988. return err_ ? node_view<const node>{} : table()[key];
  7989. }
  7990. #endif // TOML_ENABLE_WINDOWS_COMPAT
  7991. #if TOML_ENABLE_FORMATTERS
  7992. friend std::ostream& operator<<(std::ostream& os, const parse_result& result)
  7993. {
  7994. return result.err_ ? (os << result.error()) : (os << result.table());
  7995. }
  7996. #endif
  7997. };
  7998. TOML_ABI_NAMESPACE_END;
  7999. }
  8000. TOML_NAMESPACE_END;
  8001. #ifdef _MSC_VER
  8002. #pragma pop_macro("min")
  8003. #pragma pop_macro("max")
  8004. #ifndef __clang__
  8005. #pragma inline_recursion(off)
  8006. #endif
  8007. #endif
  8008. TOML_POP_WARNINGS;
  8009. #endif // TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
  8010. //******** impl/parser.hpp *******************************************************************************************
  8011. #if TOML_ENABLE_PARSER
  8012. TOML_PUSH_WARNINGS;
  8013. #ifdef _MSC_VER
  8014. #ifndef __clang__
  8015. #pragma inline_recursion(on)
  8016. #endif
  8017. #pragma push_macro("min")
  8018. #pragma push_macro("max")
  8019. #undef min
  8020. #undef max
  8021. #endif
  8022. TOML_NAMESPACE_START
  8023. {
  8024. TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, ex, noex);
  8025. TOML_NODISCARD
  8026. TOML_EXPORTED_FREE_FUNCTION
  8027. parse_result TOML_CALLCONV parse(std::string_view doc, std::string_view source_path = {});
  8028. TOML_NODISCARD
  8029. TOML_EXPORTED_FREE_FUNCTION
  8030. parse_result TOML_CALLCONV parse(std::string_view doc, std::string && source_path);
  8031. TOML_NODISCARD
  8032. TOML_EXPORTED_FREE_FUNCTION
  8033. parse_result TOML_CALLCONV parse_file(std::string_view file_path);
  8034. #if TOML_HAS_CHAR8
  8035. TOML_NODISCARD
  8036. TOML_EXPORTED_FREE_FUNCTION
  8037. parse_result TOML_CALLCONV parse(std::u8string_view doc, std::string_view source_path = {});
  8038. TOML_NODISCARD
  8039. TOML_EXPORTED_FREE_FUNCTION
  8040. parse_result TOML_CALLCONV parse(std::u8string_view doc, std::string && source_path);
  8041. TOML_NODISCARD
  8042. TOML_EXPORTED_FREE_FUNCTION
  8043. parse_result TOML_CALLCONV parse_file(std::u8string_view file_path);
  8044. #endif // TOML_HAS_CHAR8
  8045. #if TOML_ENABLE_WINDOWS_COMPAT
  8046. TOML_NODISCARD
  8047. TOML_EXPORTED_FREE_FUNCTION
  8048. parse_result TOML_CALLCONV parse(std::string_view doc, std::wstring_view source_path);
  8049. TOML_NODISCARD
  8050. TOML_EXPORTED_FREE_FUNCTION
  8051. parse_result TOML_CALLCONV parse(std::istream & doc, std::wstring_view source_path);
  8052. TOML_NODISCARD
  8053. TOML_EXPORTED_FREE_FUNCTION
  8054. parse_result TOML_CALLCONV parse_file(std::wstring_view file_path);
  8055. #endif // TOML_ENABLE_WINDOWS_COMPAT
  8056. #if TOML_HAS_CHAR8 && TOML_ENABLE_WINDOWS_COMPAT
  8057. TOML_NODISCARD
  8058. TOML_EXPORTED_FREE_FUNCTION
  8059. parse_result TOML_CALLCONV parse(std::u8string_view doc, std::wstring_view source_path);
  8060. #endif // TOML_HAS_CHAR8 && TOML_ENABLE_WINDOWS_COMPAT
  8061. TOML_NODISCARD
  8062. TOML_EXPORTED_FREE_FUNCTION
  8063. parse_result TOML_CALLCONV parse(std::istream & doc, std::string_view source_path = {});
  8064. TOML_NODISCARD
  8065. TOML_EXPORTED_FREE_FUNCTION
  8066. parse_result TOML_CALLCONV parse(std::istream & doc, std::string && source_path);
  8067. TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
  8068. inline namespace literals
  8069. {
  8070. TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, lit_ex, lit_noex);
  8071. TOML_NODISCARD
  8072. TOML_ALWAYS_INLINE
  8073. parse_result operator"" _toml(const char* str, size_t len)
  8074. {
  8075. return parse(std::string_view{ str, len });
  8076. }
  8077. #if TOML_HAS_CHAR8
  8078. TOML_NODISCARD
  8079. TOML_ALWAYS_INLINE
  8080. parse_result operator"" _toml(const char8_t* str, size_t len)
  8081. {
  8082. return parse(std::u8string_view{ str, len });
  8083. }
  8084. #endif // TOML_HAS_CHAR8
  8085. TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
  8086. }
  8087. }
  8088. TOML_NAMESPACE_END;
  8089. #ifdef _MSC_VER
  8090. #pragma pop_macro("min")
  8091. #pragma pop_macro("max")
  8092. #ifndef __clang__
  8093. #pragma inline_recursion(off)
  8094. #endif
  8095. #endif
  8096. TOML_POP_WARNINGS;
  8097. #endif // TOML_ENABLE_PARSER
  8098. //******** impl/formatter.hpp ****************************************************************************************
  8099. #if TOML_ENABLE_FORMATTERS
  8100. TOML_PUSH_WARNINGS;
  8101. #ifdef _MSC_VER
  8102. #ifndef __clang__
  8103. #pragma inline_recursion(on)
  8104. #endif
  8105. #pragma push_macro("min")
  8106. #pragma push_macro("max")
  8107. #undef min
  8108. #undef max
  8109. #endif
  8110. TOML_IMPL_NAMESPACE_START
  8111. {
  8112. struct formatter_constants
  8113. {
  8114. format_flags mandatory_flags;
  8115. format_flags ignored_flags;
  8116. std::string_view float_pos_inf;
  8117. std::string_view float_neg_inf;
  8118. std::string_view float_nan;
  8119. std::string_view bool_true;
  8120. std::string_view bool_false;
  8121. };
  8122. struct formatter_config
  8123. {
  8124. format_flags flags;
  8125. std::string_view indent;
  8126. };
  8127. class TOML_EXPORTED_CLASS formatter
  8128. {
  8129. private:
  8130. const node* source_;
  8131. #if TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
  8132. const parse_result* result_;
  8133. #endif
  8134. const formatter_constants* constants_;
  8135. formatter_config config_;
  8136. size_t indent_columns_;
  8137. format_flags int_format_mask_;
  8138. std::ostream* stream_; //
  8139. int indent_; // these are set in attach()
  8140. bool naked_newline_; //
  8141. protected:
  8142. TOML_PURE_INLINE_GETTER
  8143. const node& source() const noexcept
  8144. {
  8145. return *source_;
  8146. }
  8147. TOML_PURE_INLINE_GETTER
  8148. std::ostream& stream() const noexcept
  8149. {
  8150. return *stream_;
  8151. }
  8152. TOML_PURE_INLINE_GETTER
  8153. int indent() const noexcept
  8154. {
  8155. return indent_;
  8156. }
  8157. void indent(int level) noexcept
  8158. {
  8159. indent_ = level;
  8160. }
  8161. void increase_indent() noexcept
  8162. {
  8163. indent_++;
  8164. }
  8165. void decrease_indent() noexcept
  8166. {
  8167. indent_--;
  8168. }
  8169. TOML_PURE_INLINE_GETTER
  8170. size_t indent_columns() const noexcept
  8171. {
  8172. return indent_columns_;
  8173. }
  8174. TOML_PURE_INLINE_GETTER
  8175. bool indent_array_elements() const noexcept
  8176. {
  8177. return !!(config_.flags & format_flags::indent_array_elements);
  8178. }
  8179. TOML_PURE_INLINE_GETTER
  8180. bool indent_sub_tables() const noexcept
  8181. {
  8182. return !!(config_.flags & format_flags::indent_sub_tables);
  8183. }
  8184. TOML_PURE_INLINE_GETTER
  8185. bool literal_strings_allowed() const noexcept
  8186. {
  8187. return !!(config_.flags & format_flags::allow_literal_strings);
  8188. }
  8189. TOML_PURE_INLINE_GETTER
  8190. bool multi_line_strings_allowed() const noexcept
  8191. {
  8192. return !!(config_.flags & format_flags::allow_multi_line_strings);
  8193. }
  8194. TOML_PURE_INLINE_GETTER
  8195. bool real_tabs_in_strings_allowed() const noexcept
  8196. {
  8197. return !!(config_.flags & format_flags::allow_real_tabs_in_strings);
  8198. }
  8199. TOML_PURE_INLINE_GETTER
  8200. bool unicode_strings_allowed() const noexcept
  8201. {
  8202. return !!(config_.flags & format_flags::allow_unicode_strings);
  8203. }
  8204. TOML_PURE_INLINE_GETTER
  8205. bool terse_kvps() const noexcept
  8206. {
  8207. return !!(config_.flags & format_flags::terse_key_value_pairs);
  8208. }
  8209. TOML_EXPORTED_MEMBER_FUNCTION
  8210. void attach(std::ostream& stream) noexcept;
  8211. TOML_EXPORTED_MEMBER_FUNCTION
  8212. void detach() noexcept;
  8213. TOML_EXPORTED_MEMBER_FUNCTION
  8214. void print_newline(bool force = false);
  8215. TOML_EXPORTED_MEMBER_FUNCTION
  8216. void print_indent();
  8217. TOML_EXPORTED_MEMBER_FUNCTION
  8218. void print_unformatted(char);
  8219. TOML_EXPORTED_MEMBER_FUNCTION
  8220. void print_unformatted(std::string_view);
  8221. TOML_EXPORTED_MEMBER_FUNCTION
  8222. void print_string(std::string_view str,
  8223. bool allow_multi_line = true,
  8224. bool allow_bare = false,
  8225. bool allow_literal_whitespace = true);
  8226. TOML_EXPORTED_MEMBER_FUNCTION
  8227. void print(const value<std::string>&);
  8228. TOML_EXPORTED_MEMBER_FUNCTION
  8229. void print(const value<int64_t>&);
  8230. TOML_EXPORTED_MEMBER_FUNCTION
  8231. void print(const value<double>&);
  8232. TOML_EXPORTED_MEMBER_FUNCTION
  8233. void print(const value<bool>&);
  8234. TOML_EXPORTED_MEMBER_FUNCTION
  8235. void print(const value<date>&);
  8236. TOML_EXPORTED_MEMBER_FUNCTION
  8237. void print(const value<time>&);
  8238. TOML_EXPORTED_MEMBER_FUNCTION
  8239. void print(const value<date_time>&);
  8240. TOML_EXPORTED_MEMBER_FUNCTION
  8241. void print_value(const node&, node_type);
  8242. TOML_NODISCARD
  8243. TOML_EXPORTED_MEMBER_FUNCTION
  8244. bool dump_failed_parse_result();
  8245. TOML_NODISCARD_CTOR
  8246. TOML_EXPORTED_MEMBER_FUNCTION
  8247. formatter(const node*, const parse_result*, const formatter_constants&, const formatter_config&) noexcept;
  8248. };
  8249. }
  8250. TOML_IMPL_NAMESPACE_END;
  8251. #ifdef _MSC_VER
  8252. #pragma pop_macro("min")
  8253. #pragma pop_macro("max")
  8254. #ifndef __clang__
  8255. #pragma inline_recursion(off)
  8256. #endif
  8257. #endif
  8258. TOML_POP_WARNINGS;
  8259. #endif // TOML_ENABLE_FORMATTERS
  8260. //******** impl/toml_formatter.hpp ***********************************************************************************
  8261. #if TOML_ENABLE_FORMATTERS
  8262. TOML_PUSH_WARNINGS;
  8263. #ifdef _MSC_VER
  8264. #ifndef __clang__
  8265. #pragma inline_recursion(on)
  8266. #endif
  8267. #pragma push_macro("min")
  8268. #pragma push_macro("max")
  8269. #undef min
  8270. #undef max
  8271. #endif
  8272. TOML_NAMESPACE_START
  8273. {
  8274. class TOML_EXPORTED_CLASS toml_formatter : impl::formatter
  8275. {
  8276. private:
  8277. using base = impl::formatter;
  8278. std::vector<const key*> key_path_;
  8279. bool pending_table_separator_ = false;
  8280. TOML_EXPORTED_MEMBER_FUNCTION
  8281. void print_pending_table_separator();
  8282. TOML_EXPORTED_MEMBER_FUNCTION
  8283. void print(const key&);
  8284. TOML_EXPORTED_MEMBER_FUNCTION
  8285. void print_inline(const toml::table&);
  8286. TOML_EXPORTED_MEMBER_FUNCTION
  8287. void print(const toml::array&);
  8288. TOML_EXPORTED_MEMBER_FUNCTION
  8289. void print(const toml::table&);
  8290. TOML_EXPORTED_MEMBER_FUNCTION
  8291. void print();
  8292. static constexpr impl::formatter_constants constants = { format_flags::none, // mandatory
  8293. format_flags::none, // ignored
  8294. "inf"sv,
  8295. "-inf"sv,
  8296. "nan"sv,
  8297. "true"sv,
  8298. "false"sv };
  8299. public:
  8300. static constexpr format_flags default_flags = constants.mandatory_flags //
  8301. | format_flags::allow_literal_strings //
  8302. | format_flags::allow_multi_line_strings //
  8303. | format_flags::allow_unicode_strings //
  8304. | format_flags::allow_real_tabs_in_strings //
  8305. | format_flags::allow_binary_integers //
  8306. | format_flags::allow_octal_integers //
  8307. | format_flags::allow_hexadecimal_integers //
  8308. | format_flags::indentation;
  8309. TOML_NODISCARD_CTOR
  8310. explicit toml_formatter(const toml::node& source, format_flags flags = default_flags) noexcept
  8311. : base{ &source, nullptr, constants, { flags, " "sv } }
  8312. {}
  8313. #if TOML_DOXYGEN || (TOML_ENABLE_PARSER && !TOML_EXCEPTIONS)
  8314. TOML_NODISCARD_CTOR
  8315. explicit toml_formatter(const toml::parse_result& result, format_flags flags = default_flags) noexcept
  8316. : base{ nullptr, &result, constants, { flags, " "sv } }
  8317. {}
  8318. #endif
  8319. friend std::ostream& operator<<(std::ostream& lhs, toml_formatter& rhs)
  8320. {
  8321. rhs.attach(lhs);
  8322. rhs.key_path_.clear();
  8323. rhs.print();
  8324. rhs.detach();
  8325. return lhs;
  8326. }
  8327. friend std::ostream& operator<<(std::ostream& lhs, toml_formatter&& rhs)
  8328. {
  8329. return lhs << rhs; // as lvalue
  8330. }
  8331. };
  8332. }
  8333. TOML_NAMESPACE_END;
  8334. #ifdef _MSC_VER
  8335. #pragma pop_macro("min")
  8336. #pragma pop_macro("max")
  8337. #ifndef __clang__
  8338. #pragma inline_recursion(off)
  8339. #endif
  8340. #endif
  8341. TOML_POP_WARNINGS;
  8342. #endif // TOML_ENABLE_FORMATTERS
  8343. //******** impl/json_formatter.hpp ***********************************************************************************
  8344. #if TOML_ENABLE_FORMATTERS
  8345. TOML_PUSH_WARNINGS;
  8346. #ifdef _MSC_VER
  8347. #ifndef __clang__
  8348. #pragma inline_recursion(on)
  8349. #endif
  8350. #pragma push_macro("min")
  8351. #pragma push_macro("max")
  8352. #undef min
  8353. #undef max
  8354. #endif
  8355. TOML_NAMESPACE_START
  8356. {
  8357. class TOML_EXPORTED_CLASS json_formatter : impl::formatter
  8358. {
  8359. private:
  8360. using base = impl::formatter;
  8361. TOML_EXPORTED_MEMBER_FUNCTION
  8362. void print(const toml::table&);
  8363. TOML_EXPORTED_MEMBER_FUNCTION
  8364. void print(const toml::array&);
  8365. TOML_EXPORTED_MEMBER_FUNCTION
  8366. void print();
  8367. static constexpr impl::formatter_constants constants = {
  8368. format_flags::quote_dates_and_times, // mandatory
  8369. format_flags::allow_literal_strings | format_flags::allow_multi_line_strings, // ignored
  8370. "Infinity"sv,
  8371. "-Infinity"sv,
  8372. "NaN"sv,
  8373. "true"sv,
  8374. "false"sv
  8375. };
  8376. public:
  8377. static constexpr format_flags default_flags = constants.mandatory_flags //
  8378. | format_flags::quote_infinities_and_nans //
  8379. | format_flags::allow_unicode_strings //
  8380. | format_flags::indentation;
  8381. TOML_NODISCARD_CTOR
  8382. explicit json_formatter(const toml::node& source, format_flags flags = default_flags) noexcept
  8383. : base{ &source, nullptr, constants, { flags, " "sv } }
  8384. {}
  8385. #if TOML_DOXYGEN || (TOML_ENABLE_PARSER && !TOML_EXCEPTIONS)
  8386. TOML_NODISCARD_CTOR
  8387. explicit json_formatter(const toml::parse_result& result, format_flags flags = default_flags) noexcept
  8388. : base{ nullptr, &result, constants, { flags, " "sv } }
  8389. {}
  8390. #endif
  8391. friend std::ostream& operator<<(std::ostream& lhs, json_formatter& rhs)
  8392. {
  8393. rhs.attach(lhs);
  8394. rhs.print();
  8395. rhs.detach();
  8396. return lhs;
  8397. }
  8398. friend std::ostream& operator<<(std::ostream& lhs, json_formatter&& rhs)
  8399. {
  8400. return lhs << rhs; // as lvalue
  8401. }
  8402. };
  8403. }
  8404. TOML_NAMESPACE_END;
  8405. #ifdef _MSC_VER
  8406. #pragma pop_macro("min")
  8407. #pragma pop_macro("max")
  8408. #ifndef __clang__
  8409. #pragma inline_recursion(off)
  8410. #endif
  8411. #endif
  8412. TOML_POP_WARNINGS;
  8413. #endif // TOML_ENABLE_FORMATTERS
  8414. //******** impl/yaml_formatter.hpp ***********************************************************************************
  8415. #if TOML_ENABLE_FORMATTERS
  8416. TOML_PUSH_WARNINGS;
  8417. #ifdef _MSC_VER
  8418. #ifndef __clang__
  8419. #pragma inline_recursion(on)
  8420. #endif
  8421. #pragma push_macro("min")
  8422. #pragma push_macro("max")
  8423. #undef min
  8424. #undef max
  8425. #endif
  8426. TOML_NAMESPACE_START
  8427. {
  8428. class TOML_EXPORTED_CLASS yaml_formatter : impl::formatter
  8429. {
  8430. private:
  8431. using base = impl::formatter;
  8432. TOML_EXPORTED_MEMBER_FUNCTION
  8433. void print_yaml_string(const value<std::string>&);
  8434. TOML_EXPORTED_MEMBER_FUNCTION
  8435. void print(const toml::table&, bool = false);
  8436. TOML_EXPORTED_MEMBER_FUNCTION
  8437. void print(const toml::array&, bool = false);
  8438. TOML_EXPORTED_MEMBER_FUNCTION
  8439. void print();
  8440. static constexpr impl::formatter_constants constants = {
  8441. //
  8442. format_flags::quote_dates_and_times | format_flags::indentation, // mandatory
  8443. format_flags::allow_multi_line_strings, // ignored
  8444. ".inf"sv,
  8445. "-.inf"sv,
  8446. ".NAN"sv,
  8447. "true"sv,
  8448. "false"sv
  8449. };
  8450. public:
  8451. static constexpr format_flags default_flags = constants.mandatory_flags //
  8452. | format_flags::allow_literal_strings //
  8453. | format_flags::allow_unicode_strings //
  8454. | format_flags::allow_octal_integers //
  8455. | format_flags::allow_hexadecimal_integers;
  8456. TOML_NODISCARD_CTOR
  8457. explicit yaml_formatter(const toml::node& source, format_flags flags = default_flags) noexcept
  8458. : base{ &source, nullptr, constants, { flags, " "sv } }
  8459. {}
  8460. #if TOML_DOXYGEN || (TOML_ENABLE_PARSER && !TOML_EXCEPTIONS)
  8461. TOML_NODISCARD_CTOR
  8462. explicit yaml_formatter(const toml::parse_result& result, format_flags flags = default_flags) noexcept
  8463. : base{ nullptr, &result, constants, { flags, " "sv } }
  8464. {}
  8465. #endif
  8466. friend std::ostream& TOML_CALLCONV operator<<(std::ostream& lhs, yaml_formatter& rhs)
  8467. {
  8468. rhs.attach(lhs);
  8469. rhs.print();
  8470. rhs.detach();
  8471. return lhs;
  8472. }
  8473. friend std::ostream& TOML_CALLCONV operator<<(std::ostream& lhs, yaml_formatter&& rhs)
  8474. {
  8475. return lhs << rhs; // as lvalue
  8476. }
  8477. };
  8478. }
  8479. TOML_NAMESPACE_END;
  8480. #ifdef _MSC_VER
  8481. #pragma pop_macro("min")
  8482. #pragma pop_macro("max")
  8483. #ifndef __clang__
  8484. #pragma inline_recursion(off)
  8485. #endif
  8486. #endif
  8487. TOML_POP_WARNINGS;
  8488. #endif // TOML_ENABLE_FORMATTERS
  8489. #if TOML_IMPLEMENTATION
  8490. //******** impl/std_string.inl ***************************************************************************************
  8491. #if TOML_WINDOWS
  8492. #ifndef _WINDOWS_
  8493. #if TOML_INCLUDE_WINDOWS_H
  8494. #include <Windows.h>
  8495. #else
  8496. extern "C" __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int CodePage,
  8497. unsigned long dwFlags,
  8498. const wchar_t* lpWideCharStr,
  8499. int cchWideChar,
  8500. char* lpMultiByteStr,
  8501. int cbMultiByte,
  8502. const char* lpDefaultChar,
  8503. int* lpUsedDefaultChar);
  8504. extern "C" __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int CodePage,
  8505. unsigned long dwFlags,
  8506. const char* lpMultiByteStr,
  8507. int cbMultiByte,
  8508. wchar_t* lpWideCharStr,
  8509. int cchWideChar);
  8510. #endif // TOML_INCLUDE_WINDOWS_H
  8511. #endif // _WINDOWS_
  8512. TOML_PUSH_WARNINGS;
  8513. #ifdef _MSC_VER
  8514. #ifndef __clang__
  8515. #pragma inline_recursion(on)
  8516. #endif
  8517. #pragma push_macro("min")
  8518. #pragma push_macro("max")
  8519. #undef min
  8520. #undef max
  8521. #endif
  8522. TOML_IMPL_NAMESPACE_START
  8523. {
  8524. TOML_EXTERNAL_LINKAGE
  8525. std::string narrow(std::wstring_view str)
  8526. {
  8527. if (str.empty())
  8528. return {};
  8529. std::string s;
  8530. const auto len =
  8531. ::WideCharToMultiByte(65001, 0, str.data(), static_cast<int>(str.length()), nullptr, 0, nullptr, nullptr);
  8532. if (len)
  8533. {
  8534. s.resize(static_cast<size_t>(len));
  8535. ::WideCharToMultiByte(65001,
  8536. 0,
  8537. str.data(),
  8538. static_cast<int>(str.length()),
  8539. s.data(),
  8540. len,
  8541. nullptr,
  8542. nullptr);
  8543. }
  8544. return s;
  8545. }
  8546. TOML_EXTERNAL_LINKAGE
  8547. std::wstring widen(std::string_view str)
  8548. {
  8549. if (str.empty())
  8550. return {};
  8551. std::wstring s;
  8552. const auto len = ::MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), nullptr, 0);
  8553. if (len)
  8554. {
  8555. s.resize(static_cast<size_t>(len));
  8556. ::MultiByteToWideChar(65001, 0, str.data(), static_cast<int>(str.length()), s.data(), len);
  8557. }
  8558. return s;
  8559. }
  8560. #if TOML_HAS_CHAR8
  8561. TOML_EXTERNAL_LINKAGE
  8562. std::wstring widen(std::u8string_view str)
  8563. {
  8564. if (str.empty())
  8565. return {};
  8566. return widen(std::string_view{ reinterpret_cast<const char*>(str.data()), str.length() });
  8567. }
  8568. #endif // TOML_HAS_CHAR8
  8569. }
  8570. TOML_IMPL_NAMESPACE_END;
  8571. #ifdef _MSC_VER
  8572. #pragma pop_macro("min")
  8573. #pragma pop_macro("max")
  8574. #ifndef __clang__
  8575. #pragma inline_recursion(off)
  8576. #endif
  8577. #endif
  8578. TOML_POP_WARNINGS;
  8579. #endif // TOML_WINDOWS
  8580. //******** impl/print_to_stream.inl **********************************************************************************
  8581. TOML_DISABLE_WARNINGS;
  8582. #include <ostream>
  8583. #if TOML_INT_CHARCONV || TOML_FLOAT_CHARCONV
  8584. #include <charconv>
  8585. #endif
  8586. #if !TOML_INT_CHARCONV || !TOML_FLOAT_CHARCONV
  8587. #include <sstream>
  8588. #endif
  8589. #if !TOML_INT_CHARCONV
  8590. #include <iomanip>
  8591. #endif
  8592. TOML_ENABLE_WARNINGS;
  8593. TOML_PUSH_WARNINGS;
  8594. #ifdef _MSC_VER
  8595. #ifndef __clang__
  8596. #pragma inline_recursion(on)
  8597. #endif
  8598. #pragma push_macro("min")
  8599. #pragma push_macro("max")
  8600. #undef min
  8601. #undef max
  8602. #endif
  8603. TOML_ANON_NAMESPACE_START
  8604. {
  8605. template <typename T>
  8606. inline constexpr size_t charconv_buffer_length = 0;
  8607. template <>
  8608. inline constexpr size_t charconv_buffer_length<int8_t> = 4; // strlen("-128")
  8609. template <>
  8610. inline constexpr size_t charconv_buffer_length<int16_t> = 6; // strlen("-32768")
  8611. template <>
  8612. inline constexpr size_t charconv_buffer_length<int32_t> = 11; // strlen("-2147483648")
  8613. template <>
  8614. inline constexpr size_t charconv_buffer_length<int64_t> = 20; // strlen("-9223372036854775808")
  8615. template <>
  8616. inline constexpr size_t charconv_buffer_length<uint8_t> = 3; // strlen("255")
  8617. template <>
  8618. inline constexpr size_t charconv_buffer_length<uint16_t> = 5; // strlen("65535")
  8619. template <>
  8620. inline constexpr size_t charconv_buffer_length<uint32_t> = 10; // strlen("4294967295")
  8621. template <>
  8622. inline constexpr size_t charconv_buffer_length<uint64_t> = 20; // strlen("18446744073709551615")
  8623. template <>
  8624. inline constexpr size_t charconv_buffer_length<float> = 64;
  8625. template <>
  8626. inline constexpr size_t charconv_buffer_length<double> = 64;
  8627. template <typename T>
  8628. TOML_INTERNAL_LINKAGE
  8629. void print_integer_to_stream(std::ostream & stream, T val, value_flags format = {}, size_t min_digits = 0)
  8630. {
  8631. if (!val)
  8632. {
  8633. if (!min_digits)
  8634. min_digits = 1;
  8635. for (size_t i = 0; i < min_digits; i++)
  8636. stream.put('0');
  8637. return;
  8638. }
  8639. static constexpr auto value_flags_mask =
  8640. value_flags::format_as_binary | value_flags::format_as_octal | value_flags::format_as_hexadecimal;
  8641. format &= value_flags_mask;
  8642. int base = 10;
  8643. if (format != value_flags::none && val > T{})
  8644. {
  8645. switch (format)
  8646. {
  8647. case value_flags::format_as_binary: base = 2; break;
  8648. case value_flags::format_as_octal: base = 8; break;
  8649. case value_flags::format_as_hexadecimal: base = 16; break;
  8650. default: break;
  8651. }
  8652. }
  8653. #if TOML_INT_CHARCONV
  8654. char buf[(sizeof(T) * CHAR_BIT)];
  8655. const auto res = std::to_chars(buf, buf + sizeof(buf), val, base);
  8656. const auto len = static_cast<size_t>(res.ptr - buf);
  8657. for (size_t i = len; i < min_digits; i++)
  8658. stream.put('0');
  8659. if (base == 16)
  8660. {
  8661. for (size_t i = 0; i < len; i++)
  8662. if (buf[i] >= 'a')
  8663. buf[i] -= 32;
  8664. }
  8665. impl::print_to_stream(stream, buf, len);
  8666. #else
  8667. using unsigned_type = std::conditional_t<(sizeof(T) > sizeof(unsigned)), std::make_unsigned_t<T>, unsigned>;
  8668. using cast_type = std::conditional_t<std::is_signed_v<T>, std::make_signed_t<unsigned_type>, unsigned_type>;
  8669. if (base == 2)
  8670. {
  8671. const auto len = sizeof(T) * CHAR_BIT;
  8672. for (size_t i = len; i < min_digits; i++)
  8673. stream.put('0');
  8674. bool found_one = false;
  8675. const auto v = static_cast<unsigned_type>(val);
  8676. unsigned_type mask = unsigned_type{ 1 } << (len - 1u);
  8677. for (size_t i = 0; i < len; i++)
  8678. {
  8679. if ((v & mask))
  8680. {
  8681. stream.put('1');
  8682. found_one = true;
  8683. }
  8684. else if (found_one)
  8685. stream.put('0');
  8686. mask >>= 1;
  8687. }
  8688. }
  8689. else
  8690. {
  8691. std::ostringstream ss;
  8692. ss.imbue(std::locale::classic());
  8693. ss << std::uppercase << std::setbase(base);
  8694. if (min_digits)
  8695. ss << std::setfill('0') << std::setw(static_cast<int>(min_digits));
  8696. ss << static_cast<cast_type>(val);
  8697. const auto str = std::move(ss).str();
  8698. impl::print_to_stream(stream, str);
  8699. }
  8700. #endif
  8701. }
  8702. template <typename T>
  8703. TOML_INTERNAL_LINKAGE
  8704. void print_floating_point_to_stream(std::ostream & stream,
  8705. T val,
  8706. value_flags format,
  8707. [[maybe_unused]] bool relaxed_precision)
  8708. {
  8709. switch (impl::fpclassify(val))
  8710. {
  8711. case impl::fp_class::neg_inf: impl::print_to_stream(stream, "-inf"sv); break;
  8712. case impl::fp_class::pos_inf: impl::print_to_stream(stream, "inf"sv); break;
  8713. case impl::fp_class::nan: impl::print_to_stream(stream, "nan"sv); break;
  8714. case impl::fp_class::ok:
  8715. {
  8716. static constexpr auto needs_decimal_point = [](auto&& s) noexcept
  8717. {
  8718. for (auto c : s)
  8719. if (c == '.' || c == 'E' || c == 'e')
  8720. return false;
  8721. return true;
  8722. };
  8723. #if TOML_FLOAT_CHARCONV
  8724. const auto hex = !!(format & value_flags::format_as_hexadecimal);
  8725. char buf[charconv_buffer_length<T>];
  8726. auto res = hex ? std::to_chars(buf, buf + sizeof(buf), val, std::chars_format::hex)
  8727. : std::to_chars(buf, buf + sizeof(buf), val);
  8728. auto str = std::string_view{ buf, static_cast<size_t>(res.ptr - buf) };
  8729. char buf2[charconv_buffer_length<T>];
  8730. if (!hex && relaxed_precision)
  8731. {
  8732. res = std::to_chars(buf2, buf2 + sizeof(buf2), val, std::chars_format::general, 6);
  8733. const auto str2 = std::string_view{ buf2, static_cast<size_t>(res.ptr - buf2) };
  8734. if (str2.length() < str.length())
  8735. str = str2;
  8736. }
  8737. impl::print_to_stream(stream, str);
  8738. if (!hex && needs_decimal_point(str))
  8739. toml::impl::print_to_stream(stream, ".0"sv);
  8740. #else
  8741. std::ostringstream ss;
  8742. ss.imbue(std::locale::classic());
  8743. if (!relaxed_precision)
  8744. ss.precision(std::numeric_limits<T>::max_digits10);
  8745. if (!!(format & value_flags::format_as_hexadecimal))
  8746. ss << std::hexfloat;
  8747. ss << val;
  8748. const auto str = std::move(ss).str();
  8749. impl::print_to_stream(stream, str);
  8750. if (!(format & value_flags::format_as_hexadecimal) && needs_decimal_point(str))
  8751. impl::print_to_stream(stream, ".0"sv);
  8752. #endif
  8753. }
  8754. break;
  8755. default: TOML_UNREACHABLE;
  8756. }
  8757. }
  8758. }
  8759. TOML_ANON_NAMESPACE_END;
  8760. TOML_IMPL_NAMESPACE_START
  8761. {
  8762. TOML_EXTERNAL_LINKAGE
  8763. TOML_ATTR(nonnull)
  8764. void TOML_CALLCONV print_to_stream(std::ostream & stream, const char* val, size_t len)
  8765. {
  8766. stream.write(val, static_cast<std::streamsize>(len));
  8767. }
  8768. TOML_EXTERNAL_LINKAGE
  8769. void TOML_CALLCONV print_to_stream(std::ostream & stream, std::string_view val)
  8770. {
  8771. stream.write(val.data(), static_cast<std::streamsize>(val.length()));
  8772. }
  8773. TOML_EXTERNAL_LINKAGE
  8774. void TOML_CALLCONV print_to_stream(std::ostream & stream, const std::string& val)
  8775. {
  8776. stream.write(val.data(), static_cast<std::streamsize>(val.length()));
  8777. }
  8778. TOML_EXTERNAL_LINKAGE
  8779. void TOML_CALLCONV print_to_stream(std::ostream & stream, char val)
  8780. {
  8781. stream.put(val);
  8782. }
  8783. TOML_EXTERNAL_LINKAGE
  8784. void TOML_CALLCONV print_to_stream(std::ostream & stream, signed char val, value_flags format, size_t min_digits)
  8785. {
  8786. TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
  8787. }
  8788. TOML_EXTERNAL_LINKAGE
  8789. void TOML_CALLCONV print_to_stream(std::ostream & stream, signed short val, value_flags format, size_t min_digits)
  8790. {
  8791. TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
  8792. }
  8793. TOML_EXTERNAL_LINKAGE
  8794. void TOML_CALLCONV print_to_stream(std::ostream & stream, signed int val, value_flags format, size_t min_digits)
  8795. {
  8796. TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
  8797. }
  8798. TOML_EXTERNAL_LINKAGE
  8799. void TOML_CALLCONV print_to_stream(std::ostream & stream, signed long val, value_flags format, size_t min_digits)
  8800. {
  8801. TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
  8802. }
  8803. TOML_EXTERNAL_LINKAGE
  8804. void TOML_CALLCONV print_to_stream(std::ostream & stream,
  8805. signed long long val,
  8806. value_flags format,
  8807. size_t min_digits)
  8808. {
  8809. TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
  8810. }
  8811. TOML_EXTERNAL_LINKAGE
  8812. void TOML_CALLCONV print_to_stream(std::ostream & stream, unsigned char val, value_flags format, size_t min_digits)
  8813. {
  8814. TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
  8815. }
  8816. TOML_EXTERNAL_LINKAGE
  8817. void TOML_CALLCONV print_to_stream(std::ostream & stream, unsigned short val, value_flags format, size_t min_digits)
  8818. {
  8819. TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
  8820. }
  8821. TOML_EXTERNAL_LINKAGE
  8822. void TOML_CALLCONV print_to_stream(std::ostream & stream, unsigned int val, value_flags format, size_t min_digits)
  8823. {
  8824. TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
  8825. }
  8826. TOML_EXTERNAL_LINKAGE
  8827. void TOML_CALLCONV print_to_stream(std::ostream & stream, unsigned long val, value_flags format, size_t min_digits)
  8828. {
  8829. TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
  8830. }
  8831. TOML_EXTERNAL_LINKAGE
  8832. void TOML_CALLCONV print_to_stream(std::ostream & stream,
  8833. unsigned long long val,
  8834. value_flags format,
  8835. size_t min_digits)
  8836. {
  8837. TOML_ANON_NAMESPACE::print_integer_to_stream(stream, val, format, min_digits);
  8838. }
  8839. TOML_EXTERNAL_LINKAGE
  8840. void TOML_CALLCONV print_to_stream(std::ostream & stream, float val, value_flags format, bool relaxed_precision)
  8841. {
  8842. TOML_ANON_NAMESPACE::print_floating_point_to_stream(stream, val, format, relaxed_precision);
  8843. }
  8844. TOML_EXTERNAL_LINKAGE
  8845. void TOML_CALLCONV print_to_stream(std::ostream & stream, double val, value_flags format, bool relaxed_precision)
  8846. {
  8847. TOML_ANON_NAMESPACE::print_floating_point_to_stream(stream, val, format, relaxed_precision);
  8848. }
  8849. TOML_EXTERNAL_LINKAGE
  8850. void TOML_CALLCONV print_to_stream(std::ostream & stream, bool val)
  8851. {
  8852. print_to_stream(stream, val ? "true"sv : "false"sv);
  8853. }
  8854. TOML_EXTERNAL_LINKAGE
  8855. void TOML_CALLCONV print_to_stream(std::ostream & stream, const toml::date& val)
  8856. {
  8857. print_to_stream(stream, val.year, {}, 4);
  8858. stream.put('-');
  8859. print_to_stream(stream, val.month, {}, 2);
  8860. stream.put('-');
  8861. print_to_stream(stream, val.day, {}, 2);
  8862. }
  8863. TOML_EXTERNAL_LINKAGE
  8864. void TOML_CALLCONV print_to_stream(std::ostream & stream, const toml::time& val)
  8865. {
  8866. print_to_stream(stream, val.hour, {}, 2);
  8867. stream.put(':');
  8868. print_to_stream(stream, val.minute, {}, 2);
  8869. stream.put(':');
  8870. print_to_stream(stream, val.second, {}, 2);
  8871. if (val.nanosecond && val.nanosecond <= 999999999u)
  8872. {
  8873. stream.put('.');
  8874. auto ns = val.nanosecond;
  8875. size_t digits = 9u;
  8876. while (ns % 10u == 0u)
  8877. {
  8878. ns /= 10u;
  8879. digits--;
  8880. }
  8881. print_to_stream(stream, ns, {}, digits);
  8882. }
  8883. }
  8884. TOML_EXTERNAL_LINKAGE
  8885. void TOML_CALLCONV print_to_stream(std::ostream & stream, const toml::time_offset& val)
  8886. {
  8887. if (!val.minutes)
  8888. {
  8889. stream.put('Z');
  8890. return;
  8891. }
  8892. auto mins = static_cast<int>(val.minutes);
  8893. if (mins < 0)
  8894. {
  8895. stream.put('-');
  8896. mins = -mins;
  8897. }
  8898. else
  8899. stream.put('+');
  8900. const auto hours = mins / 60;
  8901. if (hours)
  8902. {
  8903. print_to_stream(stream, static_cast<unsigned int>(hours), {}, 2);
  8904. mins -= hours * 60;
  8905. }
  8906. else
  8907. print_to_stream(stream, "00"sv);
  8908. stream.put(':');
  8909. print_to_stream(stream, static_cast<unsigned int>(mins), {}, 2);
  8910. }
  8911. TOML_EXTERNAL_LINKAGE
  8912. void TOML_CALLCONV print_to_stream(std::ostream & stream, const toml::date_time& val)
  8913. {
  8914. print_to_stream(stream, val.date);
  8915. stream.put('T');
  8916. print_to_stream(stream, val.time);
  8917. if (val.offset)
  8918. print_to_stream(stream, *val.offset);
  8919. }
  8920. TOML_EXTERNAL_LINKAGE
  8921. void TOML_CALLCONV print_to_stream(std::ostream & stream, const source_position& val)
  8922. {
  8923. print_to_stream(stream, "line "sv);
  8924. print_to_stream(stream, val.line);
  8925. print_to_stream(stream, ", column "sv);
  8926. print_to_stream(stream, val.column);
  8927. }
  8928. TOML_EXTERNAL_LINKAGE
  8929. void TOML_CALLCONV print_to_stream(std::ostream & stream, const source_region& val)
  8930. {
  8931. print_to_stream(stream, val.begin);
  8932. if (val.path)
  8933. {
  8934. print_to_stream(stream, " of '"sv);
  8935. print_to_stream(stream, *val.path);
  8936. stream.put('\'');
  8937. }
  8938. }
  8939. #if TOML_ENABLE_FORMATTERS
  8940. TOML_EXTERNAL_LINKAGE
  8941. void TOML_CALLCONV print_to_stream(std::ostream & stream, const array& arr)
  8942. {
  8943. stream << toml_formatter{ arr };
  8944. }
  8945. TOML_EXTERNAL_LINKAGE
  8946. void TOML_CALLCONV print_to_stream(std::ostream & stream, const table& tbl)
  8947. {
  8948. stream << toml_formatter{ tbl };
  8949. }
  8950. TOML_EXTERNAL_LINKAGE
  8951. void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<std::string>& val)
  8952. {
  8953. stream << toml_formatter{ val };
  8954. }
  8955. TOML_EXTERNAL_LINKAGE
  8956. void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<int64_t>& val)
  8957. {
  8958. stream << toml_formatter{ val };
  8959. }
  8960. TOML_EXTERNAL_LINKAGE
  8961. void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<double>& val)
  8962. {
  8963. stream << toml_formatter{ val };
  8964. }
  8965. TOML_EXTERNAL_LINKAGE
  8966. void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<bool>& val)
  8967. {
  8968. stream << toml_formatter{ val };
  8969. }
  8970. TOML_EXTERNAL_LINKAGE
  8971. void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<date>& val)
  8972. {
  8973. stream << toml_formatter{ val };
  8974. }
  8975. TOML_EXTERNAL_LINKAGE
  8976. void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<time>& val)
  8977. {
  8978. stream << toml_formatter{ val };
  8979. }
  8980. TOML_EXTERNAL_LINKAGE
  8981. void TOML_CALLCONV print_to_stream(std::ostream & stream, const value<date_time>& val)
  8982. {
  8983. stream << toml_formatter{ val };
  8984. }
  8985. #endif
  8986. }
  8987. TOML_IMPL_NAMESPACE_END;
  8988. #ifdef _MSC_VER
  8989. #pragma pop_macro("min")
  8990. #pragma pop_macro("max")
  8991. #ifndef __clang__
  8992. #pragma inline_recursion(off)
  8993. #endif
  8994. #endif
  8995. TOML_POP_WARNINGS;
  8996. //******** impl/node.inl *********************************************************************************************
  8997. TOML_PUSH_WARNINGS;
  8998. #ifdef _MSC_VER
  8999. #ifndef __clang__
  9000. #pragma inline_recursion(on)
  9001. #endif
  9002. #pragma push_macro("min")
  9003. #pragma push_macro("max")
  9004. #undef min
  9005. #undef max
  9006. #endif
  9007. TOML_NAMESPACE_START
  9008. {
  9009. TOML_EXTERNAL_LINKAGE
  9010. node::node() noexcept = default;
  9011. TOML_EXTERNAL_LINKAGE
  9012. node::~node() noexcept = default;
  9013. TOML_EXTERNAL_LINKAGE
  9014. node::node(node && other) noexcept //
  9015. : source_{ std::exchange(other.source_, {}) }
  9016. {}
  9017. TOML_EXTERNAL_LINKAGE
  9018. node::node(const node& /*other*/) noexcept
  9019. {
  9020. // does not copy source information - this is not an error
  9021. //
  9022. // see https://github.com/marzer/tomlplusplus/issues/49#issuecomment-665089577
  9023. }
  9024. TOML_EXTERNAL_LINKAGE
  9025. node& node::operator=(const node& /*rhs*/) noexcept
  9026. {
  9027. // does not copy source information - this is not an error
  9028. //
  9029. // see https://github.com/marzer/tomlplusplus/issues/49#issuecomment-665089577
  9030. source_ = {};
  9031. return *this;
  9032. }
  9033. TOML_EXTERNAL_LINKAGE
  9034. node& node::operator=(node&& rhs) noexcept
  9035. {
  9036. if (&rhs != this)
  9037. source_ = std::exchange(rhs.source_, {});
  9038. return *this;
  9039. }
  9040. TOML_EXTERNAL_LINKAGE
  9041. node_view<node> node::at_path(std::string_view path) noexcept
  9042. {
  9043. return toml::at_path(*this, path);
  9044. }
  9045. TOML_EXTERNAL_LINKAGE
  9046. node_view<const node> node::at_path(std::string_view path) const noexcept
  9047. {
  9048. return toml::at_path(*this, path);
  9049. }
  9050. TOML_EXTERNAL_LINKAGE
  9051. node_view<node> node::at_path(const path& p) noexcept
  9052. {
  9053. return toml::at_path(*this, p);
  9054. }
  9055. TOML_EXTERNAL_LINKAGE
  9056. node_view<const node> node::at_path(const path& p) const noexcept
  9057. {
  9058. return toml::at_path(*this, p);
  9059. }
  9060. #if TOML_ENABLE_WINDOWS_COMPAT
  9061. TOML_EXTERNAL_LINKAGE
  9062. node_view<node> node::at_path(std::wstring_view path)
  9063. {
  9064. return toml::at_path(*this, path);
  9065. }
  9066. TOML_EXTERNAL_LINKAGE
  9067. node_view<const node> node::at_path(std::wstring_view path) const
  9068. {
  9069. return toml::at_path(*this, path);
  9070. }
  9071. #endif // TOML_ENABLE_WINDOWS_COMPAT
  9072. TOML_EXTERNAL_LINKAGE
  9073. node_view<node> node::operator[](const path& p) noexcept
  9074. {
  9075. return toml::at_path(*this, p);
  9076. }
  9077. TOML_EXTERNAL_LINKAGE
  9078. node_view<const node> node::operator[](const path& p) const noexcept
  9079. {
  9080. return toml::at_path(*this, p);
  9081. }
  9082. }
  9083. TOML_NAMESPACE_END;
  9084. TOML_IMPL_NAMESPACE_START
  9085. {
  9086. TOML_PURE_GETTER
  9087. TOML_EXTERNAL_LINKAGE
  9088. bool TOML_CALLCONV node_deep_equality(const node* lhs, const node* rhs) noexcept
  9089. {
  9090. // both same or both null
  9091. if (lhs == rhs)
  9092. return true;
  9093. // lhs null != rhs null or different types
  9094. if ((!lhs != !rhs) || lhs->type() != rhs->type())
  9095. return false;
  9096. return lhs->visit(
  9097. [=](auto& l) noexcept
  9098. {
  9099. using concrete_type = remove_cvref<decltype(l)>;
  9100. return l == *(rhs->as<concrete_type>());
  9101. });
  9102. }
  9103. }
  9104. TOML_IMPL_NAMESPACE_END;
  9105. #ifdef _MSC_VER
  9106. #pragma pop_macro("min")
  9107. #pragma pop_macro("max")
  9108. #ifndef __clang__
  9109. #pragma inline_recursion(off)
  9110. #endif
  9111. #endif
  9112. TOML_POP_WARNINGS;
  9113. //******** impl/at_path.inl ******************************************************************************************
  9114. TOML_DISABLE_WARNINGS;
  9115. #if TOML_INT_CHARCONV
  9116. #include <charconv>
  9117. #else
  9118. #include <sstream>
  9119. #endif
  9120. TOML_ENABLE_WARNINGS;
  9121. TOML_PUSH_WARNINGS;
  9122. #ifdef _MSC_VER
  9123. #ifndef __clang__
  9124. #pragma inline_recursion(on)
  9125. #endif
  9126. #pragma push_macro("min")
  9127. #pragma push_macro("max")
  9128. #undef min
  9129. #undef max
  9130. #endif
  9131. TOML_IMPL_NAMESPACE_START
  9132. {
  9133. TOML_EXTERNAL_LINKAGE
  9134. bool TOML_CALLCONV parse_path(const std::string_view path,
  9135. void* const data,
  9136. const parse_path_callback<std::string_view> on_key,
  9137. const parse_path_callback<size_t> on_index)
  9138. {
  9139. // a blank string is a valid path; it's just one component representing the "" key
  9140. if (path.empty())
  9141. return on_key(data, ""sv);
  9142. size_t pos = 0;
  9143. const auto end = path.length();
  9144. bool prev_was_array_indexer = false;
  9145. bool prev_was_dot = true; // invisible root 'dot'
  9146. while (pos < end)
  9147. {
  9148. // start of an array indexer
  9149. if (path[pos] == '[')
  9150. {
  9151. // find first digit in index
  9152. size_t index_start = pos + 1u;
  9153. while (true)
  9154. {
  9155. if TOML_UNLIKELY(index_start >= path.length())
  9156. return false;
  9157. const auto c = path[index_start];
  9158. if TOML_LIKELY(c >= '0' && c <= '9')
  9159. break;
  9160. else if (c == ' ' || c == '\t')
  9161. index_start++;
  9162. else
  9163. return false;
  9164. }
  9165. TOML_ASSERT(path[index_start] >= '0');
  9166. TOML_ASSERT(path[index_start] <= '9');
  9167. // find end of index (first non-digit character)
  9168. size_t index_end = index_start + 1u;
  9169. while (true)
  9170. {
  9171. // if an array indexer is missing the trailing ']' at the end of the string, permissively accept it
  9172. if TOML_UNLIKELY(index_end >= path.length())
  9173. break;
  9174. const auto c = path[index_end];
  9175. if (c >= '0' && c <= '9')
  9176. index_end++;
  9177. else if (c == ']' || c == ' ' || c == '\t' || c == '.' || c == '[')
  9178. break;
  9179. else
  9180. return false;
  9181. }
  9182. TOML_ASSERT(path[index_end - 1u] >= '0');
  9183. TOML_ASSERT(path[index_end - 1u] <= '9');
  9184. // move pos to after indexer (char after closing ']' or permissively EOL/subkey '.'/next opening '[')
  9185. pos = index_end;
  9186. while (true)
  9187. {
  9188. if TOML_UNLIKELY(pos >= path.length())
  9189. break;
  9190. const auto c = path[pos];
  9191. if (c == ']')
  9192. {
  9193. pos++;
  9194. break;
  9195. }
  9196. else if TOML_UNLIKELY(c == '.' || c == '[')
  9197. break;
  9198. else if (c == '\t' || c == ' ')
  9199. pos++;
  9200. else
  9201. return false;
  9202. }
  9203. // get array index substring
  9204. auto index_str = path.substr(index_start, index_end - index_start);
  9205. // parse the actual array index to an integer type
  9206. size_t index;
  9207. if (index_str.length() == 1u)
  9208. index = static_cast<size_t>(index_str[0] - '0');
  9209. else
  9210. {
  9211. #if TOML_INT_CHARCONV
  9212. auto fc_result = std::from_chars(index_str.data(), index_str.data() + index_str.length(), index);
  9213. if (fc_result.ec != std::errc{})
  9214. return false;
  9215. #else
  9216. std::stringstream ss;
  9217. ss.imbue(std::locale::classic());
  9218. ss.write(index_str.data(), static_cast<std::streamsize>(index_str.length()));
  9219. if (!(ss >> index))
  9220. return false;
  9221. #endif
  9222. }
  9223. prev_was_dot = false;
  9224. prev_was_array_indexer = true;
  9225. if (!on_index(data, index))
  9226. return false;
  9227. }
  9228. // start of a new table child
  9229. else if (path[pos] == '.')
  9230. {
  9231. // a dot immediately following another dot (or at the beginning of the string) is as if we'd asked
  9232. // for an empty child in between, e.g.
  9233. //
  9234. // foo..bar
  9235. //
  9236. // is equivalent to
  9237. //
  9238. // "foo".""."bar"
  9239. //
  9240. if (prev_was_dot && !on_key(data, ""sv))
  9241. return false;
  9242. pos++;
  9243. prev_was_dot = true;
  9244. prev_was_array_indexer = false;
  9245. }
  9246. // an errant closing ']'
  9247. else if TOML_UNLIKELY(path[pos] == ']')
  9248. return false;
  9249. // some regular subkey
  9250. else
  9251. {
  9252. const auto subkey_start = pos;
  9253. const auto subkey_len =
  9254. impl::min(path.find_first_of(".[]"sv, subkey_start + 1u), path.length()) - subkey_start;
  9255. const auto subkey = path.substr(subkey_start, subkey_len);
  9256. // a regular subkey segment immediately after an array indexer is OK if it was all whitespace, e.g.:
  9257. //
  9258. // "foo[0] .bar"
  9259. // ^^ skip this
  9260. //
  9261. // otherwise its an error (since it would have to be preceeded by a dot)
  9262. if (prev_was_array_indexer)
  9263. {
  9264. auto non_ws = subkey.find_first_not_of(" \t");
  9265. if (non_ws == std::string_view::npos)
  9266. {
  9267. pos += subkey_len;
  9268. prev_was_dot = false;
  9269. prev_was_array_indexer = false;
  9270. continue;
  9271. }
  9272. else
  9273. return false;
  9274. }
  9275. pos += subkey_len;
  9276. prev_was_dot = false;
  9277. prev_was_array_indexer = false;
  9278. if (!on_key(data, subkey))
  9279. return false;
  9280. }
  9281. }
  9282. // Last character was a '.', which implies an empty string key at the end of the path
  9283. if (prev_was_dot && !on_key(data, ""sv))
  9284. return false;
  9285. return true;
  9286. }
  9287. }
  9288. TOML_IMPL_NAMESPACE_END;
  9289. TOML_NAMESPACE_START
  9290. {
  9291. TOML_EXTERNAL_LINKAGE
  9292. node_view<node> TOML_CALLCONV at_path(node & root, std::string_view path) noexcept
  9293. {
  9294. // early-exit sanity-checks
  9295. if (root.is_value())
  9296. return {};
  9297. if (auto tbl = root.as_table(); tbl && tbl->empty())
  9298. return {};
  9299. if (auto arr = root.as_array(); arr && arr->empty())
  9300. return {};
  9301. node* current = &root;
  9302. static constexpr auto on_key = [](void* data, std::string_view key) noexcept -> bool
  9303. {
  9304. auto& curr = *static_cast<node**>(data);
  9305. TOML_ASSERT_ASSUME(curr);
  9306. const auto current_table = curr->as<table>();
  9307. if (!current_table)
  9308. return false;
  9309. curr = current_table->get(key);
  9310. return curr != nullptr;
  9311. };
  9312. static constexpr auto on_index = [](void* data, size_t index) noexcept -> bool
  9313. {
  9314. auto& curr = *static_cast<node**>(data);
  9315. TOML_ASSERT_ASSUME(curr);
  9316. const auto current_array = curr->as<array>();
  9317. if (!current_array)
  9318. return false;
  9319. curr = current_array->get(index);
  9320. return curr != nullptr;
  9321. };
  9322. if (!impl::parse_path(path, &current, on_key, on_index))
  9323. current = nullptr;
  9324. return node_view{ current };
  9325. }
  9326. TOML_EXTERNAL_LINKAGE
  9327. node_view<const node> TOML_CALLCONV at_path(const node& root, std::string_view path) noexcept
  9328. {
  9329. return node_view<const node>{ at_path(const_cast<node&>(root), path).node() };
  9330. }
  9331. #if TOML_ENABLE_WINDOWS_COMPAT
  9332. TOML_EXTERNAL_LINKAGE
  9333. node_view<node> TOML_CALLCONV at_path(node & root, std::wstring_view path)
  9334. {
  9335. // these are the same top-level checks from the narrow-string version;
  9336. // they're hoisted up here to avoid doing the wide -> narrow conversion where it would not be necessary
  9337. // (avoids an allocation)
  9338. if (root.is_value())
  9339. return {};
  9340. if (auto tbl = root.as_table(); tbl && tbl->empty())
  9341. return {};
  9342. if (auto arr = root.as_array(); arr && arr->empty())
  9343. return {};
  9344. return at_path(root, impl::narrow(path));
  9345. }
  9346. TOML_EXTERNAL_LINKAGE
  9347. node_view<const node> TOML_CALLCONV at_path(const node& root, std::wstring_view path)
  9348. {
  9349. return node_view<const node>{ at_path(const_cast<node&>(root), path).node() };
  9350. }
  9351. #endif // TOML_ENABLE_WINDOWS_COMPAT
  9352. }
  9353. TOML_NAMESPACE_END;
  9354. #ifdef _MSC_VER
  9355. #pragma pop_macro("min")
  9356. #pragma pop_macro("max")
  9357. #ifndef __clang__
  9358. #pragma inline_recursion(off)
  9359. #endif
  9360. #endif
  9361. TOML_POP_WARNINGS;
  9362. //******** impl/path.inl *********************************************************************************************
  9363. TOML_DISABLE_WARNINGS;
  9364. #if TOML_INT_CHARCONV
  9365. #include <charconv>
  9366. #endif
  9367. #include <sstream>
  9368. TOML_ENABLE_WARNINGS;
  9369. TOML_PUSH_WARNINGS;
  9370. #ifdef _MSC_VER
  9371. #ifndef __clang__
  9372. #pragma inline_recursion(on)
  9373. #endif
  9374. #pragma push_macro("min")
  9375. #pragma push_macro("max")
  9376. #undef min
  9377. #undef max
  9378. #endif
  9379. TOML_NAMESPACE_START
  9380. {
  9381. TOML_EXTERNAL_LINKAGE
  9382. path_component::path_component() //
  9383. : type_{ path_component_type::key }
  9384. {
  9385. store_key("", value_storage_);
  9386. }
  9387. TOML_EXTERNAL_LINKAGE
  9388. path_component::path_component(size_t index) noexcept //
  9389. : type_(path_component_type::array_index)
  9390. {
  9391. store_index(index, value_storage_);
  9392. }
  9393. TOML_EXTERNAL_LINKAGE
  9394. path_component::path_component(std::string_view key) //
  9395. : type_(path_component_type::key)
  9396. {
  9397. store_key(key, value_storage_);
  9398. }
  9399. #if TOML_ENABLE_WINDOWS_COMPAT
  9400. TOML_EXTERNAL_LINKAGE
  9401. path_component::path_component(std::wstring_view key) //
  9402. : path_component(impl::narrow(key))
  9403. {}
  9404. #endif
  9405. TOML_EXTERNAL_LINKAGE
  9406. path_component::path_component(const path_component& pc) //
  9407. : type_{ pc.type_ }
  9408. {
  9409. if (type_ == path_component_type::array_index)
  9410. store_index(pc.index(), value_storage_);
  9411. else
  9412. store_key(pc.key(), value_storage_);
  9413. }
  9414. TOML_EXTERNAL_LINKAGE
  9415. path_component::path_component(path_component && pc) noexcept //
  9416. : type_{ pc.type_ }
  9417. {
  9418. if (type_ == path_component_type::array_index)
  9419. store_index(pc.index_ref(), value_storage_);
  9420. else
  9421. store_key(std::move(pc.key_ref()), value_storage_);
  9422. }
  9423. TOML_EXTERNAL_LINKAGE
  9424. path_component& path_component::operator=(const path_component& rhs)
  9425. {
  9426. if (type_ != rhs.type_)
  9427. {
  9428. destroy();
  9429. type_ = rhs.type_;
  9430. if (type_ == path_component_type::array_index)
  9431. store_index(rhs.index(), value_storage_);
  9432. else
  9433. store_key(rhs.key(), value_storage_);
  9434. }
  9435. else
  9436. {
  9437. if (type_ == path_component_type::array_index)
  9438. index_ref() = rhs.index();
  9439. else
  9440. key_ref() = rhs.key();
  9441. }
  9442. return *this;
  9443. }
  9444. TOML_EXTERNAL_LINKAGE
  9445. path_component& path_component::operator=(path_component&& rhs) noexcept
  9446. {
  9447. if (type_ != rhs.type_)
  9448. {
  9449. destroy();
  9450. type_ = rhs.type_;
  9451. if (type_ == path_component_type::array_index)
  9452. store_index(rhs.index(), value_storage_);
  9453. else
  9454. store_key(std::move(rhs.key_ref()), value_storage_);
  9455. }
  9456. else
  9457. {
  9458. if (type_ == path_component_type::array_index)
  9459. index_ref() = rhs.index();
  9460. else
  9461. key_ref() = std::move(rhs.key_ref());
  9462. }
  9463. return *this;
  9464. }
  9465. TOML_PURE_GETTER
  9466. TOML_EXTERNAL_LINKAGE
  9467. bool TOML_CALLCONV path_component::equal(const path_component& lhs, const path_component& rhs) noexcept
  9468. {
  9469. // Different comparison depending on contents
  9470. if (lhs.type_ != rhs.type_)
  9471. return false;
  9472. if (lhs.type_ == path_component_type::array_index)
  9473. return lhs.index() == rhs.index();
  9474. else // path_component_type::key
  9475. return lhs.key() == rhs.key();
  9476. }
  9477. TOML_EXTERNAL_LINKAGE
  9478. path_component& path_component::operator=(size_t new_index) noexcept
  9479. {
  9480. // If currently a key, string will need to be destroyed regardless
  9481. destroy();
  9482. type_ = path_component_type::array_index;
  9483. store_index(new_index, value_storage_);
  9484. return *this;
  9485. }
  9486. TOML_EXTERNAL_LINKAGE
  9487. path_component& path_component::operator=(std::string_view new_key)
  9488. {
  9489. if (type_ == path_component_type::key)
  9490. key_ref() = new_key;
  9491. else
  9492. {
  9493. type_ = path_component_type::key;
  9494. store_key(new_key, value_storage_);
  9495. }
  9496. return *this;
  9497. }
  9498. #if TOML_ENABLE_WINDOWS_COMPAT
  9499. TOML_EXTERNAL_LINKAGE
  9500. path_component& path_component::operator=(std::wstring_view new_key)
  9501. {
  9502. if (type_ == path_component_type::key)
  9503. key_ref() = impl::narrow(new_key);
  9504. else
  9505. {
  9506. type_ = path_component_type::key;
  9507. store_key(impl::narrow(new_key), value_storage_);
  9508. }
  9509. return *this;
  9510. }
  9511. #endif
  9512. }
  9513. TOML_NAMESPACE_END;
  9514. TOML_ANON_NAMESPACE_START
  9515. {
  9516. TOML_INTERNAL_LINKAGE
  9517. bool parse_path_into(std::string_view path_str, std::vector<path_component> & components)
  9518. {
  9519. using components_type = std::remove_reference_t<decltype(components)>;
  9520. const auto original_size = components.size();
  9521. static constexpr auto on_key = [](void* data, std::string_view key) -> bool
  9522. {
  9523. auto& comps = *static_cast<components_type*>(data);
  9524. comps.emplace_back(key);
  9525. return true;
  9526. };
  9527. static constexpr auto on_index = [](void* data, size_t index) -> bool
  9528. {
  9529. auto& comps = *static_cast<components_type*>(data);
  9530. comps.emplace_back(index);
  9531. return true;
  9532. };
  9533. if (!impl::parse_path(path_str, &components, on_key, on_index))
  9534. {
  9535. components.resize(original_size);
  9536. return false;
  9537. }
  9538. return true;
  9539. }
  9540. }
  9541. TOML_ANON_NAMESPACE_END;
  9542. TOML_NAMESPACE_START
  9543. {
  9544. TOML_EXTERNAL_LINKAGE
  9545. void path::print_to(std::ostream & os) const
  9546. {
  9547. bool root = true;
  9548. for (const auto& component : components_)
  9549. {
  9550. if (component.type() == path_component_type::key) // key
  9551. {
  9552. if (!root)
  9553. impl::print_to_stream(os, '.');
  9554. impl::print_to_stream(os, component.key());
  9555. }
  9556. else if (component.type() == path_component_type::array_index) // array
  9557. {
  9558. impl::print_to_stream(os, '[');
  9559. impl::print_to_stream(os, component.index());
  9560. impl::print_to_stream(os, ']');
  9561. }
  9562. root = false;
  9563. }
  9564. }
  9565. TOML_PURE_GETTER
  9566. TOML_EXTERNAL_LINKAGE
  9567. bool TOML_CALLCONV path::equal(const path& lhs, const path& rhs) noexcept
  9568. {
  9569. return lhs.components_ == rhs.components_;
  9570. }
  9571. TOML_EXTERNAL_LINKAGE
  9572. path::path(std::string_view str) //
  9573. {
  9574. TOML_ANON_NAMESPACE::parse_path_into(str, components_);
  9575. }
  9576. #if TOML_ENABLE_WINDOWS_COMPAT
  9577. TOML_EXTERNAL_LINKAGE
  9578. path::path(std::wstring_view str) //
  9579. : path(impl::narrow(str))
  9580. {}
  9581. #endif
  9582. TOML_EXTERNAL_LINKAGE
  9583. path& path::operator=(std::string_view rhs)
  9584. {
  9585. components_.clear();
  9586. TOML_ANON_NAMESPACE::parse_path_into(rhs, components_);
  9587. return *this;
  9588. }
  9589. #if TOML_ENABLE_WINDOWS_COMPAT
  9590. TOML_EXTERNAL_LINKAGE
  9591. path& path::operator=(std::wstring_view rhs)
  9592. {
  9593. return assign(impl::narrow(rhs));
  9594. }
  9595. #endif
  9596. TOML_EXTERNAL_LINKAGE
  9597. path& path::operator+=(const path& rhs)
  9598. {
  9599. components_.insert(components_.cend(), rhs.begin(), rhs.end());
  9600. return *this;
  9601. }
  9602. TOML_EXTERNAL_LINKAGE
  9603. path& path::operator+=(path&& rhs)
  9604. {
  9605. components_.insert(components_.end(),
  9606. std::make_move_iterator(rhs.components_.begin()),
  9607. std::make_move_iterator(rhs.components_.end()));
  9608. return *this;
  9609. }
  9610. TOML_EXTERNAL_LINKAGE
  9611. path& path::operator+=(std::string_view str)
  9612. {
  9613. TOML_ANON_NAMESPACE::parse_path_into(str, components_);
  9614. return *this;
  9615. }
  9616. #if TOML_ENABLE_WINDOWS_COMPAT
  9617. TOML_EXTERNAL_LINKAGE
  9618. path& path::operator+=(std::wstring_view str)
  9619. {
  9620. return *this += impl::narrow(str);
  9621. }
  9622. #endif
  9623. TOML_EXTERNAL_LINKAGE
  9624. path& path::prepend(const path& source)
  9625. {
  9626. components_.insert(components_.begin(), source.components_.begin(), source.components_.end());
  9627. return *this;
  9628. }
  9629. TOML_EXTERNAL_LINKAGE
  9630. path& path::prepend(path && source)
  9631. {
  9632. components_.insert(components_.begin(),
  9633. std::make_move_iterator(source.components_.begin()),
  9634. std::make_move_iterator(source.components_.end()));
  9635. return *this;
  9636. }
  9637. TOML_EXTERNAL_LINKAGE
  9638. path& path::prepend(std::string_view source)
  9639. {
  9640. return prepend(path{ source });
  9641. }
  9642. #if TOML_ENABLE_WINDOWS_COMPAT
  9643. TOML_EXTERNAL_LINKAGE
  9644. path& path::prepend(std::wstring_view source)
  9645. {
  9646. return prepend(impl::narrow(source));
  9647. }
  9648. #endif
  9649. TOML_EXTERNAL_LINKAGE
  9650. std::string path::str() const
  9651. {
  9652. if (empty())
  9653. return "";
  9654. std::ostringstream ss;
  9655. print_to(ss);
  9656. return std::move(ss).str();
  9657. }
  9658. #if TOML_ENABLE_WINDOWS_COMPAT
  9659. TOML_EXTERNAL_LINKAGE
  9660. std::wstring path::wide_str() const
  9661. {
  9662. return impl::widen(str());
  9663. }
  9664. #endif
  9665. TOML_EXTERNAL_LINKAGE
  9666. void path::clear() noexcept
  9667. {
  9668. components_.clear();
  9669. }
  9670. TOML_EXTERNAL_LINKAGE
  9671. path& path::truncate(size_t n)
  9672. {
  9673. n = n > components_.size() ? components_.size() : n;
  9674. auto it_end = components_.end();
  9675. components_.erase(it_end - static_cast<int>(n), it_end);
  9676. return *this;
  9677. }
  9678. TOML_EXTERNAL_LINKAGE
  9679. path path::truncated(size_t n) const
  9680. {
  9681. path truncated_path{};
  9682. n = n > components_.size() ? components_.size() : n;
  9683. // Copy all components except one
  9684. // Need at least two path components to have a parent, since if there is
  9685. // only one path component, the parent is the root/null path ""
  9686. truncated_path.components_.insert(truncated_path.components_.begin(),
  9687. components_.begin(),
  9688. components_.end() - static_cast<int>(n));
  9689. return truncated_path;
  9690. }
  9691. TOML_EXTERNAL_LINKAGE
  9692. path path::parent() const
  9693. {
  9694. return truncated(1);
  9695. }
  9696. TOML_EXTERNAL_LINKAGE
  9697. path path::leaf(size_t n) const
  9698. {
  9699. path leaf_path{};
  9700. n = n > components_.size() ? components_.size() : n;
  9701. if (n > 0)
  9702. {
  9703. leaf_path.components_.insert(leaf_path.components_.begin(),
  9704. components_.end() - static_cast<int>(n),
  9705. components_.end());
  9706. }
  9707. return leaf_path;
  9708. }
  9709. TOML_EXTERNAL_LINKAGE
  9710. path path::subpath(std::vector<path_component>::const_iterator start,
  9711. std::vector<path_component>::const_iterator end) const
  9712. {
  9713. if (start >= end)
  9714. return {};
  9715. path subpath;
  9716. subpath.components_.insert(subpath.components_.begin(), start, end);
  9717. return subpath;
  9718. }
  9719. TOML_EXTERNAL_LINKAGE
  9720. path path::subpath(size_t start, size_t length) const
  9721. {
  9722. return subpath(begin() + static_cast<int>(start), begin() + static_cast<int>(start + length));
  9723. }
  9724. }
  9725. TOML_NAMESPACE_END;
  9726. TOML_NAMESPACE_START
  9727. {
  9728. TOML_EXTERNAL_LINKAGE
  9729. node_view<node> TOML_CALLCONV at_path(node & root, const toml::path& path) noexcept
  9730. {
  9731. // early-exit sanity-checks
  9732. if (root.is_value())
  9733. return {};
  9734. if (auto tbl = root.as_table(); tbl && tbl->empty())
  9735. return {};
  9736. if (auto arr = root.as_array(); arr && arr->empty())
  9737. return {};
  9738. node* current = &root;
  9739. for (const auto& component : path)
  9740. {
  9741. auto type = component.type();
  9742. if (type == path_component_type::array_index)
  9743. {
  9744. const auto current_array = current->as<array>();
  9745. if (!current_array)
  9746. return {}; // not an array, using array index doesn't work
  9747. current = current_array->get(component.index());
  9748. }
  9749. else if (type == path_component_type::key)
  9750. {
  9751. const auto current_table = current->as<table>();
  9752. if (!current_table)
  9753. return {};
  9754. current = current_table->get(component.key());
  9755. }
  9756. else
  9757. {
  9758. // Error: invalid component
  9759. return {};
  9760. }
  9761. if (!current)
  9762. return {}; // not found
  9763. }
  9764. return node_view{ current };
  9765. }
  9766. TOML_EXTERNAL_LINKAGE
  9767. node_view<const node> TOML_CALLCONV at_path(const node& root, const toml::path& path) noexcept
  9768. {
  9769. return node_view<const node>{ at_path(const_cast<node&>(root), path).node() };
  9770. }
  9771. }
  9772. TOML_NAMESPACE_END;
  9773. #ifdef _MSC_VER
  9774. #pragma pop_macro("min")
  9775. #pragma pop_macro("max")
  9776. #ifndef __clang__
  9777. #pragma inline_recursion(off)
  9778. #endif
  9779. #endif
  9780. TOML_POP_WARNINGS;
  9781. //******** impl/array.inl ********************************************************************************************
  9782. TOML_PUSH_WARNINGS;
  9783. #ifdef _MSC_VER
  9784. #ifndef __clang__
  9785. #pragma inline_recursion(on)
  9786. #endif
  9787. #pragma push_macro("min")
  9788. #pragma push_macro("max")
  9789. #undef min
  9790. #undef max
  9791. #endif
  9792. TOML_NAMESPACE_START
  9793. {
  9794. TOML_EXTERNAL_LINKAGE
  9795. array::array() noexcept
  9796. {
  9797. #if TOML_LIFETIME_HOOKS
  9798. TOML_ARRAY_CREATED;
  9799. #endif
  9800. }
  9801. TOML_EXTERNAL_LINKAGE
  9802. array::~array() noexcept
  9803. {
  9804. #if TOML_LIFETIME_HOOKS
  9805. TOML_ARRAY_DESTROYED;
  9806. #endif
  9807. }
  9808. TOML_EXTERNAL_LINKAGE
  9809. array::array(const impl::array_init_elem* b, const impl::array_init_elem* e)
  9810. {
  9811. #if TOML_LIFETIME_HOOKS
  9812. TOML_ARRAY_CREATED;
  9813. #endif
  9814. TOML_ASSERT_ASSUME(b);
  9815. TOML_ASSERT_ASSUME(e);
  9816. TOML_ASSERT_ASSUME(b <= e);
  9817. if TOML_UNLIKELY(b == e)
  9818. return;
  9819. size_t cap{};
  9820. for (auto it = b; it != e; it++)
  9821. {
  9822. if (it->value)
  9823. cap++;
  9824. }
  9825. if TOML_UNLIKELY(!cap)
  9826. return;
  9827. elems_.reserve(cap);
  9828. for (; b != e; b++)
  9829. {
  9830. if (b->value)
  9831. elems_.push_back(std::move(b->value));
  9832. }
  9833. }
  9834. TOML_EXTERNAL_LINKAGE
  9835. array::array(const array& other) //
  9836. : node(other)
  9837. {
  9838. elems_.reserve(other.elems_.size());
  9839. for (const auto& elem : other)
  9840. elems_.emplace_back(impl::make_node(elem));
  9841. #if TOML_LIFETIME_HOOKS
  9842. TOML_ARRAY_CREATED;
  9843. #endif
  9844. }
  9845. TOML_EXTERNAL_LINKAGE
  9846. array::array(array && other) noexcept //
  9847. : node(std::move(other)),
  9848. elems_(std::move(other.elems_))
  9849. {
  9850. #if TOML_LIFETIME_HOOKS
  9851. TOML_ARRAY_CREATED;
  9852. #endif
  9853. }
  9854. TOML_EXTERNAL_LINKAGE
  9855. array& array::operator=(const array& rhs)
  9856. {
  9857. if (&rhs != this)
  9858. {
  9859. node::operator=(rhs);
  9860. elems_.clear();
  9861. elems_.reserve(rhs.elems_.size());
  9862. for (const auto& elem : rhs)
  9863. elems_.emplace_back(impl::make_node(elem));
  9864. }
  9865. return *this;
  9866. }
  9867. TOML_EXTERNAL_LINKAGE
  9868. array& array::operator=(array&& rhs) noexcept
  9869. {
  9870. if (&rhs != this)
  9871. {
  9872. node::operator=(std::move(rhs));
  9873. elems_ = std::move(rhs.elems_);
  9874. }
  9875. return *this;
  9876. }
  9877. TOML_EXTERNAL_LINKAGE
  9878. void array::preinsertion_resize(size_t idx, size_t count)
  9879. {
  9880. TOML_ASSERT(idx <= elems_.size());
  9881. TOML_ASSERT_ASSUME(count >= 1u);
  9882. const auto old_size = elems_.size();
  9883. const auto new_size = old_size + count;
  9884. const auto inserting_at_end = idx == old_size;
  9885. elems_.resize(new_size);
  9886. if (!inserting_at_end)
  9887. {
  9888. for (size_t left = old_size, right = new_size - 1u; left-- > idx; right--)
  9889. elems_[right] = std::move(elems_[left]);
  9890. }
  9891. }
  9892. TOML_EXTERNAL_LINKAGE
  9893. void array::insert_at_back(impl::node_ptr && elem)
  9894. {
  9895. TOML_ASSERT(elem);
  9896. elems_.push_back(std::move(elem));
  9897. }
  9898. TOML_EXTERNAL_LINKAGE
  9899. array::vector_iterator array::insert_at(const_vector_iterator pos, impl::node_ptr && elem)
  9900. {
  9901. return elems_.insert(pos, std::move(elem));
  9902. }
  9903. TOML_PURE_GETTER
  9904. TOML_EXTERNAL_LINKAGE
  9905. bool array::is_homogeneous(node_type ntype) const noexcept
  9906. {
  9907. if (elems_.empty())
  9908. return false;
  9909. if (ntype == node_type::none)
  9910. ntype = elems_[0]->type();
  9911. for (const auto& val : elems_)
  9912. if (val->type() != ntype)
  9913. return false;
  9914. return true;
  9915. }
  9916. TOML_PURE_GETTER
  9917. TOML_EXTERNAL_LINKAGE
  9918. bool array::is_homogeneous(node_type ntype, node * &first_nonmatch) noexcept
  9919. {
  9920. if (elems_.empty())
  9921. {
  9922. first_nonmatch = {};
  9923. return false;
  9924. }
  9925. if (ntype == node_type::none)
  9926. ntype = elems_[0]->type();
  9927. for (const auto& val : elems_)
  9928. {
  9929. if (val->type() != ntype)
  9930. {
  9931. first_nonmatch = val.get();
  9932. return false;
  9933. }
  9934. }
  9935. return true;
  9936. }
  9937. TOML_PURE_GETTER
  9938. TOML_EXTERNAL_LINKAGE
  9939. bool array::is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept
  9940. {
  9941. node* fnm = nullptr;
  9942. const auto result = const_cast<array&>(*this).is_homogeneous(ntype, fnm);
  9943. first_nonmatch = fnm;
  9944. return result;
  9945. }
  9946. TOML_EXTERNAL_LINKAGE
  9947. node& array::at(size_t index)
  9948. {
  9949. #if TOML_COMPILER_HAS_EXCEPTIONS
  9950. return *elems_.at(index);
  9951. #else
  9952. auto n = get(index);
  9953. TOML_ASSERT_ASSUME(n && "element index not found in array!");
  9954. return *n;
  9955. #endif
  9956. }
  9957. TOML_EXTERNAL_LINKAGE
  9958. void array::reserve(size_t new_capacity)
  9959. {
  9960. elems_.reserve(new_capacity);
  9961. }
  9962. TOML_EXTERNAL_LINKAGE
  9963. void array::shrink_to_fit()
  9964. {
  9965. elems_.shrink_to_fit();
  9966. }
  9967. TOML_EXTERNAL_LINKAGE
  9968. void array::truncate(size_t new_size)
  9969. {
  9970. if (new_size < elems_.size())
  9971. elems_.resize(new_size);
  9972. }
  9973. TOML_EXTERNAL_LINKAGE
  9974. array::iterator array::erase(const_iterator pos) noexcept
  9975. {
  9976. return iterator{ elems_.erase(const_vector_iterator{ pos }) };
  9977. }
  9978. TOML_EXTERNAL_LINKAGE
  9979. array::iterator array::erase(const_iterator first, const_iterator last) noexcept
  9980. {
  9981. return iterator{ elems_.erase(const_vector_iterator{ first }, const_vector_iterator{ last }) };
  9982. }
  9983. TOML_EXTERNAL_LINKAGE
  9984. size_t array::total_leaf_count() const noexcept
  9985. {
  9986. size_t leaves{};
  9987. for (size_t i = 0, e = elems_.size(); i < e; i++)
  9988. {
  9989. auto arr = elems_[i]->as_array();
  9990. leaves += arr ? arr->total_leaf_count() : size_t{ 1 };
  9991. }
  9992. return leaves;
  9993. }
  9994. TOML_EXTERNAL_LINKAGE
  9995. void array::flatten_child(array && child, size_t & dest_index) noexcept
  9996. {
  9997. for (size_t i = 0, e = child.size(); i < e; i++)
  9998. {
  9999. auto type = child.elems_[i]->type();
  10000. if (type == node_type::array)
  10001. {
  10002. array& arr = *reinterpret_cast<array*>(child.elems_[i].get());
  10003. if (!arr.empty())
  10004. flatten_child(std::move(arr), dest_index);
  10005. }
  10006. else
  10007. elems_[dest_index++] = std::move(child.elems_[i]);
  10008. }
  10009. }
  10010. TOML_EXTERNAL_LINKAGE
  10011. array& array::flatten()&
  10012. {
  10013. if (elems_.empty())
  10014. return *this;
  10015. bool requires_flattening = false;
  10016. size_t size_after_flattening = elems_.size();
  10017. for (size_t i = elems_.size(); i-- > 0u;)
  10018. {
  10019. auto arr = elems_[i]->as_array();
  10020. if (!arr)
  10021. continue;
  10022. size_after_flattening--; // discount the array itself
  10023. const auto leaf_count = arr->total_leaf_count();
  10024. if (leaf_count > 0u)
  10025. {
  10026. requires_flattening = true;
  10027. size_after_flattening += leaf_count;
  10028. }
  10029. else
  10030. elems_.erase(elems_.cbegin() + static_cast<ptrdiff_t>(i));
  10031. }
  10032. if (!requires_flattening)
  10033. return *this;
  10034. elems_.reserve(size_after_flattening);
  10035. size_t i = 0;
  10036. while (i < elems_.size())
  10037. {
  10038. auto arr = elems_[i]->as_array();
  10039. if (!arr)
  10040. {
  10041. i++;
  10042. continue;
  10043. }
  10044. impl::node_ptr arr_storage = std::move(elems_[i]);
  10045. const auto leaf_count = arr->total_leaf_count();
  10046. if (leaf_count > 1u)
  10047. preinsertion_resize(i + 1u, leaf_count - 1u);
  10048. flatten_child(std::move(*arr), i); // increments i
  10049. }
  10050. return *this;
  10051. }
  10052. TOML_EXTERNAL_LINKAGE
  10053. array& array::prune(bool recursive)& noexcept
  10054. {
  10055. if (elems_.empty())
  10056. return *this;
  10057. for (size_t i = elems_.size(); i-- > 0u;)
  10058. {
  10059. if (auto arr = elems_[i]->as_array())
  10060. {
  10061. if (recursive)
  10062. arr->prune(true);
  10063. if (arr->empty())
  10064. elems_.erase(elems_.cbegin() + static_cast<ptrdiff_t>(i));
  10065. }
  10066. else if (auto tbl = elems_[i]->as_table())
  10067. {
  10068. if (recursive)
  10069. tbl->prune(true);
  10070. if (tbl->empty())
  10071. elems_.erase(elems_.cbegin() + static_cast<ptrdiff_t>(i));
  10072. }
  10073. }
  10074. return *this;
  10075. }
  10076. TOML_EXTERNAL_LINKAGE
  10077. void array::pop_back() noexcept
  10078. {
  10079. elems_.pop_back();
  10080. }
  10081. TOML_EXTERNAL_LINKAGE
  10082. void array::clear() noexcept
  10083. {
  10084. elems_.clear();
  10085. }
  10086. TOML_EXTERNAL_LINKAGE
  10087. bool TOML_CALLCONV array::equal(const array& lhs, const array& rhs) noexcept
  10088. {
  10089. if (&lhs == &rhs)
  10090. return true;
  10091. if (lhs.elems_.size() != rhs.elems_.size())
  10092. return false;
  10093. for (size_t i = 0, e = lhs.elems_.size(); i < e; i++)
  10094. {
  10095. const auto lhs_type = lhs.elems_[i]->type();
  10096. const node& rhs_ = *rhs.elems_[i];
  10097. const auto rhs_type = rhs_.type();
  10098. if (lhs_type != rhs_type)
  10099. return false;
  10100. const bool equal = lhs.elems_[i]->visit(
  10101. [&](const auto& lhs_) noexcept
  10102. { return lhs_ == *reinterpret_cast<std::remove_reference_t<decltype(lhs_)>*>(&rhs_); });
  10103. if (!equal)
  10104. return false;
  10105. }
  10106. return true;
  10107. }
  10108. }
  10109. TOML_NAMESPACE_END;
  10110. #ifdef _MSC_VER
  10111. #pragma pop_macro("min")
  10112. #pragma pop_macro("max")
  10113. #ifndef __clang__
  10114. #pragma inline_recursion(off)
  10115. #endif
  10116. #endif
  10117. TOML_POP_WARNINGS;
  10118. //******** impl/table.inl ********************************************************************************************
  10119. TOML_PUSH_WARNINGS;
  10120. #ifdef _MSC_VER
  10121. #ifndef __clang__
  10122. #pragma inline_recursion(on)
  10123. #endif
  10124. #pragma push_macro("min")
  10125. #pragma push_macro("max")
  10126. #undef min
  10127. #undef max
  10128. #endif
  10129. TOML_NAMESPACE_START
  10130. {
  10131. TOML_EXTERNAL_LINKAGE
  10132. table::table() noexcept
  10133. {
  10134. #if TOML_LIFETIME_HOOKS
  10135. TOML_TABLE_CREATED;
  10136. #endif
  10137. }
  10138. TOML_EXTERNAL_LINKAGE
  10139. table::~table() noexcept
  10140. {
  10141. #if TOML_LIFETIME_HOOKS
  10142. TOML_TABLE_DESTROYED;
  10143. #endif
  10144. }
  10145. TOML_EXTERNAL_LINKAGE
  10146. table::table(const impl::table_init_pair* b, const impl::table_init_pair* e)
  10147. {
  10148. #if TOML_LIFETIME_HOOKS
  10149. TOML_TABLE_CREATED;
  10150. #endif
  10151. TOML_ASSERT_ASSUME(b);
  10152. TOML_ASSERT_ASSUME(e);
  10153. TOML_ASSERT_ASSUME(b <= e);
  10154. if TOML_UNLIKELY(b == e)
  10155. return;
  10156. for (; b != e; b++)
  10157. {
  10158. if (!b->value) // empty node_views
  10159. continue;
  10160. map_.insert_or_assign(std::move(b->key), std::move(b->value));
  10161. }
  10162. }
  10163. TOML_EXTERNAL_LINKAGE
  10164. table::table(const table& other) //
  10165. : node(other),
  10166. inline_{ other.inline_ }
  10167. {
  10168. for (auto&& [k, v] : other.map_)
  10169. map_.emplace_hint(map_.end(), k, impl::make_node(*v));
  10170. #if TOML_LIFETIME_HOOKS
  10171. TOML_TABLE_CREATED;
  10172. #endif
  10173. }
  10174. TOML_EXTERNAL_LINKAGE
  10175. table::table(table && other) noexcept //
  10176. : node(std::move(other)),
  10177. map_{ std::move(other.map_) },
  10178. inline_{ other.inline_ }
  10179. {
  10180. #if TOML_LIFETIME_HOOKS
  10181. TOML_TABLE_CREATED;
  10182. #endif
  10183. }
  10184. TOML_EXTERNAL_LINKAGE
  10185. table& table::operator=(const table& rhs)
  10186. {
  10187. if (&rhs != this)
  10188. {
  10189. node::operator=(rhs);
  10190. map_.clear();
  10191. for (auto&& [k, v] : rhs.map_)
  10192. map_.emplace_hint(map_.end(), k, impl::make_node(*v));
  10193. inline_ = rhs.inline_;
  10194. }
  10195. return *this;
  10196. }
  10197. TOML_EXTERNAL_LINKAGE
  10198. table& table::operator=(table&& rhs) noexcept
  10199. {
  10200. if (&rhs != this)
  10201. {
  10202. node::operator=(std::move(rhs));
  10203. map_ = std::move(rhs.map_);
  10204. inline_ = rhs.inline_;
  10205. }
  10206. return *this;
  10207. }
  10208. TOML_PURE_GETTER
  10209. TOML_EXTERNAL_LINKAGE
  10210. bool table::is_homogeneous(node_type ntype) const noexcept
  10211. {
  10212. if (map_.empty())
  10213. return false;
  10214. if (ntype == node_type::none)
  10215. ntype = map_.cbegin()->second->type();
  10216. for (auto&& [k, v] : map_)
  10217. {
  10218. TOML_UNUSED(k);
  10219. if (v->type() != ntype)
  10220. return false;
  10221. }
  10222. return true;
  10223. }
  10224. TOML_PURE_GETTER
  10225. TOML_EXTERNAL_LINKAGE
  10226. bool table::is_homogeneous(node_type ntype, node * &first_nonmatch) noexcept
  10227. {
  10228. if (map_.empty())
  10229. {
  10230. first_nonmatch = {};
  10231. return false;
  10232. }
  10233. if (ntype == node_type::none)
  10234. ntype = map_.cbegin()->second->type();
  10235. for (const auto& [k, v] : map_)
  10236. {
  10237. TOML_UNUSED(k);
  10238. if (v->type() != ntype)
  10239. {
  10240. first_nonmatch = v.get();
  10241. return false;
  10242. }
  10243. }
  10244. return true;
  10245. }
  10246. TOML_PURE_GETTER
  10247. TOML_EXTERNAL_LINKAGE
  10248. bool table::is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept
  10249. {
  10250. node* fnm = nullptr;
  10251. const auto result = const_cast<table&>(*this).is_homogeneous(ntype, fnm);
  10252. first_nonmatch = fnm;
  10253. return result;
  10254. }
  10255. TOML_PURE_GETTER
  10256. TOML_EXTERNAL_LINKAGE
  10257. node* table::get(std::string_view key) noexcept
  10258. {
  10259. if (auto it = map_.find(key); it != map_.end())
  10260. return it->second.get();
  10261. return nullptr;
  10262. }
  10263. TOML_EXTERNAL_LINKAGE
  10264. node& table::at(std::string_view key)
  10265. {
  10266. auto n = get(key);
  10267. #if TOML_COMPILER_HAS_EXCEPTIONS
  10268. if (!n)
  10269. {
  10270. auto err = "key '"s;
  10271. err.append(key);
  10272. err.append("' not found in table"sv);
  10273. throw std::out_of_range{ err };
  10274. }
  10275. #else
  10276. TOML_ASSERT_ASSUME(n && "key not found in table!");
  10277. #endif
  10278. return *n;
  10279. }
  10280. TOML_PURE_GETTER
  10281. TOML_EXTERNAL_LINKAGE
  10282. table::map_iterator table::get_lower_bound(std::string_view key) noexcept
  10283. {
  10284. return map_.lower_bound(key);
  10285. }
  10286. TOML_PURE_GETTER
  10287. TOML_EXTERNAL_LINKAGE
  10288. table::iterator table::find(std::string_view key) noexcept
  10289. {
  10290. return iterator{ map_.find(key) };
  10291. }
  10292. TOML_PURE_GETTER
  10293. TOML_EXTERNAL_LINKAGE
  10294. table::const_iterator table::find(std::string_view key) const noexcept
  10295. {
  10296. return const_iterator{ map_.find(key) };
  10297. }
  10298. TOML_EXTERNAL_LINKAGE
  10299. table::map_iterator table::erase(const_map_iterator pos) noexcept
  10300. {
  10301. return map_.erase(pos);
  10302. }
  10303. TOML_EXTERNAL_LINKAGE
  10304. table::map_iterator table::erase(const_map_iterator begin, const_map_iterator end) noexcept
  10305. {
  10306. return map_.erase(begin, end);
  10307. }
  10308. TOML_EXTERNAL_LINKAGE
  10309. size_t table::erase(std::string_view key) noexcept
  10310. {
  10311. if (auto it = map_.find(key); it != map_.end())
  10312. {
  10313. map_.erase(it);
  10314. return size_t{ 1 };
  10315. }
  10316. return size_t{};
  10317. }
  10318. TOML_EXTERNAL_LINKAGE
  10319. table& table::prune(bool recursive)& noexcept
  10320. {
  10321. if (map_.empty())
  10322. return *this;
  10323. for (auto it = map_.begin(); it != map_.end();)
  10324. {
  10325. if (auto arr = it->second->as_array())
  10326. {
  10327. if (recursive)
  10328. arr->prune(true);
  10329. if (arr->empty())
  10330. {
  10331. it = map_.erase(it);
  10332. continue;
  10333. }
  10334. }
  10335. else if (auto tbl = it->second->as_table())
  10336. {
  10337. if (recursive)
  10338. tbl->prune(true);
  10339. if (tbl->empty())
  10340. {
  10341. it = map_.erase(it);
  10342. continue;
  10343. }
  10344. }
  10345. it++;
  10346. }
  10347. return *this;
  10348. }
  10349. TOML_EXTERNAL_LINKAGE
  10350. void table::clear() noexcept
  10351. {
  10352. map_.clear();
  10353. }
  10354. TOML_EXTERNAL_LINKAGE
  10355. table::map_iterator table::insert_with_hint(const_iterator hint, key && k, impl::node_ptr && v)
  10356. {
  10357. return map_.emplace_hint(const_map_iterator{ hint }, std::move(k), std::move(v));
  10358. }
  10359. TOML_PURE_GETTER
  10360. TOML_EXTERNAL_LINKAGE
  10361. bool TOML_CALLCONV table::equal(const table& lhs, const table& rhs) noexcept
  10362. {
  10363. if (&lhs == &rhs)
  10364. return true;
  10365. if (lhs.map_.size() != rhs.map_.size())
  10366. return false;
  10367. for (auto l = lhs.map_.begin(), r = rhs.map_.begin(), e = lhs.map_.end(); l != e; l++, r++)
  10368. {
  10369. if (l->first != r->first)
  10370. return false;
  10371. const auto lhs_type = l->second->type();
  10372. const node& rhs_ = *r->second;
  10373. const auto rhs_type = rhs_.type();
  10374. if (lhs_type != rhs_type)
  10375. return false;
  10376. const bool equal = l->second->visit(
  10377. [&](const auto& lhs_) noexcept
  10378. { return lhs_ == *reinterpret_cast<std::remove_reference_t<decltype(lhs_)>*>(&rhs_); });
  10379. if (!equal)
  10380. return false;
  10381. }
  10382. return true;
  10383. }
  10384. }
  10385. TOML_NAMESPACE_END;
  10386. #ifdef _MSC_VER
  10387. #pragma pop_macro("min")
  10388. #pragma pop_macro("max")
  10389. #ifndef __clang__
  10390. #pragma inline_recursion(off)
  10391. #endif
  10392. #endif
  10393. TOML_POP_WARNINGS;
  10394. //******** impl/simd.hpp *********************************************************************************************
  10395. #if TOML_ENABLE_SIMD
  10396. #if defined(__SSE2__) \
  10397. || (defined(_MSC_VER) && (defined(_M_AMD64) || defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 2)))
  10398. #define TOML_HAS_SSE2 1
  10399. #endif
  10400. #if defined(__SSE4_1__) || (defined(_MSC_VER) && (defined(__AVX__) || defined(__AVX2__)))
  10401. #define TOML_HAS_SSE4_1 1
  10402. #endif
  10403. #endif // TOML_ENABLE_SIMD
  10404. #ifndef TOML_HAS_SSE2
  10405. #define TOML_HAS_SSE2 0
  10406. #endif
  10407. #ifndef TOML_HAS_SSE4_1
  10408. #define TOML_HAS_SSE4_1 0
  10409. #endif
  10410. TOML_DISABLE_WARNINGS;
  10411. #if TOML_HAS_SSE4_1
  10412. #include <smmintrin.h>
  10413. #endif
  10414. #if TOML_HAS_SSE2
  10415. #include <emmintrin.h>
  10416. #endif
  10417. TOML_ENABLE_WARNINGS;
  10418. //******** impl/unicode.inl ******************************************************************************************
  10419. TOML_PUSH_WARNINGS;
  10420. #ifdef _MSC_VER
  10421. #ifndef __clang__
  10422. #pragma inline_recursion(on)
  10423. #endif
  10424. #pragma push_macro("min")
  10425. #pragma push_macro("max")
  10426. #undef min
  10427. #undef max
  10428. #endif
  10429. TOML_IMPL_NAMESPACE_START
  10430. {
  10431. TOML_PURE_GETTER
  10432. TOML_EXTERNAL_LINKAGE
  10433. bool is_ascii(const char* str, size_t len) noexcept
  10434. {
  10435. const char* const end = str + len;
  10436. #if TOML_HAS_SSE2 && (128 % CHAR_BIT) == 0
  10437. {
  10438. constexpr size_t chars_per_vector = 128u / CHAR_BIT;
  10439. if (const size_t simdable = len - (len % chars_per_vector))
  10440. {
  10441. __m128i mask = _mm_setzero_si128();
  10442. for (const char* const e = str + simdable; str < e; str += chars_per_vector)
  10443. {
  10444. const __m128i current_bytes = _mm_loadu_si128(reinterpret_cast<const __m128i*>(str));
  10445. mask = _mm_or_si128(mask, current_bytes);
  10446. }
  10447. const __m128i has_error = _mm_cmpgt_epi8(_mm_setzero_si128(), mask);
  10448. #if TOML_HAS_SSE4_1
  10449. if (!_mm_testz_si128(has_error, has_error))
  10450. return false;
  10451. #else
  10452. if (_mm_movemask_epi8(_mm_cmpeq_epi8(has_error, _mm_setzero_si128())) != 0xFFFF)
  10453. return false;
  10454. #endif
  10455. }
  10456. }
  10457. #endif
  10458. for (; str < end; str++)
  10459. if (static_cast<unsigned char>(*str) > 127u)
  10460. return false;
  10461. return true;
  10462. }
  10463. }
  10464. TOML_IMPL_NAMESPACE_END;
  10465. #ifdef _MSC_VER
  10466. #pragma pop_macro("min")
  10467. #pragma pop_macro("max")
  10468. #ifndef __clang__
  10469. #pragma inline_recursion(off)
  10470. #endif
  10471. #endif
  10472. TOML_POP_WARNINGS;
  10473. //******** impl/parser.inl *******************************************************************************************
  10474. #if TOML_ENABLE_PARSER
  10475. TOML_DISABLE_WARNINGS;
  10476. #include <istream>
  10477. #include <fstream>
  10478. #if TOML_INT_CHARCONV || TOML_FLOAT_CHARCONV
  10479. #include <charconv>
  10480. #endif
  10481. #if !TOML_INT_CHARCONV || !TOML_FLOAT_CHARCONV
  10482. #include <sstream>
  10483. #endif
  10484. #if !TOML_INT_CHARCONV
  10485. #include <iomanip>
  10486. #endif
  10487. TOML_ENABLE_WARNINGS;
  10488. TOML_PUSH_WARNINGS;
  10489. #ifdef _MSC_VER
  10490. #ifndef __clang__
  10491. #pragma inline_recursion(on)
  10492. #endif
  10493. #pragma push_macro("min")
  10494. #pragma push_macro("max")
  10495. #undef min
  10496. #undef max
  10497. #endif
  10498. TOML_ANON_NAMESPACE_START
  10499. {
  10500. template <typename T>
  10501. class utf8_byte_stream;
  10502. TOML_INTERNAL_LINKAGE
  10503. constexpr auto utf8_byte_order_mark = "\xEF\xBB\xBF"sv;
  10504. template <typename Char>
  10505. class utf8_byte_stream<std::basic_string_view<Char>>
  10506. {
  10507. static_assert(sizeof(Char) == 1);
  10508. private:
  10509. std::basic_string_view<Char> source_;
  10510. size_t position_ = {};
  10511. public:
  10512. TOML_NODISCARD_CTOR
  10513. explicit constexpr utf8_byte_stream(std::basic_string_view<Char> sv) noexcept //
  10514. : source_{ sv }
  10515. {
  10516. // skip bom
  10517. if (source_.length() >= 3u && memcmp(utf8_byte_order_mark.data(), source_.data(), 3u) == 0)
  10518. position_ += 3u;
  10519. }
  10520. TOML_CONST_INLINE_GETTER
  10521. constexpr bool error() const noexcept
  10522. {
  10523. return false;
  10524. }
  10525. TOML_PURE_INLINE_GETTER
  10526. constexpr bool eof() const noexcept
  10527. {
  10528. return position_ >= source_.length();
  10529. }
  10530. TOML_PURE_INLINE_GETTER
  10531. explicit constexpr operator bool() const noexcept
  10532. {
  10533. return !eof();
  10534. }
  10535. TOML_PURE_INLINE_GETTER
  10536. constexpr bool peek_eof() const noexcept
  10537. {
  10538. return eof();
  10539. }
  10540. TOML_NODISCARD
  10541. TOML_ATTR(nonnull)
  10542. size_t operator()(void* dest, size_t num) noexcept
  10543. {
  10544. TOML_ASSERT_ASSUME(!eof());
  10545. num = impl::min(position_ + num, source_.length()) - position_;
  10546. std::memcpy(dest, source_.data() + position_, num);
  10547. position_ += num;
  10548. return num;
  10549. }
  10550. };
  10551. template <>
  10552. class utf8_byte_stream<std::istream>
  10553. {
  10554. private:
  10555. std::istream* source_;
  10556. public:
  10557. TOML_NODISCARD_CTOR
  10558. explicit utf8_byte_stream(std::istream& stream) noexcept(!TOML_COMPILER_HAS_EXCEPTIONS) //
  10559. : source_{ &stream }
  10560. {
  10561. if (!*this) // eof, bad
  10562. return;
  10563. const auto initial_pos = source_->tellg();
  10564. char bom[3];
  10565. source_->read(bom, 3);
  10566. if (source_->bad() || (source_->gcount() == 3 && memcmp(utf8_byte_order_mark.data(), bom, 3u) == 0))
  10567. return;
  10568. source_->clear();
  10569. source_->seekg(initial_pos, std::istream::beg);
  10570. }
  10571. TOML_PURE_INLINE_GETTER
  10572. bool error() const noexcept
  10573. {
  10574. return !!(source_->rdstate() & std::istream::badbit);
  10575. }
  10576. TOML_PURE_INLINE_GETTER
  10577. bool eof() const noexcept
  10578. {
  10579. return !!(source_->rdstate() & std::istream::eofbit);
  10580. }
  10581. TOML_PURE_INLINE_GETTER
  10582. explicit operator bool() const noexcept
  10583. {
  10584. return !(source_->rdstate() & (std::istream::badbit | std::istream::eofbit));
  10585. }
  10586. TOML_NODISCARD
  10587. bool peek_eof() const noexcept(!TOML_COMPILER_HAS_EXCEPTIONS)
  10588. {
  10589. return eof() || source_->peek() == std::istream::traits_type::eof();
  10590. }
  10591. TOML_NODISCARD
  10592. TOML_ATTR(nonnull)
  10593. size_t operator()(void* dest, size_t num) noexcept(!TOML_COMPILER_HAS_EXCEPTIONS)
  10594. {
  10595. TOML_ASSERT(*this);
  10596. source_->read(static_cast<char*>(dest), static_cast<std::streamsize>(num));
  10597. return static_cast<size_t>(source_->gcount());
  10598. }
  10599. };
  10600. struct utf8_codepoint
  10601. {
  10602. char32_t value;
  10603. char bytes[4];
  10604. size_t count;
  10605. source_position position;
  10606. TOML_PURE_INLINE_GETTER
  10607. constexpr operator const char32_t&() const noexcept
  10608. {
  10609. return value;
  10610. }
  10611. TOML_PURE_INLINE_GETTER
  10612. constexpr const char32_t& operator*() const noexcept
  10613. {
  10614. return value;
  10615. }
  10616. };
  10617. static_assert(std::is_trivial_v<utf8_codepoint>);
  10618. static_assert(std::is_standard_layout_v<utf8_codepoint>);
  10619. struct TOML_ABSTRACT_INTERFACE utf8_reader_interface
  10620. {
  10621. TOML_NODISCARD
  10622. virtual const source_path_ptr& source_path() const noexcept = 0;
  10623. TOML_NODISCARD
  10624. virtual const utf8_codepoint* read_next() noexcept(!TOML_COMPILER_HAS_EXCEPTIONS) = 0;
  10625. TOML_NODISCARD
  10626. virtual bool peek_eof() const noexcept(!TOML_COMPILER_HAS_EXCEPTIONS) = 0;
  10627. #if !TOML_EXCEPTIONS
  10628. TOML_NODISCARD
  10629. virtual optional<parse_error>&& error() noexcept = 0;
  10630. #endif
  10631. virtual ~utf8_reader_interface() noexcept = default;
  10632. };
  10633. #if TOML_EXCEPTIONS
  10634. #define utf8_reader_error(...) throw parse_error(__VA_ARGS__)
  10635. #define utf8_reader_return_after_error(...) static_assert(true)
  10636. #define utf8_reader_error_check(...) static_assert(true)
  10637. #else
  10638. #define utf8_reader_error(...) err_.emplace(__VA_ARGS__)
  10639. #define utf8_reader_return_after_error(...) return __VA_ARGS__
  10640. #define utf8_reader_error_check(...) \
  10641. do \
  10642. { \
  10643. if TOML_UNLIKELY(err_) \
  10644. return __VA_ARGS__; \
  10645. } \
  10646. while (false)
  10647. #endif
  10648. #if defined(__APPLE__) || defined(__MINGW32__) || defined(__MINGW64__)
  10649. #define TOML_OVERALIGNED
  10650. #else
  10651. #define TOML_OVERALIGNED alignas(32)
  10652. #endif
  10653. template <typename T>
  10654. class TOML_EMPTY_BASES utf8_reader final : public utf8_reader_interface
  10655. {
  10656. private:
  10657. static constexpr size_t block_capacity = 32;
  10658. utf8_byte_stream<T> stream_;
  10659. source_position next_pos_ = { 1, 1 };
  10660. impl::utf8_decoder decoder_;
  10661. struct currently_decoding_t
  10662. {
  10663. char bytes[4];
  10664. size_t count;
  10665. } currently_decoding_;
  10666. struct codepoints_t
  10667. {
  10668. TOML_OVERALIGNED utf8_codepoint buffer[block_capacity];
  10669. size_t current;
  10670. size_t count;
  10671. } codepoints_;
  10672. source_path_ptr source_path_;
  10673. #if !TOML_EXCEPTIONS
  10674. optional<parse_error> err_;
  10675. #endif
  10676. bool read_next_block() noexcept(!TOML_COMPILER_HAS_EXCEPTIONS)
  10677. {
  10678. TOML_ASSERT(stream_);
  10679. TOML_OVERALIGNED char raw_bytes[block_capacity];
  10680. size_t raw_bytes_read;
  10681. // read the next raw (encoded) block in from the stream
  10682. if constexpr (noexcept(stream_(raw_bytes, block_capacity)) || !TOML_EXCEPTIONS)
  10683. {
  10684. raw_bytes_read = stream_(raw_bytes, block_capacity);
  10685. }
  10686. #if TOML_EXCEPTIONS
  10687. else
  10688. {
  10689. try
  10690. {
  10691. raw_bytes_read = stream_(raw_bytes, block_capacity);
  10692. }
  10693. catch (const std::exception& exc)
  10694. {
  10695. throw parse_error{ exc.what(), next_pos_, source_path_ };
  10696. }
  10697. catch (...)
  10698. {
  10699. throw parse_error{ "An unspecified error occurred", next_pos_, source_path_ };
  10700. }
  10701. }
  10702. #endif // TOML_EXCEPTIONS
  10703. // handle a zero-byte read
  10704. if TOML_UNLIKELY(!raw_bytes_read)
  10705. {
  10706. if (stream_.eof())
  10707. {
  10708. // EOF only sets the error state if the decoder wants more input, otherwise
  10709. // a zero-byte read might have just caused the underlying stream to realize it's exhaused and set
  10710. // the EOF flag, and that's totally fine
  10711. if (decoder_.needs_more_input())
  10712. utf8_reader_error("Encountered EOF during incomplete utf-8 code point sequence",
  10713. next_pos_,
  10714. source_path_);
  10715. }
  10716. else
  10717. {
  10718. utf8_reader_error("Reading from the underlying stream failed - zero bytes read",
  10719. next_pos_,
  10720. source_path_);
  10721. }
  10722. return false;
  10723. }
  10724. TOML_ASSERT_ASSUME(raw_bytes_read);
  10725. std::memset(&codepoints_, 0, sizeof(codepoints_));
  10726. // helper for calculating decoded codepoint line+cols
  10727. const auto calc_positions = [&]() noexcept
  10728. {
  10729. for (size_t i = 0; i < codepoints_.count; i++)
  10730. {
  10731. auto& cp = codepoints_.buffer[i];
  10732. cp.position = next_pos_;
  10733. if (cp == U'\n')
  10734. {
  10735. next_pos_.line++;
  10736. next_pos_.column = source_index{ 1 };
  10737. }
  10738. else
  10739. next_pos_.column++;
  10740. }
  10741. };
  10742. // decide whether we need to use the UTF-8 decoder or if we can treat this block as plain ASCII
  10743. const auto ascii_fast_path = !decoder_.needs_more_input() && impl::is_ascii(raw_bytes, raw_bytes_read);
  10744. // ASCII fast-path
  10745. if (ascii_fast_path)
  10746. {
  10747. decoder_.reset();
  10748. currently_decoding_.count = {};
  10749. codepoints_.count = raw_bytes_read;
  10750. for (size_t i = 0; i < codepoints_.count; i++)
  10751. {
  10752. auto& cp = codepoints_.buffer[i];
  10753. cp.value = static_cast<char32_t>(raw_bytes[i]);
  10754. cp.bytes[0] = raw_bytes[i];
  10755. cp.count = 1u;
  10756. }
  10757. }
  10758. // UTF-8 slow-path
  10759. else
  10760. {
  10761. // helper for getting precise error location
  10762. const auto error_pos = [&]() noexcept -> const source_position&
  10763. { //
  10764. return codepoints_.count ? codepoints_.buffer[codepoints_.count - 1u].position : next_pos_;
  10765. };
  10766. for (size_t i = 0; i < raw_bytes_read; i++)
  10767. {
  10768. decoder_(static_cast<uint8_t>(raw_bytes[i]));
  10769. if TOML_UNLIKELY(decoder_.error())
  10770. {
  10771. calc_positions();
  10772. utf8_reader_error("Encountered invalid utf-8 sequence", error_pos(), source_path_);
  10773. utf8_reader_return_after_error(false);
  10774. }
  10775. currently_decoding_.bytes[currently_decoding_.count++] = raw_bytes[i];
  10776. if (decoder_.has_code_point())
  10777. {
  10778. auto& cp = codepoints_.buffer[codepoints_.count++];
  10779. cp.value = decoder_.codepoint;
  10780. cp.count = currently_decoding_.count;
  10781. std::memcpy(cp.bytes, currently_decoding_.bytes, currently_decoding_.count);
  10782. currently_decoding_.count = {};
  10783. }
  10784. else if TOML_UNLIKELY(currently_decoding_.count == 4u)
  10785. {
  10786. calc_positions();
  10787. utf8_reader_error("Encountered overlong utf-8 sequence", error_pos(), source_path_);
  10788. utf8_reader_return_after_error(false);
  10789. }
  10790. }
  10791. if TOML_UNLIKELY(decoder_.needs_more_input() && stream_.eof())
  10792. {
  10793. calc_positions();
  10794. utf8_reader_error("Encountered EOF during incomplete utf-8 code point sequence",
  10795. error_pos(),
  10796. source_path_);
  10797. utf8_reader_return_after_error(false);
  10798. }
  10799. }
  10800. TOML_ASSERT_ASSUME(codepoints_.count);
  10801. calc_positions();
  10802. // handle general I/O errors
  10803. // (down here so the next_pos_ benefits from calc_positions())
  10804. if TOML_UNLIKELY(stream_.error())
  10805. {
  10806. utf8_reader_error("An I/O error occurred while reading from the underlying stream",
  10807. next_pos_,
  10808. source_path_);
  10809. utf8_reader_return_after_error(false);
  10810. }
  10811. return true;
  10812. }
  10813. public:
  10814. template <typename U, typename String = std::string_view>
  10815. TOML_NODISCARD_CTOR
  10816. explicit utf8_reader(U&& source, String&& source_path = {}) noexcept(
  10817. std::is_nothrow_constructible_v<utf8_byte_stream<T>, U&&>)
  10818. : stream_{ static_cast<U&&>(source) }
  10819. {
  10820. currently_decoding_.count = {};
  10821. codepoints_.current = {};
  10822. codepoints_.count = {};
  10823. if (!source_path.empty())
  10824. source_path_ = std::make_shared<const std::string>(static_cast<String&&>(source_path));
  10825. }
  10826. TOML_PURE_INLINE_GETTER
  10827. const source_path_ptr& source_path() const noexcept final
  10828. {
  10829. return source_path_;
  10830. }
  10831. TOML_NODISCARD
  10832. const utf8_codepoint* read_next() noexcept(!TOML_COMPILER_HAS_EXCEPTIONS) final
  10833. {
  10834. utf8_reader_error_check({});
  10835. if (codepoints_.current == codepoints_.count)
  10836. {
  10837. if TOML_UNLIKELY(!stream_ || !read_next_block())
  10838. return nullptr;
  10839. TOML_ASSERT_ASSUME(!codepoints_.current);
  10840. }
  10841. TOML_ASSERT_ASSUME(codepoints_.count);
  10842. TOML_ASSERT_ASSUME(codepoints_.count <= block_capacity);
  10843. TOML_ASSERT_ASSUME(codepoints_.current < codepoints_.count);
  10844. return &codepoints_.buffer[codepoints_.current++];
  10845. }
  10846. TOML_NODISCARD
  10847. bool peek_eof() const noexcept(!TOML_COMPILER_HAS_EXCEPTIONS) final
  10848. {
  10849. return stream_.peek_eof();
  10850. }
  10851. #if !TOML_EXCEPTIONS
  10852. TOML_NODISCARD
  10853. optional<parse_error>&& error() noexcept final
  10854. {
  10855. return std::move(err_);
  10856. }
  10857. #endif
  10858. };
  10859. template <typename Char>
  10860. utf8_reader(std::basic_string_view<Char>, std::string_view) -> utf8_reader<std::basic_string_view<Char>>;
  10861. template <typename Char>
  10862. utf8_reader(std::basic_string_view<Char>, std::string&&) -> utf8_reader<std::basic_string_view<Char>>;
  10863. template <typename Char>
  10864. utf8_reader(std::basic_istream<Char>&, std::string_view) -> utf8_reader<std::basic_istream<Char>>;
  10865. template <typename Char>
  10866. utf8_reader(std::basic_istream<Char>&, std::string&&) -> utf8_reader<std::basic_istream<Char>>;
  10867. #if TOML_EXCEPTIONS
  10868. #define utf8_buffered_reader_error_check(...) static_assert(true)
  10869. #else
  10870. #define utf8_buffered_reader_error_check(...) \
  10871. do \
  10872. { \
  10873. if TOML_UNLIKELY(reader_.error()) \
  10874. return __VA_ARGS__; \
  10875. } \
  10876. while (false)
  10877. #endif
  10878. class TOML_EMPTY_BASES utf8_buffered_reader
  10879. {
  10880. public:
  10881. static constexpr size_t max_history_length = 128;
  10882. private:
  10883. static constexpr size_t history_buffer_size = max_history_length - 1; //'head' is stored in the reader
  10884. utf8_reader_interface& reader_;
  10885. struct
  10886. {
  10887. utf8_codepoint buffer[history_buffer_size];
  10888. size_t count, first;
  10889. } history_ = {};
  10890. const utf8_codepoint* head_ = {};
  10891. size_t negative_offset_ = {};
  10892. public:
  10893. TOML_NODISCARD_CTOR
  10894. explicit utf8_buffered_reader(utf8_reader_interface& reader) noexcept //
  10895. : reader_{ reader }
  10896. {}
  10897. TOML_PURE_INLINE_GETTER
  10898. const source_path_ptr& source_path() const noexcept
  10899. {
  10900. return reader_.source_path();
  10901. }
  10902. TOML_NODISCARD
  10903. const utf8_codepoint* read_next() noexcept(!TOML_COMPILER_HAS_EXCEPTIONS)
  10904. {
  10905. utf8_buffered_reader_error_check({});
  10906. if (negative_offset_)
  10907. {
  10908. negative_offset_--;
  10909. // an entry negative offset of 1 just means "replay the current head"
  10910. if (!negative_offset_)
  10911. return head_;
  10912. // otherwise step back into the history buffer
  10913. else
  10914. return history_.buffer
  10915. + ((history_.first + history_.count - negative_offset_) % history_buffer_size);
  10916. }
  10917. else
  10918. {
  10919. // first character read from stream
  10920. if TOML_UNLIKELY(!history_.count && !head_)
  10921. head_ = reader_.read_next();
  10922. // subsequent characters and not eof
  10923. else if (head_)
  10924. {
  10925. if TOML_UNLIKELY(history_.count < history_buffer_size)
  10926. history_.buffer[history_.count++] = *head_;
  10927. else
  10928. history_.buffer[(history_.first++ + history_buffer_size) % history_buffer_size] = *head_;
  10929. head_ = reader_.read_next();
  10930. }
  10931. return head_;
  10932. }
  10933. }
  10934. TOML_NODISCARD
  10935. const utf8_codepoint* step_back(size_t count) noexcept
  10936. {
  10937. utf8_buffered_reader_error_check({});
  10938. TOML_ASSERT_ASSUME(history_.count);
  10939. TOML_ASSERT_ASSUME(negative_offset_ + count <= history_.count);
  10940. negative_offset_ += count;
  10941. return negative_offset_
  10942. ? history_.buffer + ((history_.first + history_.count - negative_offset_) % history_buffer_size)
  10943. : head_;
  10944. }
  10945. TOML_NODISCARD
  10946. bool peek_eof() const noexcept(!TOML_COMPILER_HAS_EXCEPTIONS)
  10947. {
  10948. return reader_.peek_eof();
  10949. }
  10950. #if !TOML_EXCEPTIONS
  10951. TOML_NODISCARD
  10952. optional<parse_error>&& error() noexcept
  10953. {
  10954. return reader_.error();
  10955. }
  10956. #endif
  10957. };
  10958. }
  10959. TOML_ANON_NAMESPACE_END;
  10960. #if TOML_EXCEPTIONS
  10961. #define TOML_RETURNS_BY_THROWING [[noreturn]]
  10962. #else
  10963. #define TOML_RETURNS_BY_THROWING
  10964. #endif
  10965. TOML_ANON_NAMESPACE_START
  10966. {
  10967. template <typename... T>
  10968. TOML_CONST_GETTER
  10969. TOML_INTERNAL_LINKAGE
  10970. constexpr bool is_match(char32_t codepoint, T... vals) noexcept
  10971. {
  10972. static_assert((std::is_same_v<char32_t, T> && ...));
  10973. return ((codepoint == vals) || ...);
  10974. }
  10975. template <uint64_t>
  10976. struct parse_integer_traits;
  10977. template <>
  10978. struct parse_integer_traits<2>
  10979. {
  10980. static constexpr auto scope_qualifier = "binary integer"sv;
  10981. static constexpr auto is_digit = impl::is_binary_digit;
  10982. static constexpr auto is_signed = false;
  10983. static constexpr auto max_digits = 63;
  10984. static constexpr auto prefix_codepoint = U'b';
  10985. static constexpr auto prefix = "b"sv;
  10986. static constexpr auto full_prefix = "0b"sv;
  10987. };
  10988. template <>
  10989. struct parse_integer_traits<8>
  10990. {
  10991. static constexpr auto scope_qualifier = "octal integer"sv;
  10992. static constexpr auto is_digit = impl::is_octal_digit;
  10993. static constexpr auto is_signed = false;
  10994. static constexpr auto max_digits = 21; // strlen("777777777777777777777")
  10995. static constexpr auto prefix_codepoint = U'o';
  10996. static constexpr auto prefix = "o"sv;
  10997. static constexpr auto full_prefix = "0o"sv;
  10998. };
  10999. template <>
  11000. struct parse_integer_traits<10>
  11001. {
  11002. static constexpr auto scope_qualifier = "decimal integer"sv;
  11003. static constexpr auto is_digit = impl::is_decimal_digit;
  11004. static constexpr auto is_signed = true;
  11005. static constexpr auto max_digits = 19; // strlen("9223372036854775807")
  11006. static constexpr auto full_prefix = ""sv;
  11007. };
  11008. template <>
  11009. struct parse_integer_traits<16>
  11010. {
  11011. static constexpr auto scope_qualifier = "hexadecimal integer"sv;
  11012. static constexpr auto is_digit = impl::is_hexadecimal_digit;
  11013. static constexpr auto is_signed = false;
  11014. static constexpr auto max_digits = 16; // strlen("7FFFFFFFFFFFFFFF")
  11015. static constexpr auto prefix_codepoint = U'x';
  11016. static constexpr auto prefix = "x"sv;
  11017. static constexpr auto full_prefix = "0x"sv;
  11018. };
  11019. TOML_PURE_GETTER
  11020. TOML_INTERNAL_LINKAGE
  11021. std::string_view to_sv(node_type val) noexcept
  11022. {
  11023. return impl::node_type_friendly_names[impl::unwrap_enum(val)];
  11024. }
  11025. TOML_PURE_GETTER
  11026. TOML_INTERNAL_LINKAGE
  11027. std::string_view to_sv(const std::string& str) noexcept
  11028. {
  11029. return std::string_view{ str };
  11030. }
  11031. TOML_CONST_GETTER
  11032. TOML_INTERNAL_LINKAGE
  11033. std::string_view to_sv(bool val) noexcept
  11034. {
  11035. using namespace std::string_view_literals;
  11036. return val ? "true"sv : "false"sv;
  11037. }
  11038. TOML_PURE_GETTER
  11039. TOML_INTERNAL_LINKAGE
  11040. std::string_view to_sv(const utf8_codepoint& cp) noexcept
  11041. {
  11042. if (cp.value <= U'\x1F')
  11043. return impl::control_char_escapes[cp.value];
  11044. else if (cp.value == U'\x7F')
  11045. return "\\u007F"sv;
  11046. else
  11047. return std::string_view{ cp.bytes, cp.count };
  11048. }
  11049. TOML_PURE_GETTER
  11050. TOML_INTERNAL_LINKAGE
  11051. std::string_view to_sv(const utf8_codepoint* cp) noexcept
  11052. {
  11053. if (cp)
  11054. return to_sv(*cp);
  11055. return ""sv;
  11056. }
  11057. struct escaped_codepoint
  11058. {
  11059. const utf8_codepoint& cp;
  11060. };
  11061. template <typename T>
  11062. TOML_ATTR(nonnull)
  11063. TOML_INTERNAL_LINKAGE
  11064. void concatenate(char*& write_pos, char* const buf_end, const T& arg) noexcept
  11065. {
  11066. if TOML_UNLIKELY(write_pos >= buf_end)
  11067. return;
  11068. using arg_type = impl::remove_cvref<T>;
  11069. // string views
  11070. if constexpr (std::is_same_v<arg_type, std::string_view>)
  11071. {
  11072. const auto max_chars = static_cast<size_t>(buf_end - write_pos);
  11073. const auto len = max_chars < arg.length() ? max_chars : arg.length();
  11074. std::memcpy(write_pos, arg.data(), len);
  11075. write_pos += len;
  11076. }
  11077. // doubles
  11078. else if constexpr (std::is_same_v<arg_type, double>)
  11079. {
  11080. #if TOML_FLOAT_CHARCONV
  11081. const auto result = std::to_chars(write_pos, buf_end, arg);
  11082. write_pos = result.ptr;
  11083. #else
  11084. std::ostringstream ss;
  11085. ss.imbue(std::locale::classic());
  11086. ss.precision(std::numeric_limits<arg_type>::max_digits10);
  11087. ss << arg;
  11088. concatenate(write_pos, buf_end, to_sv(std::move(ss).str()));
  11089. #endif
  11090. }
  11091. // 64-bit integers
  11092. else if constexpr (impl::is_one_of<arg_type, int64_t, uint64_t>)
  11093. {
  11094. #if TOML_INT_CHARCONV
  11095. const auto result = std::to_chars(write_pos, buf_end, arg);
  11096. write_pos = result.ptr;
  11097. #else
  11098. std::ostringstream ss;
  11099. ss.imbue(std::locale::classic());
  11100. using cast_type = std::conditional_t<std::is_signed_v<arg_type>, int64_t, uint64_t>;
  11101. ss << static_cast<cast_type>(arg);
  11102. concatenate(write_pos, buf_end, to_sv(std::move(ss).str()));
  11103. #endif
  11104. }
  11105. // escaped_codepoint
  11106. else if constexpr (std::is_same_v<arg_type, escaped_codepoint>)
  11107. {
  11108. if (arg.cp.value <= U'\x7F')
  11109. concatenate(write_pos, buf_end, to_sv(arg.cp));
  11110. else
  11111. {
  11112. auto val = static_cast<uint_least32_t>(arg.cp.value);
  11113. const auto digits = val > 0xFFFFu ? 8u : 4u;
  11114. constexpr auto mask = uint_least32_t{ 0xFu };
  11115. char buf[10] = { '\\', digits > 4 ? 'U' : 'u' };
  11116. for (auto i = 2u + digits; i-- > 2u;)
  11117. {
  11118. const auto hexdig = val & mask;
  11119. buf[i] = static_cast<char>(hexdig >= 0xAu ? ('A' + (hexdig - 0xAu)) : ('0' + hexdig));
  11120. val >>= 4;
  11121. }
  11122. concatenate(write_pos, buf_end, std::string_view{ buf, digits + 2u });
  11123. }
  11124. }
  11125. // all other floats (fallback - coerce to double)
  11126. else if constexpr (std::is_floating_point_v<arg_type>)
  11127. concatenate(write_pos, buf_end, static_cast<double>(arg));
  11128. // all other integers (fallback - coerce to (u)int64_t)
  11129. else if constexpr (std::is_arithmetic_v<arg_type> && std::is_integral_v<arg_type>)
  11130. {
  11131. using cast_type = std::conditional_t<std::is_unsigned_v<arg_type>, uint64_t, int64_t>;
  11132. concatenate(write_pos, buf_end, static_cast<cast_type>(arg));
  11133. }
  11134. else
  11135. {
  11136. static_assert(
  11137. impl::always_false<T>,
  11138. "concatenate() inputs are limited to std::string_views, integers, floats, and escaped_codepoint");
  11139. }
  11140. }
  11141. struct error_builder
  11142. {
  11143. static constexpr std::size_t buf_size = 512;
  11144. char buf[buf_size];
  11145. char* write_pos = buf;
  11146. char* const max_write_pos = buf + (buf_size - std::size_t{ 1 }); // allow for null terminator
  11147. TOML_NODISCARD_CTOR
  11148. error_builder(std::string_view scope) noexcept
  11149. {
  11150. concatenate(write_pos, max_write_pos, "Error while parsing "sv);
  11151. concatenate(write_pos, max_write_pos, scope);
  11152. concatenate(write_pos, max_write_pos, ": "sv);
  11153. }
  11154. template <typename T>
  11155. void append(const T& arg) noexcept
  11156. {
  11157. concatenate(write_pos, max_write_pos, arg);
  11158. }
  11159. TOML_RETURNS_BY_THROWING
  11160. auto finish(const source_position& pos, const source_path_ptr& source_path) const
  11161. {
  11162. *write_pos = '\0';
  11163. #if TOML_EXCEPTIONS
  11164. throw parse_error{ buf, pos, source_path };
  11165. #else
  11166. return parse_error{ std::string(buf, static_cast<size_t>(write_pos - buf)), pos, source_path };
  11167. #endif
  11168. }
  11169. TOML_DELETE_DEFAULTS(error_builder);
  11170. };
  11171. struct parse_scope
  11172. {
  11173. std::string_view& storage_;
  11174. std::string_view parent_;
  11175. TOML_NODISCARD_CTOR
  11176. explicit parse_scope(std::string_view& current_scope, std::string_view new_scope) noexcept
  11177. : storage_{ current_scope },
  11178. parent_{ current_scope }
  11179. {
  11180. storage_ = new_scope;
  11181. }
  11182. ~parse_scope() noexcept
  11183. {
  11184. storage_ = parent_;
  11185. }
  11186. TOML_DELETE_DEFAULTS(parse_scope);
  11187. };
  11188. #define push_parse_scope_2(scope, line) parse_scope ps_##line(current_scope, scope)
  11189. #define push_parse_scope_1(scope, line) push_parse_scope_2(scope, line)
  11190. #define push_parse_scope(scope) push_parse_scope_1(scope, __LINE__)
  11191. struct parse_key_buffer
  11192. {
  11193. std::string buffer;
  11194. std::vector<std::pair<size_t, size_t>> segments;
  11195. std::vector<source_position> starts;
  11196. std::vector<source_position> ends;
  11197. void clear() noexcept
  11198. {
  11199. buffer.clear();
  11200. segments.clear();
  11201. starts.clear();
  11202. ends.clear();
  11203. }
  11204. void push_back(std::string_view segment, source_position b, source_position e)
  11205. {
  11206. segments.push_back({ buffer.length(), segment.length() });
  11207. buffer.append(segment);
  11208. starts.push_back(b);
  11209. ends.push_back(e);
  11210. }
  11211. TOML_PURE_INLINE_GETTER
  11212. std::string_view operator[](size_t i) const noexcept
  11213. {
  11214. return std::string_view{ buffer.c_str() + segments[i].first, segments[i].second };
  11215. }
  11216. TOML_PURE_INLINE_GETTER
  11217. std::string_view back() const noexcept
  11218. {
  11219. return (*this)[segments.size() - 1u];
  11220. }
  11221. TOML_PURE_INLINE_GETTER
  11222. bool empty() const noexcept
  11223. {
  11224. return segments.empty();
  11225. }
  11226. TOML_PURE_INLINE_GETTER
  11227. size_t size() const noexcept
  11228. {
  11229. return segments.size();
  11230. }
  11231. };
  11232. struct depth_counter_scope
  11233. {
  11234. size_t& depth_;
  11235. TOML_NODISCARD_CTOR
  11236. explicit depth_counter_scope(size_t& depth) noexcept //
  11237. : depth_{ depth }
  11238. {
  11239. depth_++;
  11240. }
  11241. ~depth_counter_scope() noexcept
  11242. {
  11243. depth_--;
  11244. }
  11245. TOML_DELETE_DEFAULTS(depth_counter_scope);
  11246. };
  11247. struct parsed_string
  11248. {
  11249. std::string_view value;
  11250. bool was_multi_line;
  11251. };
  11252. struct table_vector_scope
  11253. {
  11254. std::vector<table*>& tables;
  11255. TOML_NODISCARD_CTOR
  11256. explicit table_vector_scope(std::vector<table*>& tables_, table& tbl) //
  11257. : tables{ tables_ }
  11258. {
  11259. tables.push_back(&tbl);
  11260. }
  11261. ~table_vector_scope() noexcept
  11262. {
  11263. tables.pop_back();
  11264. }
  11265. TOML_DELETE_DEFAULTS(table_vector_scope);
  11266. };
  11267. }
  11268. TOML_ANON_NAMESPACE_END;
  11269. #if 1 // parser helper macros
  11270. // Q: "what the fuck is this? MACROS????"
  11271. // A: The parser needs to work in exceptionless mode (returning error objects directly)
  11272. // and exception mode (reporting parse failures by throwing). Two totally different control flows.
  11273. // These macros encapsulate the differences between the two modes so I can write code code
  11274. // as though I was only targeting one mode and not want yeet myself into the sun.
  11275. // They're all #undef'd at the bottom of the parser's implementation so they should be harmless outside
  11276. // of toml++.
  11277. #define is_eof() !cp
  11278. #define assert_not_eof() TOML_ASSERT_ASSUME(cp != nullptr)
  11279. #define return_if_eof(...) \
  11280. do \
  11281. { \
  11282. if TOML_UNLIKELY(is_eof()) \
  11283. return __VA_ARGS__; \
  11284. } \
  11285. while (false)
  11286. #if TOML_EXCEPTIONS
  11287. #define is_error() false
  11288. #define return_after_error(...) TOML_UNREACHABLE
  11289. #define assert_not_error() static_assert(true)
  11290. #define return_if_error(...) static_assert(true)
  11291. #define return_if_error_or_eof(...) return_if_eof(__VA_ARGS__)
  11292. #else
  11293. #define is_error() !!err
  11294. #define return_after_error(...) return __VA_ARGS__
  11295. #define assert_not_error() TOML_ASSERT(!is_error())
  11296. #define return_if_error(...) \
  11297. do \
  11298. { \
  11299. if TOML_UNLIKELY(is_error()) \
  11300. return __VA_ARGS__; \
  11301. } \
  11302. while (false)
  11303. #define return_if_error_or_eof(...) \
  11304. do \
  11305. { \
  11306. if TOML_UNLIKELY(is_eof() || is_error()) \
  11307. return __VA_ARGS__; \
  11308. } \
  11309. while (false)
  11310. #endif
  11311. #if defined(TOML_BREAK_AT_PARSE_ERRORS) && TOML_BREAK_AT_PARSE_ERRORS
  11312. #if defined(__has_builtin)
  11313. #if __has_builtin(__builtin_debugtrap)
  11314. #define parse_error_break() __builtin_debugtrap()
  11315. #elif __has_builtin(__debugbreak)
  11316. #define parse_error_break() __debugbreak()
  11317. #endif
  11318. #endif
  11319. #ifndef parse_error_break
  11320. #if TOML_MSVC || TOML_ICC
  11321. #define parse_error_break() __debugbreak()
  11322. #else
  11323. #define parse_error_break() TOML_ASSERT(false)
  11324. #endif
  11325. #endif
  11326. #else
  11327. #define parse_error_break() static_assert(true)
  11328. #endif
  11329. #define set_error_and_return(ret, ...) \
  11330. do \
  11331. { \
  11332. if (!is_error()) \
  11333. set_error(__VA_ARGS__); \
  11334. return_after_error(ret); \
  11335. } \
  11336. while (false)
  11337. #define set_error_and_return_default(...) set_error_and_return({}, __VA_ARGS__)
  11338. #define set_error_and_return_if_eof(...) \
  11339. do \
  11340. { \
  11341. if TOML_UNLIKELY(is_eof()) \
  11342. set_error_and_return(__VA_ARGS__, "encountered end-of-file"sv); \
  11343. } \
  11344. while (false)
  11345. #define advance_and_return_if_error(...) \
  11346. do \
  11347. { \
  11348. assert_not_eof(); \
  11349. advance(); \
  11350. return_if_error(__VA_ARGS__); \
  11351. } \
  11352. while (false)
  11353. #define advance_and_return_if_error_or_eof(...) \
  11354. do \
  11355. { \
  11356. assert_not_eof(); \
  11357. advance(); \
  11358. return_if_error(__VA_ARGS__); \
  11359. set_error_and_return_if_eof(__VA_ARGS__); \
  11360. } \
  11361. while (false)
  11362. #endif // parser helper macros
  11363. TOML_IMPL_NAMESPACE_START
  11364. {
  11365. TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, impl_ex, impl_noex);
  11366. class parser
  11367. {
  11368. private:
  11369. static constexpr size_t max_nested_values = TOML_MAX_NESTED_VALUES;
  11370. utf8_buffered_reader reader;
  11371. table root;
  11372. source_position prev_pos = { 1, 1 };
  11373. const utf8_codepoint* cp = {};
  11374. std::vector<table*> implicit_tables;
  11375. std::vector<table*> dotted_key_tables;
  11376. std::vector<table*> open_inline_tables;
  11377. std::vector<array*> table_arrays;
  11378. parse_key_buffer key_buffer;
  11379. std::string string_buffer;
  11380. std::string recording_buffer; // for diagnostics
  11381. bool recording = false, recording_whitespace = true;
  11382. std::string_view current_scope;
  11383. size_t nested_values = {};
  11384. #if !TOML_EXCEPTIONS
  11385. mutable optional<parse_error> err;
  11386. #endif
  11387. TOML_NODISCARD
  11388. source_position current_position(source_index fallback_offset = 0) const noexcept
  11389. {
  11390. if (!is_eof())
  11391. return cp->position;
  11392. return { prev_pos.line, static_cast<source_index>(prev_pos.column + fallback_offset) };
  11393. }
  11394. template <typename... T>
  11395. TOML_RETURNS_BY_THROWING
  11396. TOML_NEVER_INLINE
  11397. void set_error_at(source_position pos, const T&... reason) const
  11398. {
  11399. static_assert(sizeof...(T) > 0);
  11400. return_if_error();
  11401. error_builder builder{ current_scope };
  11402. (builder.append(reason), ...);
  11403. parse_error_break();
  11404. #if TOML_EXCEPTIONS
  11405. builder.finish(pos, reader.source_path());
  11406. #else
  11407. err.emplace(builder.finish(pos, reader.source_path()));
  11408. #endif
  11409. }
  11410. template <typename... T>
  11411. TOML_RETURNS_BY_THROWING
  11412. void set_error(const T&... reason) const
  11413. {
  11414. set_error_at(current_position(1), reason...);
  11415. }
  11416. void go_back(size_t count = 1) noexcept
  11417. {
  11418. return_if_error();
  11419. TOML_ASSERT_ASSUME(count);
  11420. cp = reader.step_back(count);
  11421. prev_pos = cp->position;
  11422. }
  11423. void advance()
  11424. {
  11425. return_if_error();
  11426. assert_not_eof();
  11427. prev_pos = cp->position;
  11428. cp = reader.read_next();
  11429. #if !TOML_EXCEPTIONS
  11430. if (reader.error())
  11431. {
  11432. err = std::move(reader.error());
  11433. return;
  11434. }
  11435. #endif
  11436. if (recording && !is_eof())
  11437. {
  11438. if (recording_whitespace || !is_whitespace(*cp))
  11439. recording_buffer.append(cp->bytes, cp->count);
  11440. }
  11441. }
  11442. void start_recording(bool include_current = true) noexcept
  11443. {
  11444. return_if_error();
  11445. recording = true;
  11446. recording_whitespace = true;
  11447. recording_buffer.clear();
  11448. if (include_current && !is_eof())
  11449. recording_buffer.append(cp->bytes, cp->count);
  11450. }
  11451. void stop_recording(size_t pop_bytes = 0) noexcept
  11452. {
  11453. return_if_error();
  11454. recording = false;
  11455. if (pop_bytes)
  11456. {
  11457. if (pop_bytes >= recording_buffer.length())
  11458. recording_buffer.clear();
  11459. else if (pop_bytes == 1u)
  11460. recording_buffer.pop_back();
  11461. else
  11462. recording_buffer.erase(recording_buffer.begin()
  11463. + static_cast<ptrdiff_t>(recording_buffer.length() - pop_bytes),
  11464. recording_buffer.end());
  11465. }
  11466. }
  11467. bool consume_leading_whitespace()
  11468. {
  11469. return_if_error_or_eof({});
  11470. bool consumed = false;
  11471. while (!is_eof() && is_horizontal_whitespace(*cp))
  11472. {
  11473. if TOML_UNLIKELY(!is_ascii_horizontal_whitespace(*cp))
  11474. set_error_and_return_default("expected space or tab, saw '"sv, escaped_codepoint{ *cp }, "'"sv);
  11475. consumed = true;
  11476. advance_and_return_if_error({});
  11477. }
  11478. return consumed;
  11479. }
  11480. bool consume_line_break()
  11481. {
  11482. return_if_error_or_eof({});
  11483. if TOML_UNLIKELY(is_match(*cp, U'\v', U'\f'))
  11484. set_error_and_return_default(
  11485. R"(vertical tabs '\v' and form-feeds '\f' are not legal line breaks in TOML)"sv);
  11486. if (*cp == U'\r')
  11487. {
  11488. advance_and_return_if_error({}); // skip \r
  11489. if TOML_UNLIKELY(is_eof())
  11490. set_error_and_return_default("expected '\\n' after '\\r', saw EOF"sv);
  11491. if TOML_UNLIKELY(*cp != U'\n')
  11492. set_error_and_return_default("expected '\\n' after '\\r', saw '"sv,
  11493. escaped_codepoint{ *cp },
  11494. "'"sv);
  11495. }
  11496. else if (*cp != U'\n')
  11497. return false;
  11498. advance_and_return_if_error({}); // skip \n
  11499. return true;
  11500. }
  11501. bool consume_rest_of_line()
  11502. {
  11503. return_if_error_or_eof({});
  11504. do
  11505. {
  11506. if (is_ascii_vertical_whitespace(*cp))
  11507. return consume_line_break();
  11508. else
  11509. advance();
  11510. return_if_error({});
  11511. }
  11512. while (!is_eof());
  11513. return true;
  11514. }
  11515. bool consume_comment()
  11516. {
  11517. return_if_error_or_eof({});
  11518. if (*cp != U'#')
  11519. return false;
  11520. push_parse_scope("comment"sv);
  11521. advance_and_return_if_error({}); // skip the '#'
  11522. while (!is_eof())
  11523. {
  11524. if (consume_line_break())
  11525. return true;
  11526. return_if_error({});
  11527. #if TOML_LANG_AT_LEAST(1, 0, 0)
  11528. // toml/issues/567 (disallow non-TAB control characters in comments)
  11529. if TOML_UNLIKELY(is_nontab_control_character(*cp))
  11530. set_error_and_return_default(
  11531. "control characters other than TAB (U+0009) are explicitly prohibited in comments"sv);
  11532. // toml/pull/720 (disallow surrogates in comments)
  11533. else if TOML_UNLIKELY(is_unicode_surrogate(*cp))
  11534. set_error_and_return_default(
  11535. "unicode surrogates (U+D800 to U+DFFF) are explicitly prohibited in comments"sv);
  11536. #endif
  11537. advance_and_return_if_error({});
  11538. }
  11539. return true;
  11540. }
  11541. TOML_NODISCARD
  11542. bool consume_expected_sequence(std::u32string_view seq)
  11543. {
  11544. return_if_error({});
  11545. TOML_ASSERT(!seq.empty());
  11546. for (auto c : seq)
  11547. {
  11548. set_error_and_return_if_eof({});
  11549. if (*cp != c)
  11550. return false;
  11551. advance_and_return_if_error({});
  11552. }
  11553. return true;
  11554. }
  11555. template <typename T>
  11556. TOML_NODISCARD
  11557. bool consume_digit_sequence(T* digits, size_t len)
  11558. {
  11559. return_if_error({});
  11560. TOML_ASSERT_ASSUME(digits);
  11561. TOML_ASSERT_ASSUME(len);
  11562. for (size_t i = 0; i < len; i++)
  11563. {
  11564. set_error_and_return_if_eof({});
  11565. if (!is_decimal_digit(*cp))
  11566. return false;
  11567. digits[i] = static_cast<T>(*cp - U'0');
  11568. advance_and_return_if_error({});
  11569. }
  11570. return true;
  11571. }
  11572. template <typename T>
  11573. TOML_NODISCARD
  11574. size_t consume_variable_length_digit_sequence(T* buffer, size_t max_len)
  11575. {
  11576. return_if_error({});
  11577. TOML_ASSERT_ASSUME(buffer);
  11578. TOML_ASSERT_ASSUME(max_len);
  11579. size_t i = {};
  11580. for (; i < max_len; i++)
  11581. {
  11582. if (is_eof() || !is_decimal_digit(*cp))
  11583. break;
  11584. buffer[i] = static_cast<T>(*cp - U'0');
  11585. advance_and_return_if_error({});
  11586. }
  11587. return i;
  11588. }
  11589. TOML_NODISCARD
  11590. TOML_NEVER_INLINE
  11591. std::string_view parse_basic_string(bool multi_line)
  11592. {
  11593. return_if_error({});
  11594. assert_not_eof();
  11595. TOML_ASSERT_ASSUME(*cp == U'"');
  11596. push_parse_scope("string"sv);
  11597. // skip the '"'
  11598. advance_and_return_if_error_or_eof({});
  11599. // multi-line strings ignore a single line ending right at the beginning
  11600. if (multi_line)
  11601. {
  11602. consume_line_break();
  11603. return_if_error({});
  11604. set_error_and_return_if_eof({});
  11605. }
  11606. auto& str = string_buffer;
  11607. str.clear();
  11608. bool escaped = false;
  11609. bool skipping_whitespace = false;
  11610. do
  11611. {
  11612. if (escaped)
  11613. {
  11614. escaped = false;
  11615. // handle 'line ending slashes' in multi-line mode
  11616. if (multi_line && is_whitespace(*cp))
  11617. {
  11618. consume_leading_whitespace();
  11619. if TOML_UNLIKELY(!consume_line_break())
  11620. set_error_and_return_default(
  11621. "line-ending backslashes must be the last non-whitespace character on the line"sv);
  11622. skipping_whitespace = true;
  11623. return_if_error({});
  11624. continue;
  11625. }
  11626. bool skip_escaped_codepoint = true;
  11627. assert_not_eof();
  11628. switch (const auto escaped_codepoint = *cp)
  11629. {
  11630. // 'regular' escape codes
  11631. case U'b': str += '\b'; break;
  11632. case U'f': str += '\f'; break;
  11633. case U'n': str += '\n'; break;
  11634. case U'r': str += '\r'; break;
  11635. case U't': str += '\t'; break;
  11636. case U'"': str += '"'; break;
  11637. case U'\\': str += '\\'; break;
  11638. #if TOML_LANG_UNRELEASED // toml/pull/790 (\e shorthand for \x1B)
  11639. case U'e': str += '\x1B'; break;
  11640. #else
  11641. case U'e':
  11642. set_error_and_return_default(
  11643. "escape sequence '\\e' is not supported in TOML 1.0.0 and earlier"sv);
  11644. #endif
  11645. #if TOML_LANG_UNRELEASED // toml/pull/796 (\xHH unicode scalar sequences)
  11646. case U'x': [[fallthrough]];
  11647. #else
  11648. case U'x':
  11649. set_error_and_return_default(
  11650. "escape sequence '\\x' is not supported in TOML 1.0.0 and earlier"sv);
  11651. #endif
  11652. // unicode scalar sequences
  11653. case U'u': [[fallthrough]];
  11654. case U'U':
  11655. {
  11656. push_parse_scope("unicode scalar sequence"sv);
  11657. advance_and_return_if_error_or_eof({});
  11658. skip_escaped_codepoint = false;
  11659. uint32_t place_value =
  11660. escaped_codepoint == U'U' ? 0x10000000u : (escaped_codepoint == U'u' ? 0x1000u : 0x10u);
  11661. uint32_t sequence_value{};
  11662. while (place_value)
  11663. {
  11664. set_error_and_return_if_eof({});
  11665. if TOML_UNLIKELY(!is_hexadecimal_digit(*cp))
  11666. set_error_and_return_default("expected hex digit, saw '"sv, to_sv(*cp), "'"sv);
  11667. sequence_value += place_value * hex_to_dec(*cp);
  11668. place_value /= 16u;
  11669. advance_and_return_if_error({});
  11670. }
  11671. if TOML_UNLIKELY(is_unicode_surrogate(sequence_value))
  11672. set_error_and_return_default(
  11673. "unicode surrogates (U+D800 - U+DFFF) are explicitly prohibited"sv);
  11674. else if TOML_UNLIKELY(sequence_value > 0x10FFFFu)
  11675. set_error_and_return_default("values greater than U+10FFFF are invalid"sv);
  11676. if (sequence_value < 0x80)
  11677. {
  11678. str += static_cast<char>(sequence_value);
  11679. }
  11680. else if (sequence_value < 0x800u)
  11681. {
  11682. str += static_cast<char>((sequence_value >> 6) | 0xC0u);
  11683. str += static_cast<char>((sequence_value & 0x3Fu) | 0x80u);
  11684. }
  11685. else if (sequence_value < 0x10000u)
  11686. {
  11687. str += static_cast<char>((sequence_value >> 12) | 0xE0u);
  11688. str += static_cast<char>(((sequence_value >> 6) & 0x3Fu) | 0x80u);
  11689. str += static_cast<char>((sequence_value & 0x3Fu) | 0x80u);
  11690. }
  11691. else if (sequence_value < 0x110000u)
  11692. {
  11693. str += static_cast<char>((sequence_value >> 18) | 0xF0u);
  11694. str += static_cast<char>(((sequence_value >> 12) & 0x3Fu) | 0x80u);
  11695. str += static_cast<char>(((sequence_value >> 6) & 0x3Fu) | 0x80u);
  11696. str += static_cast<char>((sequence_value & 0x3Fu) | 0x80u);
  11697. }
  11698. break;
  11699. }
  11700. // ???
  11701. TOML_UNLIKELY_CASE
  11702. default: set_error_and_return_default("unknown escape sequence '\\"sv, to_sv(*cp), "'"sv);
  11703. }
  11704. if (skip_escaped_codepoint)
  11705. advance_and_return_if_error_or_eof({});
  11706. }
  11707. else
  11708. {
  11709. // handle closing delimiters
  11710. if (*cp == U'"')
  11711. {
  11712. if (multi_line)
  11713. {
  11714. size_t lookaheads = {};
  11715. size_t consecutive_delimiters = 1;
  11716. do
  11717. {
  11718. advance_and_return_if_error({});
  11719. lookaheads++;
  11720. if (!is_eof() && *cp == U'"')
  11721. consecutive_delimiters++;
  11722. else
  11723. break;
  11724. }
  11725. while (lookaheads < 4u);
  11726. switch (consecutive_delimiters)
  11727. {
  11728. // """ " (one quote somewhere in a ML string)
  11729. case 1:
  11730. str += '"';
  11731. skipping_whitespace = false;
  11732. continue;
  11733. // """ "" (two quotes somewhere in a ML string)
  11734. case 2:
  11735. str.append("\"\""sv);
  11736. skipping_whitespace = false;
  11737. continue;
  11738. // """ """ (the end of the string)
  11739. case 3: return str;
  11740. // """ """" (one at the end of the string)
  11741. case 4: str += '"'; return str;
  11742. // """ """"" (two quotes at the end of the string)
  11743. case 5:
  11744. str.append("\"\""sv);
  11745. advance_and_return_if_error({}); // skip the last '"'
  11746. return str;
  11747. default: TOML_UNREACHABLE;
  11748. }
  11749. }
  11750. else
  11751. {
  11752. advance_and_return_if_error({}); // skip the closing delimiter
  11753. return str;
  11754. }
  11755. }
  11756. // handle escapes
  11757. else if (*cp == U'\\')
  11758. {
  11759. advance_and_return_if_error_or_eof({}); // skip the '\'
  11760. skipping_whitespace = false;
  11761. escaped = true;
  11762. continue;
  11763. }
  11764. // handle line endings in multi-line mode
  11765. if (multi_line && is_ascii_vertical_whitespace(*cp))
  11766. {
  11767. consume_line_break();
  11768. return_if_error({});
  11769. if (!skipping_whitespace)
  11770. str += '\n';
  11771. continue;
  11772. }
  11773. // handle control characters
  11774. if TOML_UNLIKELY(is_nontab_control_character(*cp))
  11775. set_error_and_return_default(
  11776. "unescaped control characters other than TAB (U+0009) are explicitly prohibited"sv);
  11777. #if TOML_LANG_AT_LEAST(1, 0, 0)
  11778. // handle surrogates in strings
  11779. if TOML_UNLIKELY(is_unicode_surrogate(*cp))
  11780. set_error_and_return_default(
  11781. "unescaped unicode surrogates (U+D800 to U+DFFF) are explicitly prohibited"sv);
  11782. #endif
  11783. if (multi_line)
  11784. {
  11785. if (!skipping_whitespace || !is_horizontal_whitespace(*cp))
  11786. {
  11787. skipping_whitespace = false;
  11788. str.append(cp->bytes, cp->count);
  11789. }
  11790. }
  11791. else
  11792. str.append(cp->bytes, cp->count);
  11793. advance_and_return_if_error({});
  11794. }
  11795. }
  11796. while (!is_eof());
  11797. set_error_and_return_default("encountered end-of-file"sv);
  11798. }
  11799. TOML_NODISCARD
  11800. TOML_NEVER_INLINE
  11801. std::string_view parse_literal_string(bool multi_line)
  11802. {
  11803. return_if_error({});
  11804. assert_not_eof();
  11805. TOML_ASSERT_ASSUME(*cp == U'\'');
  11806. push_parse_scope("literal string"sv);
  11807. // skip the delimiter
  11808. advance_and_return_if_error_or_eof({});
  11809. // multi-line strings ignore a single line ending right at the beginning
  11810. if (multi_line)
  11811. {
  11812. consume_line_break();
  11813. return_if_error({});
  11814. set_error_and_return_if_eof({});
  11815. }
  11816. auto& str = string_buffer;
  11817. str.clear();
  11818. do
  11819. {
  11820. return_if_error({});
  11821. // handle closing delimiters
  11822. if (*cp == U'\'')
  11823. {
  11824. if (multi_line)
  11825. {
  11826. size_t lookaheads = {};
  11827. size_t consecutive_delimiters = 1;
  11828. do
  11829. {
  11830. advance_and_return_if_error({});
  11831. lookaheads++;
  11832. if (!is_eof() && *cp == U'\'')
  11833. consecutive_delimiters++;
  11834. else
  11835. break;
  11836. }
  11837. while (lookaheads < 4u);
  11838. switch (consecutive_delimiters)
  11839. {
  11840. // ''' ' (one quote somewhere in a ML string)
  11841. case 1: str += '\''; continue;
  11842. // ''' '' (two quotes somewhere in a ML string)
  11843. case 2: str.append("''"sv); continue;
  11844. // ''' ''' (the end of the string)
  11845. case 3: return str;
  11846. // ''' '''' (one at the end of the string)
  11847. case 4: str += '\''; return str;
  11848. // ''' ''''' (two quotes at the end of the string)
  11849. case 5:
  11850. str.append("''"sv);
  11851. advance_and_return_if_error({}); // skip the last '
  11852. return str;
  11853. default: TOML_UNREACHABLE;
  11854. }
  11855. }
  11856. else
  11857. {
  11858. advance_and_return_if_error({}); // skip the closing delimiter
  11859. return str;
  11860. }
  11861. }
  11862. // handle line endings in multi-line mode
  11863. if (multi_line && is_ascii_vertical_whitespace(*cp))
  11864. {
  11865. consume_line_break();
  11866. return_if_error({});
  11867. str += '\n';
  11868. continue;
  11869. }
  11870. // handle control characters
  11871. if TOML_UNLIKELY(is_nontab_control_character(*cp))
  11872. set_error_and_return_default(
  11873. "control characters other than TAB (U+0009) are explicitly prohibited"sv);
  11874. #if TOML_LANG_AT_LEAST(1, 0, 0)
  11875. // handle surrogates in strings
  11876. if TOML_UNLIKELY(is_unicode_surrogate(*cp))
  11877. set_error_and_return_default("unicode surrogates (U+D800 - U+DFFF) are explicitly prohibited"sv);
  11878. #endif
  11879. str.append(cp->bytes, cp->count);
  11880. advance_and_return_if_error({});
  11881. }
  11882. while (!is_eof());
  11883. set_error_and_return_default("encountered end-of-file"sv);
  11884. }
  11885. TOML_NODISCARD
  11886. TOML_NEVER_INLINE
  11887. parsed_string parse_string()
  11888. {
  11889. return_if_error({});
  11890. assert_not_eof();
  11891. TOML_ASSERT_ASSUME(is_string_delimiter(*cp));
  11892. push_parse_scope("string"sv);
  11893. // get the first three characters to determine the string type
  11894. const auto first = cp->value;
  11895. advance_and_return_if_error_or_eof({});
  11896. const auto second = cp->value;
  11897. advance_and_return_if_error({});
  11898. const auto third = cp ? cp->value : U'\0';
  11899. // if we were eof at the third character then first and second need to be
  11900. // the same string character (otherwise it's an unterminated string)
  11901. if (is_eof())
  11902. {
  11903. if (second == first)
  11904. return {};
  11905. set_error_and_return_default("encountered end-of-file"sv);
  11906. }
  11907. // if the first three characters are all the same string delimiter then
  11908. // it's a multi-line string.
  11909. else if (first == second && first == third)
  11910. {
  11911. return { first == U'\'' ? parse_literal_string(true) : parse_basic_string(true), true };
  11912. }
  11913. // otherwise it's just a regular string.
  11914. else
  11915. {
  11916. // step back two characters so that the current
  11917. // character is the string delimiter
  11918. go_back(2u);
  11919. return { first == U'\'' ? parse_literal_string(false) : parse_basic_string(false), false };
  11920. }
  11921. }
  11922. TOML_NODISCARD
  11923. TOML_NEVER_INLINE
  11924. std::string_view parse_bare_key_segment()
  11925. {
  11926. return_if_error({});
  11927. assert_not_eof();
  11928. TOML_ASSERT_ASSUME(is_bare_key_character(*cp));
  11929. string_buffer.clear();
  11930. while (!is_eof())
  11931. {
  11932. if (!is_bare_key_character(*cp))
  11933. break;
  11934. string_buffer.append(cp->bytes, cp->count);
  11935. advance_and_return_if_error({});
  11936. }
  11937. return string_buffer;
  11938. }
  11939. TOML_NODISCARD
  11940. TOML_NEVER_INLINE
  11941. bool parse_boolean()
  11942. {
  11943. return_if_error({});
  11944. assert_not_eof();
  11945. TOML_ASSERT_ASSUME(is_match(*cp, U't', U'f', U'T', U'F'));
  11946. push_parse_scope("boolean"sv);
  11947. start_recording(true);
  11948. auto result = is_match(*cp, U't', U'T');
  11949. if (!consume_expected_sequence(result ? U"true"sv : U"false"sv))
  11950. set_error_and_return_default("expected '"sv,
  11951. to_sv(result),
  11952. "', saw '"sv,
  11953. to_sv(recording_buffer),
  11954. "'"sv);
  11955. stop_recording();
  11956. if (cp && !is_value_terminator(*cp))
  11957. set_error_and_return_default("expected value-terminator, saw '"sv, to_sv(*cp), "'"sv);
  11958. return result;
  11959. }
  11960. TOML_NODISCARD
  11961. TOML_NEVER_INLINE
  11962. double parse_inf_or_nan()
  11963. {
  11964. return_if_error({});
  11965. assert_not_eof();
  11966. TOML_ASSERT_ASSUME(is_match(*cp, U'i', U'n', U'I', U'N', U'+', U'-'));
  11967. push_parse_scope("floating-point"sv);
  11968. start_recording(true);
  11969. const bool negative = *cp == U'-';
  11970. if (negative || *cp == U'+')
  11971. advance_and_return_if_error_or_eof({});
  11972. const bool inf = is_match(*cp, U'i', U'I');
  11973. if (!consume_expected_sequence(inf ? U"inf"sv : U"nan"sv))
  11974. set_error_and_return_default("expected '"sv,
  11975. inf ? "inf"sv : "nan"sv,
  11976. "', saw '"sv,
  11977. to_sv(recording_buffer),
  11978. "'"sv);
  11979. stop_recording();
  11980. if (cp && !is_value_terminator(*cp))
  11981. set_error_and_return_default("expected value-terminator, saw '"sv, to_sv(*cp), "'"sv);
  11982. return inf ? (negative ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity())
  11983. : std::numeric_limits<double>::quiet_NaN();
  11984. }
  11985. TOML_NODISCARD
  11986. TOML_NEVER_INLINE
  11987. double parse_float()
  11988. {
  11989. return_if_error({});
  11990. assert_not_eof();
  11991. TOML_ASSERT_ASSUME(is_match(*cp, U'+', U'-', U'.') || is_decimal_digit(*cp));
  11992. push_parse_scope("floating-point"sv);
  11993. // sign
  11994. const int sign = *cp == U'-' ? -1 : 1;
  11995. if (is_match(*cp, U'+', U'-'))
  11996. advance_and_return_if_error_or_eof({});
  11997. // consume value chars
  11998. char chars[utf8_buffered_reader::max_history_length];
  11999. size_t length = {};
  12000. const utf8_codepoint* prev = {};
  12001. bool seen_decimal = false, seen_exponent = false;
  12002. char first_integer_part = '\0';
  12003. while (!is_eof() && !is_value_terminator(*cp))
  12004. {
  12005. if (*cp == U'_')
  12006. {
  12007. if (!prev || !is_decimal_digit(*prev))
  12008. set_error_and_return_default("underscores may only follow digits"sv);
  12009. prev = cp;
  12010. advance_and_return_if_error_or_eof({});
  12011. continue;
  12012. }
  12013. else if TOML_UNLIKELY(prev && *prev == U'_' && !is_decimal_digit(*cp))
  12014. set_error_and_return_default("underscores must be followed by digits"sv);
  12015. else if TOML_UNLIKELY(length == sizeof(chars))
  12016. set_error_and_return_default("exceeds length limit of "sv,
  12017. sizeof(chars),
  12018. " digits"sv,
  12019. (seen_exponent ? ""sv : " (consider using exponent notation)"sv));
  12020. else if (*cp == U'.')
  12021. {
  12022. // .1
  12023. // -.1
  12024. // +.1 (no integer part)
  12025. if (!first_integer_part)
  12026. set_error_and_return_default("expected decimal digit, saw '.'"sv);
  12027. // 1.0e+.10 (exponent cannot have '.')
  12028. else if (seen_exponent)
  12029. set_error_and_return_default("expected exponent decimal digit or sign, saw '.'"sv);
  12030. // 1.0.e+.10
  12031. // 1..0
  12032. // (multiple '.')
  12033. else if (seen_decimal)
  12034. set_error_and_return_default("expected decimal digit or exponent, saw '.'"sv);
  12035. seen_decimal = true;
  12036. }
  12037. else if (is_match(*cp, U'e', U'E'))
  12038. {
  12039. if (prev && !is_decimal_digit(*prev))
  12040. set_error_and_return_default("expected decimal digit, saw '"sv, to_sv(*cp), "'"sv);
  12041. // 1.0ee+10 (multiple 'e')
  12042. else if (seen_exponent)
  12043. set_error_and_return_default("expected decimal digit, saw '"sv, to_sv(*cp), "'"sv);
  12044. seen_decimal = true; // implied
  12045. seen_exponent = true;
  12046. }
  12047. else if (is_match(*cp, U'+', U'-'))
  12048. {
  12049. // 1.-0 (sign in mantissa)
  12050. if (!seen_exponent)
  12051. set_error_and_return_default("expected decimal digit or '.', saw '"sv, to_sv(*cp), "'"sv);
  12052. // 1.0e1-0 (misplaced exponent sign)
  12053. else if (!is_match(*prev, U'e', U'E'))
  12054. set_error_and_return_default("expected exponent digit, saw '"sv, to_sv(*cp), "'"sv);
  12055. }
  12056. else if (is_decimal_digit(*cp))
  12057. {
  12058. if (!seen_decimal)
  12059. {
  12060. if (!first_integer_part)
  12061. first_integer_part = static_cast<char>(cp->bytes[0]);
  12062. else if (first_integer_part == '0')
  12063. set_error_and_return_default("leading zeroes are prohibited"sv);
  12064. }
  12065. }
  12066. else
  12067. set_error_and_return_default("expected decimal digit, saw '"sv, to_sv(*cp), "'"sv);
  12068. chars[length++] = static_cast<char>(cp->bytes[0]);
  12069. prev = cp;
  12070. advance_and_return_if_error({});
  12071. }
  12072. // sanity-check ending state
  12073. if (prev)
  12074. {
  12075. if (*prev == U'_')
  12076. {
  12077. set_error_and_return_if_eof({});
  12078. set_error_and_return_default("underscores must be followed by digits"sv);
  12079. }
  12080. else if (is_match(*prev, U'e', U'E', U'+', U'-', U'.'))
  12081. {
  12082. set_error_and_return_if_eof({});
  12083. set_error_and_return_default("expected decimal digit, saw '"sv, to_sv(*cp), "'"sv);
  12084. }
  12085. }
  12086. // convert to double
  12087. double result;
  12088. #if TOML_FLOAT_CHARCONV
  12089. {
  12090. auto fc_result = std::from_chars(chars, chars + length, result);
  12091. switch (fc_result.ec)
  12092. {
  12093. TOML_LIKELY_CASE
  12094. case std::errc{}: // ok
  12095. return result * sign;
  12096. case std::errc::invalid_argument:
  12097. set_error_and_return_default("'"sv,
  12098. std::string_view{ chars, length },
  12099. "' could not be interpreted as a value"sv);
  12100. break;
  12101. case std::errc::result_out_of_range:
  12102. set_error_and_return_default("'"sv,
  12103. std::string_view{ chars, length },
  12104. "' is not representable in 64 bits"sv);
  12105. break;
  12106. default: //??
  12107. set_error_and_return_default("an unspecified error occurred while trying to interpret '"sv,
  12108. std::string_view{ chars, length },
  12109. "' as a value"sv);
  12110. }
  12111. }
  12112. #else
  12113. {
  12114. std::stringstream ss;
  12115. ss.imbue(std::locale::classic());
  12116. ss.write(chars, static_cast<std::streamsize>(length));
  12117. if ((ss >> result))
  12118. return result * sign;
  12119. else
  12120. set_error_and_return_default("'"sv,
  12121. std::string_view{ chars, length },
  12122. "' could not be interpreted as a value"sv);
  12123. }
  12124. #endif
  12125. }
  12126. TOML_NODISCARD
  12127. TOML_NEVER_INLINE
  12128. double parse_hex_float()
  12129. {
  12130. return_if_error({});
  12131. assert_not_eof();
  12132. TOML_ASSERT_ASSUME(is_match(*cp, U'0', U'+', U'-'));
  12133. push_parse_scope("hexadecimal floating-point"sv);
  12134. #if TOML_LANG_UNRELEASED // toml/issues/562 (hexfloats)
  12135. // sign
  12136. const int sign = *cp == U'-' ? -1 : 1;
  12137. if (is_match(*cp, U'+', U'-'))
  12138. advance_and_return_if_error_or_eof({});
  12139. // '0'
  12140. if (*cp != U'0')
  12141. set_error_and_return_default(" expected '0', saw '"sv, to_sv(*cp), "'"sv);
  12142. advance_and_return_if_error_or_eof({});
  12143. // 'x' or 'X'
  12144. if (!is_match(*cp, U'x', U'X'))
  12145. set_error_and_return_default("expected 'x' or 'X', saw '"sv, to_sv(*cp), "'"sv);
  12146. advance_and_return_if_error_or_eof({});
  12147. // <HEX DIGITS> ([.]<HEX DIGITS>)? [pP] [+-]? <DEC DIGITS>
  12148. // consume value fragments
  12149. struct fragment
  12150. {
  12151. char chars[24];
  12152. size_t length;
  12153. double value;
  12154. };
  12155. fragment fragments[] = {
  12156. {}, // mantissa, whole part
  12157. {}, // mantissa, fractional part
  12158. {} // exponent
  12159. };
  12160. fragment* current_fragment = fragments;
  12161. const utf8_codepoint* prev = {};
  12162. int exponent_sign = 1;
  12163. while (!is_eof() && !is_value_terminator(*cp))
  12164. {
  12165. if (*cp == U'_')
  12166. {
  12167. if (!prev || !is_hexadecimal_digit(*prev))
  12168. set_error_and_return_default("underscores may only follow digits"sv);
  12169. prev = cp;
  12170. advance_and_return_if_error_or_eof({});
  12171. continue;
  12172. }
  12173. else if (prev && *prev == U'_' && !is_hexadecimal_digit(*cp))
  12174. set_error_and_return_default("underscores must be followed by digits"sv);
  12175. else if (*cp == U'.')
  12176. {
  12177. // 0x10.0p-.0 (exponent cannot have '.')
  12178. if (current_fragment == fragments + 2)
  12179. set_error_and_return_default("expected exponent digit or sign, saw '.'"sv);
  12180. // 0x10.0.p-0 (multiple '.')
  12181. else if (current_fragment == fragments + 1)
  12182. set_error_and_return_default("expected hexadecimal digit or exponent, saw '.'"sv);
  12183. else
  12184. current_fragment++;
  12185. }
  12186. else if (is_match(*cp, U'p', U'P'))
  12187. {
  12188. // 0x10.0pp-0 (multiple 'p')
  12189. if (current_fragment == fragments + 2)
  12190. set_error_and_return_default("expected exponent digit or sign, saw '"sv, to_sv(*cp), "'"sv);
  12191. // 0x.p-0 (mantissa is just '.')
  12192. else if (fragments[0].length == 0u && fragments[1].length == 0u)
  12193. set_error_and_return_default("expected hexadecimal digit, saw '"sv, to_sv(*cp), "'"sv);
  12194. else
  12195. current_fragment = fragments + 2;
  12196. }
  12197. else if (is_match(*cp, U'+', U'-'))
  12198. {
  12199. // 0x-10.0p-0 (sign in mantissa)
  12200. if (current_fragment != fragments + 2)
  12201. set_error_and_return_default("expected hexadecimal digit or '.', saw '"sv, to_sv(*cp), "'"sv);
  12202. // 0x10.0p0- (misplaced exponent sign)
  12203. else if (!is_match(*prev, U'p', U'P'))
  12204. set_error_and_return_default("expected exponent digit, saw '"sv, to_sv(*cp), "'"sv);
  12205. else
  12206. exponent_sign = *cp == U'-' ? -1 : 1;
  12207. }
  12208. else if (current_fragment < fragments + 2 && !is_hexadecimal_digit(*cp))
  12209. set_error_and_return_default("expected hexadecimal digit or '.', saw '"sv, to_sv(*cp), "'"sv);
  12210. else if (current_fragment == fragments + 2 && !is_decimal_digit(*cp))
  12211. set_error_and_return_default("expected exponent digit or sign, saw '"sv, to_sv(*cp), "'"sv);
  12212. else if (current_fragment->length == sizeof(fragment::chars))
  12213. set_error_and_return_default("fragment exceeeds maximum length of "sv,
  12214. sizeof(fragment::chars),
  12215. " characters"sv);
  12216. else
  12217. current_fragment->chars[current_fragment->length++] = static_cast<char>(cp->bytes[0]);
  12218. prev = cp;
  12219. advance_and_return_if_error({});
  12220. }
  12221. // sanity-check ending state
  12222. if (current_fragment != fragments + 2 || current_fragment->length == 0u)
  12223. {
  12224. set_error_and_return_if_eof({});
  12225. set_error_and_return_default("missing exponent"sv);
  12226. }
  12227. else if (prev && *prev == U'_')
  12228. {
  12229. set_error_and_return_if_eof({});
  12230. set_error_and_return_default("underscores must be followed by digits"sv);
  12231. }
  12232. // calculate values for the three fragments
  12233. for (int fragment_idx = 0; fragment_idx < 3; fragment_idx++)
  12234. {
  12235. auto& f = fragments[fragment_idx];
  12236. const uint32_t base = fragment_idx == 2 ? 10u : 16u;
  12237. // left-trim zeroes
  12238. const char* c = f.chars;
  12239. size_t sig = {};
  12240. while (f.length && *c == '0')
  12241. {
  12242. f.length--;
  12243. c++;
  12244. sig++;
  12245. }
  12246. if (!f.length)
  12247. continue;
  12248. // calculate value
  12249. auto place = 1u;
  12250. for (size_t i = 0; i < f.length - 1u; i++)
  12251. place *= base;
  12252. uint32_t val{};
  12253. while (place)
  12254. {
  12255. if (base == 16)
  12256. val += place * hex_to_dec(*c);
  12257. else
  12258. val += place * static_cast<uint32_t>(*c - '0');
  12259. if (fragment_idx == 1)
  12260. sig++;
  12261. c++;
  12262. place /= base;
  12263. }
  12264. f.value = static_cast<double>(val);
  12265. // shift the fractional part
  12266. if (fragment_idx == 1)
  12267. {
  12268. while (sig--)
  12269. f.value /= base;
  12270. }
  12271. }
  12272. return (fragments[0].value + fragments[1].value) * pow(2.0, fragments[2].value * exponent_sign) * sign;
  12273. #else // !TOML_LANG_UNRELEASED
  12274. set_error_and_return_default("hexadecimal floating-point values are not supported "
  12275. "in TOML 1.0.0 and earlier"sv);
  12276. #endif // !TOML_LANG_UNRELEASED
  12277. }
  12278. template <uint64_t base>
  12279. TOML_NODISCARD
  12280. TOML_NEVER_INLINE
  12281. int64_t parse_integer()
  12282. {
  12283. return_if_error({});
  12284. assert_not_eof();
  12285. using traits = parse_integer_traits<base>;
  12286. push_parse_scope(traits::scope_qualifier);
  12287. [[maybe_unused]] int64_t sign = 1;
  12288. if constexpr (traits::is_signed)
  12289. {
  12290. sign = *cp == U'-' ? -1 : 1;
  12291. if (is_match(*cp, U'+', U'-'))
  12292. advance_and_return_if_error_or_eof({});
  12293. }
  12294. if constexpr (base == 10)
  12295. {
  12296. if (!traits::is_digit(*cp))
  12297. set_error_and_return_default("expected expected digit or sign, saw '"sv, to_sv(*cp), "'"sv);
  12298. }
  12299. else
  12300. {
  12301. // '0'
  12302. if (*cp != U'0')
  12303. set_error_and_return_default("expected '0', saw '"sv, to_sv(*cp), "'"sv);
  12304. advance_and_return_if_error_or_eof({});
  12305. // 'b', 'o', 'x'
  12306. if (*cp != traits::prefix_codepoint)
  12307. set_error_and_return_default("expected '"sv, traits::prefix, "', saw '"sv, to_sv(*cp), "'"sv);
  12308. advance_and_return_if_error_or_eof({});
  12309. if (!traits::is_digit(*cp))
  12310. set_error_and_return_default("expected digit, saw '"sv, to_sv(*cp), "'"sv);
  12311. }
  12312. // consume digits
  12313. char digits[utf8_buffered_reader::max_history_length];
  12314. size_t length = {};
  12315. const utf8_codepoint* prev = {};
  12316. while (!is_eof() && !is_value_terminator(*cp))
  12317. {
  12318. if (*cp == U'_')
  12319. {
  12320. if (!prev || !traits::is_digit(*prev))
  12321. set_error_and_return_default("underscores may only follow digits"sv);
  12322. prev = cp;
  12323. advance_and_return_if_error_or_eof({});
  12324. continue;
  12325. }
  12326. else if TOML_UNLIKELY(prev && *prev == U'_' && !traits::is_digit(*cp))
  12327. set_error_and_return_default("underscores must be followed by digits"sv);
  12328. else if TOML_UNLIKELY(!traits::is_digit(*cp))
  12329. set_error_and_return_default("expected digit, saw '"sv, to_sv(*cp), "'"sv);
  12330. else if TOML_UNLIKELY(length == sizeof(digits))
  12331. set_error_and_return_default("exceeds length limit of "sv, sizeof(digits), " digits"sv);
  12332. else
  12333. digits[length++] = static_cast<char>(cp->bytes[0]);
  12334. prev = cp;
  12335. advance_and_return_if_error({});
  12336. }
  12337. // sanity check ending state
  12338. if (prev && *prev == U'_')
  12339. {
  12340. set_error_and_return_if_eof({});
  12341. set_error_and_return_default("underscores must be followed by digits"sv);
  12342. }
  12343. // single digits can be converted trivially
  12344. if (length == 1u)
  12345. {
  12346. int64_t result;
  12347. if constexpr (base == 16)
  12348. result = static_cast<int64_t>(hex_to_dec(digits[0]));
  12349. else
  12350. result = static_cast<int64_t>(digits[0] - '0');
  12351. if constexpr (traits::is_signed)
  12352. result *= sign;
  12353. return result;
  12354. }
  12355. // bin, oct and hex allow leading zeroes so trim them first
  12356. const char* end = digits + length;
  12357. const char* msd = digits;
  12358. if constexpr (base != 10)
  12359. {
  12360. while (msd < end && *msd == '0')
  12361. msd++;
  12362. if (msd == end)
  12363. return 0ll;
  12364. }
  12365. // decimal integers do not allow leading zeroes
  12366. else
  12367. {
  12368. if TOML_UNLIKELY(digits[0] == '0')
  12369. set_error_and_return_default("leading zeroes are prohibited"sv);
  12370. }
  12371. // range check
  12372. if TOML_UNLIKELY(static_cast<size_t>(end - msd) > traits::max_digits)
  12373. set_error_and_return_default("'"sv,
  12374. traits::full_prefix,
  12375. std::string_view{ digits, length },
  12376. "' is not representable in 64 bits"sv);
  12377. // do the thing
  12378. {
  12379. uint64_t result = {};
  12380. {
  12381. uint64_t power = 1;
  12382. while (--end >= msd)
  12383. {
  12384. if constexpr (base == 16)
  12385. result += power * hex_to_dec(*end);
  12386. else
  12387. result += power * static_cast<uint64_t>(*end - '0');
  12388. power *= base;
  12389. }
  12390. }
  12391. // range check
  12392. static constexpr auto i64_max = static_cast<uint64_t>((std::numeric_limits<int64_t>::max)());
  12393. if TOML_UNLIKELY(result > i64_max + (sign < 0 ? 1u : 0u))
  12394. set_error_and_return_default("'"sv,
  12395. traits::full_prefix,
  12396. std::string_view{ digits, length },
  12397. "' is not representable in 64 bits"sv);
  12398. if constexpr (traits::is_signed)
  12399. {
  12400. // avoid signed multiply UB when parsing INT64_MIN
  12401. if TOML_UNLIKELY(sign < 0 && result == i64_max + 1u)
  12402. return (std::numeric_limits<int64_t>::min)();
  12403. return static_cast<int64_t>(result) * sign;
  12404. }
  12405. else
  12406. return static_cast<int64_t>(result);
  12407. }
  12408. }
  12409. TOML_NODISCARD
  12410. TOML_NEVER_INLINE
  12411. date parse_date(bool part_of_datetime = false)
  12412. {
  12413. return_if_error({});
  12414. assert_not_eof();
  12415. TOML_ASSERT_ASSUME(is_decimal_digit(*cp));
  12416. push_parse_scope("date"sv);
  12417. // "YYYY"
  12418. uint32_t digits[4];
  12419. if (!consume_digit_sequence(digits, 4u))
  12420. set_error_and_return_default("expected 4-digit year, saw '"sv, to_sv(cp), "'"sv);
  12421. const auto year = digits[3] + digits[2] * 10u + digits[1] * 100u + digits[0] * 1000u;
  12422. const auto is_leap_year = (year % 4u == 0u) && ((year % 100u != 0u) || (year % 400u == 0u));
  12423. set_error_and_return_if_eof({});
  12424. // '-'
  12425. if (*cp != U'-')
  12426. set_error_and_return_default("expected '-', saw '"sv, to_sv(*cp), "'"sv);
  12427. advance_and_return_if_error_or_eof({});
  12428. // "MM"
  12429. if (!consume_digit_sequence(digits, 2u))
  12430. set_error_and_return_default("expected 2-digit month, saw '"sv, to_sv(cp), "'"sv);
  12431. const auto month = digits[1] + digits[0] * 10u;
  12432. if (month == 0u || month > 12u)
  12433. set_error_and_return_default("expected month between 1 and 12 (inclusive), saw "sv, month);
  12434. const auto max_days_in_month = month == 2u
  12435. ? (is_leap_year ? 29u : 28u)
  12436. : (month == 4u || month == 6u || month == 9u || month == 11u ? 30u : 31u);
  12437. set_error_and_return_if_eof({});
  12438. // '-'
  12439. if (*cp != U'-')
  12440. set_error_and_return_default("expected '-', saw '"sv, to_sv(*cp), "'"sv);
  12441. advance_and_return_if_error_or_eof({});
  12442. // "DD"
  12443. if (!consume_digit_sequence(digits, 2u))
  12444. set_error_and_return_default("expected 2-digit day, saw '"sv, to_sv(cp), "'"sv);
  12445. const auto day = digits[1] + digits[0] * 10u;
  12446. if (day == 0u || day > max_days_in_month)
  12447. set_error_and_return_default("expected day between 1 and "sv,
  12448. max_days_in_month,
  12449. " (inclusive), saw "sv,
  12450. day);
  12451. if (!part_of_datetime && !is_eof() && !is_value_terminator(*cp))
  12452. set_error_and_return_default("expected value-terminator, saw '"sv, to_sv(*cp), "'"sv);
  12453. return { year, month, day };
  12454. }
  12455. TOML_NODISCARD
  12456. TOML_NEVER_INLINE
  12457. time parse_time(bool part_of_datetime = false)
  12458. {
  12459. return_if_error({});
  12460. assert_not_eof();
  12461. TOML_ASSERT_ASSUME(is_decimal_digit(*cp));
  12462. push_parse_scope("time"sv);
  12463. static constexpr size_t max_digits = 64; // far more than necessary but needed to allow fractional
  12464. // millisecond truncation per the spec
  12465. uint32_t digits[max_digits];
  12466. // "HH"
  12467. if (!consume_digit_sequence(digits, 2u))
  12468. set_error_and_return_default("expected 2-digit hour, saw '"sv, to_sv(cp), "'"sv);
  12469. const auto hour = digits[1] + digits[0] * 10u;
  12470. if (hour > 23u)
  12471. set_error_and_return_default("expected hour between 0 to 59 (inclusive), saw "sv, hour);
  12472. set_error_and_return_if_eof({});
  12473. // ':'
  12474. if (*cp != U':')
  12475. set_error_and_return_default("expected ':', saw '"sv, to_sv(*cp), "'"sv);
  12476. advance_and_return_if_error_or_eof({});
  12477. // "MM"
  12478. if (!consume_digit_sequence(digits, 2u))
  12479. set_error_and_return_default("expected 2-digit minute, saw '"sv, to_sv(cp), "'"sv);
  12480. const auto minute = digits[1] + digits[0] * 10u;
  12481. if (minute > 59u)
  12482. set_error_and_return_default("expected minute between 0 and 59 (inclusive), saw "sv, minute);
  12483. auto time = toml::time{ hour, minute };
  12484. // ':'
  12485. if constexpr (TOML_LANG_UNRELEASED) // toml/issues/671 (allow omission of seconds)
  12486. {
  12487. if (is_eof() || is_value_terminator(*cp) || (part_of_datetime && is_match(*cp, U'+', U'-', U'Z', U'z')))
  12488. return time;
  12489. }
  12490. else
  12491. set_error_and_return_if_eof({});
  12492. if (*cp != U':')
  12493. set_error_and_return_default("expected ':', saw '"sv, to_sv(*cp), "'"sv);
  12494. advance_and_return_if_error_or_eof({});
  12495. // "SS"
  12496. if (!consume_digit_sequence(digits, 2u))
  12497. set_error_and_return_default("expected 2-digit second, saw '"sv, to_sv(cp), "'"sv);
  12498. const auto second = digits[1] + digits[0] * 10u;
  12499. if (second > 59u)
  12500. set_error_and_return_default("expected second between 0 and 59 (inclusive), saw "sv, second);
  12501. time.second = static_cast<decltype(time.second)>(second);
  12502. // '.' (early-exiting is allowed; fractional is optional)
  12503. if (is_eof() || is_value_terminator(*cp) || (part_of_datetime && is_match(*cp, U'+', U'-', U'Z', U'z')))
  12504. return time;
  12505. if (*cp != U'.')
  12506. set_error_and_return_default("expected '.', saw '"sv, to_sv(*cp), "'"sv);
  12507. advance_and_return_if_error_or_eof({});
  12508. // "FFFFFFFFF"
  12509. size_t digit_count = consume_variable_length_digit_sequence(digits, max_digits);
  12510. if (!digit_count)
  12511. {
  12512. set_error_and_return_if_eof({});
  12513. set_error_and_return_default("expected fractional digits, saw '"sv, to_sv(*cp), "'"sv);
  12514. }
  12515. else if (!is_eof())
  12516. {
  12517. if (digit_count == max_digits && is_decimal_digit(*cp))
  12518. set_error_and_return_default("fractional component exceeds maximum precision of "sv, max_digits);
  12519. else if (!part_of_datetime && !is_value_terminator(*cp))
  12520. set_error_and_return_default("expected value-terminator, saw '"sv, to_sv(*cp), "'"sv);
  12521. }
  12522. uint32_t value = 0u;
  12523. uint32_t place = 1u;
  12524. for (auto i = impl::min<size_t>(digit_count, 9u); i-- > 0u;)
  12525. {
  12526. value += digits[i] * place;
  12527. place *= 10u;
  12528. }
  12529. for (auto i = digit_count; i < 9u; i++) // implicit zeros
  12530. value *= 10u;
  12531. time.nanosecond = value;
  12532. return time;
  12533. }
  12534. TOML_NODISCARD
  12535. TOML_NEVER_INLINE
  12536. date_time parse_date_time()
  12537. {
  12538. return_if_error({});
  12539. assert_not_eof();
  12540. TOML_ASSERT_ASSUME(is_decimal_digit(*cp));
  12541. push_parse_scope("date-time"sv);
  12542. // "YYYY-MM-DD"
  12543. auto date = parse_date(true);
  12544. set_error_and_return_if_eof({});
  12545. // ' ', 'T' or 't'
  12546. if (!is_match(*cp, U' ', U'T', U't'))
  12547. set_error_and_return_default("expected space, 'T' or 't', saw '"sv, to_sv(*cp), "'"sv);
  12548. advance_and_return_if_error_or_eof({});
  12549. // "HH:MM:SS.FFFFFFFFF"
  12550. auto time = parse_time(true);
  12551. return_if_error({});
  12552. // no offset
  12553. if (is_eof() || is_value_terminator(*cp))
  12554. return { date, time };
  12555. // zero offset ('Z' or 'z')
  12556. time_offset offset{};
  12557. if (is_match(*cp, U'Z', U'z'))
  12558. advance_and_return_if_error({});
  12559. // explicit offset ("+/-HH:MM")
  12560. else if (is_match(*cp, U'+', U'-'))
  12561. {
  12562. push_parse_scope("date-time offset"sv);
  12563. // sign
  12564. int sign = *cp == U'-' ? -1 : 1;
  12565. advance_and_return_if_error_or_eof({});
  12566. // "HH"
  12567. int digits[2];
  12568. if (!consume_digit_sequence(digits, 2u))
  12569. set_error_and_return_default("expected 2-digit hour, saw '"sv, to_sv(cp), "'"sv);
  12570. const auto hour = digits[1] + digits[0] * 10;
  12571. if (hour > 23)
  12572. set_error_and_return_default("expected hour between 0 and 23 (inclusive), saw "sv, hour);
  12573. set_error_and_return_if_eof({});
  12574. // ':'
  12575. if (*cp != U':')
  12576. set_error_and_return_default("expected ':', saw '"sv, to_sv(*cp), "'"sv);
  12577. advance_and_return_if_error_or_eof({});
  12578. // "MM"
  12579. if (!consume_digit_sequence(digits, 2u))
  12580. set_error_and_return_default("expected 2-digit minute, saw '"sv, to_sv(cp), "'"sv);
  12581. const auto minute = digits[1] + digits[0] * 10;
  12582. if (minute > 59)
  12583. set_error_and_return_default("expected minute between 0 and 59 (inclusive), saw "sv, minute);
  12584. offset.minutes = static_cast<decltype(offset.minutes)>((hour * 60 + minute) * sign);
  12585. }
  12586. if (!is_eof() && !is_value_terminator(*cp))
  12587. set_error_and_return_default("expected value-terminator, saw '"sv, to_sv(*cp), "'"sv);
  12588. return { date, time, offset };
  12589. }
  12590. TOML_NODISCARD
  12591. node_ptr parse_array();
  12592. TOML_NODISCARD
  12593. node_ptr parse_inline_table();
  12594. TOML_NODISCARD
  12595. node_ptr parse_value_known_prefixes()
  12596. {
  12597. return_if_error({});
  12598. assert_not_eof();
  12599. TOML_ASSERT_ASSUME(!is_control_character(*cp));
  12600. TOML_ASSERT_ASSUME(*cp != U'_');
  12601. switch (cp->value)
  12602. {
  12603. // arrays
  12604. case U'[': return parse_array();
  12605. // inline tables
  12606. case U'{': return parse_inline_table();
  12607. // floats beginning with '.'
  12608. case U'.': return node_ptr{ new value{ parse_float() } };
  12609. // strings
  12610. case U'"': [[fallthrough]];
  12611. case U'\'': return node_ptr{ new value{ parse_string().value } };
  12612. default:
  12613. {
  12614. const auto cp_upper = static_cast<uint_least32_t>(cp->value) & ~0x20u;
  12615. // bools
  12616. if (cp_upper == 70u || cp_upper == 84u) // F or T
  12617. return node_ptr{ new value{ parse_boolean() } };
  12618. // inf/nan
  12619. else if (cp_upper == 73u || cp_upper == 78u) // I or N
  12620. return node_ptr{ new value{ parse_inf_or_nan() } };
  12621. else
  12622. return nullptr;
  12623. }
  12624. }
  12625. TOML_UNREACHABLE;
  12626. }
  12627. TOML_NODISCARD
  12628. node_ptr parse_value()
  12629. {
  12630. return_if_error({});
  12631. assert_not_eof();
  12632. TOML_ASSERT_ASSUME(!is_value_terminator(*cp));
  12633. push_parse_scope("value"sv);
  12634. const depth_counter_scope depth_counter{ nested_values };
  12635. if TOML_UNLIKELY(nested_values > max_nested_values)
  12636. set_error_and_return_default("exceeded maximum nested value depth of "sv,
  12637. max_nested_values,
  12638. " (TOML_MAX_NESTED_VALUES)"sv);
  12639. // check if it begins with some control character
  12640. // (note that this will also fail for whitespace but we're assuming we've
  12641. // called consume_leading_whitespace() before calling parse_value())
  12642. if TOML_UNLIKELY(is_control_character(*cp))
  12643. set_error_and_return_default("unexpected control character"sv);
  12644. // underscores at the beginning
  12645. else if (*cp == U'_')
  12646. set_error_and_return_default("values may not begin with underscores"sv);
  12647. const auto begin_pos = cp->position;
  12648. node_ptr val;
  12649. do
  12650. {
  12651. TOML_ASSERT_ASSUME(!is_control_character(*cp));
  12652. TOML_ASSERT_ASSUME(*cp != U'_');
  12653. // detect the value type and parse accordingly,
  12654. // starting with value types that can be detected
  12655. // unambiguously from just one character.
  12656. val = parse_value_known_prefixes();
  12657. return_if_error({});
  12658. if (val)
  12659. break;
  12660. // value types from here down require more than one character to unambiguously identify
  12661. // so scan ahead and collect a set of value 'traits'.
  12662. enum TOML_CLOSED_FLAGS_ENUM value_traits : int
  12663. {
  12664. has_nothing = 0,
  12665. has_digits = 1,
  12666. has_b = 1 << 1, // as second char only (0b)
  12667. has_e = 1 << 2, // only float exponents
  12668. has_o = 1 << 3, // as second char only (0o)
  12669. has_p = 1 << 4, // only hexfloat exponents
  12670. has_t = 1 << 5,
  12671. has_x = 1 << 6, // as second or third char only (0x, -0x, +0x)
  12672. has_z = 1 << 7,
  12673. has_colon = 1 << 8,
  12674. has_plus = 1 << 9,
  12675. has_minus = 1 << 10,
  12676. has_dot = 1 << 11,
  12677. begins_sign = 1 << 12,
  12678. begins_digit = 1 << 13,
  12679. begins_zero = 1 << 14,
  12680. signs_msk = has_plus | has_minus,
  12681. bdigit_msk = has_digits | begins_digit,
  12682. bzero_msk = bdigit_msk | begins_zero,
  12683. };
  12684. value_traits traits = has_nothing;
  12685. const auto has_any = [&](auto t) noexcept { return (traits & t) != has_nothing; };
  12686. const auto has_none = [&](auto t) noexcept { return (traits & t) == has_nothing; };
  12687. const auto add_trait = [&](auto t) noexcept { traits = static_cast<value_traits>(traits | t); };
  12688. // examine the first character to get the 'begins with' traits
  12689. // (good fail-fast opportunity; all the remaining types begin with numeric digits or signs)
  12690. if (is_decimal_digit(*cp))
  12691. {
  12692. add_trait(begins_digit);
  12693. if (*cp == U'0')
  12694. add_trait(begins_zero);
  12695. }
  12696. else if (is_match(*cp, U'+', U'-'))
  12697. add_trait(begins_sign);
  12698. else
  12699. break;
  12700. // scan the rest of the value to determine the remaining traits
  12701. char32_t chars[utf8_buffered_reader::max_history_length];
  12702. size_t char_count = {}, advance_count = {};
  12703. bool eof_while_scanning = false;
  12704. const auto scan = [&]() noexcept(!TOML_COMPILER_HAS_EXCEPTIONS)
  12705. {
  12706. if (is_eof())
  12707. return;
  12708. TOML_ASSERT_ASSUME(!is_value_terminator(*cp));
  12709. do
  12710. {
  12711. if (const auto c = **cp; c != U'_')
  12712. {
  12713. chars[char_count++] = c;
  12714. if (is_decimal_digit(c))
  12715. add_trait(has_digits);
  12716. else if (is_ascii_letter(c))
  12717. {
  12718. TOML_ASSERT_ASSUME((c >= U'a' && c <= U'z') || (c >= U'A' && c <= U'Z'));
  12719. switch (static_cast<char32_t>(c | 32u))
  12720. {
  12721. case U'b':
  12722. if (char_count == 2u && has_any(begins_zero))
  12723. add_trait(has_b);
  12724. break;
  12725. case U'e':
  12726. if (char_count > 1u
  12727. && has_none(has_b | has_o | has_p | has_t | has_x | has_z | has_colon)
  12728. && (has_none(has_plus | has_minus) || has_any(begins_sign)))
  12729. add_trait(has_e);
  12730. break;
  12731. case U'o':
  12732. if (char_count == 2u && has_any(begins_zero))
  12733. add_trait(has_o);
  12734. break;
  12735. case U'p':
  12736. if (has_any(has_x))
  12737. add_trait(has_p);
  12738. break;
  12739. case U'x':
  12740. if ((char_count == 2u && has_any(begins_zero))
  12741. || (char_count == 3u && has_any(begins_sign) && chars[1] == U'0'))
  12742. add_trait(has_x);
  12743. break;
  12744. case U't': add_trait(has_t); break;
  12745. case U'z': add_trait(has_z); break;
  12746. }
  12747. }
  12748. else if (c <= U':')
  12749. {
  12750. TOML_ASSERT_ASSUME(c < U'0' || c > U'9');
  12751. switch (c)
  12752. {
  12753. case U'+': add_trait(has_plus); break;
  12754. case U'-': add_trait(has_minus); break;
  12755. case U'.': add_trait(has_dot); break;
  12756. case U':': add_trait(has_colon); break;
  12757. }
  12758. }
  12759. }
  12760. advance_and_return_if_error();
  12761. advance_count++;
  12762. eof_while_scanning = is_eof();
  12763. }
  12764. while (advance_count < (utf8_buffered_reader::max_history_length - 1u) && !is_eof()
  12765. && !is_value_terminator(*cp));
  12766. };
  12767. scan();
  12768. return_if_error({});
  12769. // force further scanning if this could have been a date-time with a space instead of a T
  12770. if (char_count == 10u //
  12771. && (traits | begins_zero) == (bzero_msk | has_minus) //
  12772. && chars[4] == U'-' //
  12773. && chars[7] == U'-' //
  12774. && !is_eof() //
  12775. && *cp == U' ')
  12776. {
  12777. const auto pre_advance_count = advance_count;
  12778. const auto pre_scan_traits = traits;
  12779. chars[char_count++] = *cp;
  12780. add_trait(has_t);
  12781. const auto backpedal = [&]() noexcept
  12782. {
  12783. go_back(advance_count - pre_advance_count);
  12784. advance_count = pre_advance_count;
  12785. traits = pre_scan_traits;
  12786. char_count = 10u;
  12787. };
  12788. advance_and_return_if_error({});
  12789. advance_count++;
  12790. if (is_eof() || !is_decimal_digit(*cp))
  12791. backpedal();
  12792. else
  12793. {
  12794. chars[char_count++] = *cp;
  12795. advance_and_return_if_error({});
  12796. advance_count++;
  12797. scan();
  12798. return_if_error({});
  12799. if (char_count == 12u)
  12800. backpedal();
  12801. }
  12802. }
  12803. // set the reader back to where we started
  12804. go_back(advance_count);
  12805. // if after scanning ahead we still only have one value character,
  12806. // the only valid value type is an integer.
  12807. if (char_count == 1u)
  12808. {
  12809. if (has_any(begins_digit))
  12810. {
  12811. val.reset(new value{ static_cast<int64_t>(chars[0] - U'0') });
  12812. advance(); // skip the digit
  12813. break;
  12814. }
  12815. // anything else would be ambiguous.
  12816. else
  12817. set_error_and_return_default(eof_while_scanning ? "encountered end-of-file"sv
  12818. : "could not determine value type"sv);
  12819. }
  12820. // now things that can be identified from two or more characters
  12821. return_if_error({});
  12822. TOML_ASSERT_ASSUME(char_count >= 2u);
  12823. // do some 'fuzzy matching' where there's no ambiguity, since that allows the specific
  12824. // typed parse functions to take over and show better diagnostics if there's an issue
  12825. // (as opposed to the fallback "could not determine type" message)
  12826. if (has_any(has_p))
  12827. val.reset(new value{ parse_hex_float() });
  12828. else if (has_any(has_x | has_o | has_b))
  12829. {
  12830. int64_t i;
  12831. value_flags flags;
  12832. if (has_any(has_x))
  12833. {
  12834. i = parse_integer<16>();
  12835. flags = value_flags::format_as_hexadecimal;
  12836. }
  12837. else if (has_any(has_o))
  12838. {
  12839. i = parse_integer<8>();
  12840. flags = value_flags::format_as_octal;
  12841. }
  12842. else // has_b
  12843. {
  12844. i = parse_integer<2>();
  12845. flags = value_flags::format_as_binary;
  12846. }
  12847. return_if_error({});
  12848. val.reset(new value{ i });
  12849. val->ref_cast<int64_t>().flags(flags);
  12850. }
  12851. else if (has_any(has_e) || (has_any(begins_digit) && chars[1] == U'.'))
  12852. val.reset(new value{ parse_float() });
  12853. else if (has_any(begins_sign))
  12854. {
  12855. // single-digit signed integers
  12856. if (char_count == 2u && has_any(has_digits))
  12857. {
  12858. val.reset(new value{ static_cast<int64_t>(chars[1] - U'0') * (chars[0] == U'-' ? -1LL : 1LL) });
  12859. advance(); // skip the sign
  12860. advance(); // skip the digit
  12861. break;
  12862. }
  12863. // simple signed floats (e.g. +1.0)
  12864. if (is_decimal_digit(chars[1]) && chars[2] == U'.')
  12865. val.reset(new value{ parse_float() });
  12866. // signed infinity or nan
  12867. else if (is_match(chars[1], U'i', U'n', U'I', U'N'))
  12868. val.reset(new value{ parse_inf_or_nan() });
  12869. }
  12870. return_if_error({});
  12871. if (val)
  12872. break;
  12873. // match trait masks against what they can match exclusively.
  12874. // all correct value parses will come out of this list, so doing this as a switch is likely to
  12875. // be a better friend to the optimizer on the success path (failure path can be slow but that
  12876. // doesn't matter much).
  12877. switch (unwrap_enum(traits))
  12878. {
  12879. // binary integers
  12880. // 0b10
  12881. case bzero_msk | has_b:
  12882. val.reset(new value{ parse_integer<2>() });
  12883. val->ref_cast<int64_t>().flags(value_flags::format_as_binary);
  12884. break;
  12885. // octal integers
  12886. // 0o10
  12887. case bzero_msk | has_o:
  12888. val.reset(new value{ parse_integer<8>() });
  12889. val->ref_cast<int64_t>().flags(value_flags::format_as_octal);
  12890. break;
  12891. // decimal integers
  12892. // 00
  12893. // 10
  12894. // +10
  12895. // -10
  12896. case bzero_msk: [[fallthrough]];
  12897. case bdigit_msk: [[fallthrough]];
  12898. case begins_sign | has_digits | has_minus: [[fallthrough]];
  12899. case begins_sign | has_digits | has_plus:
  12900. {
  12901. // if the value was so long we exhausted the history buffer it's reasonable to assume
  12902. // there was more and the value's actual type is impossible to identify without making the
  12903. // buffer bigger (since it could have actually been a float), so emit an error.
  12904. //
  12905. // (this will likely only come up during fuzzing and similar scenarios)
  12906. static constexpr size_t max_numeric_value_length =
  12907. utf8_buffered_reader::max_history_length - 2u;
  12908. if TOML_UNLIKELY(!eof_while_scanning && advance_count > max_numeric_value_length)
  12909. set_error_and_return_default("numeric value too long to identify type - cannot exceed "sv,
  12910. max_numeric_value_length,
  12911. " characters"sv);
  12912. val.reset(new value{ parse_integer<10>() });
  12913. break;
  12914. }
  12915. // hexadecimal integers
  12916. // 0x10
  12917. case bzero_msk | has_x:
  12918. val.reset(new value{ parse_integer<16>() });
  12919. val->ref_cast<int64_t>().flags(value_flags::format_as_hexadecimal);
  12920. break;
  12921. // decimal floats
  12922. // 0e1
  12923. // 0e-1
  12924. // 0e+1
  12925. // 0.0
  12926. // 0.0e1
  12927. // 0.0e-1
  12928. // 0.0e+1
  12929. case bzero_msk | has_e: [[fallthrough]];
  12930. case bzero_msk | has_e | has_minus: [[fallthrough]];
  12931. case bzero_msk | has_e | has_plus: [[fallthrough]];
  12932. case bzero_msk | has_dot: [[fallthrough]];
  12933. case bzero_msk | has_dot | has_e: [[fallthrough]];
  12934. case bzero_msk | has_dot | has_e | has_minus: [[fallthrough]];
  12935. case bzero_msk | has_dot | has_e | has_plus: [[fallthrough]];
  12936. // 1e1
  12937. // 1e-1
  12938. // 1e+1
  12939. // 1.0
  12940. // 1.0e1
  12941. // 1.0e-1
  12942. // 1.0e+1
  12943. case bdigit_msk | has_e: [[fallthrough]];
  12944. case bdigit_msk | has_e | has_minus: [[fallthrough]];
  12945. case bdigit_msk | has_e | has_plus: [[fallthrough]];
  12946. case bdigit_msk | has_dot: [[fallthrough]];
  12947. case bdigit_msk | has_dot | has_e: [[fallthrough]];
  12948. case bdigit_msk | has_dot | has_e | has_minus: [[fallthrough]];
  12949. case bdigit_msk | has_dot | has_e | has_plus: [[fallthrough]];
  12950. // +1e1
  12951. // +1.0
  12952. // +1.0e1
  12953. // +1.0e+1
  12954. // +1.0e-1
  12955. // -1.0e+1
  12956. case begins_sign | has_digits | has_e | has_plus: [[fallthrough]];
  12957. case begins_sign | has_digits | has_dot | has_plus: [[fallthrough]];
  12958. case begins_sign | has_digits | has_dot | has_e | has_plus: [[fallthrough]];
  12959. case begins_sign | has_digits | has_dot | has_e | signs_msk: [[fallthrough]];
  12960. // -1e1
  12961. // -1e+1
  12962. // +1e-1
  12963. // -1.0
  12964. // -1.0e1
  12965. // -1.0e-1
  12966. case begins_sign | has_digits | has_e | has_minus: [[fallthrough]];
  12967. case begins_sign | has_digits | has_e | signs_msk: [[fallthrough]];
  12968. case begins_sign | has_digits | has_dot | has_minus: [[fallthrough]];
  12969. case begins_sign | has_digits | has_dot | has_e | has_minus:
  12970. val.reset(new value{ parse_float() });
  12971. break;
  12972. // hexadecimal floats
  12973. // 0x10p0
  12974. // 0x10p-0
  12975. // 0x10p+0
  12976. case bzero_msk | has_x | has_p: [[fallthrough]];
  12977. case bzero_msk | has_x | has_p | has_minus: [[fallthrough]];
  12978. case bzero_msk | has_x | has_p | has_plus: [[fallthrough]];
  12979. // -0x10p0
  12980. // -0x10p-0
  12981. // +0x10p0
  12982. // +0x10p+0
  12983. // -0x10p+0
  12984. // +0x10p-0
  12985. case begins_sign | has_digits | has_x | has_p | has_minus: [[fallthrough]];
  12986. case begins_sign | has_digits | has_x | has_p | has_plus: [[fallthrough]];
  12987. case begins_sign | has_digits | has_x | has_p | signs_msk: [[fallthrough]];
  12988. // 0x10.1p0
  12989. // 0x10.1p-0
  12990. // 0x10.1p+0
  12991. case bzero_msk | has_x | has_dot | has_p: [[fallthrough]];
  12992. case bzero_msk | has_x | has_dot | has_p | has_minus: [[fallthrough]];
  12993. case bzero_msk | has_x | has_dot | has_p | has_plus: [[fallthrough]];
  12994. // -0x10.1p0
  12995. // -0x10.1p-0
  12996. // +0x10.1p0
  12997. // +0x10.1p+0
  12998. // -0x10.1p+0
  12999. // +0x10.1p-0
  13000. case begins_sign | has_digits | has_x | has_dot | has_p | has_minus: [[fallthrough]];
  13001. case begins_sign | has_digits | has_x | has_dot | has_p | has_plus: [[fallthrough]];
  13002. case begins_sign | has_digits | has_x | has_dot | has_p | signs_msk:
  13003. val.reset(new value{ parse_hex_float() });
  13004. break;
  13005. // times
  13006. // HH:MM
  13007. // HH:MM:SS
  13008. // HH:MM:SS.FFFFFF
  13009. case bzero_msk | has_colon: [[fallthrough]];
  13010. case bzero_msk | has_colon | has_dot: [[fallthrough]];
  13011. case bdigit_msk | has_colon: [[fallthrough]];
  13012. case bdigit_msk | has_colon | has_dot: val.reset(new value{ parse_time() }); break;
  13013. // local dates
  13014. // YYYY-MM-DD
  13015. case bzero_msk | has_minus: [[fallthrough]];
  13016. case bdigit_msk | has_minus: val.reset(new value{ parse_date() }); break;
  13017. // date-times
  13018. // YYYY-MM-DDTHH:MM
  13019. // YYYY-MM-DDTHH:MM-HH:MM
  13020. // YYYY-MM-DDTHH:MM+HH:MM
  13021. // YYYY-MM-DD HH:MM
  13022. // YYYY-MM-DD HH:MM-HH:MM
  13023. // YYYY-MM-DD HH:MM+HH:MM
  13024. // YYYY-MM-DDTHH:MM:SS
  13025. // YYYY-MM-DDTHH:MM:SS-HH:MM
  13026. // YYYY-MM-DDTHH:MM:SS+HH:MM
  13027. // YYYY-MM-DD HH:MM:SS
  13028. // YYYY-MM-DD HH:MM:SS-HH:MM
  13029. // YYYY-MM-DD HH:MM:SS+HH:MM
  13030. case bzero_msk | has_minus | has_colon | has_t: [[fallthrough]];
  13031. case bzero_msk | signs_msk | has_colon | has_t: [[fallthrough]];
  13032. case bdigit_msk | has_minus | has_colon | has_t: [[fallthrough]];
  13033. case bdigit_msk | signs_msk | has_colon | has_t: [[fallthrough]];
  13034. // YYYY-MM-DDTHH:MM:SS.FFFFFF
  13035. // YYYY-MM-DDTHH:MM:SS.FFFFFF-HH:MM
  13036. // YYYY-MM-DDTHH:MM:SS.FFFFFF+HH:MM
  13037. // YYYY-MM-DD HH:MM:SS.FFFFFF
  13038. // YYYY-MM-DD HH:MM:SS.FFFFFF-HH:MM
  13039. // YYYY-MM-DD HH:MM:SS.FFFFFF+HH:MM
  13040. case bzero_msk | has_minus | has_colon | has_dot | has_t: [[fallthrough]];
  13041. case bzero_msk | signs_msk | has_colon | has_dot | has_t: [[fallthrough]];
  13042. case bdigit_msk | has_minus | has_colon | has_dot | has_t: [[fallthrough]];
  13043. case bdigit_msk | signs_msk | has_colon | has_dot | has_t: [[fallthrough]];
  13044. // YYYY-MM-DDTHH:MMZ
  13045. // YYYY-MM-DD HH:MMZ
  13046. // YYYY-MM-DDTHH:MM:SSZ
  13047. // YYYY-MM-DD HH:MM:SSZ
  13048. // YYYY-MM-DDTHH:MM:SS.FFFFFFZ
  13049. // YYYY-MM-DD HH:MM:SS.FFFFFFZ
  13050. case bzero_msk | has_minus | has_colon | has_z | has_t: [[fallthrough]];
  13051. case bzero_msk | has_minus | has_colon | has_dot | has_z | has_t: [[fallthrough]];
  13052. case bdigit_msk | has_minus | has_colon | has_z | has_t: [[fallthrough]];
  13053. case bdigit_msk | has_minus | has_colon | has_dot | has_z | has_t:
  13054. val.reset(new value{ parse_date_time() });
  13055. break;
  13056. }
  13057. }
  13058. while (false);
  13059. if (!val)
  13060. {
  13061. set_error_at(begin_pos, "could not determine value type"sv);
  13062. return_after_error({});
  13063. }
  13064. val->source_ = { begin_pos, current_position(1), reader.source_path() };
  13065. return val;
  13066. }
  13067. TOML_NEVER_INLINE
  13068. bool parse_key()
  13069. {
  13070. return_if_error({});
  13071. assert_not_eof();
  13072. TOML_ASSERT_ASSUME(is_bare_key_character(*cp) || is_string_delimiter(*cp));
  13073. push_parse_scope("key"sv);
  13074. key_buffer.clear();
  13075. recording_whitespace = false;
  13076. while (!is_error())
  13077. {
  13078. std::string_view key_segment;
  13079. const auto key_begin = current_position();
  13080. // bare_key_segment
  13081. if (is_bare_key_character(*cp))
  13082. key_segment = parse_bare_key_segment();
  13083. // "quoted key segment"
  13084. else if (is_string_delimiter(*cp))
  13085. {
  13086. const auto begin_pos = cp->position;
  13087. recording_whitespace = true;
  13088. parsed_string str = parse_string();
  13089. recording_whitespace = false;
  13090. return_if_error({});
  13091. if (str.was_multi_line)
  13092. {
  13093. set_error_at(begin_pos,
  13094. "multi-line strings are prohibited in "sv,
  13095. key_buffer.empty() ? ""sv : "dotted "sv,
  13096. "keys"sv);
  13097. return_after_error({});
  13098. }
  13099. else
  13100. key_segment = str.value;
  13101. }
  13102. // ???
  13103. else
  13104. set_error_and_return_default("expected bare key starting character or string delimiter, saw '"sv,
  13105. to_sv(*cp),
  13106. "'"sv);
  13107. const auto key_end = current_position();
  13108. // whitespace following the key segment
  13109. consume_leading_whitespace();
  13110. // store segment
  13111. key_buffer.push_back(key_segment, key_begin, key_end);
  13112. // eof or no more key to come
  13113. if (is_eof() || *cp != U'.')
  13114. break;
  13115. // was a dotted key - go around again
  13116. advance_and_return_if_error_or_eof({});
  13117. consume_leading_whitespace();
  13118. set_error_and_return_if_eof({});
  13119. }
  13120. return_if_error({});
  13121. return true;
  13122. }
  13123. TOML_NODISCARD
  13124. key make_key(size_t segment_index) const
  13125. {
  13126. TOML_ASSERT(key_buffer.size() > segment_index);
  13127. return key{
  13128. key_buffer[segment_index],
  13129. source_region{ key_buffer.starts[segment_index], key_buffer.ends[segment_index], root.source().path }
  13130. };
  13131. }
  13132. TOML_NODISCARD
  13133. TOML_NEVER_INLINE
  13134. table* parse_table_header()
  13135. {
  13136. return_if_error({});
  13137. assert_not_eof();
  13138. TOML_ASSERT_ASSUME(*cp == U'[');
  13139. push_parse_scope("table header"sv);
  13140. const source_position header_begin_pos = cp->position;
  13141. source_position header_end_pos;
  13142. bool is_arr = false;
  13143. // parse header
  13144. {
  13145. // skip first '['
  13146. advance_and_return_if_error_or_eof({});
  13147. // skip past any whitespace that followed the '['
  13148. const bool had_leading_whitespace = consume_leading_whitespace();
  13149. set_error_and_return_if_eof({});
  13150. // skip second '[' (if present)
  13151. if (*cp == U'[')
  13152. {
  13153. if (had_leading_whitespace)
  13154. set_error_and_return_default(
  13155. "[[array-of-table]] brackets must be contiguous (i.e. [ [ this ] ] is prohibited)"sv);
  13156. is_arr = true;
  13157. advance_and_return_if_error_or_eof({});
  13158. // skip past any whitespace that followed the '['
  13159. consume_leading_whitespace();
  13160. set_error_and_return_if_eof({});
  13161. }
  13162. // check for a premature closing ']'
  13163. if (*cp == U']')
  13164. set_error_and_return_default("tables with blank bare keys are explicitly prohibited"sv);
  13165. // get the actual key
  13166. start_recording();
  13167. parse_key();
  13168. stop_recording(1u);
  13169. return_if_error({});
  13170. // skip past any whitespace that followed the key
  13171. consume_leading_whitespace();
  13172. return_if_error({});
  13173. set_error_and_return_if_eof({});
  13174. // consume the closing ']'
  13175. if (*cp != U']')
  13176. set_error_and_return_default("expected ']', saw '"sv, to_sv(*cp), "'"sv);
  13177. if (is_arr)
  13178. {
  13179. advance_and_return_if_error_or_eof({});
  13180. if (*cp != U']')
  13181. set_error_and_return_default("expected ']', saw '"sv, to_sv(*cp), "'"sv);
  13182. }
  13183. advance_and_return_if_error({});
  13184. header_end_pos = current_position(1);
  13185. // handle the rest of the line after the header
  13186. consume_leading_whitespace();
  13187. if (!is_eof() && !consume_comment() && !consume_line_break())
  13188. set_error_and_return_default("expected a comment or whitespace, saw '"sv, to_sv(cp), "'"sv);
  13189. }
  13190. TOML_ASSERT(!key_buffer.empty());
  13191. // check if each parent is a table/table array, or can be created implicitly as a table.
  13192. table* parent = &root;
  13193. for (size_t i = 0, e = key_buffer.size() - 1u; i < e; i++)
  13194. {
  13195. const std::string_view segment = key_buffer[i];
  13196. auto pit = parent->lower_bound(segment);
  13197. // parent already existed
  13198. if (pit != parent->end() && pit->first == segment)
  13199. {
  13200. node& p = pit->second;
  13201. if (auto tbl = p.as_table())
  13202. {
  13203. // adding to closed inline tables is illegal
  13204. if (tbl->is_inline() && !impl::find(open_inline_tables.begin(), open_inline_tables.end(), tbl))
  13205. set_error_and_return_default("cannot insert '"sv,
  13206. to_sv(recording_buffer),
  13207. "' into existing inline table"sv);
  13208. parent = tbl;
  13209. }
  13210. else if (auto arr = p.as_array(); arr && impl::find(table_arrays.begin(), table_arrays.end(), arr))
  13211. {
  13212. // table arrays are a special case;
  13213. // the spec dictates we select the most recently declared element in the array.
  13214. TOML_ASSERT(!arr->empty());
  13215. TOML_ASSERT(arr->back().is_table());
  13216. parent = &arr->back().ref_cast<table>();
  13217. }
  13218. else
  13219. {
  13220. if (!is_arr && p.type() == node_type::table)
  13221. set_error_and_return_default("cannot redefine existing table '"sv,
  13222. to_sv(recording_buffer),
  13223. "'"sv);
  13224. else
  13225. set_error_and_return_default("cannot redefine existing "sv,
  13226. to_sv(p.type()),
  13227. " '"sv,
  13228. to_sv(recording_buffer),
  13229. "' as "sv,
  13230. is_arr ? "array-of-tables"sv : "table"sv);
  13231. }
  13232. }
  13233. // need to create a new implicit table
  13234. else
  13235. {
  13236. pit = parent->emplace_hint<table>(pit, make_key(i));
  13237. table& p = pit->second.ref_cast<table>();
  13238. p.source_ = { header_begin_pos, header_end_pos, reader.source_path() };
  13239. implicit_tables.push_back(&p);
  13240. parent = &p;
  13241. }
  13242. }
  13243. const auto last_segment = key_buffer.back();
  13244. auto it = parent->lower_bound(last_segment);
  13245. // if there was already a matching node some sanity checking is necessary;
  13246. // this is ok if we're making an array and the existing element is already an array (new element)
  13247. // or if we're making a table and the existing element is an implicitly-created table (promote it),
  13248. // otherwise this is a redefinition error.
  13249. if (it != parent->end() && it->first == last_segment)
  13250. {
  13251. node& matching_node = it->second;
  13252. if (auto arr = matching_node.as_array();
  13253. is_arr && arr && impl::find(table_arrays.begin(), table_arrays.end(), arr))
  13254. {
  13255. table& tbl = arr->emplace_back<table>();
  13256. tbl.source_ = { header_begin_pos, header_end_pos, reader.source_path() };
  13257. return &tbl;
  13258. }
  13259. else if (auto tbl = matching_node.as_table(); !is_arr && tbl && !implicit_tables.empty())
  13260. {
  13261. if (auto found = impl::find(implicit_tables.begin(), implicit_tables.end(), tbl); found)
  13262. {
  13263. bool ok = true;
  13264. if (!tbl->empty())
  13265. {
  13266. for (auto& [_, child] : *tbl)
  13267. {
  13268. if (!child.is_table() && !child.is_array_of_tables())
  13269. {
  13270. ok = false;
  13271. break;
  13272. }
  13273. }
  13274. }
  13275. if (ok)
  13276. {
  13277. implicit_tables.erase(implicit_tables.cbegin() + (found - implicit_tables.data()));
  13278. tbl->source_.begin = header_begin_pos;
  13279. tbl->source_.end = header_end_pos;
  13280. return tbl;
  13281. }
  13282. }
  13283. }
  13284. // if we get here it's a redefinition error.
  13285. if (!is_arr && matching_node.type() == node_type::table)
  13286. {
  13287. set_error_at(header_begin_pos,
  13288. "cannot redefine existing table '"sv,
  13289. to_sv(recording_buffer),
  13290. "'"sv);
  13291. return_after_error({});
  13292. }
  13293. else
  13294. {
  13295. set_error_at(header_begin_pos,
  13296. "cannot redefine existing "sv,
  13297. to_sv(matching_node.type()),
  13298. " '"sv,
  13299. to_sv(recording_buffer),
  13300. "' as "sv,
  13301. is_arr ? "array-of-tables"sv : "table"sv);
  13302. return_after_error({});
  13303. }
  13304. }
  13305. // there was no matching node, sweet - we can freely instantiate a new table/table array.
  13306. else
  13307. {
  13308. auto last_key = make_key(key_buffer.size() - 1u);
  13309. // if it's an array we need to make the array and it's first table element,
  13310. // set the starting regions, and return the table element
  13311. if (is_arr)
  13312. {
  13313. it = parent->emplace_hint<array>(it, std::move(last_key));
  13314. array& tbl_arr = it->second.ref_cast<array>();
  13315. table_arrays.push_back(&tbl_arr);
  13316. tbl_arr.source_ = { header_begin_pos, header_end_pos, reader.source_path() };
  13317. table& tbl = tbl_arr.emplace_back<table>();
  13318. tbl.source_ = { header_begin_pos, header_end_pos, reader.source_path() };
  13319. return &tbl;
  13320. }
  13321. // otherwise we're just making a table
  13322. else
  13323. {
  13324. it = parent->emplace_hint<table>(it, std::move(last_key));
  13325. table& tbl = it->second.ref_cast<table>();
  13326. tbl.source_ = { header_begin_pos, header_end_pos, reader.source_path() };
  13327. return &tbl;
  13328. }
  13329. }
  13330. }
  13331. TOML_NEVER_INLINE
  13332. bool parse_key_value_pair_and_insert(table* tbl)
  13333. {
  13334. return_if_error({});
  13335. assert_not_eof();
  13336. TOML_ASSERT_ASSUME(is_string_delimiter(*cp) || is_bare_key_character(*cp));
  13337. push_parse_scope("key-value pair"sv);
  13338. // read the key into the key buffer
  13339. start_recording();
  13340. parse_key();
  13341. stop_recording(1u);
  13342. return_if_error({});
  13343. TOML_ASSERT(key_buffer.size() >= 1u);
  13344. // skip past any whitespace that followed the key
  13345. consume_leading_whitespace();
  13346. set_error_and_return_if_eof({});
  13347. // '='
  13348. if (*cp != U'=')
  13349. set_error_and_return_default("expected '=', saw '"sv, to_sv(*cp), "'"sv);
  13350. advance_and_return_if_error_or_eof({});
  13351. // skip past any whitespace that followed the '='
  13352. consume_leading_whitespace();
  13353. return_if_error({});
  13354. set_error_and_return_if_eof({});
  13355. // check that the next character could actually be a value
  13356. if (is_value_terminator(*cp))
  13357. set_error_and_return_default("expected value, saw '"sv, to_sv(*cp), "'"sv);
  13358. // if it's a dotted kvp we need to spawn the parent sub-tables if necessary,
  13359. // and set the target table to the second-to-last one in the chain
  13360. if (key_buffer.size() > 1u)
  13361. {
  13362. for (size_t i = 0; i < key_buffer.size() - 1u; i++)
  13363. {
  13364. const std::string_view segment = key_buffer[i];
  13365. auto pit = tbl->lower_bound(segment);
  13366. // parent already existed
  13367. if (pit != tbl->end() && pit->first == segment)
  13368. {
  13369. table* p = pit->second.as_table();
  13370. // redefinition
  13371. if TOML_UNLIKELY(!p
  13372. || !(impl::find(dotted_key_tables.begin(), dotted_key_tables.end(), p)
  13373. || impl::find(implicit_tables.begin(), implicit_tables.end(), p)))
  13374. {
  13375. set_error_at(key_buffer.starts[i],
  13376. "cannot redefine existing "sv,
  13377. to_sv(pit->second.type()),
  13378. " as dotted key-value pair"sv);
  13379. return_after_error({});
  13380. }
  13381. tbl = p;
  13382. }
  13383. // need to create a new implicit table
  13384. else
  13385. {
  13386. pit = tbl->emplace_hint<table>(pit, make_key(i));
  13387. table& p = pit->second.ref_cast<table>();
  13388. p.source_ = pit->first.source();
  13389. dotted_key_tables.push_back(&p);
  13390. tbl = &p;
  13391. }
  13392. }
  13393. }
  13394. // ensure this isn't a redefinition
  13395. const std::string_view last_segment = key_buffer.back();
  13396. auto it = tbl->lower_bound(last_segment);
  13397. if (it != tbl->end() && it->first == last_segment)
  13398. {
  13399. set_error("cannot redefine existing "sv,
  13400. to_sv(it->second.type()),
  13401. " '"sv,
  13402. to_sv(recording_buffer),
  13403. "'"sv);
  13404. return_after_error({});
  13405. }
  13406. // create the key first since the key buffer will likely get overwritten during value parsing (inline
  13407. // tables)
  13408. auto last_key = make_key(key_buffer.size() - 1u);
  13409. // now we can actually parse the value
  13410. node_ptr val = parse_value();
  13411. return_if_error({});
  13412. tbl->emplace_hint<node_ptr>(it, std::move(last_key), std::move(val));
  13413. return true;
  13414. }
  13415. void parse_document()
  13416. {
  13417. assert_not_error();
  13418. assert_not_eof();
  13419. push_parse_scope("root table"sv);
  13420. table* current_table = &root;
  13421. do
  13422. {
  13423. return_if_error();
  13424. // leading whitespace, line endings, comments
  13425. if (consume_leading_whitespace() || consume_line_break() || consume_comment())
  13426. continue;
  13427. return_if_error();
  13428. // [tables]
  13429. // [[table array]]
  13430. if (*cp == U'[')
  13431. current_table = parse_table_header();
  13432. // bare_keys
  13433. // dotted.keys
  13434. // "quoted keys"
  13435. else if (is_bare_key_character(*cp) || is_string_delimiter(*cp))
  13436. {
  13437. push_parse_scope("key-value pair"sv);
  13438. parse_key_value_pair_and_insert(current_table);
  13439. // handle the rest of the line after the kvp
  13440. // (this is not done in parse_key_value_pair() because that is also used for inline tables)
  13441. consume_leading_whitespace();
  13442. return_if_error();
  13443. if (!is_eof() && !consume_comment() && !consume_line_break())
  13444. set_error("expected a comment or whitespace, saw '"sv, to_sv(cp), "'"sv);
  13445. }
  13446. else // ??
  13447. set_error("expected keys, tables, whitespace or comments, saw '"sv, to_sv(cp), "'"sv);
  13448. }
  13449. while (!is_eof());
  13450. auto eof_pos = current_position(1);
  13451. root.source_.end = eof_pos;
  13452. if (current_table && current_table != &root && current_table->source_.end <= current_table->source_.begin)
  13453. current_table->source_.end = eof_pos;
  13454. }
  13455. static void update_region_ends(node& nde) noexcept
  13456. {
  13457. const auto type = nde.type();
  13458. if (type > node_type::array)
  13459. return;
  13460. if (type == node_type::table)
  13461. {
  13462. auto& tbl = nde.ref_cast<table>();
  13463. if (tbl.is_inline()) // inline tables (and all their inline descendants) are already correctly
  13464. // terminated
  13465. return;
  13466. auto end = nde.source_.end;
  13467. for (auto&& [k, v] : tbl)
  13468. {
  13469. TOML_UNUSED(k);
  13470. update_region_ends(v);
  13471. if (end < v.source_.end)
  13472. end = v.source_.end;
  13473. }
  13474. }
  13475. else // arrays
  13476. {
  13477. auto& arr = nde.ref_cast<array>();
  13478. auto end = nde.source_.end;
  13479. for (auto&& v : arr)
  13480. {
  13481. update_region_ends(v);
  13482. if (end < v.source_.end)
  13483. end = v.source_.end;
  13484. }
  13485. nde.source_.end = end;
  13486. }
  13487. }
  13488. public:
  13489. parser(utf8_reader_interface&& reader_) //
  13490. : reader{ reader_ }
  13491. {
  13492. root.source_ = { prev_pos, prev_pos, reader.source_path() };
  13493. if (!reader.peek_eof())
  13494. {
  13495. cp = reader.read_next();
  13496. #if !TOML_EXCEPTIONS
  13497. if (reader.error())
  13498. {
  13499. err = std::move(reader.error());
  13500. return;
  13501. }
  13502. #endif
  13503. if (cp)
  13504. parse_document();
  13505. }
  13506. update_region_ends(root);
  13507. }
  13508. TOML_NODISCARD
  13509. operator parse_result() && noexcept
  13510. {
  13511. #if TOML_EXCEPTIONS
  13512. return { std::move(root) };
  13513. #else
  13514. if (err)
  13515. return parse_result{ *std::move(err) };
  13516. else
  13517. return parse_result{ std::move(root) };
  13518. #endif
  13519. }
  13520. };
  13521. TOML_EXTERNAL_LINKAGE
  13522. node_ptr parser::parse_array()
  13523. {
  13524. return_if_error({});
  13525. assert_not_eof();
  13526. TOML_ASSERT_ASSUME(*cp == U'[');
  13527. push_parse_scope("array"sv);
  13528. // skip opening '['
  13529. advance_and_return_if_error_or_eof({});
  13530. node_ptr arr_ptr{ new array{} };
  13531. array& arr = arr_ptr->ref_cast<array>();
  13532. enum class TOML_CLOSED_ENUM parse_type : int
  13533. {
  13534. none,
  13535. comma,
  13536. val
  13537. };
  13538. parse_type prev = parse_type::none;
  13539. while (!is_error())
  13540. {
  13541. while (consume_leading_whitespace() || consume_line_break() || consume_comment())
  13542. continue;
  13543. set_error_and_return_if_eof({});
  13544. // commas - only legal after a value
  13545. if (*cp == U',')
  13546. {
  13547. if (prev == parse_type::val)
  13548. {
  13549. prev = parse_type::comma;
  13550. advance_and_return_if_error_or_eof({});
  13551. continue;
  13552. }
  13553. set_error_and_return_default("expected value or closing ']', saw comma"sv);
  13554. }
  13555. // closing ']'
  13556. else if (*cp == U']')
  13557. {
  13558. advance_and_return_if_error({});
  13559. break;
  13560. }
  13561. // must be a value
  13562. else
  13563. {
  13564. if (prev == parse_type::val)
  13565. {
  13566. set_error_and_return_default("expected comma or closing ']', saw '"sv, to_sv(*cp), "'"sv);
  13567. continue;
  13568. }
  13569. prev = parse_type::val;
  13570. auto val = parse_value();
  13571. return_if_error({});
  13572. if (!arr.capacity())
  13573. arr.reserve(4u);
  13574. arr.emplace_back<node_ptr>(std::move(val));
  13575. }
  13576. }
  13577. return_if_error({});
  13578. return arr_ptr;
  13579. }
  13580. TOML_EXTERNAL_LINKAGE
  13581. node_ptr parser::parse_inline_table()
  13582. {
  13583. return_if_error({});
  13584. assert_not_eof();
  13585. TOML_ASSERT_ASSUME(*cp == U'{');
  13586. push_parse_scope("inline table"sv);
  13587. // skip opening '{'
  13588. advance_and_return_if_error_or_eof({});
  13589. node_ptr tbl_ptr{ new table{} };
  13590. table& tbl = tbl_ptr->ref_cast<table>();
  13591. tbl.is_inline(true);
  13592. table_vector_scope table_scope{ open_inline_tables, tbl };
  13593. enum class TOML_CLOSED_ENUM parse_type : int
  13594. {
  13595. none,
  13596. comma,
  13597. kvp
  13598. };
  13599. parse_type prev = parse_type::none;
  13600. while (!is_error())
  13601. {
  13602. if constexpr (TOML_LANG_UNRELEASED) // toml/issues/516 (newlines/trailing commas in inline tables)
  13603. {
  13604. while (consume_leading_whitespace() || consume_line_break() || consume_comment())
  13605. continue;
  13606. }
  13607. else
  13608. {
  13609. while (consume_leading_whitespace())
  13610. continue;
  13611. }
  13612. return_if_error({});
  13613. set_error_and_return_if_eof({});
  13614. // commas - only legal after a key-value pair
  13615. if (*cp == U',')
  13616. {
  13617. if (prev == parse_type::kvp)
  13618. {
  13619. prev = parse_type::comma;
  13620. advance_and_return_if_error_or_eof({});
  13621. }
  13622. else
  13623. set_error_and_return_default("expected key-value pair or closing '}', saw comma"sv);
  13624. }
  13625. // closing '}'
  13626. else if (*cp == U'}')
  13627. {
  13628. if constexpr (!TOML_LANG_UNRELEASED) // toml/issues/516 (newlines/trailing commas in inline tables)
  13629. {
  13630. if (prev == parse_type::comma)
  13631. {
  13632. set_error_and_return_default("expected key-value pair, saw closing '}' (dangling comma)"sv);
  13633. continue;
  13634. }
  13635. }
  13636. advance_and_return_if_error({});
  13637. break;
  13638. }
  13639. // key-value pair
  13640. else if (is_string_delimiter(*cp) || is_bare_key_character(*cp))
  13641. {
  13642. if (prev == parse_type::kvp)
  13643. set_error_and_return_default("expected comma or closing '}', saw '"sv, to_sv(*cp), "'"sv);
  13644. else
  13645. {
  13646. prev = parse_type::kvp;
  13647. parse_key_value_pair_and_insert(&tbl);
  13648. }
  13649. }
  13650. else
  13651. set_error_and_return_default("expected key or closing '}', saw '"sv, to_sv(*cp), "'"sv);
  13652. }
  13653. return_if_error({});
  13654. return tbl_ptr;
  13655. }
  13656. TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
  13657. }
  13658. TOML_IMPL_NAMESPACE_END;
  13659. #undef TOML_RETURNS_BY_THROWING
  13660. #undef advance_and_return_if_error
  13661. #undef advance_and_return_if_error_or_eof
  13662. #undef assert_not_eof
  13663. #undef assert_not_error
  13664. #undef is_eof
  13665. #undef is_error
  13666. #undef parse_error_break
  13667. #undef push_parse_scope
  13668. #undef push_parse_scope_1
  13669. #undef push_parse_scope_2
  13670. #undef return_after_error
  13671. #undef return_if_eof
  13672. #undef return_if_error
  13673. #undef return_if_error_or_eof
  13674. #undef set_error_and_return
  13675. #undef set_error_and_return_default
  13676. #undef set_error_and_return_if_eof
  13677. #undef utf8_buffered_reader_error_check
  13678. #undef utf8_reader_error
  13679. #undef utf8_reader_error_check
  13680. #undef utf8_reader_return_after_error
  13681. TOML_ANON_NAMESPACE_START
  13682. {
  13683. TOML_NODISCARD
  13684. TOML_INTERNAL_LINKAGE
  13685. parse_result do_parse(utf8_reader_interface && reader)
  13686. {
  13687. return impl::parser{ std::move(reader) };
  13688. }
  13689. TOML_NODISCARD
  13690. TOML_INTERNAL_LINKAGE
  13691. parse_result do_parse_file(std::string_view file_path)
  13692. {
  13693. #if TOML_EXCEPTIONS
  13694. #define TOML_PARSE_FILE_ERROR(msg, path) \
  13695. throw parse_error{ msg, source_position{}, std::make_shared<const std::string>(std::move(path)) }
  13696. #else
  13697. #define TOML_PARSE_FILE_ERROR(msg, path) \
  13698. return parse_result \
  13699. { \
  13700. parse_error \
  13701. { \
  13702. msg, source_position{}, std::make_shared<const std::string>(std::move(path)) \
  13703. } \
  13704. }
  13705. #endif
  13706. std::string file_path_str(file_path);
  13707. // open file with a custom-sized stack buffer
  13708. std::ifstream file;
  13709. TOML_OVERALIGNED char file_buffer[sizeof(void*) * 1024u];
  13710. file.rdbuf()->pubsetbuf(file_buffer, sizeof(file_buffer));
  13711. #if TOML_WINDOWS
  13712. file.open(impl::widen(file_path_str).c_str(), std::ifstream::in | std::ifstream::binary | std::ifstream::ate);
  13713. #else
  13714. file.open(file_path_str, std::ifstream::in | std::ifstream::binary | std::ifstream::ate);
  13715. #endif
  13716. if (!file.is_open())
  13717. TOML_PARSE_FILE_ERROR("File could not be opened for reading", file_path_str);
  13718. // get size
  13719. const auto file_size = file.tellg();
  13720. if (file_size == -1)
  13721. TOML_PARSE_FILE_ERROR("Could not determine file size", file_path_str);
  13722. file.seekg(0, std::ifstream::beg);
  13723. // read the whole file into memory first if the file isn't too large
  13724. constexpr auto large_file_threshold = 1024 * 1024 * 2; // 2 MB
  13725. if (file_size <= large_file_threshold)
  13726. {
  13727. std::vector<char> file_data;
  13728. file_data.resize(static_cast<size_t>(file_size));
  13729. file.read(file_data.data(), static_cast<std::streamsize>(file_size));
  13730. return parse(std::string_view{ file_data.data(), file_data.size() }, std::move(file_path_str));
  13731. }
  13732. // otherwise parse it using the streams
  13733. else
  13734. return parse(file, std::move(file_path_str));
  13735. #undef TOML_PARSE_FILE_ERROR
  13736. }
  13737. }
  13738. TOML_ANON_NAMESPACE_END;
  13739. TOML_NAMESPACE_START
  13740. {
  13741. TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, ex, noex);
  13742. TOML_EXTERNAL_LINKAGE
  13743. parse_result TOML_CALLCONV parse(std::string_view doc, std::string_view source_path)
  13744. {
  13745. return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, source_path });
  13746. }
  13747. TOML_EXTERNAL_LINKAGE
  13748. parse_result TOML_CALLCONV parse(std::string_view doc, std::string && source_path)
  13749. {
  13750. return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, std::move(source_path) });
  13751. }
  13752. TOML_EXTERNAL_LINKAGE
  13753. parse_result TOML_CALLCONV parse(std::istream & doc, std::string_view source_path)
  13754. {
  13755. return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, source_path });
  13756. }
  13757. TOML_EXTERNAL_LINKAGE
  13758. parse_result TOML_CALLCONV parse(std::istream & doc, std::string && source_path)
  13759. {
  13760. return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, std::move(source_path) });
  13761. }
  13762. TOML_EXTERNAL_LINKAGE
  13763. parse_result TOML_CALLCONV parse_file(std::string_view file_path)
  13764. {
  13765. return TOML_ANON_NAMESPACE::do_parse_file(file_path);
  13766. }
  13767. #if TOML_HAS_CHAR8
  13768. TOML_EXTERNAL_LINKAGE
  13769. parse_result TOML_CALLCONV parse(std::u8string_view doc, std::string_view source_path)
  13770. {
  13771. return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, source_path });
  13772. }
  13773. TOML_EXTERNAL_LINKAGE
  13774. parse_result TOML_CALLCONV parse(std::u8string_view doc, std::string && source_path)
  13775. {
  13776. return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, std::move(source_path) });
  13777. }
  13778. TOML_EXTERNAL_LINKAGE
  13779. parse_result TOML_CALLCONV parse_file(std::u8string_view file_path)
  13780. {
  13781. std::string file_path_str;
  13782. file_path_str.resize(file_path.length());
  13783. memcpy(file_path_str.data(), file_path.data(), file_path.length());
  13784. return TOML_ANON_NAMESPACE::do_parse_file(file_path_str);
  13785. }
  13786. #endif // TOML_HAS_CHAR8
  13787. #if TOML_ENABLE_WINDOWS_COMPAT
  13788. TOML_EXTERNAL_LINKAGE
  13789. parse_result TOML_CALLCONV parse(std::string_view doc, std::wstring_view source_path)
  13790. {
  13791. return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, impl::narrow(source_path) });
  13792. }
  13793. TOML_EXTERNAL_LINKAGE
  13794. parse_result TOML_CALLCONV parse(std::istream & doc, std::wstring_view source_path)
  13795. {
  13796. return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, impl::narrow(source_path) });
  13797. }
  13798. TOML_EXTERNAL_LINKAGE
  13799. parse_result TOML_CALLCONV parse_file(std::wstring_view file_path)
  13800. {
  13801. return TOML_ANON_NAMESPACE::do_parse_file(impl::narrow(file_path));
  13802. }
  13803. #endif // TOML_ENABLE_WINDOWS_COMPAT
  13804. #if TOML_HAS_CHAR8 && TOML_ENABLE_WINDOWS_COMPAT
  13805. TOML_EXTERNAL_LINKAGE
  13806. parse_result TOML_CALLCONV parse(std::u8string_view doc, std::wstring_view source_path)
  13807. {
  13808. return TOML_ANON_NAMESPACE::do_parse(TOML_ANON_NAMESPACE::utf8_reader{ doc, impl::narrow(source_path) });
  13809. }
  13810. #endif // TOML_HAS_CHAR8 && TOML_ENABLE_WINDOWS_COMPAT
  13811. TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
  13812. }
  13813. TOML_NAMESPACE_END;
  13814. #undef TOML_OVERALIGNED
  13815. #ifdef _MSC_VER
  13816. #pragma pop_macro("min")
  13817. #pragma pop_macro("max")
  13818. #ifndef __clang__
  13819. #pragma inline_recursion(off)
  13820. #endif
  13821. #endif
  13822. TOML_POP_WARNINGS;
  13823. #endif // TOML_ENABLE_PARSER
  13824. //******** impl/formatter.inl ****************************************************************************************
  13825. #if TOML_ENABLE_FORMATTERS
  13826. TOML_PUSH_WARNINGS;
  13827. #ifdef _MSC_VER
  13828. #ifndef __clang__
  13829. #pragma inline_recursion(on)
  13830. #endif
  13831. #pragma push_macro("min")
  13832. #pragma push_macro("max")
  13833. #undef min
  13834. #undef max
  13835. #endif
  13836. TOML_IMPL_NAMESPACE_START
  13837. {
  13838. enum class TOML_CLOSED_FLAGS_ENUM formatted_string_traits : unsigned
  13839. {
  13840. none,
  13841. line_breaks = 1u << 0, // \n
  13842. tabs = 1u << 1, // \t
  13843. control_chars = 1u << 2, // also includes non-ascii vertical whitespace
  13844. single_quotes = 1u << 3,
  13845. non_bare = 1u << 4, // anything not satisfying "is bare key character"
  13846. non_ascii = 1u << 5, // any codepoint >= 128
  13847. all = (non_ascii << 1u) - 1u
  13848. };
  13849. TOML_MAKE_FLAGS(formatted_string_traits);
  13850. TOML_EXTERNAL_LINKAGE
  13851. formatter::formatter(const node* source_node,
  13852. const parse_result* source_pr,
  13853. const formatter_constants& constants,
  13854. const formatter_config& config) noexcept //
  13855. #if TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
  13856. : source_{ source_pr && *source_pr ? &source_pr->table() : source_node },
  13857. result_{ source_pr },
  13858. #else
  13859. : source_{ source_pr ? source_pr : source_node },
  13860. #endif
  13861. constants_{ &constants },
  13862. config_{ config }
  13863. {
  13864. TOML_ASSERT_ASSUME(source_);
  13865. config_.flags = (config_.flags | constants_->mandatory_flags) & ~constants_->ignored_flags;
  13866. indent_columns_ = {};
  13867. for (auto c : config_.indent)
  13868. indent_columns_ += c == '\t' ? 4u : 1u;
  13869. int_format_mask_ = config_.flags
  13870. & (format_flags::allow_binary_integers | format_flags::allow_octal_integers
  13871. | format_flags::allow_hexadecimal_integers);
  13872. }
  13873. TOML_EXTERNAL_LINKAGE
  13874. void formatter::attach(std::ostream & stream) noexcept
  13875. {
  13876. indent_ = {};
  13877. naked_newline_ = true;
  13878. stream_ = &stream;
  13879. }
  13880. TOML_EXTERNAL_LINKAGE
  13881. void formatter::detach() noexcept
  13882. {
  13883. stream_ = nullptr;
  13884. }
  13885. TOML_EXTERNAL_LINKAGE
  13886. void formatter::print_newline(bool force)
  13887. {
  13888. if (!naked_newline_ || force)
  13889. {
  13890. print_to_stream(*stream_, '\n');
  13891. naked_newline_ = true;
  13892. }
  13893. }
  13894. TOML_EXTERNAL_LINKAGE
  13895. void formatter::print_indent()
  13896. {
  13897. for (int i = 0; i < indent_; i++)
  13898. {
  13899. print_to_stream(*stream_, config_.indent);
  13900. naked_newline_ = false;
  13901. }
  13902. }
  13903. TOML_EXTERNAL_LINKAGE
  13904. void formatter::print_unformatted(char c)
  13905. {
  13906. print_to_stream(*stream_, c);
  13907. naked_newline_ = false;
  13908. }
  13909. TOML_EXTERNAL_LINKAGE
  13910. void formatter::print_unformatted(std::string_view str)
  13911. {
  13912. print_to_stream(*stream_, str);
  13913. naked_newline_ = false;
  13914. }
  13915. TOML_EXTERNAL_LINKAGE
  13916. void formatter::print_string(std::string_view str,
  13917. bool allow_multi_line,
  13918. bool allow_bare,
  13919. bool allow_literal_whitespace)
  13920. {
  13921. if (str.empty())
  13922. {
  13923. print_unformatted(literal_strings_allowed() ? "''"sv : "\"\""sv);
  13924. return;
  13925. }
  13926. // pre-scan the string to determine how we should output it
  13927. formatted_string_traits traits = {};
  13928. if (!allow_bare)
  13929. traits |= formatted_string_traits::non_bare;
  13930. bool unicode_allowed = unicode_strings_allowed();
  13931. // ascii fast path
  13932. if (is_ascii(str.data(), str.length()))
  13933. {
  13934. for (auto c : str)
  13935. {
  13936. switch (c)
  13937. {
  13938. case '\n': traits |= formatted_string_traits::line_breaks; break;
  13939. case '\t': traits |= formatted_string_traits::tabs; break;
  13940. case '\'': traits |= formatted_string_traits::single_quotes; break;
  13941. default:
  13942. {
  13943. if TOML_UNLIKELY(is_control_character(c))
  13944. traits |= formatted_string_traits::control_chars;
  13945. if (!is_ascii_bare_key_character(static_cast<char32_t>(c)))
  13946. traits |= formatted_string_traits::non_bare;
  13947. break;
  13948. }
  13949. }
  13950. static constexpr auto all_ascii_traits =
  13951. formatted_string_traits::all & ~formatted_string_traits::non_ascii;
  13952. if (traits == all_ascii_traits)
  13953. break;
  13954. }
  13955. }
  13956. // unicode slow path
  13957. else
  13958. {
  13959. traits |= formatted_string_traits::non_ascii;
  13960. utf8_decoder decoder;
  13961. // if the unicode is malformed just treat the string as a single-line non-literal and
  13962. // escape all non-ascii characters (to ensure round-tripping and help with diagnostics)
  13963. const auto bad_unicode = [&]() noexcept
  13964. {
  13965. traits &= ~formatted_string_traits::line_breaks;
  13966. traits |= formatted_string_traits::control_chars | formatted_string_traits::non_bare;
  13967. unicode_allowed = false;
  13968. };
  13969. for (auto c : str)
  13970. {
  13971. decoder(c);
  13972. if TOML_UNLIKELY(decoder.error())
  13973. {
  13974. bad_unicode();
  13975. break;
  13976. }
  13977. if (!decoder.has_code_point())
  13978. continue;
  13979. switch (decoder.codepoint)
  13980. {
  13981. case U'\n': traits |= formatted_string_traits::line_breaks; break;
  13982. case U'\t': traits |= formatted_string_traits::tabs; break;
  13983. case U'\'': traits |= formatted_string_traits::single_quotes; break;
  13984. default:
  13985. {
  13986. if TOML_UNLIKELY(is_control_character(decoder.codepoint)
  13987. || is_non_ascii_vertical_whitespace(decoder.codepoint))
  13988. traits |= formatted_string_traits::control_chars;
  13989. if (!is_bare_key_character(decoder.codepoint))
  13990. traits |= formatted_string_traits::non_bare;
  13991. break;
  13992. }
  13993. }
  13994. }
  13995. if (decoder.needs_more_input())
  13996. bad_unicode();
  13997. }
  13998. // strings with line breaks, tabs, and single-quotes can't be bare
  13999. if (!!(traits
  14000. & (formatted_string_traits::line_breaks | formatted_string_traits::tabs
  14001. | formatted_string_traits::single_quotes)))
  14002. traits |= formatted_string_traits::non_bare;
  14003. // if the string meets the requirements of being 'bare' we can emit a bare string
  14004. // (bare strings are composed of letters and numbers; no whitespace, control chars, quotes, etc)
  14005. if (!(traits & formatted_string_traits::non_bare)
  14006. && (!(traits & formatted_string_traits::non_ascii) || unicode_allowed))
  14007. {
  14008. print_unformatted(str);
  14009. return;
  14010. }
  14011. const auto real_tabs_allowed = allow_literal_whitespace && real_tabs_in_strings_allowed();
  14012. // determine if this should be a multi-line string (triple-quotes)
  14013. const auto multi_line = allow_literal_whitespace //
  14014. && allow_multi_line //
  14015. && multi_line_strings_allowed() //
  14016. && !!(traits & formatted_string_traits::line_breaks);
  14017. // determine if this should be a literal string (single-quotes with no escaping)
  14018. const auto literal = literal_strings_allowed() //
  14019. && !(traits & formatted_string_traits::control_chars) //
  14020. && (!(traits & formatted_string_traits::single_quotes) || multi_line) //
  14021. && (!(traits & formatted_string_traits::tabs) || real_tabs_allowed) //
  14022. && (!(traits & formatted_string_traits::line_breaks) || multi_line) //
  14023. && (!(traits & formatted_string_traits::non_ascii) || unicode_allowed);
  14024. // literal strings (single quotes, no escape codes)
  14025. if (literal)
  14026. {
  14027. const auto quot = multi_line ? R"(''')"sv : R"(')"sv;
  14028. print_unformatted(quot);
  14029. print_unformatted(str);
  14030. print_unformatted(quot);
  14031. return;
  14032. }
  14033. // anything from here down is a non-literal string, so requires iteration and escaping.
  14034. print_unformatted(multi_line ? R"(""")"sv : R"(")"sv);
  14035. // ascii fast path
  14036. if (!(traits & formatted_string_traits::non_ascii))
  14037. {
  14038. for (auto c : str)
  14039. {
  14040. switch (c)
  14041. {
  14042. case '"': print_to_stream(*stream_, R"(\")"sv); break;
  14043. case '\\': print_to_stream(*stream_, R"(\\)"sv); break;
  14044. case '\x7F': print_to_stream(*stream_, R"(\u007F)"sv); break;
  14045. case '\t': print_to_stream(*stream_, real_tabs_allowed ? "\t"sv : R"(\t)"sv); break;
  14046. case '\n': print_to_stream(*stream_, multi_line ? "\n"sv : R"(\n)"sv); break;
  14047. default:
  14048. {
  14049. // control characters from lookup table
  14050. if TOML_UNLIKELY(c >= '\x00' && c <= '\x1F')
  14051. print_to_stream(*stream_, control_char_escapes[c]);
  14052. // regular characters
  14053. else
  14054. print_to_stream(*stream_, c);
  14055. }
  14056. }
  14057. }
  14058. }
  14059. // unicode slow path
  14060. else
  14061. {
  14062. utf8_decoder decoder;
  14063. const char* cp_start = str.data();
  14064. const char* cp_end = cp_start;
  14065. for (auto c : str)
  14066. {
  14067. decoder(c);
  14068. cp_end++;
  14069. // if the decoder encounters malformed unicode just emit raw bytes and
  14070. if (decoder.error())
  14071. {
  14072. while (cp_start != cp_end)
  14073. {
  14074. print_to_stream(*stream_, R"(\u00)"sv);
  14075. print_to_stream(*stream_,
  14076. static_cast<uint8_t>(*cp_start),
  14077. value_flags::format_as_hexadecimal,
  14078. 2);
  14079. cp_start++;
  14080. }
  14081. decoder.reset();
  14082. continue;
  14083. }
  14084. if (!decoder.has_code_point())
  14085. continue;
  14086. switch (decoder.codepoint)
  14087. {
  14088. case U'"': print_to_stream(*stream_, R"(\")"sv); break;
  14089. case U'\\': print_to_stream(*stream_, R"(\\)"sv); break;
  14090. case U'\x7F': print_to_stream(*stream_, R"(\u007F)"sv); break;
  14091. case U'\t': print_to_stream(*stream_, real_tabs_allowed ? "\t"sv : R"(\t)"sv); break;
  14092. case U'\n': print_to_stream(*stream_, multi_line ? "\n"sv : R"(\n)"sv); break;
  14093. default:
  14094. {
  14095. // control characters from lookup table
  14096. if TOML_UNLIKELY(decoder.codepoint <= U'\x1F')
  14097. print_to_stream(*stream_,
  14098. control_char_escapes[static_cast<uint_least32_t>(decoder.codepoint)]);
  14099. // escaped unicode characters
  14100. else if (decoder.codepoint > U'\x7F'
  14101. && (!unicode_allowed || is_non_ascii_vertical_whitespace(decoder.codepoint)))
  14102. {
  14103. if (static_cast<uint_least32_t>(decoder.codepoint) > 0xFFFFu)
  14104. {
  14105. print_to_stream(*stream_, R"(\U)"sv);
  14106. print_to_stream(*stream_,
  14107. static_cast<uint_least32_t>(decoder.codepoint),
  14108. value_flags::format_as_hexadecimal,
  14109. 8);
  14110. }
  14111. else
  14112. {
  14113. print_to_stream(*stream_, R"(\u)"sv);
  14114. print_to_stream(*stream_,
  14115. static_cast<uint_least32_t>(decoder.codepoint),
  14116. value_flags::format_as_hexadecimal,
  14117. 4);
  14118. }
  14119. }
  14120. // regular characters
  14121. else
  14122. print_to_stream(*stream_, cp_start, static_cast<size_t>(cp_end - cp_start));
  14123. }
  14124. }
  14125. cp_start = cp_end;
  14126. }
  14127. }
  14128. print_unformatted(multi_line ? R"(""")"sv : R"(")"sv);
  14129. }
  14130. TOML_EXTERNAL_LINKAGE
  14131. void formatter::print(const value<std::string>& val)
  14132. {
  14133. print_string(val.get());
  14134. }
  14135. TOML_EXTERNAL_LINKAGE
  14136. void formatter::print(const value<int64_t>& val)
  14137. {
  14138. naked_newline_ = false;
  14139. if (*val >= 0 && !!int_format_mask_)
  14140. {
  14141. static constexpr auto value_flags_mask =
  14142. value_flags::format_as_binary | value_flags::format_as_octal | value_flags::format_as_hexadecimal;
  14143. const auto fmt = val.flags() & value_flags_mask;
  14144. switch (fmt)
  14145. {
  14146. case value_flags::format_as_binary:
  14147. if (!!(int_format_mask_ & format_flags::allow_binary_integers))
  14148. {
  14149. print_to_stream(*stream_, "0b"sv);
  14150. print_to_stream(*stream_, *val, fmt);
  14151. return;
  14152. }
  14153. break;
  14154. case value_flags::format_as_octal:
  14155. if (!!(int_format_mask_ & format_flags::allow_octal_integers))
  14156. {
  14157. print_to_stream(*stream_, "0o"sv);
  14158. print_to_stream(*stream_, *val, fmt);
  14159. return;
  14160. }
  14161. break;
  14162. case value_flags::format_as_hexadecimal:
  14163. if (!!(int_format_mask_ & format_flags::allow_hexadecimal_integers))
  14164. {
  14165. print_to_stream(*stream_, "0x"sv);
  14166. print_to_stream(*stream_, *val, fmt);
  14167. return;
  14168. }
  14169. break;
  14170. default: break;
  14171. }
  14172. }
  14173. // fallback to decimal
  14174. print_to_stream(*stream_, *val);
  14175. }
  14176. TOML_EXTERNAL_LINKAGE
  14177. void formatter::print(const value<double>& val)
  14178. {
  14179. const std::string_view* inf_nan = nullptr;
  14180. switch (fpclassify(*val))
  14181. {
  14182. case fp_class::neg_inf: inf_nan = &constants_->float_neg_inf; break;
  14183. case fp_class::pos_inf: inf_nan = &constants_->float_pos_inf; break;
  14184. case fp_class::nan: inf_nan = &constants_->float_nan; break;
  14185. case fp_class::ok:
  14186. print_to_stream(*stream_,
  14187. *val,
  14188. value_flags::none,
  14189. !!(config_.flags & format_flags::relaxed_float_precision));
  14190. break;
  14191. default: TOML_UNREACHABLE;
  14192. }
  14193. if (inf_nan)
  14194. {
  14195. if (!!(config_.flags & format_flags::quote_infinities_and_nans))
  14196. print_to_stream_bookended(*stream_, *inf_nan, '"');
  14197. else
  14198. print_to_stream(*stream_, *inf_nan);
  14199. }
  14200. naked_newline_ = false;
  14201. }
  14202. TOML_EXTERNAL_LINKAGE
  14203. void formatter::print(const value<bool>& val)
  14204. {
  14205. print_unformatted(*val ? constants_->bool_true : constants_->bool_false);
  14206. }
  14207. TOML_EXTERNAL_LINKAGE
  14208. void formatter::print(const value<date>& val)
  14209. {
  14210. if (!!(config_.flags & format_flags::quote_dates_and_times))
  14211. print_to_stream_bookended(*stream_, *val, literal_strings_allowed() ? '\'' : '"');
  14212. else
  14213. print_to_stream(*stream_, *val);
  14214. naked_newline_ = false;
  14215. }
  14216. TOML_EXTERNAL_LINKAGE
  14217. void formatter::print(const value<time>& val)
  14218. {
  14219. if (!!(config_.flags & format_flags::quote_dates_and_times))
  14220. print_to_stream_bookended(*stream_, *val, literal_strings_allowed() ? '\'' : '"');
  14221. else
  14222. print_to_stream(*stream_, *val);
  14223. naked_newline_ = false;
  14224. }
  14225. TOML_EXTERNAL_LINKAGE
  14226. void formatter::print(const value<date_time>& val)
  14227. {
  14228. if (!!(config_.flags & format_flags::quote_dates_and_times))
  14229. print_to_stream_bookended(*stream_, *val, literal_strings_allowed() ? '\'' : '"');
  14230. else
  14231. print_to_stream(*stream_, *val);
  14232. naked_newline_ = false;
  14233. }
  14234. TOML_EXTERNAL_LINKAGE
  14235. void formatter::print_value(const node& val_node, node_type type)
  14236. {
  14237. TOML_ASSUME(type > node_type::array);
  14238. switch (type)
  14239. {
  14240. case node_type::string: print(*reinterpret_cast<const value<std::string>*>(&val_node)); break;
  14241. case node_type::integer: print(*reinterpret_cast<const value<int64_t>*>(&val_node)); break;
  14242. case node_type::floating_point: print(*reinterpret_cast<const value<double>*>(&val_node)); break;
  14243. case node_type::boolean: print(*reinterpret_cast<const value<bool>*>(&val_node)); break;
  14244. case node_type::date: print(*reinterpret_cast<const value<date>*>(&val_node)); break;
  14245. case node_type::time: print(*reinterpret_cast<const value<time>*>(&val_node)); break;
  14246. case node_type::date_time: print(*reinterpret_cast<const value<date_time>*>(&val_node)); break;
  14247. default: TOML_UNREACHABLE;
  14248. }
  14249. }
  14250. #if TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
  14251. TOML_EXTERNAL_LINKAGE
  14252. bool formatter::dump_failed_parse_result()
  14253. {
  14254. if (result_ && !(*result_))
  14255. {
  14256. stream() << result_->error();
  14257. return true;
  14258. }
  14259. return false;
  14260. }
  14261. #else
  14262. TOML_EXTERNAL_LINKAGE
  14263. TOML_ATTR(const)
  14264. bool formatter::dump_failed_parse_result()
  14265. {
  14266. return false;
  14267. }
  14268. #endif
  14269. }
  14270. TOML_IMPL_NAMESPACE_END;
  14271. #ifdef _MSC_VER
  14272. #pragma pop_macro("min")
  14273. #pragma pop_macro("max")
  14274. #ifndef __clang__
  14275. #pragma inline_recursion(off)
  14276. #endif
  14277. #endif
  14278. TOML_POP_WARNINGS;
  14279. #endif // TOML_ENABLE_FORMATTERS
  14280. //******** impl/toml_formatter.inl ***********************************************************************************
  14281. #if TOML_ENABLE_FORMATTERS
  14282. TOML_PUSH_WARNINGS;
  14283. #ifdef _MSC_VER
  14284. #ifndef __clang__
  14285. #pragma inline_recursion(on)
  14286. #endif
  14287. #pragma push_macro("min")
  14288. #pragma push_macro("max")
  14289. #undef min
  14290. #undef max
  14291. #endif
  14292. TOML_DISABLE_ARITHMETIC_WARNINGS;
  14293. TOML_ANON_NAMESPACE_START
  14294. {
  14295. TOML_INTERNAL_LINKAGE
  14296. size_t toml_formatter_count_inline_columns(const node& node, size_t line_wrap_cols) noexcept
  14297. {
  14298. switch (node.type())
  14299. {
  14300. case node_type::table:
  14301. {
  14302. auto& tbl = *reinterpret_cast<const table*>(&node);
  14303. if (tbl.empty())
  14304. return 2u; // "{}"
  14305. size_t weight = 3u; // "{ }"
  14306. for (auto&& [k, v] : tbl)
  14307. {
  14308. weight += k.length() + toml_formatter_count_inline_columns(v, line_wrap_cols) + 2u; // + ", "
  14309. if (weight >= line_wrap_cols)
  14310. break;
  14311. }
  14312. return weight;
  14313. }
  14314. case node_type::array:
  14315. {
  14316. auto& arr = *reinterpret_cast<const array*>(&node);
  14317. if (arr.empty())
  14318. return 2u; // "[]"
  14319. size_t weight = 3u; // "[ ]"
  14320. for (auto& elem : arr)
  14321. {
  14322. weight += toml_formatter_count_inline_columns(elem, line_wrap_cols) + 2u; // + ", "
  14323. if (weight >= line_wrap_cols)
  14324. break;
  14325. }
  14326. return weight;
  14327. }
  14328. case node_type::string:
  14329. {
  14330. // todo: proper utf8 decoding?
  14331. // todo: tab awareness?
  14332. auto& str = (*reinterpret_cast<const value<std::string>*>(&node)).get();
  14333. return str.length() + 2u; // + ""
  14334. }
  14335. case node_type::integer:
  14336. {
  14337. auto val = (*reinterpret_cast<const value<int64_t>*>(&node)).get();
  14338. if (!val)
  14339. return 1u;
  14340. size_t weight = {};
  14341. if (val < 0)
  14342. {
  14343. weight += 1u;
  14344. val *= -1;
  14345. }
  14346. return weight + static_cast<size_t>(log10(static_cast<double>(val))) + 1u;
  14347. }
  14348. case node_type::floating_point:
  14349. {
  14350. auto val = (*reinterpret_cast<const value<double>*>(&node)).get();
  14351. if (val == 0.0)
  14352. return 3u; // "0.0"
  14353. size_t weight = 2u; // ".0"
  14354. if (val < 0.0)
  14355. {
  14356. weight += 1u;
  14357. val *= -1.0;
  14358. }
  14359. return weight + static_cast<size_t>(log10(val)) + 1u;
  14360. break;
  14361. }
  14362. case node_type::boolean: return 5u;
  14363. case node_type::date: [[fallthrough]];
  14364. case node_type::time: return 10u;
  14365. case node_type::date_time: return 30u;
  14366. case node_type::none: TOML_UNREACHABLE;
  14367. default: TOML_UNREACHABLE;
  14368. }
  14369. TOML_UNREACHABLE;
  14370. }
  14371. TOML_INTERNAL_LINKAGE
  14372. bool toml_formatter_forces_multiline(const node& node, size_t line_wrap_cols, size_t starting_column_bias) noexcept
  14373. {
  14374. return (toml_formatter_count_inline_columns(node, line_wrap_cols) + starting_column_bias) >= line_wrap_cols;
  14375. }
  14376. }
  14377. TOML_ANON_NAMESPACE_END;
  14378. TOML_NAMESPACE_START
  14379. {
  14380. TOML_EXTERNAL_LINKAGE
  14381. void toml_formatter::print_pending_table_separator()
  14382. {
  14383. if (pending_table_separator_)
  14384. {
  14385. print_newline(true);
  14386. print_newline(true);
  14387. pending_table_separator_ = false;
  14388. }
  14389. }
  14390. TOML_EXTERNAL_LINKAGE
  14391. void toml_formatter::print(const key& k)
  14392. {
  14393. print_string(k.str(), false, true, false);
  14394. }
  14395. TOML_EXTERNAL_LINKAGE
  14396. void toml_formatter::print_inline(const table& tbl)
  14397. {
  14398. if (tbl.empty())
  14399. {
  14400. print_unformatted("{}"sv);
  14401. return;
  14402. }
  14403. print_unformatted("{ "sv);
  14404. bool first = false;
  14405. for (auto&& [k, v] : tbl)
  14406. {
  14407. if (first)
  14408. print_unformatted(", "sv);
  14409. first = true;
  14410. print(k);
  14411. if (terse_kvps())
  14412. print_unformatted("="sv);
  14413. else
  14414. print_unformatted(" = "sv);
  14415. const auto type = v.type();
  14416. TOML_ASSUME(type != node_type::none);
  14417. switch (type)
  14418. {
  14419. case node_type::table: print_inline(*reinterpret_cast<const table*>(&v)); break;
  14420. case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
  14421. default: print_value(v, type);
  14422. }
  14423. }
  14424. print_unformatted(" }"sv);
  14425. }
  14426. TOML_EXTERNAL_LINKAGE
  14427. void toml_formatter::print(const array& arr)
  14428. {
  14429. if (arr.empty())
  14430. {
  14431. print_unformatted("[]"sv);
  14432. return;
  14433. }
  14434. const auto original_indent = indent();
  14435. const auto multiline = TOML_ANON_NAMESPACE::toml_formatter_forces_multiline(
  14436. arr,
  14437. 120u,
  14438. indent_columns() * static_cast<size_t>(original_indent < 0 ? 0 : original_indent));
  14439. print_unformatted("["sv);
  14440. if (multiline)
  14441. {
  14442. if (original_indent < 0)
  14443. indent(0);
  14444. if (indent_array_elements())
  14445. increase_indent();
  14446. }
  14447. else
  14448. print_unformatted(' ');
  14449. for (size_t i = 0; i < arr.size(); i++)
  14450. {
  14451. if (i > 0u)
  14452. {
  14453. print_unformatted(',');
  14454. if (!multiline)
  14455. print_unformatted(' ');
  14456. }
  14457. if (multiline)
  14458. {
  14459. print_newline(true);
  14460. print_indent();
  14461. }
  14462. auto& v = arr[i];
  14463. const auto type = v.type();
  14464. TOML_ASSUME(type != node_type::none);
  14465. switch (type)
  14466. {
  14467. case node_type::table: print_inline(*reinterpret_cast<const table*>(&v)); break;
  14468. case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
  14469. default: print_value(v, type);
  14470. }
  14471. }
  14472. if (multiline)
  14473. {
  14474. indent(original_indent);
  14475. print_newline(true);
  14476. print_indent();
  14477. }
  14478. else
  14479. print_unformatted(' ');
  14480. print_unformatted("]"sv);
  14481. }
  14482. TOML_EXTERNAL_LINKAGE
  14483. void toml_formatter::print(const table& tbl)
  14484. {
  14485. static constexpr auto is_non_inline_array_of_tables = [](const node& n) noexcept
  14486. {
  14487. const auto arr = n.as_array();
  14488. if (!arr || !arr->is_array_of_tables())
  14489. return false;
  14490. return !reinterpret_cast<const table*>(&(*arr)[0])->is_inline();
  14491. };
  14492. // values, arrays, and inline tables/table arrays
  14493. for (auto&& [k, v] : tbl)
  14494. {
  14495. const auto type = v.type();
  14496. if ((type == node_type::table && !reinterpret_cast<const table*>(&v)->is_inline())
  14497. || (type == node_type::array && is_non_inline_array_of_tables(v)))
  14498. continue;
  14499. pending_table_separator_ = true;
  14500. print_newline();
  14501. print_indent();
  14502. print(k);
  14503. if (terse_kvps())
  14504. print_unformatted("="sv);
  14505. else
  14506. print_unformatted(" = "sv);
  14507. TOML_ASSUME(type != node_type::none);
  14508. switch (type)
  14509. {
  14510. case node_type::table: print_inline(*reinterpret_cast<const table*>(&v)); break;
  14511. case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
  14512. default: print_value(v, type);
  14513. }
  14514. }
  14515. const auto print_key_path = [&]()
  14516. {
  14517. size_t i{};
  14518. for (const auto k : key_path_)
  14519. {
  14520. if (i++)
  14521. print_unformatted('.');
  14522. print(*k);
  14523. }
  14524. };
  14525. // non-inline tables
  14526. for (auto&& [k, v] : tbl)
  14527. {
  14528. const auto type = v.type();
  14529. if (type != node_type::table || reinterpret_cast<const table*>(&v)->is_inline())
  14530. continue;
  14531. auto& child_tbl = *reinterpret_cast<const table*>(&v);
  14532. // we can skip indenting and emitting the headers for tables that only contain other tables
  14533. // (so we don't over-nest)
  14534. size_t child_value_count{}; // includes inline tables and non-table arrays
  14535. size_t child_table_count{};
  14536. size_t child_table_array_count{};
  14537. for (auto&& [child_k, child_v] : child_tbl)
  14538. {
  14539. TOML_UNUSED(child_k);
  14540. const auto child_type = child_v.type();
  14541. TOML_ASSUME(child_type != node_type::none);
  14542. switch (child_type)
  14543. {
  14544. case node_type::table:
  14545. if (reinterpret_cast<const table*>(&child_v)->is_inline())
  14546. child_value_count++;
  14547. else
  14548. child_table_count++;
  14549. break;
  14550. case node_type::array:
  14551. if (is_non_inline_array_of_tables(child_v))
  14552. child_table_array_count++;
  14553. else
  14554. child_value_count++;
  14555. break;
  14556. default: child_value_count++;
  14557. }
  14558. }
  14559. bool skip_self = false;
  14560. if (child_value_count == 0u && (child_table_count > 0u || child_table_array_count > 0u))
  14561. skip_self = true;
  14562. key_path_.push_back(&k);
  14563. if (!skip_self)
  14564. {
  14565. print_pending_table_separator();
  14566. if (indent_sub_tables())
  14567. increase_indent();
  14568. print_indent();
  14569. print_unformatted("["sv);
  14570. print_key_path();
  14571. print_unformatted("]"sv);
  14572. pending_table_separator_ = true;
  14573. }
  14574. print(child_tbl);
  14575. key_path_.pop_back();
  14576. if (!skip_self && indent_sub_tables())
  14577. decrease_indent();
  14578. }
  14579. // table arrays
  14580. for (auto&& [k, v] : tbl)
  14581. {
  14582. if (!is_non_inline_array_of_tables(v))
  14583. continue;
  14584. auto& arr = *reinterpret_cast<const array*>(&v);
  14585. if (indent_sub_tables())
  14586. increase_indent();
  14587. key_path_.push_back(&k);
  14588. for (size_t i = 0; i < arr.size(); i++)
  14589. {
  14590. print_pending_table_separator();
  14591. print_indent();
  14592. print_unformatted("[["sv);
  14593. print_key_path();
  14594. print_unformatted("]]"sv);
  14595. pending_table_separator_ = true;
  14596. print(*reinterpret_cast<const table*>(&arr[i]));
  14597. }
  14598. key_path_.pop_back();
  14599. if (indent_sub_tables())
  14600. decrease_indent();
  14601. }
  14602. }
  14603. TOML_EXTERNAL_LINKAGE
  14604. void toml_formatter::print()
  14605. {
  14606. if (dump_failed_parse_result())
  14607. return;
  14608. switch (auto source_type = source().type())
  14609. {
  14610. case node_type::table:
  14611. {
  14612. auto& tbl = *reinterpret_cast<const table*>(&source());
  14613. if (tbl.is_inline())
  14614. print_inline(tbl);
  14615. else
  14616. {
  14617. decrease_indent(); // so root kvps and tables have the same indent
  14618. print(tbl);
  14619. }
  14620. break;
  14621. }
  14622. case node_type::array: print(*reinterpret_cast<const array*>(&source())); break;
  14623. default: print_value(source(), source_type);
  14624. }
  14625. }
  14626. }
  14627. TOML_NAMESPACE_END;
  14628. #ifdef _MSC_VER
  14629. #pragma pop_macro("min")
  14630. #pragma pop_macro("max")
  14631. #ifndef __clang__
  14632. #pragma inline_recursion(off)
  14633. #endif
  14634. #endif
  14635. TOML_POP_WARNINGS;
  14636. #endif // TOML_ENABLE_FORMATTERS
  14637. //******** impl/json_formatter.inl ***********************************************************************************
  14638. #if TOML_ENABLE_FORMATTERS
  14639. TOML_PUSH_WARNINGS;
  14640. #ifdef _MSC_VER
  14641. #ifndef __clang__
  14642. #pragma inline_recursion(on)
  14643. #endif
  14644. #pragma push_macro("min")
  14645. #pragma push_macro("max")
  14646. #undef min
  14647. #undef max
  14648. #endif
  14649. TOML_NAMESPACE_START
  14650. {
  14651. TOML_EXTERNAL_LINKAGE
  14652. void json_formatter::print(const toml::table& tbl)
  14653. {
  14654. if (tbl.empty())
  14655. {
  14656. print_unformatted("{}"sv);
  14657. return;
  14658. }
  14659. print_unformatted('{');
  14660. if (indent_sub_tables())
  14661. increase_indent();
  14662. bool first = false;
  14663. for (auto&& [k, v] : tbl)
  14664. {
  14665. if (first)
  14666. print_unformatted(',');
  14667. first = true;
  14668. print_newline(true);
  14669. print_indent();
  14670. print_string(k.str(), false);
  14671. if (terse_kvps())
  14672. print_unformatted(":"sv);
  14673. else
  14674. print_unformatted(" : "sv);
  14675. const auto type = v.type();
  14676. TOML_ASSUME(type != node_type::none);
  14677. switch (type)
  14678. {
  14679. case node_type::table: print(*reinterpret_cast<const table*>(&v)); break;
  14680. case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
  14681. default: print_value(v, type);
  14682. }
  14683. }
  14684. if (indent_sub_tables())
  14685. decrease_indent();
  14686. print_newline(true);
  14687. print_indent();
  14688. print_unformatted('}');
  14689. }
  14690. TOML_EXTERNAL_LINKAGE
  14691. void json_formatter::print(const toml::array& arr)
  14692. {
  14693. if (arr.empty())
  14694. {
  14695. print_unformatted("[]"sv);
  14696. return;
  14697. }
  14698. print_unformatted('[');
  14699. if (indent_array_elements())
  14700. increase_indent();
  14701. for (size_t i = 0; i < arr.size(); i++)
  14702. {
  14703. if (i > 0u)
  14704. print_unformatted(',');
  14705. print_newline(true);
  14706. print_indent();
  14707. auto& v = arr[i];
  14708. const auto type = v.type();
  14709. TOML_ASSUME(type != node_type::none);
  14710. switch (type)
  14711. {
  14712. case node_type::table: print(*reinterpret_cast<const table*>(&v)); break;
  14713. case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
  14714. default: print_value(v, type);
  14715. }
  14716. }
  14717. if (indent_array_elements())
  14718. decrease_indent();
  14719. print_newline(true);
  14720. print_indent();
  14721. print_unformatted(']');
  14722. }
  14723. TOML_EXTERNAL_LINKAGE
  14724. void json_formatter::print()
  14725. {
  14726. if (dump_failed_parse_result())
  14727. return;
  14728. switch (auto source_type = source().type())
  14729. {
  14730. case node_type::table: print(*reinterpret_cast<const table*>(&source())); break;
  14731. case node_type::array: print(*reinterpret_cast<const array*>(&source())); break;
  14732. default: print_value(source(), source_type);
  14733. }
  14734. }
  14735. }
  14736. TOML_NAMESPACE_END;
  14737. #ifdef _MSC_VER
  14738. #pragma pop_macro("min")
  14739. #pragma pop_macro("max")
  14740. #ifndef __clang__
  14741. #pragma inline_recursion(off)
  14742. #endif
  14743. #endif
  14744. TOML_POP_WARNINGS;
  14745. #endif // TOML_ENABLE_FORMATTERS
  14746. //******** impl/yaml_formatter.inl ***********************************************************************************
  14747. #if TOML_ENABLE_FORMATTERS
  14748. TOML_PUSH_WARNINGS;
  14749. #ifdef _MSC_VER
  14750. #ifndef __clang__
  14751. #pragma inline_recursion(on)
  14752. #endif
  14753. #pragma push_macro("min")
  14754. #pragma push_macro("max")
  14755. #undef min
  14756. #undef max
  14757. #endif
  14758. TOML_NAMESPACE_START
  14759. {
  14760. TOML_EXTERNAL_LINKAGE
  14761. void yaml_formatter::print_yaml_string(const value<std::string>& str)
  14762. {
  14763. if (str->empty())
  14764. {
  14765. base::print(str);
  14766. return;
  14767. }
  14768. bool contains_newline = false;
  14769. for (auto c = str->c_str(), e = str->c_str() + str->length(); c < e && !contains_newline; c++)
  14770. contains_newline = *c == '\n';
  14771. if (contains_newline)
  14772. {
  14773. print_unformatted("|-"sv);
  14774. increase_indent();
  14775. auto line_end = str->c_str() - 1u;
  14776. const auto end = str->c_str() + str->length();
  14777. while (line_end != end)
  14778. {
  14779. auto line_start = line_end + 1u;
  14780. line_end = line_start;
  14781. for (; line_end != end && *line_end != '\n'; line_end++)
  14782. ;
  14783. if TOML_LIKELY(line_start != line_end || line_end != end)
  14784. {
  14785. print_newline();
  14786. print_indent();
  14787. print_unformatted(std::string_view{ line_start, static_cast<size_t>(line_end - line_start) });
  14788. }
  14789. }
  14790. decrease_indent();
  14791. }
  14792. else
  14793. print_string(*str, false, true);
  14794. }
  14795. TOML_EXTERNAL_LINKAGE
  14796. void yaml_formatter::print(const toml::table& tbl, bool parent_is_array)
  14797. {
  14798. if (tbl.empty())
  14799. {
  14800. print_unformatted("{}"sv);
  14801. return;
  14802. }
  14803. increase_indent();
  14804. for (auto&& [k, v] : tbl)
  14805. {
  14806. if (!parent_is_array)
  14807. {
  14808. print_newline();
  14809. print_indent();
  14810. }
  14811. parent_is_array = false;
  14812. print_string(k.str(), false, true);
  14813. if (terse_kvps())
  14814. print_unformatted(":"sv);
  14815. else
  14816. print_unformatted(": "sv);
  14817. const auto type = v.type();
  14818. TOML_ASSUME(type != node_type::none);
  14819. switch (type)
  14820. {
  14821. case node_type::table: print(*reinterpret_cast<const table*>(&v)); break;
  14822. case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
  14823. case node_type::string: print_yaml_string(*reinterpret_cast<const value<std::string>*>(&v)); break;
  14824. default: print_value(v, type);
  14825. }
  14826. }
  14827. decrease_indent();
  14828. }
  14829. TOML_EXTERNAL_LINKAGE
  14830. void yaml_formatter::print(const toml::array& arr, bool parent_is_array)
  14831. {
  14832. if (arr.empty())
  14833. {
  14834. print_unformatted("[]"sv);
  14835. return;
  14836. }
  14837. increase_indent();
  14838. for (auto&& v : arr)
  14839. {
  14840. if (!parent_is_array)
  14841. {
  14842. print_newline();
  14843. print_indent();
  14844. }
  14845. parent_is_array = false;
  14846. print_unformatted("- "sv);
  14847. const auto type = v.type();
  14848. TOML_ASSUME(type != node_type::none);
  14849. switch (type)
  14850. {
  14851. case node_type::table: print(*reinterpret_cast<const table*>(&v), true); break;
  14852. case node_type::array: print(*reinterpret_cast<const array*>(&v), true); break;
  14853. case node_type::string: print_yaml_string(*reinterpret_cast<const value<std::string>*>(&v)); break;
  14854. default: print_value(v, type);
  14855. }
  14856. }
  14857. decrease_indent();
  14858. }
  14859. TOML_EXTERNAL_LINKAGE
  14860. void yaml_formatter::print()
  14861. {
  14862. if (dump_failed_parse_result())
  14863. return;
  14864. switch (auto source_type = source().type())
  14865. {
  14866. case node_type::table:
  14867. decrease_indent(); // so root kvps and tables have the same indent
  14868. print(*reinterpret_cast<const table*>(&source()));
  14869. break;
  14870. case node_type::array: print(*reinterpret_cast<const array*>(&source())); break;
  14871. case node_type::string: print_yaml_string(*reinterpret_cast<const value<std::string>*>(&source())); break;
  14872. default: print_value(source(), source_type);
  14873. }
  14874. }
  14875. }
  14876. TOML_NAMESPACE_END;
  14877. #ifdef _MSC_VER
  14878. #pragma pop_macro("min")
  14879. #pragma pop_macro("max")
  14880. #ifndef __clang__
  14881. #pragma inline_recursion(off)
  14882. #endif
  14883. #endif
  14884. TOML_POP_WARNINGS;
  14885. #endif // TOML_ENABLE_FORMATTERS
  14886. #endif // TOML_IMPLEMENTATION
  14887. TOML_POP_WARNINGS;
  14888. // macro hygiene
  14889. #if TOML_UNDEF_MACROS
  14890. #undef TOML_ABI_NAMESPACE_BOOL
  14891. #undef TOML_ABI_NAMESPACE_END
  14892. #undef TOML_ABI_NAMESPACE_START
  14893. #undef TOML_ABI_NAMESPACES
  14894. #undef TOML_ABSTRACT_INTERFACE
  14895. #undef TOML_ALWAYS_INLINE
  14896. #undef TOML_ANON_NAMESPACE
  14897. #undef TOML_ANON_NAMESPACE_END
  14898. #undef TOML_ANON_NAMESPACE_START
  14899. #undef TOML_ARCH_AMD64
  14900. #undef TOML_ARCH_ARM
  14901. #undef TOML_ARCH_ARM32
  14902. #undef TOML_ARCH_ARM64
  14903. #undef TOML_ARCH_BITNESS
  14904. #undef TOML_ARCH_ITANIUM
  14905. #undef TOML_ARCH_X64
  14906. #undef TOML_ARCH_X86
  14907. #undef TOML_ASSERT
  14908. #undef TOML_ASSERT_ASSUME
  14909. #undef TOML_ASSUME
  14910. #undef TOML_ASYMMETRICAL_EQUALITY_OPS
  14911. #undef TOML_ATTR
  14912. #undef TOML_CLANG
  14913. #undef TOML_CLANG_VERSION
  14914. #undef TOML_CLOSED_ENUM
  14915. #undef TOML_CLOSED_FLAGS_ENUM
  14916. #undef TOML_COMPILER_HAS_EXCEPTIONS
  14917. #undef TOML_COMPILER_HAS_RTTI
  14918. #undef TOML_CONST
  14919. #undef TOML_CONST_GETTER
  14920. #undef TOML_CONST_INLINE_GETTER
  14921. #undef TOML_CONSTRAINED_TEMPLATE
  14922. #undef TOML_CPP
  14923. #undef TOML_DECLSPEC
  14924. #undef TOML_DELETE_DEFAULTS
  14925. #undef TOML_DISABLE_ARITHMETIC_WARNINGS
  14926. #undef TOML_DISABLE_CODE_ANALYSIS_WARNINGS
  14927. #undef TOML_DISABLE_SPAM_WARNINGS
  14928. #undef TOML_DISABLE_SPAM_WARNINGS_CLANG_10
  14929. #undef TOML_DISABLE_SPAM_WARNINGS_CLANG_11
  14930. #undef TOML_DISABLE_SUGGEST_ATTR_WARNINGS
  14931. #undef TOML_DISABLE_SWITCH_WARNINGS
  14932. #undef TOML_DISABLE_WARNINGS
  14933. #undef TOML_DOXYGEN
  14934. #undef TOML_EMPTY_BASES
  14935. #undef TOML_ENABLE_IF
  14936. #undef TOML_ENABLE_WARNINGS
  14937. #undef TOML_EVAL_BOOL_0
  14938. #undef TOML_EVAL_BOOL_1
  14939. #undef TOML_EXTERNAL_LINKAGE
  14940. #undef TOML_FLAGS_ENUM
  14941. #undef TOML_FLOAT_CHARCONV
  14942. #undef TOML_FLOAT128
  14943. #undef TOML_FLOAT16_DIG
  14944. #undef TOML_FLOAT16_LIMITS_SET
  14945. #undef TOML_FLOAT16_MANT_DIG
  14946. #undef TOML_FLOAT16_MAX_10_EXP
  14947. #undef TOML_FLOAT16_MAX_EXP
  14948. #undef TOML_FLOAT16_MIN_10_EXP
  14949. #undef TOML_FLOAT16_MIN_EXP
  14950. #undef TOML_GCC
  14951. #undef TOML_GCC_LIKE
  14952. #undef TOML_HAS_ATTR
  14953. #undef TOML_HAS_BUILTIN
  14954. #undef TOML_HAS_CHAR8
  14955. #undef TOML_HAS_CPP_ATTR
  14956. #undef TOML_HAS_CUSTOM_OPTIONAL_TYPE
  14957. #undef TOML_HAS_FEATURE
  14958. #undef TOML_HAS_INCLUDE
  14959. #undef TOML_HAS_SSE2
  14960. #undef TOML_HAS_SSE4_1
  14961. #undef TOML_HIDDEN_CONSTRAINT
  14962. #undef TOML_ICC
  14963. #undef TOML_ICC_CL
  14964. #undef TOML_IMPL_NAMESPACE_END
  14965. #undef TOML_IMPL_NAMESPACE_START
  14966. #undef TOML_IMPLEMENTATION
  14967. #undef TOML_INCLUDE_WINDOWS_H
  14968. #undef TOML_INLINE_GETTER
  14969. #undef TOML_INT_CHARCONV
  14970. #undef TOML_INT128
  14971. #undef TOML_INTELLISENSE
  14972. #undef TOML_INTERNAL_LINKAGE
  14973. #undef TOML_LANG_AT_LEAST
  14974. #undef TOML_LANG_EFFECTIVE_VERSION
  14975. #undef TOML_LANG_HIGHER_THAN
  14976. #undef TOML_LANG_UNRELEASED
  14977. #undef TOML_LAUNDER
  14978. #undef TOML_LIFETIME_HOOKS
  14979. #undef TOML_LIKELY
  14980. #undef TOML_LIKELY_CASE
  14981. #undef TOML_LINUX
  14982. #undef TOML_MAKE_FLAGS
  14983. #undef TOML_MAKE_FLAGS_
  14984. #undef TOML_MAKE_FLAGS_1
  14985. #undef TOML_MAKE_FLAGS_2
  14986. #undef TOML_MAKE_STRING
  14987. #undef TOML_MAKE_STRING_1
  14988. #undef TOML_MAKE_VERSION
  14989. #undef TOML_MSVC
  14990. #undef TOML_MSVC_LIKE
  14991. #undef TOML_NAMESPACE
  14992. #undef TOML_NEVER_INLINE
  14993. #undef TOML_NODISCARD
  14994. #undef TOML_NODISCARD_CTOR
  14995. #undef TOML_OPEN_ENUM
  14996. #undef TOML_OPEN_FLAGS_ENUM
  14997. #undef TOML_PARSER_TYPENAME
  14998. #undef TOML_POP_WARNINGS
  14999. #undef TOML_PRAGMA_CLANG
  15000. #undef TOML_PRAGMA_CLANG_GE_10
  15001. #undef TOML_PRAGMA_CLANG_GE_11
  15002. #undef TOML_PRAGMA_CLANG_GE_8
  15003. #undef TOML_PRAGMA_CLANG_GE_9
  15004. #undef TOML_PRAGMA_GCC
  15005. #undef TOML_PRAGMA_ICC
  15006. #undef TOML_PRAGMA_MSVC
  15007. #undef TOML_PURE
  15008. #undef TOML_PURE_GETTER
  15009. #undef TOML_PURE_INLINE_GETTER
  15010. #undef TOML_PUSH_WARNINGS
  15011. #undef TOML_REQUIRES
  15012. #undef TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN
  15013. #undef TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_MESSAGE
  15014. #undef TOML_SA_LIST_BEG
  15015. #undef TOML_SA_LIST_END
  15016. #undef TOML_SA_LIST_NEW
  15017. #undef TOML_SA_LIST_NXT
  15018. #undef TOML_SA_LIST_SEP
  15019. #undef TOML_SA_NATIVE_VALUE_TYPE_LIST
  15020. #undef TOML_SA_NEWLINE
  15021. #undef TOML_SA_NODE_TYPE_LIST
  15022. #undef TOML_SA_UNWRAPPED_NODE_TYPE_LIST
  15023. #undef TOML_SA_VALUE_EXACT_FUNC_MESSAGE
  15024. #undef TOML_SA_VALUE_FUNC_MESSAGE
  15025. #undef TOML_SA_VALUE_MESSAGE_CONST_CHAR8
  15026. #undef TOML_SA_VALUE_MESSAGE_U8STRING_VIEW
  15027. #undef TOML_SA_VALUE_MESSAGE_WSTRING
  15028. #undef TOML_SIMPLE_STATIC_ASSERT_MESSAGES
  15029. #undef TOML_TRIVIAL_ABI
  15030. #undef TOML_UINT128
  15031. #undef TOML_UNIX
  15032. #undef TOML_UNLIKELY
  15033. #undef TOML_UNLIKELY_CASE
  15034. #undef TOML_UNREACHABLE
  15035. #undef TOML_UNUSED
  15036. #undef TOML_WINDOWS
  15037. #endif
  15038. #endif // TOMLPLUSPLUS_HPP