pugixml.cpp 327 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451
  1. /**
  2. * pugixml parser - version 1.7
  3. * --------------------------------------------------------
  4. * Copyright (C) 2006-2015, by Arseny Kapoulkine ([email protected])
  5. * Report bugs and download new versions at http://pugixml.org/
  6. *
  7. * This library is distributed under the MIT License. See notice at the end
  8. * of this file.
  9. *
  10. * This work is based on the pugxml parser, which is:
  11. * Copyright (C) 2003, by Kristen Wegner ([email protected])
  12. */
  13. // Modified by Yao Wei Tjong for Urho3D
  14. #ifndef SOURCE_PUGIXML_CPP
  15. #define SOURCE_PUGIXML_CPP
  16. #include "pugixml.hpp"
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <assert.h>
  21. #include <limits.h>
  22. #ifdef PUGIXML_WCHAR_MODE
  23. # include <wchar.h>
  24. #endif
  25. #ifndef PUGIXML_NO_XPATH
  26. # include <math.h>
  27. # include <float.h>
  28. # ifdef PUGIXML_NO_EXCEPTIONS
  29. # include <setjmp.h>
  30. # endif
  31. #endif
  32. #ifndef PUGIXML_NO_STL
  33. # include <istream>
  34. # include <ostream>
  35. # include <string>
  36. #endif
  37. // For placement new
  38. #include <new>
  39. #ifdef _MSC_VER
  40. # pragma warning(push)
  41. # pragma warning(disable: 4127) // conditional expression is constant
  42. # pragma warning(disable: 4324) // structure was padded due to __declspec(align())
  43. # pragma warning(disable: 4611) // interaction between '_setjmp' and C++ object destruction is non-portable
  44. # pragma warning(disable: 4702) // unreachable code
  45. # pragma warning(disable: 4996) // this function or variable may be unsafe
  46. # pragma warning(disable: 4793) // function compiled as native: presence of '_setjmp' makes a function unmanaged
  47. #endif
  48. #ifdef __INTEL_COMPILER
  49. # pragma warning(disable: 177) // function was declared but never referenced
  50. # pragma warning(disable: 279) // controlling expression is constant
  51. # pragma warning(disable: 1478 1786) // function was declared "deprecated"
  52. # pragma warning(disable: 1684) // conversion from pointer to same-sized integral type
  53. #endif
  54. #if defined(__BORLANDC__) && defined(PUGIXML_HEADER_ONLY)
  55. # pragma warn -8080 // symbol is declared but never used; disabling this inside push/pop bracket does not make the warning go away
  56. #endif
  57. #ifdef __BORLANDC__
  58. # pragma option push
  59. # pragma warn -8008 // condition is always false
  60. # pragma warn -8066 // unreachable code
  61. #endif
  62. #ifdef __SNC__
  63. // Using diag_push/diag_pop does not disable the warnings inside templates due to a compiler bug
  64. # pragma diag_suppress=178 // function was declared but never referenced
  65. # pragma diag_suppress=237 // controlling expression is constant
  66. #endif
  67. // Inlining controls
  68. #if defined(_MSC_VER) && _MSC_VER >= 1300
  69. # define PUGI__NO_INLINE __declspec(noinline)
  70. #elif defined(__GNUC__)
  71. # define PUGI__NO_INLINE __attribute__((noinline))
  72. #else
  73. # define PUGI__NO_INLINE
  74. #endif
  75. // Branch weight controls
  76. #if defined(__GNUC__)
  77. # define PUGI__UNLIKELY(cond) __builtin_expect(cond, 0)
  78. #else
  79. # define PUGI__UNLIKELY(cond) (cond)
  80. #endif
  81. // Simple static assertion
  82. #define PUGI__STATIC_ASSERT(cond) { static const char condition_failed[(cond) ? 1 : -1] = {0}; (void)condition_failed[0]; }
  83. // Digital Mars C++ bug workaround for passing char loaded from memory via stack
  84. #ifdef __DMC__
  85. # define PUGI__DMC_VOLATILE volatile
  86. #else
  87. # define PUGI__DMC_VOLATILE
  88. #endif
  89. // Borland C++ bug workaround for not defining ::memcpy depending on header include order (can't always use std::memcpy because some compilers don't have it at all)
  90. #if defined(__BORLANDC__) && !defined(__MEM_H_USING_LIST)
  91. using std::memcpy;
  92. using std::memmove;
  93. using std::memset;
  94. #endif
  95. // In some environments MSVC is a compiler but the CRT lacks certain MSVC-specific features
  96. #if defined(_MSC_VER) && !defined(__S3E__)
  97. # define PUGI__MSVC_CRT_VERSION _MSC_VER
  98. #endif
  99. #ifdef PUGIXML_HEADER_ONLY
  100. # define PUGI__NS_BEGIN namespace pugi { namespace impl {
  101. # define PUGI__NS_END } }
  102. # define PUGI__FN inline
  103. # define PUGI__FN_NO_INLINE inline
  104. #else
  105. # if defined(_MSC_VER) && _MSC_VER < 1300 // MSVC6 seems to have an amusing bug with anonymous namespaces inside namespaces
  106. # define PUGI__NS_BEGIN namespace pugi { namespace impl {
  107. # define PUGI__NS_END } }
  108. # else
  109. # define PUGI__NS_BEGIN namespace pugi { namespace impl { namespace {
  110. # define PUGI__NS_END } } }
  111. # endif
  112. # define PUGI__FN
  113. # define PUGI__FN_NO_INLINE PUGI__NO_INLINE
  114. #endif
  115. // uintptr_t
  116. #if !defined(_MSC_VER) || _MSC_VER >= 1600
  117. # include <stdint.h>
  118. #else
  119. namespace pugi
  120. {
  121. # ifndef _UINTPTR_T_DEFINED
  122. typedef size_t uintptr_t;
  123. # endif
  124. typedef unsigned __int8 uint8_t;
  125. typedef unsigned __int16 uint16_t;
  126. typedef unsigned __int32 uint32_t;
  127. }
  128. #endif
  129. // Memory allocation
  130. PUGI__NS_BEGIN
  131. PUGI__FN void* default_allocate(size_t size)
  132. {
  133. return malloc(size);
  134. }
  135. PUGI__FN void default_deallocate(void* ptr)
  136. {
  137. free(ptr);
  138. }
  139. template <typename T>
  140. struct xml_memory_management_function_storage
  141. {
  142. static allocation_function allocate;
  143. static deallocation_function deallocate;
  144. };
  145. // Global allocation functions are stored in class statics so that in header mode linker deduplicates them
  146. // Without a template<> we'll get multiple definitions of the same static
  147. template <typename T> allocation_function xml_memory_management_function_storage<T>::allocate = default_allocate;
  148. template <typename T> deallocation_function xml_memory_management_function_storage<T>::deallocate = default_deallocate;
  149. typedef xml_memory_management_function_storage<int> xml_memory;
  150. PUGI__NS_END
  151. // String utilities
  152. PUGI__NS_BEGIN
  153. // Get string length
  154. PUGI__FN size_t strlength(const char_t* s)
  155. {
  156. assert(s);
  157. #ifdef PUGIXML_WCHAR_MODE
  158. return wcslen(s);
  159. #else
  160. return strlen(s);
  161. #endif
  162. }
  163. // Compare two strings
  164. PUGI__FN bool strequal(const char_t* src, const char_t* dst)
  165. {
  166. assert(src && dst);
  167. #ifdef PUGIXML_WCHAR_MODE
  168. return wcscmp(src, dst) == 0;
  169. #else
  170. return strcmp(src, dst) == 0;
  171. #endif
  172. }
  173. // Compare lhs with [rhs_begin, rhs_end)
  174. PUGI__FN bool strequalrange(const char_t* lhs, const char_t* rhs, size_t count)
  175. {
  176. for (size_t i = 0; i < count; ++i)
  177. if (lhs[i] != rhs[i])
  178. return false;
  179. return lhs[count] == 0;
  180. }
  181. // Get length of wide string, even if CRT lacks wide character support
  182. PUGI__FN size_t strlength_wide(const wchar_t* s)
  183. {
  184. assert(s);
  185. #ifdef PUGIXML_WCHAR_MODE
  186. return wcslen(s);
  187. #else
  188. const wchar_t* end = s;
  189. while (*end) end++;
  190. return static_cast<size_t>(end - s);
  191. #endif
  192. }
  193. PUGI__NS_END
  194. // auto_ptr-like object for exception recovery
  195. PUGI__NS_BEGIN
  196. template <typename T, typename D = void(*)(T*)> struct auto_deleter
  197. {
  198. T* data;
  199. D deleter;
  200. auto_deleter(T* data_, D deleter_): data(data_), deleter(deleter_)
  201. {
  202. }
  203. ~auto_deleter()
  204. {
  205. if (data) deleter(data);
  206. }
  207. T* release()
  208. {
  209. T* result = data;
  210. data = 0;
  211. return result;
  212. }
  213. };
  214. PUGI__NS_END
  215. #ifdef PUGIXML_COMPACT
  216. PUGI__NS_BEGIN
  217. class compact_hash_table
  218. {
  219. public:
  220. compact_hash_table(): _items(0), _capacity(0), _count(0)
  221. {
  222. }
  223. void clear()
  224. {
  225. if (_items)
  226. {
  227. xml_memory::deallocate(_items);
  228. _items = 0;
  229. _capacity = 0;
  230. _count = 0;
  231. }
  232. }
  233. void** find(const void* key)
  234. {
  235. assert(key);
  236. if (_capacity == 0) return 0;
  237. size_t hashmod = _capacity - 1;
  238. size_t bucket = hash(key) & hashmod;
  239. for (size_t probe = 0; probe <= hashmod; ++probe)
  240. {
  241. item_t& probe_item = _items[bucket];
  242. if (probe_item.key == key)
  243. return &probe_item.value;
  244. if (probe_item.key == 0)
  245. return 0;
  246. // hash collision, quadratic probing
  247. bucket = (bucket + probe + 1) & hashmod;
  248. }
  249. assert(!"Hash table is full");
  250. return 0;
  251. }
  252. void** insert(const void* key)
  253. {
  254. assert(key);
  255. assert(_capacity != 0 && _count < _capacity - _capacity / 4);
  256. size_t hashmod = _capacity - 1;
  257. size_t bucket = hash(key) & hashmod;
  258. for (size_t probe = 0; probe <= hashmod; ++probe)
  259. {
  260. item_t& probe_item = _items[bucket];
  261. if (probe_item.key == 0)
  262. {
  263. probe_item.key = key;
  264. _count++;
  265. return &probe_item.value;
  266. }
  267. if (probe_item.key == key)
  268. return &probe_item.value;
  269. // hash collision, quadratic probing
  270. bucket = (bucket + probe + 1) & hashmod;
  271. }
  272. assert(!"Hash table is full");
  273. return 0;
  274. }
  275. bool reserve()
  276. {
  277. if (_count + 16 >= _capacity - _capacity / 4)
  278. return rehash();
  279. return true;
  280. }
  281. private:
  282. struct item_t
  283. {
  284. const void* key;
  285. void* value;
  286. };
  287. item_t* _items;
  288. size_t _capacity;
  289. size_t _count;
  290. bool rehash();
  291. static unsigned int hash(const void* key)
  292. {
  293. unsigned int h = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(key));
  294. // MurmurHash3 32-bit finalizer
  295. h ^= h >> 16;
  296. h *= 0x85ebca6bu;
  297. h ^= h >> 13;
  298. h *= 0xc2b2ae35u;
  299. h ^= h >> 16;
  300. return h;
  301. }
  302. };
  303. PUGI__FN_NO_INLINE bool compact_hash_table::rehash()
  304. {
  305. compact_hash_table rt;
  306. rt._capacity = (_capacity == 0) ? 32 : _capacity * 2;
  307. rt._items = static_cast<item_t*>(xml_memory::allocate(sizeof(item_t) * rt._capacity));
  308. if (!rt._items)
  309. return false;
  310. memset(rt._items, 0, sizeof(item_t) * rt._capacity);
  311. for (size_t i = 0; i < _capacity; ++i)
  312. if (_items[i].key)
  313. *rt.insert(_items[i].key) = _items[i].value;
  314. if (_items)
  315. xml_memory::deallocate(_items);
  316. _capacity = rt._capacity;
  317. _items = rt._items;
  318. assert(_count == rt._count);
  319. return true;
  320. }
  321. PUGI__NS_END
  322. #endif
  323. PUGI__NS_BEGIN
  324. static const size_t xml_memory_page_size =
  325. #ifdef PUGIXML_MEMORY_PAGE_SIZE
  326. PUGIXML_MEMORY_PAGE_SIZE
  327. #else
  328. 32768
  329. #endif
  330. ;
  331. #ifdef PUGIXML_COMPACT
  332. static const uintptr_t xml_memory_block_alignment = 4;
  333. static const uintptr_t xml_memory_page_alignment = sizeof(void*);
  334. #else
  335. static const uintptr_t xml_memory_block_alignment = sizeof(void*);
  336. static const uintptr_t xml_memory_page_alignment = 64;
  337. static const uintptr_t xml_memory_page_pointer_mask = ~(xml_memory_page_alignment - 1);
  338. #endif
  339. // extra metadata bits
  340. static const uintptr_t xml_memory_page_contents_shared_mask = 32;
  341. static const uintptr_t xml_memory_page_name_allocated_mask = 16;
  342. static const uintptr_t xml_memory_page_value_allocated_mask = 8;
  343. static const uintptr_t xml_memory_page_type_mask = 7;
  344. // combined masks for string uniqueness
  345. static const uintptr_t xml_memory_page_name_allocated_or_shared_mask = xml_memory_page_name_allocated_mask | xml_memory_page_contents_shared_mask;
  346. static const uintptr_t xml_memory_page_value_allocated_or_shared_mask = xml_memory_page_value_allocated_mask | xml_memory_page_contents_shared_mask;
  347. #ifdef PUGIXML_COMPACT
  348. #define PUGI__GETPAGE_IMPL(header) (header).get_page()
  349. #else
  350. #define PUGI__GETPAGE_IMPL(header) reinterpret_cast<impl::xml_memory_page*>((header) & impl::xml_memory_page_pointer_mask)
  351. #endif
  352. #define PUGI__GETPAGE(n) PUGI__GETPAGE_IMPL((n)->header)
  353. #define PUGI__NODETYPE(n) static_cast<xml_node_type>(((n)->header & impl::xml_memory_page_type_mask) + 1)
  354. struct xml_allocator;
  355. struct xml_memory_page
  356. {
  357. static xml_memory_page* construct(void* memory)
  358. {
  359. xml_memory_page* result = static_cast<xml_memory_page*>(memory);
  360. result->allocator = 0;
  361. result->prev = 0;
  362. result->next = 0;
  363. result->busy_size = 0;
  364. result->freed_size = 0;
  365. #ifdef PUGIXML_COMPACT
  366. result->compact_string_base = 0;
  367. result->compact_shared_parent = 0;
  368. result->compact_page_marker = 0;
  369. #endif
  370. return result;
  371. }
  372. xml_allocator* allocator;
  373. xml_memory_page* prev;
  374. xml_memory_page* next;
  375. size_t busy_size;
  376. size_t freed_size;
  377. #ifdef PUGIXML_COMPACT
  378. char_t* compact_string_base;
  379. void* compact_shared_parent;
  380. uint32_t* compact_page_marker;
  381. #endif
  382. };
  383. struct xml_memory_string_header
  384. {
  385. uint16_t page_offset; // offset from page->data
  386. uint16_t full_size; // 0 if string occupies whole page
  387. };
  388. struct xml_allocator
  389. {
  390. xml_allocator(xml_memory_page* root): _root(root), _busy_size(root->busy_size)
  391. {
  392. #ifdef PUGIXML_COMPACT
  393. _hash = 0;
  394. #endif
  395. }
  396. xml_memory_page* allocate_page(size_t data_size)
  397. {
  398. size_t size = sizeof(xml_memory_page) + data_size;
  399. // allocate block with some alignment, leaving memory for worst-case padding
  400. void* memory = xml_memory::allocate(size + xml_memory_page_alignment);
  401. if (!memory) return 0;
  402. // align to next page boundary (note: this guarantees at least 1 usable byte before the page)
  403. char* page_memory = reinterpret_cast<char*>((reinterpret_cast<uintptr_t>(memory) + xml_memory_page_alignment) & ~(xml_memory_page_alignment - 1));
  404. // prepare page structure
  405. xml_memory_page* page = xml_memory_page::construct(page_memory);
  406. assert(page);
  407. page->allocator = _root->allocator;
  408. // record the offset for freeing the memory block
  409. assert(page_memory > memory && page_memory - static_cast<char*>(memory) <= 127);
  410. page_memory[-1] = static_cast<char>(page_memory - static_cast<char*>(memory));
  411. return page;
  412. }
  413. static void deallocate_page(xml_memory_page* page)
  414. {
  415. char* page_memory = reinterpret_cast<char*>(page);
  416. xml_memory::deallocate(page_memory - page_memory[-1]);
  417. }
  418. void* allocate_memory_oob(size_t size, xml_memory_page*& out_page);
  419. void* allocate_memory(size_t size, xml_memory_page*& out_page)
  420. {
  421. if (PUGI__UNLIKELY(_busy_size + size > xml_memory_page_size))
  422. return allocate_memory_oob(size, out_page);
  423. void* buf = reinterpret_cast<char*>(_root) + sizeof(xml_memory_page) + _busy_size;
  424. _busy_size += size;
  425. out_page = _root;
  426. return buf;
  427. }
  428. #ifdef PUGIXML_COMPACT
  429. void* allocate_object(size_t size, xml_memory_page*& out_page)
  430. {
  431. void* result = allocate_memory(size + sizeof(uint32_t), out_page);
  432. if (!result) return 0;
  433. // adjust for marker
  434. ptrdiff_t offset = static_cast<char*>(result) - reinterpret_cast<char*>(out_page->compact_page_marker);
  435. if (PUGI__UNLIKELY(static_cast<uintptr_t>(offset) >= 256 * xml_memory_block_alignment))
  436. {
  437. // insert new marker
  438. uint32_t* marker = static_cast<uint32_t*>(result);
  439. *marker = static_cast<uint32_t>(reinterpret_cast<char*>(marker) - reinterpret_cast<char*>(out_page));
  440. out_page->compact_page_marker = marker;
  441. // since we don't reuse the page space until we reallocate it, we can just pretend that we freed the marker block
  442. // this will make sure deallocate_memory correctly tracks the size
  443. out_page->freed_size += sizeof(uint32_t);
  444. return marker + 1;
  445. }
  446. else
  447. {
  448. // roll back uint32_t part
  449. _busy_size -= sizeof(uint32_t);
  450. return result;
  451. }
  452. }
  453. #else
  454. void* allocate_object(size_t size, xml_memory_page*& out_page)
  455. {
  456. return allocate_memory(size, out_page);
  457. }
  458. #endif
  459. void deallocate_memory(void* ptr, size_t size, xml_memory_page* page)
  460. {
  461. if (page == _root) page->busy_size = _busy_size;
  462. assert(ptr >= reinterpret_cast<char*>(page) + sizeof(xml_memory_page) && ptr < reinterpret_cast<char*>(page) + sizeof(xml_memory_page) + page->busy_size);
  463. (void)!ptr;
  464. page->freed_size += size;
  465. assert(page->freed_size <= page->busy_size);
  466. if (page->freed_size == page->busy_size)
  467. {
  468. if (page->next == 0)
  469. {
  470. assert(_root == page);
  471. // top page freed, just reset sizes
  472. page->busy_size = 0;
  473. page->freed_size = 0;
  474. #ifdef PUGIXML_COMPACT
  475. // reset compact state to maximize efficiency
  476. page->compact_string_base = 0;
  477. page->compact_shared_parent = 0;
  478. page->compact_page_marker = 0;
  479. #endif
  480. _busy_size = 0;
  481. }
  482. else
  483. {
  484. assert(_root != page);
  485. assert(page->prev);
  486. // remove from the list
  487. page->prev->next = page->next;
  488. page->next->prev = page->prev;
  489. // deallocate
  490. deallocate_page(page);
  491. }
  492. }
  493. }
  494. char_t* allocate_string(size_t length)
  495. {
  496. static const size_t max_encoded_offset = (1 << 16) * xml_memory_block_alignment;
  497. PUGI__STATIC_ASSERT(xml_memory_page_size <= max_encoded_offset);
  498. // allocate memory for string and header block
  499. size_t size = sizeof(xml_memory_string_header) + length * sizeof(char_t);
  500. // round size up to block alignment boundary
  501. size_t full_size = (size + (xml_memory_block_alignment - 1)) & ~(xml_memory_block_alignment - 1);
  502. xml_memory_page* page;
  503. xml_memory_string_header* header = static_cast<xml_memory_string_header*>(allocate_memory(full_size, page));
  504. if (!header) return 0;
  505. // setup header
  506. ptrdiff_t page_offset = reinterpret_cast<char*>(header) - reinterpret_cast<char*>(page) - sizeof(xml_memory_page);
  507. assert(page_offset % xml_memory_block_alignment == 0);
  508. assert(page_offset >= 0 && static_cast<size_t>(page_offset) < max_encoded_offset);
  509. header->page_offset = static_cast<uint16_t>(static_cast<size_t>(page_offset) / xml_memory_block_alignment);
  510. // full_size == 0 for large strings that occupy the whole page
  511. assert(full_size % xml_memory_block_alignment == 0);
  512. assert(full_size < max_encoded_offset || (page->busy_size == full_size && page_offset == 0));
  513. header->full_size = static_cast<uint16_t>(full_size < max_encoded_offset ? full_size / xml_memory_block_alignment : 0);
  514. // round-trip through void* to avoid 'cast increases required alignment of target type' warning
  515. // header is guaranteed a pointer-sized alignment, which should be enough for char_t
  516. return static_cast<char_t*>(static_cast<void*>(header + 1));
  517. }
  518. void deallocate_string(char_t* string)
  519. {
  520. // this function casts pointers through void* to avoid 'cast increases required alignment of target type' warnings
  521. // we're guaranteed the proper (pointer-sized) alignment on the input string if it was allocated via allocate_string
  522. // get header
  523. xml_memory_string_header* header = static_cast<xml_memory_string_header*>(static_cast<void*>(string)) - 1;
  524. assert(header);
  525. // deallocate
  526. size_t page_offset = sizeof(xml_memory_page) + header->page_offset * xml_memory_block_alignment;
  527. xml_memory_page* page = reinterpret_cast<xml_memory_page*>(static_cast<void*>(reinterpret_cast<char*>(header) - page_offset));
  528. // if full_size == 0 then this string occupies the whole page
  529. size_t full_size = header->full_size == 0 ? page->busy_size : header->full_size * xml_memory_block_alignment;
  530. deallocate_memory(header, full_size, page);
  531. }
  532. bool reserve()
  533. {
  534. #ifdef PUGIXML_COMPACT
  535. return _hash->reserve();
  536. #else
  537. return true;
  538. #endif
  539. }
  540. xml_memory_page* _root;
  541. size_t _busy_size;
  542. #ifdef PUGIXML_COMPACT
  543. compact_hash_table* _hash;
  544. #endif
  545. };
  546. PUGI__FN_NO_INLINE void* xml_allocator::allocate_memory_oob(size_t size, xml_memory_page*& out_page)
  547. {
  548. const size_t large_allocation_threshold = xml_memory_page_size / 4;
  549. xml_memory_page* page = allocate_page(size <= large_allocation_threshold ? xml_memory_page_size : size);
  550. out_page = page;
  551. if (!page) return 0;
  552. if (size <= large_allocation_threshold)
  553. {
  554. _root->busy_size = _busy_size;
  555. // insert page at the end of linked list
  556. page->prev = _root;
  557. _root->next = page;
  558. _root = page;
  559. _busy_size = size;
  560. }
  561. else
  562. {
  563. // insert page before the end of linked list, so that it is deleted as soon as possible
  564. // the last page is not deleted even if it's empty (see deallocate_memory)
  565. assert(_root->prev);
  566. page->prev = _root->prev;
  567. page->next = _root;
  568. _root->prev->next = page;
  569. _root->prev = page;
  570. page->busy_size = size;
  571. }
  572. return reinterpret_cast<char*>(page) + sizeof(xml_memory_page);
  573. }
  574. PUGI__NS_END
  575. #ifdef PUGIXML_COMPACT
  576. PUGI__NS_BEGIN
  577. static const uintptr_t compact_alignment_log2 = 2;
  578. static const uintptr_t compact_alignment = 1 << compact_alignment_log2;
  579. class compact_header
  580. {
  581. public:
  582. compact_header(xml_memory_page* page, unsigned int flags)
  583. {
  584. PUGI__STATIC_ASSERT(xml_memory_block_alignment == compact_alignment);
  585. ptrdiff_t offset = (reinterpret_cast<char*>(this) - reinterpret_cast<char*>(page->compact_page_marker));
  586. assert(offset % compact_alignment == 0 && static_cast<uintptr_t>(offset) < 256 * compact_alignment);
  587. _page = static_cast<unsigned char>(offset >> compact_alignment_log2);
  588. _flags = static_cast<unsigned char>(flags);
  589. }
  590. void operator&=(uintptr_t mod)
  591. {
  592. _flags &= static_cast<unsigned char>(mod);
  593. }
  594. void operator|=(uintptr_t mod)
  595. {
  596. _flags |= static_cast<unsigned char>(mod);
  597. }
  598. uintptr_t operator&(uintptr_t mod) const
  599. {
  600. return _flags & mod;
  601. }
  602. xml_memory_page* get_page() const
  603. {
  604. // round-trip through void* to silence 'cast increases required alignment of target type' warnings
  605. const char* page_marker = reinterpret_cast<const char*>(this) - (_page << compact_alignment_log2);
  606. const char* page = page_marker - *reinterpret_cast<const uint32_t*>(static_cast<const void*>(page_marker));
  607. return const_cast<xml_memory_page*>(reinterpret_cast<const xml_memory_page*>(static_cast<const void*>(page)));
  608. }
  609. private:
  610. unsigned char _page;
  611. unsigned char _flags;
  612. };
  613. PUGI__FN xml_memory_page* compact_get_page(const void* object, int header_offset)
  614. {
  615. const compact_header* header = reinterpret_cast<const compact_header*>(static_cast<const char*>(object) - header_offset);
  616. return header->get_page();
  617. }
  618. template <int header_offset, typename T> PUGI__FN_NO_INLINE T* compact_get_value(const void* object)
  619. {
  620. return static_cast<T*>(*compact_get_page(object, header_offset)->allocator->_hash->find(object));
  621. }
  622. template <int header_offset, typename T> PUGI__FN_NO_INLINE void compact_set_value(const void* object, T* value)
  623. {
  624. *compact_get_page(object, header_offset)->allocator->_hash->insert(object) = value;
  625. }
  626. template <typename T, int header_offset, int start = -126> class compact_pointer
  627. {
  628. public:
  629. compact_pointer(): _data(0)
  630. {
  631. }
  632. void operator=(const compact_pointer& rhs)
  633. {
  634. *this = rhs + 0;
  635. }
  636. void operator=(T* value)
  637. {
  638. if (value)
  639. {
  640. // value is guaranteed to be compact-aligned; 'this' is not
  641. // our decoding is based on 'this' aligned to compact alignment downwards (see operator T*)
  642. // so for negative offsets (e.g. -3) we need to adjust the diff by compact_alignment - 1 to
  643. // compensate for arithmetic shift rounding for negative values
  644. ptrdiff_t diff = reinterpret_cast<char*>(value) - reinterpret_cast<char*>(this);
  645. ptrdiff_t offset = ((diff + int(compact_alignment - 1)) >> compact_alignment_log2) - start;
  646. if (static_cast<uintptr_t>(offset) <= 253)
  647. _data = static_cast<unsigned char>(offset + 1);
  648. else
  649. {
  650. compact_set_value<header_offset>(this, value);
  651. _data = 255;
  652. }
  653. }
  654. else
  655. _data = 0;
  656. }
  657. operator T*() const
  658. {
  659. if (_data)
  660. {
  661. if (_data < 255)
  662. {
  663. uintptr_t base = reinterpret_cast<uintptr_t>(this) & ~(compact_alignment - 1);
  664. return reinterpret_cast<T*>(base + ((_data - 1 + start) << compact_alignment_log2));
  665. }
  666. else
  667. return compact_get_value<header_offset, T>(this);
  668. }
  669. else
  670. return 0;
  671. }
  672. T* operator->() const
  673. {
  674. return *this;
  675. }
  676. private:
  677. unsigned char _data;
  678. };
  679. template <typename T, int header_offset> class compact_pointer_parent
  680. {
  681. public:
  682. compact_pointer_parent(): _data(0)
  683. {
  684. }
  685. void operator=(const compact_pointer_parent& rhs)
  686. {
  687. *this = rhs + 0;
  688. }
  689. void operator=(T* value)
  690. {
  691. if (value)
  692. {
  693. // value is guaranteed to be compact-aligned; 'this' is not
  694. // our decoding is based on 'this' aligned to compact alignment downwards (see operator T*)
  695. // so for negative offsets (e.g. -3) we need to adjust the diff by compact_alignment - 1 to
  696. // compensate for arithmetic shift behavior for negative values
  697. ptrdiff_t diff = reinterpret_cast<char*>(value) - reinterpret_cast<char*>(this);
  698. ptrdiff_t offset = ((diff + int(compact_alignment - 1)) >> compact_alignment_log2) + 65533;
  699. if (static_cast<uintptr_t>(offset) <= 65533)
  700. {
  701. _data = static_cast<unsigned short>(offset + 1);
  702. }
  703. else
  704. {
  705. xml_memory_page* page = compact_get_page(this, header_offset);
  706. if (PUGI__UNLIKELY(page->compact_shared_parent == 0))
  707. page->compact_shared_parent = value;
  708. if (page->compact_shared_parent == value)
  709. {
  710. _data = 65534;
  711. }
  712. else
  713. {
  714. compact_set_value<header_offset>(this, value);
  715. _data = 65535;
  716. }
  717. }
  718. }
  719. else
  720. {
  721. _data = 0;
  722. }
  723. }
  724. operator T*() const
  725. {
  726. if (_data)
  727. {
  728. if (_data < 65534)
  729. {
  730. uintptr_t base = reinterpret_cast<uintptr_t>(this) & ~(compact_alignment - 1);
  731. return reinterpret_cast<T*>(base + ((_data - 1 - 65533) << compact_alignment_log2));
  732. }
  733. else if (_data == 65534)
  734. return static_cast<T*>(compact_get_page(this, header_offset)->compact_shared_parent);
  735. else
  736. return compact_get_value<header_offset, T>(this);
  737. }
  738. else
  739. return 0;
  740. }
  741. T* operator->() const
  742. {
  743. return *this;
  744. }
  745. private:
  746. uint16_t _data;
  747. };
  748. template <int header_offset, int base_offset> class compact_string
  749. {
  750. public:
  751. compact_string(): _data(0)
  752. {
  753. }
  754. void operator=(const compact_string& rhs)
  755. {
  756. *this = rhs + 0;
  757. }
  758. void operator=(char_t* value)
  759. {
  760. if (value)
  761. {
  762. xml_memory_page* page = compact_get_page(this, header_offset);
  763. if (PUGI__UNLIKELY(page->compact_string_base == 0))
  764. page->compact_string_base = value;
  765. ptrdiff_t offset = value - page->compact_string_base;
  766. if (static_cast<uintptr_t>(offset) < (65535 << 7))
  767. {
  768. // round-trip through void* to silence 'cast increases required alignment of target type' warnings
  769. uint16_t* base = reinterpret_cast<uint16_t*>(static_cast<void*>(reinterpret_cast<char*>(this) - base_offset));
  770. if (*base == 0)
  771. {
  772. *base = static_cast<uint16_t>((offset >> 7) + 1);
  773. _data = static_cast<unsigned char>((offset & 127) + 1);
  774. }
  775. else
  776. {
  777. ptrdiff_t remainder = offset - ((*base - 1) << 7);
  778. if (static_cast<uintptr_t>(remainder) <= 253)
  779. {
  780. _data = static_cast<unsigned char>(remainder + 1);
  781. }
  782. else
  783. {
  784. compact_set_value<header_offset>(this, value);
  785. _data = 255;
  786. }
  787. }
  788. }
  789. else
  790. {
  791. compact_set_value<header_offset>(this, value);
  792. _data = 255;
  793. }
  794. }
  795. else
  796. {
  797. _data = 0;
  798. }
  799. }
  800. operator char_t*() const
  801. {
  802. if (_data)
  803. {
  804. if (_data < 255)
  805. {
  806. xml_memory_page* page = compact_get_page(this, header_offset);
  807. // round-trip through void* to silence 'cast increases required alignment of target type' warnings
  808. const uint16_t* base = reinterpret_cast<const uint16_t*>(static_cast<const void*>(reinterpret_cast<const char*>(this) - base_offset));
  809. assert(*base);
  810. ptrdiff_t offset = ((*base - 1) << 7) + (_data - 1);
  811. return page->compact_string_base + offset;
  812. }
  813. else
  814. {
  815. return compact_get_value<header_offset, char_t>(this);
  816. }
  817. }
  818. else
  819. return 0;
  820. }
  821. private:
  822. unsigned char _data;
  823. };
  824. PUGI__NS_END
  825. #endif
  826. #ifdef PUGIXML_COMPACT
  827. namespace pugi
  828. {
  829. struct xml_attribute_struct
  830. {
  831. xml_attribute_struct(impl::xml_memory_page* page): header(page, 0), namevalue_base(0)
  832. {
  833. PUGI__STATIC_ASSERT(sizeof(xml_attribute_struct) == 8);
  834. }
  835. impl::compact_header header;
  836. uint16_t namevalue_base;
  837. impl::compact_string<4, 2> name;
  838. impl::compact_string<5, 3> value;
  839. impl::compact_pointer<xml_attribute_struct, 6> prev_attribute_c;
  840. impl::compact_pointer<xml_attribute_struct, 7, 0> next_attribute;
  841. };
  842. struct xml_node_struct
  843. {
  844. xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(page, type - 1), namevalue_base(0)
  845. {
  846. PUGI__STATIC_ASSERT(sizeof(xml_node_struct) == 12);
  847. }
  848. impl::compact_header header;
  849. uint16_t namevalue_base;
  850. impl::compact_string<4, 2> name;
  851. impl::compact_string<5, 3> value;
  852. impl::compact_pointer_parent<xml_node_struct, 6> parent;
  853. impl::compact_pointer<xml_node_struct, 8, 0> first_child;
  854. impl::compact_pointer<xml_node_struct, 9> prev_sibling_c;
  855. impl::compact_pointer<xml_node_struct, 10, 0> next_sibling;
  856. impl::compact_pointer<xml_attribute_struct, 11, 0> first_attribute;
  857. };
  858. }
  859. #else
  860. namespace pugi
  861. {
  862. struct xml_attribute_struct
  863. {
  864. xml_attribute_struct(impl::xml_memory_page* page): header(reinterpret_cast<uintptr_t>(page)), name(0), value(0), prev_attribute_c(0), next_attribute(0)
  865. {
  866. }
  867. uintptr_t header;
  868. char_t* name;
  869. char_t* value;
  870. xml_attribute_struct* prev_attribute_c;
  871. xml_attribute_struct* next_attribute;
  872. };
  873. struct xml_node_struct
  874. {
  875. xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(reinterpret_cast<uintptr_t>(page) | (type - 1)), name(0), value(0), parent(0), first_child(0), prev_sibling_c(0), next_sibling(0), first_attribute(0)
  876. {
  877. }
  878. uintptr_t header;
  879. char_t* name;
  880. char_t* value;
  881. xml_node_struct* parent;
  882. xml_node_struct* first_child;
  883. xml_node_struct* prev_sibling_c;
  884. xml_node_struct* next_sibling;
  885. xml_attribute_struct* first_attribute;
  886. };
  887. }
  888. #endif
  889. PUGI__NS_BEGIN
  890. struct xml_extra_buffer
  891. {
  892. char_t* buffer;
  893. xml_extra_buffer* next;
  894. };
  895. struct xml_document_struct: public xml_node_struct, public xml_allocator
  896. {
  897. xml_document_struct(xml_memory_page* page): xml_node_struct(page, node_document), xml_allocator(page), buffer(0), extra_buffers(0)
  898. {
  899. #ifdef PUGIXML_COMPACT
  900. _hash = &hash;
  901. #endif
  902. }
  903. const char_t* buffer;
  904. xml_extra_buffer* extra_buffers;
  905. #ifdef PUGIXML_COMPACT
  906. compact_hash_table hash;
  907. #endif
  908. };
  909. template <typename Object> inline xml_allocator& get_allocator(const Object* object)
  910. {
  911. assert(object);
  912. return *PUGI__GETPAGE(object)->allocator;
  913. }
  914. template <typename Object> inline xml_document_struct& get_document(const Object* object)
  915. {
  916. assert(object);
  917. return *static_cast<xml_document_struct*>(PUGI__GETPAGE(object)->allocator);
  918. }
  919. PUGI__NS_END
  920. // Low-level DOM operations
  921. PUGI__NS_BEGIN
  922. inline xml_attribute_struct* allocate_attribute(xml_allocator& alloc)
  923. {
  924. xml_memory_page* page;
  925. void* memory = alloc.allocate_object(sizeof(xml_attribute_struct), page);
  926. if (!memory) return 0;
  927. return new (memory) xml_attribute_struct(page);
  928. }
  929. inline xml_node_struct* allocate_node(xml_allocator& alloc, xml_node_type type)
  930. {
  931. xml_memory_page* page;
  932. void* memory = alloc.allocate_object(sizeof(xml_node_struct), page);
  933. if (!memory) return 0;
  934. return new (memory) xml_node_struct(page, type);
  935. }
  936. inline void destroy_attribute(xml_attribute_struct* a, xml_allocator& alloc)
  937. {
  938. if (a->header & impl::xml_memory_page_name_allocated_mask)
  939. alloc.deallocate_string(a->name);
  940. if (a->header & impl::xml_memory_page_value_allocated_mask)
  941. alloc.deallocate_string(a->value);
  942. alloc.deallocate_memory(a, sizeof(xml_attribute_struct), PUGI__GETPAGE(a));
  943. }
  944. inline void destroy_node(xml_node_struct* n, xml_allocator& alloc)
  945. {
  946. if (n->header & impl::xml_memory_page_name_allocated_mask)
  947. alloc.deallocate_string(n->name);
  948. if (n->header & impl::xml_memory_page_value_allocated_mask)
  949. alloc.deallocate_string(n->value);
  950. for (xml_attribute_struct* attr = n->first_attribute; attr; )
  951. {
  952. xml_attribute_struct* next = attr->next_attribute;
  953. destroy_attribute(attr, alloc);
  954. attr = next;
  955. }
  956. for (xml_node_struct* child = n->first_child; child; )
  957. {
  958. xml_node_struct* next = child->next_sibling;
  959. destroy_node(child, alloc);
  960. child = next;
  961. }
  962. alloc.deallocate_memory(n, sizeof(xml_node_struct), PUGI__GETPAGE(n));
  963. }
  964. inline void append_node(xml_node_struct* child, xml_node_struct* node)
  965. {
  966. child->parent = node;
  967. xml_node_struct* head = node->first_child;
  968. if (head)
  969. {
  970. xml_node_struct* tail = head->prev_sibling_c;
  971. tail->next_sibling = child;
  972. child->prev_sibling_c = tail;
  973. head->prev_sibling_c = child;
  974. }
  975. else
  976. {
  977. node->first_child = child;
  978. child->prev_sibling_c = child;
  979. }
  980. }
  981. inline void prepend_node(xml_node_struct* child, xml_node_struct* node)
  982. {
  983. child->parent = node;
  984. xml_node_struct* head = node->first_child;
  985. if (head)
  986. {
  987. child->prev_sibling_c = head->prev_sibling_c;
  988. head->prev_sibling_c = child;
  989. }
  990. else
  991. child->prev_sibling_c = child;
  992. child->next_sibling = head;
  993. node->first_child = child;
  994. }
  995. inline void insert_node_after(xml_node_struct* child, xml_node_struct* node)
  996. {
  997. xml_node_struct* parent = node->parent;
  998. child->parent = parent;
  999. if (node->next_sibling)
  1000. node->next_sibling->prev_sibling_c = child;
  1001. else
  1002. parent->first_child->prev_sibling_c = child;
  1003. child->next_sibling = node->next_sibling;
  1004. child->prev_sibling_c = node;
  1005. node->next_sibling = child;
  1006. }
  1007. inline void insert_node_before(xml_node_struct* child, xml_node_struct* node)
  1008. {
  1009. xml_node_struct* parent = node->parent;
  1010. child->parent = parent;
  1011. if (node->prev_sibling_c->next_sibling)
  1012. node->prev_sibling_c->next_sibling = child;
  1013. else
  1014. parent->first_child = child;
  1015. child->prev_sibling_c = node->prev_sibling_c;
  1016. child->next_sibling = node;
  1017. node->prev_sibling_c = child;
  1018. }
  1019. inline void remove_node(xml_node_struct* node)
  1020. {
  1021. xml_node_struct* parent = node->parent;
  1022. if (node->next_sibling)
  1023. node->next_sibling->prev_sibling_c = node->prev_sibling_c;
  1024. else
  1025. parent->first_child->prev_sibling_c = node->prev_sibling_c;
  1026. if (node->prev_sibling_c->next_sibling)
  1027. node->prev_sibling_c->next_sibling = node->next_sibling;
  1028. else
  1029. parent->first_child = node->next_sibling;
  1030. node->parent = 0;
  1031. node->prev_sibling_c = 0;
  1032. node->next_sibling = 0;
  1033. }
  1034. inline void append_attribute(xml_attribute_struct* attr, xml_node_struct* node)
  1035. {
  1036. xml_attribute_struct* head = node->first_attribute;
  1037. if (head)
  1038. {
  1039. xml_attribute_struct* tail = head->prev_attribute_c;
  1040. tail->next_attribute = attr;
  1041. attr->prev_attribute_c = tail;
  1042. head->prev_attribute_c = attr;
  1043. }
  1044. else
  1045. {
  1046. node->first_attribute = attr;
  1047. attr->prev_attribute_c = attr;
  1048. }
  1049. }
  1050. inline void prepend_attribute(xml_attribute_struct* attr, xml_node_struct* node)
  1051. {
  1052. xml_attribute_struct* head = node->first_attribute;
  1053. if (head)
  1054. {
  1055. attr->prev_attribute_c = head->prev_attribute_c;
  1056. head->prev_attribute_c = attr;
  1057. }
  1058. else
  1059. attr->prev_attribute_c = attr;
  1060. attr->next_attribute = head;
  1061. node->first_attribute = attr;
  1062. }
  1063. inline void insert_attribute_after(xml_attribute_struct* attr, xml_attribute_struct* place, xml_node_struct* node)
  1064. {
  1065. if (place->next_attribute)
  1066. place->next_attribute->prev_attribute_c = attr;
  1067. else
  1068. node->first_attribute->prev_attribute_c = attr;
  1069. attr->next_attribute = place->next_attribute;
  1070. attr->prev_attribute_c = place;
  1071. place->next_attribute = attr;
  1072. }
  1073. inline void insert_attribute_before(xml_attribute_struct* attr, xml_attribute_struct* place, xml_node_struct* node)
  1074. {
  1075. if (place->prev_attribute_c->next_attribute)
  1076. place->prev_attribute_c->next_attribute = attr;
  1077. else
  1078. node->first_attribute = attr;
  1079. attr->prev_attribute_c = place->prev_attribute_c;
  1080. attr->next_attribute = place;
  1081. place->prev_attribute_c = attr;
  1082. }
  1083. inline void remove_attribute(xml_attribute_struct* attr, xml_node_struct* node)
  1084. {
  1085. if (attr->next_attribute)
  1086. attr->next_attribute->prev_attribute_c = attr->prev_attribute_c;
  1087. else
  1088. node->first_attribute->prev_attribute_c = attr->prev_attribute_c;
  1089. if (attr->prev_attribute_c->next_attribute)
  1090. attr->prev_attribute_c->next_attribute = attr->next_attribute;
  1091. else
  1092. node->first_attribute = attr->next_attribute;
  1093. attr->prev_attribute_c = 0;
  1094. attr->next_attribute = 0;
  1095. }
  1096. PUGI__FN_NO_INLINE xml_node_struct* append_new_node(xml_node_struct* node, xml_allocator& alloc, xml_node_type type = node_element)
  1097. {
  1098. if (!alloc.reserve()) return 0;
  1099. xml_node_struct* child = allocate_node(alloc, type);
  1100. if (!child) return 0;
  1101. append_node(child, node);
  1102. return child;
  1103. }
  1104. PUGI__FN_NO_INLINE xml_attribute_struct* append_new_attribute(xml_node_struct* node, xml_allocator& alloc)
  1105. {
  1106. if (!alloc.reserve()) return 0;
  1107. xml_attribute_struct* attr = allocate_attribute(alloc);
  1108. if (!attr) return 0;
  1109. append_attribute(attr, node);
  1110. return attr;
  1111. }
  1112. PUGI__NS_END
  1113. // Helper classes for code generation
  1114. PUGI__NS_BEGIN
  1115. struct opt_false
  1116. {
  1117. enum { value = 0 };
  1118. };
  1119. struct opt_true
  1120. {
  1121. enum { value = 1 };
  1122. };
  1123. PUGI__NS_END
  1124. // Unicode utilities
  1125. PUGI__NS_BEGIN
  1126. inline uint16_t endian_swap(uint16_t value)
  1127. {
  1128. return static_cast<uint16_t>(((value & 0xff) << 8) | (value >> 8));
  1129. }
  1130. inline uint32_t endian_swap(uint32_t value)
  1131. {
  1132. return ((value & 0xff) << 24) | ((value & 0xff00) << 8) | ((value & 0xff0000) >> 8) | (value >> 24);
  1133. }
  1134. struct utf8_counter
  1135. {
  1136. typedef size_t value_type;
  1137. static value_type low(value_type result, uint32_t ch)
  1138. {
  1139. // U+0000..U+007F
  1140. if (ch < 0x80) return result + 1;
  1141. // U+0080..U+07FF
  1142. else if (ch < 0x800) return result + 2;
  1143. // U+0800..U+FFFF
  1144. else return result + 3;
  1145. }
  1146. static value_type high(value_type result, uint32_t)
  1147. {
  1148. // U+10000..U+10FFFF
  1149. return result + 4;
  1150. }
  1151. };
  1152. struct utf8_writer
  1153. {
  1154. typedef uint8_t* value_type;
  1155. static value_type low(value_type result, uint32_t ch)
  1156. {
  1157. // U+0000..U+007F
  1158. if (ch < 0x80)
  1159. {
  1160. *result = static_cast<uint8_t>(ch);
  1161. return result + 1;
  1162. }
  1163. // U+0080..U+07FF
  1164. else if (ch < 0x800)
  1165. {
  1166. result[0] = static_cast<uint8_t>(0xC0 | (ch >> 6));
  1167. result[1] = static_cast<uint8_t>(0x80 | (ch & 0x3F));
  1168. return result + 2;
  1169. }
  1170. // U+0800..U+FFFF
  1171. else
  1172. {
  1173. result[0] = static_cast<uint8_t>(0xE0 | (ch >> 12));
  1174. result[1] = static_cast<uint8_t>(0x80 | ((ch >> 6) & 0x3F));
  1175. result[2] = static_cast<uint8_t>(0x80 | (ch & 0x3F));
  1176. return result + 3;
  1177. }
  1178. }
  1179. static value_type high(value_type result, uint32_t ch)
  1180. {
  1181. // U+10000..U+10FFFF
  1182. result[0] = static_cast<uint8_t>(0xF0 | (ch >> 18));
  1183. result[1] = static_cast<uint8_t>(0x80 | ((ch >> 12) & 0x3F));
  1184. result[2] = static_cast<uint8_t>(0x80 | ((ch >> 6) & 0x3F));
  1185. result[3] = static_cast<uint8_t>(0x80 | (ch & 0x3F));
  1186. return result + 4;
  1187. }
  1188. static value_type any(value_type result, uint32_t ch)
  1189. {
  1190. return (ch < 0x10000) ? low(result, ch) : high(result, ch);
  1191. }
  1192. };
  1193. struct utf16_counter
  1194. {
  1195. typedef size_t value_type;
  1196. static value_type low(value_type result, uint32_t)
  1197. {
  1198. return result + 1;
  1199. }
  1200. static value_type high(value_type result, uint32_t)
  1201. {
  1202. return result + 2;
  1203. }
  1204. };
  1205. struct utf16_writer
  1206. {
  1207. typedef uint16_t* value_type;
  1208. static value_type low(value_type result, uint32_t ch)
  1209. {
  1210. *result = static_cast<uint16_t>(ch);
  1211. return result + 1;
  1212. }
  1213. static value_type high(value_type result, uint32_t ch)
  1214. {
  1215. uint32_t msh = static_cast<uint32_t>(ch - 0x10000) >> 10;
  1216. uint32_t lsh = static_cast<uint32_t>(ch - 0x10000) & 0x3ff;
  1217. result[0] = static_cast<uint16_t>(0xD800 + msh);
  1218. result[1] = static_cast<uint16_t>(0xDC00 + lsh);
  1219. return result + 2;
  1220. }
  1221. static value_type any(value_type result, uint32_t ch)
  1222. {
  1223. return (ch < 0x10000) ? low(result, ch) : high(result, ch);
  1224. }
  1225. };
  1226. struct utf32_counter
  1227. {
  1228. typedef size_t value_type;
  1229. static value_type low(value_type result, uint32_t)
  1230. {
  1231. return result + 1;
  1232. }
  1233. static value_type high(value_type result, uint32_t)
  1234. {
  1235. return result + 1;
  1236. }
  1237. };
  1238. struct utf32_writer
  1239. {
  1240. typedef uint32_t* value_type;
  1241. static value_type low(value_type result, uint32_t ch)
  1242. {
  1243. *result = ch;
  1244. return result + 1;
  1245. }
  1246. static value_type high(value_type result, uint32_t ch)
  1247. {
  1248. *result = ch;
  1249. return result + 1;
  1250. }
  1251. static value_type any(value_type result, uint32_t ch)
  1252. {
  1253. *result = ch;
  1254. return result + 1;
  1255. }
  1256. };
  1257. struct latin1_writer
  1258. {
  1259. typedef uint8_t* value_type;
  1260. static value_type low(value_type result, uint32_t ch)
  1261. {
  1262. *result = static_cast<uint8_t>(ch > 255 ? '?' : ch);
  1263. return result + 1;
  1264. }
  1265. static value_type high(value_type result, uint32_t ch)
  1266. {
  1267. (void)ch;
  1268. *result = '?';
  1269. return result + 1;
  1270. }
  1271. };
  1272. struct utf8_decoder
  1273. {
  1274. typedef uint8_t type;
  1275. template <typename Traits> static inline typename Traits::value_type process(const uint8_t* data, size_t size, typename Traits::value_type result, Traits)
  1276. {
  1277. const uint8_t utf8_byte_mask = 0x3f;
  1278. while (size)
  1279. {
  1280. uint8_t lead = *data;
  1281. // 0xxxxxxx -> U+0000..U+007F
  1282. if (lead < 0x80)
  1283. {
  1284. result = Traits::low(result, lead);
  1285. data += 1;
  1286. size -= 1;
  1287. // process aligned single-byte (ascii) blocks
  1288. if ((reinterpret_cast<uintptr_t>(data) & 3) == 0)
  1289. {
  1290. // round-trip through void* to silence 'cast increases required alignment of target type' warnings
  1291. while (size >= 4 && (*static_cast<const uint32_t*>(static_cast<const void*>(data)) & 0x80808080) == 0)
  1292. {
  1293. result = Traits::low(result, data[0]);
  1294. result = Traits::low(result, data[1]);
  1295. result = Traits::low(result, data[2]);
  1296. result = Traits::low(result, data[3]);
  1297. data += 4;
  1298. size -= 4;
  1299. }
  1300. }
  1301. }
  1302. // 110xxxxx -> U+0080..U+07FF
  1303. else if (static_cast<unsigned int>(lead - 0xC0) < 0x20 && size >= 2 && (data[1] & 0xc0) == 0x80)
  1304. {
  1305. result = Traits::low(result, ((lead & ~0xC0) << 6) | (data[1] & utf8_byte_mask));
  1306. data += 2;
  1307. size -= 2;
  1308. }
  1309. // 1110xxxx -> U+0800-U+FFFF
  1310. else if (static_cast<unsigned int>(lead - 0xE0) < 0x10 && size >= 3 && (data[1] & 0xc0) == 0x80 && (data[2] & 0xc0) == 0x80)
  1311. {
  1312. result = Traits::low(result, ((lead & ~0xE0) << 12) | ((data[1] & utf8_byte_mask) << 6) | (data[2] & utf8_byte_mask));
  1313. data += 3;
  1314. size -= 3;
  1315. }
  1316. // 11110xxx -> U+10000..U+10FFFF
  1317. else if (static_cast<unsigned int>(lead - 0xF0) < 0x08 && size >= 4 && (data[1] & 0xc0) == 0x80 && (data[2] & 0xc0) == 0x80 && (data[3] & 0xc0) == 0x80)
  1318. {
  1319. result = Traits::high(result, ((lead & ~0xF0) << 18) | ((data[1] & utf8_byte_mask) << 12) | ((data[2] & utf8_byte_mask) << 6) | (data[3] & utf8_byte_mask));
  1320. data += 4;
  1321. size -= 4;
  1322. }
  1323. // 10xxxxxx or 11111xxx -> invalid
  1324. else
  1325. {
  1326. data += 1;
  1327. size -= 1;
  1328. }
  1329. }
  1330. return result;
  1331. }
  1332. };
  1333. template <typename opt_swap> struct utf16_decoder
  1334. {
  1335. typedef uint16_t type;
  1336. template <typename Traits> static inline typename Traits::value_type process(const uint16_t* data, size_t size, typename Traits::value_type result, Traits)
  1337. {
  1338. while (size)
  1339. {
  1340. uint16_t lead = opt_swap::value ? endian_swap(*data) : *data;
  1341. // U+0000..U+D7FF
  1342. if (lead < 0xD800)
  1343. {
  1344. result = Traits::low(result, lead);
  1345. data += 1;
  1346. size -= 1;
  1347. }
  1348. // U+E000..U+FFFF
  1349. else if (static_cast<unsigned int>(lead - 0xE000) < 0x2000)
  1350. {
  1351. result = Traits::low(result, lead);
  1352. data += 1;
  1353. size -= 1;
  1354. }
  1355. // surrogate pair lead
  1356. else if (static_cast<unsigned int>(lead - 0xD800) < 0x400 && size >= 2)
  1357. {
  1358. uint16_t next = opt_swap::value ? endian_swap(data[1]) : data[1];
  1359. if (static_cast<unsigned int>(next - 0xDC00) < 0x400)
  1360. {
  1361. result = Traits::high(result, 0x10000 + ((lead & 0x3ff) << 10) + (next & 0x3ff));
  1362. data += 2;
  1363. size -= 2;
  1364. }
  1365. else
  1366. {
  1367. data += 1;
  1368. size -= 1;
  1369. }
  1370. }
  1371. else
  1372. {
  1373. data += 1;
  1374. size -= 1;
  1375. }
  1376. }
  1377. return result;
  1378. }
  1379. };
  1380. template <typename opt_swap> struct utf32_decoder
  1381. {
  1382. typedef uint32_t type;
  1383. template <typename Traits> static inline typename Traits::value_type process(const uint32_t* data, size_t size, typename Traits::value_type result, Traits)
  1384. {
  1385. while (size)
  1386. {
  1387. uint32_t lead = opt_swap::value ? endian_swap(*data) : *data;
  1388. // U+0000..U+FFFF
  1389. if (lead < 0x10000)
  1390. {
  1391. result = Traits::low(result, lead);
  1392. data += 1;
  1393. size -= 1;
  1394. }
  1395. // U+10000..U+10FFFF
  1396. else
  1397. {
  1398. result = Traits::high(result, lead);
  1399. data += 1;
  1400. size -= 1;
  1401. }
  1402. }
  1403. return result;
  1404. }
  1405. };
  1406. struct latin1_decoder
  1407. {
  1408. typedef uint8_t type;
  1409. template <typename Traits> static inline typename Traits::value_type process(const uint8_t* data, size_t size, typename Traits::value_type result, Traits)
  1410. {
  1411. while (size)
  1412. {
  1413. result = Traits::low(result, *data);
  1414. data += 1;
  1415. size -= 1;
  1416. }
  1417. return result;
  1418. }
  1419. };
  1420. template <size_t size> struct wchar_selector;
  1421. template <> struct wchar_selector<2>
  1422. {
  1423. typedef uint16_t type;
  1424. typedef utf16_counter counter;
  1425. typedef utf16_writer writer;
  1426. typedef utf16_decoder<opt_false> decoder;
  1427. };
  1428. template <> struct wchar_selector<4>
  1429. {
  1430. typedef uint32_t type;
  1431. typedef utf32_counter counter;
  1432. typedef utf32_writer writer;
  1433. typedef utf32_decoder<opt_false> decoder;
  1434. };
  1435. typedef wchar_selector<sizeof(wchar_t)>::counter wchar_counter;
  1436. typedef wchar_selector<sizeof(wchar_t)>::writer wchar_writer;
  1437. struct wchar_decoder
  1438. {
  1439. typedef wchar_t type;
  1440. template <typename Traits> static inline typename Traits::value_type process(const wchar_t* data, size_t size, typename Traits::value_type result, Traits traits)
  1441. {
  1442. typedef wchar_selector<sizeof(wchar_t)>::decoder decoder;
  1443. return decoder::process(reinterpret_cast<const typename decoder::type*>(data), size, result, traits);
  1444. }
  1445. };
  1446. #ifdef PUGIXML_WCHAR_MODE
  1447. PUGI__FN void convert_wchar_endian_swap(wchar_t* result, const wchar_t* data, size_t length)
  1448. {
  1449. for (size_t i = 0; i < length; ++i)
  1450. result[i] = static_cast<wchar_t>(endian_swap(static_cast<wchar_selector<sizeof(wchar_t)>::type>(data[i])));
  1451. }
  1452. #endif
  1453. PUGI__NS_END
  1454. PUGI__NS_BEGIN
  1455. enum chartype_t
  1456. {
  1457. ct_parse_pcdata = 1, // \0, &, \r, <
  1458. ct_parse_attr = 2, // \0, &, \r, ', "
  1459. ct_parse_attr_ws = 4, // \0, &, \r, ', ", \n, tab
  1460. ct_space = 8, // \r, \n, space, tab
  1461. ct_parse_cdata = 16, // \0, ], >, \r
  1462. ct_parse_comment = 32, // \0, -, >, \r
  1463. ct_symbol = 64, // Any symbol > 127, a-z, A-Z, 0-9, _, :, -, .
  1464. ct_start_symbol = 128 // Any symbol > 127, a-z, A-Z, _, :
  1465. };
  1466. static const unsigned char chartype_table[256] =
  1467. {
  1468. 55, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 0, 0, 63, 0, 0, // 0-15
  1469. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31
  1470. 8, 0, 6, 0, 0, 0, 7, 6, 0, 0, 0, 0, 0, 96, 64, 0, // 32-47
  1471. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 192, 0, 1, 0, 48, 0, // 48-63
  1472. 0, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, // 64-79
  1473. 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 0, 0, 16, 0, 192, // 80-95
  1474. 0, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, // 96-111
  1475. 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 0, 0, 0, 0, 0, // 112-127
  1476. 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, // 128+
  1477. 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
  1478. 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
  1479. 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
  1480. 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
  1481. 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
  1482. 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,
  1483. 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192
  1484. };
  1485. enum chartypex_t
  1486. {
  1487. ctx_special_pcdata = 1, // Any symbol >= 0 and < 32 (except \t, \r, \n), &, <, >
  1488. ctx_special_attr = 2, // Any symbol >= 0 and < 32 (except \t), &, <, >, "
  1489. ctx_start_symbol = 4, // Any symbol > 127, a-z, A-Z, _
  1490. ctx_digit = 8, // 0-9
  1491. ctx_symbol = 16 // Any symbol > 127, a-z, A-Z, 0-9, _, -, .
  1492. };
  1493. static const unsigned char chartypex_table[256] =
  1494. {
  1495. 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 2, 3, 3, 2, 3, 3, // 0-15
  1496. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 16-31
  1497. 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 16, 16, 0, // 32-47
  1498. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, 3, 0, 3, 0, // 48-63
  1499. 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, // 64-79
  1500. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 0, 0, 0, 0, 20, // 80-95
  1501. 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, // 96-111
  1502. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 0, 0, 0, 0, 0, // 112-127
  1503. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, // 128+
  1504. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  1505. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  1506. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  1507. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  1508. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  1509. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  1510. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20
  1511. };
  1512. #ifdef PUGIXML_WCHAR_MODE
  1513. #define PUGI__IS_CHARTYPE_IMPL(c, ct, table) ((static_cast<unsigned int>(c) < 128 ? table[static_cast<unsigned int>(c)] : table[128]) & (ct))
  1514. #else
  1515. #define PUGI__IS_CHARTYPE_IMPL(c, ct, table) (table[static_cast<unsigned char>(c)] & (ct))
  1516. #endif
  1517. #define PUGI__IS_CHARTYPE(c, ct) PUGI__IS_CHARTYPE_IMPL(c, ct, chartype_table)
  1518. #define PUGI__IS_CHARTYPEX(c, ct) PUGI__IS_CHARTYPE_IMPL(c, ct, chartypex_table)
  1519. PUGI__FN bool is_little_endian()
  1520. {
  1521. unsigned int ui = 1;
  1522. return *reinterpret_cast<unsigned char*>(&ui) == 1;
  1523. }
  1524. PUGI__FN xml_encoding get_wchar_encoding()
  1525. {
  1526. PUGI__STATIC_ASSERT(sizeof(wchar_t) == 2 || sizeof(wchar_t) == 4);
  1527. if (sizeof(wchar_t) == 2)
  1528. return is_little_endian() ? encoding_utf16_le : encoding_utf16_be;
  1529. else
  1530. return is_little_endian() ? encoding_utf32_le : encoding_utf32_be;
  1531. }
  1532. PUGI__FN xml_encoding guess_buffer_encoding(uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
  1533. {
  1534. // look for BOM in first few bytes
  1535. if (d0 == 0 && d1 == 0 && d2 == 0xfe && d3 == 0xff) return encoding_utf32_be;
  1536. if (d0 == 0xff && d1 == 0xfe && d2 == 0 && d3 == 0) return encoding_utf32_le;
  1537. if (d0 == 0xfe && d1 == 0xff) return encoding_utf16_be;
  1538. if (d0 == 0xff && d1 == 0xfe) return encoding_utf16_le;
  1539. if (d0 == 0xef && d1 == 0xbb && d2 == 0xbf) return encoding_utf8;
  1540. // look for <, <? or <?xm in various encodings
  1541. if (d0 == 0 && d1 == 0 && d2 == 0 && d3 == 0x3c) return encoding_utf32_be;
  1542. if (d0 == 0x3c && d1 == 0 && d2 == 0 && d3 == 0) return encoding_utf32_le;
  1543. if (d0 == 0 && d1 == 0x3c && d2 == 0 && d3 == 0x3f) return encoding_utf16_be;
  1544. if (d0 == 0x3c && d1 == 0 && d2 == 0x3f && d3 == 0) return encoding_utf16_le;
  1545. if (d0 == 0x3c && d1 == 0x3f && d2 == 0x78 && d3 == 0x6d) return encoding_utf8;
  1546. // look for utf16 < followed by node name (this may fail, but is better than utf8 since it's zero terminated so early)
  1547. if (d0 == 0 && d1 == 0x3c) return encoding_utf16_be;
  1548. if (d0 == 0x3c && d1 == 0) return encoding_utf16_le;
  1549. // no known BOM detected, assume utf8
  1550. return encoding_utf8;
  1551. }
  1552. PUGI__FN xml_encoding get_buffer_encoding(xml_encoding encoding, const void* contents, size_t size)
  1553. {
  1554. // replace wchar encoding with utf implementation
  1555. if (encoding == encoding_wchar) return get_wchar_encoding();
  1556. // replace utf16 encoding with utf16 with specific endianness
  1557. if (encoding == encoding_utf16) return is_little_endian() ? encoding_utf16_le : encoding_utf16_be;
  1558. // replace utf32 encoding with utf32 with specific endianness
  1559. if (encoding == encoding_utf32) return is_little_endian() ? encoding_utf32_le : encoding_utf32_be;
  1560. // only do autodetection if no explicit encoding is requested
  1561. if (encoding != encoding_auto) return encoding;
  1562. // skip encoding autodetection if input buffer is too small
  1563. if (size < 4) return encoding_utf8;
  1564. // try to guess encoding (based on XML specification, Appendix F.1)
  1565. const uint8_t* data = static_cast<const uint8_t*>(contents);
  1566. PUGI__DMC_VOLATILE uint8_t d0 = data[0], d1 = data[1], d2 = data[2], d3 = data[3];
  1567. return guess_buffer_encoding(d0, d1, d2, d3);
  1568. }
  1569. PUGI__FN bool get_mutable_buffer(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, bool is_mutable)
  1570. {
  1571. size_t length = size / sizeof(char_t);
  1572. if (is_mutable)
  1573. {
  1574. out_buffer = static_cast<char_t*>(const_cast<void*>(contents));
  1575. out_length = length;
  1576. }
  1577. else
  1578. {
  1579. char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));
  1580. if (!buffer) return false;
  1581. if (contents)
  1582. memcpy(buffer, contents, length * sizeof(char_t));
  1583. else
  1584. assert(length == 0);
  1585. buffer[length] = 0;
  1586. out_buffer = buffer;
  1587. out_length = length + 1;
  1588. }
  1589. return true;
  1590. }
  1591. #ifdef PUGIXML_WCHAR_MODE
  1592. PUGI__FN bool need_endian_swap_utf(xml_encoding le, xml_encoding re)
  1593. {
  1594. return (le == encoding_utf16_be && re == encoding_utf16_le) || (le == encoding_utf16_le && re == encoding_utf16_be) ||
  1595. (le == encoding_utf32_be && re == encoding_utf32_le) || (le == encoding_utf32_le && re == encoding_utf32_be);
  1596. }
  1597. PUGI__FN bool convert_buffer_endian_swap(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, bool is_mutable)
  1598. {
  1599. const char_t* data = static_cast<const char_t*>(contents);
  1600. size_t length = size / sizeof(char_t);
  1601. if (is_mutable)
  1602. {
  1603. char_t* buffer = const_cast<char_t*>(data);
  1604. convert_wchar_endian_swap(buffer, data, length);
  1605. out_buffer = buffer;
  1606. out_length = length;
  1607. }
  1608. else
  1609. {
  1610. char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));
  1611. if (!buffer) return false;
  1612. convert_wchar_endian_swap(buffer, data, length);
  1613. buffer[length] = 0;
  1614. out_buffer = buffer;
  1615. out_length = length + 1;
  1616. }
  1617. return true;
  1618. }
  1619. template <typename D> PUGI__FN bool convert_buffer_generic(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, D)
  1620. {
  1621. const typename D::type* data = static_cast<const typename D::type*>(contents);
  1622. size_t data_length = size / sizeof(typename D::type);
  1623. // first pass: get length in wchar_t units
  1624. size_t length = D::process(data, data_length, 0, wchar_counter());
  1625. // allocate buffer of suitable length
  1626. char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));
  1627. if (!buffer) return false;
  1628. // second pass: convert utf16 input to wchar_t
  1629. wchar_writer::value_type obegin = reinterpret_cast<wchar_writer::value_type>(buffer);
  1630. wchar_writer::value_type oend = D::process(data, data_length, obegin, wchar_writer());
  1631. assert(oend == obegin + length);
  1632. *oend = 0;
  1633. out_buffer = buffer;
  1634. out_length = length + 1;
  1635. return true;
  1636. }
  1637. PUGI__FN bool convert_buffer(char_t*& out_buffer, size_t& out_length, xml_encoding encoding, const void* contents, size_t size, bool is_mutable)
  1638. {
  1639. // get native encoding
  1640. xml_encoding wchar_encoding = get_wchar_encoding();
  1641. // fast path: no conversion required
  1642. if (encoding == wchar_encoding)
  1643. return get_mutable_buffer(out_buffer, out_length, contents, size, is_mutable);
  1644. // only endian-swapping is required
  1645. if (need_endian_swap_utf(encoding, wchar_encoding))
  1646. return convert_buffer_endian_swap(out_buffer, out_length, contents, size, is_mutable);
  1647. // source encoding is utf8
  1648. if (encoding == encoding_utf8)
  1649. return convert_buffer_generic(out_buffer, out_length, contents, size, utf8_decoder());
  1650. // source encoding is utf16
  1651. if (encoding == encoding_utf16_be || encoding == encoding_utf16_le)
  1652. {
  1653. xml_encoding native_encoding = is_little_endian() ? encoding_utf16_le : encoding_utf16_be;
  1654. return (native_encoding == encoding) ?
  1655. convert_buffer_generic(out_buffer, out_length, contents, size, utf16_decoder<opt_false>()) :
  1656. convert_buffer_generic(out_buffer, out_length, contents, size, utf16_decoder<opt_true>());
  1657. }
  1658. // source encoding is utf32
  1659. if (encoding == encoding_utf32_be || encoding == encoding_utf32_le)
  1660. {
  1661. xml_encoding native_encoding = is_little_endian() ? encoding_utf32_le : encoding_utf32_be;
  1662. return (native_encoding == encoding) ?
  1663. convert_buffer_generic(out_buffer, out_length, contents, size, utf32_decoder<opt_false>()) :
  1664. convert_buffer_generic(out_buffer, out_length, contents, size, utf32_decoder<opt_true>());
  1665. }
  1666. // source encoding is latin1
  1667. if (encoding == encoding_latin1)
  1668. return convert_buffer_generic(out_buffer, out_length, contents, size, latin1_decoder());
  1669. assert(!"Invalid encoding");
  1670. return false;
  1671. }
  1672. #else
  1673. template <typename D> PUGI__FN bool convert_buffer_generic(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, D)
  1674. {
  1675. const typename D::type* data = static_cast<const typename D::type*>(contents);
  1676. size_t data_length = size / sizeof(typename D::type);
  1677. // first pass: get length in utf8 units
  1678. size_t length = D::process(data, data_length, 0, utf8_counter());
  1679. // allocate buffer of suitable length
  1680. char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));
  1681. if (!buffer) return false;
  1682. // second pass: convert utf16 input to utf8
  1683. uint8_t* obegin = reinterpret_cast<uint8_t*>(buffer);
  1684. uint8_t* oend = D::process(data, data_length, obegin, utf8_writer());
  1685. assert(oend == obegin + length);
  1686. *oend = 0;
  1687. out_buffer = buffer;
  1688. out_length = length + 1;
  1689. return true;
  1690. }
  1691. PUGI__FN size_t get_latin1_7bit_prefix_length(const uint8_t* data, size_t size)
  1692. {
  1693. for (size_t i = 0; i < size; ++i)
  1694. if (data[i] > 127)
  1695. return i;
  1696. return size;
  1697. }
  1698. PUGI__FN bool convert_buffer_latin1(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, bool is_mutable)
  1699. {
  1700. const uint8_t* data = static_cast<const uint8_t*>(contents);
  1701. size_t data_length = size;
  1702. // get size of prefix that does not need utf8 conversion
  1703. size_t prefix_length = get_latin1_7bit_prefix_length(data, data_length);
  1704. assert(prefix_length <= data_length);
  1705. const uint8_t* postfix = data + prefix_length;
  1706. size_t postfix_length = data_length - prefix_length;
  1707. // if no conversion is needed, just return the original buffer
  1708. if (postfix_length == 0) return get_mutable_buffer(out_buffer, out_length, contents, size, is_mutable);
  1709. // first pass: get length in utf8 units
  1710. size_t length = prefix_length + latin1_decoder::process(postfix, postfix_length, 0, utf8_counter());
  1711. // allocate buffer of suitable length
  1712. char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));
  1713. if (!buffer) return false;
  1714. // second pass: convert latin1 input to utf8
  1715. memcpy(buffer, data, prefix_length);
  1716. uint8_t* obegin = reinterpret_cast<uint8_t*>(buffer);
  1717. uint8_t* oend = latin1_decoder::process(postfix, postfix_length, obegin + prefix_length, utf8_writer());
  1718. assert(oend == obegin + length);
  1719. *oend = 0;
  1720. out_buffer = buffer;
  1721. out_length = length + 1;
  1722. return true;
  1723. }
  1724. PUGI__FN bool convert_buffer(char_t*& out_buffer, size_t& out_length, xml_encoding encoding, const void* contents, size_t size, bool is_mutable)
  1725. {
  1726. // fast path: no conversion required
  1727. if (encoding == encoding_utf8)
  1728. return get_mutable_buffer(out_buffer, out_length, contents, size, is_mutable);
  1729. // source encoding is utf16
  1730. if (encoding == encoding_utf16_be || encoding == encoding_utf16_le)
  1731. {
  1732. xml_encoding native_encoding = is_little_endian() ? encoding_utf16_le : encoding_utf16_be;
  1733. return (native_encoding == encoding) ?
  1734. convert_buffer_generic(out_buffer, out_length, contents, size, utf16_decoder<opt_false>()) :
  1735. convert_buffer_generic(out_buffer, out_length, contents, size, utf16_decoder<opt_true>());
  1736. }
  1737. // source encoding is utf32
  1738. if (encoding == encoding_utf32_be || encoding == encoding_utf32_le)
  1739. {
  1740. xml_encoding native_encoding = is_little_endian() ? encoding_utf32_le : encoding_utf32_be;
  1741. return (native_encoding == encoding) ?
  1742. convert_buffer_generic(out_buffer, out_length, contents, size, utf32_decoder<opt_false>()) :
  1743. convert_buffer_generic(out_buffer, out_length, contents, size, utf32_decoder<opt_true>());
  1744. }
  1745. // source encoding is latin1
  1746. if (encoding == encoding_latin1)
  1747. return convert_buffer_latin1(out_buffer, out_length, contents, size, is_mutable);
  1748. assert(!"Invalid encoding");
  1749. return false;
  1750. }
  1751. #endif
  1752. PUGI__FN size_t as_utf8_begin(const wchar_t* str, size_t length)
  1753. {
  1754. // get length in utf8 characters
  1755. return wchar_decoder::process(str, length, 0, utf8_counter());
  1756. }
  1757. PUGI__FN void as_utf8_end(char* buffer, size_t size, const wchar_t* str, size_t length)
  1758. {
  1759. // convert to utf8
  1760. uint8_t* begin = reinterpret_cast<uint8_t*>(buffer);
  1761. uint8_t* end = wchar_decoder::process(str, length, begin, utf8_writer());
  1762. assert(begin + size == end);
  1763. (void)!end;
  1764. (void)!size;
  1765. }
  1766. #ifndef PUGIXML_NO_STL
  1767. PUGI__FN std::string as_utf8_impl(const wchar_t* str, size_t length)
  1768. {
  1769. // first pass: get length in utf8 characters
  1770. size_t size = as_utf8_begin(str, length);
  1771. // allocate resulting string
  1772. std::string result;
  1773. result.resize(size);
  1774. // second pass: convert to utf8
  1775. if (size > 0) as_utf8_end(&result[0], size, str, length);
  1776. return result;
  1777. }
  1778. PUGI__FN std::basic_string<wchar_t> as_wide_impl(const char* str, size_t size)
  1779. {
  1780. const uint8_t* data = reinterpret_cast<const uint8_t*>(str);
  1781. // first pass: get length in wchar_t units
  1782. size_t length = utf8_decoder::process(data, size, 0, wchar_counter());
  1783. // allocate resulting string
  1784. std::basic_string<wchar_t> result;
  1785. result.resize(length);
  1786. // second pass: convert to wchar_t
  1787. if (length > 0)
  1788. {
  1789. wchar_writer::value_type begin = reinterpret_cast<wchar_writer::value_type>(&result[0]);
  1790. wchar_writer::value_type end = utf8_decoder::process(data, size, begin, wchar_writer());
  1791. assert(begin + length == end);
  1792. (void)!end;
  1793. }
  1794. return result;
  1795. }
  1796. #endif
  1797. template <typename Header>
  1798. inline bool strcpy_insitu_allow(size_t length, const Header& header, uintptr_t header_mask, char_t* target)
  1799. {
  1800. // never reuse shared memory
  1801. if (header & xml_memory_page_contents_shared_mask) return false;
  1802. size_t target_length = strlength(target);
  1803. // always reuse document buffer memory if possible
  1804. if ((header & header_mask) == 0) return target_length >= length;
  1805. // reuse heap memory if waste is not too great
  1806. const size_t reuse_threshold = 32;
  1807. return target_length >= length && (target_length < reuse_threshold || target_length - length < target_length / 2);
  1808. }
  1809. template <typename String, typename Header>
  1810. PUGI__FN bool strcpy_insitu(String& dest, Header& header, uintptr_t header_mask, const char_t* source, size_t source_length)
  1811. {
  1812. if (source_length == 0)
  1813. {
  1814. // empty string and null pointer are equivalent, so just deallocate old memory
  1815. xml_allocator* alloc = PUGI__GETPAGE_IMPL(header)->allocator;
  1816. if (header & header_mask) alloc->deallocate_string(dest);
  1817. // mark the string as not allocated
  1818. dest = 0;
  1819. header &= ~header_mask;
  1820. return true;
  1821. }
  1822. else if (dest && strcpy_insitu_allow(source_length, header, header_mask, dest))
  1823. {
  1824. // we can reuse old buffer, so just copy the new data (including zero terminator)
  1825. memcpy(dest, source, source_length * sizeof(char_t));
  1826. dest[source_length] = 0;
  1827. return true;
  1828. }
  1829. else
  1830. {
  1831. xml_allocator* alloc = PUGI__GETPAGE_IMPL(header)->allocator;
  1832. if (!alloc->reserve()) return false;
  1833. // allocate new buffer
  1834. char_t* buf = alloc->allocate_string(source_length + 1);
  1835. if (!buf) return false;
  1836. // copy the string (including zero terminator)
  1837. memcpy(buf, source, source_length * sizeof(char_t));
  1838. buf[source_length] = 0;
  1839. // deallocate old buffer (*after* the above to protect against overlapping memory and/or allocation failures)
  1840. if (header & header_mask) alloc->deallocate_string(dest);
  1841. // the string is now allocated, so set the flag
  1842. dest = buf;
  1843. header |= header_mask;
  1844. return true;
  1845. }
  1846. }
  1847. struct gap
  1848. {
  1849. char_t* end;
  1850. size_t size;
  1851. gap(): end(0), size(0)
  1852. {
  1853. }
  1854. // Push new gap, move s count bytes further (skipping the gap).
  1855. // Collapse previous gap.
  1856. void push(char_t*& s, size_t count)
  1857. {
  1858. if (end) // there was a gap already; collapse it
  1859. {
  1860. // Move [old_gap_end, new_gap_start) to [old_gap_start, ...)
  1861. assert(s >= end);
  1862. memmove(end - size, end, reinterpret_cast<char*>(s) - reinterpret_cast<char*>(end));
  1863. }
  1864. s += count; // end of current gap
  1865. // "merge" two gaps
  1866. end = s;
  1867. size += count;
  1868. }
  1869. // Collapse all gaps, return past-the-end pointer
  1870. char_t* flush(char_t* s)
  1871. {
  1872. if (end)
  1873. {
  1874. // Move [old_gap_end, current_pos) to [old_gap_start, ...)
  1875. assert(s >= end);
  1876. memmove(end - size, end, reinterpret_cast<char*>(s) - reinterpret_cast<char*>(end));
  1877. return s - size;
  1878. }
  1879. else return s;
  1880. }
  1881. };
  1882. PUGI__FN char_t* strconv_escape(char_t* s, gap& g)
  1883. {
  1884. char_t* stre = s + 1;
  1885. switch (*stre)
  1886. {
  1887. case '#': // &#...
  1888. {
  1889. unsigned int ucsc = 0;
  1890. if (stre[1] == 'x') // &#x... (hex code)
  1891. {
  1892. stre += 2;
  1893. char_t ch = *stre;
  1894. if (ch == ';') return stre;
  1895. for (;;)
  1896. {
  1897. if (static_cast<unsigned int>(ch - '0') <= 9)
  1898. ucsc = 16 * ucsc + (ch - '0');
  1899. else if (static_cast<unsigned int>((ch | ' ') - 'a') <= 5)
  1900. ucsc = 16 * ucsc + ((ch | ' ') - 'a' + 10);
  1901. else if (ch == ';')
  1902. break;
  1903. else // cancel
  1904. return stre;
  1905. ch = *++stre;
  1906. }
  1907. ++stre;
  1908. }
  1909. else // &#... (dec code)
  1910. {
  1911. char_t ch = *++stre;
  1912. if (ch == ';') return stre;
  1913. for (;;)
  1914. {
  1915. if (static_cast<unsigned int>(static_cast<unsigned int>(ch) - '0') <= 9)
  1916. ucsc = 10 * ucsc + (ch - '0');
  1917. else if (ch == ';')
  1918. break;
  1919. else // cancel
  1920. return stre;
  1921. ch = *++stre;
  1922. }
  1923. ++stre;
  1924. }
  1925. #ifdef PUGIXML_WCHAR_MODE
  1926. s = reinterpret_cast<char_t*>(wchar_writer::any(reinterpret_cast<wchar_writer::value_type>(s), ucsc));
  1927. #else
  1928. s = reinterpret_cast<char_t*>(utf8_writer::any(reinterpret_cast<uint8_t*>(s), ucsc));
  1929. #endif
  1930. g.push(s, stre - s);
  1931. return stre;
  1932. }
  1933. case 'a': // &a
  1934. {
  1935. ++stre;
  1936. if (*stre == 'm') // &am
  1937. {
  1938. if (*++stre == 'p' && *++stre == ';') // &amp;
  1939. {
  1940. *s++ = '&';
  1941. ++stre;
  1942. g.push(s, stre - s);
  1943. return stre;
  1944. }
  1945. }
  1946. else if (*stre == 'p') // &ap
  1947. {
  1948. if (*++stre == 'o' && *++stre == 's' && *++stre == ';') // &apos;
  1949. {
  1950. *s++ = '\'';
  1951. ++stre;
  1952. g.push(s, stre - s);
  1953. return stre;
  1954. }
  1955. }
  1956. break;
  1957. }
  1958. case 'g': // &g
  1959. {
  1960. if (*++stre == 't' && *++stre == ';') // &gt;
  1961. {
  1962. *s++ = '>';
  1963. ++stre;
  1964. g.push(s, stre - s);
  1965. return stre;
  1966. }
  1967. break;
  1968. }
  1969. case 'l': // &l
  1970. {
  1971. if (*++stre == 't' && *++stre == ';') // &lt;
  1972. {
  1973. *s++ = '<';
  1974. ++stre;
  1975. g.push(s, stre - s);
  1976. return stre;
  1977. }
  1978. break;
  1979. }
  1980. case 'q': // &q
  1981. {
  1982. if (*++stre == 'u' && *++stre == 'o' && *++stre == 't' && *++stre == ';') // &quot;
  1983. {
  1984. *s++ = '"';
  1985. ++stre;
  1986. g.push(s, stre - s);
  1987. return stre;
  1988. }
  1989. break;
  1990. }
  1991. default:
  1992. break;
  1993. }
  1994. return stre;
  1995. }
  1996. // Parser utilities
  1997. #define PUGI__ENDSWITH(c, e) ((c) == (e) || ((c) == 0 && endch == (e)))
  1998. #define PUGI__SKIPWS() { while (PUGI__IS_CHARTYPE(*s, ct_space)) ++s; }
  1999. #define PUGI__OPTSET(OPT) ( optmsk & (OPT) )
  2000. #define PUGI__PUSHNODE(TYPE) { cursor = append_new_node(cursor, alloc, TYPE); if (!cursor) PUGI__THROW_ERROR(status_out_of_memory, s); }
  2001. #define PUGI__POPNODE() { cursor = cursor->parent; }
  2002. #define PUGI__SCANFOR(X) { while (*s != 0 && !(X)) ++s; }
  2003. #define PUGI__SCANWHILE(X) { while (X) ++s; }
  2004. #define PUGI__SCANWHILE_UNROLL(X) { for (;;) { char_t ss = s[0]; if (PUGI__UNLIKELY(!(X))) { break; } ss = s[1]; if (PUGI__UNLIKELY(!(X))) { s += 1; break; } ss = s[2]; if (PUGI__UNLIKELY(!(X))) { s += 2; break; } ss = s[3]; if (PUGI__UNLIKELY(!(X))) { s += 3; break; } s += 4; } }
  2005. #define PUGI__ENDSEG() { ch = *s; *s = 0; ++s; }
  2006. #define PUGI__THROW_ERROR(err, m) return error_offset = m, error_status = err, static_cast<char_t*>(0)
  2007. #define PUGI__CHECK_ERROR(err, m) { if (*s == 0) PUGI__THROW_ERROR(err, m); }
  2008. PUGI__FN char_t* strconv_comment(char_t* s, char_t endch)
  2009. {
  2010. gap g;
  2011. while (true)
  2012. {
  2013. PUGI__SCANWHILE_UNROLL(!PUGI__IS_CHARTYPE(ss, ct_parse_comment));
  2014. if (*s == '\r') // Either a single 0x0d or 0x0d 0x0a pair
  2015. {
  2016. *s++ = '\n'; // replace first one with 0x0a
  2017. if (*s == '\n') g.push(s, 1);
  2018. }
  2019. else if (s[0] == '-' && s[1] == '-' && PUGI__ENDSWITH(s[2], '>')) // comment ends here
  2020. {
  2021. *g.flush(s) = 0;
  2022. return s + (s[2] == '>' ? 3 : 2);
  2023. }
  2024. else if (*s == 0)
  2025. {
  2026. return 0;
  2027. }
  2028. else ++s;
  2029. }
  2030. }
  2031. PUGI__FN char_t* strconv_cdata(char_t* s, char_t endch)
  2032. {
  2033. gap g;
  2034. while (true)
  2035. {
  2036. PUGI__SCANWHILE_UNROLL(!PUGI__IS_CHARTYPE(ss, ct_parse_cdata));
  2037. if (*s == '\r') // Either a single 0x0d or 0x0d 0x0a pair
  2038. {
  2039. *s++ = '\n'; // replace first one with 0x0a
  2040. if (*s == '\n') g.push(s, 1);
  2041. }
  2042. else if (s[0] == ']' && s[1] == ']' && PUGI__ENDSWITH(s[2], '>')) // CDATA ends here
  2043. {
  2044. *g.flush(s) = 0;
  2045. return s + 1;
  2046. }
  2047. else if (*s == 0)
  2048. {
  2049. return 0;
  2050. }
  2051. else ++s;
  2052. }
  2053. }
  2054. typedef char_t* (*strconv_pcdata_t)(char_t*);
  2055. template <typename opt_trim, typename opt_eol, typename opt_escape> struct strconv_pcdata_impl
  2056. {
  2057. static char_t* parse(char_t* s)
  2058. {
  2059. gap g;
  2060. char_t* begin = s;
  2061. while (true)
  2062. {
  2063. PUGI__SCANWHILE_UNROLL(!PUGI__IS_CHARTYPE(ss, ct_parse_pcdata));
  2064. if (*s == '<') // PCDATA ends here
  2065. {
  2066. char_t* end = g.flush(s);
  2067. if (opt_trim::value)
  2068. while (end > begin && PUGI__IS_CHARTYPE(end[-1], ct_space))
  2069. --end;
  2070. *end = 0;
  2071. return s + 1;
  2072. }
  2073. else if (opt_eol::value && *s == '\r') // Either a single 0x0d or 0x0d 0x0a pair
  2074. {
  2075. *s++ = '\n'; // replace first one with 0x0a
  2076. if (*s == '\n') g.push(s, 1);
  2077. }
  2078. else if (opt_escape::value && *s == '&')
  2079. {
  2080. s = strconv_escape(s, g);
  2081. }
  2082. else if (*s == 0)
  2083. {
  2084. char_t* end = g.flush(s);
  2085. if (opt_trim::value)
  2086. while (end > begin && PUGI__IS_CHARTYPE(end[-1], ct_space))
  2087. --end;
  2088. *end = 0;
  2089. return s;
  2090. }
  2091. else ++s;
  2092. }
  2093. }
  2094. };
  2095. PUGI__FN strconv_pcdata_t get_strconv_pcdata(unsigned int optmask)
  2096. {
  2097. PUGI__STATIC_ASSERT(parse_escapes == 0x10 && parse_eol == 0x20 && parse_trim_pcdata == 0x0800);
  2098. switch (((optmask >> 4) & 3) | ((optmask >> 9) & 4)) // get bitmask for flags (eol escapes trim)
  2099. {
  2100. case 0: return strconv_pcdata_impl<opt_false, opt_false, opt_false>::parse;
  2101. case 1: return strconv_pcdata_impl<opt_false, opt_false, opt_true>::parse;
  2102. case 2: return strconv_pcdata_impl<opt_false, opt_true, opt_false>::parse;
  2103. case 3: return strconv_pcdata_impl<opt_false, opt_true, opt_true>::parse;
  2104. case 4: return strconv_pcdata_impl<opt_true, opt_false, opt_false>::parse;
  2105. case 5: return strconv_pcdata_impl<opt_true, opt_false, opt_true>::parse;
  2106. case 6: return strconv_pcdata_impl<opt_true, opt_true, opt_false>::parse;
  2107. case 7: return strconv_pcdata_impl<opt_true, opt_true, opt_true>::parse;
  2108. default: assert(false); return 0; // should not get here
  2109. }
  2110. }
  2111. typedef char_t* (*strconv_attribute_t)(char_t*, char_t);
  2112. template <typename opt_escape> struct strconv_attribute_impl
  2113. {
  2114. static char_t* parse_wnorm(char_t* s, char_t end_quote)
  2115. {
  2116. gap g;
  2117. // trim leading whitespaces
  2118. if (PUGI__IS_CHARTYPE(*s, ct_space))
  2119. {
  2120. char_t* str = s;
  2121. do ++str;
  2122. while (PUGI__IS_CHARTYPE(*str, ct_space));
  2123. g.push(s, str - s);
  2124. }
  2125. while (true)
  2126. {
  2127. PUGI__SCANWHILE_UNROLL(!PUGI__IS_CHARTYPE(ss, ct_parse_attr_ws | ct_space));
  2128. if (*s == end_quote)
  2129. {
  2130. char_t* str = g.flush(s);
  2131. do *str-- = 0;
  2132. while (PUGI__IS_CHARTYPE(*str, ct_space));
  2133. return s + 1;
  2134. }
  2135. else if (PUGI__IS_CHARTYPE(*s, ct_space))
  2136. {
  2137. *s++ = ' ';
  2138. if (PUGI__IS_CHARTYPE(*s, ct_space))
  2139. {
  2140. char_t* str = s + 1;
  2141. while (PUGI__IS_CHARTYPE(*str, ct_space)) ++str;
  2142. g.push(s, str - s);
  2143. }
  2144. }
  2145. else if (opt_escape::value && *s == '&')
  2146. {
  2147. s = strconv_escape(s, g);
  2148. }
  2149. else if (!*s)
  2150. {
  2151. return 0;
  2152. }
  2153. else ++s;
  2154. }
  2155. }
  2156. static char_t* parse_wconv(char_t* s, char_t end_quote)
  2157. {
  2158. gap g;
  2159. while (true)
  2160. {
  2161. PUGI__SCANWHILE_UNROLL(!PUGI__IS_CHARTYPE(ss, ct_parse_attr_ws));
  2162. if (*s == end_quote)
  2163. {
  2164. *g.flush(s) = 0;
  2165. return s + 1;
  2166. }
  2167. else if (PUGI__IS_CHARTYPE(*s, ct_space))
  2168. {
  2169. if (*s == '\r')
  2170. {
  2171. *s++ = ' ';
  2172. if (*s == '\n') g.push(s, 1);
  2173. }
  2174. else *s++ = ' ';
  2175. }
  2176. else if (opt_escape::value && *s == '&')
  2177. {
  2178. s = strconv_escape(s, g);
  2179. }
  2180. else if (!*s)
  2181. {
  2182. return 0;
  2183. }
  2184. else ++s;
  2185. }
  2186. }
  2187. static char_t* parse_eol(char_t* s, char_t end_quote)
  2188. {
  2189. gap g;
  2190. while (true)
  2191. {
  2192. PUGI__SCANWHILE_UNROLL(!PUGI__IS_CHARTYPE(ss, ct_parse_attr));
  2193. if (*s == end_quote)
  2194. {
  2195. *g.flush(s) = 0;
  2196. return s + 1;
  2197. }
  2198. else if (*s == '\r')
  2199. {
  2200. *s++ = '\n';
  2201. if (*s == '\n') g.push(s, 1);
  2202. }
  2203. else if (opt_escape::value && *s == '&')
  2204. {
  2205. s = strconv_escape(s, g);
  2206. }
  2207. else if (!*s)
  2208. {
  2209. return 0;
  2210. }
  2211. else ++s;
  2212. }
  2213. }
  2214. static char_t* parse_simple(char_t* s, char_t end_quote)
  2215. {
  2216. gap g;
  2217. while (true)
  2218. {
  2219. PUGI__SCANWHILE_UNROLL(!PUGI__IS_CHARTYPE(ss, ct_parse_attr));
  2220. if (*s == end_quote)
  2221. {
  2222. *g.flush(s) = 0;
  2223. return s + 1;
  2224. }
  2225. else if (opt_escape::value && *s == '&')
  2226. {
  2227. s = strconv_escape(s, g);
  2228. }
  2229. else if (!*s)
  2230. {
  2231. return 0;
  2232. }
  2233. else ++s;
  2234. }
  2235. }
  2236. };
  2237. PUGI__FN strconv_attribute_t get_strconv_attribute(unsigned int optmask)
  2238. {
  2239. PUGI__STATIC_ASSERT(parse_escapes == 0x10 && parse_eol == 0x20 && parse_wconv_attribute == 0x40 && parse_wnorm_attribute == 0x80);
  2240. switch ((optmask >> 4) & 15) // get bitmask for flags (wconv wnorm eol escapes)
  2241. {
  2242. case 0: return strconv_attribute_impl<opt_false>::parse_simple;
  2243. case 1: return strconv_attribute_impl<opt_true>::parse_simple;
  2244. case 2: return strconv_attribute_impl<opt_false>::parse_eol;
  2245. case 3: return strconv_attribute_impl<opt_true>::parse_eol;
  2246. case 4: return strconv_attribute_impl<opt_false>::parse_wconv;
  2247. case 5: return strconv_attribute_impl<opt_true>::parse_wconv;
  2248. case 6: return strconv_attribute_impl<opt_false>::parse_wconv;
  2249. case 7: return strconv_attribute_impl<opt_true>::parse_wconv;
  2250. case 8: return strconv_attribute_impl<opt_false>::parse_wnorm;
  2251. case 9: return strconv_attribute_impl<opt_true>::parse_wnorm;
  2252. case 10: return strconv_attribute_impl<opt_false>::parse_wnorm;
  2253. case 11: return strconv_attribute_impl<opt_true>::parse_wnorm;
  2254. case 12: return strconv_attribute_impl<opt_false>::parse_wnorm;
  2255. case 13: return strconv_attribute_impl<opt_true>::parse_wnorm;
  2256. case 14: return strconv_attribute_impl<opt_false>::parse_wnorm;
  2257. case 15: return strconv_attribute_impl<opt_true>::parse_wnorm;
  2258. default: assert(false); return 0; // should not get here
  2259. }
  2260. }
  2261. inline xml_parse_result make_parse_result(xml_parse_status status, ptrdiff_t offset = 0)
  2262. {
  2263. xml_parse_result result;
  2264. result.status = status;
  2265. result.offset = offset;
  2266. return result;
  2267. }
  2268. struct xml_parser
  2269. {
  2270. xml_allocator alloc;
  2271. xml_allocator* alloc_state;
  2272. char_t* error_offset;
  2273. xml_parse_status error_status;
  2274. xml_parser(xml_allocator* alloc_): alloc(*alloc_), alloc_state(alloc_), error_offset(0), error_status(status_ok)
  2275. {
  2276. }
  2277. ~xml_parser()
  2278. {
  2279. *alloc_state = alloc;
  2280. }
  2281. // DOCTYPE consists of nested sections of the following possible types:
  2282. // <!-- ... -->, <? ... ?>, "...", '...'
  2283. // <![...]]>
  2284. // <!...>
  2285. // First group can not contain nested groups
  2286. // Second group can contain nested groups of the same type
  2287. // Third group can contain all other groups
  2288. char_t* parse_doctype_primitive(char_t* s)
  2289. {
  2290. if (*s == '"' || *s == '\'')
  2291. {
  2292. // quoted string
  2293. char_t ch = *s++;
  2294. PUGI__SCANFOR(*s == ch);
  2295. if (!*s) PUGI__THROW_ERROR(status_bad_doctype, s);
  2296. s++;
  2297. }
  2298. else if (s[0] == '<' && s[1] == '?')
  2299. {
  2300. // <? ... ?>
  2301. s += 2;
  2302. PUGI__SCANFOR(s[0] == '?' && s[1] == '>'); // no need for ENDSWITH because ?> can't terminate proper doctype
  2303. if (!*s) PUGI__THROW_ERROR(status_bad_doctype, s);
  2304. s += 2;
  2305. }
  2306. else if (s[0] == '<' && s[1] == '!' && s[2] == '-' && s[3] == '-')
  2307. {
  2308. s += 4;
  2309. PUGI__SCANFOR(s[0] == '-' && s[1] == '-' && s[2] == '>'); // no need for ENDSWITH because --> can't terminate proper doctype
  2310. if (!*s) PUGI__THROW_ERROR(status_bad_doctype, s);
  2311. s += 3;
  2312. }
  2313. else PUGI__THROW_ERROR(status_bad_doctype, s);
  2314. return s;
  2315. }
  2316. char_t* parse_doctype_ignore(char_t* s)
  2317. {
  2318. size_t depth = 0;
  2319. assert(s[0] == '<' && s[1] == '!' && s[2] == '[');
  2320. s += 3;
  2321. while (*s)
  2322. {
  2323. if (s[0] == '<' && s[1] == '!' && s[2] == '[')
  2324. {
  2325. // nested ignore section
  2326. s += 3;
  2327. depth++;
  2328. }
  2329. else if (s[0] == ']' && s[1] == ']' && s[2] == '>')
  2330. {
  2331. // ignore section end
  2332. s += 3;
  2333. if (depth == 0)
  2334. return s;
  2335. depth--;
  2336. }
  2337. else s++;
  2338. }
  2339. PUGI__THROW_ERROR(status_bad_doctype, s);
  2340. }
  2341. char_t* parse_doctype_group(char_t* s, char_t endch)
  2342. {
  2343. size_t depth = 0;
  2344. assert((s[0] == '<' || s[0] == 0) && s[1] == '!');
  2345. s += 2;
  2346. while (*s)
  2347. {
  2348. if (s[0] == '<' && s[1] == '!' && s[2] != '-')
  2349. {
  2350. if (s[2] == '[')
  2351. {
  2352. // ignore
  2353. s = parse_doctype_ignore(s);
  2354. if (!s) return s;
  2355. }
  2356. else
  2357. {
  2358. // some control group
  2359. s += 2;
  2360. depth++;
  2361. }
  2362. }
  2363. else if (s[0] == '<' || s[0] == '"' || s[0] == '\'')
  2364. {
  2365. // unknown tag (forbidden), or some primitive group
  2366. s = parse_doctype_primitive(s);
  2367. if (!s) return s;
  2368. }
  2369. else if (*s == '>')
  2370. {
  2371. if (depth == 0)
  2372. return s;
  2373. depth--;
  2374. s++;
  2375. }
  2376. else s++;
  2377. }
  2378. if (depth != 0 || endch != '>') PUGI__THROW_ERROR(status_bad_doctype, s);
  2379. return s;
  2380. }
  2381. char_t* parse_exclamation(char_t* s, xml_node_struct* cursor, unsigned int optmsk, char_t endch)
  2382. {
  2383. // parse node contents, starting with exclamation mark
  2384. ++s;
  2385. if (*s == '-') // '<!-...'
  2386. {
  2387. ++s;
  2388. if (*s == '-') // '<!--...'
  2389. {
  2390. ++s;
  2391. if (PUGI__OPTSET(parse_comments))
  2392. {
  2393. PUGI__PUSHNODE(node_comment); // Append a new node on the tree.
  2394. cursor->value = s; // Save the offset.
  2395. }
  2396. if (PUGI__OPTSET(parse_eol) && PUGI__OPTSET(parse_comments))
  2397. {
  2398. s = strconv_comment(s, endch);
  2399. if (!s) PUGI__THROW_ERROR(status_bad_comment, cursor->value);
  2400. }
  2401. else
  2402. {
  2403. // Scan for terminating '-->'.
  2404. PUGI__SCANFOR(s[0] == '-' && s[1] == '-' && PUGI__ENDSWITH(s[2], '>'));
  2405. PUGI__CHECK_ERROR(status_bad_comment, s);
  2406. if (PUGI__OPTSET(parse_comments))
  2407. *s = 0; // Zero-terminate this segment at the first terminating '-'.
  2408. s += (s[2] == '>' ? 3 : 2); // Step over the '\0->'.
  2409. }
  2410. }
  2411. else PUGI__THROW_ERROR(status_bad_comment, s);
  2412. }
  2413. else if (*s == '[')
  2414. {
  2415. // '<![CDATA[...'
  2416. if (*++s=='C' && *++s=='D' && *++s=='A' && *++s=='T' && *++s=='A' && *++s == '[')
  2417. {
  2418. ++s;
  2419. if (PUGI__OPTSET(parse_cdata))
  2420. {
  2421. PUGI__PUSHNODE(node_cdata); // Append a new node on the tree.
  2422. cursor->value = s; // Save the offset.
  2423. if (PUGI__OPTSET(parse_eol))
  2424. {
  2425. s = strconv_cdata(s, endch);
  2426. if (!s) PUGI__THROW_ERROR(status_bad_cdata, cursor->value);
  2427. }
  2428. else
  2429. {
  2430. // Scan for terminating ']]>'.
  2431. PUGI__SCANFOR(s[0] == ']' && s[1] == ']' && PUGI__ENDSWITH(s[2], '>'));
  2432. PUGI__CHECK_ERROR(status_bad_cdata, s);
  2433. *s++ = 0; // Zero-terminate this segment.
  2434. }
  2435. }
  2436. else // Flagged for discard, but we still have to scan for the terminator.
  2437. {
  2438. // Scan for terminating ']]>'.
  2439. PUGI__SCANFOR(s[0] == ']' && s[1] == ']' && PUGI__ENDSWITH(s[2], '>'));
  2440. PUGI__CHECK_ERROR(status_bad_cdata, s);
  2441. ++s;
  2442. }
  2443. s += (s[1] == '>' ? 2 : 1); // Step over the last ']>'.
  2444. }
  2445. else PUGI__THROW_ERROR(status_bad_cdata, s);
  2446. }
  2447. else if (s[0] == 'D' && s[1] == 'O' && s[2] == 'C' && s[3] == 'T' && s[4] == 'Y' && s[5] == 'P' && PUGI__ENDSWITH(s[6], 'E'))
  2448. {
  2449. s -= 2;
  2450. if (cursor->parent) PUGI__THROW_ERROR(status_bad_doctype, s);
  2451. char_t* mark = s + 9;
  2452. s = parse_doctype_group(s, endch);
  2453. if (!s) return s;
  2454. assert((*s == 0 && endch == '>') || *s == '>');
  2455. if (*s) *s++ = 0;
  2456. if (PUGI__OPTSET(parse_doctype))
  2457. {
  2458. while (PUGI__IS_CHARTYPE(*mark, ct_space)) ++mark;
  2459. PUGI__PUSHNODE(node_doctype);
  2460. cursor->value = mark;
  2461. }
  2462. }
  2463. else if (*s == 0 && endch == '-') PUGI__THROW_ERROR(status_bad_comment, s);
  2464. else if (*s == 0 && endch == '[') PUGI__THROW_ERROR(status_bad_cdata, s);
  2465. else PUGI__THROW_ERROR(status_unrecognized_tag, s);
  2466. return s;
  2467. }
  2468. char_t* parse_question(char_t* s, xml_node_struct*& ref_cursor, unsigned int optmsk, char_t endch)
  2469. {
  2470. // load into registers
  2471. xml_node_struct* cursor = ref_cursor;
  2472. char_t ch = 0;
  2473. // parse node contents, starting with question mark
  2474. ++s;
  2475. // read PI target
  2476. char_t* target = s;
  2477. if (!PUGI__IS_CHARTYPE(*s, ct_start_symbol)) PUGI__THROW_ERROR(status_bad_pi, s);
  2478. PUGI__SCANWHILE(PUGI__IS_CHARTYPE(*s, ct_symbol));
  2479. PUGI__CHECK_ERROR(status_bad_pi, s);
  2480. // determine node type; stricmp / strcasecmp is not portable
  2481. bool declaration = (target[0] | ' ') == 'x' && (target[1] | ' ') == 'm' && (target[2] | ' ') == 'l' && target + 3 == s;
  2482. if (declaration ? PUGI__OPTSET(parse_declaration) : PUGI__OPTSET(parse_pi))
  2483. {
  2484. if (declaration)
  2485. {
  2486. // disallow non top-level declarations
  2487. if (cursor->parent) PUGI__THROW_ERROR(status_bad_pi, s);
  2488. PUGI__PUSHNODE(node_declaration);
  2489. }
  2490. else
  2491. {
  2492. PUGI__PUSHNODE(node_pi);
  2493. }
  2494. cursor->name = target;
  2495. PUGI__ENDSEG();
  2496. // parse value/attributes
  2497. if (ch == '?')
  2498. {
  2499. // empty node
  2500. if (!PUGI__ENDSWITH(*s, '>')) PUGI__THROW_ERROR(status_bad_pi, s);
  2501. s += (*s == '>');
  2502. PUGI__POPNODE();
  2503. }
  2504. else if (PUGI__IS_CHARTYPE(ch, ct_space))
  2505. {
  2506. PUGI__SKIPWS();
  2507. // scan for tag end
  2508. char_t* value = s;
  2509. PUGI__SCANFOR(s[0] == '?' && PUGI__ENDSWITH(s[1], '>'));
  2510. PUGI__CHECK_ERROR(status_bad_pi, s);
  2511. if (declaration)
  2512. {
  2513. // replace ending ? with / so that 'element' terminates properly
  2514. *s = '/';
  2515. // we exit from this function with cursor at node_declaration, which is a signal to parse() to go to LOC_ATTRIBUTES
  2516. s = value;
  2517. }
  2518. else
  2519. {
  2520. // store value and step over >
  2521. cursor->value = value;
  2522. PUGI__POPNODE();
  2523. PUGI__ENDSEG();
  2524. s += (*s == '>');
  2525. }
  2526. }
  2527. else PUGI__THROW_ERROR(status_bad_pi, s);
  2528. }
  2529. else
  2530. {
  2531. // scan for tag end
  2532. PUGI__SCANFOR(s[0] == '?' && PUGI__ENDSWITH(s[1], '>'));
  2533. PUGI__CHECK_ERROR(status_bad_pi, s);
  2534. s += (s[1] == '>' ? 2 : 1);
  2535. }
  2536. // store from registers
  2537. ref_cursor = cursor;
  2538. return s;
  2539. }
  2540. char_t* parse_tree(char_t* s, xml_node_struct* root, unsigned int optmsk, char_t endch)
  2541. {
  2542. strconv_attribute_t strconv_attribute = get_strconv_attribute(optmsk);
  2543. strconv_pcdata_t strconv_pcdata = get_strconv_pcdata(optmsk);
  2544. char_t ch = 0;
  2545. xml_node_struct* cursor = root;
  2546. char_t* mark = s;
  2547. while (*s != 0)
  2548. {
  2549. if (*s == '<')
  2550. {
  2551. ++s;
  2552. LOC_TAG:
  2553. if (PUGI__IS_CHARTYPE(*s, ct_start_symbol)) // '<#...'
  2554. {
  2555. PUGI__PUSHNODE(node_element); // Append a new node to the tree.
  2556. cursor->name = s;
  2557. PUGI__SCANWHILE_UNROLL(PUGI__IS_CHARTYPE(ss, ct_symbol)); // Scan for a terminator.
  2558. PUGI__ENDSEG(); // Save char in 'ch', terminate & step over.
  2559. if (ch == '>')
  2560. {
  2561. // end of tag
  2562. }
  2563. else if (PUGI__IS_CHARTYPE(ch, ct_space))
  2564. {
  2565. LOC_ATTRIBUTES:
  2566. while (true)
  2567. {
  2568. PUGI__SKIPWS(); // Eat any whitespace.
  2569. if (PUGI__IS_CHARTYPE(*s, ct_start_symbol)) // <... #...
  2570. {
  2571. xml_attribute_struct* a = append_new_attribute(cursor, alloc); // Make space for this attribute.
  2572. if (!a) PUGI__THROW_ERROR(status_out_of_memory, s);
  2573. a->name = s; // Save the offset.
  2574. PUGI__SCANWHILE_UNROLL(PUGI__IS_CHARTYPE(ss, ct_symbol)); // Scan for a terminator.
  2575. PUGI__ENDSEG(); // Save char in 'ch', terminate & step over.
  2576. if (PUGI__IS_CHARTYPE(ch, ct_space))
  2577. {
  2578. PUGI__SKIPWS(); // Eat any whitespace.
  2579. ch = *s;
  2580. ++s;
  2581. }
  2582. if (ch == '=') // '<... #=...'
  2583. {
  2584. PUGI__SKIPWS(); // Eat any whitespace.
  2585. if (*s == '"' || *s == '\'') // '<... #="...'
  2586. {
  2587. ch = *s; // Save quote char to avoid breaking on "''" -or- '""'.
  2588. ++s; // Step over the quote.
  2589. a->value = s; // Save the offset.
  2590. s = strconv_attribute(s, ch);
  2591. if (!s) PUGI__THROW_ERROR(status_bad_attribute, a->value);
  2592. // After this line the loop continues from the start;
  2593. // Whitespaces, / and > are ok, symbols and EOF are wrong,
  2594. // everything else will be detected
  2595. if (PUGI__IS_CHARTYPE(*s, ct_start_symbol)) PUGI__THROW_ERROR(status_bad_attribute, s);
  2596. }
  2597. else PUGI__THROW_ERROR(status_bad_attribute, s);
  2598. }
  2599. else PUGI__THROW_ERROR(status_bad_attribute, s);
  2600. }
  2601. else if (*s == '/')
  2602. {
  2603. ++s;
  2604. if (*s == '>')
  2605. {
  2606. PUGI__POPNODE();
  2607. s++;
  2608. break;
  2609. }
  2610. else if (*s == 0 && endch == '>')
  2611. {
  2612. PUGI__POPNODE();
  2613. break;
  2614. }
  2615. else PUGI__THROW_ERROR(status_bad_start_element, s);
  2616. }
  2617. else if (*s == '>')
  2618. {
  2619. ++s;
  2620. break;
  2621. }
  2622. else if (*s == 0 && endch == '>')
  2623. {
  2624. break;
  2625. }
  2626. else PUGI__THROW_ERROR(status_bad_start_element, s);
  2627. }
  2628. // !!!
  2629. }
  2630. else if (ch == '/') // '<#.../'
  2631. {
  2632. if (!PUGI__ENDSWITH(*s, '>')) PUGI__THROW_ERROR(status_bad_start_element, s);
  2633. PUGI__POPNODE(); // Pop.
  2634. s += (*s == '>');
  2635. }
  2636. else if (ch == 0)
  2637. {
  2638. // we stepped over null terminator, backtrack & handle closing tag
  2639. --s;
  2640. if (endch != '>') PUGI__THROW_ERROR(status_bad_start_element, s);
  2641. }
  2642. else PUGI__THROW_ERROR(status_bad_start_element, s);
  2643. }
  2644. else if (*s == '/')
  2645. {
  2646. ++s;
  2647. char_t* name = cursor->name;
  2648. if (!name) PUGI__THROW_ERROR(status_end_element_mismatch, s);
  2649. while (PUGI__IS_CHARTYPE(*s, ct_symbol))
  2650. {
  2651. if (*s++ != *name++) PUGI__THROW_ERROR(status_end_element_mismatch, s);
  2652. }
  2653. if (*name)
  2654. {
  2655. if (*s == 0 && name[0] == endch && name[1] == 0) PUGI__THROW_ERROR(status_bad_end_element, s);
  2656. else PUGI__THROW_ERROR(status_end_element_mismatch, s);
  2657. }
  2658. PUGI__POPNODE(); // Pop.
  2659. PUGI__SKIPWS();
  2660. if (*s == 0)
  2661. {
  2662. if (endch != '>') PUGI__THROW_ERROR(status_bad_end_element, s);
  2663. }
  2664. else
  2665. {
  2666. if (*s != '>') PUGI__THROW_ERROR(status_bad_end_element, s);
  2667. ++s;
  2668. }
  2669. }
  2670. else if (*s == '?') // '<?...'
  2671. {
  2672. s = parse_question(s, cursor, optmsk, endch);
  2673. if (!s) return s;
  2674. assert(cursor);
  2675. if (PUGI__NODETYPE(cursor) == node_declaration) goto LOC_ATTRIBUTES;
  2676. }
  2677. else if (*s == '!') // '<!...'
  2678. {
  2679. s = parse_exclamation(s, cursor, optmsk, endch);
  2680. if (!s) return s;
  2681. }
  2682. else if (*s == 0 && endch == '?') PUGI__THROW_ERROR(status_bad_pi, s);
  2683. else PUGI__THROW_ERROR(status_unrecognized_tag, s);
  2684. }
  2685. else
  2686. {
  2687. mark = s; // Save this offset while searching for a terminator.
  2688. PUGI__SKIPWS(); // Eat whitespace if no genuine PCDATA here.
  2689. if (*s == '<' || !*s)
  2690. {
  2691. // We skipped some whitespace characters because otherwise we would take the tag branch instead of PCDATA one
  2692. assert(mark != s);
  2693. if (!PUGI__OPTSET(parse_ws_pcdata | parse_ws_pcdata_single) || PUGI__OPTSET(parse_trim_pcdata))
  2694. {
  2695. continue;
  2696. }
  2697. else if (PUGI__OPTSET(parse_ws_pcdata_single))
  2698. {
  2699. if (s[0] != '<' || s[1] != '/' || cursor->first_child) continue;
  2700. }
  2701. }
  2702. if (!PUGI__OPTSET(parse_trim_pcdata))
  2703. s = mark;
  2704. if (cursor->parent || PUGI__OPTSET(parse_fragment))
  2705. {
  2706. PUGI__PUSHNODE(node_pcdata); // Append a new node on the tree.
  2707. cursor->value = s; // Save the offset.
  2708. s = strconv_pcdata(s);
  2709. PUGI__POPNODE(); // Pop since this is a standalone.
  2710. if (!*s) break;
  2711. }
  2712. else
  2713. {
  2714. PUGI__SCANFOR(*s == '<'); // '...<'
  2715. if (!*s) break;
  2716. ++s;
  2717. }
  2718. // We're after '<'
  2719. goto LOC_TAG;
  2720. }
  2721. }
  2722. // check that last tag is closed
  2723. if (cursor != root) PUGI__THROW_ERROR(status_end_element_mismatch, s);
  2724. return s;
  2725. }
  2726. #ifdef PUGIXML_WCHAR_MODE
  2727. static char_t* parse_skip_bom(char_t* s)
  2728. {
  2729. unsigned int bom = 0xfeff;
  2730. return (s[0] == static_cast<wchar_t>(bom)) ? s + 1 : s;
  2731. }
  2732. #else
  2733. static char_t* parse_skip_bom(char_t* s)
  2734. {
  2735. return (s[0] == '\xef' && s[1] == '\xbb' && s[2] == '\xbf') ? s + 3 : s;
  2736. }
  2737. #endif
  2738. static bool has_element_node_siblings(xml_node_struct* node)
  2739. {
  2740. while (node)
  2741. {
  2742. if (PUGI__NODETYPE(node) == node_element) return true;
  2743. node = node->next_sibling;
  2744. }
  2745. return false;
  2746. }
  2747. static xml_parse_result parse(char_t* buffer, size_t length, xml_document_struct* xmldoc, xml_node_struct* root, unsigned int optmsk)
  2748. {
  2749. // early-out for empty documents
  2750. if (length == 0)
  2751. return make_parse_result(PUGI__OPTSET(parse_fragment) ? status_ok : status_no_document_element);
  2752. // get last child of the root before parsing
  2753. xml_node_struct* last_root_child = root->first_child ? root->first_child->prev_sibling_c + 0 : 0;
  2754. // create parser on stack
  2755. xml_parser parser(static_cast<xml_allocator*>(xmldoc));
  2756. // save last character and make buffer zero-terminated (speeds up parsing)
  2757. char_t endch = buffer[length - 1];
  2758. buffer[length - 1] = 0;
  2759. // skip BOM to make sure it does not end up as part of parse output
  2760. char_t* buffer_data = parse_skip_bom(buffer);
  2761. // perform actual parsing
  2762. parser.parse_tree(buffer_data, root, optmsk, endch);
  2763. xml_parse_result result = make_parse_result(parser.error_status, parser.error_offset ? parser.error_offset - buffer : 0);
  2764. assert(result.offset >= 0 && static_cast<size_t>(result.offset) <= length);
  2765. if (result)
  2766. {
  2767. // since we removed last character, we have to handle the only possible false positive (stray <)
  2768. if (endch == '<')
  2769. return make_parse_result(status_unrecognized_tag, length - 1);
  2770. // check if there are any element nodes parsed
  2771. xml_node_struct* first_root_child_parsed = last_root_child ? last_root_child->next_sibling + 0 : root->first_child+ 0;
  2772. if (!PUGI__OPTSET(parse_fragment) && !has_element_node_siblings(first_root_child_parsed))
  2773. return make_parse_result(status_no_document_element, length - 1);
  2774. }
  2775. else
  2776. {
  2777. // roll back offset if it occurs on a null terminator in the source buffer
  2778. if (result.offset > 0 && static_cast<size_t>(result.offset) == length - 1 && endch == 0)
  2779. result.offset--;
  2780. }
  2781. return result;
  2782. }
  2783. };
  2784. // Output facilities
  2785. PUGI__FN xml_encoding get_write_native_encoding()
  2786. {
  2787. #ifdef PUGIXML_WCHAR_MODE
  2788. return get_wchar_encoding();
  2789. #else
  2790. return encoding_utf8;
  2791. #endif
  2792. }
  2793. PUGI__FN xml_encoding get_write_encoding(xml_encoding encoding)
  2794. {
  2795. // replace wchar encoding with utf implementation
  2796. if (encoding == encoding_wchar) return get_wchar_encoding();
  2797. // replace utf16 encoding with utf16 with specific endianness
  2798. if (encoding == encoding_utf16) return is_little_endian() ? encoding_utf16_le : encoding_utf16_be;
  2799. // replace utf32 encoding with utf32 with specific endianness
  2800. if (encoding == encoding_utf32) return is_little_endian() ? encoding_utf32_le : encoding_utf32_be;
  2801. // only do autodetection if no explicit encoding is requested
  2802. if (encoding != encoding_auto) return encoding;
  2803. // assume utf8 encoding
  2804. return encoding_utf8;
  2805. }
  2806. template <typename D, typename T> PUGI__FN size_t convert_buffer_output_generic(typename T::value_type dest, const char_t* data, size_t length, D, T)
  2807. {
  2808. PUGI__STATIC_ASSERT(sizeof(char_t) == sizeof(typename D::type));
  2809. typename T::value_type end = D::process(reinterpret_cast<const typename D::type*>(data), length, dest, T());
  2810. return static_cast<size_t>(end - dest) * sizeof(*dest);
  2811. }
  2812. template <typename D, typename T> PUGI__FN size_t convert_buffer_output_generic(typename T::value_type dest, const char_t* data, size_t length, D, T, bool opt_swap)
  2813. {
  2814. PUGI__STATIC_ASSERT(sizeof(char_t) == sizeof(typename D::type));
  2815. typename T::value_type end = D::process(reinterpret_cast<const typename D::type*>(data), length, dest, T());
  2816. if (opt_swap)
  2817. {
  2818. for (typename T::value_type i = dest; i != end; ++i)
  2819. *i = endian_swap(*i);
  2820. }
  2821. return static_cast<size_t>(end - dest) * sizeof(*dest);
  2822. }
  2823. #ifdef PUGIXML_WCHAR_MODE
  2824. PUGI__FN size_t get_valid_length(const char_t* data, size_t length)
  2825. {
  2826. if (length < 1) return 0;
  2827. // discard last character if it's the lead of a surrogate pair
  2828. return (sizeof(wchar_t) == 2 && static_cast<unsigned int>(static_cast<uint16_t>(data[length - 1]) - 0xD800) < 0x400) ? length - 1 : length;
  2829. }
  2830. PUGI__FN size_t convert_buffer_output(char_t* r_char, uint8_t* r_u8, uint16_t* r_u16, uint32_t* r_u32, const char_t* data, size_t length, xml_encoding encoding)
  2831. {
  2832. // only endian-swapping is required
  2833. if (need_endian_swap_utf(encoding, get_wchar_encoding()))
  2834. {
  2835. convert_wchar_endian_swap(r_char, data, length);
  2836. return length * sizeof(char_t);
  2837. }
  2838. // convert to utf8
  2839. if (encoding == encoding_utf8)
  2840. return convert_buffer_output_generic(r_u8, data, length, wchar_decoder(), utf8_writer());
  2841. // convert to utf16
  2842. if (encoding == encoding_utf16_be || encoding == encoding_utf16_le)
  2843. {
  2844. xml_encoding native_encoding = is_little_endian() ? encoding_utf16_le : encoding_utf16_be;
  2845. return convert_buffer_output_generic(r_u16, data, length, wchar_decoder(), utf16_writer(), native_encoding != encoding);
  2846. }
  2847. // convert to utf32
  2848. if (encoding == encoding_utf32_be || encoding == encoding_utf32_le)
  2849. {
  2850. xml_encoding native_encoding = is_little_endian() ? encoding_utf32_le : encoding_utf32_be;
  2851. return convert_buffer_output_generic(r_u32, data, length, wchar_decoder(), utf32_writer(), native_encoding != encoding);
  2852. }
  2853. // convert to latin1
  2854. if (encoding == encoding_latin1)
  2855. return convert_buffer_output_generic(r_u8, data, length, wchar_decoder(), latin1_writer());
  2856. assert(!"Invalid encoding");
  2857. return 0;
  2858. }
  2859. #else
  2860. PUGI__FN size_t get_valid_length(const char_t* data, size_t length)
  2861. {
  2862. if (length < 5) return 0;
  2863. for (size_t i = 1; i <= 4; ++i)
  2864. {
  2865. uint8_t ch = static_cast<uint8_t>(data[length - i]);
  2866. // either a standalone character or a leading one
  2867. if ((ch & 0xc0) != 0x80) return length - i;
  2868. }
  2869. // there are four non-leading characters at the end, sequence tail is broken so might as well process the whole chunk
  2870. return length;
  2871. }
  2872. PUGI__FN size_t convert_buffer_output(char_t* /* r_char */, uint8_t* r_u8, uint16_t* r_u16, uint32_t* r_u32, const char_t* data, size_t length, xml_encoding encoding)
  2873. {
  2874. if (encoding == encoding_utf16_be || encoding == encoding_utf16_le)
  2875. {
  2876. xml_encoding native_encoding = is_little_endian() ? encoding_utf16_le : encoding_utf16_be;
  2877. return convert_buffer_output_generic(r_u16, data, length, utf8_decoder(), utf16_writer(), native_encoding != encoding);
  2878. }
  2879. if (encoding == encoding_utf32_be || encoding == encoding_utf32_le)
  2880. {
  2881. xml_encoding native_encoding = is_little_endian() ? encoding_utf32_le : encoding_utf32_be;
  2882. return convert_buffer_output_generic(r_u32, data, length, utf8_decoder(), utf32_writer(), native_encoding != encoding);
  2883. }
  2884. if (encoding == encoding_latin1)
  2885. return convert_buffer_output_generic(r_u8, data, length, utf8_decoder(), latin1_writer());
  2886. assert(!"Invalid encoding");
  2887. return 0;
  2888. }
  2889. #endif
  2890. class xml_buffered_writer
  2891. {
  2892. xml_buffered_writer(const xml_buffered_writer&);
  2893. xml_buffered_writer& operator=(const xml_buffered_writer&);
  2894. public:
  2895. xml_buffered_writer(xml_writer& writer_, xml_encoding user_encoding): writer(writer_), bufsize(0), encoding(get_write_encoding(user_encoding))
  2896. {
  2897. PUGI__STATIC_ASSERT(bufcapacity >= 8);
  2898. }
  2899. size_t flush()
  2900. {
  2901. flush(buffer, bufsize);
  2902. bufsize = 0;
  2903. return 0;
  2904. }
  2905. void flush(const char_t* data, size_t size)
  2906. {
  2907. if (size == 0) return;
  2908. // fast path, just write data
  2909. if (encoding == get_write_native_encoding())
  2910. writer.write(data, size * sizeof(char_t));
  2911. else
  2912. {
  2913. // convert chunk
  2914. size_t result = convert_buffer_output(scratch.data_char, scratch.data_u8, scratch.data_u16, scratch.data_u32, data, size, encoding);
  2915. assert(result <= sizeof(scratch));
  2916. // write data
  2917. writer.write(scratch.data_u8, result);
  2918. }
  2919. }
  2920. void write_direct(const char_t* data, size_t length)
  2921. {
  2922. // flush the remaining buffer contents
  2923. flush();
  2924. // handle large chunks
  2925. if (length > bufcapacity)
  2926. {
  2927. if (encoding == get_write_native_encoding())
  2928. {
  2929. // fast path, can just write data chunk
  2930. writer.write(data, length * sizeof(char_t));
  2931. return;
  2932. }
  2933. // need to convert in suitable chunks
  2934. while (length > bufcapacity)
  2935. {
  2936. // get chunk size by selecting such number of characters that are guaranteed to fit into scratch buffer
  2937. // and form a complete codepoint sequence (i.e. discard start of last codepoint if necessary)
  2938. size_t chunk_size = get_valid_length(data, bufcapacity);
  2939. assert(chunk_size);
  2940. // convert chunk and write
  2941. flush(data, chunk_size);
  2942. // iterate
  2943. data += chunk_size;
  2944. length -= chunk_size;
  2945. }
  2946. // small tail is copied below
  2947. bufsize = 0;
  2948. }
  2949. memcpy(buffer + bufsize, data, length * sizeof(char_t));
  2950. bufsize += length;
  2951. }
  2952. void write_buffer(const char_t* data, size_t length)
  2953. {
  2954. size_t offset = bufsize;
  2955. if (offset + length <= bufcapacity)
  2956. {
  2957. memcpy(buffer + offset, data, length * sizeof(char_t));
  2958. bufsize = offset + length;
  2959. }
  2960. else
  2961. {
  2962. write_direct(data, length);
  2963. }
  2964. }
  2965. void write_string(const char_t* data)
  2966. {
  2967. // write the part of the string that fits in the buffer
  2968. size_t offset = bufsize;
  2969. while (*data && offset < bufcapacity)
  2970. buffer[offset++] = *data++;
  2971. // write the rest
  2972. if (offset < bufcapacity)
  2973. {
  2974. bufsize = offset;
  2975. }
  2976. else
  2977. {
  2978. // backtrack a bit if we have split the codepoint
  2979. size_t length = offset - bufsize;
  2980. size_t extra = length - get_valid_length(data - length, length);
  2981. bufsize = offset - extra;
  2982. write_direct(data - extra, strlength(data) + extra);
  2983. }
  2984. }
  2985. void write(char_t d0)
  2986. {
  2987. size_t offset = bufsize;
  2988. if (offset > bufcapacity - 1) offset = flush();
  2989. buffer[offset + 0] = d0;
  2990. bufsize = offset + 1;
  2991. }
  2992. void write(char_t d0, char_t d1)
  2993. {
  2994. size_t offset = bufsize;
  2995. if (offset > bufcapacity - 2) offset = flush();
  2996. buffer[offset + 0] = d0;
  2997. buffer[offset + 1] = d1;
  2998. bufsize = offset + 2;
  2999. }
  3000. void write(char_t d0, char_t d1, char_t d2)
  3001. {
  3002. size_t offset = bufsize;
  3003. if (offset > bufcapacity - 3) offset = flush();
  3004. buffer[offset + 0] = d0;
  3005. buffer[offset + 1] = d1;
  3006. buffer[offset + 2] = d2;
  3007. bufsize = offset + 3;
  3008. }
  3009. void write(char_t d0, char_t d1, char_t d2, char_t d3)
  3010. {
  3011. size_t offset = bufsize;
  3012. if (offset > bufcapacity - 4) offset = flush();
  3013. buffer[offset + 0] = d0;
  3014. buffer[offset + 1] = d1;
  3015. buffer[offset + 2] = d2;
  3016. buffer[offset + 3] = d3;
  3017. bufsize = offset + 4;
  3018. }
  3019. void write(char_t d0, char_t d1, char_t d2, char_t d3, char_t d4)
  3020. {
  3021. size_t offset = bufsize;
  3022. if (offset > bufcapacity - 5) offset = flush();
  3023. buffer[offset + 0] = d0;
  3024. buffer[offset + 1] = d1;
  3025. buffer[offset + 2] = d2;
  3026. buffer[offset + 3] = d3;
  3027. buffer[offset + 4] = d4;
  3028. bufsize = offset + 5;
  3029. }
  3030. void write(char_t d0, char_t d1, char_t d2, char_t d3, char_t d4, char_t d5)
  3031. {
  3032. size_t offset = bufsize;
  3033. if (offset > bufcapacity - 6) offset = flush();
  3034. buffer[offset + 0] = d0;
  3035. buffer[offset + 1] = d1;
  3036. buffer[offset + 2] = d2;
  3037. buffer[offset + 3] = d3;
  3038. buffer[offset + 4] = d4;
  3039. buffer[offset + 5] = d5;
  3040. bufsize = offset + 6;
  3041. }
  3042. // utf8 maximum expansion: x4 (-> utf32)
  3043. // utf16 maximum expansion: x2 (-> utf32)
  3044. // utf32 maximum expansion: x1
  3045. enum
  3046. {
  3047. bufcapacitybytes =
  3048. #ifdef PUGIXML_MEMORY_OUTPUT_STACK
  3049. PUGIXML_MEMORY_OUTPUT_STACK
  3050. #else
  3051. 10240
  3052. #endif
  3053. ,
  3054. bufcapacity = bufcapacitybytes / (sizeof(char_t) + 4)
  3055. };
  3056. char_t buffer[bufcapacity];
  3057. union
  3058. {
  3059. uint8_t data_u8[4 * bufcapacity];
  3060. uint16_t data_u16[2 * bufcapacity];
  3061. uint32_t data_u32[bufcapacity];
  3062. char_t data_char[bufcapacity];
  3063. } scratch;
  3064. xml_writer& writer;
  3065. size_t bufsize;
  3066. xml_encoding encoding;
  3067. };
  3068. PUGI__FN void text_output_escaped(xml_buffered_writer& writer, const char_t* s, chartypex_t type)
  3069. {
  3070. while (*s)
  3071. {
  3072. const char_t* prev = s;
  3073. // While *s is a usual symbol
  3074. PUGI__SCANWHILE_UNROLL(!PUGI__IS_CHARTYPEX(ss, type));
  3075. writer.write_buffer(prev, static_cast<size_t>(s - prev));
  3076. switch (*s)
  3077. {
  3078. case 0: break;
  3079. case '&':
  3080. writer.write('&', 'a', 'm', 'p', ';');
  3081. ++s;
  3082. break;
  3083. case '<':
  3084. writer.write('&', 'l', 't', ';');
  3085. ++s;
  3086. break;
  3087. case '>':
  3088. writer.write('&', 'g', 't', ';');
  3089. ++s;
  3090. break;
  3091. case '"':
  3092. writer.write('&', 'q', 'u', 'o', 't', ';');
  3093. ++s;
  3094. break;
  3095. default: // s is not a usual symbol
  3096. {
  3097. unsigned int ch = static_cast<unsigned int>(*s++);
  3098. assert(ch < 32);
  3099. writer.write('&', '#', static_cast<char_t>((ch / 10) + '0'), static_cast<char_t>((ch % 10) + '0'), ';');
  3100. }
  3101. }
  3102. }
  3103. }
  3104. PUGI__FN void text_output(xml_buffered_writer& writer, const char_t* s, chartypex_t type, unsigned int flags)
  3105. {
  3106. if (flags & format_no_escapes)
  3107. writer.write_string(s);
  3108. else
  3109. text_output_escaped(writer, s, type);
  3110. }
  3111. PUGI__FN void text_output_cdata(xml_buffered_writer& writer, const char_t* s)
  3112. {
  3113. do
  3114. {
  3115. writer.write('<', '!', '[', 'C', 'D');
  3116. writer.write('A', 'T', 'A', '[');
  3117. const char_t* prev = s;
  3118. // look for ]]> sequence - we can't output it as is since it terminates CDATA
  3119. while (*s && !(s[0] == ']' && s[1] == ']' && s[2] == '>')) ++s;
  3120. // skip ]] if we stopped at ]]>, > will go to the next CDATA section
  3121. if (*s) s += 2;
  3122. writer.write_buffer(prev, static_cast<size_t>(s - prev));
  3123. writer.write(']', ']', '>');
  3124. }
  3125. while (*s);
  3126. }
  3127. PUGI__FN void text_output_indent(xml_buffered_writer& writer, const char_t* indent, size_t indent_length, unsigned int depth)
  3128. {
  3129. switch (indent_length)
  3130. {
  3131. case 1:
  3132. {
  3133. for (unsigned int i = 0; i < depth; ++i)
  3134. writer.write(indent[0]);
  3135. break;
  3136. }
  3137. case 2:
  3138. {
  3139. for (unsigned int i = 0; i < depth; ++i)
  3140. writer.write(indent[0], indent[1]);
  3141. break;
  3142. }
  3143. case 3:
  3144. {
  3145. for (unsigned int i = 0; i < depth; ++i)
  3146. writer.write(indent[0], indent[1], indent[2]);
  3147. break;
  3148. }
  3149. case 4:
  3150. {
  3151. for (unsigned int i = 0; i < depth; ++i)
  3152. writer.write(indent[0], indent[1], indent[2], indent[3]);
  3153. break;
  3154. }
  3155. default:
  3156. {
  3157. for (unsigned int i = 0; i < depth; ++i)
  3158. writer.write_buffer(indent, indent_length);
  3159. }
  3160. }
  3161. }
  3162. PUGI__FN void node_output_comment(xml_buffered_writer& writer, const char_t* s)
  3163. {
  3164. writer.write('<', '!', '-', '-');
  3165. while (*s)
  3166. {
  3167. const char_t* prev = s;
  3168. // look for -\0 or -- sequence - we can't output it since -- is illegal in comment body
  3169. while (*s && !(s[0] == '-' && (s[1] == '-' || s[1] == 0))) ++s;
  3170. writer.write_buffer(prev, static_cast<size_t>(s - prev));
  3171. if (*s)
  3172. {
  3173. assert(*s == '-');
  3174. writer.write('-', ' ');
  3175. ++s;
  3176. }
  3177. }
  3178. writer.write('-', '-', '>');
  3179. }
  3180. PUGI__FN void node_output_pi_value(xml_buffered_writer& writer, const char_t* s)
  3181. {
  3182. while (*s)
  3183. {
  3184. const char_t* prev = s;
  3185. // look for ?> sequence - we can't output it since ?> terminates PI
  3186. while (*s && !(s[0] == '?' && s[1] == '>')) ++s;
  3187. writer.write_buffer(prev, static_cast<size_t>(s - prev));
  3188. if (*s)
  3189. {
  3190. assert(s[0] == '?' && s[1] == '>');
  3191. writer.write('?', ' ', '>');
  3192. s += 2;
  3193. }
  3194. }
  3195. }
  3196. PUGI__FN void node_output_attributes(xml_buffered_writer& writer, xml_node_struct* node, const char_t* indent, size_t indent_length, unsigned int flags, unsigned int depth)
  3197. {
  3198. const char_t* default_name = PUGIXML_TEXT(":anonymous");
  3199. for (xml_attribute_struct* a = node->first_attribute; a; a = a->next_attribute)
  3200. {
  3201. if ((flags & (format_indent_attributes | format_raw)) == format_indent_attributes)
  3202. {
  3203. writer.write('\n');
  3204. text_output_indent(writer, indent, indent_length, depth + 1);
  3205. }
  3206. else
  3207. {
  3208. writer.write(' ');
  3209. }
  3210. writer.write_string(a->name ? a->name + 0 : default_name);
  3211. writer.write('=', '"');
  3212. if (a->value)
  3213. text_output(writer, a->value, ctx_special_attr, flags);
  3214. writer.write('"');
  3215. }
  3216. }
  3217. PUGI__FN bool node_output_start(xml_buffered_writer& writer, xml_node_struct* node, const char_t* indent, size_t indent_length, unsigned int flags, unsigned int depth)
  3218. {
  3219. const char_t* default_name = PUGIXML_TEXT(":anonymous");
  3220. const char_t* name = node->name ? node->name + 0 : default_name;
  3221. writer.write('<');
  3222. writer.write_string(name);
  3223. if (node->first_attribute)
  3224. node_output_attributes(writer, node, indent, indent_length, flags, depth);
  3225. if (!node->first_child)
  3226. {
  3227. writer.write(' ', '/', '>');
  3228. return false;
  3229. }
  3230. else
  3231. {
  3232. writer.write('>');
  3233. return true;
  3234. }
  3235. }
  3236. PUGI__FN void node_output_end(xml_buffered_writer& writer, xml_node_struct* node)
  3237. {
  3238. const char_t* default_name = PUGIXML_TEXT(":anonymous");
  3239. const char_t* name = node->name ? node->name + 0 : default_name;
  3240. writer.write('<', '/');
  3241. writer.write_string(name);
  3242. writer.write('>');
  3243. }
  3244. PUGI__FN void node_output_simple(xml_buffered_writer& writer, xml_node_struct* node, unsigned int flags)
  3245. {
  3246. const char_t* default_name = PUGIXML_TEXT(":anonymous");
  3247. switch (PUGI__NODETYPE(node))
  3248. {
  3249. case node_pcdata:
  3250. text_output(writer, node->value ? node->value + 0 : PUGIXML_TEXT(""), ctx_special_pcdata, flags);
  3251. break;
  3252. case node_cdata:
  3253. text_output_cdata(writer, node->value ? node->value + 0 : PUGIXML_TEXT(""));
  3254. break;
  3255. case node_comment:
  3256. node_output_comment(writer, node->value ? node->value + 0 : PUGIXML_TEXT(""));
  3257. break;
  3258. case node_pi:
  3259. writer.write('<', '?');
  3260. writer.write_string(node->name ? node->name + 0 : default_name);
  3261. if (node->value)
  3262. {
  3263. writer.write(' ');
  3264. node_output_pi_value(writer, node->value);
  3265. }
  3266. writer.write('?', '>');
  3267. break;
  3268. case node_declaration:
  3269. writer.write('<', '?');
  3270. writer.write_string(node->name ? node->name + 0 : default_name);
  3271. node_output_attributes(writer, node, PUGIXML_TEXT(""), 0, flags | format_raw, 0);
  3272. writer.write('?', '>');
  3273. break;
  3274. case node_doctype:
  3275. writer.write('<', '!', 'D', 'O', 'C');
  3276. writer.write('T', 'Y', 'P', 'E');
  3277. if (node->value)
  3278. {
  3279. writer.write(' ');
  3280. writer.write_string(node->value);
  3281. }
  3282. writer.write('>');
  3283. break;
  3284. default:
  3285. assert(!"Invalid node type");
  3286. }
  3287. }
  3288. enum indent_flags_t
  3289. {
  3290. indent_newline = 1,
  3291. indent_indent = 2
  3292. };
  3293. PUGI__FN void node_output(xml_buffered_writer& writer, xml_node_struct* root, const char_t* indent, unsigned int flags, unsigned int depth)
  3294. {
  3295. size_t indent_length = ((flags & (format_indent | format_indent_attributes)) && (flags & format_raw) == 0) ? strlength(indent) : 0;
  3296. unsigned int indent_flags = indent_indent;
  3297. xml_node_struct* node = root;
  3298. do
  3299. {
  3300. assert(node);
  3301. // begin writing current node
  3302. if (PUGI__NODETYPE(node) == node_pcdata || PUGI__NODETYPE(node) == node_cdata)
  3303. {
  3304. node_output_simple(writer, node, flags);
  3305. indent_flags = 0;
  3306. }
  3307. else
  3308. {
  3309. if ((indent_flags & indent_newline) && (flags & format_raw) == 0)
  3310. writer.write('\n');
  3311. if ((indent_flags & indent_indent) && indent_length)
  3312. text_output_indent(writer, indent, indent_length, depth);
  3313. if (PUGI__NODETYPE(node) == node_element)
  3314. {
  3315. indent_flags = indent_newline | indent_indent;
  3316. if (node_output_start(writer, node, indent, indent_length, flags, depth))
  3317. {
  3318. node = node->first_child;
  3319. depth++;
  3320. continue;
  3321. }
  3322. }
  3323. else if (PUGI__NODETYPE(node) == node_document)
  3324. {
  3325. indent_flags = indent_indent;
  3326. if (node->first_child)
  3327. {
  3328. node = node->first_child;
  3329. continue;
  3330. }
  3331. }
  3332. else
  3333. {
  3334. node_output_simple(writer, node, flags);
  3335. indent_flags = indent_newline | indent_indent;
  3336. }
  3337. }
  3338. // continue to the next node
  3339. while (node != root)
  3340. {
  3341. if (node->next_sibling)
  3342. {
  3343. node = node->next_sibling;
  3344. break;
  3345. }
  3346. node = node->parent;
  3347. // write closing node
  3348. if (PUGI__NODETYPE(node) == node_element)
  3349. {
  3350. depth--;
  3351. if ((indent_flags & indent_newline) && (flags & format_raw) == 0)
  3352. writer.write('\n');
  3353. if ((indent_flags & indent_indent) && indent_length)
  3354. text_output_indent(writer, indent, indent_length, depth);
  3355. node_output_end(writer, node);
  3356. indent_flags = indent_newline | indent_indent;
  3357. }
  3358. }
  3359. }
  3360. while (node != root);
  3361. if ((indent_flags & indent_newline) && (flags & format_raw) == 0)
  3362. writer.write('\n');
  3363. }
  3364. PUGI__FN bool has_declaration(xml_node_struct* node)
  3365. {
  3366. for (xml_node_struct* child = node->first_child; child; child = child->next_sibling)
  3367. {
  3368. xml_node_type type = PUGI__NODETYPE(child);
  3369. if (type == node_declaration) return true;
  3370. if (type == node_element) return false;
  3371. }
  3372. return false;
  3373. }
  3374. PUGI__FN bool is_attribute_of(xml_attribute_struct* attr, xml_node_struct* node)
  3375. {
  3376. for (xml_attribute_struct* a = node->first_attribute; a; a = a->next_attribute)
  3377. if (a == attr)
  3378. return true;
  3379. return false;
  3380. }
  3381. PUGI__FN bool allow_insert_attribute(xml_node_type parent)
  3382. {
  3383. return parent == node_element || parent == node_declaration;
  3384. }
  3385. PUGI__FN bool allow_insert_child(xml_node_type parent, xml_node_type child)
  3386. {
  3387. if (parent != node_document && parent != node_element) return false;
  3388. if (child == node_document || child == node_null) return false;
  3389. if (parent != node_document && (child == node_declaration || child == node_doctype)) return false;
  3390. return true;
  3391. }
  3392. PUGI__FN bool allow_move(xml_node parent, xml_node child)
  3393. {
  3394. // check that child can be a child of parent
  3395. if (!allow_insert_child(parent.type(), child.type()))
  3396. return false;
  3397. // check that node is not moved between documents
  3398. if (parent.root() != child.root())
  3399. return false;
  3400. // check that new parent is not in the child subtree
  3401. xml_node cur = parent;
  3402. while (cur)
  3403. {
  3404. if (cur == child)
  3405. return false;
  3406. cur = cur.parent();
  3407. }
  3408. return true;
  3409. }
  3410. template <typename String, typename Header>
  3411. PUGI__FN void node_copy_string(String& dest, Header& header, uintptr_t header_mask, char_t* source, Header& source_header, xml_allocator* alloc)
  3412. {
  3413. assert(!dest && (header & header_mask) == 0);
  3414. if (source)
  3415. {
  3416. if (alloc && (source_header & header_mask) == 0)
  3417. {
  3418. dest = source;
  3419. // since strcpy_insitu can reuse document buffer memory we need to mark both source and dest as shared
  3420. header |= xml_memory_page_contents_shared_mask;
  3421. source_header |= xml_memory_page_contents_shared_mask;
  3422. }
  3423. else
  3424. strcpy_insitu(dest, header, header_mask, source, strlength(source));
  3425. }
  3426. }
  3427. PUGI__FN void node_copy_contents(xml_node_struct* dn, xml_node_struct* sn, xml_allocator* shared_alloc)
  3428. {
  3429. node_copy_string(dn->name, dn->header, xml_memory_page_name_allocated_mask, sn->name, sn->header, shared_alloc);
  3430. node_copy_string(dn->value, dn->header, xml_memory_page_value_allocated_mask, sn->value, sn->header, shared_alloc);
  3431. for (xml_attribute_struct* sa = sn->first_attribute; sa; sa = sa->next_attribute)
  3432. {
  3433. xml_attribute_struct* da = append_new_attribute(dn, get_allocator(dn));
  3434. if (da)
  3435. {
  3436. node_copy_string(da->name, da->header, xml_memory_page_name_allocated_mask, sa->name, sa->header, shared_alloc);
  3437. node_copy_string(da->value, da->header, xml_memory_page_value_allocated_mask, sa->value, sa->header, shared_alloc);
  3438. }
  3439. }
  3440. }
  3441. PUGI__FN void node_copy_tree(xml_node_struct* dn, xml_node_struct* sn)
  3442. {
  3443. xml_allocator& alloc = get_allocator(dn);
  3444. xml_allocator* shared_alloc = (&alloc == &get_allocator(sn)) ? &alloc : 0;
  3445. node_copy_contents(dn, sn, shared_alloc);
  3446. xml_node_struct* dit = dn;
  3447. xml_node_struct* sit = sn->first_child;
  3448. while (sit && sit != sn)
  3449. {
  3450. if (sit != dn)
  3451. {
  3452. xml_node_struct* copy = append_new_node(dit, alloc, PUGI__NODETYPE(sit));
  3453. if (copy)
  3454. {
  3455. node_copy_contents(copy, sit, shared_alloc);
  3456. if (sit->first_child)
  3457. {
  3458. dit = copy;
  3459. sit = sit->first_child;
  3460. continue;
  3461. }
  3462. }
  3463. }
  3464. // continue to the next node
  3465. do
  3466. {
  3467. if (sit->next_sibling)
  3468. {
  3469. sit = sit->next_sibling;
  3470. break;
  3471. }
  3472. sit = sit->parent;
  3473. dit = dit->parent;
  3474. }
  3475. while (sit != sn);
  3476. }
  3477. }
  3478. PUGI__FN void node_copy_attribute(xml_attribute_struct* da, xml_attribute_struct* sa)
  3479. {
  3480. xml_allocator& alloc = get_allocator(da);
  3481. xml_allocator* shared_alloc = (&alloc == &get_allocator(sa)) ? &alloc : 0;
  3482. node_copy_string(da->name, da->header, xml_memory_page_name_allocated_mask, sa->name, sa->header, shared_alloc);
  3483. node_copy_string(da->value, da->header, xml_memory_page_value_allocated_mask, sa->value, sa->header, shared_alloc);
  3484. }
  3485. inline bool is_text_node(xml_node_struct* node)
  3486. {
  3487. xml_node_type type = PUGI__NODETYPE(node);
  3488. return type == node_pcdata || type == node_cdata;
  3489. }
  3490. // get value with conversion functions
  3491. template <typename U> U string_to_integer(const char_t* value, U minneg, U maxpos)
  3492. {
  3493. U result = 0;
  3494. const char_t* s = value;
  3495. while (PUGI__IS_CHARTYPE(*s, ct_space))
  3496. s++;
  3497. bool negative = (*s == '-');
  3498. s += (*s == '+' || *s == '-');
  3499. bool overflow = false;
  3500. if (s[0] == '0' && (s[1] | ' ') == 'x')
  3501. {
  3502. s += 2;
  3503. // since overflow detection relies on length of the sequence skip leading zeros
  3504. while (*s == '0')
  3505. s++;
  3506. const char_t* start = s;
  3507. for (;;)
  3508. {
  3509. if (static_cast<unsigned>(*s - '0') < 10)
  3510. result = result * 16 + (*s - '0');
  3511. else if (static_cast<unsigned>((*s | ' ') - 'a') < 6)
  3512. result = result * 16 + ((*s | ' ') - 'a' + 10);
  3513. else
  3514. break;
  3515. s++;
  3516. }
  3517. size_t digits = static_cast<size_t>(s - start);
  3518. overflow = digits > sizeof(U) * 2;
  3519. }
  3520. else
  3521. {
  3522. // since overflow detection relies on length of the sequence skip leading zeros
  3523. while (*s == '0')
  3524. s++;
  3525. const char_t* start = s;
  3526. for (;;)
  3527. {
  3528. if (static_cast<unsigned>(*s - '0') < 10)
  3529. result = result * 10 + (*s - '0');
  3530. else
  3531. break;
  3532. s++;
  3533. }
  3534. size_t digits = static_cast<size_t>(s - start);
  3535. PUGI__STATIC_ASSERT(sizeof(U) == 8 || sizeof(U) == 4 || sizeof(U) == 2);
  3536. const size_t max_digits10 = sizeof(U) == 8 ? 20 : sizeof(U) == 4 ? 10 : 5;
  3537. const char_t max_lead = sizeof(U) == 8 ? '1' : sizeof(U) == 4 ? '4' : '6';
  3538. const size_t high_bit = sizeof(U) * 8 - 1;
  3539. overflow = digits >= max_digits10 && !(digits == max_digits10 && (*start < max_lead || (*start == max_lead && result >> high_bit)));
  3540. }
  3541. if (negative)
  3542. return (overflow || result > minneg) ? 0 - minneg : 0 - result;
  3543. else
  3544. return (overflow || result > maxpos) ? maxpos : result;
  3545. }
  3546. PUGI__FN int get_value_int(const char_t* value)
  3547. {
  3548. return string_to_integer<unsigned int>(value, 0 - static_cast<unsigned int>(INT_MIN), INT_MAX);
  3549. }
  3550. PUGI__FN unsigned int get_value_uint(const char_t* value)
  3551. {
  3552. return string_to_integer<unsigned int>(value, 0, UINT_MAX);
  3553. }
  3554. PUGI__FN double get_value_double(const char_t* value)
  3555. {
  3556. #ifdef PUGIXML_WCHAR_MODE
  3557. return wcstod(value, 0);
  3558. #else
  3559. return strtod(value, 0);
  3560. #endif
  3561. }
  3562. PUGI__FN float get_value_float(const char_t* value)
  3563. {
  3564. #ifdef PUGIXML_WCHAR_MODE
  3565. return static_cast<float>(wcstod(value, 0));
  3566. #else
  3567. return static_cast<float>(strtod(value, 0));
  3568. #endif
  3569. }
  3570. PUGI__FN bool get_value_bool(const char_t* value)
  3571. {
  3572. // only look at first char
  3573. char_t first = *value;
  3574. // 1*, t* (true), T* (True), y* (yes), Y* (YES)
  3575. return (first == '1' || first == 't' || first == 'T' || first == 'y' || first == 'Y');
  3576. }
  3577. #ifdef PUGIXML_HAS_LONG_LONG
  3578. PUGI__FN long long get_value_llong(const char_t* value)
  3579. {
  3580. return string_to_integer<unsigned long long>(value, 0 - static_cast<unsigned long long>(LLONG_MIN), LLONG_MAX);
  3581. }
  3582. PUGI__FN unsigned long long get_value_ullong(const char_t* value)
  3583. {
  3584. return string_to_integer<unsigned long long>(value, 0, ULLONG_MAX);
  3585. }
  3586. #endif
  3587. template <typename U>
  3588. PUGI__FN char_t* integer_to_string(char_t* begin, char_t* end, U value, bool negative)
  3589. {
  3590. char_t* result = end - 1;
  3591. U rest = negative ? 0 - value : value;
  3592. do
  3593. {
  3594. *result-- = static_cast<char_t>('0' + (rest % 10));
  3595. rest /= 10;
  3596. }
  3597. while (rest);
  3598. assert(result >= begin);
  3599. (void)begin;
  3600. *result = '-';
  3601. return result + !negative;
  3602. }
  3603. // set value with conversion functions
  3604. template <typename String, typename Header>
  3605. PUGI__FN bool set_value_ascii(String& dest, Header& header, uintptr_t header_mask, char* buf)
  3606. {
  3607. #ifdef PUGIXML_WCHAR_MODE
  3608. char_t wbuf[128];
  3609. assert(strlen(buf) < sizeof(wbuf) / sizeof(wbuf[0]));
  3610. size_t offset = 0;
  3611. for (; buf[offset]; ++offset) wbuf[offset] = buf[offset];
  3612. return strcpy_insitu(dest, header, header_mask, wbuf, offset);
  3613. #else
  3614. return strcpy_insitu(dest, header, header_mask, buf, strlen(buf));
  3615. #endif
  3616. }
  3617. template <typename String, typename Header>
  3618. PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, int value)
  3619. {
  3620. char_t buf[64];
  3621. char_t* end = buf + sizeof(buf) / sizeof(buf[0]);
  3622. char_t* begin = integer_to_string<unsigned int>(buf, end, value, value < 0);
  3623. return strcpy_insitu(dest, header, header_mask, begin, end - begin);
  3624. }
  3625. template <typename String, typename Header>
  3626. PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, unsigned int value)
  3627. {
  3628. char_t buf[64];
  3629. char_t* end = buf + sizeof(buf) / sizeof(buf[0]);
  3630. char_t* begin = integer_to_string<unsigned int>(buf, end, value, false);
  3631. return strcpy_insitu(dest, header, header_mask, begin, end - begin);
  3632. }
  3633. template <typename String, typename Header>
  3634. PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, float value)
  3635. {
  3636. char buf[128];
  3637. sprintf(buf, "%.9g", value);
  3638. return set_value_ascii(dest, header, header_mask, buf);
  3639. }
  3640. template <typename String, typename Header>
  3641. PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, double value)
  3642. {
  3643. char buf[128];
  3644. sprintf(buf, "%.17g", value);
  3645. return set_value_ascii(dest, header, header_mask, buf);
  3646. }
  3647. template <typename String, typename Header>
  3648. PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, bool value)
  3649. {
  3650. return strcpy_insitu(dest, header, header_mask, value ? PUGIXML_TEXT("true") : PUGIXML_TEXT("false"), value ? 4 : 5);
  3651. }
  3652. #ifdef PUGIXML_HAS_LONG_LONG
  3653. template <typename String, typename Header>
  3654. PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, long long value)
  3655. {
  3656. char_t buf[64];
  3657. char_t* end = buf + sizeof(buf) / sizeof(buf[0]);
  3658. char_t* begin = integer_to_string<unsigned long long>(buf, end, value, value < 0);
  3659. return strcpy_insitu(dest, header, header_mask, begin, end - begin);
  3660. }
  3661. template <typename String, typename Header>
  3662. PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, unsigned long long value)
  3663. {
  3664. char_t buf[64];
  3665. char_t* end = buf + sizeof(buf) / sizeof(buf[0]);
  3666. char_t* begin = integer_to_string<unsigned long long>(buf, end, value, false);
  3667. return strcpy_insitu(dest, header, header_mask, begin, end - begin);
  3668. }
  3669. #endif
  3670. PUGI__FN xml_parse_result load_buffer_impl(xml_document_struct* doc, xml_node_struct* root, void* contents, size_t size, unsigned int options, xml_encoding encoding, bool is_mutable, bool own, char_t** out_buffer)
  3671. {
  3672. // check input buffer
  3673. if (!contents && size) return make_parse_result(status_io_error);
  3674. // get actual encoding
  3675. xml_encoding buffer_encoding = impl::get_buffer_encoding(encoding, contents, size);
  3676. // get private buffer
  3677. char_t* buffer = 0;
  3678. size_t length = 0;
  3679. if (!impl::convert_buffer(buffer, length, buffer_encoding, contents, size, is_mutable)) return impl::make_parse_result(status_out_of_memory);
  3680. // delete original buffer if we performed a conversion
  3681. if (own && buffer != contents && contents) impl::xml_memory::deallocate(contents);
  3682. // grab onto buffer if it's our buffer, user is responsible for deallocating contents himself
  3683. if (own || buffer != contents) *out_buffer = buffer;
  3684. // store buffer for offset_debug
  3685. doc->buffer = buffer;
  3686. // parse
  3687. xml_parse_result res = impl::xml_parser::parse(buffer, length, doc, root, options);
  3688. // remember encoding
  3689. res.encoding = buffer_encoding;
  3690. return res;
  3691. }
  3692. // we need to get length of entire file to load it in memory; the only (relatively) sane way to do it is via seek/tell trick
  3693. PUGI__FN xml_parse_status get_file_size(FILE* file, size_t& out_result)
  3694. {
  3695. #if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400 && !defined(_WIN32_WCE)
  3696. // there are 64-bit versions of fseek/ftell, let's use them
  3697. typedef __int64 length_type;
  3698. _fseeki64(file, 0, SEEK_END);
  3699. length_type length = _ftelli64(file);
  3700. _fseeki64(file, 0, SEEK_SET);
  3701. #elif defined(__MINGW32__) && !defined(__NO_MINGW_LFS) && (!defined(__STRICT_ANSI__) || defined(__MINGW64_VERSION_MAJOR))
  3702. // there are 64-bit versions of fseek/ftell, let's use them
  3703. typedef off64_t length_type;
  3704. fseeko64(file, 0, SEEK_END);
  3705. length_type length = ftello64(file);
  3706. fseeko64(file, 0, SEEK_SET);
  3707. #else
  3708. // if this is a 32-bit OS, long is enough; if this is a unix system, long is 64-bit, which is enough; otherwise we can't do anything anyway.
  3709. typedef long length_type;
  3710. fseek(file, 0, SEEK_END);
  3711. length_type length = ftell(file);
  3712. fseek(file, 0, SEEK_SET);
  3713. #endif
  3714. // check for I/O errors
  3715. if (length < 0) return status_io_error;
  3716. // check for overflow
  3717. size_t result = static_cast<size_t>(length);
  3718. if (static_cast<length_type>(result) != length) return status_out_of_memory;
  3719. // finalize
  3720. out_result = result;
  3721. return status_ok;
  3722. }
  3723. // This function assumes that buffer has extra sizeof(char_t) writable bytes after size
  3724. PUGI__FN size_t zero_terminate_buffer(void* buffer, size_t size, xml_encoding encoding)
  3725. {
  3726. // We only need to zero-terminate if encoding conversion does not do it for us
  3727. #ifdef PUGIXML_WCHAR_MODE
  3728. xml_encoding wchar_encoding = get_wchar_encoding();
  3729. if (encoding == wchar_encoding || need_endian_swap_utf(encoding, wchar_encoding))
  3730. {
  3731. size_t length = size / sizeof(char_t);
  3732. static_cast<char_t*>(buffer)[length] = 0;
  3733. return (length + 1) * sizeof(char_t);
  3734. }
  3735. #else
  3736. if (encoding == encoding_utf8)
  3737. {
  3738. static_cast<char*>(buffer)[size] = 0;
  3739. return size + 1;
  3740. }
  3741. #endif
  3742. return size;
  3743. }
  3744. PUGI__FN xml_parse_result load_file_impl(xml_document_struct* doc, FILE* file, unsigned int options, xml_encoding encoding, char_t** out_buffer)
  3745. {
  3746. if (!file) return make_parse_result(status_file_not_found);
  3747. // get file size (can result in I/O errors)
  3748. size_t size = 0;
  3749. xml_parse_status size_status = get_file_size(file, size);
  3750. if (size_status != status_ok) return make_parse_result(size_status);
  3751. size_t max_suffix_size = sizeof(char_t);
  3752. // allocate buffer for the whole file
  3753. char* contents = static_cast<char*>(xml_memory::allocate(size + max_suffix_size));
  3754. if (!contents) return make_parse_result(status_out_of_memory);
  3755. // read file in memory
  3756. size_t read_size = fread(contents, 1, size, file);
  3757. if (read_size != size)
  3758. {
  3759. xml_memory::deallocate(contents);
  3760. return make_parse_result(status_io_error);
  3761. }
  3762. xml_encoding real_encoding = get_buffer_encoding(encoding, contents, size);
  3763. return load_buffer_impl(doc, doc, contents, zero_terminate_buffer(contents, size, real_encoding), options, real_encoding, true, true, out_buffer);
  3764. }
  3765. #ifndef PUGIXML_NO_STL
  3766. template <typename T> struct xml_stream_chunk
  3767. {
  3768. static xml_stream_chunk* create()
  3769. {
  3770. void* memory = xml_memory::allocate(sizeof(xml_stream_chunk));
  3771. if (!memory) return 0;
  3772. return new (memory) xml_stream_chunk();
  3773. }
  3774. static void destroy(xml_stream_chunk* chunk)
  3775. {
  3776. // free chunk chain
  3777. while (chunk)
  3778. {
  3779. xml_stream_chunk* next_ = chunk->next;
  3780. xml_memory::deallocate(chunk);
  3781. chunk = next_;
  3782. }
  3783. }
  3784. xml_stream_chunk(): next(0), size(0)
  3785. {
  3786. }
  3787. xml_stream_chunk* next;
  3788. size_t size;
  3789. T data[xml_memory_page_size / sizeof(T)];
  3790. };
  3791. template <typename T> PUGI__FN xml_parse_status load_stream_data_noseek(std::basic_istream<T>& stream, void** out_buffer, size_t* out_size)
  3792. {
  3793. auto_deleter<xml_stream_chunk<T> > chunks(0, xml_stream_chunk<T>::destroy);
  3794. // read file to a chunk list
  3795. size_t total = 0;
  3796. xml_stream_chunk<T>* last = 0;
  3797. while (!stream.eof())
  3798. {
  3799. // allocate new chunk
  3800. xml_stream_chunk<T>* chunk = xml_stream_chunk<T>::create();
  3801. if (!chunk) return status_out_of_memory;
  3802. // append chunk to list
  3803. if (last) last = last->next = chunk;
  3804. else chunks.data = last = chunk;
  3805. // read data to chunk
  3806. stream.read(chunk->data, static_cast<std::streamsize>(sizeof(chunk->data) / sizeof(T)));
  3807. chunk->size = static_cast<size_t>(stream.gcount()) * sizeof(T);
  3808. // read may set failbit | eofbit in case gcount() is less than read length, so check for other I/O errors
  3809. if (stream.bad() || (!stream.eof() && stream.fail())) return status_io_error;
  3810. // guard against huge files (chunk size is small enough to make this overflow check work)
  3811. if (total + chunk->size < total) return status_out_of_memory;
  3812. total += chunk->size;
  3813. }
  3814. size_t max_suffix_size = sizeof(char_t);
  3815. // copy chunk list to a contiguous buffer
  3816. char* buffer = static_cast<char*>(xml_memory::allocate(total + max_suffix_size));
  3817. if (!buffer) return status_out_of_memory;
  3818. char* write = buffer;
  3819. for (xml_stream_chunk<T>* chunk = chunks.data; chunk; chunk = chunk->next)
  3820. {
  3821. assert(write + chunk->size <= buffer + total);
  3822. memcpy(write, chunk->data, chunk->size);
  3823. write += chunk->size;
  3824. }
  3825. assert(write == buffer + total);
  3826. // return buffer
  3827. *out_buffer = buffer;
  3828. *out_size = total;
  3829. return status_ok;
  3830. }
  3831. template <typename T> PUGI__FN xml_parse_status load_stream_data_seek(std::basic_istream<T>& stream, void** out_buffer, size_t* out_size)
  3832. {
  3833. // get length of remaining data in stream
  3834. typename std::basic_istream<T>::pos_type pos = stream.tellg();
  3835. stream.seekg(0, std::ios::end);
  3836. std::streamoff length = stream.tellg() - pos;
  3837. stream.seekg(pos);
  3838. if (stream.fail() || pos < 0) return status_io_error;
  3839. // guard against huge files
  3840. size_t read_length = static_cast<size_t>(length);
  3841. if (static_cast<std::streamsize>(read_length) != length || length < 0) return status_out_of_memory;
  3842. size_t max_suffix_size = sizeof(char_t);
  3843. // read stream data into memory (guard against stream exceptions with buffer holder)
  3844. auto_deleter<void> buffer(xml_memory::allocate(read_length * sizeof(T) + max_suffix_size), xml_memory::deallocate);
  3845. if (!buffer.data) return status_out_of_memory;
  3846. stream.read(static_cast<T*>(buffer.data), static_cast<std::streamsize>(read_length));
  3847. // read may set failbit | eofbit in case gcount() is less than read_length (i.e. line ending conversion), so check for other I/O errors
  3848. if (stream.bad() || (!stream.eof() && stream.fail())) return status_io_error;
  3849. // return buffer
  3850. size_t actual_length = static_cast<size_t>(stream.gcount());
  3851. assert(actual_length <= read_length);
  3852. *out_buffer = buffer.release();
  3853. *out_size = actual_length * sizeof(T);
  3854. return status_ok;
  3855. }
  3856. template <typename T> PUGI__FN xml_parse_result load_stream_impl(xml_document_struct* doc, std::basic_istream<T>& stream, unsigned int options, xml_encoding encoding, char_t** out_buffer)
  3857. {
  3858. void* buffer = 0;
  3859. size_t size = 0;
  3860. xml_parse_status status = status_ok;
  3861. // if stream has an error bit set, bail out (otherwise tellg() can fail and we'll clear error bits)
  3862. if (stream.fail()) return make_parse_result(status_io_error);
  3863. // load stream to memory (using seek-based implementation if possible, since it's faster and takes less memory)
  3864. if (stream.tellg() < 0)
  3865. {
  3866. stream.clear(); // clear error flags that could be set by a failing tellg
  3867. status = load_stream_data_noseek(stream, &buffer, &size);
  3868. }
  3869. else
  3870. status = load_stream_data_seek(stream, &buffer, &size);
  3871. if (status != status_ok) return make_parse_result(status);
  3872. xml_encoding real_encoding = get_buffer_encoding(encoding, buffer, size);
  3873. return load_buffer_impl(doc, doc, buffer, zero_terminate_buffer(buffer, size, real_encoding), options, real_encoding, true, true, out_buffer);
  3874. }
  3875. #endif
  3876. #if defined(PUGI__MSVC_CRT_VERSION) || defined(__BORLANDC__) || (defined(__MINGW32__) && (!defined(__STRICT_ANSI__) || defined(__MINGW64_VERSION_MAJOR)))
  3877. PUGI__FN FILE* open_file_wide(const wchar_t* path, const wchar_t* mode)
  3878. {
  3879. return _wfopen(path, mode);
  3880. }
  3881. #else
  3882. PUGI__FN char* convert_path_heap(const wchar_t* str)
  3883. {
  3884. assert(str);
  3885. // first pass: get length in utf8 characters
  3886. size_t length = strlength_wide(str);
  3887. size_t size = as_utf8_begin(str, length);
  3888. // allocate resulting string
  3889. char* result = static_cast<char*>(xml_memory::allocate(size + 1));
  3890. if (!result) return 0;
  3891. // second pass: convert to utf8
  3892. as_utf8_end(result, size, str, length);
  3893. // zero-terminate
  3894. result[size] = 0;
  3895. return result;
  3896. }
  3897. PUGI__FN FILE* open_file_wide(const wchar_t* path, const wchar_t* mode)
  3898. {
  3899. // there is no standard function to open wide paths, so our best bet is to try utf8 path
  3900. char* path_utf8 = convert_path_heap(path);
  3901. if (!path_utf8) return 0;
  3902. // convert mode to ASCII (we mirror _wfopen interface)
  3903. char mode_ascii[4] = {0};
  3904. for (size_t i = 0; mode[i]; ++i) mode_ascii[i] = static_cast<char>(mode[i]);
  3905. // try to open the utf8 path
  3906. FILE* result = fopen(path_utf8, mode_ascii);
  3907. // free dummy buffer
  3908. xml_memory::deallocate(path_utf8);
  3909. return result;
  3910. }
  3911. #endif
  3912. PUGI__FN bool save_file_impl(const xml_document& doc, FILE* file, const char_t* indent, unsigned int flags, xml_encoding encoding)
  3913. {
  3914. if (!file) return false;
  3915. xml_writer_file writer(file);
  3916. doc.save(writer, indent, flags, encoding);
  3917. return ferror(file) == 0;
  3918. }
  3919. struct name_null_sentry
  3920. {
  3921. xml_node_struct* node;
  3922. char_t* name;
  3923. name_null_sentry(xml_node_struct* node_): node(node_), name(node_->name)
  3924. {
  3925. node->name = 0;
  3926. }
  3927. ~name_null_sentry()
  3928. {
  3929. node->name = name;
  3930. }
  3931. };
  3932. PUGI__NS_END
  3933. namespace pugi
  3934. {
  3935. PUGI__FN xml_writer_file::xml_writer_file(void* file_): file(file_)
  3936. {
  3937. }
  3938. PUGI__FN void xml_writer_file::write(const void* data, size_t size)
  3939. {
  3940. size_t result = fwrite(data, 1, size, static_cast<FILE*>(file));
  3941. (void)!result; // unfortunately we can't do proper error handling here
  3942. }
  3943. #ifndef PUGIXML_NO_STL
  3944. PUGI__FN xml_writer_stream::xml_writer_stream(std::basic_ostream<char, std::char_traits<char> >& stream): narrow_stream(&stream), wide_stream(0)
  3945. {
  3946. }
  3947. PUGI__FN xml_writer_stream::xml_writer_stream(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream): narrow_stream(0), wide_stream(&stream)
  3948. {
  3949. }
  3950. PUGI__FN void xml_writer_stream::write(const void* data, size_t size)
  3951. {
  3952. if (narrow_stream)
  3953. {
  3954. assert(!wide_stream);
  3955. narrow_stream->write(reinterpret_cast<const char*>(data), static_cast<std::streamsize>(size));
  3956. }
  3957. else
  3958. {
  3959. assert(wide_stream);
  3960. assert(size % sizeof(wchar_t) == 0);
  3961. wide_stream->write(reinterpret_cast<const wchar_t*>(data), static_cast<std::streamsize>(size / sizeof(wchar_t)));
  3962. }
  3963. }
  3964. #endif
  3965. PUGI__FN xml_tree_walker::xml_tree_walker(): _depth(0)
  3966. {
  3967. }
  3968. PUGI__FN xml_tree_walker::~xml_tree_walker()
  3969. {
  3970. }
  3971. PUGI__FN int xml_tree_walker::depth() const
  3972. {
  3973. return _depth;
  3974. }
  3975. PUGI__FN bool xml_tree_walker::begin(xml_node&)
  3976. {
  3977. return true;
  3978. }
  3979. PUGI__FN bool xml_tree_walker::end(xml_node&)
  3980. {
  3981. return true;
  3982. }
  3983. PUGI__FN xml_attribute::xml_attribute(): _attr(0)
  3984. {
  3985. }
  3986. PUGI__FN xml_attribute::xml_attribute(xml_attribute_struct* attr): _attr(attr)
  3987. {
  3988. }
  3989. PUGI__FN static void unspecified_bool_xml_attribute(xml_attribute***)
  3990. {
  3991. }
  3992. PUGI__FN xml_attribute::operator xml_attribute::unspecified_bool_type() const
  3993. {
  3994. return _attr ? unspecified_bool_xml_attribute : 0;
  3995. }
  3996. PUGI__FN bool xml_attribute::operator!() const
  3997. {
  3998. return !_attr;
  3999. }
  4000. PUGI__FN bool xml_attribute::operator==(const xml_attribute& r) const
  4001. {
  4002. return (_attr == r._attr);
  4003. }
  4004. PUGI__FN bool xml_attribute::operator!=(const xml_attribute& r) const
  4005. {
  4006. return (_attr != r._attr);
  4007. }
  4008. PUGI__FN bool xml_attribute::operator<(const xml_attribute& r) const
  4009. {
  4010. return (_attr < r._attr);
  4011. }
  4012. PUGI__FN bool xml_attribute::operator>(const xml_attribute& r) const
  4013. {
  4014. return (_attr > r._attr);
  4015. }
  4016. PUGI__FN bool xml_attribute::operator<=(const xml_attribute& r) const
  4017. {
  4018. return (_attr <= r._attr);
  4019. }
  4020. PUGI__FN bool xml_attribute::operator>=(const xml_attribute& r) const
  4021. {
  4022. return (_attr >= r._attr);
  4023. }
  4024. PUGI__FN xml_attribute xml_attribute::next_attribute() const
  4025. {
  4026. return _attr ? xml_attribute(_attr->next_attribute) : xml_attribute();
  4027. }
  4028. PUGI__FN xml_attribute xml_attribute::previous_attribute() const
  4029. {
  4030. return _attr && _attr->prev_attribute_c->next_attribute ? xml_attribute(_attr->prev_attribute_c) : xml_attribute();
  4031. }
  4032. PUGI__FN const char_t* xml_attribute::as_string(const char_t* def) const
  4033. {
  4034. return (_attr && _attr->value) ? _attr->value + 0 : def;
  4035. }
  4036. PUGI__FN int xml_attribute::as_int(int def) const
  4037. {
  4038. return (_attr && _attr->value) ? impl::get_value_int(_attr->value) : def;
  4039. }
  4040. PUGI__FN unsigned int xml_attribute::as_uint(unsigned int def) const
  4041. {
  4042. return (_attr && _attr->value) ? impl::get_value_uint(_attr->value) : def;
  4043. }
  4044. PUGI__FN double xml_attribute::as_double(double def) const
  4045. {
  4046. return (_attr && _attr->value) ? impl::get_value_double(_attr->value) : def;
  4047. }
  4048. PUGI__FN float xml_attribute::as_float(float def) const
  4049. {
  4050. return (_attr && _attr->value) ? impl::get_value_float(_attr->value) : def;
  4051. }
  4052. PUGI__FN bool xml_attribute::as_bool(bool def) const
  4053. {
  4054. return (_attr && _attr->value) ? impl::get_value_bool(_attr->value) : def;
  4055. }
  4056. #ifdef PUGIXML_HAS_LONG_LONG
  4057. PUGI__FN long long xml_attribute::as_llong(long long def) const
  4058. {
  4059. return (_attr && _attr->value) ? impl::get_value_llong(_attr->value) : def;
  4060. }
  4061. PUGI__FN unsigned long long xml_attribute::as_ullong(unsigned long long def) const
  4062. {
  4063. return (_attr && _attr->value) ? impl::get_value_ullong(_attr->value) : def;
  4064. }
  4065. #endif
  4066. PUGI__FN bool xml_attribute::empty() const
  4067. {
  4068. return !_attr;
  4069. }
  4070. PUGI__FN const char_t* xml_attribute::name() const
  4071. {
  4072. return (_attr && _attr->name) ? _attr->name + 0 : PUGIXML_TEXT("");
  4073. }
  4074. PUGI__FN const char_t* xml_attribute::value() const
  4075. {
  4076. return (_attr && _attr->value) ? _attr->value + 0 : PUGIXML_TEXT("");
  4077. }
  4078. PUGI__FN size_t xml_attribute::hash_value() const
  4079. {
  4080. return static_cast<size_t>(reinterpret_cast<uintptr_t>(_attr) / sizeof(xml_attribute_struct));
  4081. }
  4082. PUGI__FN xml_attribute_struct* xml_attribute::internal_object() const
  4083. {
  4084. return _attr;
  4085. }
  4086. PUGI__FN xml_attribute& xml_attribute::operator=(const char_t* rhs)
  4087. {
  4088. set_value(rhs);
  4089. return *this;
  4090. }
  4091. PUGI__FN xml_attribute& xml_attribute::operator=(int rhs)
  4092. {
  4093. set_value(rhs);
  4094. return *this;
  4095. }
  4096. PUGI__FN xml_attribute& xml_attribute::operator=(unsigned int rhs)
  4097. {
  4098. set_value(rhs);
  4099. return *this;
  4100. }
  4101. PUGI__FN xml_attribute& xml_attribute::operator=(double rhs)
  4102. {
  4103. set_value(rhs);
  4104. return *this;
  4105. }
  4106. PUGI__FN xml_attribute& xml_attribute::operator=(float rhs)
  4107. {
  4108. set_value(rhs);
  4109. return *this;
  4110. }
  4111. PUGI__FN xml_attribute& xml_attribute::operator=(bool rhs)
  4112. {
  4113. set_value(rhs);
  4114. return *this;
  4115. }
  4116. #ifdef PUGIXML_HAS_LONG_LONG
  4117. PUGI__FN xml_attribute& xml_attribute::operator=(long long rhs)
  4118. {
  4119. set_value(rhs);
  4120. return *this;
  4121. }
  4122. PUGI__FN xml_attribute& xml_attribute::operator=(unsigned long long rhs)
  4123. {
  4124. set_value(rhs);
  4125. return *this;
  4126. }
  4127. #endif
  4128. PUGI__FN bool xml_attribute::set_name(const char_t* rhs)
  4129. {
  4130. if (!_attr) return false;
  4131. return impl::strcpy_insitu(_attr->name, _attr->header, impl::xml_memory_page_name_allocated_mask, rhs, impl::strlength(rhs));
  4132. }
  4133. PUGI__FN bool xml_attribute::set_value(const char_t* rhs)
  4134. {
  4135. if (!_attr) return false;
  4136. return impl::strcpy_insitu(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, impl::strlength(rhs));
  4137. }
  4138. PUGI__FN bool xml_attribute::set_value(int rhs)
  4139. {
  4140. if (!_attr) return false;
  4141. return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
  4142. }
  4143. PUGI__FN bool xml_attribute::set_value(unsigned int rhs)
  4144. {
  4145. if (!_attr) return false;
  4146. return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
  4147. }
  4148. PUGI__FN bool xml_attribute::set_value(double rhs)
  4149. {
  4150. if (!_attr) return false;
  4151. return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
  4152. }
  4153. PUGI__FN bool xml_attribute::set_value(float rhs)
  4154. {
  4155. if (!_attr) return false;
  4156. return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
  4157. }
  4158. PUGI__FN bool xml_attribute::set_value(bool rhs)
  4159. {
  4160. if (!_attr) return false;
  4161. return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
  4162. }
  4163. #ifdef PUGIXML_HAS_LONG_LONG
  4164. PUGI__FN bool xml_attribute::set_value(long long rhs)
  4165. {
  4166. if (!_attr) return false;
  4167. return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
  4168. }
  4169. PUGI__FN bool xml_attribute::set_value(unsigned long long rhs)
  4170. {
  4171. if (!_attr) return false;
  4172. return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
  4173. }
  4174. #endif
  4175. #ifdef __BORLANDC__
  4176. PUGI__FN bool operator&&(const xml_attribute& lhs, bool rhs)
  4177. {
  4178. return (bool)lhs && rhs;
  4179. }
  4180. PUGI__FN bool operator||(const xml_attribute& lhs, bool rhs)
  4181. {
  4182. return (bool)lhs || rhs;
  4183. }
  4184. #endif
  4185. PUGI__FN xml_node::xml_node(): _root(0)
  4186. {
  4187. }
  4188. PUGI__FN xml_node::xml_node(xml_node_struct* p): _root(p)
  4189. {
  4190. }
  4191. PUGI__FN static void unspecified_bool_xml_node(xml_node***)
  4192. {
  4193. }
  4194. PUGI__FN xml_node::operator xml_node::unspecified_bool_type() const
  4195. {
  4196. return _root ? unspecified_bool_xml_node : 0;
  4197. }
  4198. PUGI__FN bool xml_node::operator!() const
  4199. {
  4200. return !_root;
  4201. }
  4202. PUGI__FN xml_node::iterator xml_node::begin() const
  4203. {
  4204. return iterator(_root ? _root->first_child + 0 : 0, _root);
  4205. }
  4206. PUGI__FN xml_node::iterator xml_node::end() const
  4207. {
  4208. return iterator(0, _root);
  4209. }
  4210. PUGI__FN xml_node::attribute_iterator xml_node::attributes_begin() const
  4211. {
  4212. return attribute_iterator(_root ? _root->first_attribute + 0 : 0, _root);
  4213. }
  4214. PUGI__FN xml_node::attribute_iterator xml_node::attributes_end() const
  4215. {
  4216. return attribute_iterator(0, _root);
  4217. }
  4218. PUGI__FN xml_object_range<xml_node_iterator> xml_node::children() const
  4219. {
  4220. return xml_object_range<xml_node_iterator>(begin(), end());
  4221. }
  4222. PUGI__FN xml_object_range<xml_named_node_iterator> xml_node::children(const char_t* name_) const
  4223. {
  4224. return xml_object_range<xml_named_node_iterator>(xml_named_node_iterator(child(name_)._root, _root, name_), xml_named_node_iterator(0, _root, name_));
  4225. }
  4226. PUGI__FN xml_object_range<xml_attribute_iterator> xml_node::attributes() const
  4227. {
  4228. return xml_object_range<xml_attribute_iterator>(attributes_begin(), attributes_end());
  4229. }
  4230. PUGI__FN bool xml_node::operator==(const xml_node& r) const
  4231. {
  4232. return (_root == r._root);
  4233. }
  4234. PUGI__FN bool xml_node::operator!=(const xml_node& r) const
  4235. {
  4236. return (_root != r._root);
  4237. }
  4238. PUGI__FN bool xml_node::operator<(const xml_node& r) const
  4239. {
  4240. return (_root < r._root);
  4241. }
  4242. PUGI__FN bool xml_node::operator>(const xml_node& r) const
  4243. {
  4244. return (_root > r._root);
  4245. }
  4246. PUGI__FN bool xml_node::operator<=(const xml_node& r) const
  4247. {
  4248. return (_root <= r._root);
  4249. }
  4250. PUGI__FN bool xml_node::operator>=(const xml_node& r) const
  4251. {
  4252. return (_root >= r._root);
  4253. }
  4254. PUGI__FN bool xml_node::empty() const
  4255. {
  4256. return !_root;
  4257. }
  4258. PUGI__FN const char_t* xml_node::name() const
  4259. {
  4260. return (_root && _root->name) ? _root->name + 0 : PUGIXML_TEXT("");
  4261. }
  4262. PUGI__FN xml_node_type xml_node::type() const
  4263. {
  4264. return _root ? PUGI__NODETYPE(_root) : node_null;
  4265. }
  4266. PUGI__FN const char_t* xml_node::value() const
  4267. {
  4268. return (_root && _root->value) ? _root->value + 0 : PUGIXML_TEXT("");
  4269. }
  4270. PUGI__FN xml_node xml_node::child(const char_t* name_) const
  4271. {
  4272. if (!_root) return xml_node();
  4273. for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
  4274. if (i->name && impl::strequal(name_, i->name)) return xml_node(i);
  4275. return xml_node();
  4276. }
  4277. PUGI__FN xml_attribute xml_node::attribute(const char_t* name_) const
  4278. {
  4279. if (!_root) return xml_attribute();
  4280. for (xml_attribute_struct* i = _root->first_attribute; i; i = i->next_attribute)
  4281. if (i->name && impl::strequal(name_, i->name))
  4282. return xml_attribute(i);
  4283. return xml_attribute();
  4284. }
  4285. PUGI__FN xml_node xml_node::next_sibling(const char_t* name_) const
  4286. {
  4287. if (!_root) return xml_node();
  4288. for (xml_node_struct* i = _root->next_sibling; i; i = i->next_sibling)
  4289. if (i->name && impl::strequal(name_, i->name)) return xml_node(i);
  4290. return xml_node();
  4291. }
  4292. PUGI__FN xml_node xml_node::next_sibling() const
  4293. {
  4294. return _root ? xml_node(_root->next_sibling) : xml_node();
  4295. }
  4296. PUGI__FN xml_node xml_node::previous_sibling(const char_t* name_) const
  4297. {
  4298. if (!_root) return xml_node();
  4299. for (xml_node_struct* i = _root->prev_sibling_c; i->next_sibling; i = i->prev_sibling_c)
  4300. if (i->name && impl::strequal(name_, i->name)) return xml_node(i);
  4301. return xml_node();
  4302. }
  4303. PUGI__FN xml_attribute xml_node::attribute(const char_t* name_, xml_attribute& hint_) const
  4304. {
  4305. xml_attribute_struct* hint = hint_._attr;
  4306. // if hint is not an attribute of node, behavior is not defined
  4307. assert(!hint || (_root && impl::is_attribute_of(hint, _root)));
  4308. if (!_root) return xml_attribute();
  4309. // optimistically search from hint up until the end
  4310. for (xml_attribute_struct* i = hint; i; i = i->next_attribute)
  4311. if (i->name && impl::strequal(name_, i->name))
  4312. {
  4313. // update hint to maximize efficiency of searching for consecutive attributes
  4314. hint_._attr = i->next_attribute;
  4315. return xml_attribute(i);
  4316. }
  4317. // wrap around and search from the first attribute until the hint
  4318. // 'j' null pointer check is technically redundant, but it prevents a crash in case the assertion above fails
  4319. for (xml_attribute_struct* j = _root->first_attribute; j && j != hint; j = j->next_attribute)
  4320. if (j->name && impl::strequal(name_, j->name))
  4321. {
  4322. // update hint to maximize efficiency of searching for consecutive attributes
  4323. hint_._attr = j->next_attribute;
  4324. return xml_attribute(j);
  4325. }
  4326. return xml_attribute();
  4327. }
  4328. PUGI__FN xml_node xml_node::previous_sibling() const
  4329. {
  4330. if (!_root) return xml_node();
  4331. if (_root->prev_sibling_c->next_sibling) return xml_node(_root->prev_sibling_c);
  4332. else return xml_node();
  4333. }
  4334. PUGI__FN xml_node xml_node::parent() const
  4335. {
  4336. return _root ? xml_node(_root->parent) : xml_node();
  4337. }
  4338. PUGI__FN xml_node xml_node::root() const
  4339. {
  4340. return _root ? xml_node(&impl::get_document(_root)) : xml_node();
  4341. }
  4342. PUGI__FN xml_text xml_node::text() const
  4343. {
  4344. return xml_text(_root);
  4345. }
  4346. PUGI__FN const char_t* xml_node::child_value() const
  4347. {
  4348. if (!_root) return PUGIXML_TEXT("");
  4349. for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
  4350. if (impl::is_text_node(i) && i->value)
  4351. return i->value;
  4352. return PUGIXML_TEXT("");
  4353. }
  4354. PUGI__FN const char_t* xml_node::child_value(const char_t* name_) const
  4355. {
  4356. return child(name_).child_value();
  4357. }
  4358. PUGI__FN xml_attribute xml_node::first_attribute() const
  4359. {
  4360. return _root ? xml_attribute(_root->first_attribute) : xml_attribute();
  4361. }
  4362. PUGI__FN xml_attribute xml_node::last_attribute() const
  4363. {
  4364. return _root && _root->first_attribute ? xml_attribute(_root->first_attribute->prev_attribute_c) : xml_attribute();
  4365. }
  4366. PUGI__FN xml_node xml_node::first_child() const
  4367. {
  4368. return _root ? xml_node(_root->first_child) : xml_node();
  4369. }
  4370. PUGI__FN xml_node xml_node::last_child() const
  4371. {
  4372. return _root && _root->first_child ? xml_node(_root->first_child->prev_sibling_c) : xml_node();
  4373. }
  4374. PUGI__FN bool xml_node::set_name(const char_t* rhs)
  4375. {
  4376. xml_node_type type_ = _root ? PUGI__NODETYPE(_root) : node_null;
  4377. if (type_ != node_element && type_ != node_pi && type_ != node_declaration)
  4378. return false;
  4379. return impl::strcpy_insitu(_root->name, _root->header, impl::xml_memory_page_name_allocated_mask, rhs, impl::strlength(rhs));
  4380. }
  4381. PUGI__FN bool xml_node::set_value(const char_t* rhs)
  4382. {
  4383. xml_node_type type_ = _root ? PUGI__NODETYPE(_root) : node_null;
  4384. if (type_ != node_pcdata && type_ != node_cdata && type_ != node_comment && type_ != node_pi && type_ != node_doctype)
  4385. return false;
  4386. return impl::strcpy_insitu(_root->value, _root->header, impl::xml_memory_page_value_allocated_mask, rhs, impl::strlength(rhs));
  4387. }
  4388. PUGI__FN xml_attribute xml_node::append_attribute(const char_t* name_)
  4389. {
  4390. if (!impl::allow_insert_attribute(type())) return xml_attribute();
  4391. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4392. if (!alloc.reserve()) return xml_attribute();
  4393. xml_attribute a(impl::allocate_attribute(alloc));
  4394. if (!a) return xml_attribute();
  4395. impl::append_attribute(a._attr, _root);
  4396. a.set_name(name_);
  4397. return a;
  4398. }
  4399. PUGI__FN xml_attribute xml_node::prepend_attribute(const char_t* name_)
  4400. {
  4401. if (!impl::allow_insert_attribute(type())) return xml_attribute();
  4402. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4403. if (!alloc.reserve()) return xml_attribute();
  4404. xml_attribute a(impl::allocate_attribute(alloc));
  4405. if (!a) return xml_attribute();
  4406. impl::prepend_attribute(a._attr, _root);
  4407. a.set_name(name_);
  4408. return a;
  4409. }
  4410. PUGI__FN xml_attribute xml_node::insert_attribute_after(const char_t* name_, const xml_attribute& attr)
  4411. {
  4412. if (!impl::allow_insert_attribute(type())) return xml_attribute();
  4413. if (!attr || !impl::is_attribute_of(attr._attr, _root)) return xml_attribute();
  4414. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4415. if (!alloc.reserve()) return xml_attribute();
  4416. xml_attribute a(impl::allocate_attribute(alloc));
  4417. if (!a) return xml_attribute();
  4418. impl::insert_attribute_after(a._attr, attr._attr, _root);
  4419. a.set_name(name_);
  4420. return a;
  4421. }
  4422. PUGI__FN xml_attribute xml_node::insert_attribute_before(const char_t* name_, const xml_attribute& attr)
  4423. {
  4424. if (!impl::allow_insert_attribute(type())) return xml_attribute();
  4425. if (!attr || !impl::is_attribute_of(attr._attr, _root)) return xml_attribute();
  4426. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4427. if (!alloc.reserve()) return xml_attribute();
  4428. xml_attribute a(impl::allocate_attribute(alloc));
  4429. if (!a) return xml_attribute();
  4430. impl::insert_attribute_before(a._attr, attr._attr, _root);
  4431. a.set_name(name_);
  4432. return a;
  4433. }
  4434. PUGI__FN xml_attribute xml_node::append_copy(const xml_attribute& proto)
  4435. {
  4436. if (!proto) return xml_attribute();
  4437. if (!impl::allow_insert_attribute(type())) return xml_attribute();
  4438. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4439. if (!alloc.reserve()) return xml_attribute();
  4440. xml_attribute a(impl::allocate_attribute(alloc));
  4441. if (!a) return xml_attribute();
  4442. impl::append_attribute(a._attr, _root);
  4443. impl::node_copy_attribute(a._attr, proto._attr);
  4444. return a;
  4445. }
  4446. PUGI__FN xml_attribute xml_node::prepend_copy(const xml_attribute& proto)
  4447. {
  4448. if (!proto) return xml_attribute();
  4449. if (!impl::allow_insert_attribute(type())) return xml_attribute();
  4450. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4451. if (!alloc.reserve()) return xml_attribute();
  4452. xml_attribute a(impl::allocate_attribute(alloc));
  4453. if (!a) return xml_attribute();
  4454. impl::prepend_attribute(a._attr, _root);
  4455. impl::node_copy_attribute(a._attr, proto._attr);
  4456. return a;
  4457. }
  4458. PUGI__FN xml_attribute xml_node::insert_copy_after(const xml_attribute& proto, const xml_attribute& attr)
  4459. {
  4460. if (!proto) return xml_attribute();
  4461. if (!impl::allow_insert_attribute(type())) return xml_attribute();
  4462. if (!attr || !impl::is_attribute_of(attr._attr, _root)) return xml_attribute();
  4463. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4464. if (!alloc.reserve()) return xml_attribute();
  4465. xml_attribute a(impl::allocate_attribute(alloc));
  4466. if (!a) return xml_attribute();
  4467. impl::insert_attribute_after(a._attr, attr._attr, _root);
  4468. impl::node_copy_attribute(a._attr, proto._attr);
  4469. return a;
  4470. }
  4471. PUGI__FN xml_attribute xml_node::insert_copy_before(const xml_attribute& proto, const xml_attribute& attr)
  4472. {
  4473. if (!proto) return xml_attribute();
  4474. if (!impl::allow_insert_attribute(type())) return xml_attribute();
  4475. if (!attr || !impl::is_attribute_of(attr._attr, _root)) return xml_attribute();
  4476. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4477. if (!alloc.reserve()) return xml_attribute();
  4478. xml_attribute a(impl::allocate_attribute(alloc));
  4479. if (!a) return xml_attribute();
  4480. impl::insert_attribute_before(a._attr, attr._attr, _root);
  4481. impl::node_copy_attribute(a._attr, proto._attr);
  4482. return a;
  4483. }
  4484. PUGI__FN xml_node xml_node::append_child(xml_node_type type_)
  4485. {
  4486. if (!impl::allow_insert_child(type(), type_)) return xml_node();
  4487. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4488. if (!alloc.reserve()) return xml_node();
  4489. xml_node n(impl::allocate_node(alloc, type_));
  4490. if (!n) return xml_node();
  4491. impl::append_node(n._root, _root);
  4492. if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml"));
  4493. return n;
  4494. }
  4495. PUGI__FN xml_node xml_node::prepend_child(xml_node_type type_)
  4496. {
  4497. if (!impl::allow_insert_child(type(), type_)) return xml_node();
  4498. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4499. if (!alloc.reserve()) return xml_node();
  4500. xml_node n(impl::allocate_node(alloc, type_));
  4501. if (!n) return xml_node();
  4502. impl::prepend_node(n._root, _root);
  4503. if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml"));
  4504. return n;
  4505. }
  4506. PUGI__FN xml_node xml_node::insert_child_before(xml_node_type type_, const xml_node& node)
  4507. {
  4508. if (!impl::allow_insert_child(type(), type_)) return xml_node();
  4509. if (!node._root || node._root->parent != _root) return xml_node();
  4510. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4511. if (!alloc.reserve()) return xml_node();
  4512. xml_node n(impl::allocate_node(alloc, type_));
  4513. if (!n) return xml_node();
  4514. impl::insert_node_before(n._root, node._root);
  4515. if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml"));
  4516. return n;
  4517. }
  4518. PUGI__FN xml_node xml_node::insert_child_after(xml_node_type type_, const xml_node& node)
  4519. {
  4520. if (!impl::allow_insert_child(type(), type_)) return xml_node();
  4521. if (!node._root || node._root->parent != _root) return xml_node();
  4522. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4523. if (!alloc.reserve()) return xml_node();
  4524. xml_node n(impl::allocate_node(alloc, type_));
  4525. if (!n) return xml_node();
  4526. impl::insert_node_after(n._root, node._root);
  4527. if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml"));
  4528. return n;
  4529. }
  4530. PUGI__FN xml_node xml_node::append_child(const char_t* name_)
  4531. {
  4532. xml_node result = append_child(node_element);
  4533. result.set_name(name_);
  4534. return result;
  4535. }
  4536. PUGI__FN xml_node xml_node::prepend_child(const char_t* name_)
  4537. {
  4538. xml_node result = prepend_child(node_element);
  4539. result.set_name(name_);
  4540. return result;
  4541. }
  4542. PUGI__FN xml_node xml_node::insert_child_after(const char_t* name_, const xml_node& node)
  4543. {
  4544. xml_node result = insert_child_after(node_element, node);
  4545. result.set_name(name_);
  4546. return result;
  4547. }
  4548. PUGI__FN xml_node xml_node::insert_child_before(const char_t* name_, const xml_node& node)
  4549. {
  4550. xml_node result = insert_child_before(node_element, node);
  4551. result.set_name(name_);
  4552. return result;
  4553. }
  4554. PUGI__FN xml_node xml_node::append_copy(const xml_node& proto)
  4555. {
  4556. xml_node_type type_ = proto.type();
  4557. if (!impl::allow_insert_child(type(), type_)) return xml_node();
  4558. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4559. if (!alloc.reserve()) return xml_node();
  4560. xml_node n(impl::allocate_node(alloc, type_));
  4561. if (!n) return xml_node();
  4562. impl::append_node(n._root, _root);
  4563. impl::node_copy_tree(n._root, proto._root);
  4564. return n;
  4565. }
  4566. PUGI__FN xml_node xml_node::prepend_copy(const xml_node& proto)
  4567. {
  4568. xml_node_type type_ = proto.type();
  4569. if (!impl::allow_insert_child(type(), type_)) return xml_node();
  4570. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4571. if (!alloc.reserve()) return xml_node();
  4572. xml_node n(impl::allocate_node(alloc, type_));
  4573. if (!n) return xml_node();
  4574. impl::prepend_node(n._root, _root);
  4575. impl::node_copy_tree(n._root, proto._root);
  4576. return n;
  4577. }
  4578. PUGI__FN xml_node xml_node::insert_copy_after(const xml_node& proto, const xml_node& node)
  4579. {
  4580. xml_node_type type_ = proto.type();
  4581. if (!impl::allow_insert_child(type(), type_)) return xml_node();
  4582. if (!node._root || node._root->parent != _root) return xml_node();
  4583. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4584. if (!alloc.reserve()) return xml_node();
  4585. xml_node n(impl::allocate_node(alloc, type_));
  4586. if (!n) return xml_node();
  4587. impl::insert_node_after(n._root, node._root);
  4588. impl::node_copy_tree(n._root, proto._root);
  4589. return n;
  4590. }
  4591. PUGI__FN xml_node xml_node::insert_copy_before(const xml_node& proto, const xml_node& node)
  4592. {
  4593. xml_node_type type_ = proto.type();
  4594. if (!impl::allow_insert_child(type(), type_)) return xml_node();
  4595. if (!node._root || node._root->parent != _root) return xml_node();
  4596. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4597. if (!alloc.reserve()) return xml_node();
  4598. xml_node n(impl::allocate_node(alloc, type_));
  4599. if (!n) return xml_node();
  4600. impl::insert_node_before(n._root, node._root);
  4601. impl::node_copy_tree(n._root, proto._root);
  4602. return n;
  4603. }
  4604. PUGI__FN xml_node xml_node::append_move(const xml_node& moved)
  4605. {
  4606. if (!impl::allow_move(*this, moved)) return xml_node();
  4607. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4608. if (!alloc.reserve()) return xml_node();
  4609. // disable document_buffer_order optimization since moving nodes around changes document order without changing buffer pointers
  4610. impl::get_document(_root).header |= impl::xml_memory_page_contents_shared_mask;
  4611. impl::remove_node(moved._root);
  4612. impl::append_node(moved._root, _root);
  4613. return moved;
  4614. }
  4615. PUGI__FN xml_node xml_node::prepend_move(const xml_node& moved)
  4616. {
  4617. if (!impl::allow_move(*this, moved)) return xml_node();
  4618. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4619. if (!alloc.reserve()) return xml_node();
  4620. // disable document_buffer_order optimization since moving nodes around changes document order without changing buffer pointers
  4621. impl::get_document(_root).header |= impl::xml_memory_page_contents_shared_mask;
  4622. impl::remove_node(moved._root);
  4623. impl::prepend_node(moved._root, _root);
  4624. return moved;
  4625. }
  4626. PUGI__FN xml_node xml_node::insert_move_after(const xml_node& moved, const xml_node& node)
  4627. {
  4628. if (!impl::allow_move(*this, moved)) return xml_node();
  4629. if (!node._root || node._root->parent != _root) return xml_node();
  4630. if (moved._root == node._root) return xml_node();
  4631. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4632. if (!alloc.reserve()) return xml_node();
  4633. // disable document_buffer_order optimization since moving nodes around changes document order without changing buffer pointers
  4634. impl::get_document(_root).header |= impl::xml_memory_page_contents_shared_mask;
  4635. impl::remove_node(moved._root);
  4636. impl::insert_node_after(moved._root, node._root);
  4637. return moved;
  4638. }
  4639. PUGI__FN xml_node xml_node::insert_move_before(const xml_node& moved, const xml_node& node)
  4640. {
  4641. if (!impl::allow_move(*this, moved)) return xml_node();
  4642. if (!node._root || node._root->parent != _root) return xml_node();
  4643. if (moved._root == node._root) return xml_node();
  4644. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4645. if (!alloc.reserve()) return xml_node();
  4646. // disable document_buffer_order optimization since moving nodes around changes document order without changing buffer pointers
  4647. impl::get_document(_root).header |= impl::xml_memory_page_contents_shared_mask;
  4648. impl::remove_node(moved._root);
  4649. impl::insert_node_before(moved._root, node._root);
  4650. return moved;
  4651. }
  4652. PUGI__FN bool xml_node::remove_attribute(const char_t* name_)
  4653. {
  4654. return remove_attribute(attribute(name_));
  4655. }
  4656. PUGI__FN bool xml_node::remove_attribute(const xml_attribute& a)
  4657. {
  4658. if (!_root || !a._attr) return false;
  4659. if (!impl::is_attribute_of(a._attr, _root)) return false;
  4660. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4661. if (!alloc.reserve()) return false;
  4662. impl::remove_attribute(a._attr, _root);
  4663. impl::destroy_attribute(a._attr, alloc);
  4664. return true;
  4665. }
  4666. PUGI__FN bool xml_node::remove_child(const char_t* name_)
  4667. {
  4668. return remove_child(child(name_));
  4669. }
  4670. PUGI__FN bool xml_node::remove_child(const xml_node& n)
  4671. {
  4672. if (!_root || !n._root || n._root->parent != _root) return false;
  4673. impl::xml_allocator& alloc = impl::get_allocator(_root);
  4674. if (!alloc.reserve()) return false;
  4675. impl::remove_node(n._root);
  4676. impl::destroy_node(n._root, alloc);
  4677. return true;
  4678. }
  4679. PUGI__FN xml_parse_result xml_node::append_buffer(const void* contents, size_t size, unsigned int options, xml_encoding encoding)
  4680. {
  4681. // append_buffer is only valid for elements/documents
  4682. if (!impl::allow_insert_child(type(), node_element)) return impl::make_parse_result(status_append_invalid_root);
  4683. // get document node
  4684. impl::xml_document_struct* doc = &impl::get_document(_root);
  4685. // disable document_buffer_order optimization since in a document with multiple buffers comparing buffer pointers does not make sense
  4686. doc->header |= impl::xml_memory_page_contents_shared_mask;
  4687. // get extra buffer element (we'll store the document fragment buffer there so that we can deallocate it later)
  4688. impl::xml_memory_page* page = 0;
  4689. impl::xml_extra_buffer* extra = static_cast<impl::xml_extra_buffer*>(doc->allocate_memory(sizeof(impl::xml_extra_buffer), page));
  4690. (void)page;
  4691. if (!extra) return impl::make_parse_result(status_out_of_memory);
  4692. // add extra buffer to the list
  4693. extra->buffer = 0;
  4694. extra->next = doc->extra_buffers;
  4695. doc->extra_buffers = extra;
  4696. // name of the root has to be NULL before parsing - otherwise closing node mismatches will not be detected at the top level
  4697. impl::name_null_sentry sentry(_root);
  4698. return impl::load_buffer_impl(doc, _root, const_cast<void*>(contents), size, options, encoding, false, false, &extra->buffer);
  4699. }
  4700. PUGI__FN xml_node xml_node::find_child_by_attribute(const char_t* name_, const char_t* attr_name, const char_t* attr_value) const
  4701. {
  4702. if (!_root) return xml_node();
  4703. for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
  4704. if (i->name && impl::strequal(name_, i->name))
  4705. {
  4706. for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute)
  4707. if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value + 0 : PUGIXML_TEXT("")))
  4708. return xml_node(i);
  4709. }
  4710. return xml_node();
  4711. }
  4712. PUGI__FN xml_node xml_node::find_child_by_attribute(const char_t* attr_name, const char_t* attr_value) const
  4713. {
  4714. if (!_root) return xml_node();
  4715. for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
  4716. for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute)
  4717. if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value + 0 : PUGIXML_TEXT("")))
  4718. return xml_node(i);
  4719. return xml_node();
  4720. }
  4721. #ifndef PUGIXML_NO_STL
  4722. PUGI__FN string_t xml_node::path(char_t delimiter) const
  4723. {
  4724. if (!_root) return string_t();
  4725. size_t offset = 0;
  4726. for (xml_node_struct* i = _root; i; i = i->parent)
  4727. {
  4728. offset += (i != _root);
  4729. offset += i->name ? impl::strlength(i->name) : 0;
  4730. }
  4731. string_t result;
  4732. result.resize(offset);
  4733. for (xml_node_struct* j = _root; j; j = j->parent)
  4734. {
  4735. if (j != _root)
  4736. result[--offset] = delimiter;
  4737. if (j->name && *j->name)
  4738. {
  4739. size_t length = impl::strlength(j->name);
  4740. offset -= length;
  4741. memcpy(&result[offset], j->name, length * sizeof(char_t));
  4742. }
  4743. }
  4744. assert(offset == 0);
  4745. return result;
  4746. }
  4747. #endif
  4748. PUGI__FN xml_node xml_node::first_element_by_path(const char_t* path_, char_t delimiter) const
  4749. {
  4750. xml_node found = *this; // Current search context.
  4751. if (!_root || !path_ || !path_[0]) return found;
  4752. if (path_[0] == delimiter)
  4753. {
  4754. // Absolute path; e.g. '/foo/bar'
  4755. found = found.root();
  4756. ++path_;
  4757. }
  4758. const char_t* path_segment = path_;
  4759. while (*path_segment == delimiter) ++path_segment;
  4760. const char_t* path_segment_end = path_segment;
  4761. while (*path_segment_end && *path_segment_end != delimiter) ++path_segment_end;
  4762. if (path_segment == path_segment_end) return found;
  4763. const char_t* next_segment = path_segment_end;
  4764. while (*next_segment == delimiter) ++next_segment;
  4765. if (*path_segment == '.' && path_segment + 1 == path_segment_end)
  4766. return found.first_element_by_path(next_segment, delimiter);
  4767. else if (*path_segment == '.' && *(path_segment+1) == '.' && path_segment + 2 == path_segment_end)
  4768. return found.parent().first_element_by_path(next_segment, delimiter);
  4769. else
  4770. {
  4771. for (xml_node_struct* j = found._root->first_child; j; j = j->next_sibling)
  4772. {
  4773. if (j->name && impl::strequalrange(j->name, path_segment, static_cast<size_t>(path_segment_end - path_segment)))
  4774. {
  4775. xml_node subsearch = xml_node(j).first_element_by_path(next_segment, delimiter);
  4776. if (subsearch) return subsearch;
  4777. }
  4778. }
  4779. return xml_node();
  4780. }
  4781. }
  4782. PUGI__FN bool xml_node::traverse(xml_tree_walker& walker)
  4783. {
  4784. walker._depth = -1;
  4785. xml_node arg_begin = *this;
  4786. if (!walker.begin(arg_begin)) return false;
  4787. xml_node cur = first_child();
  4788. if (cur)
  4789. {
  4790. ++walker._depth;
  4791. do
  4792. {
  4793. xml_node arg_for_each = cur;
  4794. if (!walker.for_each(arg_for_each))
  4795. return false;
  4796. if (cur.first_child())
  4797. {
  4798. ++walker._depth;
  4799. cur = cur.first_child();
  4800. }
  4801. else if (cur.next_sibling())
  4802. cur = cur.next_sibling();
  4803. else
  4804. {
  4805. // Borland C++ workaround
  4806. while (!cur.next_sibling() && cur != *this && !cur.parent().empty())
  4807. {
  4808. --walker._depth;
  4809. cur = cur.parent();
  4810. }
  4811. if (cur != *this)
  4812. cur = cur.next_sibling();
  4813. }
  4814. }
  4815. while (cur && cur != *this);
  4816. }
  4817. assert(walker._depth == -1);
  4818. xml_node arg_end = *this;
  4819. return walker.end(arg_end);
  4820. }
  4821. PUGI__FN size_t xml_node::hash_value() const
  4822. {
  4823. return static_cast<size_t>(reinterpret_cast<uintptr_t>(_root) / sizeof(xml_node_struct));
  4824. }
  4825. PUGI__FN xml_node_struct* xml_node::internal_object() const
  4826. {
  4827. return _root;
  4828. }
  4829. PUGI__FN void xml_node::print(xml_writer& writer, const char_t* indent, unsigned int flags, xml_encoding encoding, unsigned int depth) const
  4830. {
  4831. if (!_root) return;
  4832. impl::xml_buffered_writer buffered_writer(writer, encoding);
  4833. impl::node_output(buffered_writer, _root, indent, flags, depth);
  4834. buffered_writer.flush();
  4835. }
  4836. #ifndef PUGIXML_NO_STL
  4837. PUGI__FN void xml_node::print(std::basic_ostream<char, std::char_traits<char> >& stream, const char_t* indent, unsigned int flags, xml_encoding encoding, unsigned int depth) const
  4838. {
  4839. xml_writer_stream writer(stream);
  4840. print(writer, indent, flags, encoding, depth);
  4841. }
  4842. PUGI__FN void xml_node::print(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream, const char_t* indent, unsigned int flags, unsigned int depth) const
  4843. {
  4844. xml_writer_stream writer(stream);
  4845. print(writer, indent, flags, encoding_wchar, depth);
  4846. }
  4847. #endif
  4848. PUGI__FN ptrdiff_t xml_node::offset_debug() const
  4849. {
  4850. if (!_root) return -1;
  4851. impl::xml_document_struct& doc = impl::get_document(_root);
  4852. // we can determine the offset reliably only if there is exactly once parse buffer
  4853. if (!doc.buffer || doc.extra_buffers) return -1;
  4854. switch (type())
  4855. {
  4856. case node_document:
  4857. return 0;
  4858. case node_element:
  4859. case node_declaration:
  4860. case node_pi:
  4861. return _root->name && (_root->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0 ? _root->name - doc.buffer : -1;
  4862. case node_pcdata:
  4863. case node_cdata:
  4864. case node_comment:
  4865. case node_doctype:
  4866. return _root->value && (_root->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0 ? _root->value - doc.buffer : -1;
  4867. default:
  4868. return -1;
  4869. }
  4870. }
  4871. #ifdef __BORLANDC__
  4872. PUGI__FN bool operator&&(const xml_node& lhs, bool rhs)
  4873. {
  4874. return (bool)lhs && rhs;
  4875. }
  4876. PUGI__FN bool operator||(const xml_node& lhs, bool rhs)
  4877. {
  4878. return (bool)lhs || rhs;
  4879. }
  4880. #endif
  4881. PUGI__FN xml_text::xml_text(xml_node_struct* root): _root(root)
  4882. {
  4883. }
  4884. PUGI__FN xml_node_struct* xml_text::_data() const
  4885. {
  4886. if (!_root || impl::is_text_node(_root)) return _root;
  4887. for (xml_node_struct* node = _root->first_child; node; node = node->next_sibling)
  4888. if (impl::is_text_node(node))
  4889. return node;
  4890. return 0;
  4891. }
  4892. PUGI__FN xml_node_struct* xml_text::_data_new()
  4893. {
  4894. xml_node_struct* d = _data();
  4895. if (d) return d;
  4896. return xml_node(_root).append_child(node_pcdata).internal_object();
  4897. }
  4898. PUGI__FN xml_text::xml_text(): _root(0)
  4899. {
  4900. }
  4901. PUGI__FN static void unspecified_bool_xml_text(xml_text***)
  4902. {
  4903. }
  4904. PUGI__FN xml_text::operator xml_text::unspecified_bool_type() const
  4905. {
  4906. return _data() ? unspecified_bool_xml_text : 0;
  4907. }
  4908. PUGI__FN bool xml_text::operator!() const
  4909. {
  4910. return !_data();
  4911. }
  4912. PUGI__FN bool xml_text::empty() const
  4913. {
  4914. return _data() == 0;
  4915. }
  4916. PUGI__FN const char_t* xml_text::get() const
  4917. {
  4918. xml_node_struct* d = _data();
  4919. return (d && d->value) ? d->value + 0 : PUGIXML_TEXT("");
  4920. }
  4921. PUGI__FN const char_t* xml_text::as_string(const char_t* def) const
  4922. {
  4923. xml_node_struct* d = _data();
  4924. return (d && d->value) ? d->value + 0 : def;
  4925. }
  4926. PUGI__FN int xml_text::as_int(int def) const
  4927. {
  4928. xml_node_struct* d = _data();
  4929. return (d && d->value) ? impl::get_value_int(d->value) : def;
  4930. }
  4931. PUGI__FN unsigned int xml_text::as_uint(unsigned int def) const
  4932. {
  4933. xml_node_struct* d = _data();
  4934. return (d && d->value) ? impl::get_value_uint(d->value) : def;
  4935. }
  4936. PUGI__FN double xml_text::as_double(double def) const
  4937. {
  4938. xml_node_struct* d = _data();
  4939. return (d && d->value) ? impl::get_value_double(d->value) : def;
  4940. }
  4941. PUGI__FN float xml_text::as_float(float def) const
  4942. {
  4943. xml_node_struct* d = _data();
  4944. return (d && d->value) ? impl::get_value_float(d->value) : def;
  4945. }
  4946. PUGI__FN bool xml_text::as_bool(bool def) const
  4947. {
  4948. xml_node_struct* d = _data();
  4949. return (d && d->value) ? impl::get_value_bool(d->value) : def;
  4950. }
  4951. #ifdef PUGIXML_HAS_LONG_LONG
  4952. PUGI__FN long long xml_text::as_llong(long long def) const
  4953. {
  4954. xml_node_struct* d = _data();
  4955. return (d && d->value) ? impl::get_value_llong(d->value) : def;
  4956. }
  4957. PUGI__FN unsigned long long xml_text::as_ullong(unsigned long long def) const
  4958. {
  4959. xml_node_struct* d = _data();
  4960. return (d && d->value) ? impl::get_value_ullong(d->value) : def;
  4961. }
  4962. #endif
  4963. PUGI__FN bool xml_text::set(const char_t* rhs)
  4964. {
  4965. xml_node_struct* dn = _data_new();
  4966. return dn ? impl::strcpy_insitu(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs, impl::strlength(rhs)) : false;
  4967. }
  4968. PUGI__FN bool xml_text::set(int rhs)
  4969. {
  4970. xml_node_struct* dn = _data_new();
  4971. return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
  4972. }
  4973. PUGI__FN bool xml_text::set(unsigned int rhs)
  4974. {
  4975. xml_node_struct* dn = _data_new();
  4976. return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
  4977. }
  4978. PUGI__FN bool xml_text::set(float rhs)
  4979. {
  4980. xml_node_struct* dn = _data_new();
  4981. return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
  4982. }
  4983. PUGI__FN bool xml_text::set(double rhs)
  4984. {
  4985. xml_node_struct* dn = _data_new();
  4986. return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
  4987. }
  4988. PUGI__FN bool xml_text::set(bool rhs)
  4989. {
  4990. xml_node_struct* dn = _data_new();
  4991. return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
  4992. }
  4993. #ifdef PUGIXML_HAS_LONG_LONG
  4994. PUGI__FN bool xml_text::set(long long rhs)
  4995. {
  4996. xml_node_struct* dn = _data_new();
  4997. return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
  4998. }
  4999. PUGI__FN bool xml_text::set(unsigned long long rhs)
  5000. {
  5001. xml_node_struct* dn = _data_new();
  5002. return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
  5003. }
  5004. #endif
  5005. PUGI__FN xml_text& xml_text::operator=(const char_t* rhs)
  5006. {
  5007. set(rhs);
  5008. return *this;
  5009. }
  5010. PUGI__FN xml_text& xml_text::operator=(int rhs)
  5011. {
  5012. set(rhs);
  5013. return *this;
  5014. }
  5015. PUGI__FN xml_text& xml_text::operator=(unsigned int rhs)
  5016. {
  5017. set(rhs);
  5018. return *this;
  5019. }
  5020. PUGI__FN xml_text& xml_text::operator=(double rhs)
  5021. {
  5022. set(rhs);
  5023. return *this;
  5024. }
  5025. PUGI__FN xml_text& xml_text::operator=(float rhs)
  5026. {
  5027. set(rhs);
  5028. return *this;
  5029. }
  5030. PUGI__FN xml_text& xml_text::operator=(bool rhs)
  5031. {
  5032. set(rhs);
  5033. return *this;
  5034. }
  5035. #ifdef PUGIXML_HAS_LONG_LONG
  5036. PUGI__FN xml_text& xml_text::operator=(long long rhs)
  5037. {
  5038. set(rhs);
  5039. return *this;
  5040. }
  5041. PUGI__FN xml_text& xml_text::operator=(unsigned long long rhs)
  5042. {
  5043. set(rhs);
  5044. return *this;
  5045. }
  5046. #endif
  5047. PUGI__FN xml_node xml_text::data() const
  5048. {
  5049. return xml_node(_data());
  5050. }
  5051. #ifdef __BORLANDC__
  5052. PUGI__FN bool operator&&(const xml_text& lhs, bool rhs)
  5053. {
  5054. return (bool)lhs && rhs;
  5055. }
  5056. PUGI__FN bool operator||(const xml_text& lhs, bool rhs)
  5057. {
  5058. return (bool)lhs || rhs;
  5059. }
  5060. #endif
  5061. PUGI__FN xml_node_iterator::xml_node_iterator()
  5062. {
  5063. }
  5064. PUGI__FN xml_node_iterator::xml_node_iterator(const xml_node& node): _wrap(node), _parent(node.parent())
  5065. {
  5066. }
  5067. PUGI__FN xml_node_iterator::xml_node_iterator(xml_node_struct* ref, xml_node_struct* parent): _wrap(ref), _parent(parent)
  5068. {
  5069. }
  5070. PUGI__FN bool xml_node_iterator::operator==(const xml_node_iterator& rhs) const
  5071. {
  5072. return _wrap._root == rhs._wrap._root && _parent._root == rhs._parent._root;
  5073. }
  5074. PUGI__FN bool xml_node_iterator::operator!=(const xml_node_iterator& rhs) const
  5075. {
  5076. return _wrap._root != rhs._wrap._root || _parent._root != rhs._parent._root;
  5077. }
  5078. PUGI__FN xml_node& xml_node_iterator::operator*() const
  5079. {
  5080. assert(_wrap._root);
  5081. return _wrap;
  5082. }
  5083. PUGI__FN xml_node* xml_node_iterator::operator->() const
  5084. {
  5085. assert(_wrap._root);
  5086. return const_cast<xml_node*>(&_wrap); // BCC32 workaround
  5087. }
  5088. PUGI__FN const xml_node_iterator& xml_node_iterator::operator++()
  5089. {
  5090. assert(_wrap._root);
  5091. _wrap._root = _wrap._root->next_sibling;
  5092. return *this;
  5093. }
  5094. PUGI__FN xml_node_iterator xml_node_iterator::operator++(int)
  5095. {
  5096. xml_node_iterator temp = *this;
  5097. ++*this;
  5098. return temp;
  5099. }
  5100. PUGI__FN const xml_node_iterator& xml_node_iterator::operator--()
  5101. {
  5102. _wrap = _wrap._root ? _wrap.previous_sibling() : _parent.last_child();
  5103. return *this;
  5104. }
  5105. PUGI__FN xml_node_iterator xml_node_iterator::operator--(int)
  5106. {
  5107. xml_node_iterator temp = *this;
  5108. --*this;
  5109. return temp;
  5110. }
  5111. PUGI__FN xml_attribute_iterator::xml_attribute_iterator()
  5112. {
  5113. }
  5114. PUGI__FN xml_attribute_iterator::xml_attribute_iterator(const xml_attribute& attr, const xml_node& parent): _wrap(attr), _parent(parent)
  5115. {
  5116. }
  5117. PUGI__FN xml_attribute_iterator::xml_attribute_iterator(xml_attribute_struct* ref, xml_node_struct* parent): _wrap(ref), _parent(parent)
  5118. {
  5119. }
  5120. PUGI__FN bool xml_attribute_iterator::operator==(const xml_attribute_iterator& rhs) const
  5121. {
  5122. return _wrap._attr == rhs._wrap._attr && _parent._root == rhs._parent._root;
  5123. }
  5124. PUGI__FN bool xml_attribute_iterator::operator!=(const xml_attribute_iterator& rhs) const
  5125. {
  5126. return _wrap._attr != rhs._wrap._attr || _parent._root != rhs._parent._root;
  5127. }
  5128. PUGI__FN xml_attribute& xml_attribute_iterator::operator*() const
  5129. {
  5130. assert(_wrap._attr);
  5131. return _wrap;
  5132. }
  5133. PUGI__FN xml_attribute* xml_attribute_iterator::operator->() const
  5134. {
  5135. assert(_wrap._attr);
  5136. return const_cast<xml_attribute*>(&_wrap); // BCC32 workaround
  5137. }
  5138. PUGI__FN const xml_attribute_iterator& xml_attribute_iterator::operator++()
  5139. {
  5140. assert(_wrap._attr);
  5141. _wrap._attr = _wrap._attr->next_attribute;
  5142. return *this;
  5143. }
  5144. PUGI__FN xml_attribute_iterator xml_attribute_iterator::operator++(int)
  5145. {
  5146. xml_attribute_iterator temp = *this;
  5147. ++*this;
  5148. return temp;
  5149. }
  5150. PUGI__FN const xml_attribute_iterator& xml_attribute_iterator::operator--()
  5151. {
  5152. _wrap = _wrap._attr ? _wrap.previous_attribute() : _parent.last_attribute();
  5153. return *this;
  5154. }
  5155. PUGI__FN xml_attribute_iterator xml_attribute_iterator::operator--(int)
  5156. {
  5157. xml_attribute_iterator temp = *this;
  5158. --*this;
  5159. return temp;
  5160. }
  5161. PUGI__FN xml_named_node_iterator::xml_named_node_iterator(): _name(0)
  5162. {
  5163. }
  5164. PUGI__FN xml_named_node_iterator::xml_named_node_iterator(const xml_node& node, const char_t* name): _wrap(node), _parent(node.parent()), _name(name)
  5165. {
  5166. }
  5167. PUGI__FN xml_named_node_iterator::xml_named_node_iterator(xml_node_struct* ref, xml_node_struct* parent, const char_t* name): _wrap(ref), _parent(parent), _name(name)
  5168. {
  5169. }
  5170. PUGI__FN bool xml_named_node_iterator::operator==(const xml_named_node_iterator& rhs) const
  5171. {
  5172. return _wrap._root == rhs._wrap._root && _parent._root == rhs._parent._root;
  5173. }
  5174. PUGI__FN bool xml_named_node_iterator::operator!=(const xml_named_node_iterator& rhs) const
  5175. {
  5176. return _wrap._root != rhs._wrap._root || _parent._root != rhs._parent._root;
  5177. }
  5178. PUGI__FN xml_node& xml_named_node_iterator::operator*() const
  5179. {
  5180. assert(_wrap._root);
  5181. return _wrap;
  5182. }
  5183. PUGI__FN xml_node* xml_named_node_iterator::operator->() const
  5184. {
  5185. assert(_wrap._root);
  5186. return const_cast<xml_node*>(&_wrap); // BCC32 workaround
  5187. }
  5188. PUGI__FN const xml_named_node_iterator& xml_named_node_iterator::operator++()
  5189. {
  5190. assert(_wrap._root);
  5191. _wrap = _wrap.next_sibling(_name);
  5192. return *this;
  5193. }
  5194. PUGI__FN xml_named_node_iterator xml_named_node_iterator::operator++(int)
  5195. {
  5196. xml_named_node_iterator temp = *this;
  5197. ++*this;
  5198. return temp;
  5199. }
  5200. PUGI__FN const xml_named_node_iterator& xml_named_node_iterator::operator--()
  5201. {
  5202. if (_wrap._root)
  5203. _wrap = _wrap.previous_sibling(_name);
  5204. else
  5205. {
  5206. _wrap = _parent.last_child();
  5207. if (!impl::strequal(_wrap.name(), _name))
  5208. _wrap = _wrap.previous_sibling(_name);
  5209. }
  5210. return *this;
  5211. }
  5212. PUGI__FN xml_named_node_iterator xml_named_node_iterator::operator--(int)
  5213. {
  5214. xml_named_node_iterator temp = *this;
  5215. --*this;
  5216. return temp;
  5217. }
  5218. PUGI__FN xml_parse_result::xml_parse_result(): status(status_internal_error), offset(0), encoding(encoding_auto)
  5219. {
  5220. }
  5221. PUGI__FN xml_parse_result::operator bool() const
  5222. {
  5223. return status == status_ok;
  5224. }
  5225. PUGI__FN const char* xml_parse_result::description() const
  5226. {
  5227. switch (status)
  5228. {
  5229. case status_ok: return "No error";
  5230. case status_file_not_found: return "File was not found";
  5231. case status_io_error: return "Error reading from file/stream";
  5232. case status_out_of_memory: return "Could not allocate memory";
  5233. case status_internal_error: return "Internal error occurred";
  5234. case status_unrecognized_tag: return "Could not determine tag type";
  5235. case status_bad_pi: return "Error parsing document declaration/processing instruction";
  5236. case status_bad_comment: return "Error parsing comment";
  5237. case status_bad_cdata: return "Error parsing CDATA section";
  5238. case status_bad_doctype: return "Error parsing document type declaration";
  5239. case status_bad_pcdata: return "Error parsing PCDATA section";
  5240. case status_bad_start_element: return "Error parsing start element tag";
  5241. case status_bad_attribute: return "Error parsing element attribute";
  5242. case status_bad_end_element: return "Error parsing end element tag";
  5243. case status_end_element_mismatch: return "Start-end tags mismatch";
  5244. case status_append_invalid_root: return "Unable to append nodes: root is not an element or document";
  5245. case status_no_document_element: return "No document element found";
  5246. default: return "Unknown error";
  5247. }
  5248. }
  5249. PUGI__FN xml_document::xml_document(): _buffer(0)
  5250. {
  5251. create();
  5252. }
  5253. PUGI__FN xml_document::~xml_document()
  5254. {
  5255. destroy();
  5256. }
  5257. PUGI__FN void xml_document::reset()
  5258. {
  5259. destroy();
  5260. create();
  5261. }
  5262. PUGI__FN void xml_document::reset(const xml_document& proto)
  5263. {
  5264. reset();
  5265. for (xml_node cur = proto.first_child(); cur; cur = cur.next_sibling())
  5266. append_copy(cur);
  5267. }
  5268. PUGI__FN void xml_document::create()
  5269. {
  5270. assert(!_root);
  5271. #ifdef PUGIXML_COMPACT
  5272. const size_t page_offset = sizeof(uint32_t);
  5273. #else
  5274. const size_t page_offset = 0;
  5275. #endif
  5276. // initialize sentinel page
  5277. PUGI__STATIC_ASSERT(sizeof(impl::xml_memory_page) + sizeof(impl::xml_document_struct) + impl::xml_memory_page_alignment - sizeof(void*) + page_offset <= sizeof(_memory));
  5278. // align upwards to page boundary
  5279. void* page_memory = reinterpret_cast<void*>((reinterpret_cast<uintptr_t>(_memory) + (impl::xml_memory_page_alignment - 1)) & ~(impl::xml_memory_page_alignment - 1));
  5280. // prepare page structure
  5281. impl::xml_memory_page* page = impl::xml_memory_page::construct(page_memory);
  5282. assert(page);
  5283. page->busy_size = impl::xml_memory_page_size;
  5284. // setup first page marker
  5285. #ifdef PUGIXML_COMPACT
  5286. // round-trip through void* to avoid 'cast increases required alignment of target type' warning
  5287. page->compact_page_marker = reinterpret_cast<uint32_t*>(static_cast<void*>(reinterpret_cast<char*>(page) + sizeof(impl::xml_memory_page)));
  5288. *page->compact_page_marker = sizeof(impl::xml_memory_page);
  5289. #endif
  5290. // allocate new root
  5291. _root = new (reinterpret_cast<char*>(page) + sizeof(impl::xml_memory_page) + page_offset) impl::xml_document_struct(page);
  5292. _root->prev_sibling_c = _root;
  5293. // setup sentinel page
  5294. page->allocator = static_cast<impl::xml_document_struct*>(_root);
  5295. // verify the document allocation
  5296. assert(reinterpret_cast<char*>(_root) + sizeof(impl::xml_document_struct) <= _memory + sizeof(_memory));
  5297. }
  5298. PUGI__FN void xml_document::destroy()
  5299. {
  5300. assert(_root);
  5301. // destroy static storage
  5302. if (_buffer)
  5303. {
  5304. impl::xml_memory::deallocate(_buffer);
  5305. _buffer = 0;
  5306. }
  5307. // destroy extra buffers (note: no need to destroy linked list nodes, they're allocated using document allocator)
  5308. for (impl::xml_extra_buffer* extra = static_cast<impl::xml_document_struct*>(_root)->extra_buffers; extra; extra = extra->next)
  5309. {
  5310. if (extra->buffer) impl::xml_memory::deallocate(extra->buffer);
  5311. }
  5312. // destroy dynamic storage, leave sentinel page (it's in static memory)
  5313. impl::xml_memory_page* root_page = PUGI__GETPAGE(_root);
  5314. assert(root_page && !root_page->prev);
  5315. assert(reinterpret_cast<char*>(root_page) >= _memory && reinterpret_cast<char*>(root_page) < _memory + sizeof(_memory));
  5316. for (impl::xml_memory_page* page = root_page->next; page; )
  5317. {
  5318. impl::xml_memory_page* next = page->next;
  5319. impl::xml_allocator::deallocate_page(page);
  5320. page = next;
  5321. }
  5322. #ifdef PUGIXML_COMPACT
  5323. // destroy hash table
  5324. static_cast<impl::xml_document_struct*>(_root)->hash.clear();
  5325. #endif
  5326. _root = 0;
  5327. }
  5328. #ifndef PUGIXML_NO_STL
  5329. PUGI__FN xml_parse_result xml_document::load(std::basic_istream<char, std::char_traits<char> >& stream, unsigned int options, xml_encoding encoding)
  5330. {
  5331. reset();
  5332. return impl::load_stream_impl(static_cast<impl::xml_document_struct*>(_root), stream, options, encoding, &_buffer);
  5333. }
  5334. PUGI__FN xml_parse_result xml_document::load(std::basic_istream<wchar_t, std::char_traits<wchar_t> >& stream, unsigned int options)
  5335. {
  5336. reset();
  5337. return impl::load_stream_impl(static_cast<impl::xml_document_struct*>(_root), stream, options, encoding_wchar, &_buffer);
  5338. }
  5339. #endif
  5340. PUGI__FN xml_parse_result xml_document::load_string(const char_t* contents, unsigned int options)
  5341. {
  5342. // Force native encoding (skip autodetection)
  5343. #ifdef PUGIXML_WCHAR_MODE
  5344. xml_encoding encoding = encoding_wchar;
  5345. #else
  5346. xml_encoding encoding = encoding_utf8;
  5347. #endif
  5348. return load_buffer(contents, impl::strlength(contents) * sizeof(char_t), options, encoding);
  5349. }
  5350. PUGI__FN xml_parse_result xml_document::load(const char_t* contents, unsigned int options)
  5351. {
  5352. return load_string(contents, options);
  5353. }
  5354. PUGI__FN xml_parse_result xml_document::load_file(const char* path_, unsigned int options, xml_encoding encoding)
  5355. {
  5356. reset();
  5357. using impl::auto_deleter; // MSVC7 workaround
  5358. auto_deleter<FILE, int(*)(FILE*)> file(fopen(path_, "rb"), fclose);
  5359. return impl::load_file_impl(static_cast<impl::xml_document_struct*>(_root), file.data, options, encoding, &_buffer);
  5360. }
  5361. PUGI__FN xml_parse_result xml_document::load_file(const wchar_t* path_, unsigned int options, xml_encoding encoding)
  5362. {
  5363. reset();
  5364. using impl::auto_deleter; // MSVC7 workaround
  5365. auto_deleter<FILE, int(*)(FILE*)> file(impl::open_file_wide(path_, L"rb"), fclose);
  5366. return impl::load_file_impl(static_cast<impl::xml_document_struct*>(_root), file.data, options, encoding, &_buffer);
  5367. }
  5368. PUGI__FN xml_parse_result xml_document::load_buffer(const void* contents, size_t size, unsigned int options, xml_encoding encoding)
  5369. {
  5370. reset();
  5371. return impl::load_buffer_impl(static_cast<impl::xml_document_struct*>(_root), _root, const_cast<void*>(contents), size, options, encoding, false, false, &_buffer);
  5372. }
  5373. PUGI__FN xml_parse_result xml_document::load_buffer_inplace(void* contents, size_t size, unsigned int options, xml_encoding encoding)
  5374. {
  5375. reset();
  5376. return impl::load_buffer_impl(static_cast<impl::xml_document_struct*>(_root), _root, contents, size, options, encoding, true, false, &_buffer);
  5377. }
  5378. PUGI__FN xml_parse_result xml_document::load_buffer_inplace_own(void* contents, size_t size, unsigned int options, xml_encoding encoding)
  5379. {
  5380. reset();
  5381. return impl::load_buffer_impl(static_cast<impl::xml_document_struct*>(_root), _root, contents, size, options, encoding, true, true, &_buffer);
  5382. }
  5383. PUGI__FN void xml_document::save(xml_writer& writer, const char_t* indent, unsigned int flags, xml_encoding encoding) const
  5384. {
  5385. impl::xml_buffered_writer buffered_writer(writer, encoding);
  5386. if ((flags & format_write_bom) && encoding != encoding_latin1)
  5387. {
  5388. // BOM always represents the codepoint U+FEFF, so just write it in native encoding
  5389. #ifdef PUGIXML_WCHAR_MODE
  5390. unsigned int bom = 0xfeff;
  5391. buffered_writer.write(static_cast<wchar_t>(bom));
  5392. #else
  5393. buffered_writer.write('\xef', '\xbb', '\xbf');
  5394. #endif
  5395. }
  5396. if (!(flags & format_no_declaration) && !impl::has_declaration(_root))
  5397. {
  5398. buffered_writer.write_string(PUGIXML_TEXT("<?xml version=\"1.0\""));
  5399. if (encoding == encoding_latin1) buffered_writer.write_string(PUGIXML_TEXT(" encoding=\"ISO-8859-1\""));
  5400. buffered_writer.write('?', '>');
  5401. if (!(flags & format_raw)) buffered_writer.write('\n');
  5402. }
  5403. impl::node_output(buffered_writer, _root, indent, flags, 0);
  5404. buffered_writer.flush();
  5405. }
  5406. #ifndef PUGIXML_NO_STL
  5407. PUGI__FN void xml_document::save(std::basic_ostream<char, std::char_traits<char> >& stream, const char_t* indent, unsigned int flags, xml_encoding encoding) const
  5408. {
  5409. xml_writer_stream writer(stream);
  5410. save(writer, indent, flags, encoding);
  5411. }
  5412. PUGI__FN void xml_document::save(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream, const char_t* indent, unsigned int flags) const
  5413. {
  5414. xml_writer_stream writer(stream);
  5415. save(writer, indent, flags, encoding_wchar);
  5416. }
  5417. #endif
  5418. PUGI__FN bool xml_document::save_file(const char* path_, const char_t* indent, unsigned int flags, xml_encoding encoding) const
  5419. {
  5420. using impl::auto_deleter; // MSVC7 workaround
  5421. auto_deleter<FILE, int(*)(FILE*)> file(fopen(path_, (flags & format_save_file_text) ? "w" : "wb"), fclose);
  5422. return impl::save_file_impl(*this, file.data, indent, flags, encoding);
  5423. }
  5424. PUGI__FN bool xml_document::save_file(const wchar_t* path_, const char_t* indent, unsigned int flags, xml_encoding encoding) const
  5425. {
  5426. using impl::auto_deleter; // MSVC7 workaround
  5427. auto_deleter<FILE, int(*)(FILE*)> file(impl::open_file_wide(path_, (flags & format_save_file_text) ? L"w" : L"wb"), fclose);
  5428. return impl::save_file_impl(*this, file.data, indent, flags, encoding);
  5429. }
  5430. PUGI__FN xml_node xml_document::document_element() const
  5431. {
  5432. assert(_root);
  5433. for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
  5434. if (PUGI__NODETYPE(i) == node_element)
  5435. return xml_node(i);
  5436. return xml_node();
  5437. }
  5438. #ifndef PUGIXML_NO_STL
  5439. PUGI__FN std::string PUGIXML_FUNCTION as_utf8(const wchar_t* str)
  5440. {
  5441. assert(str);
  5442. return impl::as_utf8_impl(str, impl::strlength_wide(str));
  5443. }
  5444. PUGI__FN std::string PUGIXML_FUNCTION as_utf8(const std::basic_string<wchar_t>& str)
  5445. {
  5446. return impl::as_utf8_impl(str.c_str(), str.size());
  5447. }
  5448. PUGI__FN std::basic_string<wchar_t> PUGIXML_FUNCTION as_wide(const char* str)
  5449. {
  5450. assert(str);
  5451. return impl::as_wide_impl(str, strlen(str));
  5452. }
  5453. PUGI__FN std::basic_string<wchar_t> PUGIXML_FUNCTION as_wide(const std::string& str)
  5454. {
  5455. return impl::as_wide_impl(str.c_str(), str.size());
  5456. }
  5457. #endif
  5458. PUGI__FN void PUGIXML_FUNCTION set_memory_management_functions(allocation_function allocate, deallocation_function deallocate)
  5459. {
  5460. impl::xml_memory::allocate = allocate;
  5461. impl::xml_memory::deallocate = deallocate;
  5462. }
  5463. PUGI__FN allocation_function PUGIXML_FUNCTION get_memory_allocation_function()
  5464. {
  5465. return impl::xml_memory::allocate;
  5466. }
  5467. PUGI__FN deallocation_function PUGIXML_FUNCTION get_memory_deallocation_function()
  5468. {
  5469. return impl::xml_memory::deallocate;
  5470. }
  5471. }
  5472. #if !defined(PUGIXML_NO_STL) && (defined(_MSC_VER) || defined(__ICC))
  5473. namespace std
  5474. {
  5475. // Workarounds for (non-standard) iterator category detection for older versions (MSVC7/IC8 and earlier)
  5476. PUGI__FN std::bidirectional_iterator_tag _Iter_cat(const pugi::xml_node_iterator&)
  5477. {
  5478. return std::bidirectional_iterator_tag();
  5479. }
  5480. PUGI__FN std::bidirectional_iterator_tag _Iter_cat(const pugi::xml_attribute_iterator&)
  5481. {
  5482. return std::bidirectional_iterator_tag();
  5483. }
  5484. PUGI__FN std::bidirectional_iterator_tag _Iter_cat(const pugi::xml_named_node_iterator&)
  5485. {
  5486. return std::bidirectional_iterator_tag();
  5487. }
  5488. }
  5489. #endif
  5490. #if !defined(PUGIXML_NO_STL) && defined(__SUNPRO_CC)
  5491. namespace std
  5492. {
  5493. // Workarounds for (non-standard) iterator category detection
  5494. PUGI__FN std::bidirectional_iterator_tag __iterator_category(const pugi::xml_node_iterator&)
  5495. {
  5496. return std::bidirectional_iterator_tag();
  5497. }
  5498. PUGI__FN std::bidirectional_iterator_tag __iterator_category(const pugi::xml_attribute_iterator&)
  5499. {
  5500. return std::bidirectional_iterator_tag();
  5501. }
  5502. PUGI__FN std::bidirectional_iterator_tag __iterator_category(const pugi::xml_named_node_iterator&)
  5503. {
  5504. return std::bidirectional_iterator_tag();
  5505. }
  5506. }
  5507. #endif
  5508. #ifndef PUGIXML_NO_XPATH
  5509. // STL replacements
  5510. PUGI__NS_BEGIN
  5511. struct equal_to
  5512. {
  5513. template <typename T> bool operator()(const T& lhs, const T& rhs) const
  5514. {
  5515. return lhs == rhs;
  5516. }
  5517. };
  5518. struct not_equal_to
  5519. {
  5520. template <typename T> bool operator()(const T& lhs, const T& rhs) const
  5521. {
  5522. return lhs != rhs;
  5523. }
  5524. };
  5525. struct less
  5526. {
  5527. template <typename T> bool operator()(const T& lhs, const T& rhs) const
  5528. {
  5529. return lhs < rhs;
  5530. }
  5531. };
  5532. struct less_equal
  5533. {
  5534. template <typename T> bool operator()(const T& lhs, const T& rhs) const
  5535. {
  5536. return lhs <= rhs;
  5537. }
  5538. };
  5539. template <typename T> void swap(T& lhs, T& rhs)
  5540. {
  5541. T temp = lhs;
  5542. lhs = rhs;
  5543. rhs = temp;
  5544. }
  5545. template <typename I, typename Pred> I min_element(I begin, I end, const Pred& pred)
  5546. {
  5547. I result = begin;
  5548. for (I it = begin + 1; it != end; ++it)
  5549. if (pred(*it, *result))
  5550. result = it;
  5551. return result;
  5552. }
  5553. template <typename I> void reverse(I begin, I end)
  5554. {
  5555. while (end - begin > 1) swap(*begin++, *--end);
  5556. }
  5557. template <typename I> I unique(I begin, I end)
  5558. {
  5559. // fast skip head
  5560. while (end - begin > 1 && *begin != *(begin + 1)) begin++;
  5561. if (begin == end) return begin;
  5562. // last written element
  5563. I write = begin++;
  5564. // merge unique elements
  5565. while (begin != end)
  5566. {
  5567. if (*begin != *write)
  5568. *++write = *begin++;
  5569. else
  5570. begin++;
  5571. }
  5572. // past-the-end (write points to live element)
  5573. return write + 1;
  5574. }
  5575. template <typename I> void copy_backwards(I begin, I end, I target)
  5576. {
  5577. while (begin != end) *--target = *--end;
  5578. }
  5579. template <typename I, typename Pred, typename T> void insertion_sort(I begin, I end, const Pred& pred, T*)
  5580. {
  5581. assert(begin != end);
  5582. for (I it = begin + 1; it != end; ++it)
  5583. {
  5584. T val = *it;
  5585. if (pred(val, *begin))
  5586. {
  5587. // move to front
  5588. copy_backwards(begin, it, it + 1);
  5589. *begin = val;
  5590. }
  5591. else
  5592. {
  5593. I hole = it;
  5594. // move hole backwards
  5595. while (pred(val, *(hole - 1)))
  5596. {
  5597. *hole = *(hole - 1);
  5598. hole--;
  5599. }
  5600. // fill hole with element
  5601. *hole = val;
  5602. }
  5603. }
  5604. }
  5605. // std variant for elements with ==
  5606. template <typename I, typename Pred> void partition(I begin, I middle, I end, const Pred& pred, I* out_eqbeg, I* out_eqend)
  5607. {
  5608. I eqbeg = middle, eqend = middle + 1;
  5609. // expand equal range
  5610. while (eqbeg != begin && *(eqbeg - 1) == *eqbeg) --eqbeg;
  5611. while (eqend != end && *eqend == *eqbeg) ++eqend;
  5612. // process outer elements
  5613. I ltend = eqbeg, gtbeg = eqend;
  5614. for (;;)
  5615. {
  5616. // find the element from the right side that belongs to the left one
  5617. for (; gtbeg != end; ++gtbeg)
  5618. if (!pred(*eqbeg, *gtbeg))
  5619. {
  5620. if (*gtbeg == *eqbeg) swap(*gtbeg, *eqend++);
  5621. else break;
  5622. }
  5623. // find the element from the left side that belongs to the right one
  5624. for (; ltend != begin; --ltend)
  5625. if (!pred(*(ltend - 1), *eqbeg))
  5626. {
  5627. if (*eqbeg == *(ltend - 1)) swap(*(ltend - 1), *--eqbeg);
  5628. else break;
  5629. }
  5630. // scanned all elements
  5631. if (gtbeg == end && ltend == begin)
  5632. {
  5633. *out_eqbeg = eqbeg;
  5634. *out_eqend = eqend;
  5635. return;
  5636. }
  5637. // make room for elements by moving equal area
  5638. if (gtbeg == end)
  5639. {
  5640. if (--ltend != --eqbeg) swap(*ltend, *eqbeg);
  5641. swap(*eqbeg, *--eqend);
  5642. }
  5643. else if (ltend == begin)
  5644. {
  5645. if (eqend != gtbeg) swap(*eqbeg, *eqend);
  5646. ++eqend;
  5647. swap(*gtbeg++, *eqbeg++);
  5648. }
  5649. else swap(*gtbeg++, *--ltend);
  5650. }
  5651. }
  5652. template <typename I, typename Pred> void median3(I first, I middle, I last, const Pred& pred)
  5653. {
  5654. if (pred(*middle, *first)) swap(*middle, *first);
  5655. if (pred(*last, *middle)) swap(*last, *middle);
  5656. if (pred(*middle, *first)) swap(*middle, *first);
  5657. }
  5658. template <typename I, typename Pred> void median(I first, I middle, I last, const Pred& pred)
  5659. {
  5660. if (last - first <= 40)
  5661. {
  5662. // median of three for small chunks
  5663. median3(first, middle, last, pred);
  5664. }
  5665. else
  5666. {
  5667. // median of nine
  5668. size_t step = (last - first + 1) / 8;
  5669. median3(first, first + step, first + 2 * step, pred);
  5670. median3(middle - step, middle, middle + step, pred);
  5671. median3(last - 2 * step, last - step, last, pred);
  5672. median3(first + step, middle, last - step, pred);
  5673. }
  5674. }
  5675. template <typename I, typename Pred> void sort(I begin, I end, const Pred& pred)
  5676. {
  5677. // sort large chunks
  5678. while (end - begin > 32)
  5679. {
  5680. // find median element
  5681. I middle = begin + (end - begin) / 2;
  5682. median(begin, middle, end - 1, pred);
  5683. // partition in three chunks (< = >)
  5684. I eqbeg, eqend;
  5685. partition(begin, middle, end, pred, &eqbeg, &eqend);
  5686. // loop on larger half
  5687. if (eqbeg - begin > end - eqend)
  5688. {
  5689. sort(eqend, end, pred);
  5690. end = eqbeg;
  5691. }
  5692. else
  5693. {
  5694. sort(begin, eqbeg, pred);
  5695. begin = eqend;
  5696. }
  5697. }
  5698. // insertion sort small chunk
  5699. if (begin != end) insertion_sort(begin, end, pred, &*begin);
  5700. }
  5701. PUGI__NS_END
  5702. // Allocator used for AST and evaluation stacks
  5703. PUGI__NS_BEGIN
  5704. static const size_t xpath_memory_page_size =
  5705. #ifdef PUGIXML_MEMORY_XPATH_PAGE_SIZE
  5706. PUGIXML_MEMORY_XPATH_PAGE_SIZE
  5707. #else
  5708. 4096
  5709. #endif
  5710. ;
  5711. static const uintptr_t xpath_memory_block_alignment = sizeof(double) > sizeof(void*) ? sizeof(double) : sizeof(void*);
  5712. struct xpath_memory_block
  5713. {
  5714. xpath_memory_block* next;
  5715. size_t capacity;
  5716. union
  5717. {
  5718. char data[xpath_memory_page_size];
  5719. double alignment;
  5720. };
  5721. };
  5722. class xpath_allocator
  5723. {
  5724. xpath_memory_block* _root;
  5725. size_t _root_size;
  5726. public:
  5727. #ifdef PUGIXML_NO_EXCEPTIONS
  5728. jmp_buf* error_handler;
  5729. #endif
  5730. xpath_allocator(xpath_memory_block* root, size_t root_size = 0): _root(root), _root_size(root_size)
  5731. {
  5732. #ifdef PUGIXML_NO_EXCEPTIONS
  5733. error_handler = 0;
  5734. #endif
  5735. }
  5736. void* allocate_nothrow(size_t size)
  5737. {
  5738. // round size up to block alignment boundary
  5739. size = (size + xpath_memory_block_alignment - 1) & ~(xpath_memory_block_alignment - 1);
  5740. if (_root_size + size <= _root->capacity)
  5741. {
  5742. void* buf = &_root->data[0] + _root_size;
  5743. _root_size += size;
  5744. return buf;
  5745. }
  5746. else
  5747. {
  5748. // make sure we have at least 1/4th of the page free after allocation to satisfy subsequent allocation requests
  5749. size_t block_capacity_base = sizeof(_root->data);
  5750. size_t block_capacity_req = size + block_capacity_base / 4;
  5751. size_t block_capacity = (block_capacity_base > block_capacity_req) ? block_capacity_base : block_capacity_req;
  5752. size_t block_size = block_capacity + offsetof(xpath_memory_block, data);
  5753. xpath_memory_block* block = static_cast<xpath_memory_block*>(xml_memory::allocate(block_size));
  5754. if (!block) return 0;
  5755. block->next = _root;
  5756. block->capacity = block_capacity;
  5757. _root = block;
  5758. _root_size = size;
  5759. return block->data;
  5760. }
  5761. }
  5762. void* allocate(size_t size)
  5763. {
  5764. void* result = allocate_nothrow(size);
  5765. if (!result)
  5766. {
  5767. #ifdef PUGIXML_NO_EXCEPTIONS
  5768. assert(error_handler);
  5769. longjmp(*error_handler, 1);
  5770. #else
  5771. throw std::bad_alloc();
  5772. #endif
  5773. }
  5774. return result;
  5775. }
  5776. void* reallocate(void* ptr, size_t old_size, size_t new_size)
  5777. {
  5778. // round size up to block alignment boundary
  5779. old_size = (old_size + xpath_memory_block_alignment - 1) & ~(xpath_memory_block_alignment - 1);
  5780. new_size = (new_size + xpath_memory_block_alignment - 1) & ~(xpath_memory_block_alignment - 1);
  5781. // we can only reallocate the last object
  5782. assert(ptr == 0 || static_cast<char*>(ptr) + old_size == &_root->data[0] + _root_size);
  5783. // adjust root size so that we have not allocated the object at all
  5784. bool only_object = (_root_size == old_size);
  5785. if (ptr) _root_size -= old_size;
  5786. // allocate a new version (this will obviously reuse the memory if possible)
  5787. void* result = allocate(new_size);
  5788. assert(result);
  5789. // we have a new block
  5790. if (result != ptr && ptr)
  5791. {
  5792. // copy old data
  5793. assert(new_size >= old_size);
  5794. memcpy(result, ptr, old_size);
  5795. // free the previous page if it had no other objects
  5796. if (only_object)
  5797. {
  5798. assert(_root->data == result);
  5799. assert(_root->next);
  5800. xpath_memory_block* next = _root->next->next;
  5801. if (next)
  5802. {
  5803. // deallocate the whole page, unless it was the first one
  5804. xml_memory::deallocate(_root->next);
  5805. _root->next = next;
  5806. }
  5807. }
  5808. }
  5809. return result;
  5810. }
  5811. void revert(const xpath_allocator& state)
  5812. {
  5813. // free all new pages
  5814. xpath_memory_block* cur = _root;
  5815. while (cur != state._root)
  5816. {
  5817. xpath_memory_block* next = cur->next;
  5818. xml_memory::deallocate(cur);
  5819. cur = next;
  5820. }
  5821. // restore state
  5822. _root = state._root;
  5823. _root_size = state._root_size;
  5824. }
  5825. void release()
  5826. {
  5827. xpath_memory_block* cur = _root;
  5828. assert(cur);
  5829. while (cur->next)
  5830. {
  5831. xpath_memory_block* next = cur->next;
  5832. xml_memory::deallocate(cur);
  5833. cur = next;
  5834. }
  5835. }
  5836. };
  5837. struct xpath_allocator_capture
  5838. {
  5839. xpath_allocator_capture(xpath_allocator* alloc): _target(alloc), _state(*alloc)
  5840. {
  5841. }
  5842. ~xpath_allocator_capture()
  5843. {
  5844. _target->revert(_state);
  5845. }
  5846. xpath_allocator* _target;
  5847. xpath_allocator _state;
  5848. };
  5849. struct xpath_stack
  5850. {
  5851. xpath_allocator* result;
  5852. xpath_allocator* temp;
  5853. };
  5854. struct xpath_stack_data
  5855. {
  5856. xpath_memory_block blocks[2];
  5857. xpath_allocator result;
  5858. xpath_allocator temp;
  5859. xpath_stack stack;
  5860. #ifdef PUGIXML_NO_EXCEPTIONS
  5861. jmp_buf error_handler;
  5862. #endif
  5863. xpath_stack_data(): result(blocks + 0), temp(blocks + 1)
  5864. {
  5865. blocks[0].next = blocks[1].next = 0;
  5866. blocks[0].capacity = blocks[1].capacity = sizeof(blocks[0].data);
  5867. stack.result = &result;
  5868. stack.temp = &temp;
  5869. #ifdef PUGIXML_NO_EXCEPTIONS
  5870. result.error_handler = temp.error_handler = &error_handler;
  5871. #endif
  5872. }
  5873. ~xpath_stack_data()
  5874. {
  5875. result.release();
  5876. temp.release();
  5877. }
  5878. };
  5879. PUGI__NS_END
  5880. // String class
  5881. PUGI__NS_BEGIN
  5882. class xpath_string
  5883. {
  5884. const char_t* _buffer;
  5885. bool _uses_heap;
  5886. size_t _length_heap;
  5887. static char_t* duplicate_string(const char_t* string, size_t length, xpath_allocator* alloc)
  5888. {
  5889. char_t* result = static_cast<char_t*>(alloc->allocate((length + 1) * sizeof(char_t)));
  5890. assert(result);
  5891. memcpy(result, string, length * sizeof(char_t));
  5892. result[length] = 0;
  5893. return result;
  5894. }
  5895. xpath_string(const char_t* buffer, bool uses_heap_, size_t length_heap): _buffer(buffer), _uses_heap(uses_heap_), _length_heap(length_heap)
  5896. {
  5897. }
  5898. public:
  5899. static xpath_string from_const(const char_t* str)
  5900. {
  5901. return xpath_string(str, false, 0);
  5902. }
  5903. static xpath_string from_heap_preallocated(const char_t* begin, const char_t* end)
  5904. {
  5905. assert(begin <= end && *end == 0);
  5906. return xpath_string(begin, true, static_cast<size_t>(end - begin));
  5907. }
  5908. static xpath_string from_heap(const char_t* begin, const char_t* end, xpath_allocator* alloc)
  5909. {
  5910. assert(begin <= end);
  5911. size_t length = static_cast<size_t>(end - begin);
  5912. return length == 0 ? xpath_string() : xpath_string(duplicate_string(begin, length, alloc), true, length);
  5913. }
  5914. xpath_string(): _buffer(PUGIXML_TEXT("")), _uses_heap(false), _length_heap(0)
  5915. {
  5916. }
  5917. void append(const xpath_string& o, xpath_allocator* alloc)
  5918. {
  5919. // skip empty sources
  5920. if (!*o._buffer) return;
  5921. // fast append for constant empty target and constant source
  5922. if (!*_buffer && !_uses_heap && !o._uses_heap)
  5923. {
  5924. _buffer = o._buffer;
  5925. }
  5926. else
  5927. {
  5928. // need to make heap copy
  5929. size_t target_length = length();
  5930. size_t source_length = o.length();
  5931. size_t result_length = target_length + source_length;
  5932. // allocate new buffer
  5933. char_t* result = static_cast<char_t*>(alloc->reallocate(_uses_heap ? const_cast<char_t*>(_buffer) : 0, (target_length + 1) * sizeof(char_t), (result_length + 1) * sizeof(char_t)));
  5934. assert(result);
  5935. // append first string to the new buffer in case there was no reallocation
  5936. if (!_uses_heap) memcpy(result, _buffer, target_length * sizeof(char_t));
  5937. // append second string to the new buffer
  5938. memcpy(result + target_length, o._buffer, source_length * sizeof(char_t));
  5939. result[result_length] = 0;
  5940. // finalize
  5941. _buffer = result;
  5942. _uses_heap = true;
  5943. _length_heap = result_length;
  5944. }
  5945. }
  5946. const char_t* c_str() const
  5947. {
  5948. return _buffer;
  5949. }
  5950. size_t length() const
  5951. {
  5952. return _uses_heap ? _length_heap : strlength(_buffer);
  5953. }
  5954. char_t* data(xpath_allocator* alloc)
  5955. {
  5956. // make private heap copy
  5957. if (!_uses_heap)
  5958. {
  5959. size_t length_ = strlength(_buffer);
  5960. _buffer = duplicate_string(_buffer, length_, alloc);
  5961. _uses_heap = true;
  5962. _length_heap = length_;
  5963. }
  5964. return const_cast<char_t*>(_buffer);
  5965. }
  5966. bool empty() const
  5967. {
  5968. return *_buffer == 0;
  5969. }
  5970. bool operator==(const xpath_string& o) const
  5971. {
  5972. return strequal(_buffer, o._buffer);
  5973. }
  5974. bool operator!=(const xpath_string& o) const
  5975. {
  5976. return !strequal(_buffer, o._buffer);
  5977. }
  5978. bool uses_heap() const
  5979. {
  5980. return _uses_heap;
  5981. }
  5982. };
  5983. PUGI__NS_END
  5984. PUGI__NS_BEGIN
  5985. PUGI__FN bool starts_with(const char_t* string, const char_t* pattern)
  5986. {
  5987. while (*pattern && *string == *pattern)
  5988. {
  5989. string++;
  5990. pattern++;
  5991. }
  5992. return *pattern == 0;
  5993. }
  5994. PUGI__FN const char_t* find_char(const char_t* s, char_t c)
  5995. {
  5996. #ifdef PUGIXML_WCHAR_MODE
  5997. return wcschr(s, c);
  5998. #else
  5999. return strchr(s, c);
  6000. #endif
  6001. }
  6002. PUGI__FN const char_t* find_substring(const char_t* s, const char_t* p)
  6003. {
  6004. #ifdef PUGIXML_WCHAR_MODE
  6005. // MSVC6 wcsstr bug workaround (if s is empty it always returns 0)
  6006. return (*p == 0) ? s : wcsstr(s, p);
  6007. #else
  6008. return strstr(s, p);
  6009. #endif
  6010. }
  6011. // Converts symbol to lower case, if it is an ASCII one
  6012. PUGI__FN char_t tolower_ascii(char_t ch)
  6013. {
  6014. return static_cast<unsigned int>(ch - 'A') < 26 ? static_cast<char_t>(ch | ' ') : ch;
  6015. }
  6016. PUGI__FN xpath_string string_value(const xpath_node& na, xpath_allocator* alloc)
  6017. {
  6018. if (na.attribute())
  6019. return xpath_string::from_const(na.attribute().value());
  6020. else
  6021. {
  6022. xml_node n = na.node();
  6023. switch (n.type())
  6024. {
  6025. case node_pcdata:
  6026. case node_cdata:
  6027. case node_comment:
  6028. case node_pi:
  6029. return xpath_string::from_const(n.value());
  6030. case node_document:
  6031. case node_element:
  6032. {
  6033. xpath_string result;
  6034. xml_node cur = n.first_child();
  6035. while (cur && cur != n)
  6036. {
  6037. if (cur.type() == node_pcdata || cur.type() == node_cdata)
  6038. result.append(xpath_string::from_const(cur.value()), alloc);
  6039. if (cur.first_child())
  6040. cur = cur.first_child();
  6041. else if (cur.next_sibling())
  6042. cur = cur.next_sibling();
  6043. else
  6044. {
  6045. while (!cur.next_sibling() && cur != n)
  6046. cur = cur.parent();
  6047. if (cur != n) cur = cur.next_sibling();
  6048. }
  6049. }
  6050. return result;
  6051. }
  6052. default:
  6053. return xpath_string();
  6054. }
  6055. }
  6056. }
  6057. PUGI__FN bool node_is_before_sibling(xml_node_struct* ln, xml_node_struct* rn)
  6058. {
  6059. assert(ln->parent == rn->parent);
  6060. // there is no common ancestor (the shared parent is null), nodes are from different documents
  6061. if (!ln->parent) return ln < rn;
  6062. // determine sibling order
  6063. xml_node_struct* ls = ln;
  6064. xml_node_struct* rs = rn;
  6065. while (ls && rs)
  6066. {
  6067. if (ls == rn) return true;
  6068. if (rs == ln) return false;
  6069. ls = ls->next_sibling;
  6070. rs = rs->next_sibling;
  6071. }
  6072. // if rn sibling chain ended ln must be before rn
  6073. return !rs;
  6074. }
  6075. PUGI__FN bool node_is_before(xml_node_struct* ln, xml_node_struct* rn)
  6076. {
  6077. // find common ancestor at the same depth, if any
  6078. xml_node_struct* lp = ln;
  6079. xml_node_struct* rp = rn;
  6080. while (lp && rp && lp->parent != rp->parent)
  6081. {
  6082. lp = lp->parent;
  6083. rp = rp->parent;
  6084. }
  6085. // parents are the same!
  6086. if (lp && rp) return node_is_before_sibling(lp, rp);
  6087. // nodes are at different depths, need to normalize heights
  6088. bool left_higher = !lp;
  6089. while (lp)
  6090. {
  6091. lp = lp->parent;
  6092. ln = ln->parent;
  6093. }
  6094. while (rp)
  6095. {
  6096. rp = rp->parent;
  6097. rn = rn->parent;
  6098. }
  6099. // one node is the ancestor of the other
  6100. if (ln == rn) return left_higher;
  6101. // find common ancestor... again
  6102. while (ln->parent != rn->parent)
  6103. {
  6104. ln = ln->parent;
  6105. rn = rn->parent;
  6106. }
  6107. return node_is_before_sibling(ln, rn);
  6108. }
  6109. PUGI__FN bool node_is_ancestor(xml_node_struct* parent, xml_node_struct* node)
  6110. {
  6111. while (node && node != parent) node = node->parent;
  6112. return parent && node == parent;
  6113. }
  6114. PUGI__FN const void* document_buffer_order(const xpath_node& xnode)
  6115. {
  6116. xml_node_struct* node = xnode.node().internal_object();
  6117. if (node)
  6118. {
  6119. if ((get_document(node).header & xml_memory_page_contents_shared_mask) == 0)
  6120. {
  6121. if (node->name && (node->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0) return node->name;
  6122. if (node->value && (node->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0) return node->value;
  6123. }
  6124. return 0;
  6125. }
  6126. xml_attribute_struct* attr = xnode.attribute().internal_object();
  6127. if (attr)
  6128. {
  6129. if ((get_document(attr).header & xml_memory_page_contents_shared_mask) == 0)
  6130. {
  6131. if ((attr->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0) return attr->name;
  6132. if ((attr->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0) return attr->value;
  6133. }
  6134. return 0;
  6135. }
  6136. return 0;
  6137. }
  6138. struct document_order_comparator
  6139. {
  6140. bool operator()(const xpath_node& lhs, const xpath_node& rhs) const
  6141. {
  6142. // optimized document order based check
  6143. const void* lo = document_buffer_order(lhs);
  6144. const void* ro = document_buffer_order(rhs);
  6145. if (lo && ro) return lo < ro;
  6146. // slow comparison
  6147. xml_node ln = lhs.node(), rn = rhs.node();
  6148. // compare attributes
  6149. if (lhs.attribute() && rhs.attribute())
  6150. {
  6151. // shared parent
  6152. if (lhs.parent() == rhs.parent())
  6153. {
  6154. // determine sibling order
  6155. for (xml_attribute a = lhs.attribute(); a; a = a.next_attribute())
  6156. if (a == rhs.attribute())
  6157. return true;
  6158. return false;
  6159. }
  6160. // compare attribute parents
  6161. ln = lhs.parent();
  6162. rn = rhs.parent();
  6163. }
  6164. else if (lhs.attribute())
  6165. {
  6166. // attributes go after the parent element
  6167. if (lhs.parent() == rhs.node()) return false;
  6168. ln = lhs.parent();
  6169. }
  6170. else if (rhs.attribute())
  6171. {
  6172. // attributes go after the parent element
  6173. if (rhs.parent() == lhs.node()) return true;
  6174. rn = rhs.parent();
  6175. }
  6176. if (ln == rn) return false;
  6177. if (!ln || !rn) return ln < rn;
  6178. return node_is_before(ln.internal_object(), rn.internal_object());
  6179. }
  6180. };
  6181. struct duplicate_comparator
  6182. {
  6183. bool operator()(const xpath_node& lhs, const xpath_node& rhs) const
  6184. {
  6185. if (lhs.attribute()) return rhs.attribute() ? lhs.attribute() < rhs.attribute() : true;
  6186. else return rhs.attribute() ? false : lhs.node() < rhs.node();
  6187. }
  6188. };
  6189. PUGI__FN double gen_nan()
  6190. {
  6191. #if defined(__STDC_IEC_559__) || ((FLT_RADIX - 0 == 2) && (FLT_MAX_EXP - 0 == 128) && (FLT_MANT_DIG - 0 == 24))
  6192. union { float f; uint32_t i; } u[sizeof(float) == sizeof(uint32_t) ? 1 : -1];
  6193. u[0].i = 0x7fc00000;
  6194. return u[0].f;
  6195. #else
  6196. // fallback
  6197. const volatile double zero = 0.0;
  6198. return zero / zero;
  6199. #endif
  6200. }
  6201. PUGI__FN bool is_nan(double value)
  6202. {
  6203. #if defined(PUGI__MSVC_CRT_VERSION) || defined(__BORLANDC__)
  6204. return !!_isnan(value);
  6205. #elif defined(fpclassify) && defined(FP_NAN)
  6206. return fpclassify(value) == FP_NAN;
  6207. #else
  6208. // fallback
  6209. const volatile double v = value;
  6210. return v != v;
  6211. #endif
  6212. }
  6213. PUGI__FN const char_t* convert_number_to_string_special(double value)
  6214. {
  6215. #if defined(PUGI__MSVC_CRT_VERSION) || defined(__BORLANDC__)
  6216. if (_finite(value)) return (value == 0) ? PUGIXML_TEXT("0") : 0;
  6217. if (_isnan(value)) return PUGIXML_TEXT("NaN");
  6218. return value > 0 ? PUGIXML_TEXT("Infinity") : PUGIXML_TEXT("-Infinity");
  6219. #elif defined(fpclassify) && defined(FP_NAN) && defined(FP_INFINITE) && defined(FP_ZERO)
  6220. switch (fpclassify(value))
  6221. {
  6222. case FP_NAN:
  6223. return PUGIXML_TEXT("NaN");
  6224. case FP_INFINITE:
  6225. return value > 0 ? PUGIXML_TEXT("Infinity") : PUGIXML_TEXT("-Infinity");
  6226. case FP_ZERO:
  6227. return PUGIXML_TEXT("0");
  6228. default:
  6229. return 0;
  6230. }
  6231. #else
  6232. // fallback
  6233. const volatile double v = value;
  6234. if (v == 0) return PUGIXML_TEXT("0");
  6235. if (v != v) return PUGIXML_TEXT("NaN");
  6236. if (v * 2 == v) return value > 0 ? PUGIXML_TEXT("Infinity") : PUGIXML_TEXT("-Infinity");
  6237. return 0;
  6238. #endif
  6239. }
  6240. PUGI__FN bool convert_number_to_boolean(double value)
  6241. {
  6242. return (value != 0 && !is_nan(value));
  6243. }
  6244. PUGI__FN void truncate_zeros(char* begin, char* end)
  6245. {
  6246. while (begin != end && end[-1] == '0') end--;
  6247. *end = 0;
  6248. }
  6249. // gets mantissa digits in the form of 0.xxxxx with 0. implied and the exponent
  6250. #if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400 && !defined(_WIN32_WCE)
  6251. PUGI__FN void convert_number_to_mantissa_exponent(double value, char* buffer, size_t buffer_size, char** out_mantissa, int* out_exponent)
  6252. {
  6253. // get base values
  6254. int sign, exponent;
  6255. _ecvt_s(buffer, buffer_size, value, DBL_DIG + 1, &exponent, &sign);
  6256. // truncate redundant zeros
  6257. truncate_zeros(buffer, buffer + strlen(buffer));
  6258. // fill results
  6259. *out_mantissa = buffer;
  6260. *out_exponent = exponent;
  6261. }
  6262. #else
  6263. PUGI__FN void convert_number_to_mantissa_exponent(double value, char* buffer, size_t buffer_size, char** out_mantissa, int* out_exponent)
  6264. {
  6265. // get a scientific notation value with IEEE DBL_DIG decimals
  6266. sprintf(buffer, "%.*e", DBL_DIG, value);
  6267. assert(strlen(buffer) < buffer_size);
  6268. (void)!buffer_size;
  6269. // get the exponent (possibly negative)
  6270. char* exponent_string = strchr(buffer, 'e');
  6271. assert(exponent_string);
  6272. int exponent = atoi(exponent_string + 1);
  6273. // extract mantissa string: skip sign
  6274. char* mantissa = buffer[0] == '-' ? buffer + 1 : buffer;
  6275. assert(mantissa[0] != '0' && mantissa[1] == '.');
  6276. // divide mantissa by 10 to eliminate integer part
  6277. mantissa[1] = mantissa[0];
  6278. mantissa++;
  6279. exponent++;
  6280. // remove extra mantissa digits and zero-terminate mantissa
  6281. truncate_zeros(mantissa, exponent_string);
  6282. // fill results
  6283. *out_mantissa = mantissa;
  6284. *out_exponent = exponent;
  6285. }
  6286. #endif
  6287. PUGI__FN xpath_string convert_number_to_string(double value, xpath_allocator* alloc)
  6288. {
  6289. // try special number conversion
  6290. const char_t* special = convert_number_to_string_special(value);
  6291. if (special) return xpath_string::from_const(special);
  6292. // get mantissa + exponent form
  6293. char mantissa_buffer[32];
  6294. char* mantissa;
  6295. int exponent;
  6296. convert_number_to_mantissa_exponent(value, mantissa_buffer, sizeof(mantissa_buffer), &mantissa, &exponent);
  6297. // allocate a buffer of suitable length for the number
  6298. size_t result_size = strlen(mantissa_buffer) + (exponent > 0 ? exponent : -exponent) + 4;
  6299. char_t* result = static_cast<char_t*>(alloc->allocate(sizeof(char_t) * result_size));
  6300. assert(result);
  6301. // make the number!
  6302. char_t* s = result;
  6303. // sign
  6304. if (value < 0) *s++ = '-';
  6305. // integer part
  6306. if (exponent <= 0)
  6307. {
  6308. *s++ = '0';
  6309. }
  6310. else
  6311. {
  6312. while (exponent > 0)
  6313. {
  6314. assert(*mantissa == 0 || static_cast<unsigned int>(static_cast<unsigned int>(*mantissa) - '0') <= 9);
  6315. *s++ = *mantissa ? *mantissa++ : '0';
  6316. exponent--;
  6317. }
  6318. }
  6319. // fractional part
  6320. if (*mantissa)
  6321. {
  6322. // decimal point
  6323. *s++ = '.';
  6324. // extra zeroes from negative exponent
  6325. while (exponent < 0)
  6326. {
  6327. *s++ = '0';
  6328. exponent++;
  6329. }
  6330. // extra mantissa digits
  6331. while (*mantissa)
  6332. {
  6333. assert(static_cast<unsigned int>(*mantissa - '0') <= 9);
  6334. *s++ = *mantissa++;
  6335. }
  6336. }
  6337. // zero-terminate
  6338. assert(s < result + result_size);
  6339. *s = 0;
  6340. return xpath_string::from_heap_preallocated(result, s);
  6341. }
  6342. PUGI__FN bool check_string_to_number_format(const char_t* string)
  6343. {
  6344. // parse leading whitespace
  6345. while (PUGI__IS_CHARTYPE(*string, ct_space)) ++string;
  6346. // parse sign
  6347. if (*string == '-') ++string;
  6348. if (!*string) return false;
  6349. // if there is no integer part, there should be a decimal part with at least one digit
  6350. if (!PUGI__IS_CHARTYPEX(string[0], ctx_digit) && (string[0] != '.' || !PUGI__IS_CHARTYPEX(string[1], ctx_digit))) return false;
  6351. // parse integer part
  6352. while (PUGI__IS_CHARTYPEX(*string, ctx_digit)) ++string;
  6353. // parse decimal part
  6354. if (*string == '.')
  6355. {
  6356. ++string;
  6357. while (PUGI__IS_CHARTYPEX(*string, ctx_digit)) ++string;
  6358. }
  6359. // parse trailing whitespace
  6360. while (PUGI__IS_CHARTYPE(*string, ct_space)) ++string;
  6361. return *string == 0;
  6362. }
  6363. PUGI__FN double convert_string_to_number(const char_t* string)
  6364. {
  6365. // check string format
  6366. if (!check_string_to_number_format(string)) return gen_nan();
  6367. // parse string
  6368. #ifdef PUGIXML_WCHAR_MODE
  6369. return wcstod(string, 0);
  6370. #else
  6371. return strtod(string, 0);
  6372. #endif
  6373. }
  6374. PUGI__FN bool convert_string_to_number_scratch(char_t (&buffer)[32], const char_t* begin, const char_t* end, double* out_result)
  6375. {
  6376. size_t length = static_cast<size_t>(end - begin);
  6377. char_t* scratch = buffer;
  6378. if (length >= sizeof(buffer) / sizeof(buffer[0]))
  6379. {
  6380. // need to make dummy on-heap copy
  6381. scratch = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));
  6382. if (!scratch) return false;
  6383. }
  6384. // copy string to zero-terminated buffer and perform conversion
  6385. memcpy(scratch, begin, length * sizeof(char_t));
  6386. scratch[length] = 0;
  6387. *out_result = convert_string_to_number(scratch);
  6388. // free dummy buffer
  6389. if (scratch != buffer) xml_memory::deallocate(scratch);
  6390. return true;
  6391. }
  6392. PUGI__FN double round_nearest(double value)
  6393. {
  6394. return floor(value + 0.5);
  6395. }
  6396. PUGI__FN double round_nearest_nzero(double value)
  6397. {
  6398. // same as round_nearest, but returns -0 for [-0.5, -0]
  6399. // ceil is used to differentiate between +0 and -0 (we return -0 for [-0.5, -0] and +0 for +0)
  6400. return (value >= -0.5 && value <= 0) ? ceil(value) : floor(value + 0.5);
  6401. }
  6402. PUGI__FN const char_t* qualified_name(const xpath_node& node)
  6403. {
  6404. return node.attribute() ? node.attribute().name() : node.node().name();
  6405. }
  6406. PUGI__FN const char_t* local_name(const xpath_node& node)
  6407. {
  6408. const char_t* name = qualified_name(node);
  6409. const char_t* p = find_char(name, ':');
  6410. return p ? p + 1 : name;
  6411. }
  6412. struct namespace_uri_predicate
  6413. {
  6414. const char_t* prefix;
  6415. size_t prefix_length;
  6416. namespace_uri_predicate(const char_t* name)
  6417. {
  6418. const char_t* pos = find_char(name, ':');
  6419. prefix = pos ? name : 0;
  6420. prefix_length = pos ? static_cast<size_t>(pos - name) : 0;
  6421. }
  6422. bool operator()(xml_attribute a) const
  6423. {
  6424. const char_t* name = a.name();
  6425. if (!starts_with(name, PUGIXML_TEXT("xmlns"))) return false;
  6426. return prefix ? name[5] == ':' && strequalrange(name + 6, prefix, prefix_length) : name[5] == 0;
  6427. }
  6428. };
  6429. PUGI__FN const char_t* namespace_uri(xml_node node)
  6430. {
  6431. namespace_uri_predicate pred = node.name();
  6432. xml_node p = node;
  6433. while (p)
  6434. {
  6435. xml_attribute a = p.find_attribute(pred);
  6436. if (a) return a.value();
  6437. p = p.parent();
  6438. }
  6439. return PUGIXML_TEXT("");
  6440. }
  6441. PUGI__FN const char_t* namespace_uri(xml_attribute attr, xml_node parent)
  6442. {
  6443. namespace_uri_predicate pred = attr.name();
  6444. // Default namespace does not apply to attributes
  6445. if (!pred.prefix) return PUGIXML_TEXT("");
  6446. xml_node p = parent;
  6447. while (p)
  6448. {
  6449. xml_attribute a = p.find_attribute(pred);
  6450. if (a) return a.value();
  6451. p = p.parent();
  6452. }
  6453. return PUGIXML_TEXT("");
  6454. }
  6455. PUGI__FN const char_t* namespace_uri(const xpath_node& node)
  6456. {
  6457. return node.attribute() ? namespace_uri(node.attribute(), node.parent()) : namespace_uri(node.node());
  6458. }
  6459. PUGI__FN char_t* normalize_space(char_t* buffer)
  6460. {
  6461. char_t* write = buffer;
  6462. for (char_t* it = buffer; *it; )
  6463. {
  6464. char_t ch = *it++;
  6465. if (PUGI__IS_CHARTYPE(ch, ct_space))
  6466. {
  6467. // replace whitespace sequence with single space
  6468. while (PUGI__IS_CHARTYPE(*it, ct_space)) it++;
  6469. // avoid leading spaces
  6470. if (write != buffer) *write++ = ' ';
  6471. }
  6472. else *write++ = ch;
  6473. }
  6474. // remove trailing space
  6475. if (write != buffer && PUGI__IS_CHARTYPE(write[-1], ct_space)) write--;
  6476. // zero-terminate
  6477. *write = 0;
  6478. return write;
  6479. }
  6480. PUGI__FN char_t* translate(char_t* buffer, const char_t* from, const char_t* to, size_t to_length)
  6481. {
  6482. char_t* write = buffer;
  6483. while (*buffer)
  6484. {
  6485. PUGI__DMC_VOLATILE char_t ch = *buffer++;
  6486. const char_t* pos = find_char(from, ch);
  6487. if (!pos)
  6488. *write++ = ch; // do not process
  6489. else if (static_cast<size_t>(pos - from) < to_length)
  6490. *write++ = to[pos - from]; // replace
  6491. }
  6492. // zero-terminate
  6493. *write = 0;
  6494. return write;
  6495. }
  6496. PUGI__FN unsigned char* translate_table_generate(xpath_allocator* alloc, const char_t* from, const char_t* to)
  6497. {
  6498. unsigned char table[128] = {0};
  6499. while (*from)
  6500. {
  6501. unsigned int fc = static_cast<unsigned int>(*from);
  6502. unsigned int tc = static_cast<unsigned int>(*to);
  6503. if (fc >= 128 || tc >= 128)
  6504. return 0;
  6505. // code=128 means "skip character"
  6506. if (!table[fc])
  6507. table[fc] = static_cast<unsigned char>(tc ? tc : 128);
  6508. from++;
  6509. if (tc) to++;
  6510. }
  6511. for (int i = 0; i < 128; ++i)
  6512. if (!table[i])
  6513. table[i] = static_cast<unsigned char>(i);
  6514. void* result = alloc->allocate_nothrow(sizeof(table));
  6515. if (result)
  6516. {
  6517. memcpy(result, table, sizeof(table));
  6518. }
  6519. return static_cast<unsigned char*>(result);
  6520. }
  6521. PUGI__FN char_t* translate_table(char_t* buffer, const unsigned char* table)
  6522. {
  6523. char_t* write = buffer;
  6524. while (*buffer)
  6525. {
  6526. char_t ch = *buffer++;
  6527. unsigned int index = static_cast<unsigned int>(ch);
  6528. if (index < 128)
  6529. {
  6530. unsigned char code = table[index];
  6531. // code=128 means "skip character" (table size is 128 so 128 can be a special value)
  6532. // this code skips these characters without extra branches
  6533. *write = static_cast<char_t>(code);
  6534. write += 1 - (code >> 7);
  6535. }
  6536. else
  6537. {
  6538. *write++ = ch;
  6539. }
  6540. }
  6541. // zero-terminate
  6542. *write = 0;
  6543. return write;
  6544. }
  6545. inline bool is_xpath_attribute(const char_t* name)
  6546. {
  6547. return !(starts_with(name, PUGIXML_TEXT("xmlns")) && (name[5] == 0 || name[5] == ':'));
  6548. }
  6549. struct xpath_variable_boolean: xpath_variable
  6550. {
  6551. xpath_variable_boolean(): xpath_variable(xpath_type_boolean), value(false)
  6552. {
  6553. }
  6554. bool value;
  6555. char_t name[1];
  6556. };
  6557. struct xpath_variable_number: xpath_variable
  6558. {
  6559. xpath_variable_number(): xpath_variable(xpath_type_number), value(0)
  6560. {
  6561. }
  6562. double value;
  6563. char_t name[1];
  6564. };
  6565. struct xpath_variable_string: xpath_variable
  6566. {
  6567. xpath_variable_string(): xpath_variable(xpath_type_string), value(0)
  6568. {
  6569. }
  6570. ~xpath_variable_string()
  6571. {
  6572. if (value) xml_memory::deallocate(value);
  6573. }
  6574. char_t* value;
  6575. char_t name[1];
  6576. };
  6577. struct xpath_variable_node_set: xpath_variable
  6578. {
  6579. xpath_variable_node_set(): xpath_variable(xpath_type_node_set)
  6580. {
  6581. }
  6582. xpath_node_set value;
  6583. char_t name[1];
  6584. };
  6585. static const xpath_node_set dummy_node_set;
  6586. PUGI__FN unsigned int hash_string(const char_t* str)
  6587. {
  6588. // Jenkins one-at-a-time hash (http://en.wikipedia.org/wiki/Jenkins_hash_function#one-at-a-time)
  6589. unsigned int result = 0;
  6590. while (*str)
  6591. {
  6592. result += static_cast<unsigned int>(*str++);
  6593. result += result << 10;
  6594. result ^= result >> 6;
  6595. }
  6596. result += result << 3;
  6597. result ^= result >> 11;
  6598. result += result << 15;
  6599. return result;
  6600. }
  6601. template <typename T> PUGI__FN T* new_xpath_variable(const char_t* name)
  6602. {
  6603. size_t length = strlength(name);
  6604. if (length == 0) return 0; // empty variable names are invalid
  6605. // $$ we can't use offsetof(T, name) because T is non-POD, so we just allocate additional length characters
  6606. void* memory = xml_memory::allocate(sizeof(T) + length * sizeof(char_t));
  6607. if (!memory) return 0;
  6608. T* result = new (memory) T();
  6609. memcpy(result->name, name, (length + 1) * sizeof(char_t));
  6610. return result;
  6611. }
  6612. PUGI__FN xpath_variable* new_xpath_variable(xpath_value_type type, const char_t* name)
  6613. {
  6614. switch (type)
  6615. {
  6616. case xpath_type_node_set:
  6617. return new_xpath_variable<xpath_variable_node_set>(name);
  6618. case xpath_type_number:
  6619. return new_xpath_variable<xpath_variable_number>(name);
  6620. case xpath_type_string:
  6621. return new_xpath_variable<xpath_variable_string>(name);
  6622. case xpath_type_boolean:
  6623. return new_xpath_variable<xpath_variable_boolean>(name);
  6624. default:
  6625. return 0;
  6626. }
  6627. }
  6628. template <typename T> PUGI__FN void delete_xpath_variable(T* var)
  6629. {
  6630. var->~T();
  6631. xml_memory::deallocate(var);
  6632. }
  6633. PUGI__FN void delete_xpath_variable(xpath_value_type type, xpath_variable* var)
  6634. {
  6635. switch (type)
  6636. {
  6637. case xpath_type_node_set:
  6638. delete_xpath_variable(static_cast<xpath_variable_node_set*>(var));
  6639. break;
  6640. case xpath_type_number:
  6641. delete_xpath_variable(static_cast<xpath_variable_number*>(var));
  6642. break;
  6643. case xpath_type_string:
  6644. delete_xpath_variable(static_cast<xpath_variable_string*>(var));
  6645. break;
  6646. case xpath_type_boolean:
  6647. delete_xpath_variable(static_cast<xpath_variable_boolean*>(var));
  6648. break;
  6649. default:
  6650. assert(!"Invalid variable type");
  6651. }
  6652. }
  6653. PUGI__FN bool copy_xpath_variable(xpath_variable* lhs, const xpath_variable* rhs)
  6654. {
  6655. switch (rhs->type())
  6656. {
  6657. case xpath_type_node_set:
  6658. return lhs->set(static_cast<const xpath_variable_node_set*>(rhs)->value);
  6659. case xpath_type_number:
  6660. return lhs->set(static_cast<const xpath_variable_number*>(rhs)->value);
  6661. case xpath_type_string:
  6662. return lhs->set(static_cast<const xpath_variable_string*>(rhs)->value);
  6663. case xpath_type_boolean:
  6664. return lhs->set(static_cast<const xpath_variable_boolean*>(rhs)->value);
  6665. default:
  6666. assert(!"Invalid variable type");
  6667. return false;
  6668. }
  6669. }
  6670. PUGI__FN bool get_variable_scratch(char_t (&buffer)[32], xpath_variable_set* set, const char_t* begin, const char_t* end, xpath_variable** out_result)
  6671. {
  6672. size_t length = static_cast<size_t>(end - begin);
  6673. char_t* scratch = buffer;
  6674. if (length >= sizeof(buffer) / sizeof(buffer[0]))
  6675. {
  6676. // need to make dummy on-heap copy
  6677. scratch = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));
  6678. if (!scratch) return false;
  6679. }
  6680. // copy string to zero-terminated buffer and perform lookup
  6681. memcpy(scratch, begin, length * sizeof(char_t));
  6682. scratch[length] = 0;
  6683. *out_result = set->get(scratch);
  6684. // free dummy buffer
  6685. if (scratch != buffer) xml_memory::deallocate(scratch);
  6686. return true;
  6687. }
  6688. PUGI__NS_END
  6689. // Internal node set class
  6690. PUGI__NS_BEGIN
  6691. PUGI__FN xpath_node_set::type_t xpath_get_order(const xpath_node* begin, const xpath_node* end)
  6692. {
  6693. if (end - begin < 2)
  6694. return xpath_node_set::type_sorted;
  6695. document_order_comparator cmp;
  6696. bool first = cmp(begin[0], begin[1]);
  6697. for (const xpath_node* it = begin + 1; it + 1 < end; ++it)
  6698. if (cmp(it[0], it[1]) != first)
  6699. return xpath_node_set::type_unsorted;
  6700. return first ? xpath_node_set::type_sorted : xpath_node_set::type_sorted_reverse;
  6701. }
  6702. PUGI__FN xpath_node_set::type_t xpath_sort(xpath_node* begin, xpath_node* end, xpath_node_set::type_t type, bool rev)
  6703. {
  6704. xpath_node_set::type_t order = rev ? xpath_node_set::type_sorted_reverse : xpath_node_set::type_sorted;
  6705. if (type == xpath_node_set::type_unsorted)
  6706. {
  6707. xpath_node_set::type_t sorted = xpath_get_order(begin, end);
  6708. if (sorted == xpath_node_set::type_unsorted)
  6709. {
  6710. sort(begin, end, document_order_comparator());
  6711. type = xpath_node_set::type_sorted;
  6712. }
  6713. else
  6714. type = sorted;
  6715. }
  6716. if (type != order) reverse(begin, end);
  6717. return order;
  6718. }
  6719. PUGI__FN xpath_node xpath_first(const xpath_node* begin, const xpath_node* end, xpath_node_set::type_t type)
  6720. {
  6721. if (begin == end) return xpath_node();
  6722. switch (type)
  6723. {
  6724. case xpath_node_set::type_sorted:
  6725. return *begin;
  6726. case xpath_node_set::type_sorted_reverse:
  6727. return *(end - 1);
  6728. case xpath_node_set::type_unsorted:
  6729. return *min_element(begin, end, document_order_comparator());
  6730. default:
  6731. assert(!"Invalid node set type");
  6732. return xpath_node();
  6733. }
  6734. }
  6735. class xpath_node_set_raw
  6736. {
  6737. xpath_node_set::type_t _type;
  6738. xpath_node* _begin;
  6739. xpath_node* _end;
  6740. xpath_node* _eos;
  6741. public:
  6742. xpath_node_set_raw(): _type(xpath_node_set::type_unsorted), _begin(0), _end(0), _eos(0)
  6743. {
  6744. }
  6745. xpath_node* begin() const
  6746. {
  6747. return _begin;
  6748. }
  6749. xpath_node* end() const
  6750. {
  6751. return _end;
  6752. }
  6753. bool empty() const
  6754. {
  6755. return _begin == _end;
  6756. }
  6757. size_t size() const
  6758. {
  6759. return static_cast<size_t>(_end - _begin);
  6760. }
  6761. xpath_node first() const
  6762. {
  6763. return xpath_first(_begin, _end, _type);
  6764. }
  6765. void push_back_grow(const xpath_node& node, xpath_allocator* alloc);
  6766. void push_back(const xpath_node& node, xpath_allocator* alloc)
  6767. {
  6768. if (_end != _eos)
  6769. *_end++ = node;
  6770. else
  6771. push_back_grow(node, alloc);
  6772. }
  6773. void append(const xpath_node* begin_, const xpath_node* end_, xpath_allocator* alloc)
  6774. {
  6775. if (begin_ == end_) return;
  6776. size_t size_ = static_cast<size_t>(_end - _begin);
  6777. size_t capacity = static_cast<size_t>(_eos - _begin);
  6778. size_t count = static_cast<size_t>(end_ - begin_);
  6779. if (size_ + count > capacity)
  6780. {
  6781. // reallocate the old array or allocate a new one
  6782. xpath_node* data = static_cast<xpath_node*>(alloc->reallocate(_begin, capacity * sizeof(xpath_node), (size_ + count) * sizeof(xpath_node)));
  6783. assert(data);
  6784. // finalize
  6785. _begin = data;
  6786. _end = data + size_;
  6787. _eos = data + size_ + count;
  6788. }
  6789. memcpy(_end, begin_, count * sizeof(xpath_node));
  6790. _end += count;
  6791. }
  6792. void sort_do()
  6793. {
  6794. _type = xpath_sort(_begin, _end, _type, false);
  6795. }
  6796. void truncate(xpath_node* pos)
  6797. {
  6798. assert(_begin <= pos && pos <= _end);
  6799. _end = pos;
  6800. }
  6801. void remove_duplicates()
  6802. {
  6803. if (_type == xpath_node_set::type_unsorted)
  6804. sort(_begin, _end, duplicate_comparator());
  6805. _end = unique(_begin, _end);
  6806. }
  6807. xpath_node_set::type_t type() const
  6808. {
  6809. return _type;
  6810. }
  6811. void set_type(xpath_node_set::type_t value)
  6812. {
  6813. _type = value;
  6814. }
  6815. };
  6816. PUGI__FN_NO_INLINE void xpath_node_set_raw::push_back_grow(const xpath_node& node, xpath_allocator* alloc)
  6817. {
  6818. size_t capacity = static_cast<size_t>(_eos - _begin);
  6819. // get new capacity (1.5x rule)
  6820. size_t new_capacity = capacity + capacity / 2 + 1;
  6821. // reallocate the old array or allocate a new one
  6822. xpath_node* data = static_cast<xpath_node*>(alloc->reallocate(_begin, capacity * sizeof(xpath_node), new_capacity * sizeof(xpath_node)));
  6823. assert(data);
  6824. // finalize
  6825. _begin = data;
  6826. _end = data + capacity;
  6827. _eos = data + new_capacity;
  6828. // push
  6829. *_end++ = node;
  6830. }
  6831. PUGI__NS_END
  6832. PUGI__NS_BEGIN
  6833. struct xpath_context
  6834. {
  6835. xpath_node n;
  6836. size_t position, size;
  6837. xpath_context(const xpath_node& n_, size_t position_, size_t size_): n(n_), position(position_), size(size_)
  6838. {
  6839. }
  6840. };
  6841. enum lexeme_t
  6842. {
  6843. lex_none = 0,
  6844. lex_equal,
  6845. lex_not_equal,
  6846. lex_less,
  6847. lex_greater,
  6848. lex_less_or_equal,
  6849. lex_greater_or_equal,
  6850. lex_plus,
  6851. lex_minus,
  6852. lex_multiply,
  6853. lex_union,
  6854. lex_var_ref,
  6855. lex_open_brace,
  6856. lex_close_brace,
  6857. lex_quoted_string,
  6858. lex_number,
  6859. lex_slash,
  6860. lex_double_slash,
  6861. lex_open_square_brace,
  6862. lex_close_square_brace,
  6863. lex_string,
  6864. lex_comma,
  6865. lex_axis_attribute,
  6866. lex_dot,
  6867. lex_double_dot,
  6868. lex_double_colon,
  6869. lex_eof
  6870. };
  6871. struct xpath_lexer_string
  6872. {
  6873. const char_t* begin;
  6874. const char_t* end;
  6875. xpath_lexer_string(): begin(0), end(0)
  6876. {
  6877. }
  6878. bool operator==(const char_t* other) const
  6879. {
  6880. size_t length = static_cast<size_t>(end - begin);
  6881. return strequalrange(other, begin, length);
  6882. }
  6883. };
  6884. class xpath_lexer
  6885. {
  6886. const char_t* _cur;
  6887. const char_t* _cur_lexeme_pos;
  6888. xpath_lexer_string _cur_lexeme_contents;
  6889. lexeme_t _cur_lexeme;
  6890. public:
  6891. explicit xpath_lexer(const char_t* query): _cur(query)
  6892. {
  6893. next();
  6894. }
  6895. const char_t* state() const
  6896. {
  6897. return _cur;
  6898. }
  6899. void next()
  6900. {
  6901. const char_t* cur = _cur;
  6902. while (PUGI__IS_CHARTYPE(*cur, ct_space)) ++cur;
  6903. // save lexeme position for error reporting
  6904. _cur_lexeme_pos = cur;
  6905. switch (*cur)
  6906. {
  6907. case 0:
  6908. _cur_lexeme = lex_eof;
  6909. break;
  6910. case '>':
  6911. if (*(cur+1) == '=')
  6912. {
  6913. cur += 2;
  6914. _cur_lexeme = lex_greater_or_equal;
  6915. }
  6916. else
  6917. {
  6918. cur += 1;
  6919. _cur_lexeme = lex_greater;
  6920. }
  6921. break;
  6922. case '<':
  6923. if (*(cur+1) == '=')
  6924. {
  6925. cur += 2;
  6926. _cur_lexeme = lex_less_or_equal;
  6927. }
  6928. else
  6929. {
  6930. cur += 1;
  6931. _cur_lexeme = lex_less;
  6932. }
  6933. break;
  6934. case '!':
  6935. if (*(cur+1) == '=')
  6936. {
  6937. cur += 2;
  6938. _cur_lexeme = lex_not_equal;
  6939. }
  6940. else
  6941. {
  6942. _cur_lexeme = lex_none;
  6943. }
  6944. break;
  6945. case '=':
  6946. cur += 1;
  6947. _cur_lexeme = lex_equal;
  6948. break;
  6949. case '+':
  6950. cur += 1;
  6951. _cur_lexeme = lex_plus;
  6952. break;
  6953. case '-':
  6954. cur += 1;
  6955. _cur_lexeme = lex_minus;
  6956. break;
  6957. case '*':
  6958. cur += 1;
  6959. _cur_lexeme = lex_multiply;
  6960. break;
  6961. case '|':
  6962. cur += 1;
  6963. _cur_lexeme = lex_union;
  6964. break;
  6965. case '$':
  6966. cur += 1;
  6967. if (PUGI__IS_CHARTYPEX(*cur, ctx_start_symbol))
  6968. {
  6969. _cur_lexeme_contents.begin = cur;
  6970. while (PUGI__IS_CHARTYPEX(*cur, ctx_symbol)) cur++;
  6971. if (cur[0] == ':' && PUGI__IS_CHARTYPEX(cur[1], ctx_symbol)) // qname
  6972. {
  6973. cur++; // :
  6974. while (PUGI__IS_CHARTYPEX(*cur, ctx_symbol)) cur++;
  6975. }
  6976. _cur_lexeme_contents.end = cur;
  6977. _cur_lexeme = lex_var_ref;
  6978. }
  6979. else
  6980. {
  6981. _cur_lexeme = lex_none;
  6982. }
  6983. break;
  6984. case '(':
  6985. cur += 1;
  6986. _cur_lexeme = lex_open_brace;
  6987. break;
  6988. case ')':
  6989. cur += 1;
  6990. _cur_lexeme = lex_close_brace;
  6991. break;
  6992. case '[':
  6993. cur += 1;
  6994. _cur_lexeme = lex_open_square_brace;
  6995. break;
  6996. case ']':
  6997. cur += 1;
  6998. _cur_lexeme = lex_close_square_brace;
  6999. break;
  7000. case ',':
  7001. cur += 1;
  7002. _cur_lexeme = lex_comma;
  7003. break;
  7004. case '/':
  7005. if (*(cur+1) == '/')
  7006. {
  7007. cur += 2;
  7008. _cur_lexeme = lex_double_slash;
  7009. }
  7010. else
  7011. {
  7012. cur += 1;
  7013. _cur_lexeme = lex_slash;
  7014. }
  7015. break;
  7016. case '.':
  7017. if (*(cur+1) == '.')
  7018. {
  7019. cur += 2;
  7020. _cur_lexeme = lex_double_dot;
  7021. }
  7022. else if (PUGI__IS_CHARTYPEX(*(cur+1), ctx_digit))
  7023. {
  7024. _cur_lexeme_contents.begin = cur; // .
  7025. ++cur;
  7026. while (PUGI__IS_CHARTYPEX(*cur, ctx_digit)) cur++;
  7027. _cur_lexeme_contents.end = cur;
  7028. _cur_lexeme = lex_number;
  7029. }
  7030. else
  7031. {
  7032. cur += 1;
  7033. _cur_lexeme = lex_dot;
  7034. }
  7035. break;
  7036. case '@':
  7037. cur += 1;
  7038. _cur_lexeme = lex_axis_attribute;
  7039. break;
  7040. case '"':
  7041. case '\'':
  7042. {
  7043. char_t terminator = *cur;
  7044. ++cur;
  7045. _cur_lexeme_contents.begin = cur;
  7046. while (*cur && *cur != terminator) cur++;
  7047. _cur_lexeme_contents.end = cur;
  7048. if (!*cur)
  7049. _cur_lexeme = lex_none;
  7050. else
  7051. {
  7052. cur += 1;
  7053. _cur_lexeme = lex_quoted_string;
  7054. }
  7055. break;
  7056. }
  7057. case ':':
  7058. if (*(cur+1) == ':')
  7059. {
  7060. cur += 2;
  7061. _cur_lexeme = lex_double_colon;
  7062. }
  7063. else
  7064. {
  7065. _cur_lexeme = lex_none;
  7066. }
  7067. break;
  7068. default:
  7069. if (PUGI__IS_CHARTYPEX(*cur, ctx_digit))
  7070. {
  7071. _cur_lexeme_contents.begin = cur;
  7072. while (PUGI__IS_CHARTYPEX(*cur, ctx_digit)) cur++;
  7073. if (*cur == '.')
  7074. {
  7075. cur++;
  7076. while (PUGI__IS_CHARTYPEX(*cur, ctx_digit)) cur++;
  7077. }
  7078. _cur_lexeme_contents.end = cur;
  7079. _cur_lexeme = lex_number;
  7080. }
  7081. else if (PUGI__IS_CHARTYPEX(*cur, ctx_start_symbol))
  7082. {
  7083. _cur_lexeme_contents.begin = cur;
  7084. while (PUGI__IS_CHARTYPEX(*cur, ctx_symbol)) cur++;
  7085. if (cur[0] == ':')
  7086. {
  7087. if (cur[1] == '*') // namespace test ncname:*
  7088. {
  7089. cur += 2; // :*
  7090. }
  7091. else if (PUGI__IS_CHARTYPEX(cur[1], ctx_symbol)) // namespace test qname
  7092. {
  7093. cur++; // :
  7094. while (PUGI__IS_CHARTYPEX(*cur, ctx_symbol)) cur++;
  7095. }
  7096. }
  7097. _cur_lexeme_contents.end = cur;
  7098. _cur_lexeme = lex_string;
  7099. }
  7100. else
  7101. {
  7102. _cur_lexeme = lex_none;
  7103. }
  7104. }
  7105. _cur = cur;
  7106. }
  7107. lexeme_t current() const
  7108. {
  7109. return _cur_lexeme;
  7110. }
  7111. const char_t* current_pos() const
  7112. {
  7113. return _cur_lexeme_pos;
  7114. }
  7115. const xpath_lexer_string& contents() const
  7116. {
  7117. assert(_cur_lexeme == lex_var_ref || _cur_lexeme == lex_number || _cur_lexeme == lex_string || _cur_lexeme == lex_quoted_string);
  7118. return _cur_lexeme_contents;
  7119. }
  7120. };
  7121. enum ast_type_t
  7122. {
  7123. ast_unknown,
  7124. ast_op_or, // left or right
  7125. ast_op_and, // left and right
  7126. ast_op_equal, // left = right
  7127. ast_op_not_equal, // left != right
  7128. ast_op_less, // left < right
  7129. ast_op_greater, // left > right
  7130. ast_op_less_or_equal, // left <= right
  7131. ast_op_greater_or_equal, // left >= right
  7132. ast_op_add, // left + right
  7133. ast_op_subtract, // left - right
  7134. ast_op_multiply, // left * right
  7135. ast_op_divide, // left / right
  7136. ast_op_mod, // left % right
  7137. ast_op_negate, // left - right
  7138. ast_op_union, // left | right
  7139. ast_predicate, // apply predicate to set; next points to next predicate
  7140. ast_filter, // select * from left where right
  7141. ast_string_constant, // string constant
  7142. ast_number_constant, // number constant
  7143. ast_variable, // variable
  7144. ast_func_last, // last()
  7145. ast_func_position, // position()
  7146. ast_func_count, // count(left)
  7147. ast_func_id, // id(left)
  7148. ast_func_local_name_0, // local-name()
  7149. ast_func_local_name_1, // local-name(left)
  7150. ast_func_namespace_uri_0, // namespace-uri()
  7151. ast_func_namespace_uri_1, // namespace-uri(left)
  7152. ast_func_name_0, // name()
  7153. ast_func_name_1, // name(left)
  7154. ast_func_string_0, // string()
  7155. ast_func_string_1, // string(left)
  7156. ast_func_concat, // concat(left, right, siblings)
  7157. ast_func_starts_with, // starts_with(left, right)
  7158. ast_func_contains, // contains(left, right)
  7159. ast_func_substring_before, // substring-before(left, right)
  7160. ast_func_substring_after, // substring-after(left, right)
  7161. ast_func_substring_2, // substring(left, right)
  7162. ast_func_substring_3, // substring(left, right, third)
  7163. ast_func_string_length_0, // string-length()
  7164. ast_func_string_length_1, // string-length(left)
  7165. ast_func_normalize_space_0, // normalize-space()
  7166. ast_func_normalize_space_1, // normalize-space(left)
  7167. ast_func_translate, // translate(left, right, third)
  7168. ast_func_boolean, // boolean(left)
  7169. ast_func_not, // not(left)
  7170. ast_func_true, // true()
  7171. ast_func_false, // false()
  7172. ast_func_lang, // lang(left)
  7173. ast_func_number_0, // number()
  7174. ast_func_number_1, // number(left)
  7175. ast_func_sum, // sum(left)
  7176. ast_func_floor, // floor(left)
  7177. ast_func_ceiling, // ceiling(left)
  7178. ast_func_round, // round(left)
  7179. ast_step, // process set left with step
  7180. ast_step_root, // select root node
  7181. ast_opt_translate_table, // translate(left, right, third) where right/third are constants
  7182. ast_opt_compare_attribute // @name = 'string'
  7183. };
  7184. enum axis_t
  7185. {
  7186. axis_ancestor,
  7187. axis_ancestor_or_self,
  7188. axis_attribute,
  7189. axis_child,
  7190. axis_descendant,
  7191. axis_descendant_or_self,
  7192. axis_following,
  7193. axis_following_sibling,
  7194. axis_namespace,
  7195. axis_parent,
  7196. axis_preceding,
  7197. axis_preceding_sibling,
  7198. axis_self
  7199. };
  7200. enum nodetest_t
  7201. {
  7202. nodetest_none,
  7203. nodetest_name,
  7204. nodetest_type_node,
  7205. nodetest_type_comment,
  7206. nodetest_type_pi,
  7207. nodetest_type_text,
  7208. nodetest_pi,
  7209. nodetest_all,
  7210. nodetest_all_in_namespace
  7211. };
  7212. enum predicate_t
  7213. {
  7214. predicate_default,
  7215. predicate_posinv,
  7216. predicate_constant,
  7217. predicate_constant_one
  7218. };
  7219. enum nodeset_eval_t
  7220. {
  7221. nodeset_eval_all,
  7222. nodeset_eval_any,
  7223. nodeset_eval_first
  7224. };
  7225. template <axis_t N> struct axis_to_type
  7226. {
  7227. static const axis_t axis;
  7228. };
  7229. template <axis_t N> const axis_t axis_to_type<N>::axis = N;
  7230. class xpath_ast_node
  7231. {
  7232. private:
  7233. // node type
  7234. char _type;
  7235. char _rettype;
  7236. // for ast_step
  7237. char _axis;
  7238. // for ast_step/ast_predicate/ast_filter
  7239. char _test;
  7240. // tree node structure
  7241. xpath_ast_node* _left;
  7242. xpath_ast_node* _right;
  7243. xpath_ast_node* _next;
  7244. union
  7245. {
  7246. // value for ast_string_constant
  7247. const char_t* string;
  7248. // value for ast_number_constant
  7249. double number;
  7250. // variable for ast_variable
  7251. xpath_variable* variable;
  7252. // node test for ast_step (node name/namespace/node type/pi target)
  7253. const char_t* nodetest;
  7254. // table for ast_opt_translate_table
  7255. const unsigned char* table;
  7256. } _data;
  7257. xpath_ast_node(const xpath_ast_node&);
  7258. xpath_ast_node& operator=(const xpath_ast_node&);
  7259. template <class Comp> static bool compare_eq(xpath_ast_node* lhs, xpath_ast_node* rhs, const xpath_context& c, const xpath_stack& stack, const Comp& comp)
  7260. {
  7261. xpath_value_type lt = lhs->rettype(), rt = rhs->rettype();
  7262. if (lt != xpath_type_node_set && rt != xpath_type_node_set)
  7263. {
  7264. if (lt == xpath_type_boolean || rt == xpath_type_boolean)
  7265. return comp(lhs->eval_boolean(c, stack), rhs->eval_boolean(c, stack));
  7266. else if (lt == xpath_type_number || rt == xpath_type_number)
  7267. return comp(lhs->eval_number(c, stack), rhs->eval_number(c, stack));
  7268. else if (lt == xpath_type_string || rt == xpath_type_string)
  7269. {
  7270. xpath_allocator_capture cr(stack.result);
  7271. xpath_string ls = lhs->eval_string(c, stack);
  7272. xpath_string rs = rhs->eval_string(c, stack);
  7273. return comp(ls, rs);
  7274. }
  7275. }
  7276. else if (lt == xpath_type_node_set && rt == xpath_type_node_set)
  7277. {
  7278. xpath_allocator_capture cr(stack.result);
  7279. xpath_node_set_raw ls = lhs->eval_node_set(c, stack, nodeset_eval_all);
  7280. xpath_node_set_raw rs = rhs->eval_node_set(c, stack, nodeset_eval_all);
  7281. for (const xpath_node* li = ls.begin(); li != ls.end(); ++li)
  7282. for (const xpath_node* ri = rs.begin(); ri != rs.end(); ++ri)
  7283. {
  7284. xpath_allocator_capture cri(stack.result);
  7285. if (comp(string_value(*li, stack.result), string_value(*ri, stack.result)))
  7286. return true;
  7287. }
  7288. return false;
  7289. }
  7290. else
  7291. {
  7292. if (lt == xpath_type_node_set)
  7293. {
  7294. swap(lhs, rhs);
  7295. swap(lt, rt);
  7296. }
  7297. if (lt == xpath_type_boolean)
  7298. return comp(lhs->eval_boolean(c, stack), rhs->eval_boolean(c, stack));
  7299. else if (lt == xpath_type_number)
  7300. {
  7301. xpath_allocator_capture cr(stack.result);
  7302. double l = lhs->eval_number(c, stack);
  7303. xpath_node_set_raw rs = rhs->eval_node_set(c, stack, nodeset_eval_all);
  7304. for (const xpath_node* ri = rs.begin(); ri != rs.end(); ++ri)
  7305. {
  7306. xpath_allocator_capture cri(stack.result);
  7307. if (comp(l, convert_string_to_number(string_value(*ri, stack.result).c_str())))
  7308. return true;
  7309. }
  7310. return false;
  7311. }
  7312. else if (lt == xpath_type_string)
  7313. {
  7314. xpath_allocator_capture cr(stack.result);
  7315. xpath_string l = lhs->eval_string(c, stack);
  7316. xpath_node_set_raw rs = rhs->eval_node_set(c, stack, nodeset_eval_all);
  7317. for (const xpath_node* ri = rs.begin(); ri != rs.end(); ++ri)
  7318. {
  7319. xpath_allocator_capture cri(stack.result);
  7320. if (comp(l, string_value(*ri, stack.result)))
  7321. return true;
  7322. }
  7323. return false;
  7324. }
  7325. }
  7326. assert(!"Wrong types");
  7327. return false;
  7328. }
  7329. static bool eval_once(xpath_node_set::type_t type, nodeset_eval_t eval)
  7330. {
  7331. return type == xpath_node_set::type_sorted ? eval != nodeset_eval_all : eval == nodeset_eval_any;
  7332. }
  7333. template <class Comp> static bool compare_rel(xpath_ast_node* lhs, xpath_ast_node* rhs, const xpath_context& c, const xpath_stack& stack, const Comp& comp)
  7334. {
  7335. xpath_value_type lt = lhs->rettype(), rt = rhs->rettype();
  7336. if (lt != xpath_type_node_set && rt != xpath_type_node_set)
  7337. return comp(lhs->eval_number(c, stack), rhs->eval_number(c, stack));
  7338. else if (lt == xpath_type_node_set && rt == xpath_type_node_set)
  7339. {
  7340. xpath_allocator_capture cr(stack.result);
  7341. xpath_node_set_raw ls = lhs->eval_node_set(c, stack, nodeset_eval_all);
  7342. xpath_node_set_raw rs = rhs->eval_node_set(c, stack, nodeset_eval_all);
  7343. for (const xpath_node* li = ls.begin(); li != ls.end(); ++li)
  7344. {
  7345. xpath_allocator_capture cri(stack.result);
  7346. double l = convert_string_to_number(string_value(*li, stack.result).c_str());
  7347. for (const xpath_node* ri = rs.begin(); ri != rs.end(); ++ri)
  7348. {
  7349. xpath_allocator_capture crii(stack.result);
  7350. if (comp(l, convert_string_to_number(string_value(*ri, stack.result).c_str())))
  7351. return true;
  7352. }
  7353. }
  7354. return false;
  7355. }
  7356. else if (lt != xpath_type_node_set && rt == xpath_type_node_set)
  7357. {
  7358. xpath_allocator_capture cr(stack.result);
  7359. double l = lhs->eval_number(c, stack);
  7360. xpath_node_set_raw rs = rhs->eval_node_set(c, stack, nodeset_eval_all);
  7361. for (const xpath_node* ri = rs.begin(); ri != rs.end(); ++ri)
  7362. {
  7363. xpath_allocator_capture cri(stack.result);
  7364. if (comp(l, convert_string_to_number(string_value(*ri, stack.result).c_str())))
  7365. return true;
  7366. }
  7367. return false;
  7368. }
  7369. else if (lt == xpath_type_node_set && rt != xpath_type_node_set)
  7370. {
  7371. xpath_allocator_capture cr(stack.result);
  7372. xpath_node_set_raw ls = lhs->eval_node_set(c, stack, nodeset_eval_all);
  7373. double r = rhs->eval_number(c, stack);
  7374. for (const xpath_node* li = ls.begin(); li != ls.end(); ++li)
  7375. {
  7376. xpath_allocator_capture cri(stack.result);
  7377. if (comp(convert_string_to_number(string_value(*li, stack.result).c_str()), r))
  7378. return true;
  7379. }
  7380. return false;
  7381. }
  7382. else
  7383. {
  7384. assert(!"Wrong types");
  7385. return false;
  7386. }
  7387. }
  7388. static void apply_predicate_boolean(xpath_node_set_raw& ns, size_t first, xpath_ast_node* expr, const xpath_stack& stack, bool once)
  7389. {
  7390. assert(ns.size() >= first);
  7391. assert(expr->rettype() != xpath_type_number);
  7392. size_t i = 1;
  7393. size_t size = ns.size() - first;
  7394. xpath_node* last = ns.begin() + first;
  7395. // remove_if... or well, sort of
  7396. for (xpath_node* it = last; it != ns.end(); ++it, ++i)
  7397. {
  7398. xpath_context c(*it, i, size);
  7399. if (expr->eval_boolean(c, stack))
  7400. {
  7401. *last++ = *it;
  7402. if (once) break;
  7403. }
  7404. }
  7405. ns.truncate(last);
  7406. }
  7407. static void apply_predicate_number(xpath_node_set_raw& ns, size_t first, xpath_ast_node* expr, const xpath_stack& stack, bool once)
  7408. {
  7409. assert(ns.size() >= first);
  7410. assert(expr->rettype() == xpath_type_number);
  7411. size_t i = 1;
  7412. size_t size = ns.size() - first;
  7413. xpath_node* last = ns.begin() + first;
  7414. // remove_if... or well, sort of
  7415. for (xpath_node* it = last; it != ns.end(); ++it, ++i)
  7416. {
  7417. xpath_context c(*it, i, size);
  7418. if (expr->eval_number(c, stack) == i)
  7419. {
  7420. *last++ = *it;
  7421. if (once) break;
  7422. }
  7423. }
  7424. ns.truncate(last);
  7425. }
  7426. static void apply_predicate_number_const(xpath_node_set_raw& ns, size_t first, xpath_ast_node* expr, const xpath_stack& stack)
  7427. {
  7428. assert(ns.size() >= first);
  7429. assert(expr->rettype() == xpath_type_number);
  7430. size_t size = ns.size() - first;
  7431. xpath_node* last = ns.begin() + first;
  7432. xpath_context c(xpath_node(), 1, size);
  7433. double er = expr->eval_number(c, stack);
  7434. if (er >= 1.0 && er <= size)
  7435. {
  7436. size_t eri = static_cast<size_t>(er);
  7437. if (er == eri)
  7438. {
  7439. xpath_node r = last[eri - 1];
  7440. *last++ = r;
  7441. }
  7442. }
  7443. ns.truncate(last);
  7444. }
  7445. void apply_predicate(xpath_node_set_raw& ns, size_t first, const xpath_stack& stack, bool once)
  7446. {
  7447. if (ns.size() == first) return;
  7448. assert(_type == ast_filter || _type == ast_predicate);
  7449. if (_test == predicate_constant || _test == predicate_constant_one)
  7450. apply_predicate_number_const(ns, first, _right, stack);
  7451. else if (_right->rettype() == xpath_type_number)
  7452. apply_predicate_number(ns, first, _right, stack, once);
  7453. else
  7454. apply_predicate_boolean(ns, first, _right, stack, once);
  7455. }
  7456. void apply_predicates(xpath_node_set_raw& ns, size_t first, const xpath_stack& stack, nodeset_eval_t eval)
  7457. {
  7458. if (ns.size() == first) return;
  7459. bool last_once = eval_once(ns.type(), eval);
  7460. for (xpath_ast_node* pred = _right; pred; pred = pred->_next)
  7461. pred->apply_predicate(ns, first, stack, !pred->_next && last_once);
  7462. }
  7463. bool step_push(xpath_node_set_raw& ns, xml_attribute_struct* a, xml_node_struct* parent, xpath_allocator* alloc)
  7464. {
  7465. assert(a);
  7466. const char_t* name = a->name ? a->name + 0 : PUGIXML_TEXT("");
  7467. switch (_test)
  7468. {
  7469. case nodetest_name:
  7470. if (strequal(name, _data.nodetest) && is_xpath_attribute(name))
  7471. {
  7472. ns.push_back(xpath_node(xml_attribute(a), xml_node(parent)), alloc);
  7473. return true;
  7474. }
  7475. break;
  7476. case nodetest_type_node:
  7477. case nodetest_all:
  7478. if (is_xpath_attribute(name))
  7479. {
  7480. ns.push_back(xpath_node(xml_attribute(a), xml_node(parent)), alloc);
  7481. return true;
  7482. }
  7483. break;
  7484. case nodetest_all_in_namespace:
  7485. if (starts_with(name, _data.nodetest) && is_xpath_attribute(name))
  7486. {
  7487. ns.push_back(xpath_node(xml_attribute(a), xml_node(parent)), alloc);
  7488. return true;
  7489. }
  7490. break;
  7491. default:
  7492. ;
  7493. }
  7494. return false;
  7495. }
  7496. bool step_push(xpath_node_set_raw& ns, xml_node_struct* n, xpath_allocator* alloc)
  7497. {
  7498. assert(n);
  7499. xml_node_type type = PUGI__NODETYPE(n);
  7500. switch (_test)
  7501. {
  7502. case nodetest_name:
  7503. if (type == node_element && n->name && strequal(n->name, _data.nodetest))
  7504. {
  7505. ns.push_back(xml_node(n), alloc);
  7506. return true;
  7507. }
  7508. break;
  7509. case nodetest_type_node:
  7510. ns.push_back(xml_node(n), alloc);
  7511. return true;
  7512. case nodetest_type_comment:
  7513. if (type == node_comment)
  7514. {
  7515. ns.push_back(xml_node(n), alloc);
  7516. return true;
  7517. }
  7518. break;
  7519. case nodetest_type_text:
  7520. if (type == node_pcdata || type == node_cdata)
  7521. {
  7522. ns.push_back(xml_node(n), alloc);
  7523. return true;
  7524. }
  7525. break;
  7526. case nodetest_type_pi:
  7527. if (type == node_pi)
  7528. {
  7529. ns.push_back(xml_node(n), alloc);
  7530. return true;
  7531. }
  7532. break;
  7533. case nodetest_pi:
  7534. if (type == node_pi && n->name && strequal(n->name, _data.nodetest))
  7535. {
  7536. ns.push_back(xml_node(n), alloc);
  7537. return true;
  7538. }
  7539. break;
  7540. case nodetest_all:
  7541. if (type == node_element)
  7542. {
  7543. ns.push_back(xml_node(n), alloc);
  7544. return true;
  7545. }
  7546. break;
  7547. case nodetest_all_in_namespace:
  7548. if (type == node_element && n->name && starts_with(n->name, _data.nodetest))
  7549. {
  7550. ns.push_back(xml_node(n), alloc);
  7551. return true;
  7552. }
  7553. break;
  7554. default:
  7555. assert(!"Unknown axis");
  7556. }
  7557. return false;
  7558. }
  7559. template <class T> void step_fill(xpath_node_set_raw& ns, xml_node_struct* n, xpath_allocator* alloc, bool once, T)
  7560. {
  7561. const axis_t axis = T::axis;
  7562. switch (axis)
  7563. {
  7564. case axis_attribute:
  7565. {
  7566. for (xml_attribute_struct* a = n->first_attribute; a; a = a->next_attribute)
  7567. if (step_push(ns, a, n, alloc) & once)
  7568. return;
  7569. break;
  7570. }
  7571. case axis_child:
  7572. {
  7573. for (xml_node_struct* c = n->first_child; c; c = c->next_sibling)
  7574. if (step_push(ns, c, alloc) & once)
  7575. return;
  7576. break;
  7577. }
  7578. case axis_descendant:
  7579. case axis_descendant_or_self:
  7580. {
  7581. if (axis == axis_descendant_or_self)
  7582. if (step_push(ns, n, alloc) & once)
  7583. return;
  7584. xml_node_struct* cur = n->first_child;
  7585. while (cur)
  7586. {
  7587. if (step_push(ns, cur, alloc) & once)
  7588. return;
  7589. if (cur->first_child)
  7590. cur = cur->first_child;
  7591. else
  7592. {
  7593. while (!cur->next_sibling)
  7594. {
  7595. cur = cur->parent;
  7596. if (cur == n) return;
  7597. }
  7598. cur = cur->next_sibling;
  7599. }
  7600. }
  7601. break;
  7602. }
  7603. case axis_following_sibling:
  7604. {
  7605. for (xml_node_struct* c = n->next_sibling; c; c = c->next_sibling)
  7606. if (step_push(ns, c, alloc) & once)
  7607. return;
  7608. break;
  7609. }
  7610. case axis_preceding_sibling:
  7611. {
  7612. for (xml_node_struct* c = n->prev_sibling_c; c->next_sibling; c = c->prev_sibling_c)
  7613. if (step_push(ns, c, alloc) & once)
  7614. return;
  7615. break;
  7616. }
  7617. case axis_following:
  7618. {
  7619. xml_node_struct* cur = n;
  7620. // exit from this node so that we don't include descendants
  7621. while (!cur->next_sibling)
  7622. {
  7623. cur = cur->parent;
  7624. if (!cur) return;
  7625. }
  7626. cur = cur->next_sibling;
  7627. while (cur)
  7628. {
  7629. if (step_push(ns, cur, alloc) & once)
  7630. return;
  7631. if (cur->first_child)
  7632. cur = cur->first_child;
  7633. else
  7634. {
  7635. while (!cur->next_sibling)
  7636. {
  7637. cur = cur->parent;
  7638. if (!cur) return;
  7639. }
  7640. cur = cur->next_sibling;
  7641. }
  7642. }
  7643. break;
  7644. }
  7645. case axis_preceding:
  7646. {
  7647. xml_node_struct* cur = n;
  7648. // exit from this node so that we don't include descendants
  7649. while (!cur->prev_sibling_c->next_sibling)
  7650. {
  7651. cur = cur->parent;
  7652. if (!cur) return;
  7653. }
  7654. cur = cur->prev_sibling_c;
  7655. while (cur)
  7656. {
  7657. if (cur->first_child)
  7658. cur = cur->first_child->prev_sibling_c;
  7659. else
  7660. {
  7661. // leaf node, can't be ancestor
  7662. if (step_push(ns, cur, alloc) & once)
  7663. return;
  7664. while (!cur->prev_sibling_c->next_sibling)
  7665. {
  7666. cur = cur->parent;
  7667. if (!cur) return;
  7668. if (!node_is_ancestor(cur, n))
  7669. if (step_push(ns, cur, alloc) & once)
  7670. return;
  7671. }
  7672. cur = cur->prev_sibling_c;
  7673. }
  7674. }
  7675. break;
  7676. }
  7677. case axis_ancestor:
  7678. case axis_ancestor_or_self:
  7679. {
  7680. if (axis == axis_ancestor_or_self)
  7681. if (step_push(ns, n, alloc) & once)
  7682. return;
  7683. xml_node_struct* cur = n->parent;
  7684. while (cur)
  7685. {
  7686. if (step_push(ns, cur, alloc) & once)
  7687. return;
  7688. cur = cur->parent;
  7689. }
  7690. break;
  7691. }
  7692. case axis_self:
  7693. {
  7694. step_push(ns, n, alloc);
  7695. break;
  7696. }
  7697. case axis_parent:
  7698. {
  7699. if (n->parent)
  7700. step_push(ns, n->parent, alloc);
  7701. break;
  7702. }
  7703. default:
  7704. assert(!"Unimplemented axis");
  7705. }
  7706. }
  7707. template <class T> void step_fill(xpath_node_set_raw& ns, xml_attribute_struct* a, xml_node_struct* p, xpath_allocator* alloc, bool once, T v)
  7708. {
  7709. const axis_t axis = T::axis;
  7710. switch (axis)
  7711. {
  7712. case axis_ancestor:
  7713. case axis_ancestor_or_self:
  7714. {
  7715. if (axis == axis_ancestor_or_self && _test == nodetest_type_node) // reject attributes based on principal node type test
  7716. if (step_push(ns, a, p, alloc) & once)
  7717. return;
  7718. xml_node_struct* cur = p;
  7719. while (cur)
  7720. {
  7721. if (step_push(ns, cur, alloc) & once)
  7722. return;
  7723. cur = cur->parent;
  7724. }
  7725. break;
  7726. }
  7727. case axis_descendant_or_self:
  7728. case axis_self:
  7729. {
  7730. if (_test == nodetest_type_node) // reject attributes based on principal node type test
  7731. step_push(ns, a, p, alloc);
  7732. break;
  7733. }
  7734. case axis_following:
  7735. {
  7736. xml_node_struct* cur = p;
  7737. while (cur)
  7738. {
  7739. if (cur->first_child)
  7740. cur = cur->first_child;
  7741. else
  7742. {
  7743. while (!cur->next_sibling)
  7744. {
  7745. cur = cur->parent;
  7746. if (!cur) return;
  7747. }
  7748. cur = cur->next_sibling;
  7749. }
  7750. if (step_push(ns, cur, alloc) & once)
  7751. return;
  7752. }
  7753. break;
  7754. }
  7755. case axis_parent:
  7756. {
  7757. step_push(ns, p, alloc);
  7758. break;
  7759. }
  7760. case axis_preceding:
  7761. {
  7762. // preceding:: axis does not include attribute nodes and attribute ancestors (they are the same as parent's ancestors), so we can reuse node preceding
  7763. step_fill(ns, p, alloc, once, v);
  7764. break;
  7765. }
  7766. default:
  7767. assert(!"Unimplemented axis");
  7768. }
  7769. }
  7770. template <class T> void step_fill(xpath_node_set_raw& ns, const xpath_node& xn, xpath_allocator* alloc, bool once, T v)
  7771. {
  7772. const axis_t axis = T::axis;
  7773. const bool axis_has_attributes = (axis == axis_ancestor || axis == axis_ancestor_or_self || axis == axis_descendant_or_self || axis == axis_following || axis == axis_parent || axis == axis_preceding || axis == axis_self);
  7774. if (xn.node())
  7775. step_fill(ns, xn.node().internal_object(), alloc, once, v);
  7776. else if (axis_has_attributes && xn.attribute() && xn.parent())
  7777. step_fill(ns, xn.attribute().internal_object(), xn.parent().internal_object(), alloc, once, v);
  7778. }
  7779. template <class T> xpath_node_set_raw step_do(const xpath_context& c, const xpath_stack& stack, nodeset_eval_t eval, T v)
  7780. {
  7781. const axis_t axis = T::axis;
  7782. const bool axis_reverse = (axis == axis_ancestor || axis == axis_ancestor_or_self || axis == axis_preceding || axis == axis_preceding_sibling);
  7783. const xpath_node_set::type_t axis_type = axis_reverse ? xpath_node_set::type_sorted_reverse : xpath_node_set::type_sorted;
  7784. bool once =
  7785. (axis == axis_attribute && _test == nodetest_name) ||
  7786. (!_right && eval_once(axis_type, eval)) ||
  7787. (_right && !_right->_next && _right->_test == predicate_constant_one);
  7788. xpath_node_set_raw ns;
  7789. ns.set_type(axis_type);
  7790. if (_left)
  7791. {
  7792. xpath_node_set_raw s = _left->eval_node_set(c, stack, nodeset_eval_all);
  7793. // self axis preserves the original order
  7794. if (axis == axis_self) ns.set_type(s.type());
  7795. for (const xpath_node* it = s.begin(); it != s.end(); ++it)
  7796. {
  7797. size_t size = ns.size();
  7798. // in general, all axes generate elements in a particular order, but there is no order guarantee if axis is applied to two nodes
  7799. if (axis != axis_self && size != 0) ns.set_type(xpath_node_set::type_unsorted);
  7800. step_fill(ns, *it, stack.result, once, v);
  7801. if (_right) apply_predicates(ns, size, stack, eval);
  7802. }
  7803. }
  7804. else
  7805. {
  7806. step_fill(ns, c.n, stack.result, once, v);
  7807. if (_right) apply_predicates(ns, 0, stack, eval);
  7808. }
  7809. // child, attribute and self axes always generate unique set of nodes
  7810. // for other axis, if the set stayed sorted, it stayed unique because the traversal algorithms do not visit the same node twice
  7811. if (axis != axis_child && axis != axis_attribute && axis != axis_self && ns.type() == xpath_node_set::type_unsorted)
  7812. ns.remove_duplicates();
  7813. return ns;
  7814. }
  7815. public:
  7816. xpath_ast_node(ast_type_t type, xpath_value_type rettype_, const char_t* value):
  7817. _type(static_cast<char>(type)), _rettype(static_cast<char>(rettype_)), _axis(0), _test(0), _left(0), _right(0), _next(0)
  7818. {
  7819. assert(type == ast_string_constant);
  7820. _data.string = value;
  7821. }
  7822. xpath_ast_node(ast_type_t type, xpath_value_type rettype_, double value):
  7823. _type(static_cast<char>(type)), _rettype(static_cast<char>(rettype_)), _axis(0), _test(0), _left(0), _right(0), _next(0)
  7824. {
  7825. assert(type == ast_number_constant);
  7826. _data.number = value;
  7827. }
  7828. xpath_ast_node(ast_type_t type, xpath_value_type rettype_, xpath_variable* value):
  7829. _type(static_cast<char>(type)), _rettype(static_cast<char>(rettype_)), _axis(0), _test(0), _left(0), _right(0), _next(0)
  7830. {
  7831. assert(type == ast_variable);
  7832. _data.variable = value;
  7833. }
  7834. xpath_ast_node(ast_type_t type, xpath_value_type rettype_, xpath_ast_node* left = 0, xpath_ast_node* right = 0):
  7835. _type(static_cast<char>(type)), _rettype(static_cast<char>(rettype_)), _axis(0), _test(0), _left(left), _right(right), _next(0)
  7836. {
  7837. }
  7838. xpath_ast_node(ast_type_t type, xpath_ast_node* left, axis_t axis, nodetest_t test, const char_t* contents):
  7839. _type(static_cast<char>(type)), _rettype(xpath_type_node_set), _axis(static_cast<char>(axis)), _test(static_cast<char>(test)), _left(left), _right(0), _next(0)
  7840. {
  7841. assert(type == ast_step);
  7842. _data.nodetest = contents;
  7843. }
  7844. xpath_ast_node(ast_type_t type, xpath_ast_node* left, xpath_ast_node* right, predicate_t test):
  7845. _type(static_cast<char>(type)), _rettype(xpath_type_node_set), _axis(0), _test(static_cast<char>(test)), _left(left), _right(right), _next(0)
  7846. {
  7847. assert(type == ast_filter || type == ast_predicate);
  7848. }
  7849. void set_next(xpath_ast_node* value)
  7850. {
  7851. _next = value;
  7852. }
  7853. void set_right(xpath_ast_node* value)
  7854. {
  7855. _right = value;
  7856. }
  7857. bool eval_boolean(const xpath_context& c, const xpath_stack& stack)
  7858. {
  7859. switch (_type)
  7860. {
  7861. case ast_op_or:
  7862. return _left->eval_boolean(c, stack) || _right->eval_boolean(c, stack);
  7863. case ast_op_and:
  7864. return _left->eval_boolean(c, stack) && _right->eval_boolean(c, stack);
  7865. case ast_op_equal:
  7866. return compare_eq(_left, _right, c, stack, equal_to());
  7867. case ast_op_not_equal:
  7868. return compare_eq(_left, _right, c, stack, not_equal_to());
  7869. case ast_op_less:
  7870. return compare_rel(_left, _right, c, stack, less());
  7871. case ast_op_greater:
  7872. return compare_rel(_right, _left, c, stack, less());
  7873. case ast_op_less_or_equal:
  7874. return compare_rel(_left, _right, c, stack, less_equal());
  7875. case ast_op_greater_or_equal:
  7876. return compare_rel(_right, _left, c, stack, less_equal());
  7877. case ast_func_starts_with:
  7878. {
  7879. xpath_allocator_capture cr(stack.result);
  7880. xpath_string lr = _left->eval_string(c, stack);
  7881. xpath_string rr = _right->eval_string(c, stack);
  7882. return starts_with(lr.c_str(), rr.c_str());
  7883. }
  7884. case ast_func_contains:
  7885. {
  7886. xpath_allocator_capture cr(stack.result);
  7887. xpath_string lr = _left->eval_string(c, stack);
  7888. xpath_string rr = _right->eval_string(c, stack);
  7889. return find_substring(lr.c_str(), rr.c_str()) != 0;
  7890. }
  7891. case ast_func_boolean:
  7892. return _left->eval_boolean(c, stack);
  7893. case ast_func_not:
  7894. return !_left->eval_boolean(c, stack);
  7895. case ast_func_true:
  7896. return true;
  7897. case ast_func_false:
  7898. return false;
  7899. case ast_func_lang:
  7900. {
  7901. if (c.n.attribute()) return false;
  7902. xpath_allocator_capture cr(stack.result);
  7903. xpath_string lang = _left->eval_string(c, stack);
  7904. for (xml_node n = c.n.node(); n; n = n.parent())
  7905. {
  7906. xml_attribute a = n.attribute(PUGIXML_TEXT("xml:lang"));
  7907. if (a)
  7908. {
  7909. const char_t* value = a.value();
  7910. // strnicmp / strncasecmp is not portable
  7911. for (const char_t* lit = lang.c_str(); *lit; ++lit)
  7912. {
  7913. if (tolower_ascii(*lit) != tolower_ascii(*value)) return false;
  7914. ++value;
  7915. }
  7916. return *value == 0 || *value == '-';
  7917. }
  7918. }
  7919. return false;
  7920. }
  7921. case ast_opt_compare_attribute:
  7922. {
  7923. const char_t* value = (_right->_type == ast_string_constant) ? _right->_data.string : _right->_data.variable->get_string();
  7924. xml_attribute attr = c.n.node().attribute(_left->_data.nodetest);
  7925. return attr && strequal(attr.value(), value) && is_xpath_attribute(attr.name());
  7926. }
  7927. case ast_variable:
  7928. {
  7929. assert(_rettype == _data.variable->type());
  7930. if (_rettype == xpath_type_boolean)
  7931. return _data.variable->get_boolean();
  7932. // fallthrough to type conversion
  7933. }
  7934. default:
  7935. {
  7936. switch (_rettype)
  7937. {
  7938. case xpath_type_number:
  7939. return convert_number_to_boolean(eval_number(c, stack));
  7940. case xpath_type_string:
  7941. {
  7942. xpath_allocator_capture cr(stack.result);
  7943. return !eval_string(c, stack).empty();
  7944. }
  7945. case xpath_type_node_set:
  7946. {
  7947. xpath_allocator_capture cr(stack.result);
  7948. return !eval_node_set(c, stack, nodeset_eval_any).empty();
  7949. }
  7950. default:
  7951. assert(!"Wrong expression for return type boolean");
  7952. return false;
  7953. }
  7954. }
  7955. }
  7956. }
  7957. double eval_number(const xpath_context& c, const xpath_stack& stack)
  7958. {
  7959. switch (_type)
  7960. {
  7961. case ast_op_add:
  7962. return _left->eval_number(c, stack) + _right->eval_number(c, stack);
  7963. case ast_op_subtract:
  7964. return _left->eval_number(c, stack) - _right->eval_number(c, stack);
  7965. case ast_op_multiply:
  7966. return _left->eval_number(c, stack) * _right->eval_number(c, stack);
  7967. case ast_op_divide:
  7968. return _left->eval_number(c, stack) / _right->eval_number(c, stack);
  7969. case ast_op_mod:
  7970. return fmod(_left->eval_number(c, stack), _right->eval_number(c, stack));
  7971. case ast_op_negate:
  7972. return -_left->eval_number(c, stack);
  7973. case ast_number_constant:
  7974. return _data.number;
  7975. case ast_func_last:
  7976. return static_cast<double>(c.size);
  7977. case ast_func_position:
  7978. return static_cast<double>(c.position);
  7979. case ast_func_count:
  7980. {
  7981. xpath_allocator_capture cr(stack.result);
  7982. return static_cast<double>(_left->eval_node_set(c, stack, nodeset_eval_all).size());
  7983. }
  7984. case ast_func_string_length_0:
  7985. {
  7986. xpath_allocator_capture cr(stack.result);
  7987. return static_cast<double>(string_value(c.n, stack.result).length());
  7988. }
  7989. case ast_func_string_length_1:
  7990. {
  7991. xpath_allocator_capture cr(stack.result);
  7992. return static_cast<double>(_left->eval_string(c, stack).length());
  7993. }
  7994. case ast_func_number_0:
  7995. {
  7996. xpath_allocator_capture cr(stack.result);
  7997. return convert_string_to_number(string_value(c.n, stack.result).c_str());
  7998. }
  7999. case ast_func_number_1:
  8000. return _left->eval_number(c, stack);
  8001. case ast_func_sum:
  8002. {
  8003. xpath_allocator_capture cr(stack.result);
  8004. double r = 0;
  8005. xpath_node_set_raw ns = _left->eval_node_set(c, stack, nodeset_eval_all);
  8006. for (const xpath_node* it = ns.begin(); it != ns.end(); ++it)
  8007. {
  8008. xpath_allocator_capture cri(stack.result);
  8009. r += convert_string_to_number(string_value(*it, stack.result).c_str());
  8010. }
  8011. return r;
  8012. }
  8013. case ast_func_floor:
  8014. {
  8015. double r = _left->eval_number(c, stack);
  8016. return r == r ? floor(r) : r;
  8017. }
  8018. case ast_func_ceiling:
  8019. {
  8020. double r = _left->eval_number(c, stack);
  8021. return r == r ? ceil(r) : r;
  8022. }
  8023. case ast_func_round:
  8024. return round_nearest_nzero(_left->eval_number(c, stack));
  8025. case ast_variable:
  8026. {
  8027. assert(_rettype == _data.variable->type());
  8028. if (_rettype == xpath_type_number)
  8029. return _data.variable->get_number();
  8030. // fallthrough to type conversion
  8031. }
  8032. default:
  8033. {
  8034. switch (_rettype)
  8035. {
  8036. case xpath_type_boolean:
  8037. return eval_boolean(c, stack) ? 1 : 0;
  8038. case xpath_type_string:
  8039. {
  8040. xpath_allocator_capture cr(stack.result);
  8041. return convert_string_to_number(eval_string(c, stack).c_str());
  8042. }
  8043. case xpath_type_node_set:
  8044. {
  8045. xpath_allocator_capture cr(stack.result);
  8046. return convert_string_to_number(eval_string(c, stack).c_str());
  8047. }
  8048. default:
  8049. assert(!"Wrong expression for return type number");
  8050. return 0;
  8051. }
  8052. }
  8053. }
  8054. }
  8055. xpath_string eval_string_concat(const xpath_context& c, const xpath_stack& stack)
  8056. {
  8057. assert(_type == ast_func_concat);
  8058. xpath_allocator_capture ct(stack.temp);
  8059. // count the string number
  8060. size_t count = 1;
  8061. for (xpath_ast_node* nc = _right; nc; nc = nc->_next) count++;
  8062. // gather all strings
  8063. xpath_string static_buffer[4];
  8064. xpath_string* buffer = static_buffer;
  8065. // allocate on-heap for large concats
  8066. if (count > sizeof(static_buffer) / sizeof(static_buffer[0]))
  8067. {
  8068. buffer = static_cast<xpath_string*>(stack.temp->allocate(count * sizeof(xpath_string)));
  8069. assert(buffer);
  8070. }
  8071. // evaluate all strings to temporary stack
  8072. xpath_stack swapped_stack = {stack.temp, stack.result};
  8073. buffer[0] = _left->eval_string(c, swapped_stack);
  8074. size_t pos = 1;
  8075. for (xpath_ast_node* n = _right; n; n = n->_next, ++pos) buffer[pos] = n->eval_string(c, swapped_stack);
  8076. assert(pos == count);
  8077. // get total length
  8078. size_t length = 0;
  8079. for (size_t i = 0; i < count; ++i) length += buffer[i].length();
  8080. // create final string
  8081. char_t* result = static_cast<char_t*>(stack.result->allocate((length + 1) * sizeof(char_t)));
  8082. assert(result);
  8083. char_t* ri = result;
  8084. for (size_t j = 0; j < count; ++j)
  8085. for (const char_t* bi = buffer[j].c_str(); *bi; ++bi)
  8086. *ri++ = *bi;
  8087. *ri = 0;
  8088. return xpath_string::from_heap_preallocated(result, ri);
  8089. }
  8090. xpath_string eval_string(const xpath_context& c, const xpath_stack& stack)
  8091. {
  8092. switch (_type)
  8093. {
  8094. case ast_string_constant:
  8095. return xpath_string::from_const(_data.string);
  8096. case ast_func_local_name_0:
  8097. {
  8098. xpath_node na = c.n;
  8099. return xpath_string::from_const(local_name(na));
  8100. }
  8101. case ast_func_local_name_1:
  8102. {
  8103. xpath_allocator_capture cr(stack.result);
  8104. xpath_node_set_raw ns = _left->eval_node_set(c, stack, nodeset_eval_first);
  8105. xpath_node na = ns.first();
  8106. return xpath_string::from_const(local_name(na));
  8107. }
  8108. case ast_func_name_0:
  8109. {
  8110. xpath_node na = c.n;
  8111. return xpath_string::from_const(qualified_name(na));
  8112. }
  8113. case ast_func_name_1:
  8114. {
  8115. xpath_allocator_capture cr(stack.result);
  8116. xpath_node_set_raw ns = _left->eval_node_set(c, stack, nodeset_eval_first);
  8117. xpath_node na = ns.first();
  8118. return xpath_string::from_const(qualified_name(na));
  8119. }
  8120. case ast_func_namespace_uri_0:
  8121. {
  8122. xpath_node na = c.n;
  8123. return xpath_string::from_const(namespace_uri(na));
  8124. }
  8125. case ast_func_namespace_uri_1:
  8126. {
  8127. xpath_allocator_capture cr(stack.result);
  8128. xpath_node_set_raw ns = _left->eval_node_set(c, stack, nodeset_eval_first);
  8129. xpath_node na = ns.first();
  8130. return xpath_string::from_const(namespace_uri(na));
  8131. }
  8132. case ast_func_string_0:
  8133. return string_value(c.n, stack.result);
  8134. case ast_func_string_1:
  8135. return _left->eval_string(c, stack);
  8136. case ast_func_concat:
  8137. return eval_string_concat(c, stack);
  8138. case ast_func_substring_before:
  8139. {
  8140. xpath_allocator_capture cr(stack.temp);
  8141. xpath_stack swapped_stack = {stack.temp, stack.result};
  8142. xpath_string s = _left->eval_string(c, swapped_stack);
  8143. xpath_string p = _right->eval_string(c, swapped_stack);
  8144. const char_t* pos = find_substring(s.c_str(), p.c_str());
  8145. return pos ? xpath_string::from_heap(s.c_str(), pos, stack.result) : xpath_string();
  8146. }
  8147. case ast_func_substring_after:
  8148. {
  8149. xpath_allocator_capture cr(stack.temp);
  8150. xpath_stack swapped_stack = {stack.temp, stack.result};
  8151. xpath_string s = _left->eval_string(c, swapped_stack);
  8152. xpath_string p = _right->eval_string(c, swapped_stack);
  8153. const char_t* pos = find_substring(s.c_str(), p.c_str());
  8154. if (!pos) return xpath_string();
  8155. const char_t* rbegin = pos + p.length();
  8156. const char_t* rend = s.c_str() + s.length();
  8157. return s.uses_heap() ? xpath_string::from_heap(rbegin, rend, stack.result) : xpath_string::from_const(rbegin);
  8158. }
  8159. case ast_func_substring_2:
  8160. {
  8161. xpath_allocator_capture cr(stack.temp);
  8162. xpath_stack swapped_stack = {stack.temp, stack.result};
  8163. xpath_string s = _left->eval_string(c, swapped_stack);
  8164. size_t s_length = s.length();
  8165. double first = round_nearest(_right->eval_number(c, stack));
  8166. if (is_nan(first)) return xpath_string(); // NaN
  8167. else if (first >= s_length + 1) return xpath_string();
  8168. size_t pos = first < 1 ? 1 : static_cast<size_t>(first);
  8169. assert(1 <= pos && pos <= s_length + 1);
  8170. const char_t* rbegin = s.c_str() + (pos - 1);
  8171. const char_t* rend = s.c_str() + s.length();
  8172. return s.uses_heap() ? xpath_string::from_heap(rbegin, rend, stack.result) : xpath_string::from_const(rbegin);
  8173. }
  8174. case ast_func_substring_3:
  8175. {
  8176. xpath_allocator_capture cr(stack.temp);
  8177. xpath_stack swapped_stack = {stack.temp, stack.result};
  8178. xpath_string s = _left->eval_string(c, swapped_stack);
  8179. size_t s_length = s.length();
  8180. double first = round_nearest(_right->eval_number(c, stack));
  8181. double last = first + round_nearest(_right->_next->eval_number(c, stack));
  8182. if (is_nan(first) || is_nan(last)) return xpath_string();
  8183. else if (first >= s_length + 1) return xpath_string();
  8184. else if (first >= last) return xpath_string();
  8185. else if (last < 1) return xpath_string();
  8186. size_t pos = first < 1 ? 1 : static_cast<size_t>(first);
  8187. size_t end = last >= s_length + 1 ? s_length + 1 : static_cast<size_t>(last);
  8188. assert(1 <= pos && pos <= end && end <= s_length + 1);
  8189. const char_t* rbegin = s.c_str() + (pos - 1);
  8190. const char_t* rend = s.c_str() + (end - 1);
  8191. return (end == s_length + 1 && !s.uses_heap()) ? xpath_string::from_const(rbegin) : xpath_string::from_heap(rbegin, rend, stack.result);
  8192. }
  8193. case ast_func_normalize_space_0:
  8194. {
  8195. xpath_string s = string_value(c.n, stack.result);
  8196. char_t* begin = s.data(stack.result);
  8197. char_t* end = normalize_space(begin);
  8198. return xpath_string::from_heap_preallocated(begin, end);
  8199. }
  8200. case ast_func_normalize_space_1:
  8201. {
  8202. xpath_string s = _left->eval_string(c, stack);
  8203. char_t* begin = s.data(stack.result);
  8204. char_t* end = normalize_space(begin);
  8205. return xpath_string::from_heap_preallocated(begin, end);
  8206. }
  8207. case ast_func_translate:
  8208. {
  8209. xpath_allocator_capture cr(stack.temp);
  8210. xpath_stack swapped_stack = {stack.temp, stack.result};
  8211. xpath_string s = _left->eval_string(c, stack);
  8212. xpath_string from = _right->eval_string(c, swapped_stack);
  8213. xpath_string to = _right->_next->eval_string(c, swapped_stack);
  8214. char_t* begin = s.data(stack.result);
  8215. char_t* end = translate(begin, from.c_str(), to.c_str(), to.length());
  8216. return xpath_string::from_heap_preallocated(begin, end);
  8217. }
  8218. case ast_opt_translate_table:
  8219. {
  8220. xpath_string s = _left->eval_string(c, stack);
  8221. char_t* begin = s.data(stack.result);
  8222. char_t* end = translate_table(begin, _data.table);
  8223. return xpath_string::from_heap_preallocated(begin, end);
  8224. }
  8225. case ast_variable:
  8226. {
  8227. assert(_rettype == _data.variable->type());
  8228. if (_rettype == xpath_type_string)
  8229. return xpath_string::from_const(_data.variable->get_string());
  8230. // fallthrough to type conversion
  8231. }
  8232. default:
  8233. {
  8234. switch (_rettype)
  8235. {
  8236. case xpath_type_boolean:
  8237. return xpath_string::from_const(eval_boolean(c, stack) ? PUGIXML_TEXT("true") : PUGIXML_TEXT("false"));
  8238. case xpath_type_number:
  8239. return convert_number_to_string(eval_number(c, stack), stack.result);
  8240. case xpath_type_node_set:
  8241. {
  8242. xpath_allocator_capture cr(stack.temp);
  8243. xpath_stack swapped_stack = {stack.temp, stack.result};
  8244. xpath_node_set_raw ns = eval_node_set(c, swapped_stack, nodeset_eval_first);
  8245. return ns.empty() ? xpath_string() : string_value(ns.first(), stack.result);
  8246. }
  8247. default:
  8248. assert(!"Wrong expression for return type string");
  8249. return xpath_string();
  8250. }
  8251. }
  8252. }
  8253. }
  8254. xpath_node_set_raw eval_node_set(const xpath_context& c, const xpath_stack& stack, nodeset_eval_t eval)
  8255. {
  8256. switch (_type)
  8257. {
  8258. case ast_op_union:
  8259. {
  8260. xpath_allocator_capture cr(stack.temp);
  8261. xpath_stack swapped_stack = {stack.temp, stack.result};
  8262. xpath_node_set_raw ls = _left->eval_node_set(c, swapped_stack, eval);
  8263. xpath_node_set_raw rs = _right->eval_node_set(c, stack, eval);
  8264. // we can optimize merging two sorted sets, but this is a very rare operation, so don't bother
  8265. rs.set_type(xpath_node_set::type_unsorted);
  8266. rs.append(ls.begin(), ls.end(), stack.result);
  8267. rs.remove_duplicates();
  8268. return rs;
  8269. }
  8270. case ast_filter:
  8271. {
  8272. xpath_node_set_raw set = _left->eval_node_set(c, stack, _test == predicate_constant_one ? nodeset_eval_first : nodeset_eval_all);
  8273. // either expression is a number or it contains position() call; sort by document order
  8274. if (_test != predicate_posinv) set.sort_do();
  8275. bool once = eval_once(set.type(), eval);
  8276. apply_predicate(set, 0, stack, once);
  8277. return set;
  8278. }
  8279. case ast_func_id:
  8280. return xpath_node_set_raw();
  8281. case ast_step:
  8282. {
  8283. switch (_axis)
  8284. {
  8285. case axis_ancestor:
  8286. return step_do(c, stack, eval, axis_to_type<axis_ancestor>());
  8287. case axis_ancestor_or_self:
  8288. return step_do(c, stack, eval, axis_to_type<axis_ancestor_or_self>());
  8289. case axis_attribute:
  8290. return step_do(c, stack, eval, axis_to_type<axis_attribute>());
  8291. case axis_child:
  8292. return step_do(c, stack, eval, axis_to_type<axis_child>());
  8293. case axis_descendant:
  8294. return step_do(c, stack, eval, axis_to_type<axis_descendant>());
  8295. case axis_descendant_or_self:
  8296. return step_do(c, stack, eval, axis_to_type<axis_descendant_or_self>());
  8297. case axis_following:
  8298. return step_do(c, stack, eval, axis_to_type<axis_following>());
  8299. case axis_following_sibling:
  8300. return step_do(c, stack, eval, axis_to_type<axis_following_sibling>());
  8301. case axis_namespace:
  8302. // namespaced axis is not supported
  8303. return xpath_node_set_raw();
  8304. case axis_parent:
  8305. return step_do(c, stack, eval, axis_to_type<axis_parent>());
  8306. case axis_preceding:
  8307. return step_do(c, stack, eval, axis_to_type<axis_preceding>());
  8308. case axis_preceding_sibling:
  8309. return step_do(c, stack, eval, axis_to_type<axis_preceding_sibling>());
  8310. case axis_self:
  8311. return step_do(c, stack, eval, axis_to_type<axis_self>());
  8312. default:
  8313. assert(!"Unknown axis");
  8314. return xpath_node_set_raw();
  8315. }
  8316. }
  8317. case ast_step_root:
  8318. {
  8319. assert(!_right); // root step can't have any predicates
  8320. xpath_node_set_raw ns;
  8321. ns.set_type(xpath_node_set::type_sorted);
  8322. if (c.n.node()) ns.push_back(c.n.node().root(), stack.result);
  8323. else if (c.n.attribute()) ns.push_back(c.n.parent().root(), stack.result);
  8324. return ns;
  8325. }
  8326. case ast_variable:
  8327. {
  8328. assert(_rettype == _data.variable->type());
  8329. if (_rettype == xpath_type_node_set)
  8330. {
  8331. const xpath_node_set& s = _data.variable->get_node_set();
  8332. xpath_node_set_raw ns;
  8333. ns.set_type(s.type());
  8334. ns.append(s.begin(), s.end(), stack.result);
  8335. return ns;
  8336. }
  8337. // fallthrough to type conversion
  8338. }
  8339. default:
  8340. assert(!"Wrong expression for return type node set");
  8341. return xpath_node_set_raw();
  8342. }
  8343. }
  8344. void optimize(xpath_allocator* alloc)
  8345. {
  8346. if (_left) _left->optimize(alloc);
  8347. if (_right) _right->optimize(alloc);
  8348. if (_next) _next->optimize(alloc);
  8349. optimize_self(alloc);
  8350. }
  8351. void optimize_self(xpath_allocator* alloc)
  8352. {
  8353. // Rewrite [position()=expr] with [expr]
  8354. // Note that this step has to go before classification to recognize [position()=1]
  8355. if ((_type == ast_filter || _type == ast_predicate) &&
  8356. _right->_type == ast_op_equal && _right->_left->_type == ast_func_position && _right->_right->_rettype == xpath_type_number)
  8357. {
  8358. _right = _right->_right;
  8359. }
  8360. // Classify filter/predicate ops to perform various optimizations during evaluation
  8361. if (_type == ast_filter || _type == ast_predicate)
  8362. {
  8363. assert(_test == predicate_default);
  8364. if (_right->_type == ast_number_constant && _right->_data.number == 1.0)
  8365. _test = predicate_constant_one;
  8366. else if (_right->_rettype == xpath_type_number && (_right->_type == ast_number_constant || _right->_type == ast_variable || _right->_type == ast_func_last))
  8367. _test = predicate_constant;
  8368. else if (_right->_rettype != xpath_type_number && _right->is_posinv_expr())
  8369. _test = predicate_posinv;
  8370. }
  8371. // Rewrite descendant-or-self::node()/child::foo with descendant::foo
  8372. // The former is a full form of //foo, the latter is much faster since it executes the node test immediately
  8373. // Do a similar kind of rewrite for self/descendant/descendant-or-self axes
  8374. // Note that we only rewrite positionally invariant steps (//foo[1] != /descendant::foo[1])
  8375. if (_type == ast_step && (_axis == axis_child || _axis == axis_self || _axis == axis_descendant || _axis == axis_descendant_or_self) && _left &&
  8376. _left->_type == ast_step && _left->_axis == axis_descendant_or_self && _left->_test == nodetest_type_node && !_left->_right &&
  8377. is_posinv_step())
  8378. {
  8379. if (_axis == axis_child || _axis == axis_descendant)
  8380. _axis = axis_descendant;
  8381. else
  8382. _axis = axis_descendant_or_self;
  8383. _left = _left->_left;
  8384. }
  8385. // Use optimized lookup table implementation for translate() with constant arguments
  8386. if (_type == ast_func_translate && _right->_type == ast_string_constant && _right->_next->_type == ast_string_constant)
  8387. {
  8388. unsigned char* table = translate_table_generate(alloc, _right->_data.string, _right->_next->_data.string);
  8389. if (table)
  8390. {
  8391. _type = ast_opt_translate_table;
  8392. _data.table = table;
  8393. }
  8394. }
  8395. // Use optimized path for @attr = 'value' or @attr = $value
  8396. if (_type == ast_op_equal &&
  8397. _left->_type == ast_step && _left->_axis == axis_attribute && _left->_test == nodetest_name && !_left->_left && !_left->_right &&
  8398. (_right->_type == ast_string_constant || (_right->_type == ast_variable && _right->_rettype == xpath_type_string)))
  8399. {
  8400. _type = ast_opt_compare_attribute;
  8401. }
  8402. }
  8403. bool is_posinv_expr() const
  8404. {
  8405. switch (_type)
  8406. {
  8407. case ast_func_position:
  8408. case ast_func_last:
  8409. return false;
  8410. case ast_string_constant:
  8411. case ast_number_constant:
  8412. case ast_variable:
  8413. return true;
  8414. case ast_step:
  8415. case ast_step_root:
  8416. return true;
  8417. case ast_predicate:
  8418. case ast_filter:
  8419. return true;
  8420. default:
  8421. if (_left && !_left->is_posinv_expr()) return false;
  8422. for (xpath_ast_node* n = _right; n; n = n->_next)
  8423. if (!n->is_posinv_expr()) return false;
  8424. return true;
  8425. }
  8426. }
  8427. bool is_posinv_step() const
  8428. {
  8429. assert(_type == ast_step);
  8430. for (xpath_ast_node* n = _right; n; n = n->_next)
  8431. {
  8432. assert(n->_type == ast_predicate);
  8433. if (n->_test != predicate_posinv)
  8434. return false;
  8435. }
  8436. return true;
  8437. }
  8438. xpath_value_type rettype() const
  8439. {
  8440. return static_cast<xpath_value_type>(_rettype);
  8441. }
  8442. };
  8443. struct xpath_parser
  8444. {
  8445. xpath_allocator* _alloc;
  8446. xpath_lexer _lexer;
  8447. const char_t* _query;
  8448. xpath_variable_set* _variables;
  8449. xpath_parse_result* _result;
  8450. char_t _scratch[32];
  8451. #ifdef PUGIXML_NO_EXCEPTIONS
  8452. jmp_buf _error_handler;
  8453. #endif
  8454. void throw_error(const char* message)
  8455. {
  8456. _result->error = message;
  8457. _result->offset = _lexer.current_pos() - _query;
  8458. #ifdef PUGIXML_NO_EXCEPTIONS
  8459. longjmp(_error_handler, 1);
  8460. #else
  8461. throw xpath_exception(*_result);
  8462. #endif
  8463. }
  8464. void throw_error_oom()
  8465. {
  8466. #ifdef PUGIXML_NO_EXCEPTIONS
  8467. throw_error("Out of memory");
  8468. #else
  8469. throw std::bad_alloc();
  8470. #endif
  8471. }
  8472. void* alloc_node()
  8473. {
  8474. void* result = _alloc->allocate_nothrow(sizeof(xpath_ast_node));
  8475. if (!result) throw_error_oom();
  8476. return result;
  8477. }
  8478. const char_t* alloc_string(const xpath_lexer_string& value)
  8479. {
  8480. if (value.begin)
  8481. {
  8482. size_t length = static_cast<size_t>(value.end - value.begin);
  8483. char_t* c = static_cast<char_t*>(_alloc->allocate_nothrow((length + 1) * sizeof(char_t)));
  8484. if (!c) throw_error_oom();
  8485. assert(c); // workaround for clang static analysis
  8486. memcpy(c, value.begin, length * sizeof(char_t));
  8487. c[length] = 0;
  8488. return c;
  8489. }
  8490. else return 0;
  8491. }
  8492. xpath_ast_node* parse_function_helper(ast_type_t type0, ast_type_t type1, size_t argc, xpath_ast_node* args[2])
  8493. {
  8494. assert(argc <= 1);
  8495. if (argc == 1 && args[0]->rettype() != xpath_type_node_set) throw_error("Function has to be applied to node set");
  8496. return new (alloc_node()) xpath_ast_node(argc == 0 ? type0 : type1, xpath_type_string, args[0]);
  8497. }
  8498. xpath_ast_node* parse_function(const xpath_lexer_string& name, size_t argc, xpath_ast_node* args[2])
  8499. {
  8500. switch (name.begin[0])
  8501. {
  8502. case 'b':
  8503. if (name == PUGIXML_TEXT("boolean") && argc == 1)
  8504. return new (alloc_node()) xpath_ast_node(ast_func_boolean, xpath_type_boolean, args[0]);
  8505. break;
  8506. case 'c':
  8507. if (name == PUGIXML_TEXT("count") && argc == 1)
  8508. {
  8509. if (args[0]->rettype() != xpath_type_node_set) throw_error("Function has to be applied to node set");
  8510. return new (alloc_node()) xpath_ast_node(ast_func_count, xpath_type_number, args[0]);
  8511. }
  8512. else if (name == PUGIXML_TEXT("contains") && argc == 2)
  8513. return new (alloc_node()) xpath_ast_node(ast_func_contains, xpath_type_boolean, args[0], args[1]);
  8514. else if (name == PUGIXML_TEXT("concat") && argc >= 2)
  8515. return new (alloc_node()) xpath_ast_node(ast_func_concat, xpath_type_string, args[0], args[1]);
  8516. else if (name == PUGIXML_TEXT("ceiling") && argc == 1)
  8517. return new (alloc_node()) xpath_ast_node(ast_func_ceiling, xpath_type_number, args[0]);
  8518. break;
  8519. case 'f':
  8520. if (name == PUGIXML_TEXT("false") && argc == 0)
  8521. return new (alloc_node()) xpath_ast_node(ast_func_false, xpath_type_boolean);
  8522. else if (name == PUGIXML_TEXT("floor") && argc == 1)
  8523. return new (alloc_node()) xpath_ast_node(ast_func_floor, xpath_type_number, args[0]);
  8524. break;
  8525. case 'i':
  8526. if (name == PUGIXML_TEXT("id") && argc == 1)
  8527. return new (alloc_node()) xpath_ast_node(ast_func_id, xpath_type_node_set, args[0]);
  8528. break;
  8529. case 'l':
  8530. if (name == PUGIXML_TEXT("last") && argc == 0)
  8531. return new (alloc_node()) xpath_ast_node(ast_func_last, xpath_type_number);
  8532. else if (name == PUGIXML_TEXT("lang") && argc == 1)
  8533. return new (alloc_node()) xpath_ast_node(ast_func_lang, xpath_type_boolean, args[0]);
  8534. else if (name == PUGIXML_TEXT("local-name") && argc <= 1)
  8535. return parse_function_helper(ast_func_local_name_0, ast_func_local_name_1, argc, args);
  8536. break;
  8537. case 'n':
  8538. if (name == PUGIXML_TEXT("name") && argc <= 1)
  8539. return parse_function_helper(ast_func_name_0, ast_func_name_1, argc, args);
  8540. else if (name == PUGIXML_TEXT("namespace-uri") && argc <= 1)
  8541. return parse_function_helper(ast_func_namespace_uri_0, ast_func_namespace_uri_1, argc, args);
  8542. else if (name == PUGIXML_TEXT("normalize-space") && argc <= 1)
  8543. return new (alloc_node()) xpath_ast_node(argc == 0 ? ast_func_normalize_space_0 : ast_func_normalize_space_1, xpath_type_string, args[0], args[1]);
  8544. else if (name == PUGIXML_TEXT("not") && argc == 1)
  8545. return new (alloc_node()) xpath_ast_node(ast_func_not, xpath_type_boolean, args[0]);
  8546. else if (name == PUGIXML_TEXT("number") && argc <= 1)
  8547. return new (alloc_node()) xpath_ast_node(argc == 0 ? ast_func_number_0 : ast_func_number_1, xpath_type_number, args[0]);
  8548. break;
  8549. case 'p':
  8550. if (name == PUGIXML_TEXT("position") && argc == 0)
  8551. return new (alloc_node()) xpath_ast_node(ast_func_position, xpath_type_number);
  8552. break;
  8553. case 'r':
  8554. if (name == PUGIXML_TEXT("round") && argc == 1)
  8555. return new (alloc_node()) xpath_ast_node(ast_func_round, xpath_type_number, args[0]);
  8556. break;
  8557. case 's':
  8558. if (name == PUGIXML_TEXT("string") && argc <= 1)
  8559. return new (alloc_node()) xpath_ast_node(argc == 0 ? ast_func_string_0 : ast_func_string_1, xpath_type_string, args[0]);
  8560. else if (name == PUGIXML_TEXT("string-length") && argc <= 1)
  8561. return new (alloc_node()) xpath_ast_node(argc == 0 ? ast_func_string_length_0 : ast_func_string_length_1, xpath_type_number, args[0]);
  8562. else if (name == PUGIXML_TEXT("starts-with") && argc == 2)
  8563. return new (alloc_node()) xpath_ast_node(ast_func_starts_with, xpath_type_boolean, args[0], args[1]);
  8564. else if (name == PUGIXML_TEXT("substring-before") && argc == 2)
  8565. return new (alloc_node()) xpath_ast_node(ast_func_substring_before, xpath_type_string, args[0], args[1]);
  8566. else if (name == PUGIXML_TEXT("substring-after") && argc == 2)
  8567. return new (alloc_node()) xpath_ast_node(ast_func_substring_after, xpath_type_string, args[0], args[1]);
  8568. else if (name == PUGIXML_TEXT("substring") && (argc == 2 || argc == 3))
  8569. return new (alloc_node()) xpath_ast_node(argc == 2 ? ast_func_substring_2 : ast_func_substring_3, xpath_type_string, args[0], args[1]);
  8570. else if (name == PUGIXML_TEXT("sum") && argc == 1)
  8571. {
  8572. if (args[0]->rettype() != xpath_type_node_set) throw_error("Function has to be applied to node set");
  8573. return new (alloc_node()) xpath_ast_node(ast_func_sum, xpath_type_number, args[0]);
  8574. }
  8575. break;
  8576. case 't':
  8577. if (name == PUGIXML_TEXT("translate") && argc == 3)
  8578. return new (alloc_node()) xpath_ast_node(ast_func_translate, xpath_type_string, args[0], args[1]);
  8579. else if (name == PUGIXML_TEXT("true") && argc == 0)
  8580. return new (alloc_node()) xpath_ast_node(ast_func_true, xpath_type_boolean);
  8581. break;
  8582. default:
  8583. break;
  8584. }
  8585. throw_error("Unrecognized function or wrong parameter count");
  8586. return 0;
  8587. }
  8588. axis_t parse_axis_name(const xpath_lexer_string& name, bool& specified)
  8589. {
  8590. specified = true;
  8591. switch (name.begin[0])
  8592. {
  8593. case 'a':
  8594. if (name == PUGIXML_TEXT("ancestor"))
  8595. return axis_ancestor;
  8596. else if (name == PUGIXML_TEXT("ancestor-or-self"))
  8597. return axis_ancestor_or_self;
  8598. else if (name == PUGIXML_TEXT("attribute"))
  8599. return axis_attribute;
  8600. break;
  8601. case 'c':
  8602. if (name == PUGIXML_TEXT("child"))
  8603. return axis_child;
  8604. break;
  8605. case 'd':
  8606. if (name == PUGIXML_TEXT("descendant"))
  8607. return axis_descendant;
  8608. else if (name == PUGIXML_TEXT("descendant-or-self"))
  8609. return axis_descendant_or_self;
  8610. break;
  8611. case 'f':
  8612. if (name == PUGIXML_TEXT("following"))
  8613. return axis_following;
  8614. else if (name == PUGIXML_TEXT("following-sibling"))
  8615. return axis_following_sibling;
  8616. break;
  8617. case 'n':
  8618. if (name == PUGIXML_TEXT("namespace"))
  8619. return axis_namespace;
  8620. break;
  8621. case 'p':
  8622. if (name == PUGIXML_TEXT("parent"))
  8623. return axis_parent;
  8624. else if (name == PUGIXML_TEXT("preceding"))
  8625. return axis_preceding;
  8626. else if (name == PUGIXML_TEXT("preceding-sibling"))
  8627. return axis_preceding_sibling;
  8628. break;
  8629. case 's':
  8630. if (name == PUGIXML_TEXT("self"))
  8631. return axis_self;
  8632. break;
  8633. default:
  8634. break;
  8635. }
  8636. specified = false;
  8637. return axis_child;
  8638. }
  8639. nodetest_t parse_node_test_type(const xpath_lexer_string& name)
  8640. {
  8641. switch (name.begin[0])
  8642. {
  8643. case 'c':
  8644. if (name == PUGIXML_TEXT("comment"))
  8645. return nodetest_type_comment;
  8646. break;
  8647. case 'n':
  8648. if (name == PUGIXML_TEXT("node"))
  8649. return nodetest_type_node;
  8650. break;
  8651. case 'p':
  8652. if (name == PUGIXML_TEXT("processing-instruction"))
  8653. return nodetest_type_pi;
  8654. break;
  8655. case 't':
  8656. if (name == PUGIXML_TEXT("text"))
  8657. return nodetest_type_text;
  8658. break;
  8659. default:
  8660. break;
  8661. }
  8662. return nodetest_none;
  8663. }
  8664. // PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
  8665. xpath_ast_node* parse_primary_expression()
  8666. {
  8667. switch (_lexer.current())
  8668. {
  8669. case lex_var_ref:
  8670. {
  8671. xpath_lexer_string name = _lexer.contents();
  8672. if (!_variables)
  8673. throw_error("Unknown variable: variable set is not provided");
  8674. xpath_variable* var = 0;
  8675. if (!get_variable_scratch(_scratch, _variables, name.begin, name.end, &var))
  8676. throw_error_oom();
  8677. if (!var)
  8678. throw_error("Unknown variable: variable set does not contain the given name");
  8679. _lexer.next();
  8680. return new (alloc_node()) xpath_ast_node(ast_variable, var->type(), var);
  8681. }
  8682. case lex_open_brace:
  8683. {
  8684. _lexer.next();
  8685. xpath_ast_node* n = parse_expression();
  8686. if (_lexer.current() != lex_close_brace)
  8687. throw_error("Unmatched braces");
  8688. _lexer.next();
  8689. return n;
  8690. }
  8691. case lex_quoted_string:
  8692. {
  8693. const char_t* value = alloc_string(_lexer.contents());
  8694. xpath_ast_node* n = new (alloc_node()) xpath_ast_node(ast_string_constant, xpath_type_string, value);
  8695. _lexer.next();
  8696. return n;
  8697. }
  8698. case lex_number:
  8699. {
  8700. double value = 0;
  8701. if (!convert_string_to_number_scratch(_scratch, _lexer.contents().begin, _lexer.contents().end, &value))
  8702. throw_error_oom();
  8703. xpath_ast_node* n = new (alloc_node()) xpath_ast_node(ast_number_constant, xpath_type_number, value);
  8704. _lexer.next();
  8705. return n;
  8706. }
  8707. case lex_string:
  8708. {
  8709. xpath_ast_node* args[2] = {0};
  8710. size_t argc = 0;
  8711. xpath_lexer_string function = _lexer.contents();
  8712. _lexer.next();
  8713. xpath_ast_node* last_arg = 0;
  8714. if (_lexer.current() != lex_open_brace)
  8715. throw_error("Unrecognized function call");
  8716. _lexer.next();
  8717. if (_lexer.current() != lex_close_brace)
  8718. args[argc++] = parse_expression();
  8719. while (_lexer.current() != lex_close_brace)
  8720. {
  8721. if (_lexer.current() != lex_comma)
  8722. throw_error("No comma between function arguments");
  8723. _lexer.next();
  8724. xpath_ast_node* n = parse_expression();
  8725. if (argc < 2) args[argc] = n;
  8726. else last_arg->set_next(n);
  8727. argc++;
  8728. last_arg = n;
  8729. }
  8730. _lexer.next();
  8731. return parse_function(function, argc, args);
  8732. }
  8733. default:
  8734. throw_error("Unrecognizable primary expression");
  8735. return 0;
  8736. }
  8737. }
  8738. // FilterExpr ::= PrimaryExpr | FilterExpr Predicate
  8739. // Predicate ::= '[' PredicateExpr ']'
  8740. // PredicateExpr ::= Expr
  8741. xpath_ast_node* parse_filter_expression()
  8742. {
  8743. xpath_ast_node* n = parse_primary_expression();
  8744. while (_lexer.current() == lex_open_square_brace)
  8745. {
  8746. _lexer.next();
  8747. xpath_ast_node* expr = parse_expression();
  8748. if (n->rettype() != xpath_type_node_set) throw_error("Predicate has to be applied to node set");
  8749. n = new (alloc_node()) xpath_ast_node(ast_filter, n, expr, predicate_default);
  8750. if (_lexer.current() != lex_close_square_brace)
  8751. throw_error("Unmatched square brace");
  8752. _lexer.next();
  8753. }
  8754. return n;
  8755. }
  8756. // Step ::= AxisSpecifier NodeTest Predicate* | AbbreviatedStep
  8757. // AxisSpecifier ::= AxisName '::' | '@'?
  8758. // NodeTest ::= NameTest | NodeType '(' ')' | 'processing-instruction' '(' Literal ')'
  8759. // NameTest ::= '*' | NCName ':' '*' | QName
  8760. // AbbreviatedStep ::= '.' | '..'
  8761. xpath_ast_node* parse_step(xpath_ast_node* set)
  8762. {
  8763. if (set && set->rettype() != xpath_type_node_set)
  8764. throw_error("Step has to be applied to node set");
  8765. bool axis_specified = false;
  8766. axis_t axis = axis_child; // implied child axis
  8767. if (_lexer.current() == lex_axis_attribute)
  8768. {
  8769. axis = axis_attribute;
  8770. axis_specified = true;
  8771. _lexer.next();
  8772. }
  8773. else if (_lexer.current() == lex_dot)
  8774. {
  8775. _lexer.next();
  8776. return new (alloc_node()) xpath_ast_node(ast_step, set, axis_self, nodetest_type_node, 0);
  8777. }
  8778. else if (_lexer.current() == lex_double_dot)
  8779. {
  8780. _lexer.next();
  8781. return new (alloc_node()) xpath_ast_node(ast_step, set, axis_parent, nodetest_type_node, 0);
  8782. }
  8783. nodetest_t nt_type = nodetest_none;
  8784. xpath_lexer_string nt_name;
  8785. if (_lexer.current() == lex_string)
  8786. {
  8787. // node name test
  8788. nt_name = _lexer.contents();
  8789. _lexer.next();
  8790. // was it an axis name?
  8791. if (_lexer.current() == lex_double_colon)
  8792. {
  8793. // parse axis name
  8794. if (axis_specified) throw_error("Two axis specifiers in one step");
  8795. axis = parse_axis_name(nt_name, axis_specified);
  8796. if (!axis_specified) throw_error("Unknown axis");
  8797. // read actual node test
  8798. _lexer.next();
  8799. if (_lexer.current() == lex_multiply)
  8800. {
  8801. nt_type = nodetest_all;
  8802. nt_name = xpath_lexer_string();
  8803. _lexer.next();
  8804. }
  8805. else if (_lexer.current() == lex_string)
  8806. {
  8807. nt_name = _lexer.contents();
  8808. _lexer.next();
  8809. }
  8810. else throw_error("Unrecognized node test");
  8811. }
  8812. if (nt_type == nodetest_none)
  8813. {
  8814. // node type test or processing-instruction
  8815. if (_lexer.current() == lex_open_brace)
  8816. {
  8817. _lexer.next();
  8818. if (_lexer.current() == lex_close_brace)
  8819. {
  8820. _lexer.next();
  8821. nt_type = parse_node_test_type(nt_name);
  8822. if (nt_type == nodetest_none) throw_error("Unrecognized node type");
  8823. nt_name = xpath_lexer_string();
  8824. }
  8825. else if (nt_name == PUGIXML_TEXT("processing-instruction"))
  8826. {
  8827. if (_lexer.current() != lex_quoted_string)
  8828. throw_error("Only literals are allowed as arguments to processing-instruction()");
  8829. nt_type = nodetest_pi;
  8830. nt_name = _lexer.contents();
  8831. _lexer.next();
  8832. if (_lexer.current() != lex_close_brace)
  8833. throw_error("Unmatched brace near processing-instruction()");
  8834. _lexer.next();
  8835. }
  8836. else
  8837. throw_error("Unmatched brace near node type test");
  8838. }
  8839. // QName or NCName:*
  8840. else
  8841. {
  8842. if (nt_name.end - nt_name.begin > 2 && nt_name.end[-2] == ':' && nt_name.end[-1] == '*') // NCName:*
  8843. {
  8844. nt_name.end--; // erase *
  8845. nt_type = nodetest_all_in_namespace;
  8846. }
  8847. else nt_type = nodetest_name;
  8848. }
  8849. }
  8850. }
  8851. else if (_lexer.current() == lex_multiply)
  8852. {
  8853. nt_type = nodetest_all;
  8854. _lexer.next();
  8855. }
  8856. else throw_error("Unrecognized node test");
  8857. xpath_ast_node* n = new (alloc_node()) xpath_ast_node(ast_step, set, axis, nt_type, alloc_string(nt_name));
  8858. xpath_ast_node* last = 0;
  8859. while (_lexer.current() == lex_open_square_brace)
  8860. {
  8861. _lexer.next();
  8862. xpath_ast_node* expr = parse_expression();
  8863. xpath_ast_node* pred = new (alloc_node()) xpath_ast_node(ast_predicate, 0, expr, predicate_default);
  8864. if (_lexer.current() != lex_close_square_brace)
  8865. throw_error("Unmatched square brace");
  8866. _lexer.next();
  8867. if (last) last->set_next(pred);
  8868. else n->set_right(pred);
  8869. last = pred;
  8870. }
  8871. return n;
  8872. }
  8873. // RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
  8874. xpath_ast_node* parse_relative_location_path(xpath_ast_node* set)
  8875. {
  8876. xpath_ast_node* n = parse_step(set);
  8877. while (_lexer.current() == lex_slash || _lexer.current() == lex_double_slash)
  8878. {
  8879. lexeme_t l = _lexer.current();
  8880. _lexer.next();
  8881. if (l == lex_double_slash)
  8882. n = new (alloc_node()) xpath_ast_node(ast_step, n, axis_descendant_or_self, nodetest_type_node, 0);
  8883. n = parse_step(n);
  8884. }
  8885. return n;
  8886. }
  8887. // LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
  8888. // AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
  8889. xpath_ast_node* parse_location_path()
  8890. {
  8891. if (_lexer.current() == lex_slash)
  8892. {
  8893. _lexer.next();
  8894. xpath_ast_node* n = new (alloc_node()) xpath_ast_node(ast_step_root, xpath_type_node_set);
  8895. // relative location path can start from axis_attribute, dot, double_dot, multiply and string lexemes; any other lexeme means standalone root path
  8896. lexeme_t l = _lexer.current();
  8897. if (l == lex_string || l == lex_axis_attribute || l == lex_dot || l == lex_double_dot || l == lex_multiply)
  8898. return parse_relative_location_path(n);
  8899. else
  8900. return n;
  8901. }
  8902. else if (_lexer.current() == lex_double_slash)
  8903. {
  8904. _lexer.next();
  8905. xpath_ast_node* n = new (alloc_node()) xpath_ast_node(ast_step_root, xpath_type_node_set);
  8906. n = new (alloc_node()) xpath_ast_node(ast_step, n, axis_descendant_or_self, nodetest_type_node, 0);
  8907. return parse_relative_location_path(n);
  8908. }
  8909. // else clause moved outside of if because of bogus warning 'control may reach end of non-void function being inlined' in gcc 4.0.1
  8910. return parse_relative_location_path(0);
  8911. }
  8912. // PathExpr ::= LocationPath
  8913. // | FilterExpr
  8914. // | FilterExpr '/' RelativeLocationPath
  8915. // | FilterExpr '//' RelativeLocationPath
  8916. // UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
  8917. // UnaryExpr ::= UnionExpr | '-' UnaryExpr
  8918. xpath_ast_node* parse_path_or_unary_expression()
  8919. {
  8920. // Clarification.
  8921. // PathExpr begins with either LocationPath or FilterExpr.
  8922. // FilterExpr begins with PrimaryExpr
  8923. // PrimaryExpr begins with '$' in case of it being a variable reference,
  8924. // '(' in case of it being an expression, string literal, number constant or
  8925. // function call.
  8926. if (_lexer.current() == lex_var_ref || _lexer.current() == lex_open_brace ||
  8927. _lexer.current() == lex_quoted_string || _lexer.current() == lex_number ||
  8928. _lexer.current() == lex_string)
  8929. {
  8930. if (_lexer.current() == lex_string)
  8931. {
  8932. // This is either a function call, or not - if not, we shall proceed with location path
  8933. const char_t* state = _lexer.state();
  8934. while (PUGI__IS_CHARTYPE(*state, ct_space)) ++state;
  8935. if (*state != '(') return parse_location_path();
  8936. // This looks like a function call; however this still can be a node-test. Check it.
  8937. if (parse_node_test_type(_lexer.contents()) != nodetest_none) return parse_location_path();
  8938. }
  8939. xpath_ast_node* n = parse_filter_expression();
  8940. if (_lexer.current() == lex_slash || _lexer.current() == lex_double_slash)
  8941. {
  8942. lexeme_t l = _lexer.current();
  8943. _lexer.next();
  8944. if (l == lex_double_slash)
  8945. {
  8946. if (n->rettype() != xpath_type_node_set) throw_error("Step has to be applied to node set");
  8947. n = new (alloc_node()) xpath_ast_node(ast_step, n, axis_descendant_or_self, nodetest_type_node, 0);
  8948. }
  8949. // select from location path
  8950. return parse_relative_location_path(n);
  8951. }
  8952. return n;
  8953. }
  8954. else if (_lexer.current() == lex_minus)
  8955. {
  8956. _lexer.next();
  8957. // precedence 7+ - only parses union expressions
  8958. xpath_ast_node* expr = parse_expression_rec(parse_path_or_unary_expression(), 7);
  8959. return new (alloc_node()) xpath_ast_node(ast_op_negate, xpath_type_number, expr);
  8960. }
  8961. else
  8962. return parse_location_path();
  8963. }
  8964. struct binary_op_t
  8965. {
  8966. ast_type_t asttype;
  8967. xpath_value_type rettype;
  8968. int precedence;
  8969. binary_op_t(): asttype(ast_unknown), rettype(xpath_type_none), precedence(0)
  8970. {
  8971. }
  8972. binary_op_t(ast_type_t asttype_, xpath_value_type rettype_, int precedence_): asttype(asttype_), rettype(rettype_), precedence(precedence_)
  8973. {
  8974. }
  8975. static binary_op_t parse(xpath_lexer& lexer)
  8976. {
  8977. switch (lexer.current())
  8978. {
  8979. case lex_string:
  8980. if (lexer.contents() == PUGIXML_TEXT("or"))
  8981. return binary_op_t(ast_op_or, xpath_type_boolean, 1);
  8982. else if (lexer.contents() == PUGIXML_TEXT("and"))
  8983. return binary_op_t(ast_op_and, xpath_type_boolean, 2);
  8984. else if (lexer.contents() == PUGIXML_TEXT("div"))
  8985. return binary_op_t(ast_op_divide, xpath_type_number, 6);
  8986. else if (lexer.contents() == PUGIXML_TEXT("mod"))
  8987. return binary_op_t(ast_op_mod, xpath_type_number, 6);
  8988. else
  8989. return binary_op_t();
  8990. case lex_equal:
  8991. return binary_op_t(ast_op_equal, xpath_type_boolean, 3);
  8992. case lex_not_equal:
  8993. return binary_op_t(ast_op_not_equal, xpath_type_boolean, 3);
  8994. case lex_less:
  8995. return binary_op_t(ast_op_less, xpath_type_boolean, 4);
  8996. case lex_greater:
  8997. return binary_op_t(ast_op_greater, xpath_type_boolean, 4);
  8998. case lex_less_or_equal:
  8999. return binary_op_t(ast_op_less_or_equal, xpath_type_boolean, 4);
  9000. case lex_greater_or_equal:
  9001. return binary_op_t(ast_op_greater_or_equal, xpath_type_boolean, 4);
  9002. case lex_plus:
  9003. return binary_op_t(ast_op_add, xpath_type_number, 5);
  9004. case lex_minus:
  9005. return binary_op_t(ast_op_subtract, xpath_type_number, 5);
  9006. case lex_multiply:
  9007. return binary_op_t(ast_op_multiply, xpath_type_number, 6);
  9008. case lex_union:
  9009. return binary_op_t(ast_op_union, xpath_type_node_set, 7);
  9010. default:
  9011. return binary_op_t();
  9012. }
  9013. }
  9014. };
  9015. xpath_ast_node* parse_expression_rec(xpath_ast_node* lhs, int limit)
  9016. {
  9017. binary_op_t op = binary_op_t::parse(_lexer);
  9018. while (op.asttype != ast_unknown && op.precedence >= limit)
  9019. {
  9020. _lexer.next();
  9021. xpath_ast_node* rhs = parse_path_or_unary_expression();
  9022. binary_op_t nextop = binary_op_t::parse(_lexer);
  9023. while (nextop.asttype != ast_unknown && nextop.precedence > op.precedence)
  9024. {
  9025. rhs = parse_expression_rec(rhs, nextop.precedence);
  9026. nextop = binary_op_t::parse(_lexer);
  9027. }
  9028. if (op.asttype == ast_op_union && (lhs->rettype() != xpath_type_node_set || rhs->rettype() != xpath_type_node_set))
  9029. throw_error("Union operator has to be applied to node sets");
  9030. lhs = new (alloc_node()) xpath_ast_node(op.asttype, op.rettype, lhs, rhs);
  9031. op = binary_op_t::parse(_lexer);
  9032. }
  9033. return lhs;
  9034. }
  9035. // Expr ::= OrExpr
  9036. // OrExpr ::= AndExpr | OrExpr 'or' AndExpr
  9037. // AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
  9038. // EqualityExpr ::= RelationalExpr
  9039. // | EqualityExpr '=' RelationalExpr
  9040. // | EqualityExpr '!=' RelationalExpr
  9041. // RelationalExpr ::= AdditiveExpr
  9042. // | RelationalExpr '<' AdditiveExpr
  9043. // | RelationalExpr '>' AdditiveExpr
  9044. // | RelationalExpr '<=' AdditiveExpr
  9045. // | RelationalExpr '>=' AdditiveExpr
  9046. // AdditiveExpr ::= MultiplicativeExpr
  9047. // | AdditiveExpr '+' MultiplicativeExpr
  9048. // | AdditiveExpr '-' MultiplicativeExpr
  9049. // MultiplicativeExpr ::= UnaryExpr
  9050. // | MultiplicativeExpr '*' UnaryExpr
  9051. // | MultiplicativeExpr 'div' UnaryExpr
  9052. // | MultiplicativeExpr 'mod' UnaryExpr
  9053. xpath_ast_node* parse_expression()
  9054. {
  9055. return parse_expression_rec(parse_path_or_unary_expression(), 0);
  9056. }
  9057. xpath_parser(const char_t* query, xpath_variable_set* variables, xpath_allocator* alloc, xpath_parse_result* result): _alloc(alloc), _lexer(query), _query(query), _variables(variables), _result(result)
  9058. {
  9059. }
  9060. xpath_ast_node* parse()
  9061. {
  9062. xpath_ast_node* result = parse_expression();
  9063. if (_lexer.current() != lex_eof)
  9064. {
  9065. // there are still unparsed tokens left, error
  9066. throw_error("Incorrect query");
  9067. }
  9068. return result;
  9069. }
  9070. static xpath_ast_node* parse(const char_t* query, xpath_variable_set* variables, xpath_allocator* alloc, xpath_parse_result* result)
  9071. {
  9072. xpath_parser parser(query, variables, alloc, result);
  9073. #ifdef PUGIXML_NO_EXCEPTIONS
  9074. int error = setjmp(parser._error_handler);
  9075. return (error == 0) ? parser.parse() : 0;
  9076. #else
  9077. return parser.parse();
  9078. #endif
  9079. }
  9080. };
  9081. struct xpath_query_impl
  9082. {
  9083. static xpath_query_impl* create()
  9084. {
  9085. void* memory = xml_memory::allocate(sizeof(xpath_query_impl));
  9086. if (!memory) return 0;
  9087. return new (memory) xpath_query_impl();
  9088. }
  9089. static void destroy(xpath_query_impl* impl)
  9090. {
  9091. // free all allocated pages
  9092. impl->alloc.release();
  9093. // free allocator memory (with the first page)
  9094. xml_memory::deallocate(impl);
  9095. }
  9096. xpath_query_impl(): root(0), alloc(&block)
  9097. {
  9098. block.next = 0;
  9099. block.capacity = sizeof(block.data);
  9100. }
  9101. xpath_ast_node* root;
  9102. xpath_allocator alloc;
  9103. xpath_memory_block block;
  9104. };
  9105. PUGI__FN xpath_string evaluate_string_impl(xpath_query_impl* impl, const xpath_node& n, xpath_stack_data& sd)
  9106. {
  9107. if (!impl) return xpath_string();
  9108. #ifdef PUGIXML_NO_EXCEPTIONS
  9109. if (setjmp(sd.error_handler)) return xpath_string();
  9110. #endif
  9111. xpath_context c(n, 1, 1);
  9112. return impl->root->eval_string(c, sd.stack);
  9113. }
  9114. PUGI__FN impl::xpath_ast_node* evaluate_node_set_prepare(xpath_query_impl* impl)
  9115. {
  9116. if (!impl) return 0;
  9117. if (impl->root->rettype() != xpath_type_node_set)
  9118. {
  9119. #ifdef PUGIXML_NO_EXCEPTIONS
  9120. return 0;
  9121. #else
  9122. xpath_parse_result res;
  9123. res.error = "Expression does not evaluate to node set";
  9124. throw xpath_exception(res);
  9125. #endif
  9126. }
  9127. return impl->root;
  9128. }
  9129. PUGI__NS_END
  9130. namespace pugi
  9131. {
  9132. #ifndef PUGIXML_NO_EXCEPTIONS
  9133. PUGI__FN xpath_exception::xpath_exception(const xpath_parse_result& result_): _result(result_)
  9134. {
  9135. assert(_result.error);
  9136. }
  9137. PUGI__FN const char* xpath_exception::what() const throw()
  9138. {
  9139. return _result.error;
  9140. }
  9141. PUGI__FN const xpath_parse_result& xpath_exception::result() const
  9142. {
  9143. return _result;
  9144. }
  9145. #endif
  9146. PUGI__FN xpath_node::xpath_node()
  9147. {
  9148. }
  9149. // Urho3D - copy constructor from another xpath_node
  9150. PUGI__FN xpath_node::xpath_node(const xpath_node& rhs) : _node(rhs._node), _attribute(rhs._attribute)
  9151. {
  9152. }
  9153. PUGI__FN xpath_node::xpath_node(const xml_node& node_): _node(node_)
  9154. {
  9155. }
  9156. PUGI__FN xpath_node::xpath_node(const xml_attribute& attribute_, const xml_node& parent_): _node(attribute_ ? parent_ : xml_node()), _attribute(attribute_)
  9157. {
  9158. }
  9159. PUGI__FN xml_node xpath_node::node() const
  9160. {
  9161. return _attribute ? xml_node() : _node;
  9162. }
  9163. PUGI__FN xml_attribute xpath_node::attribute() const
  9164. {
  9165. return _attribute;
  9166. }
  9167. PUGI__FN xml_node xpath_node::parent() const
  9168. {
  9169. return _attribute ? _node : _node.parent();
  9170. }
  9171. PUGI__FN static void unspecified_bool_xpath_node(xpath_node***)
  9172. {
  9173. }
  9174. PUGI__FN xpath_node::operator xpath_node::unspecified_bool_type() const
  9175. {
  9176. return (_node || _attribute) ? unspecified_bool_xpath_node : 0;
  9177. }
  9178. PUGI__FN bool xpath_node::operator!() const
  9179. {
  9180. return !(_node || _attribute);
  9181. }
  9182. PUGI__FN bool xpath_node::operator==(const xpath_node& n) const
  9183. {
  9184. return _node == n._node && _attribute == n._attribute;
  9185. }
  9186. PUGI__FN bool xpath_node::operator!=(const xpath_node& n) const
  9187. {
  9188. return _node != n._node || _attribute != n._attribute;
  9189. }
  9190. #ifdef __BORLANDC__
  9191. PUGI__FN bool operator&&(const xpath_node& lhs, bool rhs)
  9192. {
  9193. return (bool)lhs && rhs;
  9194. }
  9195. PUGI__FN bool operator||(const xpath_node& lhs, bool rhs)
  9196. {
  9197. return (bool)lhs || rhs;
  9198. }
  9199. #endif
  9200. PUGI__FN void xpath_node_set::_assign(const_iterator begin_, const_iterator end_, type_t type_)
  9201. {
  9202. assert(begin_ <= end_);
  9203. size_t size_ = static_cast<size_t>(end_ - begin_);
  9204. if (size_ <= 1)
  9205. {
  9206. // deallocate old buffer
  9207. if (_begin != &_storage) impl::xml_memory::deallocate(_begin);
  9208. // use internal buffer
  9209. if (begin_ != end_) _storage = *begin_;
  9210. _begin = &_storage;
  9211. _end = &_storage + size_;
  9212. _type = type_;
  9213. }
  9214. else
  9215. {
  9216. // make heap copy
  9217. xpath_node* storage = static_cast<xpath_node*>(impl::xml_memory::allocate(size_ * sizeof(xpath_node)));
  9218. if (!storage)
  9219. {
  9220. #ifdef PUGIXML_NO_EXCEPTIONS
  9221. return;
  9222. #else
  9223. throw std::bad_alloc();
  9224. #endif
  9225. }
  9226. memcpy(storage, begin_, size_ * sizeof(xpath_node));
  9227. // deallocate old buffer
  9228. if (_begin != &_storage) impl::xml_memory::deallocate(_begin);
  9229. // finalize
  9230. _begin = storage;
  9231. _end = storage + size_;
  9232. _type = type_;
  9233. }
  9234. }
  9235. #if __cplusplus >= 201103
  9236. PUGI__FN void xpath_node_set::_move(xpath_node_set& rhs)
  9237. {
  9238. _type = rhs._type;
  9239. _storage = rhs._storage;
  9240. _begin = (rhs._begin == &rhs._storage) ? &_storage : rhs._begin;
  9241. _end = _begin + (rhs._end - rhs._begin);
  9242. rhs._type = type_unsorted;
  9243. rhs._begin = &rhs._storage;
  9244. rhs._end = rhs._begin;
  9245. }
  9246. #endif
  9247. PUGI__FN xpath_node_set::xpath_node_set(): _type(type_unsorted), _begin(&_storage), _end(&_storage)
  9248. {
  9249. }
  9250. PUGI__FN xpath_node_set::xpath_node_set(const_iterator begin_, const_iterator end_, type_t type_): _type(type_unsorted), _begin(&_storage), _end(&_storage)
  9251. {
  9252. _assign(begin_, end_, type_);
  9253. }
  9254. PUGI__FN xpath_node_set::~xpath_node_set()
  9255. {
  9256. if (_begin != &_storage)
  9257. impl::xml_memory::deallocate(_begin);
  9258. }
  9259. PUGI__FN xpath_node_set::xpath_node_set(const xpath_node_set& ns): _type(type_unsorted), _begin(&_storage), _end(&_storage)
  9260. {
  9261. _assign(ns._begin, ns._end, ns._type);
  9262. }
  9263. PUGI__FN xpath_node_set& xpath_node_set::operator=(const xpath_node_set& ns)
  9264. {
  9265. if (this == &ns) return *this;
  9266. _assign(ns._begin, ns._end, ns._type);
  9267. return *this;
  9268. }
  9269. #if __cplusplus >= 201103
  9270. PUGI__FN xpath_node_set::xpath_node_set(xpath_node_set&& rhs): _type(type_unsorted), _begin(&_storage), _end(&_storage)
  9271. {
  9272. _move(rhs);
  9273. }
  9274. PUGI__FN xpath_node_set& xpath_node_set::operator=(xpath_node_set&& rhs)
  9275. {
  9276. if (this == &rhs) return *this;
  9277. if (_begin != &_storage)
  9278. impl::xml_memory::deallocate(_begin);
  9279. _move(rhs);
  9280. return *this;
  9281. }
  9282. #endif
  9283. PUGI__FN xpath_node_set::type_t xpath_node_set::type() const
  9284. {
  9285. return _type;
  9286. }
  9287. PUGI__FN size_t xpath_node_set::size() const
  9288. {
  9289. return _end - _begin;
  9290. }
  9291. PUGI__FN bool xpath_node_set::empty() const
  9292. {
  9293. return _begin == _end;
  9294. }
  9295. PUGI__FN const xpath_node& xpath_node_set::operator[](size_t index) const
  9296. {
  9297. assert(index < size());
  9298. return _begin[index];
  9299. }
  9300. PUGI__FN xpath_node_set::const_iterator xpath_node_set::begin() const
  9301. {
  9302. return _begin;
  9303. }
  9304. PUGI__FN xpath_node_set::const_iterator xpath_node_set::end() const
  9305. {
  9306. return _end;
  9307. }
  9308. PUGI__FN void xpath_node_set::sort(bool reverse)
  9309. {
  9310. _type = impl::xpath_sort(_begin, _end, _type, reverse);
  9311. }
  9312. PUGI__FN xpath_node xpath_node_set::first() const
  9313. {
  9314. return impl::xpath_first(_begin, _end, _type);
  9315. }
  9316. PUGI__FN xpath_parse_result::xpath_parse_result(): error("Internal error"), offset(0)
  9317. {
  9318. }
  9319. PUGI__FN xpath_parse_result::operator bool() const
  9320. {
  9321. return error == 0;
  9322. }
  9323. PUGI__FN const char* xpath_parse_result::description() const
  9324. {
  9325. return error ? error : "No error";
  9326. }
  9327. PUGI__FN xpath_variable::xpath_variable(xpath_value_type type_): _type(type_), _next(0)
  9328. {
  9329. }
  9330. PUGI__FN const char_t* xpath_variable::name() const
  9331. {
  9332. switch (_type)
  9333. {
  9334. case xpath_type_node_set:
  9335. return static_cast<const impl::xpath_variable_node_set*>(this)->name;
  9336. case xpath_type_number:
  9337. return static_cast<const impl::xpath_variable_number*>(this)->name;
  9338. case xpath_type_string:
  9339. return static_cast<const impl::xpath_variable_string*>(this)->name;
  9340. case xpath_type_boolean:
  9341. return static_cast<const impl::xpath_variable_boolean*>(this)->name;
  9342. default:
  9343. assert(!"Invalid variable type");
  9344. return 0;
  9345. }
  9346. }
  9347. PUGI__FN xpath_value_type xpath_variable::type() const
  9348. {
  9349. return _type;
  9350. }
  9351. PUGI__FN bool xpath_variable::get_boolean() const
  9352. {
  9353. return (_type == xpath_type_boolean) ? static_cast<const impl::xpath_variable_boolean*>(this)->value : false;
  9354. }
  9355. PUGI__FN double xpath_variable::get_number() const
  9356. {
  9357. return (_type == xpath_type_number) ? static_cast<const impl::xpath_variable_number*>(this)->value : impl::gen_nan();
  9358. }
  9359. PUGI__FN const char_t* xpath_variable::get_string() const
  9360. {
  9361. const char_t* value = (_type == xpath_type_string) ? static_cast<const impl::xpath_variable_string*>(this)->value : 0;
  9362. return value ? value : PUGIXML_TEXT("");
  9363. }
  9364. PUGI__FN const xpath_node_set& xpath_variable::get_node_set() const
  9365. {
  9366. return (_type == xpath_type_node_set) ? static_cast<const impl::xpath_variable_node_set*>(this)->value : impl::dummy_node_set;
  9367. }
  9368. PUGI__FN bool xpath_variable::set(bool value)
  9369. {
  9370. if (_type != xpath_type_boolean) return false;
  9371. static_cast<impl::xpath_variable_boolean*>(this)->value = value;
  9372. return true;
  9373. }
  9374. PUGI__FN bool xpath_variable::set(double value)
  9375. {
  9376. if (_type != xpath_type_number) return false;
  9377. static_cast<impl::xpath_variable_number*>(this)->value = value;
  9378. return true;
  9379. }
  9380. PUGI__FN bool xpath_variable::set(const char_t* value)
  9381. {
  9382. if (_type != xpath_type_string) return false;
  9383. impl::xpath_variable_string* var = static_cast<impl::xpath_variable_string*>(this);
  9384. // duplicate string
  9385. size_t size = (impl::strlength(value) + 1) * sizeof(char_t);
  9386. char_t* copy = static_cast<char_t*>(impl::xml_memory::allocate(size));
  9387. if (!copy) return false;
  9388. memcpy(copy, value, size);
  9389. // replace old string
  9390. if (var->value) impl::xml_memory::deallocate(var->value);
  9391. var->value = copy;
  9392. return true;
  9393. }
  9394. PUGI__FN bool xpath_variable::set(const xpath_node_set& value)
  9395. {
  9396. if (_type != xpath_type_node_set) return false;
  9397. static_cast<impl::xpath_variable_node_set*>(this)->value = value;
  9398. return true;
  9399. }
  9400. PUGI__FN xpath_variable_set::xpath_variable_set()
  9401. {
  9402. for (size_t i = 0; i < sizeof(_data) / sizeof(_data[0]); ++i)
  9403. _data[i] = 0;
  9404. }
  9405. PUGI__FN xpath_variable_set::~xpath_variable_set()
  9406. {
  9407. for (size_t i = 0; i < sizeof(_data) / sizeof(_data[0]); ++i)
  9408. _destroy(_data[i]);
  9409. }
  9410. PUGI__FN xpath_variable_set::xpath_variable_set(const xpath_variable_set& rhs)
  9411. {
  9412. for (size_t i = 0; i < sizeof(_data) / sizeof(_data[0]); ++i)
  9413. _data[i] = 0;
  9414. _assign(rhs);
  9415. }
  9416. PUGI__FN xpath_variable_set& xpath_variable_set::operator=(const xpath_variable_set& rhs)
  9417. {
  9418. if (this == &rhs) return *this;
  9419. _assign(rhs);
  9420. return *this;
  9421. }
  9422. #if __cplusplus >= 201103
  9423. PUGI__FN xpath_variable_set::xpath_variable_set(xpath_variable_set&& rhs)
  9424. {
  9425. for (size_t i = 0; i < sizeof(_data) / sizeof(_data[0]); ++i)
  9426. {
  9427. _data[i] = rhs._data[i];
  9428. rhs._data[i] = 0;
  9429. }
  9430. }
  9431. PUGI__FN xpath_variable_set& xpath_variable_set::operator=(xpath_variable_set&& rhs)
  9432. {
  9433. for (size_t i = 0; i < sizeof(_data) / sizeof(_data[0]); ++i)
  9434. {
  9435. _destroy(_data[i]);
  9436. _data[i] = rhs._data[i];
  9437. rhs._data[i] = 0;
  9438. }
  9439. return *this;
  9440. }
  9441. #endif
  9442. PUGI__FN void xpath_variable_set::_assign(const xpath_variable_set& rhs)
  9443. {
  9444. xpath_variable_set temp;
  9445. for (size_t i = 0; i < sizeof(_data) / sizeof(_data[0]); ++i)
  9446. if (rhs._data[i] && !_clone(rhs._data[i], &temp._data[i]))
  9447. return;
  9448. _swap(temp);
  9449. }
  9450. PUGI__FN void xpath_variable_set::_swap(xpath_variable_set& rhs)
  9451. {
  9452. for (size_t i = 0; i < sizeof(_data) / sizeof(_data[0]); ++i)
  9453. {
  9454. xpath_variable* chain = _data[i];
  9455. _data[i] = rhs._data[i];
  9456. rhs._data[i] = chain;
  9457. }
  9458. }
  9459. PUGI__FN xpath_variable* xpath_variable_set::_find(const char_t* name) const
  9460. {
  9461. const size_t hash_size = sizeof(_data) / sizeof(_data[0]);
  9462. size_t hash = impl::hash_string(name) % hash_size;
  9463. // look for existing variable
  9464. for (xpath_variable* var = _data[hash]; var; var = var->_next)
  9465. if (impl::strequal(var->name(), name))
  9466. return var;
  9467. return 0;
  9468. }
  9469. PUGI__FN bool xpath_variable_set::_clone(xpath_variable* var, xpath_variable** out_result)
  9470. {
  9471. xpath_variable* last = 0;
  9472. while (var)
  9473. {
  9474. // allocate storage for new variable
  9475. xpath_variable* nvar = impl::new_xpath_variable(var->_type, var->name());
  9476. if (!nvar) return false;
  9477. // link the variable to the result immediately to handle failures gracefully
  9478. if (last)
  9479. last->_next = nvar;
  9480. else
  9481. *out_result = nvar;
  9482. last = nvar;
  9483. // copy the value; this can fail due to out-of-memory conditions
  9484. if (!impl::copy_xpath_variable(nvar, var)) return false;
  9485. var = var->_next;
  9486. }
  9487. return true;
  9488. }
  9489. PUGI__FN void xpath_variable_set::_destroy(xpath_variable* var)
  9490. {
  9491. while (var)
  9492. {
  9493. xpath_variable* next = var->_next;
  9494. impl::delete_xpath_variable(var->_type, var);
  9495. var = next;
  9496. }
  9497. }
  9498. PUGI__FN xpath_variable* xpath_variable_set::add(const char_t* name, xpath_value_type type)
  9499. {
  9500. const size_t hash_size = sizeof(_data) / sizeof(_data[0]);
  9501. size_t hash = impl::hash_string(name) % hash_size;
  9502. // look for existing variable
  9503. for (xpath_variable* var = _data[hash]; var; var = var->_next)
  9504. if (impl::strequal(var->name(), name))
  9505. return var->type() == type ? var : 0;
  9506. // add new variable
  9507. xpath_variable* result = impl::new_xpath_variable(type, name);
  9508. if (result)
  9509. {
  9510. result->_next = _data[hash];
  9511. _data[hash] = result;
  9512. }
  9513. return result;
  9514. }
  9515. PUGI__FN bool xpath_variable_set::set(const char_t* name, bool value)
  9516. {
  9517. xpath_variable* var = add(name, xpath_type_boolean);
  9518. return var ? var->set(value) : false;
  9519. }
  9520. PUGI__FN bool xpath_variable_set::set(const char_t* name, double value)
  9521. {
  9522. xpath_variable* var = add(name, xpath_type_number);
  9523. return var ? var->set(value) : false;
  9524. }
  9525. PUGI__FN bool xpath_variable_set::set(const char_t* name, const char_t* value)
  9526. {
  9527. xpath_variable* var = add(name, xpath_type_string);
  9528. return var ? var->set(value) : false;
  9529. }
  9530. PUGI__FN bool xpath_variable_set::set(const char_t* name, const xpath_node_set& value)
  9531. {
  9532. xpath_variable* var = add(name, xpath_type_node_set);
  9533. return var ? var->set(value) : false;
  9534. }
  9535. PUGI__FN xpath_variable* xpath_variable_set::get(const char_t* name)
  9536. {
  9537. return _find(name);
  9538. }
  9539. PUGI__FN const xpath_variable* xpath_variable_set::get(const char_t* name) const
  9540. {
  9541. return _find(name);
  9542. }
  9543. PUGI__FN xpath_query::xpath_query(const char_t* query, xpath_variable_set* variables): _impl(0)
  9544. {
  9545. impl::xpath_query_impl* qimpl = impl::xpath_query_impl::create();
  9546. if (!qimpl)
  9547. {
  9548. #ifdef PUGIXML_NO_EXCEPTIONS
  9549. _result.error = "Out of memory";
  9550. #else
  9551. throw std::bad_alloc();
  9552. #endif
  9553. }
  9554. else
  9555. {
  9556. using impl::auto_deleter; // MSVC7 workaround
  9557. auto_deleter<impl::xpath_query_impl> impl(qimpl, impl::xpath_query_impl::destroy);
  9558. qimpl->root = impl::xpath_parser::parse(query, variables, &qimpl->alloc, &_result);
  9559. if (qimpl->root)
  9560. {
  9561. qimpl->root->optimize(&qimpl->alloc);
  9562. _impl = impl.release();
  9563. _result.error = 0;
  9564. }
  9565. }
  9566. }
  9567. PUGI__FN xpath_query::xpath_query(): _impl(0)
  9568. {
  9569. }
  9570. PUGI__FN xpath_query::~xpath_query()
  9571. {
  9572. if (_impl)
  9573. impl::xpath_query_impl::destroy(static_cast<impl::xpath_query_impl*>(_impl));
  9574. }
  9575. #if __cplusplus >= 201103
  9576. PUGI__FN xpath_query::xpath_query(xpath_query&& rhs)
  9577. {
  9578. _impl = rhs._impl;
  9579. _result = rhs._result;
  9580. rhs._impl = 0;
  9581. rhs._result = xpath_parse_result();
  9582. }
  9583. PUGI__FN xpath_query& xpath_query::operator=(xpath_query&& rhs)
  9584. {
  9585. if (this == &rhs) return *this;
  9586. if (_impl)
  9587. impl::xpath_query_impl::destroy(static_cast<impl::xpath_query_impl*>(_impl));
  9588. _impl = rhs._impl;
  9589. _result = rhs._result;
  9590. rhs._impl = 0;
  9591. rhs._result = xpath_parse_result();
  9592. return *this;
  9593. }
  9594. #endif
  9595. PUGI__FN xpath_value_type xpath_query::return_type() const
  9596. {
  9597. if (!_impl) return xpath_type_none;
  9598. return static_cast<impl::xpath_query_impl*>(_impl)->root->rettype();
  9599. }
  9600. PUGI__FN bool xpath_query::evaluate_boolean(const xpath_node& n) const
  9601. {
  9602. if (!_impl) return false;
  9603. impl::xpath_context c(n, 1, 1);
  9604. impl::xpath_stack_data sd;
  9605. #ifdef PUGIXML_NO_EXCEPTIONS
  9606. if (setjmp(sd.error_handler)) return false;
  9607. #endif
  9608. return static_cast<impl::xpath_query_impl*>(_impl)->root->eval_boolean(c, sd.stack);
  9609. }
  9610. PUGI__FN double xpath_query::evaluate_number(const xpath_node& n) const
  9611. {
  9612. if (!_impl) return impl::gen_nan();
  9613. impl::xpath_context c(n, 1, 1);
  9614. impl::xpath_stack_data sd;
  9615. #ifdef PUGIXML_NO_EXCEPTIONS
  9616. if (setjmp(sd.error_handler)) return impl::gen_nan();
  9617. #endif
  9618. return static_cast<impl::xpath_query_impl*>(_impl)->root->eval_number(c, sd.stack);
  9619. }
  9620. #ifndef PUGIXML_NO_STL
  9621. PUGI__FN string_t xpath_query::evaluate_string(const xpath_node& n) const
  9622. {
  9623. impl::xpath_stack_data sd;
  9624. impl::xpath_string r = impl::evaluate_string_impl(static_cast<impl::xpath_query_impl*>(_impl), n, sd);
  9625. return string_t(r.c_str(), r.length());
  9626. }
  9627. #endif
  9628. PUGI__FN size_t xpath_query::evaluate_string(char_t* buffer, size_t capacity, const xpath_node& n) const
  9629. {
  9630. impl::xpath_stack_data sd;
  9631. impl::xpath_string r = impl::evaluate_string_impl(static_cast<impl::xpath_query_impl*>(_impl), n, sd);
  9632. size_t full_size = r.length() + 1;
  9633. if (capacity > 0)
  9634. {
  9635. size_t size = (full_size < capacity) ? full_size : capacity;
  9636. assert(size > 0);
  9637. memcpy(buffer, r.c_str(), (size - 1) * sizeof(char_t));
  9638. buffer[size - 1] = 0;
  9639. }
  9640. return full_size;
  9641. }
  9642. PUGI__FN xpath_node_set xpath_query::evaluate_node_set(const xpath_node& n) const
  9643. {
  9644. impl::xpath_ast_node* root = impl::evaluate_node_set_prepare(static_cast<impl::xpath_query_impl*>(_impl));
  9645. if (!root) return xpath_node_set();
  9646. impl::xpath_context c(n, 1, 1);
  9647. impl::xpath_stack_data sd;
  9648. #ifdef PUGIXML_NO_EXCEPTIONS
  9649. if (setjmp(sd.error_handler)) return xpath_node_set();
  9650. #endif
  9651. impl::xpath_node_set_raw r = root->eval_node_set(c, sd.stack, impl::nodeset_eval_all);
  9652. return xpath_node_set(r.begin(), r.end(), r.type());
  9653. }
  9654. PUGI__FN xpath_node xpath_query::evaluate_node(const xpath_node& n) const
  9655. {
  9656. impl::xpath_ast_node* root = impl::evaluate_node_set_prepare(static_cast<impl::xpath_query_impl*>(_impl));
  9657. if (!root) return xpath_node();
  9658. impl::xpath_context c(n, 1, 1);
  9659. impl::xpath_stack_data sd;
  9660. #ifdef PUGIXML_NO_EXCEPTIONS
  9661. if (setjmp(sd.error_handler)) return xpath_node();
  9662. #endif
  9663. impl::xpath_node_set_raw r = root->eval_node_set(c, sd.stack, impl::nodeset_eval_first);
  9664. return r.first();
  9665. }
  9666. PUGI__FN const xpath_parse_result& xpath_query::result() const
  9667. {
  9668. return _result;
  9669. }
  9670. PUGI__FN static void unspecified_bool_xpath_query(xpath_query***)
  9671. {
  9672. }
  9673. PUGI__FN xpath_query::operator xpath_query::unspecified_bool_type() const
  9674. {
  9675. return _impl ? unspecified_bool_xpath_query : 0;
  9676. }
  9677. PUGI__FN bool xpath_query::operator!() const
  9678. {
  9679. return !_impl;
  9680. }
  9681. PUGI__FN xpath_node xml_node::select_node(const char_t* query, xpath_variable_set* variables) const
  9682. {
  9683. xpath_query q(query, variables);
  9684. return select_node(q);
  9685. }
  9686. PUGI__FN xpath_node xml_node::select_node(const xpath_query& query) const
  9687. {
  9688. return query.evaluate_node(*this);
  9689. }
  9690. PUGI__FN xpath_node_set xml_node::select_nodes(const char_t* query, xpath_variable_set* variables) const
  9691. {
  9692. xpath_query q(query, variables);
  9693. return select_nodes(q);
  9694. }
  9695. PUGI__FN xpath_node_set xml_node::select_nodes(const xpath_query& query) const
  9696. {
  9697. return query.evaluate_node_set(*this);
  9698. }
  9699. PUGI__FN xpath_node xml_node::select_single_node(const char_t* query, xpath_variable_set* variables) const
  9700. {
  9701. xpath_query q(query, variables);
  9702. return select_single_node(q);
  9703. }
  9704. PUGI__FN xpath_node xml_node::select_single_node(const xpath_query& query) const
  9705. {
  9706. return query.evaluate_node(*this);
  9707. }
  9708. }
  9709. #endif
  9710. #ifdef __BORLANDC__
  9711. # pragma option pop
  9712. #endif
  9713. // Intel C++ does not properly keep warning state for function templates,
  9714. // so popping warning state at the end of translation unit leads to warnings in the middle.
  9715. #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
  9716. # pragma warning(pop)
  9717. #endif
  9718. // Undefine all local macros (makes sure we're not leaking macros in header-only mode)
  9719. #undef PUGI__NO_INLINE
  9720. #undef PUGI__UNLIKELY
  9721. #undef PUGI__STATIC_ASSERT
  9722. #undef PUGI__DMC_VOLATILE
  9723. #undef PUGI__MSVC_CRT_VERSION
  9724. #undef PUGI__NS_BEGIN
  9725. #undef PUGI__NS_END
  9726. #undef PUGI__FN
  9727. #undef PUGI__FN_NO_INLINE
  9728. #undef PUGI__GETPAGE_IMPL
  9729. #undef PUGI__GETPAGE
  9730. #undef PUGI__NODETYPE
  9731. #undef PUGI__IS_CHARTYPE_IMPL
  9732. #undef PUGI__IS_CHARTYPE
  9733. #undef PUGI__IS_CHARTYPEX
  9734. #undef PUGI__ENDSWITH
  9735. #undef PUGI__SKIPWS
  9736. #undef PUGI__OPTSET
  9737. #undef PUGI__PUSHNODE
  9738. #undef PUGI__POPNODE
  9739. #undef PUGI__SCANFOR
  9740. #undef PUGI__SCANWHILE
  9741. #undef PUGI__SCANWHILE_UNROLL
  9742. #undef PUGI__ENDSEG
  9743. #undef PUGI__THROW_ERROR
  9744. #undef PUGI__CHECK_ERROR
  9745. #endif
  9746. /**
  9747. * Copyright (c) 2006-2015 Arseny Kapoulkine
  9748. *
  9749. * Permission is hereby granted, free of charge, to any person
  9750. * obtaining a copy of this software and associated documentation
  9751. * files (the "Software"), to deal in the Software without
  9752. * restriction, including without limitation the rights to use,
  9753. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  9754. * copies of the Software, and to permit persons to whom the
  9755. * Software is furnished to do so, subject to the following
  9756. * conditions:
  9757. *
  9758. * The above copyright notice and this permission notice shall be
  9759. * included in all copies or substantial portions of the Software.
  9760. *
  9761. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  9762. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  9763. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  9764. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  9765. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  9766. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  9767. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  9768. * OTHER DEALINGS IN THE SOFTWARE.
  9769. */