123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959 |
- { Unicode parser helper unit.
- Copyright (c) 2012-2015 by Inoussa OUEDRAOGO
- The source code is distributed under the Library GNU
- General Public License with the following modification:
- - object files and libraries linked into an application may be
- distributed without source code.
- If you didn't receive a copy of the file COPYING, contact:
- Free Software Foundation
- 675 Mass Ave
- Cambridge, MA 02139
- USA
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. }
- unit helper;
- {$mode delphi}
- {$H+}
- {$PACKENUM 1}
- {$pointermath on}
- {$typedaddress on}
- {$warn 4056 off} //Conversion between ordinals and pointers is not portable
- {$macro on}
- {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
- {$define X_PACKED:=}
- {$else FPC_REQUIRES_PROPER_ALIGNMENT}
- {$define X_PACKED:=packed}
- {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
- interface
- uses
- Classes, SysUtils, StrUtils;
- const
- SLicenseText =
- ' { Unicode implementation tables. ' + sLineBreak +
- ' ' + sLineBreak +
- ' Copyright (c) 2013 - 2017 by Inoussa OUEDRAOGO ' + sLineBreak +
- ' ' + sLineBreak +
- ' Permission is hereby granted, free of charge, to any person ' + sLineBreak +
- ' obtaining a copy of the Unicode data files and any associated ' + sLineBreak +
- ' documentation (the "Data Files") or Unicode software and any ' + sLineBreak +
- ' associated documentation (the "Software") to deal in the Data ' + sLineBreak +
- ' Files or Software without restriction, including without ' + sLineBreak +
- ' limitation the rights to use, copy, modify, merge, publish, ' + sLineBreak +
- ' distribute, and/or sell copies of the Data Files or Software, ' + sLineBreak +
- ' and to permit persons to whom the Data Files or Software are ' + sLineBreak +
- ' furnished to do so, provided that (a) the above copyright ' + sLineBreak +
- ' notice(s) and this permission notice appear with all copies ' + sLineBreak +
- ' of the Data Files or Software, (b) both the above copyright ' + sLineBreak +
- ' notice(s) and this permission notice appear in associated ' + sLineBreak +
- ' documentation, and (c) there is clear notice in each modified ' + sLineBreak +
- ' Data File or in the Software as well as in the documentation ' + sLineBreak +
- ' associated with the Data File(s) or Software that the data or ' + sLineBreak +
- ' software has been modified. ' + sLineBreak +
- ' ' + sLineBreak +
- ' ' + sLineBreak +
- ' This program is distributed in the hope that it will be useful, ' + sLineBreak +
- ' but WITHOUT ANY WARRANTY; without even the implied warranty of ' + sLineBreak +
- ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. }';
- WEIGHT_LEVEL_COUNT = 3;
- type
- // Unicode General Category
- TUnicodeCategory = (
- ucUppercaseLetter, // Lu = Letter, uppercase
- ucLowercaseLetter, // Ll = Letter, lowercase
- ucTitlecaseLetter, // Lt = Letter, titlecase
- ucModifierLetter, // Lm = Letter, modifier
- ucOtherLetter, // Lo = Letter, other
- ucNonSpacingMark, // Mn = Mark, nonspacing
- ucCombiningMark, // Mc = Mark, spacing combining
- ucEnclosingMark, // Me = Mark, enclosing
- ucDecimalNumber, // Nd = Number, decimal digit
- ucLetterNumber, // Nl = Number, letter
- ucOtherNumber, // No = Number, other
- ucConnectPunctuation, // Pc = Punctuation, connector
- ucDashPunctuation, // Pd = Punctuation, dash
- ucOpenPunctuation, // Ps = Punctuation, open
- ucClosePunctuation, // Pe = Punctuation, close
- ucInitialPunctuation, // Pi = Punctuation, initial quote (may behave like Ps or Pe depending on usage)
- ucFinalPunctuation, // Pf = Punctuation, final quote (may behave like Ps or Pe depending on usage)
- ucOtherPunctuation, // Po = Punctuation, other
- ucMathSymbol, // Sm = Symbol, math
- ucCurrencySymbol, // Sc = Symbol, currency
- ucModifierSymbol, // Sk = Symbol, modifier
- ucOtherSymbol, // So = Symbol, other
- ucSpaceSeparator, // Zs = Separator, space
- ucLineSeparator, // Zl = Separator, line
- ucParagraphSeparator, // Zp = Separator, paragraph
- ucControl, // Cc = Other, control
- ucFormat, // Cf = Other, format
- ucSurrogate, // Cs = Other, surrogate
- ucPrivateUse, // Co = Other, private use
- ucUnassigned // Cn = Other, not assigned (including noncharacters)
- );
- TUInt24Rec = packed record
- public
- {$ifdef FPC_LITTLE_ENDIAN}
- byte0, byte1, byte2 : Byte;
- {$else FPC_LITTLE_ENDIAN}
- byte2, byte1, byte0 : Byte;
- {$endif FPC_LITTLE_ENDIAN}
- public
- class operator Implicit(a : TUInt24Rec) : Cardinal;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Implicit(a : TUInt24Rec) : LongInt;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Implicit(a : TUInt24Rec) : Word;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Implicit(a : TUInt24Rec) : Byte;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Implicit(a : Cardinal) : TUInt24Rec;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Explicit(a : TUInt24Rec) : Cardinal;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Equal(a, b: TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Equal(a : TUInt24Rec; b : Cardinal): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Equal(a : Cardinal; b : TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Equal(a : TUInt24Rec; b : LongInt): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Equal(a : LongInt; b : TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Equal(a : TUInt24Rec; b : Word): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Equal(a : Word; b : TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Equal(a : TUInt24Rec; b : Byte): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator Equal(a : Byte; b : TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator NotEqual(a, b: TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator NotEqual(a : TUInt24Rec; b : Cardinal): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator NotEqual(a : Cardinal; b : TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator GreaterThan(a, b: TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator GreaterThan(a : TUInt24Rec; b : Cardinal): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator GreaterThan(a : Cardinal; b : TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator GreaterThanOrEqual(a, b: TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator GreaterThanOrEqual(a : TUInt24Rec; b : Cardinal): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator GreaterThanOrEqual(a : Cardinal; b : TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator LessThan(a, b: TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator LessThan(a : TUInt24Rec; b : Cardinal): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator LessThan(a : Cardinal; b : TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator LessThanOrEqual(a, b: TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator LessThanOrEqual(a : TUInt24Rec; b : Cardinal): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- class operator LessThanOrEqual(a : Cardinal; b : TUInt24Rec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- end;
- UInt24 = TUInt24Rec;
- PUInt24 = ^UInt24;
- TUnicodeCodePoint = Cardinal;
- TUnicodeCodePointArray = array of TUnicodeCodePoint;
- TDecompositionArray = array of TUnicodeCodePointArray;
- TNumericValue = Double;
- TNumericValueArray = array of TNumericValue;
- TBlockItemRec = packed record
- RangeStart : TUnicodeCodePoint;
- RangeEnd : TUnicodeCodePoint;
- Name : string[120];
- CanonicalName : string[120];
- end;
- TBlocks = array of TBlockItemRec;
- PPropRec = ^TPropRec;
- { TPropRec }
- TPropRec = packed record
- private
- function GetCategory : TUnicodeCategory;inline;
- procedure SetCategory(AValue : TUnicodeCategory);
- function GetWhiteSpace : Boolean;inline;
- procedure SetWhiteSpace(AValue : Boolean);
- function GetHangulSyllable : Boolean;inline;
- procedure SetHangulSyllable(AValue : Boolean);
- public
- CategoryData : Byte;
- PropID : Word;
- CCC : Byte; // Canonical Combining Class
- NumericIndex : Byte;
- SimpleUpperCase : UInt24;
- SimpleLowerCase : UInt24;
- DecompositionID : SmallInt;
- public
- property Category : TUnicodeCategory read GetCategory write SetCategory;
- property WhiteSpace : Boolean read GetWhiteSpace write SetWhiteSpace;
- property HangulSyllable : Boolean read GetHangulSyllable write SetHangulSyllable;
- end;
- TPropRecArray = array of TPropRec;
- TDecompositionIndexRec = packed record
- StartPosition : Word;
- Length : Byte;
- end;
- TDecompositionBook = X_PACKED record
- Index : array of TDecompositionIndexRec;
- CodePoints : array of TUnicodeCodePoint;
- end;
- PDataLineRec = ^TDataLineRec;
- TDataLineRec = record
- PropID : Integer;
- case LineType : Byte of
- 0 : (CodePoint : TUnicodeCodePoint);
- 1 : (StartCodePoint, EndCodePoint : TUnicodeCodePoint);
- end;
- TDataLineRecArray = array of TDataLineRec;
- TCodePointRec = record
- case LineType : Byte of
- 0 : (CodePoint : TUnicodeCodePoint);
- 1 : (StartCodePoint, EndCodePoint : TUnicodeCodePoint);
- end;
- TCodePointRecArray = array of TCodePointRec;
- TPropListLineRec = packed record
- CodePoint : TCodePointRec;
- PropName : string[123];
- end;
- TPropListLineRecArray = array of TPropListLineRec;
- { TUCA_WeightRec }
- TUCA_WeightRec = packed record
- public
- Weights : array[0..3] of Cardinal;
- Variable : Boolean;
- public
- class operator Equal(a, b: TUCA_WeightRec): Boolean;{$ifdef USE_INLINE}inline;{$ENDIF}
- end;
- TUCA_WeightRecArray = array of TUCA_WeightRec;
- PUCA_LineContextItemRec = ^TUCA_LineContextItemRec;
- TUCA_LineContextItemRec = X_PACKED record
- public
- CodePoints : TUnicodeCodePointArray;
- Weights : TUCA_WeightRecArray;
- public
- procedure Clear();
- procedure Assign(ASource : PUCA_LineContextItemRec);
- function Clone() : TUCA_LineContextItemRec;
- end;
- PUCA_LineContextRec = ^TUCA_LineContextRec;
- TUCA_LineContextRec = X_PACKED record
- public
- Data : array of TUCA_LineContextItemRec;
- public
- procedure Clear();
- procedure Assign(ASource : PUCA_LineContextRec);
- function Clone() : TUCA_LineContextRec;
- end;
- PUCA_LineRec = ^TUCA_LineRec;
- TUCA_LineRec = X_PACKED record
- public
- CodePoints : TUnicodeCodePointArray;
- Weights : TUCA_WeightRecArray;
- Context : TUCA_LineContextRec;
- //Variable : Boolean;
- Deleted : Boolean;
- Stored : Boolean;
- public
- procedure Clear();
- procedure Assign(ASource : PUCA_LineRec);
- function Clone() : TUCA_LineRec;
- function HasContext() : Boolean;
- end;
- TUCA_VariableKind = (
- ucaShifted, ucaNonIgnorable, ucaBlanked, ucaShiftedTrimmed,
- ucaIgnoreSP
- );
- TUCA_DataBook = X_PACKED record
- Version : string;
- VariableWeight : TUCA_VariableKind;
- Backwards : array[0..3] of Boolean;
- Lines : array of TUCA_LineRec;
- end;
- PUCA_DataBook = ^TUCA_DataBook;
- TUCA_DataBookIndex = array of Integer;
- type
- TUCA_PropWeights = packed record
- Weights : array[0..2] of Word;
- //Variable : Byte;
- end;
- PUCA_PropWeights = ^TUCA_PropWeights;
- TUCA_PropItemContextRec = packed record
- CodePointCount : Byte;
- WeightCount : Byte;
- //CodePoints : UInt24;
- //Weights : TUCA_PropWeights;
- end;
- PUCA_PropItemContextRec = ^TUCA_PropItemContextRec;
- TUCA_PropItemContextTreeNodeRec = packed record
- Left : Word;
- Right : Word;
- Data : TUCA_PropItemContextRec;
- end;
- PUCA_PropItemContextTreeNodeRec = ^TUCA_PropItemContextTreeNodeRec;
- TUCA_PropItemContextTreeRec = packed record
- public
- Size : UInt24;
- public
- function GetData:PUCA_PropItemContextTreeNodeRec;inline;
- property Data : PUCA_PropItemContextTreeNodeRec read GetData;
- end;
- PUCA_PropItemContextTreeRec = ^TUCA_PropItemContextTreeRec;
- { TUCA_PropItemRec }
- TUCA_PropItemRec = packed record
- private
- const FLAG_VALID = 0;
- const FLAG_CODEPOINT = 1;
- const FLAG_CONTEXTUAL = 2;
- const FLAG_DELETION = 3;
- const FLAG_COMPRESS_WEIGHT_1 = 6;
- const FLAG_COMPRESS_WEIGHT_2 = 7;
- private
- function GetWeightSize : Word;inline;
- public
- WeightLength : Byte;
- ChildCount : Byte;
- Size : Word;
- Flags : Byte;
- public
- function HasCodePoint() : Boolean;inline;
- function GetCodePoint() : UInt24;//inline;
- property CodePoint : UInt24 read GetCodePoint;
- //Weights : array[0..WeightLength] of TUCA_PropWeights;
- procedure GetWeightArray(ADest : PUCA_PropWeights);
- function GetSelfOnlySize() : Cardinal;inline;
- procedure SetContextual(AValue : Boolean);inline;
- function GetContextual() : Boolean;inline;
- property Contextual : Boolean read GetContextual write setContextual;
- function GetContext() : PUCA_PropItemContextTreeRec;
- procedure SetDeleted(AValue : Boolean);inline;
- function IsDeleted() : Boolean;inline;
- function IsValid() : Boolean;inline;
- function IsWeightCompress_1() : Boolean;inline;
- function IsWeightCompress_2() : Boolean;inline;
- end;
- PUCA_PropItemRec = ^TUCA_PropItemRec;
- TUCA_PropIndexItem = packed record
- CodePoint : Cardinal;
- Position : Integer;
- end;
- PUCA_PropIndexItem = ^TUCA_PropIndexItem;
- TUCA_PropBook = X_PACKED record
- ItemSize : Integer;
- Index : array of TUCA_PropIndexItem;
- Items : PUCA_PropItemRec; //Native Endian
- ItemsOtherEndian : PUCA_PropItemRec;//Non Native Endian
- VariableLowLimit : Word;
- VariableHighLimit : Word;
- end;
- PUCA_PropBook = ^TUCA_PropBook;
- TBmpFirstTable = array[0..255] of Byte;
- TBmpSecondTableItem = array[0..255] of Word;
- TBmpSecondTable = array of TBmpSecondTableItem;
- T3lvlBmp1Table = array[0..255] of Byte;
- T3lvlBmp2TableItem = array[0..15] of Word;
- T3lvlBmp2Table = array of T3lvlBmp2TableItem;
- T3lvlBmp3TableItem = array[0..15] of Word;
- T3lvlBmp3Table = array of T3lvlBmp3TableItem;
- TucaBmpFirstTable = array[0..255] of Byte;
- TucaBmpSecondTableItem = array[0..255] of Cardinal;
- TucaBmpSecondTable = array of TucaBmpSecondTableItem;
- PucaBmpFirstTable = ^TucaBmpFirstTable;
- PucaBmpSecondTable = ^TucaBmpSecondTable;
- const
- LOW_SURROGATE_BEGIN = Word($DC00);
- LOW_SURROGATE_END = Word($DFFF);
- LOW_SURROGATE_COUNT = LOW_SURROGATE_END - LOW_SURROGATE_BEGIN + 1;
- HIGH_SURROGATE_BEGIN = Word($D800);
- HIGH_SURROGATE_END = Word($DBFF);
- HIGH_SURROGATE_COUNT = HIGH_SURROGATE_END - HIGH_SURROGATE_BEGIN + 1;
- type
- TOBmpFirstTable = array[0..(HIGH_SURROGATE_COUNT-1)] of Word;
- TOBmpSecondTableItem = array[0..(LOW_SURROGATE_COUNT-1)] of Word;
- TOBmpSecondTable = array of TOBmpSecondTableItem;
- T3lvlOBmp1Table = array[0..1023] of Byte;
- T3lvlOBmp2TableItem = array[0..31] of Word;
- T3lvlOBmp2Table = array of T3lvlOBmp2TableItem;
- T3lvlOBmp3TableItem = array[0..31] of Word;
- T3lvlOBmp3Table = array of T3lvlOBmp3TableItem;
- TucaOBmpFirstTable = array[0..(HIGH_SURROGATE_COUNT-1)] of Word;
- TucaOBmpSecondTableItem = array[0..(LOW_SURROGATE_COUNT-1)] of Cardinal;
- TucaOBmpSecondTable = array of TucaOBmpSecondTableItem;
- PucaOBmpFirstTable = ^TucaOBmpFirstTable;
- PucaOBmpSecondTable = ^TucaOBmpSecondTable;
- type
- TEndianKind = (ekLittle, ekBig);
- const
- ENDIAN_SUFFIX : array[TEndianKind] of string[2] = ('le','be');
- {$IFDEF ENDIAN_LITTLE}
- ENDIAN_NATIVE = ekLittle;
- ENDIAN_NON_NATIVE = ekBig;
- {$ENDIF ENDIAN_LITTLE}
- {$IFDEF ENDIAN_BIG}
- ENDIAN_NATIVE = ekBig;
- ENDIAN_NON_NATIVE = ekLittle;
- {$ENDIF ENDIAN_BIG}
- procedure GenerateLicenceText(ADest : TStream);
- function BoolToByte(AValue : Boolean): Byte;inline;
- function IsHangulSyllable(
- const ACodePoint : TUnicodeCodePoint;
- const AHangulList : TCodePointRecArray
- ) : Boolean;
- procedure ParseHangulSyllableTypes(
- ADataAStream : TMemoryStream;
- var ACodePointList : TCodePointRecArray
- );
- procedure ParseProps(
- ADataAStream : TMemoryStream;
- var APropList : TPropListLineRecArray
- );
- function FindCodePointsByProperty(
- const APropName : string;
- const APropList : TPropListLineRecArray
- ) : TCodePointRecArray;
- procedure ParseBlokcs(
- ADataAStream : TMemoryStream;
- var ABlocks : TBlocks
- );
- procedure ParseUCAFile(
- ADataAStream : TMemoryStream;
- var ABook : TUCA_DataBook
- );
- procedure MakeUCA_Props(
- ABook : PUCA_DataBook;
- out AProps : PUCA_PropBook
- );
- procedure FreeUcaBook(var ABook : PUCA_PropBook);
- procedure MakeUCA_BmpTables(
- var AFirstTable : TucaBmpFirstTable;
- var ASecondTable : TucaBmpSecondTable;
- const APropBook : PUCA_PropBook
- );
- procedure MakeUCA_OBmpTables(
- var AFirstTable : TucaOBmpFirstTable;
- var ASecondTable : TucaOBmpSecondTable;
- const APropBook : PUCA_PropBook
- );
- function GetPropPosition(
- const AHighS,
- ALowS : Word;
- const AFirstTable : PucaOBmpFirstTable;
- const ASecondTable : PucaOBmpSecondTable
- ): Integer;inline;overload;
- procedure GenerateUCA_Head(
- ADest : TStream;
- ABook : PUCA_DataBook;
- AProps : PUCA_PropBook
- );
- procedure GenerateUCA_BmpTables(
- AStream,
- ANativeEndianStream,
- ANonNativeEndianStream : TStream;
- var AFirstTable : TucaBmpFirstTable;
- var ASecondTable : TucaBmpSecondTable
- );
- procedure GenerateBinaryUCA_BmpTables(
- ANativeEndianStream,
- ANonNativeEndianStream : TStream;
- var AFirstTable : TucaBmpFirstTable;
- var ASecondTable : TucaBmpSecondTable
- );
- procedure GenerateUCA_PropTable(
- ADest : TStream;
- const APropBook : PUCA_PropBook;
- const AEndian : TEndianKind
- );
- procedure GenerateBinaryUCA_PropTable(
- // WARNING : files must be generated for each endianess (Little / Big)
- ANativeEndianStream,
- ANonNativeEndianStream : TStream;
- const APropBook : PUCA_PropBook
- );
- procedure GenerateUCA_OBmpTables(
- AStream,
- ANativeEndianStream,
- ANonNativeEndianStream : TStream;
- var AFirstTable : TucaOBmpFirstTable;
- var ASecondTable : TucaOBmpSecondTable
- );
- procedure GenerateBinaryUCA_OBmpTables(
- ANativeEndianStream,
- ANonNativeEndianStream : TStream;
- var AFirstTable : TucaOBmpFirstTable;
- var ASecondTable : TucaOBmpSecondTable
- );
- procedure Parse_UnicodeData(
- ADataAStream : TMemoryStream;
- var APropList : TPropRecArray;
- var ANumericTable : TNumericValueArray;
- var ADataLineList : TDataLineRecArray;
- var ADecomposition : TDecompositionArray;
- const AHangulList : TCodePointRecArray;
- const AWhiteSpaces : TCodePointRecArray
- );
- procedure MakeDecomposition(
- const ARawData : TDecompositionArray;
- var ABook : TDecompositionBook
- );
- procedure MakeBmpTables(
- var AFirstTable : TBmpFirstTable;
- var ASecondTable : TBmpSecondTable;
- const ADataLineList : TDataLineRecArray
- );
- procedure MakeBmpTables3Levels(
- var AFirstTable : T3lvlBmp1Table;
- var ASecondTable : T3lvlBmp2Table;
- var AThirdTable : T3lvlBmp3Table;
- const ADataLineList : TDataLineRecArray
- );
- procedure GenerateBmpTables(
- ADest : TStream;
- var AFirstTable : TBmpFirstTable;
- var ASecondTable : TBmpSecondTable
- );
- procedure Generate3lvlBmpTables(
- ADest : TStream;
- var AFirstTable : T3lvlBmp1Table;
- var ASecondTable : T3lvlBmp2Table;
- var AThirdTable : T3lvlBmp3Table
- );
- procedure GeneratePropTable(
- ADest : TStream;
- const APropList : TPropRecArray;
- const AEndian : TEndianKind
- );
- procedure GenerateNumericTable(
- ADest : TStream;
- const ANumList : TNumericValueArray;
- const ACompleteUnit : Boolean
- );
- procedure GenerateDecompositionBookTable(
- ADest : TStream;
- const ABook : TDecompositionBook;
- const AEndian : TEndianKind
- );
- procedure GenerateOutBmpTable(
- ADest : TStream;
- const AList : TDataLineRecArray
- );
- function Compress(const AData : TDataLineRecArray) : TDataLineRecArray;
- function EvaluateFloat(const AStr : string) : Double;
- function StrToCategory(const AStr : string) : TUnicodeCategory;
- function StringToCodePoint(ACP : string) : TUnicodeCodePoint;
- function IsWhiteSpace(
- const ACodePoint : TUnicodeCodePoint;
- const AWhiteSpaces : TCodePointRecArray
- ) : Boolean;
- function GetPropID(
- ACodePoint : TUnicodeCodePoint;
- const ADataLineList : TDataLineRecArray
- ) : Cardinal;
- //--------------------
- procedure MakeOBmpTables(
- var AFirstTable : TOBmpFirstTable;
- var ASecondTable : TOBmpSecondTable;
- const ADataLineList : TDataLineRecArray
- );
- procedure MakeOBmpTables3Levels(
- var AFirstTable : T3lvlOBmp1Table;
- var ASecondTable : T3lvlOBmp2Table;
- var AThirdTable : T3lvlOBmp3Table;
- const ADataLineList : TDataLineRecArray
- );
- procedure GenerateOBmpTables(
- ADest : TStream;
- var AFirstTable : TOBmpFirstTable;
- var ASecondTable : TOBmpSecondTable
- );
- procedure Generate3lvlOBmpTables(
- ADest : TStream;
- var AFirstTable : T3lvlOBmp1Table;
- var ASecondTable : T3lvlOBmp2Table;
- var AThirdTable : T3lvlOBmp3Table
- );
- function GetProp(
- const AHighS,
- ALowS : Word;
- const AProps : TPropRecArray;
- var AFirstTable : TOBmpFirstTable;
- var ASecondTable : TOBmpSecondTable
- ): PPropRec; inline;overload;
- function GetProp(
- const AHighS,
- ALowS : Word;
- const AProps : TPropRecArray;
- var AFirstTable : T3lvlOBmp1Table;
- var ASecondTable : T3lvlOBmp2Table;
- var AThirdTable : T3lvlOBmp3Table
- ): PPropRec; inline;overload;
- procedure FromUCS4(const AValue : TUnicodeCodePoint; var AHighS, ALowS : Word);inline;
- function ToUCS4(const AHighS, ALowS : Word) : TUnicodeCodePoint; inline;
- type
- TBitOrder = 0..7;
- function IsBitON(const AData : Byte; const ABit : TBitOrder) : Boolean ;{$IFDEF USE_INLINE}inline;{$ENDIF}
- procedure SetBit(var AData : Byte; const ABit : TBitOrder; const AValue : Boolean);{$IFDEF USE_INLINE}inline;{$ENDIF}
- function GenerateEndianIncludeFileName(
- const AStoreName : string;
- const AEndian : TEndianKind
- ): string;inline;
- procedure ReverseFromNativeEndian(
- const AData : PUCA_PropItemRec;
- const ADataLen : Cardinal;
- const ADest : PUCA_PropItemRec
- );
- procedure ReverseToNativeEndian(
- const AData : PUCA_PropItemRec;
- const ADataLen : Cardinal;
- const ADest : PUCA_PropItemRec
- );
- procedure CompareProps(
- const AProp1,
- AProp2 : PUCA_PropItemRec;
- const ADataLen : Integer
- );
- type
- TCollationName = array[0..(128-1)] of Byte;
- TCollationVersion = TCollationName;
- TSerializedCollationHeader = packed record
- Base : TCollationName;
- Version : TCollationVersion;
- CollationName : TCollationName;
- CollationAliases : TCollationName; // ";" separated
- VariableWeight : Byte;
- Backwards : Byte;
- BMP_Table1Length : DWord;
- BMP_Table2Length : DWord;
- OBMP_Table1Length : DWord;
- OBMP_Table2Length : DWord;
- PropCount : DWord;
- VariableLowLimit : Word;
- VariableHighLimit : Word;
- NoNormalization : Byte;
- Strength : Byte;
- ChangedFields : Byte;
- end;
- PSerializedCollationHeader = ^TSerializedCollationHeader;
- procedure StringToByteArray(AStr : UnicodeString; var ABuffer : array of Byte);overload;
- procedure StringToByteArray(AStr : UnicodeString; ABuffer : PByte; const ABufferLength : Integer);overload;
- procedure ReverseRecordBytes(var AItem : TSerializedCollationHeader);
- procedure ReverseBytes(var AData; const ALength : Integer);
- procedure ReverseArray(var AValue; const AArrayLength, AItemSize : PtrInt);
- function CalcMaxLevel2Value(ALines : array of TUCA_LineRec) : Cardinal;
- procedure RewriteLevel2Values(ALines : PUCA_LineRec; ALength : Integer);
- function RewriteLevel2(
- const ALevel1Value : Cardinal;
- ALines : PUCA_LineRec;
- const ALinesLength : Integer
- ) : Integer;
- resourcestring
- SInsufficientMemoryBuffer = 'Insufficient Memory Buffer';
- implementation
- uses
- typinfo, Math, AVL_Tree,
- trie;
- type
- TCardinalRec = packed record
- {$ifdef FPC_LITTLE_ENDIAN}
- byte0, byte1, byte2, byte3 : Byte;
- {$else FPC_LITTLE_ENDIAN}
- byte3, byte2, byte1, byte0 : Byte;
- {$endif FPC_LITTLE_ENDIAN}
- end;
- TWordRec = packed record
- {$ifdef FPC_LITTLE_ENDIAN}
- byte0, byte1 : Byte;
- {$else FPC_LITTLE_ENDIAN}
- byte1, byte0 : Byte;
- {$endif FPC_LITTLE_ENDIAN}
- end;
- { TUInt24Rec }
- class operator TUInt24Rec.Explicit(a : TUInt24Rec) : Cardinal;
- begin
- TCardinalRec(Result).byte0 := a.byte0;
- TCardinalRec(Result).byte1 := a.byte1;
- TCardinalRec(Result).byte2 := a.byte2;
- TCardinalRec(Result).byte3 := 0;
- end;
- class operator TUInt24Rec.Implicit(a : TUInt24Rec) : Cardinal;
- begin
- TCardinalRec(Result).byte0 := a.byte0;
- TCardinalRec(Result).byte1 := a.byte1;
- TCardinalRec(Result).byte2 := a.byte2;
- TCardinalRec(Result).byte3 := 0;
- end;
- class operator TUInt24Rec.Implicit(a : TUInt24Rec) : LongInt;
- begin
- Result := Cardinal(a);
- end;
- class operator TUInt24Rec.Implicit(a : TUInt24Rec) : Word;
- begin
- {$IFOPT R+}
- if (a.byte2 > 0) then
- Error(reIntOverflow);
- {$ENDIF R+}
- TWordRec(Result).byte0 := a.byte0;
- TWordRec(Result).byte1 := a.byte1;
- end;
- class operator TUInt24Rec.Implicit(a : TUInt24Rec) : Byte;
- begin
- {$IFOPT R+}
- if (a.byte1 > 0) or (a.byte2 > 0) then
- Error(reIntOverflow);
- {$ENDIF R+}
- Result := a.byte0;
- end;
- class operator TUInt24Rec.Implicit(a : Cardinal) : TUInt24Rec;
- begin
- {$IFOPT R+}
- if (a > $FFFFFF) then
- Error(reIntOverflow);
- {$ENDIF R+}
- Result.byte0 := TCardinalRec(a).byte0;
- Result.byte1 := TCardinalRec(a).byte1;
- Result.byte2 := TCardinalRec(a).byte2;
- end;
- class operator TUInt24Rec.Equal(a, b : TUInt24Rec) : Boolean;
- begin
- Result := (a.byte0 = b.byte0) and (a.byte1 = b.byte1) and (a.byte2 = b.byte2);
- end;
- class operator TUInt24Rec.Equal(a : TUInt24Rec; b : Cardinal) : Boolean;
- begin
- Result := (TCardinalRec(b).byte3 = 0) and
- (a.byte0 = TCardinalRec(b).byte0) and
- (a.byte1 = TCardinalRec(b).byte1) and
- (a.byte2 = TCardinalRec(b).byte2);
- end;
- class operator TUInt24Rec.Equal(a : Cardinal; b : TUInt24Rec) : Boolean;
- begin
- Result := (b = a);
- end;
- class operator TUInt24Rec.Equal(a : TUInt24Rec; b : LongInt) : Boolean;
- begin
- Result := (LongInt(a) = b);
- end;
- class operator TUInt24Rec.Equal(a : LongInt; b : TUInt24Rec) : Boolean;
- begin
- Result := (b = a);
- end;
- class operator TUInt24Rec.Equal(a : TUInt24Rec; b : Word) : Boolean;
- begin
- Result := (a.byte2 = 0) and
- (a.byte0 = TWordRec(b).byte0) and
- (a.byte1 = TWordRec(b).byte1);
- end;
- class operator TUInt24Rec.Equal(a : Word; b : TUInt24Rec) : Boolean;
- begin
- Result := (b = a);
- end;
- class operator TUInt24Rec.Equal(a : TUInt24Rec; b : Byte) : Boolean;
- begin
- Result := (a.byte2 = 0) and
- (a.byte1 = 0) and
- (a.byte0 = b);
- end;
- class operator TUInt24Rec.Equal(a : Byte; b : TUInt24Rec) : Boolean;
- begin
- Result := (b = a);
- end;
- class operator TUInt24Rec.NotEqual(a, b : TUInt24Rec) : Boolean;
- begin
- Result := (a.byte0 <> b.byte0) or (a.byte1 <> b.byte1) or (a.byte2 <> b.byte2);
- end;
- class operator TUInt24Rec.NotEqual(a : TUInt24Rec; b : Cardinal) : Boolean;
- begin
- Result := (TCardinalRec(b).byte3 <> 0) or
- (a.byte0 <> TCardinalRec(b).byte0) or
- (a.byte1 <> TCardinalRec(b).byte1) or
- (a.byte2 <> TCardinalRec(b).byte2);
- end;
- class operator TUInt24Rec.NotEqual(a : Cardinal; b : TUInt24Rec) : Boolean;
- begin
- Result := (b <> a);
- end;
- class operator TUInt24Rec.GreaterThan(a, b: TUInt24Rec): Boolean;
- begin
- Result := (a.byte2 > b.byte2) or
- ((a.byte2 = b.byte2) and (a.byte1 > b.byte1)) or
- ((a.byte2 = b.byte2) and (a.byte1 = b.byte1) and (a.byte0 > b.byte0));
- end;
- class operator TUInt24Rec.GreaterThan(a: TUInt24Rec; b: Cardinal): Boolean;
- begin
- Result := Cardinal(a) > b;
- end;
- class operator TUInt24Rec.GreaterThan(a: Cardinal; b: TUInt24Rec): Boolean;
- begin
- Result := a > Cardinal(b);
- end;
- class operator TUInt24Rec.GreaterThanOrEqual(a, b: TUInt24Rec): Boolean;
- begin
- Result := (a.byte2 > b.byte2) or
- ((a.byte2 = b.byte2) and (a.byte1 > b.byte1)) or
- ((a.byte2 = b.byte2) and (a.byte1 = b.byte1) and (a.byte0 >= b.byte0));
- end;
- class operator TUInt24Rec.GreaterThanOrEqual(a: TUInt24Rec; b: Cardinal): Boolean;
- begin
- Result := Cardinal(a) >= b;
- end;
- class operator TUInt24Rec.GreaterThanOrEqual(a: Cardinal; b: TUInt24Rec): Boolean;
- begin
- Result := a >= Cardinal(b);
- end;
- class operator TUInt24Rec.LessThan(a, b: TUInt24Rec): Boolean;
- begin
- Result := (b > a);
- end;
- class operator TUInt24Rec.LessThan(a: TUInt24Rec; b: Cardinal): Boolean;
- begin
- Result := Cardinal(a) < b;
- end;
- class operator TUInt24Rec.LessThan(a: Cardinal; b: TUInt24Rec): Boolean;
- begin
- Result := a < Cardinal(b);
- end;
- class operator TUInt24Rec.LessThanOrEqual(a, b: TUInt24Rec): Boolean;
- begin
- Result := (b >= a);
- end;
- class operator TUInt24Rec.LessThanOrEqual(a: TUInt24Rec; b: Cardinal): Boolean;
- begin
- Result := Cardinal(a) <= b;
- end;
- class operator TUInt24Rec.LessThanOrEqual(a: Cardinal; b: TUInt24Rec): Boolean;
- begin
- Result := a <= Cardinal(b);
- end;
- { TUCA_WeightRec }
- class operator TUCA_WeightRec.Equal(a, b : TUCA_WeightRec) : Boolean;
- begin
- Result := (a.Weights[0] = b.Weights[0]) and (a.Weights[1] = b.Weights[1]) and
- (a.Weights[2] = b.Weights[2]) and (a.Weights[3] = b.Weights[3]) and
- (a.Variable = b.Variable);
- end;
- procedure StringToByteArray(AStr : UnicodeString; var ABuffer : array of Byte);
- begin
- StringToByteArray(AStr,@(ABuffer[Low(ABuffer)]),Length(ABuffer));
- end;
- procedure StringToByteArray(AStr : UnicodeString; ABuffer : PByte; const ABufferLength : Integer);
- var
- c, i, bl : Integer;
- ps : PWord;
- pb : PByte;
- begin
- if (ABufferLength < 1) then
- exit;
- c := Length(AStr);
- if (c > ABufferLength) then
- c := ABufferLength;
- bl := 0;
- pb := ABuffer;
- if (c > 0) then begin
- ps := PWord(@AStr[1]);
- for i := 1 to c do begin
- if (ps^ <= High(Byte)) then begin
- pb^ := ps^;
- bl := bl+1;
- Inc(pb);
- end;
- Inc(ps);
- end;
- end;
- if (bl < ABufferLength) then begin
- for i := bl+1 to ABufferLength do begin
- pb^:= 0;
- Inc(pb);
- end;
- end;
- end;
- function GenerateEndianIncludeFileName(
- const AStoreName : string;
- const AEndian : TEndianKind
- ): string;inline;
- begin
- Result := ExtractFilePath(AStoreName) +
- ChangeFileExt(ExtractFileName(AStoreName),Format('_%s.inc',[ENDIAN_SUFFIX[AEndian]]));
- end;
- function IsBitON(const AData : Byte; const ABit : TBitOrder) : Boolean ;
- begin
- Result := ( ( AData and ( 1 shl ABit ) ) <> 0 );
- end;
- procedure SetBit(var AData : Byte; const ABit : TBitOrder; const AValue : Boolean);
- begin
- if AValue then
- AData := AData or (1 shl (ABit mod 8))
- else
- AData := AData and ( not ( 1 shl ( ABit mod 8 ) ) );
- end;
- var
- FS : TFormatSettings;
- function EvaluateFloat(const AStr : string) : Double;
- var
- s, n, d : string;
- i : Integer;
- begin
- Result := 0;
- s := Trim(AStr);
- if (Length(s) > 0) then begin
- i := Pos('/',s);
- if (i < 1) then
- Result := StrToFloat(s,FS)
- else begin
- n := Copy(s,1,i-1);
- d := Copy(s,i+1,MaxInt);
- Result := StrToInt(n) / StrToInt(d);
- end;
- end;
- end;
- function StrToCategory(const AStr : string) : TUnicodeCategory;
- var
- s : string;
- begin
- s := UpperCase(Trim(AStr));
- if (s = 'LU') then
- Result := ucUppercaseLetter
- else if (s = 'LL') then
- Result := ucLowercaseLetter
- else if (s = 'LT') then
- Result := ucTitlecaseLetter
- else if (s = 'LM') then
- Result := ucModifierLetter
- else if (s = 'LO') then
- Result := ucOtherLetter
- else
- if (s = 'MN') then
- Result := ucNonSpacingMark
- else if (s = 'MC') then
- Result := ucCombiningMark
- else if (s = 'ME') then
- Result := ucEnclosingMark
- else
- if (s = 'ND') then
- Result := ucDecimalNumber
- else if (s = 'NL') then
- Result := ucLetterNumber
- else if (s = 'NO') then
- Result := ucOtherNumber
- else
- if (s = 'PC') then
- Result := ucConnectPunctuation
- else if (s = 'PD') then
- Result := ucDashPunctuation
- else if (s = 'PS') then
- Result := ucOpenPunctuation
- else if (s = 'PE') then
- Result := ucClosePunctuation
- else if (s = 'PI') then
- Result := ucInitialPunctuation
- else if (s = 'PF') then
- Result := ucFinalPunctuation
- else if (s = 'PO') then
- Result := ucOtherPunctuation
- else
- if (s = 'SM') then
- Result := ucMathSymbol
- else if (s = 'SC') then
- Result := ucCurrencySymbol
- else if (s = 'SK') then
- Result := ucModifierSymbol
- else if (s = 'SO') then
- Result := ucOtherSymbol
- else
- if (s = 'ZS') then
- Result := ucSpaceSeparator
- else if (s = 'ZL') then
- Result := ucLineSeparator
- else if (s = 'ZP') then
- Result := ucParagraphSeparator
- else
- if (s = 'CC') then
- Result := ucControl
- else if (s = 'CF') then
- Result := ucFormat
- else if (s = 'CS') then
- Result := ucSurrogate
- else if (s = 'CO') then
- Result := ucPrivateUse
- else
- Result := ucUnassigned;
- end;
- function StringToCodePoint(ACP : string) : TUnicodeCodePoint;
- var
- s : string;
- begin
- s := Trim(ACP);
- Result := 0;
- if (Length(s) > 0) and (s <> '#') then
- Result := StrToInt('$' + s);
- end;
- {function IsWhiteSpace(const ACodePoint : TUnicodeCodePoint) : Boolean;
- begin
- case ACodePoint of
- $0009..$000D : Result := True;// White_Space # Cc [5] <control-0009>..<control-000D>
- $0020 : Result := True;// White_Space # Zs SPACE
- $0085 : Result := True;// White_Space # Cc <control-0085>
- $00A0 : Result := True;// White_Space # Zs NO-BREAK SPACE
- $1680 : Result := True;// White_Space # Zs OGHAM SPACE MARK
- $180E : Result := True;// White_Space # Zs MONGOLIAN VOWEL SEPARATOR
- $2000..$200A : Result := True;// White_Space # Zs [11] EN QUAD..HAIR SPACE
- $2028 : Result := True;// White_Space # Zl LINE SEPARATOR
- $2029 : Result := True;// White_Space # Zp PARAGRAPH SEPARATOR
- $202F : Result := True;// White_Space # Zs NARROW NO-BREAK SPACE
- $205F : Result := True;// White_Space # Zs MEDIUM MATHEMATICAL SPACE
- $3000 : Result := True;// White_Space # Zs IDEOGRAPHIC SPACE
- else
- Result := False;
- end;
- end;}
- function IsWhiteSpace(
- const ACodePoint : TUnicodeCodePoint;
- const AWhiteSpaces : TCodePointRecArray
- ) : Boolean;
- var
- i : Integer;
- p : ^TCodePointRec;
- begin
- p := @AWhiteSpaces[Low(AWhiteSpaces)];
- for i := Low(AWhiteSpaces) to High(AWhiteSpaces) do begin
- if (p^.LineType = 0) then begin
- if (p^.CodePoint = ACodePoint) then
- exit(True);
- end else begin
- if (ACodePoint >= p^.StartCodePoint) and (ACodePoint <= p^.EndCodePoint) then
- exit(True);
- end;
- Inc(p);
- end;
- Result := False;
- end;
- function NormalizeBlockName(const AName : string) : string;
- var
- i, c, k : Integer;
- s : string;
- begin
- c := Length(AName);
- SetLength(Result,c);
- s := LowerCase(AName);
- k := 0;
- for i := 1 to c do begin
- if (s[1] in ['a'..'z','0'..'9','-']) then begin
- k := k + 1;
- Result[k] := s[i];
- end;
- end;
- SetLength(Result,k);
- end;
- procedure ParseBlokcs(
- ADataAStream : TMemoryStream;
- var ABlocks : TBlocks
- );
- const
- LINE_LENGTH = 1024;
- DATA_LENGTH = 25000;
- var
- p : PAnsiChar;
- actualDataLen : Integer;
- bufferLength, bufferPos, lineLength, linePos : Integer;
- line : ansistring;
- function NextLine() : Boolean;
- var
- locOldPos : Integer;
- locOldPointer : PAnsiChar;
- begin
- Result := False;
- locOldPointer := p;
- locOldPos := bufferPos;
- while (bufferPos < bufferLength) and (p^ <> #10) do begin
- Inc(p);
- Inc(bufferPos);
- end;
- if (locOldPos = bufferPos) and (p^ = #10) then begin
- lineLength := 0;
- Inc(p);
- Inc(bufferPos);
- linePos := 1;
- Result := True;
- end else if (locOldPos < bufferPos) then begin
- lineLength := (bufferPos - locOldPos);
- Move(locOldPointer^,line[1],lineLength);
- if (p^ = #10) then begin
- Dec(lineLength);
- Inc(p);
- Inc(bufferPos);
- end;
- linePos := 1;
- Result := True;
- end;
- end;
- function NextToken() : ansistring;
- var
- k : Integer;
- begin
- k := linePos;
- if (linePos < lineLength) and (line[linePos] in [';','#','.']) then begin
- Inc(linePos);
- Result := Copy(line,k,(linePos-k));
- exit;
- end;
- while (linePos < lineLength) and not(line[linePos] in [';','#','.']) do
- Inc(linePos);
- if (linePos > k) then begin
- if (line[linePos] in [';','#','.']) then
- Result := Copy(line,k,(linePos-k))
- else
- Result := Copy(line,k,(linePos-k+1));
- Result := Trim(Result);
- end else begin
- Result := '';
- end;
- end;
- procedure ParseLine();
- var
- locData : TBlockItemRec;
- s : ansistring;
- begin
- s := NextToken();
- if (s = '') or (s[1] = '#') then
- exit;
- locData.RangeStart := StrToInt('$'+s);
- s := NextToken();
- if (s <> '.') then
- raise Exception.CreateFmt('"." expected but "%s" found.',[s]);
- s := NextToken();
- if (s <> '.') then
- raise Exception.CreateFmt('"." expected but "%s" found.',[s]);
- s := NextToken();
- locData.RangeEnd := StrToInt('$'+s);
- s := NextToken();
- if (s <> ';') then
- raise Exception.CreateFmt('";" expected but "%s" found.',[s]);
- locData.Name := Trim(NextToken());
- locData.CanonicalName := NormalizeBlockName(locData.Name);
- if (Length(ABlocks) <= actualDataLen) then
- SetLength(ABlocks,Length(ABlocks)*2);
- ABlocks[actualDataLen] := locData;
- Inc(actualDataLen);
- end;
- procedure Prepare();
- begin
- SetLength(ABlocks,DATA_LENGTH);
- actualDataLen := 0;
- bufferLength := ADataAStream.Size;
- bufferPos := 0;
- p := ADataAStream.Memory;
- lineLength := 0;
- SetLength(line,LINE_LENGTH);
- end;
- begin
- Prepare();
- while NextLine() do
- ParseLine();
- SetLength(ABlocks,actualDataLen);
- end;
- procedure ParseProps(
- ADataAStream : TMemoryStream;
- var APropList : TPropListLineRecArray
- );
- const
- LINE_LENGTH = 1024;
- DATA_LENGTH = 25000;
- var
- p : PAnsiChar;
- actualDataLen : Integer;
- bufferLength, bufferPos, lineLength, linePos : Integer;
- line : ansistring;
- function NextLine() : Boolean;
- var
- locOldPos : Integer;
- locOldPointer : PAnsiChar;
- begin
- Result := False;
- locOldPointer := p;
- locOldPos := bufferPos;
- while (bufferPos < bufferLength) and (p^ <> #10) do begin
- Inc(p);
- Inc(bufferPos);
- end;
- if (locOldPos = bufferPos) and (p^ = #10) then begin
- lineLength := 0;
- Inc(p);
- Inc(bufferPos);
- linePos := 1;
- Result := True;
- end else if (locOldPos < bufferPos) then begin
- lineLength := (bufferPos - locOldPos);
- Move(locOldPointer^,line[1],lineLength);
- if (p^ = #10) then begin
- Dec(lineLength);
- Inc(p);
- Inc(bufferPos);
- end;
- linePos := 1;
- Result := True;
- end;
- end;
- function NextToken() : ansistring;
- var
- k : Integer;
- begin
- k := linePos;
- if (linePos < lineLength) and (line[linePos] in [';','#','.']) then begin
- Inc(linePos);
- Result := Copy(line,k,(linePos-k));
- exit;
- end;
- while (linePos < lineLength) and not(line[linePos] in [';','#','.']) do
- Inc(linePos);
- if (linePos > k) then begin
- if (line[linePos] in [';','#','.']) then
- Result := Copy(line,k,(linePos-k))
- else
- Result := Copy(line,k,(linePos-k+1));
- Result := Trim(Result);
- end else begin
- Result := '';
- end;
- end;
- procedure ParseLine();
- var
- locCP : Cardinal;
- locData : TPropListLineRec;
- s : ansistring;
- begin
- s := NextToken();
- if (s = '') or (s[1] = '#') then
- exit;
- locCP := StrToInt('$'+s);
- s := NextToken();
- if (s = ';') then begin
- locData.CodePoint.LineType := 0;
- locData.CodePoint.CodePoint := locCP;
- end else begin
- if (s = '') or (s <> '.') or (NextToken() <> '.') then
- raise Exception.CreateFmt('Invalid line : "%s".',[Copy(line,1,lineLength)]);
- locData.CodePoint.LineType := 1;
- locData.CodePoint.StartCodePoint := locCP;
- locData.CodePoint.EndCodePoint := StrToInt('$'+NextToken());
- s := NextToken();
- if (s <> ';') then
- raise Exception.CreateFmt('"." expected but "%s" found.',[s]);
- end;
- locData.PropName := Trim(NextToken());
- if (Length(APropList) <= actualDataLen) then
- SetLength(APropList,Length(APropList)*2);
- APropList[actualDataLen] := locData;
- Inc(actualDataLen);
- end;
- procedure Prepare();
- begin
- SetLength(APropList,DATA_LENGTH);
- actualDataLen := 0;
- bufferLength := ADataAStream.Size;
- bufferPos := 0;
- p := ADataAStream.Memory;
- lineLength := 0;
- SetLength(line,LINE_LENGTH);
- end;
- begin
- Prepare();
- while NextLine() do
- ParseLine();
- SetLength(APropList,actualDataLen);
- end;
- function FindCodePointsByProperty(
- const APropName : string;
- const APropList : TPropListLineRecArray
- ) : TCodePointRecArray;
- var
- r : TCodePointRecArray;
- i, k : Integer;
- s : string;
- begin
- k := 0;
- r := nil;
- s := LowerCase(Trim(APropName));
- for i := Low(APropList) to High(APropList) do begin
- if (LowerCase(APropList[i].PropName) = s) then begin
- if (k >= Length(r)) then begin
- if (k = 0) then
- SetLength(r,24)
- else
- SetLength(r,(2*Length(r)));
- end;
- r[k] := APropList[i].CodePoint;
- Inc(k);
- end;
- end;
- SetLength(r,k);
- Result := r;
- end;
- procedure ParseHangulSyllableTypes(
- ADataAStream : TMemoryStream;
- var ACodePointList : TCodePointRecArray
- );
- const
- LINE_LENGTH = 1024;
- DATA_LENGTH = 25000;
- var
- p : PAnsiChar;
- actualDataLen : Integer;
- bufferLength, bufferPos, lineLength, linePos : Integer;
- line : ansistring;
- function NextLine() : Boolean;
- var
- locOldPos : Integer;
- locOldPointer : PAnsiChar;
- begin
- Result := False;
- locOldPointer := p;
- locOldPos := bufferPos;
- while (bufferPos < bufferLength) and (p^ <> #10) do begin
- Inc(p);
- Inc(bufferPos);
- end;
- if (locOldPos = bufferPos) and (p^ = #10) then begin
- lineLength := 0;
- Inc(p);
- Inc(bufferPos);
- linePos := 1;
- Result := True;
- end else if (locOldPos < bufferPos) then begin
- lineLength := (bufferPos - locOldPos);
- Move(locOldPointer^,line[1],lineLength);
- if (p^ = #10) then begin
- Dec(lineLength);
- Inc(p);
- Inc(bufferPos);
- end;
- linePos := 1;
- Result := True;
- end;
- end;
- function NextToken() : ansistring;
- var
- k : Integer;
- begin
- k := linePos;
- if (linePos < lineLength) and (line[linePos] = '.') then begin
- Inc(linePos);
- while (linePos < lineLength) and (line[linePos] = '.') do begin
- Inc(linePos);
- end;
- Result := Copy(line,k,(linePos-k));
- exit;
- end;
- while (linePos < lineLength) and not(line[linePos] in [';','#','.']) do
- Inc(linePos);
- if (linePos > k) then begin
- if (line[linePos] in [';','#','.']) then
- Result := Copy(line,k,(linePos-k))
- else
- Result := Copy(line,k,(linePos-k+1));
- Result := Trim(Result);
- end else begin
- Result := '';
- end;
- //Inc(linePos);
- end;
- procedure ParseLine();
- var
- locData : TCodePointRec;
- s : ansistring;
- begin
- s := NextToken();
- if (s = '') or (s[1] = '#') then
- exit;
- locData.CodePoint := StrToInt('$'+s);
- s := NextToken();
- if (s = '') or (s[1] in [';','#']) then begin
- locData.LineType := 0;
- end else begin
- if (s <> '..') then
- raise Exception.CreateFmt('Unknown line type : "%s"',[Copy(line,1,lineLength)]);
- locData.StartCodePoint := locData.CodePoint;
- locData.EndCodePoint := StrToInt('$'+NextToken());
- locData.LineType := 1;
- end;
- if (Length(ACodePointList) <= actualDataLen) then
- SetLength(ACodePointList,Length(ACodePointList)*2);
- ACodePointList[actualDataLen] := locData;
- Inc(actualDataLen);
- end;
- procedure Prepare();
- begin
- SetLength(ACodePointList,DATA_LENGTH);
- actualDataLen := 0;
- bufferLength := ADataAStream.Size;
- bufferPos := 0;
- p := ADataAStream.Memory;
- lineLength := 0;
- SetLength(line,LINE_LENGTH);
- end;
- begin
- Prepare();
- while NextLine() do
- ParseLine();
- SetLength(ACodePointList,actualDataLen);
- end;
- function IsHangulSyllable(
- const ACodePoint : TUnicodeCodePoint;
- const AHangulList : TCodePointRecArray
- ) : Boolean;
- var
- i : Integer;
- p : ^TCodePointRec;
- begin
- Result := False;
- p := @AHangulList[Low(AHangulList)];
- for i := Low(AHangulList) to High(AHangulList) do begin
- if ( (p^.LineType = 0) and (ACodePoint = p^.CodePoint) ) or
- ( (p^.LineType = 1) and (ACodePoint >= p^.StartCodePoint) and (ACodePoint <= p^.EndCodePoint) )
- then begin
- Result := True;
- Break;
- end;
- Inc(p);
- end;
- end;
- function IndexOf(
- const AProp : TPropRec;
- const APropList : TPropRecArray;
- const AActualLen : Integer
- ) : Integer;overload;
- var
- i : Integer;
- p : PPropRec;
- begin
- Result := -1;
- if (AActualLen > 0) then begin
- p := @APropList[0];
- for i := 0 to AActualLen - 1 do begin
- if (AProp.Category = p^.Category) and
- (AProp.CCC = p^.CCC) and
- (AProp.NumericIndex = p^.NumericIndex) and
- (AProp.SimpleUpperCase = p^.SimpleUpperCase) and
- (AProp.SimpleLowerCase = p^.SimpleLowerCase) and
- (AProp.WhiteSpace = p^.WhiteSpace) and
- //
- (AProp.DecompositionID = p^.DecompositionID) and
- (* ( (AProp.DecompositionID = -1 ) and (p^.DecompositionID = -1) ) or
- ( (AProp.DecompositionID <> -1 ) and (p^.DecompositionID <> -1) )
- *)
- (AProp.HangulSyllable = p^.HangulSyllable)
- then begin
- Result := i;
- Break;
- end;
- Inc(p);
- end;
- end;
- end;
- function IndexOf(
- const AItem : TUnicodeCodePointArray;
- const AList : TDecompositionArray
- ) : Integer;overload;
- var
- p : TUnicodeCodePointArray;
- i : Integer;
- begin
- Result := -1;
- if (Length(AList) = 0) then
- exit;
- for i := Low(AList) to High(AList) do begin
- p := AList[i];
- if (Length(p) = Length(AItem)) then begin
- if CompareMem(@p[0],@AItem[0],Length(AItem)*SizeOf(TUnicodeCodePoint)) then
- exit(i);
- end;
- end;
- Result := -1;
- end;
- function IndexOf(
- const AItem : TNumericValue;
- const AList : TNumericValueArray;
- const AActualLen : Integer
- ) : Integer;overload;
- var
- p : ^TNumericValue;
- i : Integer;
- begin
- Result := -1;
- if (AActualLen = 0) then
- exit;
- p := @AList[Low(AList)];
- for i := Low(AList) to AActualLen - 1 do begin
- if (AItem = p^) then
- exit(i);
- Inc(p);
- end;
- Result := -1;
- end;
- procedure Parse_UnicodeData(
- ADataAStream : TMemoryStream;
- var APropList : TPropRecArray;
- var ANumericTable : TNumericValueArray;
- var ADataLineList : TDataLineRecArray;
- var ADecomposition : TDecompositionArray;
- const AHangulList : TCodePointRecArray;
- const AWhiteSpaces : TCodePointRecArray
- );
- const
- LINE_LENGTH = 1024;
- PROP_LENGTH = 5000;
- DATA_LENGTH = 25000;
- var
- p : PAnsiChar;
- bufferLength, bufferPos : Integer;
- actualPropLen, actualDataLen, actualNumLen : Integer;
- line : ansistring;
- lineLength, linePos : Integer;
- function NextLine() : Boolean;
- var
- locOldPos : Integer;
- locOldPointer : PAnsiChar;
- begin
- Result := False;
- locOldPointer := p;
- locOldPos := bufferPos;
- while (bufferPos < bufferLength) and (p^ <> #10) do begin
- Inc(p);
- Inc(bufferPos);
- end;
- if (locOldPos < bufferPos) then begin
- lineLength := (bufferPos - locOldPos);
- Move(locOldPointer^,line[1],lineLength);
- if (p^ = #10) then begin
- Dec(lineLength);
- Inc(p);
- Inc(bufferPos);
- end;
- if (lineLength > 7) then begin
- linePos := 1;
- Result := True;
- end;
- end;
- end;
- function NextToken() : ansistring;
- var
- k : Integer;
- begin
- k := linePos;
- while (linePos < lineLength) and not(line[linePos] in [';','#']) do
- Inc(linePos);
- if (linePos > k) then begin
- if (line[linePos] in [';','#']) then
- Result := Copy(line,k,(linePos-k))
- else
- Result := Copy(line,k,(linePos-k+1));
- Result := Trim(Result);
- end else begin
- Result := '';
- end;
- Inc(linePos);
- end;
- function ParseCanonicalDecomposition(AStr : ansistring) : TUnicodeCodePointArray;
- var
- locStr, ks : ansistring;
- k0,k : Integer;
- begin
- SetLength(Result,0);
- locStr := UpperCase(Trim(AStr));
- if (locStr = '') or (locStr[1] = '<') then
- exit;
- k0 := 1;
- k := 1;
- while (k <= Length(locStr)) do begin
- while (k <= Length(locStr)) and (locStr[k] in ['0'..'9','A'..'F']) do
- inc(k);
- ks := Trim(Copy(locStr,k0,k-k0));
- SetLength(Result,Length(Result)+1);
- Result[Length(Result)-1] := StringToCodePoint(ks);
- Inc(k);
- k0 := k;
- end;
- end;
- procedure ParseLine();
- var
- locCP : TUnicodeCodePoint;
- locProp : TPropRec;
- locData : TDataLineRec;
- s : ansistring;
- locRangeStart, locRangeEnd : Boolean;
- k : Integer;
- locDecompItem : TUnicodeCodePointArray;
- numVal : TNumericValue;
- begin
- FillChar(locData,SizeOf(locData),#0);
- FillChar(locProp,SizeOf(locProp),#0);
- locCP := StrToInt('$'+NextToken());
- s := NextToken();
- locRangeStart := AnsiEndsText(', First>',s);
- if locRangeStart then
- locRangeEnd := False
- else
- locRangeEnd := AnsiEndsText(', Last>',s);
- if locRangeStart then begin
- locData.LineType := 1;
- locData.StartCodePoint := locCP;
- end else if locRangeEnd then begin
- ADataLineList[actualDataLen - 1].EndCodePoint := locCP;
- exit;
- //locData.EndCodePoint := locCP;
- end else begin
- locData.LineType := 0;
- locData.CodePoint := locCP;
- end;
- locProp.Category := StrToCategory(NextToken());
- locProp.CCC := StrToInt(NextToken());//Canonical_Combining_Class
- NextToken();//Bidi_Class
- s := NextToken();//Decomposition_Type
- locDecompItem := ParseCanonicalDecomposition(s);
- if (Length(locDecompItem) = 0) then
- locProp.DecompositionID := -1
- else begin
- locProp.DecompositionID := IndexOf(locDecompItem,ADecomposition);
- if (locProp.DecompositionID = -1) then begin
- k := Length(ADecomposition);
- locProp.DecompositionID := k;
- SetLength(ADecomposition,k+1);
- ADecomposition[k] := locDecompItem;
- end;
- end;
- numVal := EvaluateFloat(NextToken());
- if (numVal <> Double(0.0)) then begin
- NextToken();
- NextToken();
- end else begin
- s := NextToken();
- if (s <> '') then
- numVal := EvaluateFloat(s);
- s := NextToken();
- if (numVal = Double(0.0)) then
- numVal := EvaluateFloat(s);
- end;
- k := IndexOf(numVal,ANumericTable,actualNumLen);
- if (k = -1) then begin
- if (actualNumLen >= Length(ANumericTable)) then
- SetLength(ANumericTable,(actualNumLen*2));
- ANumericTable[actualNumLen] := numVal;
- k := actualNumLen;
- Inc(actualNumLen);
- end;
- locProp.NumericIndex := k;
- NextToken();//Bidi_Mirroed
- NextToken();//Unicode_l_Name
- NextToken();//ISO_Comment
- locProp.SimpleUpperCase := StringToCodePoint(NextToken());
- locProp.SimpleLowerCase := StringToCodePoint(NextToken());
- NextToken();//Simple_Title_Case_Mapping
- locProp.WhiteSpace := IsWhiteSpace(locCP,AWhiteSpaces);
- locProp.HangulSyllable := IsHangulSyllable(locCP,AHangulList);
- k := IndexOf(locProp,APropList,actualPropLen);
- if (k = -1) then begin
- k := actualPropLen;
- locProp.PropID := k{ + 1};
- APropList[k] := locProp;
- Inc(actualPropLen);
- end;
- locData.PropID := k;
- if (actualDataLen >= Length(ADataLineList)) then
- SetLength(ADataLineList,(2*Length(ADataLineList)));
- ADataLineList[actualDataLen] := locData;
- Inc(actualDataLen);
- end;
- procedure Prepare();
- var
- r : TPropRec;
- begin
- SetLength(APropList,PROP_LENGTH);
- actualPropLen := 0;
- SetLength(ADataLineList,DATA_LENGTH);
- actualDataLen := 0;
- bufferLength := ADataAStream.Size;
- bufferPos := 0;
- p := ADataAStream.Memory;
- lineLength := 0;
- SetLength(line,LINE_LENGTH);
- SetLength(ANumericTable,500);
- actualNumLen := 0;
- FillChar(r,SizeOf(r),#0);
- r.PropID := 0;
- r.Category := ucUnassigned;
- r.DecompositionID := -1;
- r.NumericIndex := 0;
- APropList[0] := r;
- Inc(actualPropLen);
- ANumericTable[0] := 0;
- Inc(actualNumLen);
- end;
- begin
- Prepare();
- while NextLine() do
- ParseLine();
- SetLength(APropList,actualPropLen);
- SetLength(ADataLineList,actualDataLen);
- SetLength(ANumericTable,actualNumLen);
- end;
- function GetPropID(
- ACodePoint : TUnicodeCodePoint;
- const ADataLineList : TDataLineRecArray
- ) : Cardinal;
- var
- i : Integer;
- p : PDataLineRec;
- begin
- Result := 0;
- p := @ADataLineList[Low(ADataLineList)];
- for i := Low(ADataLineList) to High(ADataLineList) do begin
- if (p^.LineType = 0) then begin
- if (p^.CodePoint = ACodePoint) then begin
- Result := p^.PropID;
- Break;
- end;
- end else begin
- if (p^.StartCodePoint <= ACodePoint) and (p^.EndCodePoint >= ACodePoint) then begin
- Result := p^.PropID;
- Break;
- end;
- end;
- Inc(p);
- end;
- end;
- procedure MakeDecomposition(
- const ARawData : TDecompositionArray;
- var ABook : TDecompositionBook
- );
- var
- i, c, locPos : Integer;
- locItem : TUnicodeCodePointArray;
- begin
- c := 0;
- for i := Low(ARawData) to High(ARawData) do
- c := c + Length(ARawData[i]);
- SetLength(ABook.CodePoints,c);
- SetLength(ABook.Index,Length(ARawData));
- locPos := 0;
- for i := Low(ARawData) to High(ARawData) do begin
- locItem := ARawData[i];
- ABook.Index[i].StartPosition := locPos;
- ABook.Index[i].Length := Length(locItem);
- Move(locItem[0],ABook.CodePoints[locPos],(Length(locItem) * SizeOf(TUnicodeCodePoint)));
- locPos := locPos + Length(locItem);
- end;
- end;
- type
- PBmpSecondTableItem = ^TBmpSecondTableItem;
- function IndexOf(
- const AItem : PBmpSecondTableItem;
- const ATable : TBmpSecondTable;
- const ATableActualLength : Integer
- ) : Integer;overload;
- var
- i : Integer;
- p : PBmpSecondTableItem;
- begin
- Result := -1;
- if (ATableActualLength > 0) then begin
- p := @ATable[0];
- for i := 0 to ATableActualLength - 1 do begin
- if CompareMem(p,AItem,SizeOf(TBmpSecondTableItem)) then begin
- Result := i;
- Break;
- end;
- Inc(p);
- end;
- end;
- end;
- procedure MakeBmpTables(
- var AFirstTable : TBmpFirstTable;
- var ASecondTable : TBmpSecondTable;
- const ADataLineList : TDataLineRecArray
- );
- var
- locLowByte, locHighByte : Byte;
- locTableItem : TBmpSecondTableItem;
- locCP : TUnicodeCodePoint;
- i, locSecondActualLen : Integer;
- begin
- SetLength(ASecondTable,120);
- locSecondActualLen := 0;
- for locHighByte := 0 to 255 do begin
- FillChar(locTableItem,SizeOf(locTableItem),#0);
- for locLowByte := 0 to 255 do begin
- locCP := (locHighByte * 256) + locLowByte;
- locTableItem[locLowByte] := GetPropID(locCP,ADataLineList)// - 1;
- end;
- i := IndexOf(@locTableItem,ASecondTable,locSecondActualLen);
- if (i = -1) then begin
- if (locSecondActualLen = Length(ASecondTable)) then
- SetLength(ASecondTable,locSecondActualLen + 50);
- i := locSecondActualLen;
- ASecondTable[i] := locTableItem;
- Inc(locSecondActualLen);
- end;
- AFirstTable[locHighByte] := i;
- end;
- SetLength(ASecondTable,locSecondActualLen);
- end;
- type
- P3lvlBmp3TableItem = ^T3lvlBmp3TableItem;
- function IndexOf(
- const AItem : P3lvlBmp3TableItem;
- const ATable : T3lvlBmp3Table;
- const ATableActualLength : Integer
- ) : Integer;overload;
- var
- i : Integer;
- p : P3lvlBmp3TableItem;
- begin
- Result := -1;
- if (ATableActualLength > 0) then begin
- p := @ATable[0];
- for i := 0 to ATableActualLength - 1 do begin
- if CompareMem(p,AItem,SizeOf(T3lvlBmp3TableItem)) then begin
- Result := i;
- Break;
- end;
- Inc(p);
- end;
- end;
- end;
- type
- P3lvlBmp2TableItem = ^T3lvlBmp2TableItem;
- function IndexOf(
- const AItem : P3lvlBmp2TableItem;
- const ATable : T3lvlBmp2Table
- ) : Integer;overload;
- var
- i : Integer;
- p : P3lvlBmp2TableItem;
- begin
- Result := -1;
- if (Length(ATable) > 0) then begin
- p := @ATable[0];
- for i := 0 to Length(ATable) - 1 do begin
- if CompareMem(p,AItem,SizeOf(T3lvlBmp2TableItem)) then begin
- Result := i;
- Break;
- end;
- Inc(p);
- end;
- end;
- end;
- procedure MakeBmpTables3Levels(
- var AFirstTable : T3lvlBmp1Table;
- var ASecondTable : T3lvlBmp2Table;
- var AThirdTable : T3lvlBmp3Table;
- const ADataLineList : TDataLineRecArray
- );
- var
- locLowByte0, locLowByte1, locHighByte : Byte;
- locTableItem2 : T3lvlBmp2TableItem;
- locTableItem3 : T3lvlBmp3TableItem;
- locCP : TUnicodeCodePoint;
- i, locThirdActualLen : Integer;
- begin
- SetLength(AThirdTable,120);
- locThirdActualLen := 0;
- for locHighByte := 0 to 255 do begin
- FillChar(locTableItem2,SizeOf(locTableItem2),#0);
- for locLowByte0 := 0 to 15 do begin
- FillChar(locTableItem3,SizeOf(locTableItem3),#0);
- for locLowByte1 := 0 to 15 do begin
- locCP := (locHighByte * 256) + (locLowByte0*16) + locLowByte1;
- locTableItem3[locLowByte1] := GetPropID(locCP,ADataLineList);
- end;
- i := IndexOf(@locTableItem3,AThirdTable,locThirdActualLen);
- if (i = -1) then begin
- if (locThirdActualLen = Length(AThirdTable)) then
- SetLength(AThirdTable,locThirdActualLen + 50);
- i := locThirdActualLen;
- AThirdTable[i] := locTableItem3;
- Inc(locThirdActualLen);
- end;
- locTableItem2[locLowByte0] := i;
- end;
- i := IndexOf(@locTableItem2,ASecondTable);
- if (i = -1) then begin
- i := Length(ASecondTable);
- SetLength(ASecondTable,(i + 1));
- ASecondTable[i] := locTableItem2;
- end;
- AFirstTable[locHighByte] := i;
- end;
- SetLength(AThirdTable,locThirdActualLen);
- end;
- procedure GenerateLicenceText(ADest : TStream);
- var
- s : ansistring;
- begin
- s := SLicenseText + sLineBreak + sLineBreak;
- ADest.Write(s[1],Length(s));
- end;
- procedure GenerateBmpTables(
- ADest : TStream;
- var AFirstTable : TBmpFirstTable;
- var ASecondTable : TBmpSecondTable
- );
- procedure AddLine(const ALine : ansistring);
- var
- buffer : ansistring;
- begin
- buffer := ALine + sLineBreak;
- ADest.Write(buffer[1],Length(buffer));
- end;
- var
- i, j, c : Integer;
- locLine : string;
- begin
- AddLine('const');
- AddLine(' UC_TABLE_1 : array[0..255] of Byte = (');
- locLine := '';
- for i := Low(AFirstTable) to High(AFirstTable) - 1 do begin
- locLine := locLine + IntToStr(AFirstTable[i]) + ',';
- if (((i+1) mod 16) = 0) then begin
- locLine := ' ' + locLine;
- AddLine(locLine);
- locLine := '';
- end;
- end;
- locLine := locLine + IntToStr(AFirstTable[High(AFirstTable)]);
- locLine := ' ' + locLine;
- AddLine(locLine);
- AddLine(' );' + sLineBreak);
- AddLine(' UC_TABLE_2 : array[0..(256*' + IntToStr(Length(ASecondTable)) +'-1)] of Word =(');
- c := High(ASecondTable);
- for i := Low(ASecondTable) to c do begin
- locLine := '';
- for j := Low(TBmpSecondTableItem) to High(TBmpSecondTableItem) do begin
- locLine := locLine + IntToStr(ASecondTable[i][j]) + ',';
- if (((j+1) mod 16) = 0) then begin
- if (i = c) and (j = 255) then
- Delete(locLine,Length(locLine),1);
- locLine := ' ' + locLine;
- AddLine(locLine);
- locLine := '';
- end;
- end;
- end;
- AddLine(' );' + sLineBreak);
- end;
- //----------------------------------
- procedure Generate3lvlBmpTables(
- ADest : TStream;
- var AFirstTable : T3lvlBmp1Table;
- var ASecondTable : T3lvlBmp2Table;
- var AThirdTable : T3lvlBmp3Table
- );
- procedure AddLine(const ALine : ansistring);
- var
- buffer : ansistring;
- begin
- buffer := ALine + sLineBreak;
- ADest.Write(buffer[1],Length(buffer));
- end;
- var
- i, j, c : Integer;
- locLine : string;
- begin
- AddLine('const');
- AddLine(' UC_TABLE_1 : array[0..255] of Byte = (');
- locLine := '';
- for i := Low(AFirstTable) to High(AFirstTable) - 1 do begin
- locLine := locLine + IntToStr(AFirstTable[i]) + ',';
- if (((i+1) mod 16) = 0) then begin
- locLine := ' ' + locLine;
- AddLine(locLine);
- locLine := '';
- end;
- end;
- locLine := locLine + IntToStr(AFirstTable[High(AFirstTable)]);
- locLine := ' ' + locLine;
- AddLine(locLine);
- AddLine(' );' + sLineBreak);
- AddLine(' UC_TABLE_2 : array[0..' + IntToStr(Length(ASecondTable)-1) +'] of array[0..15] of Word = (');
- c := High(ASecondTable);
- for i := Low(ASecondTable) to c do begin
- locLine := '(';
- for j := Low(T3lvlBmp2TableItem) to High(T3lvlBmp2TableItem) do
- locLine := locLine + IntToStr(ASecondTable[i][j]) + ',';
- Delete(locLine,Length(locLine),1);
- locLine := ' ' + locLine + ')';
- if (i < c) then
- locLine := locLine + ',';
- AddLine(locLine);
- end;
- AddLine(' );' + sLineBreak);
- AddLine(' UC_TABLE_3 : array[0..' + IntToStr(Length(AThirdTable)-1) +'] of array[0..15] of Word = (');
- c := High(AThirdTable);
- for i := Low(AThirdTable) to c do begin
- locLine := '(';
- for j := Low(T3lvlBmp3TableItem) to High(T3lvlBmp3TableItem) do
- locLine := locLine + IntToStr(AThirdTable[i][j]) + ',';
- Delete(locLine,Length(locLine),1);
- locLine := ' ' + locLine + ')';
- if (i < c) then
- locLine := locLine + ',';
- AddLine(locLine);
- end;
- AddLine(' );' + sLineBreak);
- end;
- function UInt24ToStr(const AValue : UInt24; const AEndian : TEndianKind): string;inline;
- begin
- if (AEndian = ekBig) then
- Result := Format(
- '(byte2 : $%s; byte1 : $%s; byte0 : $%s;)',
- [ IntToHex(AValue.byte2,2), IntToHex(AValue.byte1,2),
- IntToHex(AValue.byte0,2)
- ]
- )
- else
- Result := Format(
- '(byte0 : $%s; byte1 : $%s; byte2 : $%s;)',
- [ IntToHex(AValue.byte0,2), IntToHex(AValue.byte1,2),
- IntToHex(AValue.byte2,2)
- ]
- );
- end;
- procedure GeneratePropTable(
- ADest : TStream;
- const APropList : TPropRecArray;
- const AEndian : TEndianKind
- );
- procedure AddLine(const ALine : ansistring);
- var
- buffer : ansistring;
- begin
- buffer := ALine + sLineBreak;
- ADest.Write(buffer[1],Length(buffer));
- end;
- var
- i : Integer;
- locLine : string;
- p : PPropRec;
- begin
- AddLine('');
- AddLine('const');
- AddLine(' UC_PROP_REC_COUNT = ' + IntToStr(Length(APropList)) + ';');
- AddLine(' UC_PROP_ARRAY : array[0..(UC_PROP_REC_COUNT-1)] of TUC_Prop = (');
- p := @APropList[0];
- for i := Low(APropList) to High(APropList) - 1 do begin
- locLine := ' (CategoryData : ' + IntToStr(p^.CategoryData) + ';' +
- ' CCC : ' + IntToStr(p^.CCC) + ';' +
- ' NumericIndex : ' + IntToStr(p^.NumericIndex) + ';' +
- ' SimpleUpperCase : ' + UInt24ToStr(p^.SimpleUpperCase,AEndian) + ';' +
- ' SimpleLowerCase : ' + UInt24ToStr(p^.SimpleLowerCase,AEndian) + ';' +
- ' DecompositionID : ' + IntToStr(p^.DecompositionID) + '),';
- AddLine(locLine);
- Inc(p);
- end;
- locLine := //' (Category : TUnicodeCategory.' + GetEnumName(pti,Ord(p^.Category)) + ';' +
- ' (CategoryData : ' + IntToStr(p^.CategoryData) + ';' +
- ' CCC : ' + IntToStr(p^.CCC) + ';' +
- ' NumericIndex : ' + IntToStr(p^.NumericIndex) + ';' +
- ' SimpleUpperCase : ' + UInt24ToStr(p^.SimpleUpperCase,AEndian) + ';' +
- ' SimpleLowerCase : ' + UInt24ToStr(p^.SimpleLowerCase,AEndian) + ';' +
- ' DecompositionID : ' + IntToStr(p^.DecompositionID) + ')';
- AddLine(locLine);
- AddLine(' );' + sLineBreak);
- end;
- procedure GenerateNumericTable(
- ADest : TStream;
- const ANumList : TNumericValueArray;
- const ACompleteUnit : Boolean
- );
- procedure AddLine(const ALine : ansistring);
- var
- buffer : ansistring;
- begin
- buffer := ALine + sLineBreak;
- ADest.Write(buffer[1],Length(buffer));
- end;
- var
- i : Integer;
- locLine : string;
- p : ^TNumericValue;
- begin
- if ACompleteUnit then begin
- GenerateLicenceText(ADest);
- AddLine('unit unicodenumtable;');
- AddLine('interface');
- AddLine('');
- end;
- AddLine('');
- AddLine('const');
- AddLine(' UC_NUMERIC_COUNT = ' + IntToStr(Length(ANumList)) + ';');
- AddLine(' UC_NUMERIC_ARRAY : array[0..(UC_NUMERIC_COUNT-1)] of Double = (');
- locLine := '';
- p := @ANumList[0];
- for i := Low(ANumList) to High(ANumList) - 1 do begin
- locLine := locLine + FloatToStr(p^,FS) + ' ,';
- if (i > 0) and ((i mod 8) = 0) then begin
- AddLine(' ' + locLine);
- locLine := '';
- end;
- Inc(p);
- end;
- locLine := locLine + FloatToStr(p^,FS);
- AddLine(' ' + locLine);
- AddLine(' );' + sLineBreak);
- if ACompleteUnit then begin
- AddLine('');
- AddLine('implementation');
- AddLine('');
- AddLine('end.');
- end;
- end;
- procedure GenerateDecompositionBookTable(
- ADest : TStream;
- const ABook : TDecompositionBook;
- const AEndian : TEndianKind
- );
- procedure AddLine(const ALine : ansistring);
- var
- buffer : ansistring;
- begin
- buffer := ALine + sLineBreak;
- ADest.Write(buffer[1],Length(buffer));
- end;
- var
- i, k : Integer;
- p : ^TDecompositionIndexRec;
- cp : ^TUnicodeCodePoint;
- cp24 : UInt24;
- locLine : string;
- begin
- AddLine('const');
- AddLine(' UC_DEC_BOOK_INDEX_LENGTH = ' + IntToStr(Length(ABook.Index)) + ';');
- AddLine(' UC_DEC_BOOK_DATA_LENGTH = ' + IntToStr(Length(ABook.CodePoints)) + ';');
- AddLine('type');
- AddLine(' TDecompositionIndexRec = packed record');
- AddLine(' StartPosition : Word;');
- AddLine(' Length : Byte;');
- AddLine(' end;');
- AddLine(' TDecompositionBookRec = packed record');
- AddLine(' Index : array[0..(UC_DEC_BOOK_INDEX_LENGTH-1)] of TDecompositionIndexRec;');
- AddLine(' CodePoints : array[0..(UC_DEC_BOOK_DATA_LENGTH-1)] of UInt24;');
- AddLine(' end;');
- AddLine('const');
- AddLine(' UC_DEC_BOOK_DATA : TDecompositionBookRec = (');
- p := @ABook.Index[0];
- AddLine(' Index : (// Index BEGIN');
- k := 0;
- locLine := ' ';
- for i := Low(ABook.Index) to High(ABook.Index) - 1 do begin
- locLine := locLine + '(StartPosition : ' + IntToStr(p^.StartPosition) + ';' +
- ' Length : ' + IntToStr(p^.Length) + '), ';
- k := k + 1;
- if (k >= 2) then begin
- AddLine(locLine);
- locLine := ' ';
- k := 0;
- end;
- Inc(p);
- end;
- locLine := locLine + '(StartPosition : ' + IntToStr(p^.StartPosition) + ';' +
- ' Length : ' + IntToStr(p^.Length) + ')';
- AddLine(locLine);
- AddLine(' ); // Index END');
- cp := @ABook.CodePoints[0];
- AddLine(' CodePoints : (// CodePoints BEGIN');
- k := 0;
- locLine := ' ';
- for i := Low(ABook.CodePoints) to High(ABook.CodePoints) - 1 do begin
- cp24 := cp^;
- locLine := locLine + Format('%s,',[UInt24ToStr(cp24,AEndian)]);
- Inc(k);
- if (k >= 16) then begin
- AddLine(locLine);
- k := 0;
- locLine := ' ';
- end;
- Inc(cp);
- end;
- cp24 := cp^;
- locLine := locLine + Format('%s',[UInt24ToStr(cp24,AEndian)]);
- AddLine(locLine);
- AddLine(' ); // CodePoints END');
- AddLine(' );' + sLineBreak);
- end;
- procedure GenerateOutBmpTable(
- ADest : TStream;
- const AList : TDataLineRecArray
- );
- procedure AddLine(const ALine : ansistring);
- var
- buffer : ansistring;
- begin
- buffer := ALine + sLineBreak;
- ADest.Write(buffer[1],Length(buffer));
- end;
- var
- i, j : Integer;
- locLine : string;
- p : PDataLineRec;
- begin
- AddLine('');
- //AddLine(' UC_PROP_REC_COUNT = ' + IntToStr(Length(APropList)) + ';');
- //AddLine(' UC_PROP_ARRAY : array[0..(UC_PROP_REC_COUNT-1)] of TUC_Prop = (');
- j := -1;
- p := @AList[0];
- for i := 0 to Length(AList) - 1 do begin
- if ((p^.LineType = 0) and (p^.CodePoint >$FFFF)) or
- (p^.StartCodePoint > $FFFF)
- then begin
- j := i;
- Break;
- end;
- Inc(p);
- end;
- if (j < 0) then
- exit;
- for i := j to Length(AList) - 2 do begin
- locLine := ' (PropID : ' + IntToStr(p^.PropID) + ';' +
- ' CodePoint : ' + IntToStr(p^.CodePoint) + ';' +
- ' RangeEnd : ' + IntToStr(p^.EndCodePoint) + '),' ;
- AddLine(locLine);
- Inc(p);
- end;
- locLine := ' (PropID : ' + IntToStr(p^.PropID) + ';' +
- ' CodePoint : ' + IntToStr(p^.CodePoint) + ';' +
- ' RangeEnd : ' + IntToStr(p^.EndCodePoint) + ')' ;
- AddLine(locLine);
- AddLine(' );' + sLineBreak);
- end;
- function Compress(const AData : TDataLineRecArray) : TDataLineRecArray;
- var
- k, i, locResLen : Integer;
- q, p, pr : PDataLineRec;
- k_end : TUnicodeCodePoint;
- begin
- locResLen := 1;
- SetLength(Result,Length(AData));
- FillChar(Result[0],Length(Result),#0);
- Result[0] := AData[0];
- q := @AData[0];
- k := 0;
- while (k < Length(AData)) do begin
- if (q^.LineType = 0) then
- k_end := q^.CodePoint
- else
- k_end := q^.EndCodePoint;
- if ((k+1) = Length(AData)) then begin
- i := k;
- end else begin
- p := @AData[k+1];
- i := k +1;
- while (i < (Length(AData) {- 1})) do begin
- if (p^.PropID <> q^.PropID) then begin
- i := i - 1;
- Break;
- end;
- if (p^.LineType = 0) then begin
- if (p^.CodePoint <> (k_end + 1)) then begin
- i := i - 1;
- Break;
- end;
- Inc(k_end);
- end else begin
- if (p^.StartCodePoint <> (k_end + 1)) then begin
- i := i - 1;
- Break;
- end;
- k_end := p^.EndCodePoint;
- end;
- Inc(i);
- Inc(p);
- end;
- end;
- {if (i = k) then begin
- Result[locResLen] := q^;
- Inc(locResLen);
- end else begin }
- p := @AData[i];
- pr := @Result[locResLen];
- pr^.PropID := q^.PropID;
- if (q^.LineType = 0) then
- pr^.StartCodePoint := q^.CodePoint
- else
- pr^.StartCodePoint := q^.StartCodePoint;
- pr^.LineType := 1;
- if (p^.LineType = 0) then
- pr^.EndCodePoint := p^.CodePoint
- else
- pr^.EndCodePoint := p^.EndCodePoint;
- Inc(locResLen);
- //end;
- k := i + 1;
- if (k = Length(AData)) then
- Break;
- q := @AData[k];
- end;
- SetLength(Result,locResLen);
- end;
- procedure ParseUCAFile(
- ADataAStream : TMemoryStream;
- var ABook : TUCA_DataBook
- );
- const
- LINE_LENGTH = 1024;
- DATA_LENGTH = 25000;
- var
- p : PAnsiChar;
- actualDataLen : Integer;
- bufferLength, bufferPos, lineLength, linePos : Integer;
- line : ansistring;
- function NextLine() : Boolean;
- var
- locOldPos : Integer;
- locOldPointer : PAnsiChar;
- begin
- Result := False;
- locOldPointer := p;
- locOldPos := bufferPos;
- while (bufferPos < bufferLength) and (p^ <> #10) do begin
- Inc(p);
- Inc(bufferPos);
- end;
- if (locOldPos = bufferPos) and (p^ = #10) then begin
- lineLength := 0;
- Inc(p);
- Inc(bufferPos);
- linePos := 1;
- Result := True;
- end else if (locOldPos < bufferPos) then begin
- lineLength := (bufferPos - locOldPos) + 1;
- Move(locOldPointer^,line[1],lineLength);
- if (p^ = #10) then begin
- Dec(lineLength);
- Inc(p);
- Inc(bufferPos);
- end;
- linePos := 1;
- Result := True;
- end;
- end;
- procedure SkipSpace();
- begin
- while (linePos < lineLength) and (line[linePos] in [' ',#9]) do
- Inc(linePos);
- end;
- function NextToken() : ansistring;
- const C_SEPARATORS = [';','#','.','[',']','*','@'];
- var
- k : Integer;
- begin
- SkipSpace();
- k := linePos;
- if (linePos <= lineLength) and (line[linePos] in C_SEPARATORS) then begin
- Result := line[linePos];
- Inc(linePos);
- exit;
- end;
- while (linePos <= lineLength) and not(line[linePos] in (C_SEPARATORS+[' '])) do
- Inc(linePos);
- if (linePos > k) then begin
- if (line[Min(linePos,lineLength)] in C_SEPARATORS) then
- Result := Copy(line,k,(linePos-k))
- else
- Result := Copy(line,k,(linePos-k+1));
- Result := Trim(Result);
- end else begin
- Result := '';
- end;
- end;
- procedure CheckToken(const AToken : string);
- var
- a, b : string;
- begin
- a := LowerCase(Trim(AToken));
- b := LowerCase(Trim(NextToken()));
- if (a <> b) then
- raise Exception.CreateFmt('Expected token "%s" but found "%s", Line = "%s".',[a,b,line]);
- end;
- function ReadWeightBlock(var ADest : TUCA_WeightRec) : Boolean;
- var
- s :AnsiString;
- k : Integer;
- begin
- Result := False;
- s := NextToken();
- if (s <> '[') then
- exit;
- s := NextToken();
- if (s = '.') then
- ADest.Variable := False
- else begin
- if (s <> '*') then
- raise Exception.CreateFmt('Expected "%s" but found "%s".',['*',s]);
- ADest.Variable := True;
- end;
- ADest.Weights[0] := StrToInt('$'+NextToken());
- for k := 1 to WEIGHT_LEVEL_COUNT-1 do begin
- CheckToken('.');
- ADest.Weights[k] := StrToInt('$'+NextToken());
- end;
- CheckToken(']');
- Result := True;
- end;
- procedure ParseHeaderVar();
- var
- s,ss : string;
- k : Integer;
- begin
- s := NextToken();
- if (s = 'version') then begin
- ss := '';
- while True do begin
- s := NextToken();
- if (s = '') then
- Break;
- ss := ss + s;
- end;
- ABook.Version := ss;
- end else if (s = 'variable') then begin
- if (s = 'blanked') then
- ABook.VariableWeight := ucaBlanked
- else if (s = 'non-ignorable') then
- ABook.VariableWeight := ucaNonIgnorable
- else if (s = 'shifted') then
- ABook.VariableWeight := ucaShifted
- else if (s = 'shift-trimmed') then
- ABook.VariableWeight := ucaShiftedTrimmed
- else if (s = 'ignoresp') then
- ABook.VariableWeight := ucaIgnoreSP
- else
- raise Exception.CreateFmt('Unknown "@variable" type : "%s".',[s]);
- end else if (s = 'backwards') or (s = 'forwards') then begin
- ss := s;
- s := NextToken();
- k := StrToInt(s);
- if (k < 1) or (k > 4) then
- raise Exception.CreateFmt('Invalid "%s" position : %d.',[ss,s]);
- ABook.Backwards[k] := (s = 'backwards');
- end;
- end;
- procedure ParseLine();
- var
- locData : ^TUCA_LineRec;
- s : ansistring;
- kc : Integer;
- begin
- if (Length(ABook.Lines) <= actualDataLen) then
- SetLength(ABook.Lines,Length(ABook.Lines)*2);
- locData := @ABook.Lines[actualDataLen];
- s := NextToken();
- if (s = '') or (s[1] = '#') then
- exit;
- if (s[1] = '@') then begin
- ParseHeaderVar();
- exit;
- end;
- SetLength(locData^.CodePoints,10);
- locData^.CodePoints[0] := StrToInt('$'+s);
- kc := 1;
- while True do begin
- s := Trim(NextToken());
- if (s = '') then
- exit;
- if (s = ';') then
- Break;
- locData^.CodePoints[kc] := StrToInt('$'+s);
- Inc(kc);
- end;
- if (kc = 0) then
- exit;
- SetLength(locData^.CodePoints,kc);
- SetLength(locData^.Weights,24);
- kc := 0;
- while ReadWeightBlock(locData^.Weights[kc]) do begin
- Inc(kc);
- end;
- SetLength(locData^.Weights,kc);
- Inc(actualDataLen);
- end;
- procedure Prepare();
- var
- k : Integer;
- begin
- ABook.VariableWeight := ucaShifted;
- for k := Low(ABook.Backwards) to High(ABook.Backwards) do
- ABook.Backwards[k] := False;
- SetLength(ABook.Lines,DATA_LENGTH);
- actualDataLen := 0;
- bufferLength := ADataAStream.Size;
- bufferPos := 0;
- p := ADataAStream.Memory;
- lineLength := 0;
- SetLength(line,LINE_LENGTH);
- end;
- begin
- Prepare();
- while NextLine() do
- ParseLine();
- SetLength(ABook.Lines,actualDataLen);
- end;
- procedure Dump(X : array of TUnicodeCodePoint; const ATitle : string = '');
- var
- i : Integer;
- begin
- Write(ATitle, ' ');
- for i := 0 to Length(X) - 1 do
- Write(X[i],' ');
- WriteLn();
- end;
- function IsGreaterThan(A, B : PUCA_LineRec) : Integer;
- var
- i, hb : Integer;
- begin
- if (A=B) then
- exit(0);
- Result := 1;
- hb := Length(B^.CodePoints) - 1;
- for i := 0 to Length(A^.CodePoints) - 1 do begin
- if (i > hb) then
- exit;
- if (A^.CodePoints[i] < B^.CodePoints[i]) then
- exit(-1);
- if (A^.CodePoints[i] > B^.CodePoints[i]) then
- exit(1);
- end;
- if (Length(A^.CodePoints) = Length(B^.CodePoints)) then
- exit(0);
- exit(-1);
- end;
- procedure QuickSort(
- var AList : TUCA_DataBookIndex;
- L, R : Longint;
- ABook : PUCA_DataBook
- );overload;
- var
- I, J : Longint;
- P, Q : Integer;
- begin
- repeat
- I := L;
- J := R;
- P := AList[ (L + R) div 2 ];
- repeat
- while IsGreaterThan(@ABook^.Lines[P], @ABook^.Lines[AList[i]]) > 0 do
- I := I + 1;
- while IsGreaterThan(@ABook^.Lines[P], @ABook^.Lines[AList[J]]) < 0 do
- J := J - 1;
- If I <= J then
- begin
- Q := AList[I];
- AList[I] := AList[J];
- AList[J] := Q;
- I := I + 1;
- J := J - 1;
- end;
- until I > J;
- // sort the smaller range recursively
- // sort the bigger range via the loop
- // Reasons: memory usage is O(log(n)) instead of O(n) and loop is faster than recursion
- if J - L < R - I then
- begin
- if L < J then
- QuickSort(AList, L, J, ABook);
- L := I;
- end
- else
- begin
- if I < R then
- QuickSort(AList, I, R, ABook);
- R := J;
- end;
- until L >= R;
- end;
- function CreateIndex(ABook : PUCA_DataBook) : TUCA_DataBookIndex;
- var
- r : TUCA_DataBookIndex;
- i, c : Integer;
- begin
- c := Length(ABook^.Lines);
- SetLength(r,c);
- for i := 0 to c - 1 do
- r[i] := i;
- QuickSort(r,0,c-1,ABook);
- Result := r;
- end;
- function ConstructContextTree(
- const AContext : PUCA_LineContextRec;
- var ADestBuffer;
- const ADestBufferLength : Cardinal
- ) : PUCA_PropItemContextTreeRec;forward;
- function ConstructItem(
- AItem : PUCA_PropItemRec;
- ACodePoint : Cardinal;
- AValid : Byte;
- AChildCount : Byte;
- const AWeights : array of TUCA_WeightRec;
- const AStoreCP : Boolean;
- const AContext : PUCA_LineContextRec;
- const ADeleted : Boolean
- ) : Cardinal;
- var
- i : Integer;
- p : PUCA_PropItemRec;
- pw : PUCA_PropWeights;
- pb : PByte;
- hasContext : Boolean;
- contextTree : PUCA_PropItemContextTreeRec;
- wl : Integer;
- begin
- p := AItem;
- p^.Size := 0;
- p^.Flags := 0;
- p^.WeightLength := 0;
- SetBit(p^.Flags,AItem^.FLAG_VALID,(AValid <> 0));
- p^.ChildCount := AChildCount;
- hasContext := (AContext <> nil) and (Length(AContext^.Data) > 0);
- if hasContext then
- wl := 0
- else
- wl := Length(AWeights);
- p^.WeightLength := wl;
- if (wl = 0) then begin
- Result := SizeOf(TUCA_PropItemRec);
- if ADeleted then
- SetBit(AItem^.Flags,AItem^.FLAG_DELETION,True);
- end else begin
- Result := SizeOf(TUCA_PropItemRec) + (wl*SizeOf(TUCA_PropWeights));
- pb := PByte(PtrUInt(p) + SizeOf(TUCA_PropItemRec));
- Unaligned(PWord(pb)^) := AWeights[0].Weights[0];
- pb := pb + 2;
- if (AWeights[0].Weights[1] > High(Byte)) then begin
- Unaligned(PWord(pb)^) := AWeights[0].Weights[1];
- pb := pb + 2;
- end else begin
- SetBit(p^.Flags,p^.FLAG_COMPRESS_WEIGHT_1,True);
- pb^ := AWeights[0].Weights[1];
- pb := pb + 1;
- Result := Result - 1;
- end;
- if (AWeights[0].Weights[2] > High(Byte)) then begin
- Unaligned(PWord(pb)^) := AWeights[0].Weights[2];
- pb := pb + 2;
- end else begin
- SetBit(p^.Flags,p^.FLAG_COMPRESS_WEIGHT_2,True);
- pb^ := AWeights[0].Weights[2];
- pb := pb + 1;
- Result := Result - 1;
- end;
- pw := PUCA_PropWeights(pb);
- for i := 1 to wl - 1 do begin
- pw^.Weights[0] := AWeights[i].Weights[0];
- pw^.Weights[1] := AWeights[i].Weights[1];
- pw^.Weights[2] := AWeights[i].Weights[2];
- //pw^.Variable := BoolToByte(AWeights[i].Variable);
- Inc(pw);
- end;
- end;
- hasContext := (AContext <> nil) and (Length(AContext^.Data) > 0);
- if AStoreCP or hasContext then begin
- Unaligned(PUInt24(PtrUInt(AItem)+Result)^) := ACodePoint;
- Result := Result + SizeOf(UInt24);
- SetBit(AItem^.Flags,AItem^.FLAG_CODEPOINT,True);
- end;
- if hasContext then begin
- contextTree := ConstructContextTree(AContext,Unaligned(Pointer(PtrUInt(AItem)+Result)^),MaxInt);
- Result := Result + Cardinal(contextTree^.Size);
- SetBit(AItem^.Flags,AItem^.FLAG_CONTEXTUAL,True);
- end;
- p^.Size := Result;
- end;
- function CalcCharChildCount(
- const ASearchStartPos : Integer;
- const ALinePos : Integer;
- const ABookLines : PUCA_LineRec;
- const AMaxLength : Integer;
- const ABookIndex : TUCA_DataBookIndex;
- out ALineCount : Word
- ) : Byte;
- var
- locLinePos : Integer;
- p : PUCA_LineRec;
- procedure IncP();
- begin
- Inc(locLinePos);
- p := @ABookLines[ABookIndex[locLinePos]];
- end;
- var
- i, locTargetLen, locTargetBufferSize, r : Integer;
- locTarget : array[0..127] of Cardinal;
- locLastChar : Cardinal;
- begin
- locLinePos := ALinePos;
- p := @ABookLines[ABookIndex[locLinePos]];
- locTargetLen := ASearchStartPos;
- locTargetBufferSize := (locTargetLen*SizeOf(Cardinal));
- Move(p^.CodePoints[0],locTarget[0],locTargetBufferSize);
- if (Length(p^.CodePoints) = ASearchStartPos) then begin
- r := 0;
- locLastChar := High(Cardinal);
- end else begin
- r := 1;
- locLastChar := p^.CodePoints[ASearchStartPos];
- end;
- i := 1;
- while (i < AMaxLength) do begin
- IncP();
- if (Length(p^.CodePoints) < locTargetLen) then
- Break;
- if not CompareMem(@locTarget[0],@p^.CodePoints[0],locTargetBufferSize) then
- Break;
- if (p^.CodePoints[ASearchStartPos] <> locLastChar) then begin
- Inc(r);
- locLastChar := p^.CodePoints[ASearchStartPos];
- end;
- Inc(i);
- end;
- ALineCount := i;
- Result := r;
- end;
- function BuildTrie(
- const ALinePos : Integer;
- const ABookLines : PUCA_LineRec;
- const AMaxLength : Integer;
- const ABookIndex : TUCA_DataBookIndex
- ) : PTrieNode;
- var
- p : PUCA_LineRec;
- root : PTrieNode;
- ki, k, i : Integer;
- key : array of TKeyType;
- begin
- k := ABookIndex[ALinePos];
- p := @ABookLines[k];
- if (Length(p^.CodePoints) = 1) then
- root := CreateNode(p^.CodePoints[0],k)
- else
- root := CreateNode(p^.CodePoints[0]);
- for i := ALinePos to ALinePos + AMaxLength - 1 do begin
- k := ABookIndex[i];
- p := @ABookLines[k];
- if (Length(p^.CodePoints) = 1) then begin
- InsertWord(root,p^.CodePoints[0],k);
- end else begin
- SetLength(key,Length(p^.CodePoints));
- for ki := 0 to Length(p^.CodePoints) - 1 do
- key[ki] := p^.CodePoints[ki];
- InsertWord(root,key,k);
- end;
- end;
- Result := root;
- end;
- function BoolToByte(AValue : Boolean): Byte;inline;
- begin
- if AValue then
- Result := 1
- else
- Result := 0;
- end;
- function InternalConstructFromTrie(
- const ATrie : PTrieNode;
- const AItem : PUCA_PropItemRec;
- const ALines : PUCA_LineRec;
- const AStoreCp : Boolean
- ) : Cardinal;
- var
- i : Integer;
- size : Cardinal;
- p : PUCA_PropItemRec;
- n : PTrieNode;
- begin
- if (ATrie = nil) then
- exit(0);
- p := AItem;
- n := ATrie;
- if n^.DataNode then
- size := ConstructItem(p,n^.Key,1,n^.ChildCount,ALines[n^.Data].Weights,AStoreCp,@(ALines[n^.Data].Context),ALines[n^.Data].Deleted)
- else
- size := ConstructItem(p,n^.Key,0,n^.ChildCount,[],AStoreCp,nil,False);
- Result := size;
- if (n^.ChildCount > 0) then begin
- for i := 0 to n^.ChildCount - 1 do begin
- p := PUCA_PropItemRec(PtrUInt(p) + size);
- size := InternalConstructFromTrie(n^.Children[i],p,ALines,True);
- Result := Result + size;
- end;
- end;
- AItem^.Size := Result;
- end;
- function ConstructFromTrie(
- const ATrie : PTrieNode;
- const AItem : PUCA_PropItemRec;
- const ALines : PUCA_LineRec
- ) : Integer;
- begin
- Result := InternalConstructFromTrie(ATrie,AItem,ALines,False);
- end;
- procedure MakeUCA_Props(
- ABook : PUCA_DataBook;
- out AProps : PUCA_PropBook
- );
- var
- propIndexCount : Integer;
- procedure CapturePropIndex(AItem : PUCA_PropItemRec; ACodePoint : Cardinal);
- begin
- AProps^.Index[propIndexCount].CodePoint := ACodePoint;
- AProps^.Index[propIndexCount].Position := PtrUInt(AItem) - PtrUInt(AProps^.Items);
- propIndexCount := propIndexCount + 1;
- end;
- var
- locIndex : TUCA_DataBookIndex;
- i, c, k, kc : Integer;
- p, p1, p2 : PUCA_PropItemRec;
- lines, pl1, pl2 : PUCA_LineRec;
- childCount, lineCount : Word;
- size : Cardinal;
- trieRoot : PTrieNode;
- MaxChildCount, MaxSize : Cardinal;
- childList : array of PUCA_PropItemRec;
- begin
- locIndex := CreateIndex(ABook);
- i := Length(ABook^.Lines);
- i := 30 * i * (SizeOf(TUCA_PropItemRec) + SizeOf(TUCA_PropWeights));
- AProps := AllocMem(SizeOf(TUCA_PropBook));
- AProps^.ItemSize := i;
- AProps^.Items := AllocMem(i);
- propIndexCount := 0;
- SetLength(AProps^.Index,Length(ABook^.Lines));
- p := AProps^.Items;
- lines := @ABook^.Lines[0];
- c := Length(locIndex);
- i := 0;
- MaxChildCount := 0; MaxSize := 0;
- while (i < (c-1)) do begin
- pl1 := @lines[locIndex[i]];
- if not pl1^.Stored then begin
- i := i + 1;
- Continue;
- end;
- pl2 := @lines[locIndex[i+1]];
- if (pl1^.CodePoints[0] <> pl2^.CodePoints[0]) then begin
- if (Length(pl1^.CodePoints) = 1) then begin
- size := ConstructItem(p,pl1^.CodePoints[0],1,0,pl1^.Weights,False,@pl1^.Context,pl1^.Deleted);
- CapturePropIndex(p,pl1^.CodePoints[0]);
- p := PUCA_PropItemRec(PtrUInt(p) + size);
- if (size > MaxSize) then
- MaxSize := size;
- end else begin
- kc := Length(pl1^.CodePoints);
- SetLength(childList,kc);
- for k := 0 to kc - 2 do begin
- size := ConstructItem(p,pl1^.CodePoints[k],0,1,[],(k>0),nil,False);
- if (k = 0) then
- CapturePropIndex(p,pl1^.CodePoints[k]);
- childList[k] := p;
- p := PUCA_PropItemRec(PtrUInt(p) + size);
- end;
- size := ConstructItem(p,pl1^.CodePoints[kc-1],1,0,pl1^.Weights,True,@pl1^.Context,pl1^.Deleted);
- childList[kc-1] := p;
- p := PUCA_PropItemRec(PtrUInt(p) + size);
- for k := kc - 2 downto 0 do begin
- p1 := childList[k];
- p2 := childList[k+1];
- p1^.Size := p1^.Size + p2^.Size;
- end;
- if (p1^.Size > MaxSize) then
- MaxSize := p1^.Size;
- end;
- lineCount := 1;
- end else begin
- childCount := CalcCharChildCount(1,i,lines,c,locIndex,lineCount);
- if (childCount < 1) then
- raise Exception.CreateFmt('Expected "child count > 1" but found %d.',[childCount]);
- if (lineCount < 2) then
- raise Exception.CreateFmt('Expected "line count > 2" but found %d.',[lineCount]);
- if (childCount > MaxChildCount) then
- MaxChildCount := childCount;
- trieRoot := BuildTrie(i,lines,lineCount,locIndex);
- size := ConstructFromTrie(trieRoot,p,lines);
- CapturePropIndex(p,pl1^.CodePoints[0]);
- FreeNode(trieRoot);
- p := PUCA_PropItemRec(PtrUInt(p) + size);
- if (size > MaxSize) then
- MaxSize := size;
- end;
- i := i + lineCount;
- end;
- if (i = (c-1)) then begin
- pl1 := @lines[locIndex[i]];
- if (Length(pl1^.CodePoints) = 1) then begin
- size := ConstructItem(p,pl1^.CodePoints[0],1,0,pl1^.Weights,False,@pl1^.Context,pl1^.Deleted);
- CapturePropIndex(p,pl1^.CodePoints[0]);
- p := PUCA_PropItemRec(PtrUInt(p) + size);
- if (size > MaxSize) then
- MaxSize := size;
- end else begin
- kc := Length(pl1^.CodePoints);
- SetLength(childList,kc);
- for k := 0 to kc - 2 do begin
- size := ConstructItem(p,pl1^.CodePoints[k],0,1,[],(k>0),@pl1^.Context,pl1^.Deleted);
- if (k = 0) then
- CapturePropIndex(p,pl1^.CodePoints[0]);
- childList[k] := p;
- p := PUCA_PropItemRec(PtrUInt(p) + size);
- end;
- size := ConstructItem(p,pl1^.CodePoints[kc-1],1,0,pl1^.Weights,True,@pl1^.Context,pl1^.Deleted);
- childList[kc-1] := p;
- p := PUCA_PropItemRec(PtrUInt(p) + size);
- for i := kc - 2 downto 0 do begin
- p1 := childList[i];
- p2 := childList[i+1];
- p1^.Size := p1^.Size + p2^.Size;
- end;
- if (size > MaxSize) then
- MaxSize := size;
- end;
- end;
- //c := Int64(PtrUInt(p)) - Int64(PtrUInt(AProps^.Items));
- c := UInt64(PtrUInt(p)) - UInt64(PtrUInt(AProps^.Items));
- ReAllocMem(AProps^.Items,c);
- AProps^.ItemSize := c;
- SetLength(AProps^.Index,propIndexCount);
- AProps^.ItemsOtherEndian := AllocMem(AProps^.ItemSize);
- ReverseFromNativeEndian(AProps^.Items,AProps^.ItemSize,AProps^.ItemsOtherEndian);
- k := 0;
- c := High(Word);
- for i := 0 to Length(ABook^.Lines) - 1 do begin
- if (Length(ABook^.Lines[i].Weights) > 0) then begin
- if (ABook^.Lines[i].Weights[0].Variable) then begin
- if (ABook^.Lines[i].Weights[0].Weights[0] > k) then
- k := ABook^.Lines[i].Weights[0].Weights[0];
- if (ABook^.Lines[i].Weights[0].Weights[0] < c) then
- c := ABook^.Lines[i].Weights[0].Weights[0];
- end;
- end;
- end;
- AProps^.VariableHighLimit := k;
- AProps^.VariableLowLimit := c;
- end;
- procedure FreeUcaBook(var ABook : PUCA_PropBook);
- var
- p : PUCA_PropBook;
- begin
- if (ABook = nil) then
- exit;
- p := ABook;
- ABook := nil;
- p^.Index := nil;
- FreeMem(p^.Items,p^.ItemSize);
- FreeMem(p^.ItemsOtherEndian,p^.ItemSize);
- FreeMem(p,SizeOf(p^));
- end;
- function IndexOf(const ACodePoint : Cardinal; APropBook : PUCA_PropBook): Integer;overload;
- var
- i : Integer;
- begin
- for i := 0 to Length(APropBook^.Index) - 1 do begin
- if (ACodePoint = APropBook^.Index[i].CodePoint) then
- exit(i);
- end;
- Result := -1;
- end;
- type
- PucaBmpSecondTableItem = ^TucaBmpSecondTableItem;
- function IndexOf(
- const AItem : PucaBmpSecondTableItem;
- const ATable : TucaBmpSecondTable;
- const ATableActualLength : Integer
- ) : Integer;overload;
- var
- i : Integer;
- p : PucaBmpSecondTableItem;
- begin
- Result := -1;
- if (ATableActualLength > 0) then begin
- p := @ATable[0];
- for i := 0 to ATableActualLength - 1 do begin
- if CompareMem(p,AItem,SizeOf(TucaBmpSecondTableItem)) then begin
- Result := i;
- Break;
- end;
- Inc(p);
- end;
- end;
- end;
- procedure MakeUCA_BmpTables(
- var AFirstTable : TucaBmpFirstTable;
- var ASecondTable : TucaBmpSecondTable;
- const APropBook : PUCA_PropBook
- );
- var
- locLowByte, locHighByte : Byte;
- locTableItem : TucaBmpSecondTableItem;
- locCP : TUnicodeCodePoint;
- i, locSecondActualLen : Integer;
- k : Integer;
- begin
- SetLength(ASecondTable,120);
- locSecondActualLen := 0;
- for locHighByte := 0 to 255 do begin
- FillChar(locTableItem,SizeOf(locTableItem),#0);
- for locLowByte := 0 to 255 do begin
- locCP := (locHighByte * 256) + locLowByte;
- k := IndexOf(locCP,APropBook);
- if (k = -1) then
- k := 0
- else
- k := APropBook^.Index[k].Position + 1;
- locTableItem[locLowByte] := k;
- end;
- i := IndexOf(@locTableItem,ASecondTable,locSecondActualLen);
- if (i = -1) then begin
- if (locSecondActualLen = Length(ASecondTable)) then
- SetLength(ASecondTable,locSecondActualLen + 50);
- i := locSecondActualLen;
- ASecondTable[i] := locTableItem;
- Inc(locSecondActualLen);
- end;
- AFirstTable[locHighByte] := i;
- end;
- SetLength(ASecondTable,locSecondActualLen);
- end;
- function ToUCS4(const AHighS, ALowS : Word) : TUnicodeCodePoint; inline;
- begin
- //copied from utf16toutf32
- Result := (UCS4Char(AHighS)-$d800) shl 10 + (UCS4Char(ALowS)-$dc00) + $10000;
- end;
- procedure FromUCS4(const AValue : TUnicodeCodePoint; var AHighS, ALowS : Word);
- begin
- AHighS := Word((AValue - $10000) shr 10 + $d800);
- ALowS := Word((AValue - $10000) and $3ff + $dc00);
- end;
- type
- PucaOBmpSecondTableItem = ^TucaOBmpSecondTableItem;
- function IndexOf(
- const AItem : PucaOBmpSecondTableItem;
- const ATable : TucaOBmpSecondTable;
- const ATableActualLength : Integer
- ) : Integer;overload;
- var
- i : Integer;
- p : PucaOBmpSecondTableItem;
- begin
- Result := -1;
- if (ATableActualLength > 0) then begin
- p := @ATable[0];
- for i := 0 to ATableActualLength - 1 do begin
- if CompareMem(p,AItem,SizeOf(TucaOBmpSecondTableItem)) then begin
- Result := i;
- Break;
- end;
- Inc(p);
- end;
- end;
- end;
- procedure MakeUCA_OBmpTables(
- var AFirstTable : TucaOBmpFirstTable;
- var ASecondTable : TucaOBmpSecondTable;
- const APropBook : PUCA_PropBook
- );
- var
- locLowByte, locHighByte : Word;
- locTableItem : TucaOBmpSecondTableItem;
- locCP : TUnicodeCodePoint;
- i, locSecondActualLen : Integer;
- k : Integer;
- begin
- if (Length(ASecondTable) = 0) then
- SetLength(ASecondTable,2000);
- locSecondActualLen := 0;
- for locHighByte := 0 to HIGH_SURROGATE_COUNT - 1 do begin
- FillChar(locTableItem,SizeOf(locTableItem),#0);
- for locLowByte := 0 to LOW_SURROGATE_COUNT - 1 do begin
- locCP := ToUCS4(HIGH_SURROGATE_BEGIN + locHighByte,LOW_SURROGATE_BEGIN + locLowByte);
- k := IndexOf(locCP,APropBook);
- if (k = -1) then
- k := 0
- else
- k := APropBook^.Index[k].Position + 1;
- locTableItem[locLowByte] := k;
- end;
- i := IndexOf(@locTableItem,ASecondTable,locSecondActualLen);
- if (i = -1) then begin
- if (locSecondActualLen = Length(ASecondTable)) then
- SetLength(ASecondTable,locSecondActualLen + 50);
- i := locSecondActualLen;
- ASecondTable[i] := locTableItem;
- Inc(locSecondActualLen);
- end;
- AFirstTable[locHighByte] := i;
- end;
- SetLength(ASecondTable,locSecondActualLen);
- end;
- function GetPropPosition(
- const AHighS,
- ALowS : Word;
- const AFirstTable : PucaOBmpFirstTable;
- const ASecondTable : PucaOBmpSecondTable
- ): Integer;inline;overload;
- begin
- Result := ASecondTable^[AFirstTable^[AHighS-HIGH_SURROGATE_BEGIN]][ALowS-LOW_SURROGATE_BEGIN] - 1;
- end;
- procedure GenerateUCA_Head(
- ADest : TStream;
- ABook : PUCA_DataBook;
- AProps : PUCA_PropBook
- );
- procedure AddLine(const ALine : ansistring);
- var
- buffer : ansistring;
- begin
- buffer := ALine + sLineBreak;
- ADest.Write(buffer[1],Length(buffer));
- end;
- begin
- AddLine('const');
- //AddLine(' VERSION_STRING = ' + QuotedStr(ABook^.Version) + ';');
- AddLine(' VARIABLE_LOW_LIMIT = ' + IntToStr(AProps^.VariableLowLimit) + ';');
- AddLine(' VARIABLE_HIGH_LIMIT = ' + IntToStr(AProps^.VariableHighLimit) + ';');
- AddLine(' VARIABLE_WEIGHT = ' + IntToStr(Ord(ABook^.VariableWeight)) + ';');
- AddLine(' BACKWARDS_0 = ' + BoolToStr(ABook^.Backwards[0],'True','False') + ';');
- AddLine(' BACKWARDS_1 = ' + BoolToStr(ABook^.Backwards[1],'True','False') + ';');
- AddLine(' BACKWARDS_2 = ' + BoolToStr(ABook^.Backwards[2],'True','False') + ';');
- AddLine(' BACKWARDS_3 = ' + BoolToStr(ABook^.Backwards[3],'True','False') + ';');
- AddLine(' PROP_COUNT = ' + IntToStr(Ord(AProps^.ItemSize)) + ';');
- AddLine('');
- end;
- procedure GenerateUCA_BmpTables(
- AStream,
- ANativeEndianStream,
- ANonNativeEndianStream : TStream;
- var AFirstTable : TucaBmpFirstTable;
- var ASecondTable : TucaBmpSecondTable
- );
- procedure AddLine(AOut : TStream; const ALine : ansistring);
- var
- buffer : ansistring;
- begin
- buffer := ALine + sLineBreak;
- AOut.Write(buffer[1],Length(buffer));
- end;
- var
- i, j, c : Integer;
- locLine : string;
- value : UInt24;
- begin
- AddLine(AStream,'const');
- AddLine(AStream,' UCA_TABLE_1 : array[0..255] of Byte = (');
- locLine := '';
- for i := Low(AFirstTable) to High(AFirstTable) - 1 do begin
- locLine := locLine + IntToStr(AFirstTable[i]) + ',';
- if (((i+1) mod 16) = 0) then begin
- locLine := ' ' + locLine;
- AddLine(AStream,locLine);
- locLine := '';
- end;
- end;
- locLine := locLine + IntToStr(AFirstTable[High(AFirstTable)]);
- locLine := ' ' + locLine;
- AddLine(AStream,locLine);
- AddLine(AStream,' );' + sLineBreak);
- AddLine(ANativeEndianStream,'const');
- AddLine(ANativeEndianStream,' UCA_TABLE_2 : array[0..(256*' + IntToStr(Length(ASecondTable)) +'-1)] of UInt24 =(');
- c := High(ASecondTable);
- for i := Low(ASecondTable) to c do begin
- locLine := '';
- for j := Low(TucaBmpSecondTableItem) to High(TucaBmpSecondTableItem) do begin
- value := ASecondTable[i][j];
- locLine := locLine + UInt24ToStr(value,ENDIAN_NATIVE) + ',';
- if (((j+1) mod 2) = 0) then begin
- if (i = c) and (j = 255) then
- Delete(locLine,Length(locLine),1);
- locLine := ' ' + locLine;
- AddLine(ANativeEndianStream,locLine);
- locLine := '';
- end;
- end;
- end;
- AddLine(ANativeEndianStream,' );' + sLineBreak);
- AddLine(ANonNativeEndianStream,'const');
- AddLine(ANonNativeEndianStream,' UCA_TABLE_2 : array[0..(256*' + IntToStr(Length(ASecondTable)) +'-1)] of UInt24 =(');
- c := High(ASecondTable);
- for i := Low(ASecondTable) to c do begin
- locLine := '';
- for j := Low(TucaBmpSecondTableItem) to High(TucaBmpSecondTableItem) do begin
- value := ASecondTable[i][j];
- locLine := locLine + UInt24ToStr(value,ENDIAN_NON_NATIVE) + ',';
- if (((j+1) mod 2) = 0) then begin
- if (i = c) and (j = 255) then
- Delete(locLine,Length(locLine),1);
- locLine := ' ' + locLine;
- AddLine(ANonNativeEndianStream,locLine);
- locLine := '';
- end;
- end;
- end;
- AddLine(ANonNativeEndianStream,' );' + sLineBreak);
- end;
- procedure GenerateBinaryUCA_BmpTables(
- ANativeEndianStream,
- ANonNativeEndianStream : TStream;
- var AFirstTable : TucaBmpFirstTable;
- var ASecondTable : TucaBmpSecondTable
- );
- var
- i, j : Integer;
- value : UInt24;
- begin
- ANativeEndianStream.Write(AFirstTable[0],Length(AFirstTable));
- ANonNativeEndianStream.Write(AFirstTable[0],Length(AFirstTable));
- for i := Low(ASecondTable) to High(ASecondTable) do begin
- for j := Low(TucaBmpSecondTableItem) to High(TucaBmpSecondTableItem) do begin
- value := ASecondTable[i][j];
- ANativeEndianStream.Write(value,SizeOf(value));
- ReverseBytes(value,SizeOf(value));
- ANonNativeEndianStream.Write(value,SizeOf(value));
- end;
- end;
- end;
- procedure GenerateUCA_PropTable(
- // WARNING : files must be generated for each endianess (Little / Big)
- ADest : TStream;
- const APropBook : PUCA_PropBook;
- const AEndian : TEndianKind
- );
- procedure AddLine(const ALine : ansistring);
- var
- buffer : ansistring;
- begin
- buffer := ALine + sLineBreak;
- ADest.Write(buffer[1],Length(buffer));
- end;
- var
- i, c : Integer;
- locLine : string;
- p : PByte;
- begin
- c := APropBook^.ItemSize;
- AddLine('const');
- AddLine(' UCA_PROPS : array[0..' + IntToStr(c-1) + '] of Byte = (');
- locLine := '';
- if (AEndian = ENDIAN_NATIVE) then
- p := PByte(APropBook^.Items)
- else
- p := PByte(APropBook^.ItemsOtherEndian);
- for i := 0 to c - 2 do begin
- locLine := locLine + IntToStr(p[i]) + ',';
- if (((i+1) mod 60) = 0) then begin
- locLine := ' ' + locLine;
- AddLine(locLine);
- locLine := '';
- end;
- end;
- locLine := locLine + IntToStr(p[c-1]);
- locLine := ' ' + locLine;
- AddLine(locLine);
- AddLine(' );' + sLineBreak);
- end;
- procedure GenerateBinaryUCA_PropTable(
- // WARNING : files must be generated for each endianess (Little / Big)
- ANativeEndianStream,
- ANonNativeEndianStream : TStream;
- const APropBook : PUCA_PropBook
- );
- begin
- ANativeEndianStream.Write(APropBook^.Items^,APropBook^.ItemSize);
- ANonNativeEndianStream.Write(APropBook^.ItemsOtherEndian^,APropBook^.ItemSize);
- end;
- procedure GenerateUCA_OBmpTables(
- AStream,
- ANativeEndianStream,
- ANonNativeEndianStream : TStream;
- var AFirstTable : TucaOBmpFirstTable;
- var ASecondTable : TucaOBmpSecondTable
- );
- procedure AddLine(AOut : TStream; const ALine : ansistring);
- var
- buffer : ansistring;
- begin
- buffer := ALine + sLineBreak;
- AOut.Write(buffer[1],Length(buffer));
- end;
- var
- i, j, c : Integer;
- locLine : string;
- value : UInt24;
- begin
- AddLine(AStream,'const');
- AddLine(AStream,' UCAO_TABLE_1 : array[0..' + IntToStr(HIGH_SURROGATE_COUNT-1) + '] of Word = (');
- locLine := '';
- for i := Low(AFirstTable) to High(AFirstTable) - 1 do begin
- locLine := locLine + IntToStr(AFirstTable[i]) + ',';
- if (((i+1) mod 16) = 0) then begin
- locLine := ' ' + locLine;
- AddLine(AStream,locLine);
- locLine := '';
- end;
- end;
- locLine := locLine + IntToStr(AFirstTable[High(AFirstTable)]);
- locLine := ' ' + locLine;
- AddLine(AStream,locLine);
- AddLine(AStream,' );' + sLineBreak);
- AddLine(ANativeEndianStream,' UCAO_TABLE_2 : array[0..('+IntToStr(LOW_SURROGATE_COUNT)+'*' + IntToStr(Length(ASecondTable)) +'-1)] of UInt24 =(');
- c := High(ASecondTable);
- for i := Low(ASecondTable) to c do begin
- locLine := '';
- for j := Low(TucaOBmpSecondTableItem) to High(TucaOBmpSecondTableItem) do begin
- value := ASecondTable[i][j];
- locLine := locLine + UInt24ToStr(value,ENDIAN_NATIVE) + ',';
- if (((j+1) mod 2) = 0) then begin
- if (i = c) and (j = High(TucaOBmpSecondTableItem)) then
- Delete(locLine,Length(locLine),1);
- locLine := ' ' + locLine;
- AddLine(ANativeEndianStream,locLine);
- locLine := '';
- end;
- end;
- end;
- AddLine(ANativeEndianStream,' );' + sLineBreak);
- AddLine(ANonNativeEndianStream,' UCAO_TABLE_2 : array[0..('+IntToStr(LOW_SURROGATE_COUNT)+'*' + IntToStr(Length(ASecondTable)) +'-1)] of UInt24 =(');
- c := High(ASecondTable);
- for i := Low(ASecondTable) to c do begin
- locLine := '';
- for j := Low(TucaOBmpSecondTableItem) to High(TucaOBmpSecondTableItem) do begin
- value := ASecondTable[i][j];
- locLine := locLine + UInt24ToStr(value,ENDIAN_NON_NATIVE) + ',';
- if (((j+1) mod 2) = 0) then begin
- if (i = c) and (j = High(TucaOBmpSecondTableItem)) then
- Delete(locLine,Length(locLine),1);
- locLine := ' ' + locLine;
- AddLine(ANonNativeEndianStream,locLine);
- locLine := '';
- end;
- end;
- end;
- AddLine(ANonNativeEndianStream,' );' + sLineBreak);
- end;
- procedure GenerateBinaryUCA_OBmpTables(
- ANativeEndianStream,
- ANonNativeEndianStream : TStream;
- var AFirstTable : TucaOBmpFirstTable;
- var ASecondTable : TucaOBmpSecondTable
- );
- var
- i, j : Integer;
- locLine : string;
- wordValue : Word;
- value : UInt24;
- begin
- for i := Low(AFirstTable) to High(AFirstTable) do begin
- wordValue := AFirstTable[i];
- ANativeEndianStream.Write(wordValue,SizeOf(wordValue));
- ReverseBytes(wordValue,SizeOf(wordValue));
- ANonNativeEndianStream.Write(wordValue,SizeOf(wordValue));
- end;
- for i := Low(ASecondTable) to High(ASecondTable) do begin
- for j := Low(TucaOBmpSecondTableItem) to High(TucaOBmpSecondTableItem) do begin
- value := ASecondTable[i][j];
- ANativeEndianStream.Write(value,SizeOf(value));
- ReverseBytes(value,SizeOf(value));
- ANonNativeEndianStream.Write(value,SizeOf(value));
- end;
- end;
- end;
- type
- POBmpSecondTableItem = ^TOBmpSecondTableItem;
- function IndexOf(
- const AItem : POBmpSecondTableItem;
- const ATable : TOBmpSecondTable;
- const ATableActualLength : Integer
- ) : Integer;overload;
- var
- i : Integer;
- p : POBmpSecondTableItem;
- begin
- Result := -1;
- if (ATableActualLength > 0) then begin
- p := @ATable[0];
- for i := 0 to ATableActualLength - 1 do begin
- if CompareMem(p,AItem,SizeOf(TOBmpSecondTableItem)) then begin
- Result := i;
- Break;
- end;
- Inc(p);
- end;
- end;
- end;
- procedure MakeOBmpTables(
- var AFirstTable : TOBmpFirstTable;
- var ASecondTable : TOBmpSecondTable;
- const ADataLineList : TDataLineRecArray
- );
- var
- locLowByte, locHighByte : Word;
- locTableItem : TOBmpSecondTableItem;
- locCP : TUnicodeCodePoint;
- i, locSecondActualLen : Integer;
- begin
- SetLength(ASecondTable,2000);
- locSecondActualLen := 0;
- for locHighByte := 0 to HIGH_SURROGATE_COUNT - 1 do begin
- FillChar(locTableItem,SizeOf(locTableItem),#0);
- for locLowByte := 0 to LOW_SURROGATE_COUNT - 1 do begin
- locCP := ToUCS4(HIGH_SURROGATE_BEGIN + locHighByte,LOW_SURROGATE_BEGIN + locLowByte);
- locTableItem[locLowByte] := GetPropID(locCP,ADataLineList)// - 1;
- end;
- i := IndexOf(@locTableItem,ASecondTable,locSecondActualLen);
- if (i = -1) then begin
- if (locSecondActualLen = Length(ASecondTable)) then
- SetLength(ASecondTable,locSecondActualLen + 50);
- i := locSecondActualLen;
- ASecondTable[i] := locTableItem;
- Inc(locSecondActualLen);
- end;
- AFirstTable[locHighByte] := i;
- end;
- SetLength(ASecondTable,locSecondActualLen);
- end;
- type
- P3lvlOBmp3TableItem = ^T3lvlOBmp3TableItem;
- function IndexOf(
- const AItem : P3lvlOBmp3TableItem;
- const ATable : T3lvlOBmp3Table;
- const ATableActualLength : Integer
- ) : Integer;overload;
- var
- i : Integer;
- p : P3lvlOBmp3TableItem;
- begin
- Result := -1;
- if (ATableActualLength > 0) then begin
- p := @ATable[0];
- for i := 0 to ATableActualLength - 1 do begin
- if CompareMem(p,AItem,SizeOf(T3lvlOBmp3TableItem)) then begin
- Result := i;
- Break;
- end;
- Inc(p);
- end;
- end;
- end;
- type
- P3lvlOBmp2TableItem = ^T3lvlOBmp2TableItem;
- function IndexOf(
- const AItem : P3lvlOBmp2TableItem;
- const ATable : T3lvlOBmp2Table
- ) : Integer;overload;
- var
- i : Integer;
- p : P3lvlOBmp2TableItem;
- begin
- Result := -1;
- if (Length(ATable) > 0) then begin
- p := @ATable[0];
- for i := 0 to Length(ATable) - 1 do begin
- if CompareMem(p,AItem,SizeOf(T3lvlOBmp2TableItem)) then begin
- Result := i;
- Break;
- end;
- Inc(p);
- end;
- end;
- end;
- procedure MakeOBmpTables3Levels(
- var AFirstTable : T3lvlOBmp1Table;
- var ASecondTable : T3lvlOBmp2Table;
- var AThirdTable : T3lvlOBmp3Table;
- const ADataLineList : TDataLineRecArray
- );
- var
- locLowByte0, locLowByte1, locHighByte : Word;
- locTableItem2 : T3lvlOBmp2TableItem;
- locTableItem3 : T3lvlOBmp3TableItem;
- locCP : TUnicodeCodePoint;
- i, locThirdActualLen : Integer;
- begin
- SetLength(AThirdTable,120);
- locThirdActualLen := 0;
- for locHighByte := 0 to 1023 do begin
- FillChar(locTableItem2,SizeOf(locTableItem2),#0);
- for locLowByte0 := 0 to 31 do begin
- FillChar(locTableItem3,SizeOf(locTableItem3),#0);
- for locLowByte1 := 0 to 31 do begin
- locCP := ToUCS4(HIGH_SURROGATE_BEGIN + locHighByte,LOW_SURROGATE_BEGIN + (locLowByte0*32) + locLowByte1);
- locTableItem3[locLowByte1] := GetPropID(locCP,ADataLineList);
- end;
- i := IndexOf(@locTableItem3,AThirdTable,locThirdActualLen);
- if (i = -1) then begin
- if (locThirdActualLen = Length(AThirdTable)) then
- SetLength(AThirdTable,locThirdActualLen + 50);
- i := locThirdActualLen;
- AThirdTable[i] := locTableItem3;
- Inc(locThirdActualLen);
- end;
- locTableItem2[locLowByte0] := i;
- end;
- i := IndexOf(@locTableItem2,ASecondTable);
- if (i = -1) then begin
- i := Length(ASecondTable);
- SetLength(ASecondTable,(i + 1));
- ASecondTable[i] := locTableItem2;
- end;
- AFirstTable[locHighByte] := i;
- end;
- SetLength(AThirdTable,locThirdActualLen);
- end;
- procedure GenerateOBmpTables(
- ADest : TStream;
- var AFirstTable : TOBmpFirstTable;
- var ASecondTable : TOBmpSecondTable
- );
- procedure AddLine(const ALine : ansistring);
- var
- buffer : ansistring;
- begin
- buffer := ALine + sLineBreak;
- ADest.Write(buffer[1],Length(buffer));
- end;
- var
- i, j, c : Integer;
- locLine : string;
- begin
- AddLine('const');
- AddLine(' UCO_TABLE_1 : array[0..' + IntToStr(HIGH_SURROGATE_COUNT-1) + '] of Word = (');
- locLine := '';
- for i := Low(AFirstTable) to High(AFirstTable) - 1 do begin
- locLine := locLine + IntToStr(AFirstTable[i]) + ',';
- if (((i+1) mod 16) = 0) then begin
- locLine := ' ' + locLine;
- AddLine(locLine);
- locLine := '';
- end;
- end;
- locLine := locLine + IntToStr(AFirstTable[High(AFirstTable)]);
- locLine := ' ' + locLine;
- AddLine(locLine);
- AddLine(' );' + sLineBreak);
- AddLine(' UCO_TABLE_2 : array[0..('+IntToStr(LOW_SURROGATE_COUNT)+'*' + IntToStr(Length(ASecondTable)) +'-1)] of Word =(');
- c := High(ASecondTable);
- for i := Low(ASecondTable) to c do begin
- locLine := '';
- for j := Low(TOBmpSecondTableItem) to High(TOBmpSecondTableItem) do begin
- locLine := locLine + IntToStr(ASecondTable[i][j]) + ',';
- if (((j+1) mod 16) = 0) then begin
- if (i = c) and (j = High(TOBmpSecondTableItem)) then
- Delete(locLine,Length(locLine),1);
- locLine := ' ' + locLine;
- AddLine(locLine);
- locLine := '';
- end;
- end;
- end;
- AddLine(' );' + sLineBreak);
- end;
- //----------------------------------
- procedure Generate3lvlOBmpTables(
- ADest : TStream;
- var AFirstTable : T3lvlOBmp1Table;
- var ASecondTable : T3lvlOBmp2Table;
- var AThirdTable : T3lvlOBmp3Table
- );
- procedure AddLine(const ALine : ansistring);
- var
- buffer : ansistring;
- begin
- buffer := ALine + sLineBreak;
- ADest.Write(buffer[1],Length(buffer));
- end;
- var
- i, j, c : Integer;
- locLine : string;
- begin
- AddLine('const');
- AddLine(' UCO_TABLE_1 : array[0..1023] of Word = (');
- locLine := '';
- for i := Low(AFirstTable) to High(AFirstTable) - 1 do begin
- locLine := locLine + IntToStr(AFirstTable[i]) + ',';
- if (((i+1) mod 16) = 0) then begin
- locLine := ' ' + locLine;
- AddLine(locLine);
- locLine := '';
- end;
- end;
- locLine := locLine + IntToStr(AFirstTable[High(AFirstTable)]);
- locLine := ' ' + locLine;
- AddLine(locLine);
- AddLine(' );' + sLineBreak);
- AddLine(' UCO_TABLE_2 : array[0..' + IntToStr(Length(ASecondTable)-1) +'] of array[0..31] of Word = (');
- c := High(ASecondTable);
- for i := Low(ASecondTable) to c do begin
- locLine := '(';
- for j := Low(T3lvlOBmp2TableItem) to High(T3lvlOBmp2TableItem) do
- locLine := locLine + IntToStr(ASecondTable[i][j]) + ',';
- Delete(locLine,Length(locLine),1);
- locLine := ' ' + locLine + ')';
- if (i < c) then
- locLine := locLine + ',';
- AddLine(locLine);
- end;
- AddLine(' );' + sLineBreak);
- AddLine(' UCO_TABLE_3 : array[0..' + IntToStr(Length(AThirdTable)-1) +'] of array[0..31] of Word = (');
- c := High(AThirdTable);
- for i := Low(AThirdTable) to c do begin
- locLine := '(';
- for j := Low(T3lvlOBmp3TableItem) to High(T3lvlOBmp3TableItem) do
- locLine := locLine + IntToStr(AThirdTable[i][j]) + ',';
- Delete(locLine,Length(locLine),1);
- locLine := ' ' + locLine + ')';
- if (i < c) then
- locLine := locLine + ',';
- AddLine(locLine);
- end;
- AddLine(' );' + sLineBreak);
- end;
- function GetProp(
- const AHighS,
- ALowS : Word;
- const AProps : TPropRecArray;
- var AFirstTable : TOBmpFirstTable;
- var ASecondTable : TOBmpSecondTable
- ): PPropRec;
- begin
- Result := @AProps[ASecondTable[AFirstTable[AHighS-HIGH_SURROGATE_BEGIN]][ALowS-LOW_SURROGATE_BEGIN]];
- end;
- function GetProp(
- const AHighS,
- ALowS : Word;
- const AProps : TPropRecArray;
- var AFirstTable : T3lvlOBmp1Table;
- var ASecondTable : T3lvlOBmp2Table;
- var AThirdTable : T3lvlOBmp3Table
- ): PPropRec;
- begin
- Result := @AProps[AThirdTable[ASecondTable[AFirstTable[AHighS]][ALowS div 32]][ALowS mod 32]];
- //Result := @AProps[ASecondTable[AFirstTable[AHighS-HIGH_SURROGATE_BEGIN]][ALowS-LOW_SURROGATE_BEGIN]];
- end;
- { TUCA_PropItemContextTreeRec }
- function TUCA_PropItemContextTreeRec.GetData : PUCA_PropItemContextTreeNodeRec;
- begin
- if (Size = 0) then
- Result := nil
- else
- Result := PUCA_PropItemContextTreeNodeRec(
- PtrUInt(
- PtrUInt(@Self) + SizeOf(UInt24){Size}
- )
- );
- end;
- { TUCA_LineContextRec }
- procedure TUCA_LineContextRec.Clear;
- begin
- Data := nil
- end;
- procedure TUCA_LineContextRec.Assign(ASource : PUCA_LineContextRec);
- var
- c, i : Integer;
- begin
- if (ASource = nil) then begin
- Clear();
- exit;
- end;
- c := Length(ASource^.Data);
- SetLength(Self.Data,c);
- for i := 0 to c-1 do
- Self.Data[i].Assign(@ASource^.Data[i]);
- end;
- function TUCA_LineContextRec.Clone : TUCA_LineContextRec;
- begin
- Result.Clear();
- Result.Assign(@Self);
- end;
- { TUCA_LineContextItemRec }
- procedure TUCA_LineContextItemRec.Clear();
- begin
- CodePoints := nil;
- Weights := nil;
- end;
- procedure TUCA_LineContextItemRec.Assign(ASource : PUCA_LineContextItemRec);
- begin
- if (ASource = nil) then begin
- Clear();
- exit;
- end;
- Self.CodePoints := Copy(ASource^.CodePoints);
- Self.Weights := Copy(ASource^.Weights);
- end;
- function TUCA_LineContextItemRec.Clone() : TUCA_LineContextItemRec;
- begin
- Result.Clear();
- Result.Assign(@Self);
- end;
- { TUCA_LineRec }
- procedure TUCA_LineRec.Clear;
- begin
- CodePoints := nil;
- Weights := nil;
- Deleted := False;
- Stored := False;
- Context.Clear();
- end;
- procedure TUCA_LineRec.Assign(ASource : PUCA_LineRec);
- begin
- if (ASource = nil) then begin
- Clear();
- exit;
- end;
- Self.CodePoints := Copy(ASource^.CodePoints);
- Self.Weights := Copy(ASource^.Weights);
- Self.Deleted := ASource^.Deleted;
- Self.Stored := ASource^.Stored;
- Self.Context.Assign(@ASource^.Context);
- end;
- function TUCA_LineRec.Clone : TUCA_LineRec;
- begin
- Result.Clear();
- Result.Assign(@Self);
- end;
- function TUCA_LineRec.HasContext() : Boolean;
- begin
- Result := (Length(Context.Data) > 0);
- end;
- { TPropRec }
- function TPropRec.GetCategory: TUnicodeCategory;
- begin
- Result := TUnicodeCategory((CategoryData and Byte($F8)) shr 3);
- end;
- procedure TPropRec.SetCategory(AValue: TUnicodeCategory);
- var
- b : Byte;
- begin
- b := Ord(AValue);
- b := b shl 3;
- CategoryData := CategoryData or b;
- //CategoryData := CategoryData or Byte(Byte(Ord(AValue)) shl 3);
- end;
- function TPropRec.GetWhiteSpace: Boolean;
- begin
- Result := IsBitON(CategoryData,0);
- end;
- procedure TPropRec.SetWhiteSpace(AValue: Boolean);
- begin
- SetBit(CategoryData,0,AValue);
- end;
- function TPropRec.GetHangulSyllable: Boolean;
- begin
- Result := IsBitON(CategoryData,1);
- end;
- procedure TPropRec.SetHangulSyllable(AValue: Boolean);
- begin
- SetBit(CategoryData,1,AValue);
- end;
- { TUCA_PropItemRec }
- function TUCA_PropItemRec.GetWeightSize : Word;
- var
- c : Integer;
- begin
- c := WeightLength;
- if (c = 0) then
- exit(0);
- Result := c*SizeOf(TUCA_PropWeights);
- if IsWeightCompress_1() then
- Result := Result - 1;
- if IsWeightCompress_2() then
- Result := Result - 1;
- end;
- function TUCA_PropItemRec.HasCodePoint(): Boolean;
- begin
- Result := IsBitON(Flags,FLAG_CODEPOINT);
- end;
- procedure TUCA_PropItemRec.GetWeightArray(ADest: PUCA_PropWeights);
- var
- c : Integer;
- p : PByte;
- pd : PUCA_PropWeights;
- begin
- c := WeightLength;
- p := PByte(PtrUInt(@Self) + SizeOf(TUCA_PropItemRec));
- pd := ADest;
- pd^.Weights[0] := PWord(p)^;
- p := p + 2;
- if not IsWeightCompress_1() then begin
- pd^.Weights[1] := PWord(p)^;
- p := p + 2;
- end else begin
- pd^.Weights[1] := p^;
- p := p + 1;
- end;
- if not IsWeightCompress_2() then begin
- pd^.Weights[2] := PWord(p)^;
- p := p + 2;
- end else begin
- pd^.Weights[2] := p^;
- p := p + 1;
- end;
- if (c > 1) then
- Move(p^, (pd+1)^, ((c-1)*SizeOf(TUCA_PropWeights)));
- end;
- function TUCA_PropItemRec.GetSelfOnlySize() : Cardinal;
- begin
- Result := SizeOf(TUCA_PropItemRec);
- if (WeightLength > 0) then begin
- Result := Result + (WeightLength * Sizeof(TUCA_PropWeights));
- if IsWeightCompress_1() then
- Result := Result - 1;
- if IsWeightCompress_2() then
- Result := Result - 1;
- end;
- if HasCodePoint() then
- Result := Result + SizeOf(UInt24);
- if Contextual then
- Result := Result + Cardinal(GetContext()^.Size);
- end;
- procedure TUCA_PropItemRec.SetContextual(AValue : Boolean);
- begin
- SetBit(Flags,FLAG_CONTEXTUAL,AValue);
- end;
- function TUCA_PropItemRec.GetContextual : Boolean;
- begin
- Result := IsBitON(Flags,FLAG_CONTEXTUAL);
- end;
- function TUCA_PropItemRec.GetContext() : PUCA_PropItemContextTreeRec;
- var
- p : PtrUInt;
- begin
- if not Contextual then
- exit(nil);
- p := PtrUInt(@Self) + SizeOf(TUCA_PropItemRec);
- if IsBitON(Flags,FLAG_CODEPOINT) then
- p := p + SizeOf(UInt24);
- Result := PUCA_PropItemContextTreeRec(p);
- end;
- procedure TUCA_PropItemRec.SetDeleted(AValue: Boolean);
- begin
- SetBit(Flags,FLAG_DELETION,AValue);
- end;
- function TUCA_PropItemRec.IsDeleted: Boolean;
- begin
- Result := IsBitON(Flags,FLAG_DELETION);
- end;
- function TUCA_PropItemRec.IsValid() : Boolean;
- begin
- Result := IsBitON(Flags,FLAG_VALID);
- end;
- function TUCA_PropItemRec.IsWeightCompress_1 : Boolean;
- begin
- Result := IsBitON(Flags,FLAG_COMPRESS_WEIGHT_1);
- end;
- function TUCA_PropItemRec.IsWeightCompress_2 : Boolean;
- begin
- Result := IsBitON(Flags,FLAG_COMPRESS_WEIGHT_2);
- end;
- function TUCA_PropItemRec.GetCodePoint: UInt24;
- begin
- if HasCodePoint() then begin
- if Contextual then
- Result := PUInt24(
- PtrUInt(@Self) + Self.GetSelfOnlySize()- SizeOf(UInt24) -
- Cardinal(GetContext()^.Size)
- )^
- else
- Result := PUInt24(PtrUInt(@Self) + Self.GetSelfOnlySize() - SizeOf(UInt24))^
- end else begin
- raise Exception.Create('TUCA_PropItemRec.GetCodePoint : "No code point available."');
- end
- end;
- function avl_CompareCodePoints(Item1, Item2: Pointer): Integer;
- var
- a, b : PUCA_LineContextItemRec;
- i, hb : Integer;
- begin
- if (Item1 = Item2) then
- exit(0);
- if (Item1 = nil) then
- exit(-1);
- if (Item2 = nil) then
- exit(1);
- a := Item1;
- b := Item2;
- if (a^.CodePoints = b^.CodePoints) then
- exit(0);
- Result := 1;
- hb := Length(b^.CodePoints) - 1;
- for i := 0 to Length(a^.CodePoints) - 1 do begin
- if (i > hb) then
- exit;
- if (a^.CodePoints[i] < b^.CodePoints[i]) then
- exit(-1);
- if (a^.CodePoints[i] > b^.CodePoints[i]) then
- exit(1);
- end;
- if (Length(a^.CodePoints) = Length(b^.CodePoints)) then
- exit(0);
- exit(-1);
- end;
- function ConstructAvlContextTree(AContext : PUCA_LineContextRec) : TAVLTree;
- var
- r : TAVLTree;
- i : Integer;
- begin
- r := TAVLTree.Create(@avl_CompareCodePoints);
- try
- for i := 0 to Length(AContext^.Data) - 1 do
- r.Add(@AContext^.Data[i]);
- Result := r;
- except
- FreeAndNil(r);
- raise;
- end;
- end;
- function ConstructContextTree(
- const AContext : PUCA_LineContextRec;
- var ADestBuffer;
- const ADestBufferLength : Cardinal
- ) : PUCA_PropItemContextTreeRec;
- function CalcItemOnlySize(AItem : TAVLTreeNode) : Cardinal;
- var
- kitem : PUCA_LineContextItemRec;
- begin
- if (AItem = nil) then
- exit(0);
- kitem := AItem.Data;
- Result := SizeOf(PUCA_PropItemContextTreeNodeRec^.Left) +
- SizeOf(PUCA_PropItemContextTreeNodeRec^.Right) +
- SizeOf(PUCA_PropItemContextRec^.CodePointCount) +
- (Length(kitem^.CodePoints)*SizeOf(UInt24)) +
- SizeOf(PUCA_PropItemContextRec^.WeightCount) +
- (Length(kitem^.Weights)*SizeOf(TUCA_PropWeights));
- end;
- function CalcItemSize(AItem : TAVLTreeNode) : Cardinal;
- begin
- if (AItem = nil) then
- exit(0);
- Result := CalcItemOnlySize(AItem);
- if (AItem.Left <> nil) then
- Result := Result + CalcItemSize(AItem.Left);
- if (AItem.Right <> nil) then
- Result := Result + CalcItemSize(AItem.Right);
- end;
- function CalcSize(AData : TAVLTree) : Cardinal;
- begin
- Result := SizeOf(PUCA_PropItemContextTreeRec^.Size) + CalcItemSize(AData.Root);
- end;
- function ConstructItem(ASource : TAVLTreeNode; ADest : PUCA_PropItemContextTreeNodeRec) : Cardinal;
- var
- k : Integer;
- kitem : PUCA_LineContextItemRec;
- kpcp : PUInt24;
- kpw : PUCA_PropWeights;
- pextra : PtrUInt;
- pnext : PUCA_PropItemContextTreeNodeRec;
- begin
- kitem := ASource.Data;
- ADest^.Data.CodePointCount := Length(kitem^.CodePoints);
- ADest^.Data.WeightCount := Length(kitem^.Weights);
- pextra := PtrUInt(ADest)+SizeOf(ADest^.Left)+SizeOf(ADest^.Right)+
- SizeOf(ADest^.Data.CodePointCount)+SizeOf(ADest^.Data.WeightCount);
- if (ADest^.Data.CodePointCount > 0) then begin
- kpcp := PUInt24(pextra);
- for k := 0 to ADest^.Data.CodePointCount - 1 do begin
- kpcp^ := kitem^.CodePoints[k];
- Inc(kpcp);
- end;
- end;
- if (ADest^.Data.WeightCount > 0) then begin
- kpw := PUCA_PropWeights(pextra + (ADest^.Data.CodePointCount*SizeOf(UInt24)));
- for k := 0 to ADest^.Data.WeightCount - 1 do begin
- kpw^.Weights[0] := kitem^.Weights[k].Weights[0];
- kpw^.Weights[1] := kitem^.Weights[k].Weights[1];
- kpw^.Weights[2] := kitem^.Weights[k].Weights[2];
- Inc(kpw);
- end;
- end;
- Result := CalcItemOnlySize(ASource);
- if (ASource.Left <> nil) then begin
- pnext := PUCA_PropItemContextTreeNodeRec(PtrUInt(ADest) + Result);
- ADest^.Left := Result;
- Result := Result + ConstructItem(ASource.Left,pnext);
- end else begin
- ADest^.Left := 0;
- end;
- if (ASource.Right <> nil) then begin
- pnext := PUCA_PropItemContextTreeNodeRec(PtrUInt(ADest) + Result);
- ADest^.Right := Result;
- Result := Result + ConstructItem(ASource.Right,pnext);
- end else begin
- ADest^.Right := 0;
- end;
- end;
- var
- c : PtrUInt;
- r : PUCA_PropItemContextTreeRec;
- p : PUCA_PropItemContextTreeNodeRec;
- tempTree : TAVLTree;
- begin
- tempTree := ConstructAvlContextTree(AContext);
- try
- c := CalcSize(tempTree);
- if (ADestBufferLength > 0) and (c > ADestBufferLength) then
- raise Exception.Create(SInsufficientMemoryBuffer);
- r := @ADestBuffer;
- r^.Size := c;
- p := PUCA_PropItemContextTreeNodeRec(PtrUInt(r) + SizeOf(r^.Size));
- ConstructItem(tempTree.Root,p);
- finally
- tempTree.Free();
- end;
- Result := r;
- end;
- procedure ReverseRecordBytes(var AItem : TSerializedCollationHeader);
- begin
- ReverseBytes(AItem.BMP_Table1Length,SizeOf(AItem.BMP_Table1Length));
- ReverseBytes(AItem.BMP_Table2Length,SizeOf(AItem.BMP_Table2Length));
- ReverseBytes(AItem.OBMP_Table1Length,SizeOf(AItem.OBMP_Table1Length));
- ReverseBytes(AItem.OBMP_Table2Length,SizeOf(AItem.OBMP_Table2Length));
- ReverseBytes(AItem.PropCount,SizeOf(AItem.PropCount));
- ReverseBytes(AItem.VariableLowLimit,SizeOf(AItem.VariableLowLimit));
- ReverseBytes(AItem.VariableHighLimit,SizeOf(AItem.VariableHighLimit));
- end;
- procedure ReverseBytes(var AData; const ALength : Integer);
- var
- i,j : PtrInt;
- c : Byte;
- p : PByte;
- begin
- if (ALength = 1) then
- exit;
- p := @AData;
- j := ALength div 2;
- for i := 0 to Pred(j) do begin
- c := p[i];
- p[i] := p[(ALength - 1 ) - i];
- p[(ALength - 1 ) - i] := c;
- end;
- end;
- procedure ReverseArray(var AValue; const AArrayLength, AItemSize : PtrInt);
- var
- p : PByte;
- i : PtrInt;
- begin
- if ( AArrayLength > 0 ) and ( AItemSize > 1 ) then begin
- p := @AValue;
- for i := 0 to Pred(AArrayLength) do begin
- ReverseBytes(p^,AItemSize);
- Inc(p,AItemSize);
- end;
- end;
- end;
- procedure ReverseContextNodeFromNativeEndian(s, d : PUCA_PropItemContextTreeNodeRec);
- var
- k : PtrUInt;
- p_s, p_d : PByte;
- begin
- d^.Left := s^.Left;
- ReverseBytes(d^.Left,SizeOf(d^.Left));
- d^.Right := s^.Right;
- ReverseBytes(d^.Right,SizeOf(d^.Right));
- d^.Data.CodePointCount := s^.Data.CodePointCount;
- ReverseBytes(d^.Data.CodePointCount,SizeOf(d^.Data.CodePointCount));
- d^.Data.WeightCount := s^.Data.WeightCount;
- ReverseBytes(d^.Data.WeightCount,SizeOf(d^.Data.WeightCount));
- k := SizeOf(TUCA_PropItemContextTreeNodeRec);
- p_s := PByte(PtrUInt(s) + k);
- p_d := PByte(PtrUInt(d) + k);
- k := (s^.Data.CodePointCount*SizeOf(UInt24));
- Move(p_s^,p_d^, k);
- ReverseArray(p_d^,s^.Data.CodePointCount,SizeOf(UInt24));
- p_s := PByte(PtrUInt(p_s) + k);
- p_d := PByte(PtrUInt(p_d) + k);
- k := (s^.Data.WeightCount*SizeOf(TUCA_PropWeights));
- Move(p_s^,p_d^,k);
- ReverseArray(p_d^,(s^.Data.WeightCount*Length(TUCA_PropWeights.Weights)),SizeOf(TUCA_PropWeights.Weights[0]));
- if (s^.Left > 0) then
- ReverseContextNodeFromNativeEndian(
- PUCA_PropItemContextTreeNodeRec(PtrUInt(s) + s^.Left),
- PUCA_PropItemContextTreeNodeRec(PtrUInt(d) + s^.Left)
- );
- if (s^.Right > 0) then
- ReverseContextNodeFromNativeEndian(
- PUCA_PropItemContextTreeNodeRec(PtrUInt(s) + s^.Right),
- PUCA_PropItemContextTreeNodeRec(PtrUInt(d) + s^.Right)
- );
- end;
- procedure ReverseContextFromNativeEndian(s, d : PUCA_PropItemContextTreeRec);
- var
- k : PtrUInt;
- begin
- d^.Size := s^.Size;
- ReverseBytes(d^.Size,SizeOf(d^.Size));
- if (s^.Size = 0) then
- exit;
- k := SizeOf(s^.Size);
- ReverseContextNodeFromNativeEndian(
- PUCA_PropItemContextTreeNodeRec(PtrUInt(s)+k),
- PUCA_PropItemContextTreeNodeRec(PtrUInt(d)+k)
- );
- end;
- procedure ReverseFromNativeEndian(
- const AData : PUCA_PropItemRec;
- const ADataLen : Cardinal;
- const ADest : PUCA_PropItemRec
- );
- var
- s, d : PUCA_PropItemRec;
- sCtx, dCtx : PUCA_PropItemContextTreeRec;
- dataEnd : PtrUInt;
- k, i : PtrUInt;
- p_s, p_d : PByte;
- pw_s, pw_d : PUCA_PropWeights;
- begin
- dataEnd := PtrUInt(AData) + ADataLen;
- s := AData;
- d := ADest;
- while True do begin
- d^.WeightLength := s^.WeightLength;
- ReverseBytes(d^.WeightLength,SizeOf(d^.WeightLength));
- d^.ChildCount := s^.ChildCount;
- ReverseBytes(d^.ChildCount,SizeOf(d^.ChildCount));
- d^.Size := s^.Size;
- ReverseBytes(d^.Size,SizeOf(d^.Size));
- d^.Flags := s^.Flags;
- ReverseBytes(d^.Flags,SizeOf(d^.Flags));
- if s^.Contextual then begin
- k := SizeOf(TUCA_PropItemRec);
- if s^.HasCodePoint() then
- k := k + SizeOf(UInt24);
- sCtx := PUCA_PropItemContextTreeRec(PtrUInt(s) + k);
- dCtx := PUCA_PropItemContextTreeRec(PtrUInt(d) + k);
- ReverseContextFromNativeEndian(sCtx,dCtx);
- end;
- if s^.HasCodePoint() then begin
- if s^.Contextual then
- k := s^.GetSelfOnlySize()- SizeOf(UInt24) - Cardinal(s^.GetContext()^.Size)
- else
- k := s^.GetSelfOnlySize() - SizeOf(UInt24);
- p_s := PByte(PtrUInt(s) + k);
- p_d := PByte(PtrUInt(d) + k);
- Unaligned(PUInt24(p_d)^) := Unaligned(PUInt24(p_s)^);
- ReverseBytes(p_d^,SizeOf(UInt24));
- end;
- if (s^.WeightLength > 0) then begin
- k := SizeOf(TUCA_PropItemRec);
- p_s := PByte(PtrUInt(s) + k);
- p_d := PByte(PtrUInt(d) + k);
- k := SizeOf(Word);
- Unaligned(PWord(p_d)^) := Unaligned(PWord(p_s)^);
- ReverseBytes(Unaligned(p_d^),k);
- p_s := PByte(PtrUInt(p_s) + k);
- p_d := PByte(PtrUInt(p_d) + k);
- if s^.IsWeightCompress_1() then begin
- k := SizeOf(Byte);
- PByte(p_d)^ := PByte(p_s)^;
- end else begin
- k := SizeOf(Word);
- Unaligned(PWord(p_d)^) := Unaligned(PWord(p_s)^);
- end;
- ReverseBytes(p_d^,k);
- p_s := PByte(PtrUInt(p_s) + k);
- p_d := PByte(PtrUInt(p_d) + k);
- if s^.IsWeightCompress_2() then begin
- k := SizeOf(Byte);
- PByte(p_d)^ := PByte(p_s)^;
- end else begin
- k := SizeOf(Word);
- Unaligned(PWord(p_d)^) := Unaligned(PWord(p_s)^);
- end;
- ReverseBytes(p_d^,k);
- if (s^.WeightLength > 1) then begin
- pw_s := PUCA_PropWeights(PtrUInt(p_s) + k);
- pw_d := PUCA_PropWeights(PtrUInt(p_d) + k);
- for i := 1 to s^.WeightLength - 1 do begin
- pw_d^.Weights[0] := pw_s^.Weights[0];
- pw_d^.Weights[1] := pw_s^.Weights[1];
- pw_d^.Weights[2] := pw_s^.Weights[2];
- ReverseArray(pw_d^,3,SizeOf(pw_s^.Weights[0]));
- Inc(pw_s);
- Inc(pw_d);
- end;
- end;
- end;
- k := s^.GetSelfOnlySize();
- s := PUCA_PropItemRec(PtrUInt(s)+k);
- d := PUCA_PropItemRec(PtrUInt(d)+k);
- if (PtrUInt(s) >= dataEnd) then
- Break;
- end;
- if ( (PtrUInt(s)-PtrUInt(AData)) <> (PtrUInt(d)-PtrUInt(ADest)) ) then
- raise Exception.CreateFmt('Read data length(%d) differs from written data length(%d).',[(PtrUInt(s)-PtrUInt(AData)), (PtrUInt(d)-PtrUInt(ADest))]);
- end;
- //------------------------------------------------------------------------------
- procedure ReverseContextNodeToNativeEndian(s, d : PUCA_PropItemContextTreeNodeRec);
- var
- k : PtrUInt;
- p_s, p_d : PByte;
- begin
- d^.Left := s^.Left;
- ReverseBytes(d^.Left,SizeOf(d^.Left));
- d^.Right := s^.Right;
- ReverseBytes(d^.Right,SizeOf(d^.Right));
- d^.Data.CodePointCount := s^.Data.CodePointCount;
- ReverseBytes(d^.Data.CodePointCount,SizeOf(d^.Data.CodePointCount));
- d^.Data.WeightCount := s^.Data.WeightCount;
- ReverseBytes(d^.Data.WeightCount,SizeOf(d^.Data.WeightCount));
- k := SizeOf(TUCA_PropItemContextTreeNodeRec);
- p_s := PByte(PtrUInt(s) + k);
- p_d := PByte(PtrUInt(d) + k);
- k := (d^.Data.CodePointCount*SizeOf(UInt24));
- Move(p_s^,p_d^, k);
- ReverseArray(p_d^,d^.Data.CodePointCount,SizeOf(UInt24));
- p_s := PByte(PtrUInt(p_s) + k);
- p_d := PByte(PtrUInt(p_d) + k);
- k := (d^.Data.WeightCount*SizeOf(TUCA_PropWeights));
- Move(p_s^,p_d^,k);
- ReverseArray(p_d^,(d^.Data.WeightCount*Length(TUCA_PropWeights.Weights)),SizeOf(TUCA_PropWeights.Weights[0]));
- if (d^.Left > 0) then
- ReverseContextNodeToNativeEndian(
- PUCA_PropItemContextTreeNodeRec(PtrUInt(s) + d^.Left),
- PUCA_PropItemContextTreeNodeRec(PtrUInt(d) + d^.Left)
- );
- if (d^.Right > 0) then
- ReverseContextNodeToNativeEndian(
- PUCA_PropItemContextTreeNodeRec(PtrUInt(s) + d^.Right),
- PUCA_PropItemContextTreeNodeRec(PtrUInt(d) + d^.Right)
- );
- end;
- procedure ReverseContextToNativeEndian(s, d : PUCA_PropItemContextTreeRec);
- var
- k : PtrUInt;
- begin
- d^.Size := s^.Size;
- ReverseBytes(d^.Size,SizeOf(d^.Size));
- if (s^.Size = 0) then
- exit;
- k := SizeOf(s^.Size);
- ReverseContextNodeToNativeEndian(
- PUCA_PropItemContextTreeNodeRec(PtrUInt(s)+k),
- PUCA_PropItemContextTreeNodeRec(PtrUInt(d)+k)
- );
- end;
- procedure ReverseToNativeEndian(
- const AData : PUCA_PropItemRec;
- const ADataLen : Cardinal;
- const ADest : PUCA_PropItemRec
- );
- var
- s, d : PUCA_PropItemRec;
- sCtx, dCtx : PUCA_PropItemContextTreeRec;
- dataEnd : PtrUInt;
- k, i : PtrUInt;
- p_s, p_d : PByte;
- pw_s, pw_d : PUCA_PropWeights;
- begin
- dataEnd := PtrUInt(AData) + ADataLen;
- s := AData;
- d := ADest;
- while True do begin
- d^.WeightLength := s^.WeightLength;
- ReverseBytes(d^.WeightLength,SizeOf(d^.WeightLength));
- d^.ChildCount := s^.ChildCount;
- ReverseBytes(d^.ChildCount,SizeOf(d^.ChildCount));
- d^.Size := s^.Size;
- ReverseBytes(d^.Size,SizeOf(d^.Size));
- d^.Flags := s^.Flags;
- ReverseBytes(d^.Flags,SizeOf(d^.Flags));
- if d^.Contextual then begin
- k := SizeOf(TUCA_PropItemRec);
- if d^.HasCodePoint() then
- k := k + SizeOf(UInt24);
- sCtx := PUCA_PropItemContextTreeRec(PtrUInt(s) + k);
- dCtx := PUCA_PropItemContextTreeRec(PtrUInt(d) + k);
- ReverseContextToNativeEndian(sCtx,dCtx);
- end;
- if d^.HasCodePoint() then begin
- if d^.Contextual then
- k := d^.GetSelfOnlySize()- SizeOf(UInt24) - Cardinal(d^.GetContext()^.Size)
- else
- k := d^.GetSelfOnlySize() - SizeOf(UInt24);
- p_s := PByte(PtrUInt(s) + k);
- p_d := PByte(PtrUInt(d) + k);
- Unaligned(PUInt24(p_d)^) := Unaligned(PUInt24(p_s)^);
- ReverseBytes(p_d^,SizeOf(UInt24));
- end;
- if (d^.WeightLength > 0) then begin
- k := SizeOf(TUCA_PropItemRec);
- p_s := PByte(PtrUInt(s) + k);
- p_d := PByte(PtrUInt(d) + k);
- k := SizeOf(Word);
- Unaligned(PWord(p_d)^) := Unaligned(PWord(p_s)^);
- ReverseBytes(p_d^,k);
- p_s := PByte(PtrUInt(p_s) + k);
- p_d := PByte(PtrUInt(p_d) + k);
- if d^.IsWeightCompress_1() then begin
- k := SizeOf(Byte);
- PByte(p_d)^ := PByte(p_s)^;
- end else begin
- k := SizeOf(Word);
- Unaligned(PWord(p_d)^) := Unaligned(PWord(p_s)^);
- end;
- ReverseBytes(p_d^,k);
- p_s := PByte(PtrUInt(p_s) + k);
- p_d := PByte(PtrUInt(p_d) + k);
- if d^.IsWeightCompress_2() then begin
- k := SizeOf(Byte);
- PByte(p_d)^ := PByte(p_s)^;
- end else begin
- k := SizeOf(Word);
- Unaligned(PWord(p_d)^) := Unaligned(PWord(p_s)^);
- end;
- ReverseBytes(p_d^,k);
- if (d^.WeightLength > 1) then begin
- pw_s := PUCA_PropWeights(PtrUInt(p_s) + k);
- pw_d := PUCA_PropWeights(PtrUInt(p_d) + k);
- for i := 1 to d^.WeightLength - 1 do begin
- pw_d^.Weights[0] := pw_s^.Weights[0];
- pw_d^.Weights[1] := pw_s^.Weights[1];
- pw_d^.Weights[2] := pw_s^.Weights[2];
- ReverseArray(pw_d^,3,SizeOf(pw_s^.Weights[0]));
- Inc(pw_s);
- Inc(pw_d);
- end;
- end;
- end;
- k := d^.GetSelfOnlySize();
- s := PUCA_PropItemRec(PtrUInt(s)+k);
- d := PUCA_PropItemRec(PtrUInt(d)+k);
- if (PtrUInt(s) >= dataEnd) then
- Break;
- end;
- if ( (PtrUInt(s)-PtrUInt(AData)) <> (PtrUInt(d)-PtrUInt(ADest)) ) then
- raise Exception.CreateFmt('Read data length(%d) differs from written data length(%d).',[(PtrUInt(s)-PtrUInt(AData)), (PtrUInt(d)-PtrUInt(ADest))]);
- end;
- procedure Check(const ACondition : Boolean; const AMsg : string);overload;
- begin
- if not ACondition then
- raise Exception.Create(AMsg);
- end;
- procedure Check(
- const ACondition : Boolean;
- const AFormatMsg : string;
- const AArgs : array of const
- );overload;
- begin
- Check(ACondition,Format(AFormatMsg,AArgs));
- end;
- procedure Check(const ACondition : Boolean);overload;
- begin
- Check(ACondition,'Check failed.')
- end;
- procedure CompareWeights(a, b : PUCA_PropWeights; const ALength : Integer);
- var
- i : Integer;
- begin
- if (ALength > 0) then begin
- for i := 0 to ALength - 1 do begin
- Check(a[i].Weights[0]=b[i].Weights[0]);
- Check(a[i].Weights[1]=b[i].Weights[1]);
- Check(a[i].Weights[2]=b[i].Weights[2]);
- end;
- end;
- end;
- procedure CompareCodePoints(a, b : PUInt24; const ALength : Integer);
- var
- i : Integer;
- begin
- if (ALength > 0) then begin
- for i := 0 to ALength - 1 do
- Check(a[i]=b[i]);
- end;
- end;
- procedure CompareContextNode(AProp1, AProp2 : PUCA_PropItemContextTreeNodeRec);
- var
- a, b : PUCA_PropItemContextTreeNodeRec;
- k : Cardinal;
- begin
- if (AProp1=nil) then begin
- Check(AProp2=nil);
- exit;
- end;
- a := AProp1;
- b := AProp2;
- Check(a^.Left=b^.Left);
- Check(a^.Right=b^.Right);
- Check(a^.Data.CodePointCount=b^.Data.CodePointCount);
- Check(a^.Data.WeightCount=b^.Data.WeightCount);
- k := SizeOf(a^.Data);
- CompareCodePoints(
- PUInt24(PtrUInt(a)+k),
- PUInt24(PtrUInt(b)+k),
- a^.Data.CodePointCount
- );
- k := SizeOf(a^.Data)+ (a^.Data.CodePointCount*SizeOf(UInt24));
- CompareWeights(
- PUCA_PropWeights(PtrUInt(a)+k),
- PUCA_PropWeights(PtrUInt(b)+k),
- a^.Data.WeightCount
- );
- if (a^.Left > 0) then begin
- k := a^.Left;
- CompareContextNode(
- PUCA_PropItemContextTreeNodeRec(PtrUInt(a)+k),
- PUCA_PropItemContextTreeNodeRec(PtrUInt(b)+k)
- );
- end;
- if (a^.Right > 0) then begin
- k := a^.Right;
- CompareContextNode(
- PUCA_PropItemContextTreeNodeRec(PtrUInt(a)+k),
- PUCA_PropItemContextTreeNodeRec(PtrUInt(b)+k)
- );
- end;
- end;
- procedure CompareContext(AProp1, AProp2 : PUCA_PropItemContextTreeRec);
- var
- a, b : PUCA_PropItemContextTreeNodeRec;
- k : Integer;
- begin
- if (AProp1=nil) then begin
- Check(AProp2=nil);
- exit;
- end;
- Check(AProp1^.Size=AProp2^.Size);
- k := Cardinal(AProp1^.Size);
- a := PUCA_PropItemContextTreeNodeRec(PtrUInt(AProp1)+k);
- b := PUCA_PropItemContextTreeNodeRec(PtrUInt(AProp2)+k);
- CompareContextNode(a,b);
- end;
- procedure CompareProps(const AProp1, AProp2 : PUCA_PropItemRec; const ADataLen : Integer);
- var
- a, b, pend : PUCA_PropItemRec;
- wa, wb : array of TUCA_PropWeights;
- k : Integer;
- begin
- if (ADataLen <= 0) then
- exit;
- a := PUCA_PropItemRec(AProp1);
- b := PUCA_PropItemRec(AProp2);
- pend := PUCA_PropItemRec(PtrUInt(AProp1)+ADataLen);
- while (a<pend) do begin
- Check(a^.WeightLength=b^.WeightLength);
- Check(a^.ChildCount=b^.ChildCount);
- Check(a^.Size=b^.Size);
- Check(a^.Flags=b^.Flags);
- if a^.HasCodePoint() then
- Check(a^.CodePoint = b^.CodePoint);
- if (a^.WeightLength > 0) then begin
- k := a^.WeightLength;
- SetLength(wa,k);
- SetLength(wb,k);
- a^.GetWeightArray(@wa[0]);
- b^.GetWeightArray(@wb[0]);
- CompareWeights(@wa[0],@wb[0],k);
- end;
- if a^.Contextual then
- CompareContext(a^.GetContext(),b^.GetContext());
- Check(a^.GetSelfOnlySize()=b^.GetSelfOnlySize());
- k := a^.GetSelfOnlySize();
- a := PUCA_PropItemRec(PtrUInt(a)+k);
- b := PUCA_PropItemRec(PtrUInt(b)+k);
- end;
- end;
- Procedure QuickSort(AList : PCardinal; L, R : Longint);overload;
- var
- I, J : Longint;
- P, Q : Cardinal;
- begin
- repeat
- I := L;
- J := R;
- P := AList[ (L + R) div 2 ];
- repeat
- while (P > AList[i]) do
- I := I + 1;
- while (P < AList[J]) do
- J := J - 1;
- If I <= J then
- begin
- Q := AList[I];
- AList[I] := AList[J];
- AList[J] := Q;
- I := I + 1;
- J := J - 1;
- end;
- until I > J;
- if J - L < R - I then
- begin
- if L < J then
- QuickSort(AList, L, J);
- L := I;
- end
- else
- begin
- if I < R then
- QuickSort(AList, I, R);
- R := J;
- end;
- until L >= R;
- end;
- function CalcMaxLevel2Count(
- const ALevel1Value : Cardinal;
- ALines : array of TUCA_LineRec
- ) : Integer;
- var
- i, c, k : Integer;
- ac : Integer;
- items : array of Cardinal;
- p : PUCA_LineRec;
- pw : ^TUCA_WeightRec;
- begin
- c := Length(ALines);
- if (c < 1) then
- exit(0);
- SetLength(items,0);
- ac := 0;
- p := @ALines[Low(ALines)];
- for i := 0 to c-1 do begin
- if (Length(p^.Weights) > 0) then begin
- pw := @p^.Weights[Low(p^.Weights)];
- for k := 0 to Length(p^.Weights)-1 do begin
- if (pw^.Weights[0] = ALevel1Value) then begin
- if (ac = 0) or (IndexDWord(items[0],ac,pw^.Weights[1]) < 0) then begin
- if (ac >= Length(items)) then
- SetLength(items,Length(items)+256);
- items[ac] := pw^.Weights[1];
- ac := ac+1;
- end;
- end;
- Inc(pw);
- end;
- end;
- Inc(p);
- end;
- Result := ac;
- end;
- function RewriteLevel2(
- const ALevel1Value : Cardinal;
- ALines : PUCA_LineRec;
- const ALinesLength : Integer
- ) : Integer;
- var
- i, c, k : Integer;
- ac : Integer;
- items : array of Cardinal;
- p : PUCA_LineRec;
- pw : ^TUCA_WeightRec;
- newValue : Int64;
- begin
- c := ALinesLength;
- if (c < 1) then
- exit(0);
- SetLength(items,256);
- ac := 0;
- p := ALines;
- for i := 0 to c-1 do begin
- if (Length(p^.Weights) > 0) then begin
- for k := 0 to Length(p^.Weights)-1 do begin
- pw := @p^.Weights[k];
- if (pw^.Weights[0] = ALevel1Value) then begin
- if (ac = 0) or (IndexDWord(items[0],ac,pw^.Weights[1]) < 0) then begin
- if (ac >= Length(items)) then
- SetLength(items,Length(items)+256);
- items[ac] := pw^.Weights[1];
- ac := ac+1;
- end;
- end;
- end;
- end;
- Inc(p);
- end;
- SetLength(items,ac);
- if (ac > 1) then
- QuickSort(@items[0],0,(ac-1));
- p := ALines;
- for i := 0 to c-1 do begin
- if (Length(p^.Weights) > 0) then begin
- for k := 0 to Length(p^.Weights)-1 do begin
- pw := @p^.Weights[k];
- if (pw^.Weights[0] = ALevel1Value) then begin
- newValue := IndexDWord(items[0],ac,pw^.Weights[1]);
- if (newValue < 0) then
- raise Exception.CreateFmt('level 2 value %d missed in rewrite of level 1 value of %d.',[pw^.Weights[1],ALevel1Value]);
- pw^.Weights[1] := newValue;//+1;
- end;
- end;
- end;
- Inc(p);
- end;
- if (Length(items) > 0) then
- Result := items[Length(items)-1]
- else
- Result := 0;
- end;
- procedure RewriteLevel2Values(ALines : PUCA_LineRec; ALength : Integer);
- var
- c, i, ac, k : Integer;
- p : PUCA_LineRec;
- level1List : array of Cardinal;
- pw : ^TUCA_WeightRec;
- begin
- c := ALength;
- if (c < 1) then
- exit;
- ac := 0;
- SetLength(level1List,c);
- p := ALines;
- for i := 0 to c-1 do begin
- if (Length(p^.Weights) > 0) then begin
- for k := 0 to Length(p^.Weights)-1 do begin
- pw := @p^.Weights[k];
- if (ac = 0) or (IndexDWord(level1List[0],ac,pw^.Weights[0]) < 0) then begin
- if (ac >= Length(level1List)) then
- SetLength(level1List,ac+1000);
- level1List[ac] := pw^.Weights[0];
- RewriteLevel2(level1List[ac],ALines,ALength);
- ac := ac+1;
- end;
- end;
- end;
- Inc(p);
- end;
- end;
- function CalcMaxLevel2Value(ALines : array of TUCA_LineRec) : Cardinal;
- var
- i, c, k, tempValue : Integer;
- p : PUCA_LineRec;
- maxLevel : Cardinal;
- maxValue : Integer;
- begin
- c := Length(ALines);
- if (c < 2) then
- exit(0);
- maxLevel := 0;
- maxValue := CalcMaxLevel2Count(maxLevel,ALines);
- p := @ALines[Low(ALines)+1];
- for i := 1 to c-1 do begin
- if (Length(p^.Weights) > 0) then begin
- for k := 0 to Length(p^.Weights)-1 do begin
- if (p^.Weights[k].Weights[0] <> maxLevel) then begin
- tempValue := CalcMaxLevel2Count(p^.Weights[k].Weights[0],ALines);
- if (tempValue > maxValue) then begin
- maxLevel := p^.Weights[k].Weights[0];
- maxValue := tempValue;
- end;
- end;
- end;
- end;
- Inc(p);
- end;
- Result := maxValue;
- end;
- initialization
- FS := DefaultFormatSettings;
- FS.DecimalSeparator := '.';
- end.
|