format.h 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. /*
  3. Formatting library for C++
  4. Copyright (c) 2012 - present, Victor Zverovich
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. --- Optional exception to the license ---
  22. As an exception, if, as a result of your compiling your source code, portions
  23. of this Software are embedded into a machine-executable object form of such
  24. source code, you may redistribute such embedded portions in such object form
  25. without including the above copyright and permission notices.
  26. */
  27. #ifndef FMT_FORMAT_H_
  28. #define FMT_FORMAT_H_
  29. #ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
  30. # define _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
  31. # define FMT_REMOVE_TRANSITIVE_INCLUDES
  32. #endif
  33. #include "base.h"
  34. // libc++ supports string_view in pre-c++17.
  35. #if FMT_HAS_INCLUDE(<string_view>) && \
  36. (FMT_CPLUSPLUS >= 201703L || defined(_LIBCPP_VERSION))
  37. # define FMT_USE_STRING_VIEW
  38. #endif
  39. #ifndef FMT_MODULE
  40. # include <stdlib.h> // malloc, free
  41. # include <cmath> // std::signbit
  42. # include <cstddef> // std::byte
  43. # include <cstdint> // uint32_t
  44. # include <cstring> // std::memcpy
  45. # include <limits> // std::numeric_limits
  46. # include <new> // std::bad_alloc
  47. # if defined(__GLIBCXX__) && !defined(_GLIBCXX_USE_DUAL_ABI)
  48. // Workaround for pre gcc 5 libstdc++.
  49. # include <memory> // std::allocator_traits
  50. # endif
  51. # include <stdexcept> // std::runtime_error
  52. # include <string> // std::string
  53. # include <system_error> // std::system_error
  54. // Check FMT_CPLUSPLUS to avoid a warning in MSVC.
  55. # if FMT_HAS_INCLUDE(<bit>) && FMT_CPLUSPLUS > 201703L
  56. # include <bit> // std::bit_cast
  57. # endif
  58. # if defined(FMT_USE_STRING_VIEW)
  59. # include <string_view>
  60. # endif
  61. # if FMT_MSC_VERSION
  62. # include <intrin.h> // _BitScanReverse[64], _umul128
  63. # endif
  64. #endif // FMT_MODULE
  65. #if defined(FMT_USE_NONTYPE_TEMPLATE_ARGS)
  66. // Use the provided definition.
  67. #elif defined(__NVCOMPILER)
  68. # define FMT_USE_NONTYPE_TEMPLATE_ARGS 0
  69. #elif FMT_GCC_VERSION >= 903 && FMT_CPLUSPLUS >= 201709L
  70. # define FMT_USE_NONTYPE_TEMPLATE_ARGS 1
  71. #elif defined(__cpp_nontype_template_args) && \
  72. __cpp_nontype_template_args >= 201911L
  73. # define FMT_USE_NONTYPE_TEMPLATE_ARGS 1
  74. #elif FMT_CLANG_VERSION >= 1200 && FMT_CPLUSPLUS >= 202002L
  75. # define FMT_USE_NONTYPE_TEMPLATE_ARGS 1
  76. #else
  77. # define FMT_USE_NONTYPE_TEMPLATE_ARGS 0
  78. #endif
  79. #if defined __cpp_inline_variables && __cpp_inline_variables >= 201606L
  80. # define FMT_INLINE_VARIABLE inline
  81. #else
  82. # define FMT_INLINE_VARIABLE
  83. #endif
  84. // Check if RTTI is disabled.
  85. #ifdef FMT_USE_RTTI
  86. // Use the provided definition.
  87. #elif defined(__GXX_RTTI) || FMT_HAS_FEATURE(cxx_rtti) || defined(_CPPRTTI) || \
  88. defined(__INTEL_RTTI__) || defined(__RTTI)
  89. // __RTTI is for EDG compilers. _CPPRTTI is for MSVC.
  90. # define FMT_USE_RTTI 1
  91. #else
  92. # define FMT_USE_RTTI 0
  93. #endif
  94. // Visibility when compiled as a shared library/object.
  95. #if defined(FMT_LIB_EXPORT) || defined(FMT_SHARED)
  96. # define FMT_SO_VISIBILITY(value) FMT_VISIBILITY(value)
  97. #else
  98. # define FMT_SO_VISIBILITY(value)
  99. #endif
  100. #if FMT_GCC_VERSION || FMT_CLANG_VERSION
  101. # define FMT_NOINLINE __attribute__((noinline))
  102. #else
  103. # define FMT_NOINLINE
  104. #endif
  105. #ifdef FMT_DEPRECATED
  106. // Use the provided definition.
  107. #elif FMT_HAS_CPP14_ATTRIBUTE(deprecated)
  108. # define FMT_DEPRECATED [[deprecated]]
  109. #else
  110. # define FMT_DEPRECATED /* deprecated */
  111. #endif
  112. // Detect constexpr std::string.
  113. #if !FMT_USE_CONSTEVAL
  114. # define FMT_USE_CONSTEXPR_STRING 0
  115. #elif defined(__cpp_lib_constexpr_string) && \
  116. __cpp_lib_constexpr_string >= 201907L
  117. # if FMT_CLANG_VERSION && FMT_GLIBCXX_RELEASE
  118. // clang + libstdc++ are able to work only starting with gcc13.3
  119. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113294
  120. # if FMT_GLIBCXX_RELEASE < 13
  121. # define FMT_USE_CONSTEXPR_STRING 0
  122. # elif FMT_GLIBCXX_RELEASE == 13 && __GLIBCXX__ < 20240521
  123. # define FMT_USE_CONSTEXPR_STRING 0
  124. # else
  125. # define FMT_USE_CONSTEXPR_STRING 1
  126. # endif
  127. # else
  128. # define FMT_USE_CONSTEXPR_STRING 1
  129. # endif
  130. #else
  131. # define FMT_USE_CONSTEXPR_STRING 0
  132. #endif
  133. #if FMT_USE_CONSTEXPR_STRING
  134. # define FMT_CONSTEXPR_STRING constexpr
  135. #else
  136. # define FMT_CONSTEXPR_STRING
  137. #endif
  138. // GCC 4.9 doesn't support qualified names in specializations.
  139. namespace std {
  140. template <typename T> struct iterator_traits<fmt::basic_appender<T>> {
  141. using iterator_category = output_iterator_tag;
  142. using value_type = T;
  143. using difference_type =
  144. decltype(static_cast<int*>(nullptr) - static_cast<int*>(nullptr));
  145. using pointer = void;
  146. using reference = void;
  147. };
  148. } // namespace std
  149. #ifdef FMT_THROW
  150. // Use the provided definition.
  151. #elif FMT_USE_EXCEPTIONS
  152. # define FMT_THROW(x) throw x
  153. #else
  154. # define FMT_THROW(x) ::fmt::assert_fail(__FILE__, __LINE__, (x).what())
  155. #endif
  156. #ifdef __clang_analyzer__
  157. # define FMT_CLANG_ANALYZER 1
  158. #else
  159. # define FMT_CLANG_ANALYZER 0
  160. #endif
  161. // Defining FMT_REDUCE_INT_INSTANTIATIONS to 1, will reduce the number of
  162. // integer formatter template instantiations to just one by only using the
  163. // largest integer type. This results in a reduction in binary size but will
  164. // cause a decrease in integer formatting performance.
  165. #if !defined(FMT_REDUCE_INT_INSTANTIATIONS)
  166. # define FMT_REDUCE_INT_INSTANTIATIONS 0
  167. #endif
  168. FMT_BEGIN_NAMESPACE
  169. template <typename Char, typename Traits, typename Allocator>
  170. struct is_contiguous<std::basic_string<Char, Traits, Allocator>>
  171. : std::true_type {};
  172. namespace detail {
  173. // __builtin_clz is broken in clang with Microsoft codegen:
  174. // https://github.com/fmtlib/fmt/issues/519.
  175. #if !FMT_MSC_VERSION
  176. # if FMT_HAS_BUILTIN(__builtin_clz) || FMT_GCC_VERSION || FMT_ICC_VERSION
  177. # define FMT_BUILTIN_CLZ(n) __builtin_clz(n)
  178. # endif
  179. # if FMT_HAS_BUILTIN(__builtin_clzll) || FMT_GCC_VERSION || FMT_ICC_VERSION
  180. # define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n)
  181. # endif
  182. #endif
  183. // Some compilers masquerade as both MSVC and GCC but otherwise support
  184. // __builtin_clz and __builtin_clzll, so only define FMT_BUILTIN_CLZ using the
  185. // MSVC intrinsics if the clz and clzll builtins are not available.
  186. #if FMT_MSC_VERSION && !defined(FMT_BUILTIN_CLZLL)
  187. // Avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning.
  188. # ifndef __clang__
  189. # pragma intrinsic(_BitScanReverse)
  190. # ifdef _WIN64
  191. # pragma intrinsic(_BitScanReverse64)
  192. # endif
  193. # endif
  194. inline auto clz(uint32_t x) -> int {
  195. FMT_ASSERT(x != 0, "");
  196. FMT_MSC_WARNING(suppress : 6102) // Suppress a bogus static analysis warning.
  197. unsigned long r = 0;
  198. _BitScanReverse(&r, x);
  199. return 31 ^ static_cast<int>(r);
  200. }
  201. # define FMT_BUILTIN_CLZ(n) detail::clz(n)
  202. inline auto clzll(uint64_t x) -> int {
  203. FMT_ASSERT(x != 0, "");
  204. FMT_MSC_WARNING(suppress : 6102) // Suppress a bogus static analysis warning.
  205. unsigned long r = 0;
  206. # ifdef _WIN64
  207. _BitScanReverse64(&r, x);
  208. # else
  209. // Scan the high 32 bits.
  210. if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32)))
  211. return 63 ^ static_cast<int>(r + 32);
  212. // Scan the low 32 bits.
  213. _BitScanReverse(&r, static_cast<uint32_t>(x));
  214. # endif
  215. return 63 ^ static_cast<int>(r);
  216. }
  217. # define FMT_BUILTIN_CLZLL(n) detail::clzll(n)
  218. #endif // FMT_MSC_VERSION && !defined(FMT_BUILTIN_CLZLL)
  219. FMT_CONSTEXPR inline void abort_fuzzing_if(bool condition) {
  220. ignore_unused(condition);
  221. #ifdef FMT_FUZZ
  222. if (condition) throw std::runtime_error("fuzzing limit reached");
  223. #endif
  224. }
  225. #if defined(FMT_USE_STRING_VIEW)
  226. template <typename Char> using std_string_view = std::basic_string_view<Char>;
  227. #else
  228. template <typename Char> struct std_string_view {
  229. operator basic_string_view<Char>() const;
  230. };
  231. #endif
  232. template <typename Char, Char... C> struct string_literal {
  233. static constexpr Char value[sizeof...(C)] = {C...};
  234. constexpr operator basic_string_view<Char>() const {
  235. return {value, sizeof...(C)};
  236. }
  237. };
  238. #if FMT_CPLUSPLUS < 201703L
  239. template <typename Char, Char... C>
  240. constexpr Char string_literal<Char, C...>::value[sizeof...(C)];
  241. #endif
  242. // Implementation of std::bit_cast for pre-C++20.
  243. template <typename To, typename From, FMT_ENABLE_IF(sizeof(To) == sizeof(From))>
  244. FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To {
  245. #ifdef __cpp_lib_bit_cast
  246. if (is_constant_evaluated()) return std::bit_cast<To>(from);
  247. #endif
  248. auto to = To();
  249. // The cast suppresses a bogus -Wclass-memaccess on GCC.
  250. std::memcpy(static_cast<void*>(&to), &from, sizeof(to));
  251. return to;
  252. }
  253. inline auto is_big_endian() -> bool {
  254. #ifdef _WIN32
  255. return false;
  256. #elif defined(__BIG_ENDIAN__)
  257. return true;
  258. #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
  259. return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
  260. #else
  261. struct bytes {
  262. char data[sizeof(int)];
  263. };
  264. return bit_cast<bytes>(1).data[0] == 0;
  265. #endif
  266. }
  267. class uint128_fallback {
  268. private:
  269. uint64_t lo_, hi_;
  270. public:
  271. constexpr uint128_fallback(uint64_t hi, uint64_t lo) : lo_(lo), hi_(hi) {}
  272. constexpr uint128_fallback(uint64_t value = 0) : lo_(value), hi_(0) {}
  273. constexpr auto high() const noexcept -> uint64_t { return hi_; }
  274. constexpr auto low() const noexcept -> uint64_t { return lo_; }
  275. template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
  276. constexpr explicit operator T() const {
  277. return static_cast<T>(lo_);
  278. }
  279. friend constexpr auto operator==(const uint128_fallback& lhs,
  280. const uint128_fallback& rhs) -> bool {
  281. return lhs.hi_ == rhs.hi_ && lhs.lo_ == rhs.lo_;
  282. }
  283. friend constexpr auto operator!=(const uint128_fallback& lhs,
  284. const uint128_fallback& rhs) -> bool {
  285. return !(lhs == rhs);
  286. }
  287. friend constexpr auto operator>(const uint128_fallback& lhs,
  288. const uint128_fallback& rhs) -> bool {
  289. return lhs.hi_ != rhs.hi_ ? lhs.hi_ > rhs.hi_ : lhs.lo_ > rhs.lo_;
  290. }
  291. friend constexpr auto operator|(const uint128_fallback& lhs,
  292. const uint128_fallback& rhs)
  293. -> uint128_fallback {
  294. return {lhs.hi_ | rhs.hi_, lhs.lo_ | rhs.lo_};
  295. }
  296. friend constexpr auto operator&(const uint128_fallback& lhs,
  297. const uint128_fallback& rhs)
  298. -> uint128_fallback {
  299. return {lhs.hi_ & rhs.hi_, lhs.lo_ & rhs.lo_};
  300. }
  301. friend constexpr auto operator~(const uint128_fallback& n)
  302. -> uint128_fallback {
  303. return {~n.hi_, ~n.lo_};
  304. }
  305. friend FMT_CONSTEXPR auto operator+(const uint128_fallback& lhs,
  306. const uint128_fallback& rhs)
  307. -> uint128_fallback {
  308. auto result = uint128_fallback(lhs);
  309. result += rhs;
  310. return result;
  311. }
  312. friend FMT_CONSTEXPR auto operator*(const uint128_fallback& lhs, uint32_t rhs)
  313. -> uint128_fallback {
  314. FMT_ASSERT(lhs.hi_ == 0, "");
  315. uint64_t hi = (lhs.lo_ >> 32) * rhs;
  316. uint64_t lo = (lhs.lo_ & ~uint32_t()) * rhs;
  317. uint64_t new_lo = (hi << 32) + lo;
  318. return {(hi >> 32) + (new_lo < lo ? 1 : 0), new_lo};
  319. }
  320. friend constexpr auto operator-(const uint128_fallback& lhs, uint64_t rhs)
  321. -> uint128_fallback {
  322. return {lhs.hi_ - (lhs.lo_ < rhs ? 1 : 0), lhs.lo_ - rhs};
  323. }
  324. FMT_CONSTEXPR auto operator>>(int shift) const -> uint128_fallback {
  325. if (shift == 64) return {0, hi_};
  326. if (shift > 64) return uint128_fallback(0, hi_) >> (shift - 64);
  327. return {hi_ >> shift, (hi_ << (64 - shift)) | (lo_ >> shift)};
  328. }
  329. FMT_CONSTEXPR auto operator<<(int shift) const -> uint128_fallback {
  330. if (shift == 64) return {lo_, 0};
  331. if (shift > 64) return uint128_fallback(lo_, 0) << (shift - 64);
  332. return {hi_ << shift | (lo_ >> (64 - shift)), (lo_ << shift)};
  333. }
  334. FMT_CONSTEXPR auto operator>>=(int shift) -> uint128_fallback& {
  335. return *this = *this >> shift;
  336. }
  337. FMT_CONSTEXPR void operator+=(uint128_fallback n) {
  338. uint64_t new_lo = lo_ + n.lo_;
  339. uint64_t new_hi = hi_ + n.hi_ + (new_lo < lo_ ? 1 : 0);
  340. FMT_ASSERT(new_hi >= hi_, "");
  341. lo_ = new_lo;
  342. hi_ = new_hi;
  343. }
  344. FMT_CONSTEXPR void operator&=(uint128_fallback n) {
  345. lo_ &= n.lo_;
  346. hi_ &= n.hi_;
  347. }
  348. FMT_CONSTEXPR20 auto operator+=(uint64_t n) noexcept -> uint128_fallback& {
  349. if (is_constant_evaluated()) {
  350. lo_ += n;
  351. hi_ += (lo_ < n ? 1 : 0);
  352. return *this;
  353. }
  354. #if FMT_HAS_BUILTIN(__builtin_addcll) && !defined(__ibmxl__)
  355. unsigned long long carry;
  356. lo_ = __builtin_addcll(lo_, n, 0, &carry);
  357. hi_ += carry;
  358. #elif FMT_HAS_BUILTIN(__builtin_ia32_addcarryx_u64) && !defined(__ibmxl__)
  359. unsigned long long result;
  360. auto carry = __builtin_ia32_addcarryx_u64(0, lo_, n, &result);
  361. lo_ = result;
  362. hi_ += carry;
  363. #elif defined(_MSC_VER) && defined(_M_X64)
  364. auto carry = _addcarry_u64(0, lo_, n, &lo_);
  365. _addcarry_u64(carry, hi_, 0, &hi_);
  366. #else
  367. lo_ += n;
  368. hi_ += (lo_ < n ? 1 : 0);
  369. #endif
  370. return *this;
  371. }
  372. };
  373. using uint128_t = conditional_t<FMT_USE_INT128, uint128_opt, uint128_fallback>;
  374. #ifdef UINTPTR_MAX
  375. using uintptr_t = ::uintptr_t;
  376. #else
  377. using uintptr_t = uint128_t;
  378. #endif
  379. // Returns the largest possible value for type T. Same as
  380. // std::numeric_limits<T>::max() but shorter and not affected by the max macro.
  381. template <typename T> constexpr auto max_value() -> T {
  382. return (std::numeric_limits<T>::max)();
  383. }
  384. template <typename T> constexpr auto num_bits() -> int {
  385. return std::numeric_limits<T>::digits;
  386. }
  387. // std::numeric_limits<T>::digits may return 0 for 128-bit ints.
  388. template <> constexpr auto num_bits<int128_opt>() -> int { return 128; }
  389. template <> constexpr auto num_bits<uint128_opt>() -> int { return 128; }
  390. template <> constexpr auto num_bits<uint128_fallback>() -> int { return 128; }
  391. // A heterogeneous bit_cast used for converting 96-bit long double to uint128_t
  392. // and 128-bit pointers to uint128_fallback.
  393. template <typename To, typename From, FMT_ENABLE_IF(sizeof(To) > sizeof(From))>
  394. inline auto bit_cast(const From& from) -> To {
  395. constexpr auto size = static_cast<int>(sizeof(From) / sizeof(unsigned short));
  396. struct data_t {
  397. unsigned short value[static_cast<unsigned>(size)];
  398. } data = bit_cast<data_t>(from);
  399. auto result = To();
  400. if (const_check(is_big_endian())) {
  401. for (int i = 0; i < size; ++i)
  402. result = (result << num_bits<unsigned short>()) | data.value[i];
  403. } else {
  404. for (int i = size - 1; i >= 0; --i)
  405. result = (result << num_bits<unsigned short>()) | data.value[i];
  406. }
  407. return result;
  408. }
  409. template <typename UInt>
  410. FMT_CONSTEXPR20 inline auto countl_zero_fallback(UInt n) -> int {
  411. int lz = 0;
  412. constexpr UInt msb_mask = static_cast<UInt>(1) << (num_bits<UInt>() - 1);
  413. for (; (n & msb_mask) == 0; n <<= 1) lz++;
  414. return lz;
  415. }
  416. FMT_CONSTEXPR20 inline auto countl_zero(uint32_t n) -> int {
  417. #ifdef FMT_BUILTIN_CLZ
  418. if (!is_constant_evaluated()) return FMT_BUILTIN_CLZ(n);
  419. #endif
  420. return countl_zero_fallback(n);
  421. }
  422. FMT_CONSTEXPR20 inline auto countl_zero(uint64_t n) -> int {
  423. #ifdef FMT_BUILTIN_CLZLL
  424. if (!is_constant_evaluated()) return FMT_BUILTIN_CLZLL(n);
  425. #endif
  426. return countl_zero_fallback(n);
  427. }
  428. FMT_INLINE void assume(bool condition) {
  429. (void)condition;
  430. #if FMT_HAS_BUILTIN(__builtin_assume) && !FMT_ICC_VERSION
  431. __builtin_assume(condition);
  432. #elif FMT_GCC_VERSION
  433. if (!condition) __builtin_unreachable();
  434. #endif
  435. }
  436. // Attempts to reserve space for n extra characters in the output range.
  437. // Returns a pointer to the reserved range or a reference to it.
  438. template <typename OutputIt,
  439. FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value&&
  440. is_contiguous<typename OutputIt::container>::value)>
  441. #if FMT_CLANG_VERSION >= 307 && !FMT_ICC_VERSION
  442. __attribute__((no_sanitize("undefined")))
  443. #endif
  444. FMT_CONSTEXPR20 inline auto
  445. reserve(OutputIt it, size_t n) -> typename OutputIt::value_type* {
  446. auto& c = get_container(it);
  447. size_t size = c.size();
  448. c.resize(size + n);
  449. return &c[size];
  450. }
  451. template <typename T>
  452. FMT_CONSTEXPR20 inline auto reserve(basic_appender<T> it, size_t n)
  453. -> basic_appender<T> {
  454. buffer<T>& buf = get_container(it);
  455. buf.try_reserve(buf.size() + n);
  456. return it;
  457. }
  458. template <typename Iterator>
  459. constexpr auto reserve(Iterator& it, size_t) -> Iterator& {
  460. return it;
  461. }
  462. template <typename OutputIt>
  463. using reserve_iterator =
  464. remove_reference_t<decltype(reserve(std::declval<OutputIt&>(), 0))>;
  465. template <typename T, typename OutputIt>
  466. constexpr auto to_pointer(OutputIt, size_t) -> T* {
  467. return nullptr;
  468. }
  469. template <typename T> FMT_CONSTEXPR auto to_pointer(T*& ptr, size_t n) -> T* {
  470. T* begin = ptr;
  471. ptr += n;
  472. return begin;
  473. }
  474. template <typename T>
  475. FMT_CONSTEXPR20 auto to_pointer(basic_appender<T> it, size_t n) -> T* {
  476. buffer<T>& buf = get_container(it);
  477. buf.try_reserve(buf.size() + n);
  478. auto size = buf.size();
  479. if (buf.capacity() < size + n) return nullptr;
  480. buf.try_resize(size + n);
  481. return buf.data() + size;
  482. }
  483. template <typename OutputIt,
  484. FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value&&
  485. is_contiguous<typename OutputIt::container>::value)>
  486. inline auto base_iterator(OutputIt it,
  487. typename OutputIt::container_type::value_type*)
  488. -> OutputIt {
  489. return it;
  490. }
  491. template <typename Iterator>
  492. constexpr auto base_iterator(Iterator, Iterator it) -> Iterator {
  493. return it;
  494. }
  495. // <algorithm> is spectacularly slow to compile in C++20 so use a simple fill_n
  496. // instead (#1998).
  497. template <typename OutputIt, typename Size, typename T>
  498. FMT_CONSTEXPR auto fill_n(OutputIt out, Size count, const T& value)
  499. -> OutputIt {
  500. for (Size i = 0; i < count; ++i) *out++ = value;
  501. return out;
  502. }
  503. template <typename T, typename Size>
  504. FMT_CONSTEXPR20 auto fill_n(T* out, Size count, char value) -> T* {
  505. if (is_constant_evaluated()) return fill_n<T*, Size, T>(out, count, value);
  506. static_assert(sizeof(T) == 1,
  507. "sizeof(T) must be 1 to use char for initialization");
  508. std::memset(out, value, to_unsigned(count));
  509. return out + count;
  510. }
  511. template <typename OutChar, typename InputIt, typename OutputIt>
  512. FMT_CONSTEXPR FMT_NOINLINE auto copy_noinline(InputIt begin, InputIt end,
  513. OutputIt out) -> OutputIt {
  514. return copy<OutChar>(begin, end, out);
  515. }
  516. // A public domain branchless UTF-8 decoder by Christopher Wellons:
  517. // https://github.com/skeeto/branchless-utf8
  518. /* Decode the next character, c, from s, reporting errors in e.
  519. *
  520. * Since this is a branchless decoder, four bytes will be read from the
  521. * buffer regardless of the actual length of the next character. This
  522. * means the buffer _must_ have at least three bytes of zero padding
  523. * following the end of the data stream.
  524. *
  525. * Errors are reported in e, which will be non-zero if the parsed
  526. * character was somehow invalid: invalid byte sequence, non-canonical
  527. * encoding, or a surrogate half.
  528. *
  529. * The function returns a pointer to the next character. When an error
  530. * occurs, this pointer will be a guess that depends on the particular
  531. * error, but it will always advance at least one byte.
  532. */
  533. FMT_CONSTEXPR inline auto utf8_decode(const char* s, uint32_t* c, int* e)
  534. -> const char* {
  535. constexpr int masks[] = {0x00, 0x7f, 0x1f, 0x0f, 0x07};
  536. constexpr uint32_t mins[] = {4194304, 0, 128, 2048, 65536};
  537. constexpr int shiftc[] = {0, 18, 12, 6, 0};
  538. constexpr int shifte[] = {0, 6, 4, 2, 0};
  539. int len = "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0\0\0\2\2\2\2\3\3\4"
  540. [static_cast<unsigned char>(*s) >> 3];
  541. // Compute the pointer to the next character early so that the next
  542. // iteration can start working on the next character. Neither Clang
  543. // nor GCC figure out this reordering on their own.
  544. const char* next = s + len + !len;
  545. using uchar = unsigned char;
  546. // Assume a four-byte character and load four bytes. Unused bits are
  547. // shifted out.
  548. *c = uint32_t(uchar(s[0]) & masks[len]) << 18;
  549. *c |= uint32_t(uchar(s[1]) & 0x3f) << 12;
  550. *c |= uint32_t(uchar(s[2]) & 0x3f) << 6;
  551. *c |= uint32_t(uchar(s[3]) & 0x3f) << 0;
  552. *c >>= shiftc[len];
  553. // Accumulate the various error conditions.
  554. *e = (*c < mins[len]) << 6; // non-canonical encoding
  555. *e |= ((*c >> 11) == 0x1b) << 7; // surrogate half?
  556. *e |= (*c > 0x10FFFF) << 8; // out of range?
  557. *e |= (uchar(s[1]) & 0xc0) >> 2;
  558. *e |= (uchar(s[2]) & 0xc0) >> 4;
  559. *e |= uchar(s[3]) >> 6;
  560. *e ^= 0x2a; // top two bits of each tail byte correct?
  561. *e >>= shifte[len];
  562. return next;
  563. }
  564. constexpr FMT_INLINE_VARIABLE uint32_t invalid_code_point = ~uint32_t();
  565. // Invokes f(cp, sv) for every code point cp in s with sv being the string view
  566. // corresponding to the code point. cp is invalid_code_point on error.
  567. template <typename F>
  568. FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) {
  569. auto decode = [f](const char* buf_ptr, const char* ptr) {
  570. auto cp = uint32_t();
  571. auto error = 0;
  572. auto end = utf8_decode(buf_ptr, &cp, &error);
  573. bool result = f(error ? invalid_code_point : cp,
  574. string_view(ptr, error ? 1 : to_unsigned(end - buf_ptr)));
  575. return result ? (error ? buf_ptr + 1 : end) : nullptr;
  576. };
  577. auto p = s.data();
  578. const size_t block_size = 4; // utf8_decode always reads blocks of 4 chars.
  579. if (s.size() >= block_size) {
  580. for (auto end = p + s.size() - block_size + 1; p < end;) {
  581. p = decode(p, p);
  582. if (!p) return;
  583. }
  584. }
  585. auto num_chars_left = to_unsigned(s.data() + s.size() - p);
  586. if (num_chars_left == 0) return;
  587. // Suppress bogus -Wstringop-overflow.
  588. if (FMT_GCC_VERSION) num_chars_left &= 3;
  589. char buf[2 * block_size - 1] = {};
  590. copy<char>(p, p + num_chars_left, buf);
  591. const char* buf_ptr = buf;
  592. do {
  593. auto end = decode(buf_ptr, p);
  594. if (!end) return;
  595. p += end - buf_ptr;
  596. buf_ptr = end;
  597. } while (buf_ptr < buf + num_chars_left);
  598. }
  599. FMT_CONSTEXPR inline auto display_width_of(uint32_t cp) noexcept -> size_t {
  600. return to_unsigned(
  601. 1 + (cp >= 0x1100 &&
  602. (cp <= 0x115f || // Hangul Jamo init. consonants
  603. cp == 0x2329 || // LEFT-POINTING ANGLE BRACKET
  604. cp == 0x232a || // RIGHT-POINTING ANGLE BRACKET
  605. // CJK ... Yi except IDEOGRAPHIC HALF FILL SPACE:
  606. (cp >= 0x2e80 && cp <= 0xa4cf && cp != 0x303f) ||
  607. (cp >= 0xac00 && cp <= 0xd7a3) || // Hangul Syllables
  608. (cp >= 0xf900 && cp <= 0xfaff) || // CJK Compatibility Ideographs
  609. (cp >= 0xfe10 && cp <= 0xfe19) || // Vertical Forms
  610. (cp >= 0xfe30 && cp <= 0xfe6f) || // CJK Compatibility Forms
  611. (cp >= 0xff00 && cp <= 0xff60) || // Fullwidth Forms
  612. (cp >= 0xffe0 && cp <= 0xffe6) || // Fullwidth Forms
  613. (cp >= 0x20000 && cp <= 0x2fffd) || // CJK
  614. (cp >= 0x30000 && cp <= 0x3fffd) ||
  615. // Miscellaneous Symbols and Pictographs + Emoticons:
  616. (cp >= 0x1f300 && cp <= 0x1f64f) ||
  617. // Supplemental Symbols and Pictographs:
  618. (cp >= 0x1f900 && cp <= 0x1f9ff))));
  619. }
  620. template <typename T> struct is_integral : std::is_integral<T> {};
  621. template <> struct is_integral<int128_opt> : std::true_type {};
  622. template <> struct is_integral<uint128_t> : std::true_type {};
  623. template <typename T>
  624. using is_signed =
  625. std::integral_constant<bool, std::numeric_limits<T>::is_signed ||
  626. std::is_same<T, int128_opt>::value>;
  627. template <typename T>
  628. using is_integer =
  629. bool_constant<is_integral<T>::value && !std::is_same<T, bool>::value &&
  630. !std::is_same<T, char>::value &&
  631. !std::is_same<T, wchar_t>::value>;
  632. #if defined(FMT_USE_FLOAT128)
  633. // Use the provided definition.
  634. #elif FMT_CLANG_VERSION >= 309 && FMT_HAS_INCLUDE(<quadmath.h>)
  635. # define FMT_USE_FLOAT128 1
  636. #elif FMT_GCC_VERSION && defined(_GLIBCXX_USE_FLOAT128) && \
  637. !defined(__STRICT_ANSI__)
  638. # define FMT_USE_FLOAT128 1
  639. #else
  640. # define FMT_USE_FLOAT128 0
  641. #endif
  642. #if FMT_USE_FLOAT128
  643. using float128 = __float128;
  644. #else
  645. struct float128 {};
  646. #endif
  647. template <typename T> using is_float128 = std::is_same<T, float128>;
  648. template <typename T> struct is_floating_point : std::is_floating_point<T> {};
  649. template <> struct is_floating_point<float128> : std::true_type {};
  650. template <typename T, bool = is_floating_point<T>::value>
  651. struct is_fast_float : bool_constant<std::numeric_limits<T>::is_iec559 &&
  652. sizeof(T) <= sizeof(double)> {};
  653. template <typename T> struct is_fast_float<T, false> : std::false_type {};
  654. template <typename T>
  655. using fast_float_t = conditional_t<sizeof(T) == sizeof(double), double, float>;
  656. template <typename T>
  657. using is_double_double = bool_constant<std::numeric_limits<T>::digits == 106>;
  658. #ifndef FMT_USE_FULL_CACHE_DRAGONBOX
  659. # define FMT_USE_FULL_CACHE_DRAGONBOX 0
  660. #endif
  661. // An allocator that uses malloc/free to allow removing dependency on the C++
  662. // standard libary runtime. std::decay is used for back_inserter to be found by
  663. // ADL when applied to memory_buffer.
  664. template <typename T> struct allocator : private std::decay<void> {
  665. using value_type = T;
  666. auto allocate(size_t n) -> T* {
  667. FMT_ASSERT(n <= max_value<size_t>() / sizeof(T), "");
  668. T* p = static_cast<T*>(malloc(n * sizeof(T)));
  669. if (!p) FMT_THROW(std::bad_alloc());
  670. return p;
  671. }
  672. void deallocate(T* p, size_t) { free(p); }
  673. constexpr friend auto operator==(allocator, allocator) noexcept -> bool {
  674. return true; // All instances of this allocator are equivalent.
  675. }
  676. constexpr friend auto operator!=(allocator, allocator) noexcept -> bool {
  677. return false;
  678. }
  679. };
  680. template <typename Formatter>
  681. FMT_CONSTEXPR auto maybe_set_debug_format(Formatter& f, bool set)
  682. -> decltype(f.set_debug_format(set)) {
  683. f.set_debug_format(set);
  684. }
  685. template <typename Formatter>
  686. FMT_CONSTEXPR void maybe_set_debug_format(Formatter&, ...) {}
  687. } // namespace detail
  688. FMT_BEGIN_EXPORT
  689. // The number of characters to store in the basic_memory_buffer object itself
  690. // to avoid dynamic memory allocation.
  691. enum { inline_buffer_size = 500 };
  692. /**
  693. * A dynamically growing memory buffer for trivially copyable/constructible
  694. * types with the first `SIZE` elements stored in the object itself. Most
  695. * commonly used via the `memory_buffer` alias for `char`.
  696. *
  697. * **Example**:
  698. *
  699. * auto out = fmt::memory_buffer();
  700. * fmt::format_to(std::back_inserter(out), "The answer is {}.", 42);
  701. *
  702. * This will append "The answer is 42." to `out`. The buffer content can be
  703. * converted to `std::string` with `to_string(out)`.
  704. */
  705. template <typename T, size_t SIZE = inline_buffer_size,
  706. typename Allocator = detail::allocator<T>>
  707. class basic_memory_buffer : public detail::buffer<T> {
  708. private:
  709. T store_[SIZE];
  710. // Don't inherit from Allocator to avoid generating type_info for it.
  711. FMT_NO_UNIQUE_ADDRESS Allocator alloc_;
  712. // Deallocate memory allocated by the buffer.
  713. FMT_CONSTEXPR20 void deallocate() {
  714. T* data = this->data();
  715. if (data != store_) alloc_.deallocate(data, this->capacity());
  716. }
  717. static FMT_CONSTEXPR20 void grow(detail::buffer<T>& buf, size_t size) {
  718. detail::abort_fuzzing_if(size > 5000);
  719. auto& self = static_cast<basic_memory_buffer&>(buf);
  720. const size_t max_size =
  721. std::allocator_traits<Allocator>::max_size(self.alloc_);
  722. size_t old_capacity = buf.capacity();
  723. size_t new_capacity = old_capacity + old_capacity / 2;
  724. if (size > new_capacity)
  725. new_capacity = size;
  726. else if (new_capacity > max_size)
  727. new_capacity = max_of(size, max_size);
  728. T* old_data = buf.data();
  729. T* new_data = self.alloc_.allocate(new_capacity);
  730. // Suppress a bogus -Wstringop-overflow in gcc 13.1 (#3481).
  731. detail::assume(buf.size() <= new_capacity);
  732. // The following code doesn't throw, so the raw pointer above doesn't leak.
  733. memcpy(new_data, old_data, buf.size() * sizeof(T));
  734. self.set(new_data, new_capacity);
  735. // deallocate must not throw according to the standard, but even if it does,
  736. // the buffer already uses the new storage and will deallocate it in
  737. // destructor.
  738. if (old_data != self.store_) self.alloc_.deallocate(old_data, old_capacity);
  739. }
  740. public:
  741. using value_type = T;
  742. using const_reference = const T&;
  743. FMT_CONSTEXPR explicit basic_memory_buffer(
  744. const Allocator& alloc = Allocator())
  745. : detail::buffer<T>(grow), alloc_(alloc) {
  746. this->set(store_, SIZE);
  747. if (detail::is_constant_evaluated()) detail::fill_n(store_, SIZE, T());
  748. }
  749. FMT_CONSTEXPR20 ~basic_memory_buffer() { deallocate(); }
  750. private:
  751. template <typename Alloc = Allocator,
  752. FMT_ENABLE_IF(std::allocator_traits<Alloc>::
  753. propagate_on_container_move_assignment::value)>
  754. FMT_CONSTEXPR20 auto move_alloc(basic_memory_buffer& other) -> bool {
  755. alloc_ = std::move(other.alloc_);
  756. return true;
  757. }
  758. // If the allocator does not propagate then copy the data from other.
  759. template <typename Alloc = Allocator,
  760. FMT_ENABLE_IF(!std::allocator_traits<Alloc>::
  761. propagate_on_container_move_assignment::value)>
  762. FMT_CONSTEXPR20 auto move_alloc(basic_memory_buffer& other) -> bool {
  763. T* data = other.data();
  764. if (alloc_ == other.alloc_ || data == other.store_) return true;
  765. size_t size = other.size();
  766. // Perform copy operation, allocators are different.
  767. this->resize(size);
  768. detail::copy<T>(data, data + size, this->data());
  769. return false;
  770. }
  771. // Move data from other to this buffer.
  772. FMT_CONSTEXPR20 void move(basic_memory_buffer& other) {
  773. T* data = other.data();
  774. size_t size = other.size(), capacity = other.capacity();
  775. if (!move_alloc(other)) return;
  776. if (data == other.store_) {
  777. this->set(store_, capacity);
  778. detail::copy<T>(other.store_, other.store_ + size, store_);
  779. } else {
  780. this->set(data, capacity);
  781. // Set pointer to the inline array so that delete is not called
  782. // when deallocating.
  783. other.set(other.store_, 0);
  784. other.clear();
  785. }
  786. this->resize(size);
  787. }
  788. public:
  789. /// Constructs a `basic_memory_buffer` object moving the content of the other
  790. /// object to it.
  791. FMT_CONSTEXPR20 basic_memory_buffer(basic_memory_buffer&& other) noexcept
  792. : detail::buffer<T>(grow) {
  793. move(other);
  794. }
  795. /// Moves the content of the other `basic_memory_buffer` object to this one.
  796. auto operator=(basic_memory_buffer&& other) noexcept -> basic_memory_buffer& {
  797. FMT_ASSERT(this != &other, "");
  798. deallocate();
  799. move(other);
  800. return *this;
  801. }
  802. // Returns a copy of the allocator associated with this buffer.
  803. auto get_allocator() const -> Allocator { return alloc_; }
  804. /// Resizes the buffer to contain `count` elements. If T is a POD type new
  805. /// elements may not be initialized.
  806. FMT_CONSTEXPR void resize(size_t count) { this->try_resize(count); }
  807. /// Increases the buffer capacity to `new_capacity`.
  808. void reserve(size_t new_capacity) { this->try_reserve(new_capacity); }
  809. using detail::buffer<T>::append;
  810. template <typename ContiguousRange>
  811. FMT_CONSTEXPR20 void append(const ContiguousRange& range) {
  812. append(range.data(), range.data() + range.size());
  813. }
  814. };
  815. using memory_buffer = basic_memory_buffer<char>;
  816. template <size_t SIZE>
  817. FMT_NODISCARD auto to_string(const basic_memory_buffer<char, SIZE>& buf)
  818. -> std::string {
  819. auto size = buf.size();
  820. detail::assume(size < std::string().max_size());
  821. return {buf.data(), size};
  822. }
  823. // A writer to a buffered stream. It doesn't own the underlying stream.
  824. class writer {
  825. private:
  826. detail::buffer<char>* buf_;
  827. // We cannot create a file buffer in advance because any write to a FILE may
  828. // invalidate it.
  829. FILE* file_;
  830. public:
  831. inline writer(FILE* f) : buf_(nullptr), file_(f) {}
  832. inline writer(detail::buffer<char>& buf) : buf_(&buf) {}
  833. /// Formats `args` according to specifications in `fmt` and writes the
  834. /// output to the file.
  835. template <typename... T> void print(format_string<T...> fmt, T&&... args) {
  836. if (buf_)
  837. fmt::format_to(appender(*buf_), fmt, std::forward<T>(args)...);
  838. else
  839. fmt::print(file_, fmt, std::forward<T>(args)...);
  840. }
  841. };
  842. class string_buffer {
  843. private:
  844. std::string str_;
  845. detail::container_buffer<std::string> buf_;
  846. public:
  847. inline string_buffer() : buf_(str_) {}
  848. inline operator writer() { return buf_; }
  849. inline auto str() -> std::string& { return str_; }
  850. };
  851. template <typename T, size_t SIZE, typename Allocator>
  852. struct is_contiguous<basic_memory_buffer<T, SIZE, Allocator>> : std::true_type {
  853. };
  854. // Suppress a misleading warning in older versions of clang.
  855. FMT_PRAGMA_CLANG(diagnostic ignored "-Wweak-vtables")
  856. /// An error reported from a formatting function.
  857. class FMT_SO_VISIBILITY("default") format_error : public std::runtime_error {
  858. public:
  859. using std::runtime_error::runtime_error;
  860. };
  861. class loc_value;
  862. FMT_END_EXPORT
  863. namespace detail {
  864. FMT_API auto write_console(int fd, string_view text) -> bool;
  865. FMT_API void print(FILE*, string_view);
  866. } // namespace detail
  867. namespace detail {
  868. template <typename Char, size_t N> struct fixed_string {
  869. FMT_CONSTEXPR20 fixed_string(const Char (&s)[N]) {
  870. detail::copy<Char, const Char*, Char*>(static_cast<const Char*>(s), s + N,
  871. data);
  872. }
  873. Char data[N] = {};
  874. };
  875. // Converts a compile-time string to basic_string_view.
  876. FMT_EXPORT template <typename Char, size_t N>
  877. constexpr auto compile_string_to_view(const Char (&s)[N])
  878. -> basic_string_view<Char> {
  879. // Remove trailing NUL character if needed. Won't be present if this is used
  880. // with a raw character array (i.e. not defined as a string).
  881. return {s, N - (std::char_traits<Char>::to_int_type(s[N - 1]) == 0 ? 1 : 0)};
  882. }
  883. FMT_EXPORT template <typename Char>
  884. constexpr auto compile_string_to_view(basic_string_view<Char> s)
  885. -> basic_string_view<Char> {
  886. return s;
  887. }
  888. // Returns true if value is negative, false otherwise.
  889. // Same as `value < 0` but doesn't produce warnings if T is an unsigned type.
  890. template <typename T, FMT_ENABLE_IF(is_signed<T>::value)>
  891. constexpr auto is_negative(T value) -> bool {
  892. return value < 0;
  893. }
  894. template <typename T, FMT_ENABLE_IF(!is_signed<T>::value)>
  895. constexpr auto is_negative(T) -> bool {
  896. return false;
  897. }
  898. // Smallest of uint32_t, uint64_t, uint128_t that is large enough to
  899. // represent all values of an integral type T.
  900. template <typename T>
  901. using uint32_or_64_or_128_t =
  902. conditional_t<num_bits<T>() <= 32 && !FMT_REDUCE_INT_INSTANTIATIONS,
  903. uint32_t,
  904. conditional_t<num_bits<T>() <= 64, uint64_t, uint128_t>>;
  905. template <typename T>
  906. using uint64_or_128_t = conditional_t<num_bits<T>() <= 64, uint64_t, uint128_t>;
  907. #define FMT_POWERS_OF_10(factor) \
  908. factor * 10, (factor) * 100, (factor) * 1000, (factor) * 10000, \
  909. (factor) * 100000, (factor) * 1000000, (factor) * 10000000, \
  910. (factor) * 100000000, (factor) * 1000000000
  911. // Converts value in the range [0, 100) to a string.
  912. // GCC generates slightly better code when value is pointer-size.
  913. inline auto digits2(size_t value) -> const char* {
  914. // Align data since unaligned access may be slower when crossing a
  915. // hardware-specific boundary.
  916. alignas(2) static const char data[] =
  917. "0001020304050607080910111213141516171819"
  918. "2021222324252627282930313233343536373839"
  919. "4041424344454647484950515253545556575859"
  920. "6061626364656667686970717273747576777879"
  921. "8081828384858687888990919293949596979899";
  922. return &data[value * 2];
  923. }
  924. template <typename Char> constexpr auto getsign(sign s) -> Char {
  925. return static_cast<char>(((' ' << 24) | ('+' << 16) | ('-' << 8)) >>
  926. (static_cast<int>(s) * 8));
  927. }
  928. template <typename T> FMT_CONSTEXPR auto count_digits_fallback(T n) -> int {
  929. int count = 1;
  930. for (;;) {
  931. // Integer division is slow so do it for a group of four digits instead
  932. // of for every digit. The idea comes from the talk by Alexandrescu
  933. // "Three Optimization Tips for C++". See speed-test for a comparison.
  934. if (n < 10) return count;
  935. if (n < 100) return count + 1;
  936. if (n < 1000) return count + 2;
  937. if (n < 10000) return count + 3;
  938. n /= 10000u;
  939. count += 4;
  940. }
  941. }
  942. #if FMT_USE_INT128
  943. FMT_CONSTEXPR inline auto count_digits(uint128_opt n) -> int {
  944. return count_digits_fallback(n);
  945. }
  946. #endif
  947. #ifdef FMT_BUILTIN_CLZLL
  948. // It is a separate function rather than a part of count_digits to workaround
  949. // the lack of static constexpr in constexpr functions.
  950. inline auto do_count_digits(uint64_t n) -> int {
  951. // This has comparable performance to the version by Kendall Willets
  952. // (https://github.com/fmtlib/format-benchmark/blob/master/digits10)
  953. // but uses smaller tables.
  954. // Maps bsr(n) to ceil(log10(pow(2, bsr(n) + 1) - 1)).
  955. static constexpr uint8_t bsr2log10[] = {
  956. 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5,
  957. 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10,
  958. 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15,
  959. 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20};
  960. auto t = bsr2log10[FMT_BUILTIN_CLZLL(n | 1) ^ 63];
  961. static constexpr uint64_t zero_or_powers_of_10[] = {
  962. 0, 0, FMT_POWERS_OF_10(1U), FMT_POWERS_OF_10(1000000000ULL),
  963. 10000000000000000000ULL};
  964. return t - (n < zero_or_powers_of_10[t]);
  965. }
  966. #endif
  967. // Returns the number of decimal digits in n. Leading zeros are not counted
  968. // except for n == 0 in which case count_digits returns 1.
  969. FMT_CONSTEXPR20 inline auto count_digits(uint64_t n) -> int {
  970. #ifdef FMT_BUILTIN_CLZLL
  971. if (!is_constant_evaluated() && !FMT_OPTIMIZE_SIZE) return do_count_digits(n);
  972. #endif
  973. return count_digits_fallback(n);
  974. }
  975. // Counts the number of digits in n. BITS = log2(radix).
  976. template <int BITS, typename UInt>
  977. FMT_CONSTEXPR auto count_digits(UInt n) -> int {
  978. #ifdef FMT_BUILTIN_CLZ
  979. if (!is_constant_evaluated() && num_bits<UInt>() == 32)
  980. return (FMT_BUILTIN_CLZ(static_cast<uint32_t>(n) | 1) ^ 31) / BITS + 1;
  981. #endif
  982. // Lambda avoids unreachable code warnings from NVHPC.
  983. return [](UInt m) {
  984. int num_digits = 0;
  985. do {
  986. ++num_digits;
  987. } while ((m >>= BITS) != 0);
  988. return num_digits;
  989. }(n);
  990. }
  991. #ifdef FMT_BUILTIN_CLZ
  992. // It is a separate function rather than a part of count_digits to workaround
  993. // the lack of static constexpr in constexpr functions.
  994. FMT_INLINE auto do_count_digits(uint32_t n) -> int {
  995. // An optimization by Kendall Willets from https://bit.ly/3uOIQrB.
  996. // This increments the upper 32 bits (log10(T) - 1) when >= T is added.
  997. # define FMT_INC(T) (((sizeof(#T) - 1ull) << 32) - T)
  998. static constexpr uint64_t table[] = {
  999. FMT_INC(0), FMT_INC(0), FMT_INC(0), // 8
  1000. FMT_INC(10), FMT_INC(10), FMT_INC(10), // 64
  1001. FMT_INC(100), FMT_INC(100), FMT_INC(100), // 512
  1002. FMT_INC(1000), FMT_INC(1000), FMT_INC(1000), // 4096
  1003. FMT_INC(10000), FMT_INC(10000), FMT_INC(10000), // 32k
  1004. FMT_INC(100000), FMT_INC(100000), FMT_INC(100000), // 256k
  1005. FMT_INC(1000000), FMT_INC(1000000), FMT_INC(1000000), // 2048k
  1006. FMT_INC(10000000), FMT_INC(10000000), FMT_INC(10000000), // 16M
  1007. FMT_INC(100000000), FMT_INC(100000000), FMT_INC(100000000), // 128M
  1008. FMT_INC(1000000000), FMT_INC(1000000000), FMT_INC(1000000000), // 1024M
  1009. FMT_INC(1000000000), FMT_INC(1000000000) // 4B
  1010. };
  1011. auto inc = table[FMT_BUILTIN_CLZ(n | 1) ^ 31];
  1012. return static_cast<int>((n + inc) >> 32);
  1013. }
  1014. #endif
  1015. // Optional version of count_digits for better performance on 32-bit platforms.
  1016. FMT_CONSTEXPR20 inline auto count_digits(uint32_t n) -> int {
  1017. #ifdef FMT_BUILTIN_CLZ
  1018. if (!is_constant_evaluated() && !FMT_OPTIMIZE_SIZE) return do_count_digits(n);
  1019. #endif
  1020. return count_digits_fallback(n);
  1021. }
  1022. template <typename Int> constexpr auto digits10() noexcept -> int {
  1023. return std::numeric_limits<Int>::digits10;
  1024. }
  1025. template <> constexpr auto digits10<int128_opt>() noexcept -> int { return 38; }
  1026. template <> constexpr auto digits10<uint128_t>() noexcept -> int { return 38; }
  1027. template <typename Char> struct thousands_sep_result {
  1028. std::string grouping;
  1029. Char thousands_sep;
  1030. };
  1031. template <typename Char>
  1032. FMT_API auto thousands_sep_impl(locale_ref loc) -> thousands_sep_result<Char>;
  1033. template <typename Char>
  1034. inline auto thousands_sep(locale_ref loc) -> thousands_sep_result<Char> {
  1035. auto result = thousands_sep_impl<char>(loc);
  1036. return {result.grouping, Char(result.thousands_sep)};
  1037. }
  1038. template <>
  1039. inline auto thousands_sep(locale_ref loc) -> thousands_sep_result<wchar_t> {
  1040. return thousands_sep_impl<wchar_t>(loc);
  1041. }
  1042. template <typename Char>
  1043. FMT_API auto decimal_point_impl(locale_ref loc) -> Char;
  1044. template <typename Char> inline auto decimal_point(locale_ref loc) -> Char {
  1045. return Char(decimal_point_impl<char>(loc));
  1046. }
  1047. template <> inline auto decimal_point(locale_ref loc) -> wchar_t {
  1048. return decimal_point_impl<wchar_t>(loc);
  1049. }
  1050. #ifndef FMT_HEADER_ONLY
  1051. FMT_BEGIN_EXPORT
  1052. extern template FMT_API auto thousands_sep_impl<char>(locale_ref)
  1053. -> thousands_sep_result<char>;
  1054. extern template FMT_API auto thousands_sep_impl<wchar_t>(locale_ref)
  1055. -> thousands_sep_result<wchar_t>;
  1056. extern template FMT_API auto decimal_point_impl(locale_ref) -> char;
  1057. extern template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t;
  1058. FMT_END_EXPORT
  1059. #endif // FMT_HEADER_ONLY
  1060. // Compares two characters for equality.
  1061. template <typename Char> auto equal2(const Char* lhs, const char* rhs) -> bool {
  1062. return lhs[0] == Char(rhs[0]) && lhs[1] == Char(rhs[1]);
  1063. }
  1064. inline auto equal2(const char* lhs, const char* rhs) -> bool {
  1065. return memcmp(lhs, rhs, 2) == 0;
  1066. }
  1067. // Writes a two-digit value to out.
  1068. template <typename Char>
  1069. FMT_CONSTEXPR20 FMT_INLINE void write2digits(Char* out, size_t value) {
  1070. if (!is_constant_evaluated() && std::is_same<Char, char>::value &&
  1071. !FMT_OPTIMIZE_SIZE) {
  1072. memcpy(out, digits2(value), 2);
  1073. return;
  1074. }
  1075. *out++ = static_cast<Char>('0' + value / 10);
  1076. *out = static_cast<Char>('0' + value % 10);
  1077. }
  1078. // Formats a decimal unsigned integer value writing to out pointing to a buffer
  1079. // of specified size. The caller must ensure that the buffer is large enough.
  1080. template <typename Char, typename UInt>
  1081. FMT_CONSTEXPR20 auto do_format_decimal(Char* out, UInt value, int size)
  1082. -> Char* {
  1083. FMT_ASSERT(size >= count_digits(value), "invalid digit count");
  1084. unsigned n = to_unsigned(size);
  1085. while (value >= 100) {
  1086. // Integer division is slow so do it for a group of two digits instead
  1087. // of for every digit. The idea comes from the talk by Alexandrescu
  1088. // "Three Optimization Tips for C++". See speed-test for a comparison.
  1089. n -= 2;
  1090. write2digits(out + n, static_cast<unsigned>(value % 100));
  1091. value /= 100;
  1092. }
  1093. if (value >= 10) {
  1094. n -= 2;
  1095. write2digits(out + n, static_cast<unsigned>(value));
  1096. } else {
  1097. out[--n] = static_cast<Char>('0' + value);
  1098. }
  1099. return out + n;
  1100. }
  1101. template <typename Char, typename UInt>
  1102. FMT_CONSTEXPR FMT_INLINE auto format_decimal(Char* out, UInt value,
  1103. int num_digits) -> Char* {
  1104. do_format_decimal(out, value, num_digits);
  1105. return out + num_digits;
  1106. }
  1107. template <typename Char, typename UInt, typename OutputIt,
  1108. FMT_ENABLE_IF(!std::is_pointer<remove_cvref_t<OutputIt>>::value)>
  1109. FMT_CONSTEXPR auto format_decimal(OutputIt out, UInt value, int num_digits)
  1110. -> OutputIt {
  1111. if (auto ptr = to_pointer<Char>(out, to_unsigned(num_digits))) {
  1112. do_format_decimal(ptr, value, num_digits);
  1113. return out;
  1114. }
  1115. // Buffer is large enough to hold all digits (digits10 + 1).
  1116. char buffer[digits10<UInt>() + 1];
  1117. if (is_constant_evaluated()) fill_n(buffer, sizeof(buffer), '\0');
  1118. do_format_decimal(buffer, value, num_digits);
  1119. return copy_noinline<Char>(buffer, buffer + num_digits, out);
  1120. }
  1121. template <typename Char, typename UInt>
  1122. FMT_CONSTEXPR auto do_format_base2e(int base_bits, Char* out, UInt value,
  1123. int size, bool upper = false) -> Char* {
  1124. out += size;
  1125. do {
  1126. const char* digits = upper ? "0123456789ABCDEF" : "0123456789abcdef";
  1127. unsigned digit = static_cast<unsigned>(value & ((1u << base_bits) - 1));
  1128. *--out = static_cast<Char>(base_bits < 4 ? static_cast<char>('0' + digit)
  1129. : digits[digit]);
  1130. } while ((value >>= base_bits) != 0);
  1131. return out;
  1132. }
  1133. // Formats an unsigned integer in the power of two base (binary, octal, hex).
  1134. template <typename Char, typename UInt>
  1135. FMT_CONSTEXPR auto format_base2e(int base_bits, Char* out, UInt value,
  1136. int num_digits, bool upper = false) -> Char* {
  1137. do_format_base2e(base_bits, out, value, num_digits, upper);
  1138. return out + num_digits;
  1139. }
  1140. template <typename Char, typename OutputIt, typename UInt,
  1141. FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value)>
  1142. FMT_CONSTEXPR inline auto format_base2e(int base_bits, OutputIt out, UInt value,
  1143. int num_digits, bool upper = false)
  1144. -> OutputIt {
  1145. if (auto ptr = to_pointer<Char>(out, to_unsigned(num_digits))) {
  1146. format_base2e(base_bits, ptr, value, num_digits, upper);
  1147. return out;
  1148. }
  1149. // Make buffer large enough for any base.
  1150. char buffer[num_bits<UInt>()];
  1151. if (is_constant_evaluated()) fill_n(buffer, sizeof(buffer), '\0');
  1152. format_base2e(base_bits, buffer, value, num_digits, upper);
  1153. return detail::copy_noinline<Char>(buffer, buffer + num_digits, out);
  1154. }
  1155. // A converter from UTF-8 to UTF-16.
  1156. class utf8_to_utf16 {
  1157. private:
  1158. basic_memory_buffer<wchar_t> buffer_;
  1159. public:
  1160. FMT_API explicit utf8_to_utf16(string_view s);
  1161. inline operator basic_string_view<wchar_t>() const {
  1162. return {&buffer_[0], size()};
  1163. }
  1164. inline auto size() const -> size_t { return buffer_.size() - 1; }
  1165. inline auto c_str() const -> const wchar_t* { return &buffer_[0]; }
  1166. inline auto str() const -> std::wstring { return {&buffer_[0], size()}; }
  1167. };
  1168. enum class to_utf8_error_policy { abort, replace };
  1169. // A converter from UTF-16/UTF-32 (host endian) to UTF-8.
  1170. template <typename WChar, typename Buffer = memory_buffer> class to_utf8 {
  1171. private:
  1172. Buffer buffer_;
  1173. public:
  1174. to_utf8() {}
  1175. explicit to_utf8(basic_string_view<WChar> s,
  1176. to_utf8_error_policy policy = to_utf8_error_policy::abort) {
  1177. static_assert(sizeof(WChar) == 2 || sizeof(WChar) == 4,
  1178. "expected utf16 or utf32");
  1179. if (!convert(s, policy)) {
  1180. FMT_THROW(std::runtime_error(sizeof(WChar) == 2 ? "invalid utf16"
  1181. : "invalid utf32"));
  1182. }
  1183. }
  1184. operator string_view() const { return string_view(&buffer_[0], size()); }
  1185. auto size() const -> size_t { return buffer_.size() - 1; }
  1186. auto c_str() const -> const char* { return &buffer_[0]; }
  1187. auto str() const -> std::string { return std::string(&buffer_[0], size()); }
  1188. // Performs conversion returning a bool instead of throwing exception on
  1189. // conversion error. This method may still throw in case of memory allocation
  1190. // error.
  1191. auto convert(basic_string_view<WChar> s,
  1192. to_utf8_error_policy policy = to_utf8_error_policy::abort)
  1193. -> bool {
  1194. if (!convert(buffer_, s, policy)) return false;
  1195. buffer_.push_back(0);
  1196. return true;
  1197. }
  1198. static auto convert(Buffer& buf, basic_string_view<WChar> s,
  1199. to_utf8_error_policy policy = to_utf8_error_policy::abort)
  1200. -> bool {
  1201. for (auto p = s.begin(); p != s.end(); ++p) {
  1202. uint32_t c = static_cast<uint32_t>(*p);
  1203. if (sizeof(WChar) == 2 && c >= 0xd800 && c <= 0xdfff) {
  1204. // Handle a surrogate pair.
  1205. ++p;
  1206. if (p == s.end() || (c & 0xfc00) != 0xd800 || (*p & 0xfc00) != 0xdc00) {
  1207. if (policy == to_utf8_error_policy::abort) return false;
  1208. buf.append(string_view("\xEF\xBF\xBD"));
  1209. --p;
  1210. continue;
  1211. }
  1212. c = (c << 10) + static_cast<uint32_t>(*p) - 0x35fdc00;
  1213. }
  1214. if (c < 0x80) {
  1215. buf.push_back(static_cast<char>(c));
  1216. } else if (c < 0x800) {
  1217. buf.push_back(static_cast<char>(0xc0 | (c >> 6)));
  1218. buf.push_back(static_cast<char>(0x80 | (c & 0x3f)));
  1219. } else if ((c >= 0x800 && c <= 0xd7ff) || (c >= 0xe000 && c <= 0xffff)) {
  1220. buf.push_back(static_cast<char>(0xe0 | (c >> 12)));
  1221. buf.push_back(static_cast<char>(0x80 | ((c & 0xfff) >> 6)));
  1222. buf.push_back(static_cast<char>(0x80 | (c & 0x3f)));
  1223. } else if (c >= 0x10000 && c <= 0x10ffff) {
  1224. buf.push_back(static_cast<char>(0xf0 | (c >> 18)));
  1225. buf.push_back(static_cast<char>(0x80 | ((c & 0x3ffff) >> 12)));
  1226. buf.push_back(static_cast<char>(0x80 | ((c & 0xfff) >> 6)));
  1227. buf.push_back(static_cast<char>(0x80 | (c & 0x3f)));
  1228. } else {
  1229. return false;
  1230. }
  1231. }
  1232. return true;
  1233. }
  1234. };
  1235. // Computes 128-bit result of multiplication of two 64-bit unsigned integers.
  1236. FMT_INLINE auto umul128(uint64_t x, uint64_t y) noexcept -> uint128_fallback {
  1237. #if FMT_USE_INT128
  1238. auto p = static_cast<uint128_opt>(x) * static_cast<uint128_opt>(y);
  1239. return {static_cast<uint64_t>(p >> 64), static_cast<uint64_t>(p)};
  1240. #elif defined(_MSC_VER) && defined(_M_X64)
  1241. auto hi = uint64_t();
  1242. auto lo = _umul128(x, y, &hi);
  1243. return {hi, lo};
  1244. #else
  1245. const uint64_t mask = static_cast<uint64_t>(max_value<uint32_t>());
  1246. uint64_t a = x >> 32;
  1247. uint64_t b = x & mask;
  1248. uint64_t c = y >> 32;
  1249. uint64_t d = y & mask;
  1250. uint64_t ac = a * c;
  1251. uint64_t bc = b * c;
  1252. uint64_t ad = a * d;
  1253. uint64_t bd = b * d;
  1254. uint64_t intermediate = (bd >> 32) + (ad & mask) + (bc & mask);
  1255. return {ac + (intermediate >> 32) + (ad >> 32) + (bc >> 32),
  1256. (intermediate << 32) + (bd & mask)};
  1257. #endif
  1258. }
  1259. namespace dragonbox {
  1260. // Computes floor(log10(pow(2, e))) for e in [-2620, 2620] using the method from
  1261. // https://fmt.dev/papers/Dragonbox.pdf#page=28, section 6.1.
  1262. inline auto floor_log10_pow2(int e) noexcept -> int {
  1263. FMT_ASSERT(e <= 2620 && e >= -2620, "too large exponent");
  1264. static_assert((-1 >> 1) == -1, "right shift is not arithmetic");
  1265. return (e * 315653) >> 20;
  1266. }
  1267. inline auto floor_log2_pow10(int e) noexcept -> int {
  1268. FMT_ASSERT(e <= 1233 && e >= -1233, "too large exponent");
  1269. return (e * 1741647) >> 19;
  1270. }
  1271. // Computes upper 64 bits of multiplication of two 64-bit unsigned integers.
  1272. inline auto umul128_upper64(uint64_t x, uint64_t y) noexcept -> uint64_t {
  1273. #if FMT_USE_INT128
  1274. auto p = static_cast<uint128_opt>(x) * static_cast<uint128_opt>(y);
  1275. return static_cast<uint64_t>(p >> 64);
  1276. #elif defined(_MSC_VER) && defined(_M_X64)
  1277. return __umulh(x, y);
  1278. #else
  1279. return umul128(x, y).high();
  1280. #endif
  1281. }
  1282. // Computes upper 128 bits of multiplication of a 64-bit unsigned integer and a
  1283. // 128-bit unsigned integer.
  1284. inline auto umul192_upper128(uint64_t x, uint128_fallback y) noexcept
  1285. -> uint128_fallback {
  1286. uint128_fallback r = umul128(x, y.high());
  1287. r += umul128_upper64(x, y.low());
  1288. return r;
  1289. }
  1290. FMT_API auto get_cached_power(int k) noexcept -> uint128_fallback;
  1291. // Type-specific information that Dragonbox uses.
  1292. template <typename T, typename Enable = void> struct float_info;
  1293. template <> struct float_info<float> {
  1294. using carrier_uint = uint32_t;
  1295. static const int exponent_bits = 8;
  1296. static const int kappa = 1;
  1297. static const int big_divisor = 100;
  1298. static const int small_divisor = 10;
  1299. static const int min_k = -31;
  1300. static const int max_k = 46;
  1301. static const int shorter_interval_tie_lower_threshold = -35;
  1302. static const int shorter_interval_tie_upper_threshold = -35;
  1303. };
  1304. template <> struct float_info<double> {
  1305. using carrier_uint = uint64_t;
  1306. static const int exponent_bits = 11;
  1307. static const int kappa = 2;
  1308. static const int big_divisor = 1000;
  1309. static const int small_divisor = 100;
  1310. static const int min_k = -292;
  1311. static const int max_k = 341;
  1312. static const int shorter_interval_tie_lower_threshold = -77;
  1313. static const int shorter_interval_tie_upper_threshold = -77;
  1314. };
  1315. // An 80- or 128-bit floating point number.
  1316. template <typename T>
  1317. struct float_info<T, enable_if_t<std::numeric_limits<T>::digits == 64 ||
  1318. std::numeric_limits<T>::digits == 113 ||
  1319. is_float128<T>::value>> {
  1320. using carrier_uint = detail::uint128_t;
  1321. static const int exponent_bits = 15;
  1322. };
  1323. // A double-double floating point number.
  1324. template <typename T>
  1325. struct float_info<T, enable_if_t<is_double_double<T>::value>> {
  1326. using carrier_uint = detail::uint128_t;
  1327. };
  1328. template <typename T> struct decimal_fp {
  1329. using significand_type = typename float_info<T>::carrier_uint;
  1330. significand_type significand;
  1331. int exponent;
  1332. };
  1333. template <typename T> FMT_API auto to_decimal(T x) noexcept -> decimal_fp<T>;
  1334. } // namespace dragonbox
  1335. // Returns true iff Float has the implicit bit which is not stored.
  1336. template <typename Float> constexpr auto has_implicit_bit() -> bool {
  1337. // An 80-bit FP number has a 64-bit significand an no implicit bit.
  1338. return std::numeric_limits<Float>::digits != 64;
  1339. }
  1340. // Returns the number of significand bits stored in Float. The implicit bit is
  1341. // not counted since it is not stored.
  1342. template <typename Float> constexpr auto num_significand_bits() -> int {
  1343. // std::numeric_limits may not support __float128.
  1344. return is_float128<Float>() ? 112
  1345. : (std::numeric_limits<Float>::digits -
  1346. (has_implicit_bit<Float>() ? 1 : 0));
  1347. }
  1348. template <typename Float>
  1349. constexpr auto exponent_mask() ->
  1350. typename dragonbox::float_info<Float>::carrier_uint {
  1351. using float_uint = typename dragonbox::float_info<Float>::carrier_uint;
  1352. return ((float_uint(1) << dragonbox::float_info<Float>::exponent_bits) - 1)
  1353. << num_significand_bits<Float>();
  1354. }
  1355. template <typename Float> constexpr auto exponent_bias() -> int {
  1356. // std::numeric_limits may not support __float128.
  1357. return is_float128<Float>() ? 16383
  1358. : std::numeric_limits<Float>::max_exponent - 1;
  1359. }
  1360. FMT_CONSTEXPR inline auto compute_exp_size(int exp) -> int {
  1361. auto prefix_size = 2; // sign + 'e'
  1362. auto abs_exp = exp >= 0 ? exp : -exp;
  1363. if (abs_exp < 100) return prefix_size + 2;
  1364. return prefix_size + (abs_exp >= 1000 ? 4 : 3);
  1365. }
  1366. // Writes the exponent exp in the form "[+-]d{2,3}" to buffer.
  1367. template <typename Char, typename OutputIt>
  1368. FMT_CONSTEXPR auto write_exponent(int exp, OutputIt out) -> OutputIt {
  1369. FMT_ASSERT(-10000 < exp && exp < 10000, "exponent out of range");
  1370. if (exp < 0) {
  1371. *out++ = static_cast<Char>('-');
  1372. exp = -exp;
  1373. } else {
  1374. *out++ = static_cast<Char>('+');
  1375. }
  1376. auto uexp = static_cast<uint32_t>(exp);
  1377. if (is_constant_evaluated()) {
  1378. if (uexp < 10) *out++ = '0';
  1379. return format_decimal<Char>(out, uexp, count_digits(uexp));
  1380. }
  1381. if (uexp >= 100u) {
  1382. const char* top = digits2(uexp / 100);
  1383. if (uexp >= 1000u) *out++ = static_cast<Char>(top[0]);
  1384. *out++ = static_cast<Char>(top[1]);
  1385. uexp %= 100;
  1386. }
  1387. const char* d = digits2(uexp);
  1388. *out++ = static_cast<Char>(d[0]);
  1389. *out++ = static_cast<Char>(d[1]);
  1390. return out;
  1391. }
  1392. // A floating-point number f * pow(2, e) where F is an unsigned type.
  1393. template <typename F> struct basic_fp {
  1394. F f;
  1395. int e;
  1396. static constexpr int num_significand_bits =
  1397. static_cast<int>(sizeof(F) * num_bits<unsigned char>());
  1398. constexpr basic_fp() : f(0), e(0) {}
  1399. constexpr basic_fp(uint64_t f_val, int e_val) : f(f_val), e(e_val) {}
  1400. // Constructs fp from an IEEE754 floating-point number.
  1401. template <typename Float> FMT_CONSTEXPR basic_fp(Float n) { assign(n); }
  1402. // Assigns n to this and return true iff predecessor is closer than successor.
  1403. template <typename Float, FMT_ENABLE_IF(!is_double_double<Float>::value)>
  1404. FMT_CONSTEXPR auto assign(Float n) -> bool {
  1405. static_assert(std::numeric_limits<Float>::digits <= 113, "unsupported FP");
  1406. // Assume Float is in the format [sign][exponent][significand].
  1407. using carrier_uint = typename dragonbox::float_info<Float>::carrier_uint;
  1408. const auto num_float_significand_bits =
  1409. detail::num_significand_bits<Float>();
  1410. const auto implicit_bit = carrier_uint(1) << num_float_significand_bits;
  1411. const auto significand_mask = implicit_bit - 1;
  1412. auto u = bit_cast<carrier_uint>(n);
  1413. f = static_cast<F>(u & significand_mask);
  1414. auto biased_e = static_cast<int>((u & exponent_mask<Float>()) >>
  1415. num_float_significand_bits);
  1416. // The predecessor is closer if n is a normalized power of 2 (f == 0)
  1417. // other than the smallest normalized number (biased_e > 1).
  1418. auto is_predecessor_closer = f == 0 && biased_e > 1;
  1419. if (biased_e == 0)
  1420. biased_e = 1; // Subnormals use biased exponent 1 (min exponent).
  1421. else if (has_implicit_bit<Float>())
  1422. f += static_cast<F>(implicit_bit);
  1423. e = biased_e - exponent_bias<Float>() - num_float_significand_bits;
  1424. if (!has_implicit_bit<Float>()) ++e;
  1425. return is_predecessor_closer;
  1426. }
  1427. template <typename Float, FMT_ENABLE_IF(is_double_double<Float>::value)>
  1428. FMT_CONSTEXPR auto assign(Float n) -> bool {
  1429. static_assert(std::numeric_limits<double>::is_iec559, "unsupported FP");
  1430. return assign(static_cast<double>(n));
  1431. }
  1432. };
  1433. using fp = basic_fp<unsigned long long>;
  1434. // Normalizes the value converted from double and multiplied by (1 << SHIFT).
  1435. template <int SHIFT = 0, typename F>
  1436. FMT_CONSTEXPR auto normalize(basic_fp<F> value) -> basic_fp<F> {
  1437. // Handle subnormals.
  1438. const auto implicit_bit = F(1) << num_significand_bits<double>();
  1439. const auto shifted_implicit_bit = implicit_bit << SHIFT;
  1440. while ((value.f & shifted_implicit_bit) == 0) {
  1441. value.f <<= 1;
  1442. --value.e;
  1443. }
  1444. // Subtract 1 to account for hidden bit.
  1445. const auto offset = basic_fp<F>::num_significand_bits -
  1446. num_significand_bits<double>() - SHIFT - 1;
  1447. value.f <<= offset;
  1448. value.e -= offset;
  1449. return value;
  1450. }
  1451. // Computes lhs * rhs / pow(2, 64) rounded to nearest with half-up tie breaking.
  1452. FMT_CONSTEXPR inline auto multiply(uint64_t lhs, uint64_t rhs) -> uint64_t {
  1453. #if FMT_USE_INT128
  1454. auto product = static_cast<__uint128_t>(lhs) * rhs;
  1455. auto f = static_cast<uint64_t>(product >> 64);
  1456. return (static_cast<uint64_t>(product) & (1ULL << 63)) != 0 ? f + 1 : f;
  1457. #else
  1458. // Multiply 32-bit parts of significands.
  1459. uint64_t mask = (1ULL << 32) - 1;
  1460. uint64_t a = lhs >> 32, b = lhs & mask;
  1461. uint64_t c = rhs >> 32, d = rhs & mask;
  1462. uint64_t ac = a * c, bc = b * c, ad = a * d, bd = b * d;
  1463. // Compute mid 64-bit of result and round.
  1464. uint64_t mid = (bd >> 32) + (ad & mask) + (bc & mask) + (1U << 31);
  1465. return ac + (ad >> 32) + (bc >> 32) + (mid >> 32);
  1466. #endif
  1467. }
  1468. FMT_CONSTEXPR inline auto operator*(fp x, fp y) -> fp {
  1469. return {multiply(x.f, y.f), x.e + y.e + 64};
  1470. }
  1471. template <typename T, bool doublish = num_bits<T>() == num_bits<double>()>
  1472. using convert_float_result =
  1473. conditional_t<std::is_same<T, float>::value || doublish, double, T>;
  1474. template <typename T>
  1475. constexpr auto convert_float(T value) -> convert_float_result<T> {
  1476. return static_cast<convert_float_result<T>>(value);
  1477. }
  1478. template <bool C, typename T, typename F, FMT_ENABLE_IF(C)>
  1479. auto select(T true_value, F) -> T {
  1480. return true_value;
  1481. }
  1482. template <bool C, typename T, typename F, FMT_ENABLE_IF(!C)>
  1483. auto select(T, F false_value) -> F {
  1484. return false_value;
  1485. }
  1486. template <typename Char, typename OutputIt>
  1487. FMT_CONSTEXPR FMT_NOINLINE auto fill(OutputIt it, size_t n,
  1488. const basic_specs& specs) -> OutputIt {
  1489. auto fill_size = specs.fill_size();
  1490. if (fill_size == 1) return detail::fill_n(it, n, specs.fill_unit<Char>());
  1491. if (const Char* data = specs.fill<Char>()) {
  1492. for (size_t i = 0; i < n; ++i) it = copy<Char>(data, data + fill_size, it);
  1493. }
  1494. return it;
  1495. }
  1496. // Writes the output of f, padded according to format specifications in specs.
  1497. // size: output size in code units.
  1498. // width: output display width in (terminal) column positions.
  1499. template <typename Char, align default_align = align::left, typename OutputIt,
  1500. typename F>
  1501. FMT_CONSTEXPR auto write_padded(OutputIt out, const format_specs& specs,
  1502. size_t size, size_t width, F&& f) -> OutputIt {
  1503. static_assert(default_align == align::left || default_align == align::right,
  1504. "");
  1505. unsigned spec_width = to_unsigned(specs.width);
  1506. size_t padding = spec_width > width ? spec_width - width : 0;
  1507. // Shifts are encoded as string literals because static constexpr is not
  1508. // supported in constexpr functions.
  1509. auto* shifts =
  1510. default_align == align::left ? "\x1f\x1f\x00\x01" : "\x00\x1f\x00\x01";
  1511. size_t left_padding = padding >> shifts[static_cast<int>(specs.align())];
  1512. size_t right_padding = padding - left_padding;
  1513. auto it = reserve(out, size + padding * specs.fill_size());
  1514. if (left_padding != 0) it = fill<Char>(it, left_padding, specs);
  1515. it = f(it);
  1516. if (right_padding != 0) it = fill<Char>(it, right_padding, specs);
  1517. return base_iterator(out, it);
  1518. }
  1519. template <typename Char, align default_align = align::left, typename OutputIt,
  1520. typename F>
  1521. constexpr auto write_padded(OutputIt out, const format_specs& specs,
  1522. size_t size, F&& f) -> OutputIt {
  1523. return write_padded<Char, default_align>(out, specs, size, size, f);
  1524. }
  1525. template <typename Char, align default_align = align::left, typename OutputIt>
  1526. FMT_CONSTEXPR auto write_bytes(OutputIt out, string_view bytes,
  1527. const format_specs& specs = {}) -> OutputIt {
  1528. return write_padded<Char, default_align>(
  1529. out, specs, bytes.size(), [bytes](reserve_iterator<OutputIt> it) {
  1530. const char* data = bytes.data();
  1531. return copy<Char>(data, data + bytes.size(), it);
  1532. });
  1533. }
  1534. template <typename Char, typename OutputIt, typename UIntPtr>
  1535. auto write_ptr(OutputIt out, UIntPtr value, const format_specs* specs)
  1536. -> OutputIt {
  1537. int num_digits = count_digits<4>(value);
  1538. auto size = to_unsigned(num_digits) + size_t(2);
  1539. auto write = [=](reserve_iterator<OutputIt> it) {
  1540. *it++ = static_cast<Char>('0');
  1541. *it++ = static_cast<Char>('x');
  1542. return format_base2e<Char>(4, it, value, num_digits);
  1543. };
  1544. return specs ? write_padded<Char, align::right>(out, *specs, size, write)
  1545. : base_iterator(out, write(reserve(out, size)));
  1546. }
  1547. // Returns true iff the code point cp is printable.
  1548. FMT_API auto is_printable(uint32_t cp) -> bool;
  1549. inline auto needs_escape(uint32_t cp) -> bool {
  1550. if (cp < 0x20 || cp == 0x7f || cp == '"' || cp == '\\') return true;
  1551. if (const_check(FMT_OPTIMIZE_SIZE > 1)) return false;
  1552. return !is_printable(cp);
  1553. }
  1554. template <typename Char> struct find_escape_result {
  1555. const Char* begin;
  1556. const Char* end;
  1557. uint32_t cp;
  1558. };
  1559. template <typename Char>
  1560. auto find_escape(const Char* begin, const Char* end)
  1561. -> find_escape_result<Char> {
  1562. for (; begin != end; ++begin) {
  1563. uint32_t cp = static_cast<unsigned_char<Char>>(*begin);
  1564. if (const_check(sizeof(Char) == 1) && cp >= 0x80) continue;
  1565. if (needs_escape(cp)) return {begin, begin + 1, cp};
  1566. }
  1567. return {begin, nullptr, 0};
  1568. }
  1569. inline auto find_escape(const char* begin, const char* end)
  1570. -> find_escape_result<char> {
  1571. if (const_check(!use_utf8)) return find_escape<char>(begin, end);
  1572. auto result = find_escape_result<char>{end, nullptr, 0};
  1573. for_each_codepoint(string_view(begin, to_unsigned(end - begin)),
  1574. [&](uint32_t cp, string_view sv) {
  1575. if (needs_escape(cp)) {
  1576. result = {sv.begin(), sv.end(), cp};
  1577. return false;
  1578. }
  1579. return true;
  1580. });
  1581. return result;
  1582. }
  1583. template <size_t width, typename Char, typename OutputIt>
  1584. auto write_codepoint(OutputIt out, char prefix, uint32_t cp) -> OutputIt {
  1585. *out++ = static_cast<Char>('\\');
  1586. *out++ = static_cast<Char>(prefix);
  1587. Char buf[width];
  1588. fill_n(buf, width, static_cast<Char>('0'));
  1589. format_base2e(4, buf, cp, width);
  1590. return copy<Char>(buf, buf + width, out);
  1591. }
  1592. template <typename OutputIt, typename Char>
  1593. auto write_escaped_cp(OutputIt out, const find_escape_result<Char>& escape)
  1594. -> OutputIt {
  1595. auto c = static_cast<Char>(escape.cp);
  1596. switch (escape.cp) {
  1597. case '\n':
  1598. *out++ = static_cast<Char>('\\');
  1599. c = static_cast<Char>('n');
  1600. break;
  1601. case '\r':
  1602. *out++ = static_cast<Char>('\\');
  1603. c = static_cast<Char>('r');
  1604. break;
  1605. case '\t':
  1606. *out++ = static_cast<Char>('\\');
  1607. c = static_cast<Char>('t');
  1608. break;
  1609. case '"': FMT_FALLTHROUGH;
  1610. case '\'': FMT_FALLTHROUGH;
  1611. case '\\': *out++ = static_cast<Char>('\\'); break;
  1612. default:
  1613. if (escape.cp < 0x100) return write_codepoint<2, Char>(out, 'x', escape.cp);
  1614. if (escape.cp < 0x10000)
  1615. return write_codepoint<4, Char>(out, 'u', escape.cp);
  1616. if (escape.cp < 0x110000)
  1617. return write_codepoint<8, Char>(out, 'U', escape.cp);
  1618. for (Char escape_char : basic_string_view<Char>(
  1619. escape.begin, to_unsigned(escape.end - escape.begin))) {
  1620. out = write_codepoint<2, Char>(out, 'x',
  1621. static_cast<uint32_t>(escape_char) & 0xFF);
  1622. }
  1623. return out;
  1624. }
  1625. *out++ = c;
  1626. return out;
  1627. }
  1628. template <typename Char, typename OutputIt>
  1629. auto write_escaped_string(OutputIt out, basic_string_view<Char> str)
  1630. -> OutputIt {
  1631. *out++ = static_cast<Char>('"');
  1632. auto begin = str.begin(), end = str.end();
  1633. do {
  1634. auto escape = find_escape(begin, end);
  1635. out = copy<Char>(begin, escape.begin, out);
  1636. begin = escape.end;
  1637. if (!begin) break;
  1638. out = write_escaped_cp<OutputIt, Char>(out, escape);
  1639. } while (begin != end);
  1640. *out++ = static_cast<Char>('"');
  1641. return out;
  1642. }
  1643. template <typename Char, typename OutputIt>
  1644. auto write_escaped_char(OutputIt out, Char v) -> OutputIt {
  1645. Char v_array[1] = {v};
  1646. *out++ = static_cast<Char>('\'');
  1647. if ((needs_escape(static_cast<uint32_t>(v)) && v != static_cast<Char>('"')) ||
  1648. v == static_cast<Char>('\'')) {
  1649. out = write_escaped_cp(out,
  1650. find_escape_result<Char>{v_array, v_array + 1,
  1651. static_cast<uint32_t>(v)});
  1652. } else {
  1653. *out++ = v;
  1654. }
  1655. *out++ = static_cast<Char>('\'');
  1656. return out;
  1657. }
  1658. template <typename Char, typename OutputIt>
  1659. FMT_CONSTEXPR auto write_char(OutputIt out, Char value,
  1660. const format_specs& specs) -> OutputIt {
  1661. bool is_debug = specs.type() == presentation_type::debug;
  1662. return write_padded<Char>(out, specs, 1, [=](reserve_iterator<OutputIt> it) {
  1663. if (is_debug) return write_escaped_char(it, value);
  1664. *it++ = value;
  1665. return it;
  1666. });
  1667. }
  1668. template <typename Char> class digit_grouping {
  1669. private:
  1670. std::string grouping_;
  1671. std::basic_string<Char> thousands_sep_;
  1672. struct next_state {
  1673. std::string::const_iterator group;
  1674. int pos;
  1675. };
  1676. auto initial_state() const -> next_state { return {grouping_.begin(), 0}; }
  1677. // Returns the next digit group separator position.
  1678. auto next(next_state& state) const -> int {
  1679. if (thousands_sep_.empty()) return max_value<int>();
  1680. if (state.group == grouping_.end()) return state.pos += grouping_.back();
  1681. if (*state.group <= 0 || *state.group == max_value<char>())
  1682. return max_value<int>();
  1683. state.pos += *state.group++;
  1684. return state.pos;
  1685. }
  1686. public:
  1687. explicit digit_grouping(locale_ref loc, bool localized = true) {
  1688. if (!localized) return;
  1689. auto sep = thousands_sep<Char>(loc);
  1690. grouping_ = sep.grouping;
  1691. if (sep.thousands_sep) thousands_sep_.assign(1, sep.thousands_sep);
  1692. }
  1693. digit_grouping(std::string grouping, std::basic_string<Char> sep)
  1694. : grouping_(std::move(grouping)), thousands_sep_(std::move(sep)) {}
  1695. auto has_separator() const -> bool { return !thousands_sep_.empty(); }
  1696. auto count_separators(int num_digits) const -> int {
  1697. int count = 0;
  1698. auto state = initial_state();
  1699. while (num_digits > next(state)) ++count;
  1700. return count;
  1701. }
  1702. // Applies grouping to digits and writes the output to out.
  1703. template <typename Out, typename C>
  1704. auto apply(Out out, basic_string_view<C> digits) const -> Out {
  1705. auto num_digits = static_cast<int>(digits.size());
  1706. auto separators = basic_memory_buffer<int>();
  1707. separators.push_back(0);
  1708. auto state = initial_state();
  1709. while (int i = next(state)) {
  1710. if (i >= num_digits) break;
  1711. separators.push_back(i);
  1712. }
  1713. for (int i = 0, sep_index = static_cast<int>(separators.size() - 1);
  1714. i < num_digits; ++i) {
  1715. if (num_digits - i == separators[sep_index]) {
  1716. out = copy<Char>(thousands_sep_.data(),
  1717. thousands_sep_.data() + thousands_sep_.size(), out);
  1718. --sep_index;
  1719. }
  1720. *out++ = static_cast<Char>(digits[to_unsigned(i)]);
  1721. }
  1722. return out;
  1723. }
  1724. };
  1725. FMT_CONSTEXPR inline void prefix_append(unsigned& prefix, unsigned value) {
  1726. prefix |= prefix != 0 ? value << 8 : value;
  1727. prefix += (1u + (value > 0xff ? 1 : 0)) << 24;
  1728. }
  1729. // Writes a decimal integer with digit grouping.
  1730. template <typename OutputIt, typename UInt, typename Char>
  1731. auto write_int(OutputIt out, UInt value, unsigned prefix,
  1732. const format_specs& specs, const digit_grouping<Char>& grouping)
  1733. -> OutputIt {
  1734. static_assert(std::is_same<uint64_or_128_t<UInt>, UInt>::value, "");
  1735. int num_digits = 0;
  1736. auto buffer = memory_buffer();
  1737. switch (specs.type()) {
  1738. default: FMT_ASSERT(false, ""); FMT_FALLTHROUGH;
  1739. case presentation_type::none:
  1740. case presentation_type::dec:
  1741. num_digits = count_digits(value);
  1742. format_decimal<char>(appender(buffer), value, num_digits);
  1743. break;
  1744. case presentation_type::hex:
  1745. if (specs.alt())
  1746. prefix_append(prefix, unsigned(specs.upper() ? 'X' : 'x') << 8 | '0');
  1747. num_digits = count_digits<4>(value);
  1748. format_base2e<char>(4, appender(buffer), value, num_digits, specs.upper());
  1749. break;
  1750. case presentation_type::oct:
  1751. num_digits = count_digits<3>(value);
  1752. // Octal prefix '0' is counted as a digit, so only add it if precision
  1753. // is not greater than the number of digits.
  1754. if (specs.alt() && specs.precision <= num_digits && value != 0)
  1755. prefix_append(prefix, '0');
  1756. format_base2e<char>(3, appender(buffer), value, num_digits);
  1757. break;
  1758. case presentation_type::bin:
  1759. if (specs.alt())
  1760. prefix_append(prefix, unsigned(specs.upper() ? 'B' : 'b') << 8 | '0');
  1761. num_digits = count_digits<1>(value);
  1762. format_base2e<char>(1, appender(buffer), value, num_digits);
  1763. break;
  1764. case presentation_type::chr:
  1765. return write_char<Char>(out, static_cast<Char>(value), specs);
  1766. }
  1767. unsigned size = (prefix != 0 ? prefix >> 24 : 0) + to_unsigned(num_digits) +
  1768. to_unsigned(grouping.count_separators(num_digits));
  1769. return write_padded<Char, align::right>(
  1770. out, specs, size, size, [&](reserve_iterator<OutputIt> it) {
  1771. for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8)
  1772. *it++ = static_cast<Char>(p & 0xff);
  1773. return grouping.apply(it, string_view(buffer.data(), buffer.size()));
  1774. });
  1775. }
  1776. #if FMT_USE_LOCALE
  1777. // Writes a localized value.
  1778. FMT_API auto write_loc(appender out, loc_value value, const format_specs& specs,
  1779. locale_ref loc) -> bool;
  1780. auto write_loc(basic_appender<wchar_t> out, loc_value value,
  1781. const format_specs& specs, locale_ref loc) -> bool;
  1782. #endif
  1783. template <typename OutputIt>
  1784. inline auto write_loc(OutputIt, const loc_value&, const format_specs&,
  1785. locale_ref) -> bool {
  1786. return false;
  1787. }
  1788. template <typename UInt> struct write_int_arg {
  1789. UInt abs_value;
  1790. unsigned prefix;
  1791. };
  1792. template <typename T>
  1793. FMT_CONSTEXPR auto make_write_int_arg(T value, sign s)
  1794. -> write_int_arg<uint32_or_64_or_128_t<T>> {
  1795. auto prefix = 0u;
  1796. auto abs_value = static_cast<uint32_or_64_or_128_t<T>>(value);
  1797. if (is_negative(value)) {
  1798. prefix = 0x01000000 | '-';
  1799. abs_value = 0 - abs_value;
  1800. } else {
  1801. constexpr unsigned prefixes[4] = {0, 0, 0x1000000u | '+', 0x1000000u | ' '};
  1802. prefix = prefixes[static_cast<int>(s)];
  1803. }
  1804. return {abs_value, prefix};
  1805. }
  1806. template <typename Char = char> struct loc_writer {
  1807. basic_appender<Char> out;
  1808. const format_specs& specs;
  1809. std::basic_string<Char> sep;
  1810. std::string grouping;
  1811. std::basic_string<Char> decimal_point;
  1812. template <typename T, FMT_ENABLE_IF(is_integer<T>::value)>
  1813. auto operator()(T value) -> bool {
  1814. auto arg = make_write_int_arg(value, specs.sign());
  1815. write_int(out, static_cast<uint64_or_128_t<T>>(arg.abs_value), arg.prefix,
  1816. specs, digit_grouping<Char>(grouping, sep));
  1817. return true;
  1818. }
  1819. template <typename T, FMT_ENABLE_IF(!is_integer<T>::value)>
  1820. auto operator()(T) -> bool {
  1821. return false;
  1822. }
  1823. };
  1824. // Size and padding computation separate from write_int to avoid template bloat.
  1825. struct size_padding {
  1826. unsigned size;
  1827. unsigned padding;
  1828. FMT_CONSTEXPR size_padding(int num_digits, unsigned prefix,
  1829. const format_specs& specs)
  1830. : size((prefix >> 24) + to_unsigned(num_digits)), padding(0) {
  1831. if (specs.align() == align::numeric) {
  1832. auto width = to_unsigned(specs.width);
  1833. if (width > size) {
  1834. padding = width - size;
  1835. size = width;
  1836. }
  1837. } else if (specs.precision > num_digits) {
  1838. size = (prefix >> 24) + to_unsigned(specs.precision);
  1839. padding = to_unsigned(specs.precision - num_digits);
  1840. }
  1841. }
  1842. };
  1843. template <typename Char, typename OutputIt, typename T>
  1844. FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg<T> arg,
  1845. const format_specs& specs) -> OutputIt {
  1846. static_assert(std::is_same<T, uint32_or_64_or_128_t<T>>::value, "");
  1847. constexpr size_t buffer_size = num_bits<T>();
  1848. char buffer[buffer_size];
  1849. if (is_constant_evaluated()) fill_n(buffer, buffer_size, '\0');
  1850. const char* begin = nullptr;
  1851. const char* end = buffer + buffer_size;
  1852. auto abs_value = arg.abs_value;
  1853. auto prefix = arg.prefix;
  1854. switch (specs.type()) {
  1855. default: FMT_ASSERT(false, ""); FMT_FALLTHROUGH;
  1856. case presentation_type::none:
  1857. case presentation_type::dec:
  1858. begin = do_format_decimal(buffer, abs_value, buffer_size);
  1859. break;
  1860. case presentation_type::hex:
  1861. begin = do_format_base2e(4, buffer, abs_value, buffer_size, specs.upper());
  1862. if (specs.alt())
  1863. prefix_append(prefix, unsigned(specs.upper() ? 'X' : 'x') << 8 | '0');
  1864. break;
  1865. case presentation_type::oct: {
  1866. begin = do_format_base2e(3, buffer, abs_value, buffer_size);
  1867. // Octal prefix '0' is counted as a digit, so only add it if precision
  1868. // is not greater than the number of digits.
  1869. auto num_digits = end - begin;
  1870. if (specs.alt() && specs.precision <= num_digits && abs_value != 0)
  1871. prefix_append(prefix, '0');
  1872. break;
  1873. }
  1874. case presentation_type::bin:
  1875. begin = do_format_base2e(1, buffer, abs_value, buffer_size);
  1876. if (specs.alt())
  1877. prefix_append(prefix, unsigned(specs.upper() ? 'B' : 'b') << 8 | '0');
  1878. break;
  1879. case presentation_type::chr:
  1880. return write_char<Char>(out, static_cast<Char>(abs_value), specs);
  1881. }
  1882. // Write an integer in the format
  1883. // <left-padding><prefix><numeric-padding><digits><right-padding>
  1884. // prefix contains chars in three lower bytes and the size in the fourth byte.
  1885. int num_digits = static_cast<int>(end - begin);
  1886. // Slightly faster check for specs.width == 0 && specs.precision == -1.
  1887. if ((specs.width | (specs.precision + 1)) == 0) {
  1888. auto it = reserve(out, to_unsigned(num_digits) + (prefix >> 24));
  1889. for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8)
  1890. *it++ = static_cast<Char>(p & 0xff);
  1891. return base_iterator(out, copy<Char>(begin, end, it));
  1892. }
  1893. auto sp = size_padding(num_digits, prefix, specs);
  1894. unsigned padding = sp.padding;
  1895. return write_padded<Char, align::right>(
  1896. out, specs, sp.size, [=](reserve_iterator<OutputIt> it) {
  1897. for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8)
  1898. *it++ = static_cast<Char>(p & 0xff);
  1899. it = detail::fill_n(it, padding, static_cast<Char>('0'));
  1900. return copy<Char>(begin, end, it);
  1901. });
  1902. }
  1903. template <typename Char, typename OutputIt, typename T>
  1904. FMT_CONSTEXPR FMT_NOINLINE auto write_int_noinline(OutputIt out,
  1905. write_int_arg<T> arg,
  1906. const format_specs& specs)
  1907. -> OutputIt {
  1908. return write_int<Char>(out, arg, specs);
  1909. }
  1910. template <typename Char, typename T,
  1911. FMT_ENABLE_IF(is_integral<T>::value &&
  1912. !std::is_same<T, bool>::value &&
  1913. !std::is_same<T, Char>::value)>
  1914. FMT_CONSTEXPR FMT_INLINE auto write(basic_appender<Char> out, T value,
  1915. const format_specs& specs, locale_ref loc)
  1916. -> basic_appender<Char> {
  1917. if (specs.localized() && write_loc(out, value, specs, loc)) return out;
  1918. return write_int_noinline<Char>(out, make_write_int_arg(value, specs.sign()),
  1919. specs);
  1920. }
  1921. // An inlined version of write used in format string compilation.
  1922. template <typename Char, typename OutputIt, typename T,
  1923. FMT_ENABLE_IF(is_integral<T>::value &&
  1924. !std::is_same<T, bool>::value &&
  1925. !std::is_same<T, Char>::value &&
  1926. !std::is_same<OutputIt, basic_appender<Char>>::value)>
  1927. FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value,
  1928. const format_specs& specs, locale_ref loc)
  1929. -> OutputIt {
  1930. if (specs.localized() && write_loc(out, value, specs, loc)) return out;
  1931. return write_int<Char>(out, make_write_int_arg(value, specs.sign()), specs);
  1932. }
  1933. template <typename Char, typename OutputIt>
  1934. FMT_CONSTEXPR auto write(OutputIt out, Char value, const format_specs& specs,
  1935. locale_ref loc = {}) -> OutputIt {
  1936. // char is formatted as unsigned char for consistency across platforms.
  1937. using unsigned_type =
  1938. conditional_t<std::is_same<Char, char>::value, unsigned char, unsigned>;
  1939. return check_char_specs(specs)
  1940. ? write_char<Char>(out, value, specs)
  1941. : write<Char>(out, static_cast<unsigned_type>(value), specs, loc);
  1942. }
  1943. template <typename Char, typename OutputIt,
  1944. FMT_ENABLE_IF(std::is_same<Char, char>::value)>
  1945. FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
  1946. const format_specs& specs) -> OutputIt {
  1947. bool is_debug = specs.type() == presentation_type::debug;
  1948. if (specs.precision < 0 && specs.width == 0) {
  1949. auto&& it = reserve(out, s.size());
  1950. return is_debug ? write_escaped_string(it, s) : copy<char>(s, it);
  1951. }
  1952. size_t display_width_limit =
  1953. specs.precision < 0 ? SIZE_MAX : to_unsigned(specs.precision);
  1954. size_t display_width =
  1955. !is_debug || specs.precision == 0 ? 0 : 1; // Account for opening '"'.
  1956. size_t size = !is_debug || specs.precision == 0 ? 0 : 1;
  1957. for_each_codepoint(s, [&](uint32_t cp, string_view sv) {
  1958. if (is_debug && needs_escape(cp)) {
  1959. counting_buffer<char> buf;
  1960. write_escaped_cp(basic_appender<char>(buf),
  1961. find_escape_result<char>{sv.begin(), sv.end(), cp});
  1962. // We're reinterpreting bytes as display width. That's okay
  1963. // because write_escaped_cp() only writes ASCII characters.
  1964. size_t cp_width = buf.count();
  1965. if (display_width + cp_width <= display_width_limit) {
  1966. display_width += cp_width;
  1967. size += cp_width;
  1968. // If this is the end of the string, account for closing '"'.
  1969. if (display_width < display_width_limit && sv.end() == s.end()) {
  1970. ++display_width;
  1971. ++size;
  1972. }
  1973. return true;
  1974. }
  1975. size += display_width_limit - display_width;
  1976. display_width = display_width_limit;
  1977. return false;
  1978. }
  1979. size_t cp_width = display_width_of(cp);
  1980. if (cp_width + display_width <= display_width_limit) {
  1981. display_width += cp_width;
  1982. size += sv.size();
  1983. // If this is the end of the string, account for closing '"'.
  1984. if (is_debug && display_width < display_width_limit &&
  1985. sv.end() == s.end()) {
  1986. ++display_width;
  1987. ++size;
  1988. }
  1989. return true;
  1990. }
  1991. return false;
  1992. });
  1993. struct bounded_output_iterator {
  1994. reserve_iterator<OutputIt> underlying_iterator;
  1995. size_t bound;
  1996. FMT_CONSTEXPR auto operator*() -> bounded_output_iterator& { return *this; }
  1997. FMT_CONSTEXPR auto operator++() -> bounded_output_iterator& {
  1998. return *this;
  1999. }
  2000. FMT_CONSTEXPR auto operator++(int) -> bounded_output_iterator& {
  2001. return *this;
  2002. }
  2003. FMT_CONSTEXPR auto operator=(char c) -> bounded_output_iterator& {
  2004. if (bound > 0) {
  2005. *underlying_iterator++ = c;
  2006. --bound;
  2007. }
  2008. return *this;
  2009. }
  2010. };
  2011. return write_padded<char>(
  2012. out, specs, size, display_width, [=](reserve_iterator<OutputIt> it) {
  2013. return is_debug
  2014. ? write_escaped_string(bounded_output_iterator{it, size}, s)
  2015. .underlying_iterator
  2016. : copy<char>(s.data(), s.data() + size, it);
  2017. });
  2018. }
  2019. template <typename Char, typename OutputIt,
  2020. FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
  2021. FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
  2022. const format_specs& specs) -> OutputIt {
  2023. auto data = s.data();
  2024. auto size = s.size();
  2025. if (specs.precision >= 0 && to_unsigned(specs.precision) < size)
  2026. size = to_unsigned(specs.precision);
  2027. bool is_debug = specs.type() == presentation_type::debug;
  2028. if (is_debug) {
  2029. auto buf = counting_buffer<Char>();
  2030. write_escaped_string(basic_appender<Char>(buf), s);
  2031. size = buf.count();
  2032. }
  2033. return write_padded<Char>(
  2034. out, specs, size, [=](reserve_iterator<OutputIt> it) {
  2035. return is_debug ? write_escaped_string(it, s)
  2036. : copy<Char>(data, data + size, it);
  2037. });
  2038. }
  2039. template <typename Char, typename OutputIt>
  2040. FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
  2041. const format_specs& specs, locale_ref) -> OutputIt {
  2042. return write<Char>(out, s, specs);
  2043. }
  2044. template <typename Char, typename OutputIt>
  2045. FMT_CONSTEXPR auto write(OutputIt out, const Char* s, const format_specs& specs,
  2046. locale_ref) -> OutputIt {
  2047. if (specs.type() == presentation_type::pointer)
  2048. return write_ptr<Char>(out, bit_cast<uintptr_t>(s), &specs);
  2049. if (!s) report_error("string pointer is null");
  2050. return write<Char>(out, basic_string_view<Char>(s), specs, {});
  2051. }
  2052. template <typename Char, typename OutputIt, typename T,
  2053. FMT_ENABLE_IF(is_integral<T>::value &&
  2054. !std::is_same<T, bool>::value &&
  2055. !std::is_same<T, Char>::value)>
  2056. FMT_CONSTEXPR auto write(OutputIt out, T value) -> OutputIt {
  2057. auto abs_value = static_cast<uint32_or_64_or_128_t<T>>(value);
  2058. bool negative = is_negative(value);
  2059. // Don't do -abs_value since it trips unsigned-integer-overflow sanitizer.
  2060. if (negative) abs_value = ~abs_value + 1;
  2061. int num_digits = count_digits(abs_value);
  2062. auto size = (negative ? 1 : 0) + static_cast<size_t>(num_digits);
  2063. if (auto ptr = to_pointer<Char>(out, size)) {
  2064. if (negative) *ptr++ = static_cast<Char>('-');
  2065. format_decimal<Char>(ptr, abs_value, num_digits);
  2066. return out;
  2067. }
  2068. if (negative) *out++ = static_cast<Char>('-');
  2069. return format_decimal<Char>(out, abs_value, num_digits);
  2070. }
  2071. template <typename Char>
  2072. FMT_CONSTEXPR auto parse_align(const Char* begin, const Char* end,
  2073. format_specs& specs) -> const Char* {
  2074. FMT_ASSERT(begin != end, "");
  2075. auto alignment = align::none;
  2076. auto p = begin + code_point_length(begin);
  2077. if (end - p <= 0) p = begin;
  2078. for (;;) {
  2079. switch (to_ascii(*p)) {
  2080. case '<': alignment = align::left; break;
  2081. case '>': alignment = align::right; break;
  2082. case '^': alignment = align::center; break;
  2083. }
  2084. if (alignment != align::none) {
  2085. if (p != begin) {
  2086. auto c = *begin;
  2087. if (c == '}') return begin;
  2088. if (c == '{') {
  2089. report_error("invalid fill character '{'");
  2090. return begin;
  2091. }
  2092. specs.set_fill(basic_string_view<Char>(begin, to_unsigned(p - begin)));
  2093. begin = p + 1;
  2094. } else {
  2095. ++begin;
  2096. }
  2097. break;
  2098. } else if (p == begin) {
  2099. break;
  2100. }
  2101. p = begin;
  2102. }
  2103. specs.set_align(alignment);
  2104. return begin;
  2105. }
  2106. template <typename Char, typename OutputIt>
  2107. FMT_CONSTEXPR20 auto write_nonfinite(OutputIt out, bool isnan,
  2108. format_specs specs, sign s) -> OutputIt {
  2109. auto str =
  2110. isnan ? (specs.upper() ? "NAN" : "nan") : (specs.upper() ? "INF" : "inf");
  2111. constexpr size_t str_size = 3;
  2112. auto size = str_size + (s != sign::none ? 1 : 0);
  2113. // Replace '0'-padding with space for non-finite values.
  2114. const bool is_zero_fill =
  2115. specs.fill_size() == 1 && specs.fill_unit<Char>() == '0';
  2116. if (is_zero_fill) specs.set_fill(' ');
  2117. return write_padded<Char>(out, specs, size,
  2118. [=](reserve_iterator<OutputIt> it) {
  2119. if (s != sign::none)
  2120. *it++ = detail::getsign<Char>(s);
  2121. return copy<Char>(str, str + str_size, it);
  2122. });
  2123. }
  2124. // A decimal floating-point number significand * pow(10, exp).
  2125. struct big_decimal_fp {
  2126. const char* significand;
  2127. int significand_size;
  2128. int exponent;
  2129. };
  2130. constexpr auto get_significand_size(const big_decimal_fp& f) -> int {
  2131. return f.significand_size;
  2132. }
  2133. template <typename T>
  2134. inline auto get_significand_size(const dragonbox::decimal_fp<T>& f) -> int {
  2135. return count_digits(f.significand);
  2136. }
  2137. template <typename Char, typename OutputIt>
  2138. constexpr auto write_significand(OutputIt out, const char* significand,
  2139. int significand_size) -> OutputIt {
  2140. return copy<Char>(significand, significand + significand_size, out);
  2141. }
  2142. template <typename Char, typename OutputIt, typename UInt>
  2143. inline auto write_significand(OutputIt out, UInt significand,
  2144. int significand_size) -> OutputIt {
  2145. return format_decimal<Char>(out, significand, significand_size);
  2146. }
  2147. template <typename Char, typename OutputIt, typename T, typename Grouping>
  2148. FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand,
  2149. int significand_size, int exponent,
  2150. const Grouping& grouping) -> OutputIt {
  2151. if (!grouping.has_separator()) {
  2152. out = write_significand<Char>(out, significand, significand_size);
  2153. return detail::fill_n(out, exponent, static_cast<Char>('0'));
  2154. }
  2155. auto buffer = memory_buffer();
  2156. write_significand<char>(appender(buffer), significand, significand_size);
  2157. detail::fill_n(appender(buffer), exponent, '0');
  2158. return grouping.apply(out, string_view(buffer.data(), buffer.size()));
  2159. }
  2160. template <typename Char, typename UInt,
  2161. FMT_ENABLE_IF(std::is_integral<UInt>::value)>
  2162. inline auto write_significand(Char* out, UInt significand, int significand_size,
  2163. int integral_size, Char decimal_point) -> Char* {
  2164. if (!decimal_point) return format_decimal(out, significand, significand_size);
  2165. out += significand_size + 1;
  2166. Char* end = out;
  2167. int floating_size = significand_size - integral_size;
  2168. for (int i = floating_size / 2; i > 0; --i) {
  2169. out -= 2;
  2170. write2digits(out, static_cast<size_t>(significand % 100));
  2171. significand /= 100;
  2172. }
  2173. if (floating_size % 2 != 0) {
  2174. *--out = static_cast<Char>('0' + significand % 10);
  2175. significand /= 10;
  2176. }
  2177. *--out = decimal_point;
  2178. format_decimal(out - integral_size, significand, integral_size);
  2179. return end;
  2180. }
  2181. template <typename OutputIt, typename UInt, typename Char,
  2182. FMT_ENABLE_IF(!std::is_pointer<remove_cvref_t<OutputIt>>::value)>
  2183. inline auto write_significand(OutputIt out, UInt significand,
  2184. int significand_size, int integral_size,
  2185. Char decimal_point) -> OutputIt {
  2186. // Buffer is large enough to hold digits (digits10 + 1) and a decimal point.
  2187. Char buffer[digits10<UInt>() + 2];
  2188. auto end = write_significand(buffer, significand, significand_size,
  2189. integral_size, decimal_point);
  2190. return detail::copy_noinline<Char>(buffer, end, out);
  2191. }
  2192. template <typename OutputIt, typename Char>
  2193. FMT_CONSTEXPR auto write_significand(OutputIt out, const char* significand,
  2194. int significand_size, int integral_size,
  2195. Char decimal_point) -> OutputIt {
  2196. out = detail::copy_noinline<Char>(significand, significand + integral_size,
  2197. out);
  2198. if (!decimal_point) return out;
  2199. *out++ = decimal_point;
  2200. return detail::copy_noinline<Char>(significand + integral_size,
  2201. significand + significand_size, out);
  2202. }
  2203. template <typename OutputIt, typename Char, typename T, typename Grouping>
  2204. FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand,
  2205. int significand_size, int integral_size,
  2206. Char decimal_point,
  2207. const Grouping& grouping) -> OutputIt {
  2208. if (!grouping.has_separator()) {
  2209. return write_significand(out, significand, significand_size, integral_size,
  2210. decimal_point);
  2211. }
  2212. auto buffer = basic_memory_buffer<Char>();
  2213. write_significand(basic_appender<Char>(buffer), significand, significand_size,
  2214. integral_size, decimal_point);
  2215. grouping.apply(
  2216. out, basic_string_view<Char>(buffer.data(), to_unsigned(integral_size)));
  2217. return detail::copy_noinline<Char>(buffer.data() + integral_size,
  2218. buffer.end(), out);
  2219. }
  2220. // Numbers with exponents greater or equal to the returned value will use
  2221. // the exponential notation.
  2222. template <typename T> FMT_CONSTEVAL auto exp_upper() -> int {
  2223. return std::numeric_limits<T>::digits10 != 0
  2224. ? min_of(16, std::numeric_limits<T>::digits10 + 1)
  2225. : 16;
  2226. }
  2227. // Use the fixed notation if the exponent is in [-4, exp_upper),
  2228. // e.g. 0.0001 instead of 1e-04. Otherwise use the exponent notation.
  2229. constexpr auto use_fixed(int exp, int exp_upper) -> bool {
  2230. return exp >= -4 && exp < exp_upper;
  2231. }
  2232. template <typename Char> class fallback_digit_grouping {
  2233. public:
  2234. constexpr fallback_digit_grouping(locale_ref, bool) {}
  2235. constexpr auto has_separator() const -> bool { return false; }
  2236. constexpr auto count_separators(int) const -> int { return 0; }
  2237. template <typename Out, typename C>
  2238. constexpr auto apply(Out out, basic_string_view<C>) const -> Out {
  2239. return out;
  2240. }
  2241. };
  2242. template <typename Char, typename Grouping, typename OutputIt,
  2243. typename DecimalFP>
  2244. FMT_CONSTEXPR20 auto write_fixed(OutputIt out, const DecimalFP& f,
  2245. int significand_size, Char decimal_point,
  2246. const format_specs& specs, sign s,
  2247. locale_ref loc = {}) -> OutputIt {
  2248. using iterator = reserve_iterator<OutputIt>;
  2249. int exp = f.exponent + significand_size;
  2250. long long size = significand_size + (s != sign::none ? 1 : 0);
  2251. if (f.exponent >= 0) {
  2252. // 1234e5 -> 123400000[.0+]
  2253. size += f.exponent;
  2254. int num_zeros = specs.precision - exp;
  2255. abort_fuzzing_if(num_zeros > 5000);
  2256. if (specs.alt()) {
  2257. ++size;
  2258. if (num_zeros <= 0 && specs.type() != presentation_type::fixed)
  2259. num_zeros = 0;
  2260. if (num_zeros > 0) size += num_zeros;
  2261. }
  2262. auto grouping = Grouping(loc, specs.localized());
  2263. size += grouping.count_separators(exp);
  2264. return write_padded<Char, align::right>(
  2265. out, specs, static_cast<size_t>(size), [&](iterator it) {
  2266. if (s != sign::none) *it++ = detail::getsign<Char>(s);
  2267. it = write_significand<Char>(it, f.significand, significand_size,
  2268. f.exponent, grouping);
  2269. if (!specs.alt()) return it;
  2270. *it++ = decimal_point;
  2271. return num_zeros > 0 ? detail::fill_n(it, num_zeros, Char('0')) : it;
  2272. });
  2273. }
  2274. if (exp > 0) {
  2275. // 1234e-2 -> 12.34[0+]
  2276. int num_zeros = specs.alt() ? specs.precision - significand_size : 0;
  2277. size += 1 + max_of(num_zeros, 0);
  2278. auto grouping = Grouping(loc, specs.localized());
  2279. size += grouping.count_separators(exp);
  2280. return write_padded<Char, align::right>(
  2281. out, specs, to_unsigned(size), [&](iterator it) {
  2282. if (s != sign::none) *it++ = detail::getsign<Char>(s);
  2283. it = write_significand(it, f.significand, significand_size, exp,
  2284. decimal_point, grouping);
  2285. return num_zeros > 0 ? detail::fill_n(it, num_zeros, Char('0')) : it;
  2286. });
  2287. }
  2288. // 1234e-6 -> 0.001234
  2289. int num_zeros = -exp;
  2290. if (significand_size == 0 && specs.precision >= 0 &&
  2291. specs.precision < num_zeros) {
  2292. num_zeros = specs.precision;
  2293. }
  2294. bool pointy = num_zeros != 0 || significand_size != 0 || specs.alt();
  2295. size += 1 + (pointy ? 1 : 0) + num_zeros;
  2296. return write_padded<Char, align::right>(
  2297. out, specs, to_unsigned(size), [&](iterator it) {
  2298. if (s != sign::none) *it++ = detail::getsign<Char>(s);
  2299. *it++ = Char('0');
  2300. if (!pointy) return it;
  2301. *it++ = decimal_point;
  2302. it = detail::fill_n(it, num_zeros, Char('0'));
  2303. return write_significand<Char>(it, f.significand, significand_size);
  2304. });
  2305. }
  2306. template <typename Char, typename Grouping, typename OutputIt,
  2307. typename DecimalFP>
  2308. FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f,
  2309. const format_specs& specs, sign s,
  2310. int exp_upper, locale_ref loc) -> OutputIt {
  2311. Char point = specs.localized() ? detail::decimal_point<Char>(loc) : Char('.');
  2312. int significand_size = get_significand_size(f);
  2313. int exp = f.exponent + significand_size - 1;
  2314. if (specs.type() == presentation_type::fixed ||
  2315. (specs.type() != presentation_type::exp &&
  2316. use_fixed(exp, specs.precision > 0 ? specs.precision : exp_upper))) {
  2317. return write_fixed<Char, Grouping>(out, f, significand_size, point, specs,
  2318. s, loc);
  2319. }
  2320. // Write value in the exponential format.
  2321. int num_zeros = 0;
  2322. long long size = significand_size + (s != sign::none ? 1 : 0);
  2323. if (specs.alt()) {
  2324. num_zeros = max_of(specs.precision - significand_size, 0);
  2325. size += num_zeros;
  2326. } else if (significand_size == 1) {
  2327. point = Char();
  2328. }
  2329. size += (point ? 1 : 0) + compute_exp_size(exp);
  2330. char exp_char = specs.upper() ? 'E' : 'e';
  2331. auto write = [=](reserve_iterator<OutputIt> it) {
  2332. if (s != sign::none) *it++ = detail::getsign<Char>(s);
  2333. // Insert a decimal point after the first digit and add an exponent.
  2334. it = write_significand(it, f.significand, significand_size, 1, point);
  2335. if (num_zeros > 0) it = detail::fill_n(it, num_zeros, Char('0'));
  2336. *it++ = Char(exp_char);
  2337. return write_exponent<Char>(exp, it);
  2338. };
  2339. auto usize = to_unsigned(size);
  2340. return specs.width > 0
  2341. ? write_padded<Char, align::right>(out, specs, usize, write)
  2342. : base_iterator(out, write(reserve(out, usize)));
  2343. }
  2344. template <typename Char, typename OutputIt, typename DecimalFP>
  2345. FMT_CONSTEXPR20 auto write_float(OutputIt out, const DecimalFP& f,
  2346. const format_specs& specs, sign s,
  2347. int exp_upper, locale_ref loc) -> OutputIt {
  2348. if (is_constant_evaluated()) {
  2349. return do_write_float<Char, fallback_digit_grouping<Char>>(out, f, specs, s,
  2350. exp_upper, loc);
  2351. } else {
  2352. return do_write_float<Char, digit_grouping<Char>>(out, f, specs, s,
  2353. exp_upper, loc);
  2354. }
  2355. }
  2356. template <typename T> constexpr auto isnan(T value) -> bool {
  2357. return value != value; // std::isnan doesn't support __float128.
  2358. }
  2359. template <typename T, typename Enable = void>
  2360. struct has_isfinite : std::false_type {};
  2361. template <typename T>
  2362. struct has_isfinite<T, enable_if_t<sizeof(std::isfinite(T())) != 0>>
  2363. : std::true_type {};
  2364. template <typename T,
  2365. FMT_ENABLE_IF(is_floating_point<T>::value&& has_isfinite<T>::value)>
  2366. FMT_CONSTEXPR20 auto isfinite(T value) -> bool {
  2367. constexpr T inf = T(std::numeric_limits<double>::infinity());
  2368. if (is_constant_evaluated())
  2369. return !detail::isnan(value) && value < inf && value > -inf;
  2370. return std::isfinite(value);
  2371. }
  2372. template <typename T, FMT_ENABLE_IF(!has_isfinite<T>::value)>
  2373. FMT_CONSTEXPR auto isfinite(T value) -> bool {
  2374. T inf = T(std::numeric_limits<double>::infinity());
  2375. // std::isfinite doesn't support __float128.
  2376. return !detail::isnan(value) && value < inf && value > -inf;
  2377. }
  2378. template <typename T, FMT_ENABLE_IF(is_floating_point<T>::value)>
  2379. FMT_INLINE FMT_CONSTEXPR auto signbit(T value) -> bool {
  2380. if (is_constant_evaluated()) {
  2381. #ifdef __cpp_if_constexpr
  2382. if constexpr (std::numeric_limits<double>::is_iec559) {
  2383. auto bits = detail::bit_cast<uint64_t>(static_cast<double>(value));
  2384. return (bits >> (num_bits<uint64_t>() - 1)) != 0;
  2385. }
  2386. #endif
  2387. }
  2388. return std::signbit(static_cast<double>(value));
  2389. }
  2390. inline FMT_CONSTEXPR20 void adjust_precision(int& precision, int exp10) {
  2391. // Adjust fixed precision by exponent because it is relative to decimal
  2392. // point.
  2393. if (exp10 > 0 && precision > max_value<int>() - exp10)
  2394. FMT_THROW(format_error("number is too big"));
  2395. precision += exp10;
  2396. }
  2397. class bigint {
  2398. private:
  2399. // A bigint is a number in the form bigit_[N - 1] ... bigit_[0] * 32^exp_.
  2400. using bigit = uint32_t; // A big digit.
  2401. using double_bigit = uint64_t;
  2402. enum { bigit_bits = num_bits<bigit>() };
  2403. enum { bigits_capacity = 32 };
  2404. basic_memory_buffer<bigit, bigits_capacity> bigits_;
  2405. int exp_;
  2406. friend struct formatter<bigint>;
  2407. FMT_CONSTEXPR auto get_bigit(int i) const -> bigit {
  2408. return i >= exp_ && i < num_bigits() ? bigits_[i - exp_] : 0;
  2409. }
  2410. FMT_CONSTEXPR void subtract_bigits(int index, bigit other, bigit& borrow) {
  2411. auto result = double_bigit(bigits_[index]) - other - borrow;
  2412. bigits_[index] = static_cast<bigit>(result);
  2413. borrow = static_cast<bigit>(result >> (bigit_bits * 2 - 1));
  2414. }
  2415. FMT_CONSTEXPR void remove_leading_zeros() {
  2416. int num_bigits = static_cast<int>(bigits_.size()) - 1;
  2417. while (num_bigits > 0 && bigits_[num_bigits] == 0) --num_bigits;
  2418. bigits_.resize(to_unsigned(num_bigits + 1));
  2419. }
  2420. // Computes *this -= other assuming aligned bigints and *this >= other.
  2421. FMT_CONSTEXPR void subtract_aligned(const bigint& other) {
  2422. FMT_ASSERT(other.exp_ >= exp_, "unaligned bigints");
  2423. FMT_ASSERT(compare(*this, other) >= 0, "");
  2424. bigit borrow = 0;
  2425. int i = other.exp_ - exp_;
  2426. for (size_t j = 0, n = other.bigits_.size(); j != n; ++i, ++j)
  2427. subtract_bigits(i, other.bigits_[j], borrow);
  2428. if (borrow != 0) subtract_bigits(i, 0, borrow);
  2429. FMT_ASSERT(borrow == 0, "");
  2430. remove_leading_zeros();
  2431. }
  2432. FMT_CONSTEXPR void multiply(uint32_t value) {
  2433. bigit carry = 0;
  2434. const double_bigit wide_value = value;
  2435. for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
  2436. double_bigit result = bigits_[i] * wide_value + carry;
  2437. bigits_[i] = static_cast<bigit>(result);
  2438. carry = static_cast<bigit>(result >> bigit_bits);
  2439. }
  2440. if (carry != 0) bigits_.push_back(carry);
  2441. }
  2442. template <typename UInt, FMT_ENABLE_IF(std::is_same<UInt, uint64_t>::value ||
  2443. std::is_same<UInt, uint128_t>::value)>
  2444. FMT_CONSTEXPR void multiply(UInt value) {
  2445. using half_uint =
  2446. conditional_t<std::is_same<UInt, uint128_t>::value, uint64_t, uint32_t>;
  2447. const int shift = num_bits<half_uint>() - bigit_bits;
  2448. const UInt lower = static_cast<half_uint>(value);
  2449. const UInt upper = value >> num_bits<half_uint>();
  2450. UInt carry = 0;
  2451. for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
  2452. UInt result = lower * bigits_[i] + static_cast<bigit>(carry);
  2453. carry = (upper * bigits_[i] << shift) + (result >> bigit_bits) +
  2454. (carry >> bigit_bits);
  2455. bigits_[i] = static_cast<bigit>(result);
  2456. }
  2457. while (carry != 0) {
  2458. bigits_.push_back(static_cast<bigit>(carry));
  2459. carry >>= bigit_bits;
  2460. }
  2461. }
  2462. template <typename UInt, FMT_ENABLE_IF(std::is_same<UInt, uint64_t>::value ||
  2463. std::is_same<UInt, uint128_t>::value)>
  2464. FMT_CONSTEXPR void assign(UInt n) {
  2465. size_t num_bigits = 0;
  2466. do {
  2467. bigits_[num_bigits++] = static_cast<bigit>(n);
  2468. n >>= bigit_bits;
  2469. } while (n != 0);
  2470. bigits_.resize(num_bigits);
  2471. exp_ = 0;
  2472. }
  2473. public:
  2474. FMT_CONSTEXPR bigint() : exp_(0) {}
  2475. explicit bigint(uint64_t n) { assign(n); }
  2476. bigint(const bigint&) = delete;
  2477. void operator=(const bigint&) = delete;
  2478. FMT_CONSTEXPR void assign(const bigint& other) {
  2479. auto size = other.bigits_.size();
  2480. bigits_.resize(size);
  2481. auto data = other.bigits_.data();
  2482. copy<bigit>(data, data + size, bigits_.data());
  2483. exp_ = other.exp_;
  2484. }
  2485. template <typename Int> FMT_CONSTEXPR void operator=(Int n) {
  2486. FMT_ASSERT(n > 0, "");
  2487. assign(uint64_or_128_t<Int>(n));
  2488. }
  2489. FMT_CONSTEXPR auto num_bigits() const -> int {
  2490. return static_cast<int>(bigits_.size()) + exp_;
  2491. }
  2492. FMT_CONSTEXPR auto operator<<=(int shift) -> bigint& {
  2493. FMT_ASSERT(shift >= 0, "");
  2494. exp_ += shift / bigit_bits;
  2495. shift %= bigit_bits;
  2496. if (shift == 0) return *this;
  2497. bigit carry = 0;
  2498. for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
  2499. bigit c = bigits_[i] >> (bigit_bits - shift);
  2500. bigits_[i] = (bigits_[i] << shift) + carry;
  2501. carry = c;
  2502. }
  2503. if (carry != 0) bigits_.push_back(carry);
  2504. return *this;
  2505. }
  2506. template <typename Int> FMT_CONSTEXPR auto operator*=(Int value) -> bigint& {
  2507. FMT_ASSERT(value > 0, "");
  2508. multiply(uint32_or_64_or_128_t<Int>(value));
  2509. return *this;
  2510. }
  2511. friend FMT_CONSTEXPR auto compare(const bigint& b1, const bigint& b2) -> int {
  2512. int num_bigits1 = b1.num_bigits(), num_bigits2 = b2.num_bigits();
  2513. if (num_bigits1 != num_bigits2) return num_bigits1 > num_bigits2 ? 1 : -1;
  2514. int i = static_cast<int>(b1.bigits_.size()) - 1;
  2515. int j = static_cast<int>(b2.bigits_.size()) - 1;
  2516. int end = i - j;
  2517. if (end < 0) end = 0;
  2518. for (; i >= end; --i, --j) {
  2519. bigit b1_bigit = b1.bigits_[i], b2_bigit = b2.bigits_[j];
  2520. if (b1_bigit != b2_bigit) return b1_bigit > b2_bigit ? 1 : -1;
  2521. }
  2522. if (i != j) return i > j ? 1 : -1;
  2523. return 0;
  2524. }
  2525. // Returns compare(lhs1 + lhs2, rhs).
  2526. friend FMT_CONSTEXPR auto add_compare(const bigint& lhs1, const bigint& lhs2,
  2527. const bigint& rhs) -> int {
  2528. int max_lhs_bigits = max_of(lhs1.num_bigits(), lhs2.num_bigits());
  2529. int num_rhs_bigits = rhs.num_bigits();
  2530. if (max_lhs_bigits + 1 < num_rhs_bigits) return -1;
  2531. if (max_lhs_bigits > num_rhs_bigits) return 1;
  2532. double_bigit borrow = 0;
  2533. int min_exp = min_of(min_of(lhs1.exp_, lhs2.exp_), rhs.exp_);
  2534. for (int i = num_rhs_bigits - 1; i >= min_exp; --i) {
  2535. double_bigit sum = double_bigit(lhs1.get_bigit(i)) + lhs2.get_bigit(i);
  2536. bigit rhs_bigit = rhs.get_bigit(i);
  2537. if (sum > rhs_bigit + borrow) return 1;
  2538. borrow = rhs_bigit + borrow - sum;
  2539. if (borrow > 1) return -1;
  2540. borrow <<= bigit_bits;
  2541. }
  2542. return borrow != 0 ? -1 : 0;
  2543. }
  2544. // Assigns pow(10, exp) to this bigint.
  2545. FMT_CONSTEXPR20 void assign_pow10(int exp) {
  2546. FMT_ASSERT(exp >= 0, "");
  2547. if (exp == 0) return *this = 1;
  2548. int bitmask = 1 << (num_bits<unsigned>() -
  2549. countl_zero(static_cast<uint32_t>(exp)) - 1);
  2550. // pow(10, exp) = pow(5, exp) * pow(2, exp). First compute pow(5, exp) by
  2551. // repeated squaring and multiplication.
  2552. *this = 5;
  2553. bitmask >>= 1;
  2554. while (bitmask != 0) {
  2555. square();
  2556. if ((exp & bitmask) != 0) *this *= 5;
  2557. bitmask >>= 1;
  2558. }
  2559. *this <<= exp; // Multiply by pow(2, exp) by shifting.
  2560. }
  2561. FMT_CONSTEXPR20 void square() {
  2562. int num_bigits = static_cast<int>(bigits_.size());
  2563. int num_result_bigits = 2 * num_bigits;
  2564. basic_memory_buffer<bigit, bigits_capacity> n(std::move(bigits_));
  2565. bigits_.resize(to_unsigned(num_result_bigits));
  2566. auto sum = uint128_t();
  2567. for (int bigit_index = 0; bigit_index < num_bigits; ++bigit_index) {
  2568. // Compute bigit at position bigit_index of the result by adding
  2569. // cross-product terms n[i] * n[j] such that i + j == bigit_index.
  2570. for (int i = 0, j = bigit_index; j >= 0; ++i, --j) {
  2571. // Most terms are multiplied twice which can be optimized in the future.
  2572. sum += double_bigit(n[i]) * n[j];
  2573. }
  2574. bigits_[bigit_index] = static_cast<bigit>(sum);
  2575. sum >>= num_bits<bigit>(); // Compute the carry.
  2576. }
  2577. // Do the same for the top half.
  2578. for (int bigit_index = num_bigits; bigit_index < num_result_bigits;
  2579. ++bigit_index) {
  2580. for (int j = num_bigits - 1, i = bigit_index - j; i < num_bigits;)
  2581. sum += double_bigit(n[i++]) * n[j--];
  2582. bigits_[bigit_index] = static_cast<bigit>(sum);
  2583. sum >>= num_bits<bigit>();
  2584. }
  2585. remove_leading_zeros();
  2586. exp_ *= 2;
  2587. }
  2588. // If this bigint has a bigger exponent than other, adds trailing zero to make
  2589. // exponents equal. This simplifies some operations such as subtraction.
  2590. FMT_CONSTEXPR void align(const bigint& other) {
  2591. int exp_difference = exp_ - other.exp_;
  2592. if (exp_difference <= 0) return;
  2593. int num_bigits = static_cast<int>(bigits_.size());
  2594. bigits_.resize(to_unsigned(num_bigits + exp_difference));
  2595. for (int i = num_bigits - 1, j = i + exp_difference; i >= 0; --i, --j)
  2596. bigits_[j] = bigits_[i];
  2597. fill_n(bigits_.data(), to_unsigned(exp_difference), 0U);
  2598. exp_ -= exp_difference;
  2599. }
  2600. // Divides this bignum by divisor, assigning the remainder to this and
  2601. // returning the quotient.
  2602. FMT_CONSTEXPR auto divmod_assign(const bigint& divisor) -> int {
  2603. FMT_ASSERT(this != &divisor, "");
  2604. if (compare(*this, divisor) < 0) return 0;
  2605. FMT_ASSERT(divisor.bigits_[divisor.bigits_.size() - 1u] != 0, "");
  2606. align(divisor);
  2607. int quotient = 0;
  2608. do {
  2609. subtract_aligned(divisor);
  2610. ++quotient;
  2611. } while (compare(*this, divisor) >= 0);
  2612. return quotient;
  2613. }
  2614. };
  2615. // format_dragon flags.
  2616. enum dragon {
  2617. predecessor_closer = 1,
  2618. fixup = 2, // Run fixup to correct exp10 which can be off by one.
  2619. fixed = 4,
  2620. };
  2621. // Formats a floating-point number using a variation of the Fixed-Precision
  2622. // Positive Floating-Point Printout ((FPP)^2) algorithm by Steele & White:
  2623. // https://fmt.dev/papers/p372-steele.pdf.
  2624. FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value,
  2625. unsigned flags, int num_digits,
  2626. buffer<char>& buf, int& exp10) {
  2627. bigint numerator; // 2 * R in (FPP)^2.
  2628. bigint denominator; // 2 * S in (FPP)^2.
  2629. // lower and upper are differences between value and corresponding boundaries.
  2630. bigint lower; // (M^- in (FPP)^2).
  2631. bigint upper_store; // upper's value if different from lower.
  2632. bigint* upper = nullptr; // (M^+ in (FPP)^2).
  2633. // Shift numerator and denominator by an extra bit or two (if lower boundary
  2634. // is closer) to make lower and upper integers. This eliminates multiplication
  2635. // by 2 during later computations.
  2636. bool is_predecessor_closer = (flags & dragon::predecessor_closer) != 0;
  2637. int shift = is_predecessor_closer ? 2 : 1;
  2638. if (value.e >= 0) {
  2639. numerator = value.f;
  2640. numerator <<= value.e + shift;
  2641. lower = 1;
  2642. lower <<= value.e;
  2643. if (is_predecessor_closer) {
  2644. upper_store = 1;
  2645. upper_store <<= value.e + 1;
  2646. upper = &upper_store;
  2647. }
  2648. denominator.assign_pow10(exp10);
  2649. denominator <<= shift;
  2650. } else if (exp10 < 0) {
  2651. numerator.assign_pow10(-exp10);
  2652. lower.assign(numerator);
  2653. if (is_predecessor_closer) {
  2654. upper_store.assign(numerator);
  2655. upper_store <<= 1;
  2656. upper = &upper_store;
  2657. }
  2658. numerator *= value.f;
  2659. numerator <<= shift;
  2660. denominator = 1;
  2661. denominator <<= shift - value.e;
  2662. } else {
  2663. numerator = value.f;
  2664. numerator <<= shift;
  2665. denominator.assign_pow10(exp10);
  2666. denominator <<= shift - value.e;
  2667. lower = 1;
  2668. if (is_predecessor_closer) {
  2669. upper_store = 1ULL << 1;
  2670. upper = &upper_store;
  2671. }
  2672. }
  2673. int even = static_cast<int>((value.f & 1) == 0);
  2674. if (!upper) upper = &lower;
  2675. bool shortest = num_digits < 0;
  2676. if ((flags & dragon::fixup) != 0) {
  2677. if (add_compare(numerator, *upper, denominator) + even <= 0) {
  2678. --exp10;
  2679. numerator *= 10;
  2680. if (num_digits < 0) {
  2681. lower *= 10;
  2682. if (upper != &lower) *upper *= 10;
  2683. }
  2684. }
  2685. if ((flags & dragon::fixed) != 0) adjust_precision(num_digits, exp10 + 1);
  2686. }
  2687. // Invariant: value == (numerator / denominator) * pow(10, exp10).
  2688. if (shortest) {
  2689. // Generate the shortest representation.
  2690. num_digits = 0;
  2691. char* data = buf.data();
  2692. for (;;) {
  2693. int digit = numerator.divmod_assign(denominator);
  2694. bool low = compare(numerator, lower) - even < 0; // numerator <[=] lower.
  2695. // numerator + upper >[=] pow10:
  2696. bool high = add_compare(numerator, *upper, denominator) + even > 0;
  2697. data[num_digits++] = static_cast<char>('0' + digit);
  2698. if (low || high) {
  2699. if (!low) {
  2700. ++data[num_digits - 1];
  2701. } else if (high) {
  2702. int result = add_compare(numerator, numerator, denominator);
  2703. // Round half to even.
  2704. if (result > 0 || (result == 0 && (digit % 2) != 0))
  2705. ++data[num_digits - 1];
  2706. }
  2707. buf.try_resize(to_unsigned(num_digits));
  2708. exp10 -= num_digits - 1;
  2709. return;
  2710. }
  2711. numerator *= 10;
  2712. lower *= 10;
  2713. if (upper != &lower) *upper *= 10;
  2714. }
  2715. }
  2716. // Generate the given number of digits.
  2717. exp10 -= num_digits - 1;
  2718. if (num_digits <= 0) {
  2719. auto digit = '0';
  2720. if (num_digits == 0) {
  2721. denominator *= 10;
  2722. digit = add_compare(numerator, numerator, denominator) > 0 ? '1' : '0';
  2723. }
  2724. buf.push_back(digit);
  2725. return;
  2726. }
  2727. buf.try_resize(to_unsigned(num_digits));
  2728. for (int i = 0; i < num_digits - 1; ++i) {
  2729. int digit = numerator.divmod_assign(denominator);
  2730. buf[i] = static_cast<char>('0' + digit);
  2731. numerator *= 10;
  2732. }
  2733. int digit = numerator.divmod_assign(denominator);
  2734. auto result = add_compare(numerator, numerator, denominator);
  2735. if (result > 0 || (result == 0 && (digit % 2) != 0)) {
  2736. if (digit == 9) {
  2737. const auto overflow = '0' + 10;
  2738. buf[num_digits - 1] = overflow;
  2739. // Propagate the carry.
  2740. for (int i = num_digits - 1; i > 0 && buf[i] == overflow; --i) {
  2741. buf[i] = '0';
  2742. ++buf[i - 1];
  2743. }
  2744. if (buf[0] == overflow) {
  2745. buf[0] = '1';
  2746. if ((flags & dragon::fixed) != 0)
  2747. buf.push_back('0');
  2748. else
  2749. ++exp10;
  2750. }
  2751. return;
  2752. }
  2753. ++digit;
  2754. }
  2755. buf[num_digits - 1] = static_cast<char>('0' + digit);
  2756. }
  2757. // Formats a floating-point number using the hexfloat format.
  2758. template <typename Float, FMT_ENABLE_IF(!is_double_double<Float>::value)>
  2759. FMT_CONSTEXPR20 void format_hexfloat(Float value, format_specs specs,
  2760. buffer<char>& buf) {
  2761. // float is passed as double to reduce the number of instantiations and to
  2762. // simplify implementation.
  2763. static_assert(!std::is_same<Float, float>::value, "");
  2764. using info = dragonbox::float_info<Float>;
  2765. // Assume Float is in the format [sign][exponent][significand].
  2766. using carrier_uint = typename info::carrier_uint;
  2767. const auto num_float_significand_bits = detail::num_significand_bits<Float>();
  2768. basic_fp<carrier_uint> f(value);
  2769. f.e += num_float_significand_bits;
  2770. if (!has_implicit_bit<Float>()) --f.e;
  2771. const auto num_fraction_bits =
  2772. num_float_significand_bits + (has_implicit_bit<Float>() ? 1 : 0);
  2773. const auto num_xdigits = (num_fraction_bits + 3) / 4;
  2774. const auto leading_shift = ((num_xdigits - 1) * 4);
  2775. const auto leading_mask = carrier_uint(0xF) << leading_shift;
  2776. const auto leading_xdigit =
  2777. static_cast<uint32_t>((f.f & leading_mask) >> leading_shift);
  2778. if (leading_xdigit > 1) f.e -= (32 - countl_zero(leading_xdigit) - 1);
  2779. int print_xdigits = num_xdigits - 1;
  2780. if (specs.precision >= 0 && print_xdigits > specs.precision) {
  2781. const int shift = ((print_xdigits - specs.precision - 1) * 4);
  2782. const auto mask = carrier_uint(0xF) << shift;
  2783. const auto v = static_cast<uint32_t>((f.f & mask) >> shift);
  2784. if (v >= 8) {
  2785. const auto inc = carrier_uint(1) << (shift + 4);
  2786. f.f += inc;
  2787. f.f &= ~(inc - 1);
  2788. }
  2789. // Check long double overflow
  2790. if (!has_implicit_bit<Float>()) {
  2791. const auto implicit_bit = carrier_uint(1) << num_float_significand_bits;
  2792. if ((f.f & implicit_bit) == implicit_bit) {
  2793. f.f >>= 4;
  2794. f.e += 4;
  2795. }
  2796. }
  2797. print_xdigits = specs.precision;
  2798. }
  2799. char xdigits[num_bits<carrier_uint>() / 4];
  2800. detail::fill_n(xdigits, sizeof(xdigits), '0');
  2801. format_base2e(4, xdigits, f.f, num_xdigits, specs.upper());
  2802. // Remove zero tail
  2803. while (print_xdigits > 0 && xdigits[print_xdigits] == '0') --print_xdigits;
  2804. buf.push_back('0');
  2805. buf.push_back(specs.upper() ? 'X' : 'x');
  2806. buf.push_back(xdigits[0]);
  2807. if (specs.alt() || print_xdigits > 0 || print_xdigits < specs.precision)
  2808. buf.push_back('.');
  2809. buf.append(xdigits + 1, xdigits + 1 + print_xdigits);
  2810. for (; print_xdigits < specs.precision; ++print_xdigits) buf.push_back('0');
  2811. buf.push_back(specs.upper() ? 'P' : 'p');
  2812. uint32_t abs_e;
  2813. if (f.e < 0) {
  2814. buf.push_back('-');
  2815. abs_e = static_cast<uint32_t>(-f.e);
  2816. } else {
  2817. buf.push_back('+');
  2818. abs_e = static_cast<uint32_t>(f.e);
  2819. }
  2820. format_decimal<char>(appender(buf), abs_e, detail::count_digits(abs_e));
  2821. }
  2822. template <typename Float, FMT_ENABLE_IF(is_double_double<Float>::value)>
  2823. FMT_CONSTEXPR20 void format_hexfloat(Float value, format_specs specs,
  2824. buffer<char>& buf) {
  2825. format_hexfloat(static_cast<double>(value), specs, buf);
  2826. }
  2827. constexpr auto fractional_part_rounding_thresholds(int index) -> uint32_t {
  2828. // For checking rounding thresholds.
  2829. // The kth entry is chosen to be the smallest integer such that the
  2830. // upper 32-bits of 10^(k+1) times it is strictly bigger than 5 * 10^k.
  2831. // It is equal to ceil(2^31 + 2^32/10^(k + 1)).
  2832. // These are stored in a string literal because we cannot have static arrays
  2833. // in constexpr functions and non-static ones are poorly optimized.
  2834. return U"\x9999999a\x828f5c29\x80418938\x80068db9\x8000a7c6\x800010c7"
  2835. U"\x800001ae\x8000002b"[index];
  2836. }
  2837. template <typename Float>
  2838. FMT_CONSTEXPR20 auto format_float(Float value, int precision,
  2839. const format_specs& specs, bool binary32,
  2840. buffer<char>& buf) -> int {
  2841. // float is passed as double to reduce the number of instantiations.
  2842. static_assert(!std::is_same<Float, float>::value, "");
  2843. auto converted_value = convert_float(value);
  2844. const bool fixed = specs.type() == presentation_type::fixed;
  2845. if (value == 0) {
  2846. if (precision <= 0 || !fixed) {
  2847. buf.push_back('0');
  2848. return 0;
  2849. }
  2850. buf.try_resize(to_unsigned(precision));
  2851. fill_n(buf.data(), precision, '0');
  2852. return -precision;
  2853. }
  2854. int exp = 0;
  2855. bool use_dragon = true;
  2856. unsigned dragon_flags = 0;
  2857. if (!is_fast_float<Float>() || is_constant_evaluated()) {
  2858. const auto inv_log2_10 = 0.3010299956639812; // 1 / log2(10)
  2859. using info = dragonbox::float_info<decltype(converted_value)>;
  2860. const auto f = basic_fp<typename info::carrier_uint>(converted_value);
  2861. // Compute exp, an approximate power of 10, such that
  2862. // 10^(exp - 1) <= value < 10^exp or 10^exp <= value < 10^(exp + 1).
  2863. // This is based on log10(value) == log2(value) / log2(10) and approximation
  2864. // of log2(value) by e + num_fraction_bits idea from double-conversion.
  2865. auto e = (f.e + count_digits<1>(f.f) - 1) * inv_log2_10 - 1e-10;
  2866. exp = static_cast<int>(e);
  2867. if (e > exp) ++exp; // Compute ceil.
  2868. dragon_flags = dragon::fixup;
  2869. } else {
  2870. // Extract significand bits and exponent bits.
  2871. using info = dragonbox::float_info<double>;
  2872. auto br = bit_cast<uint64_t>(static_cast<double>(value));
  2873. const uint64_t significand_mask =
  2874. (static_cast<uint64_t>(1) << num_significand_bits<double>()) - 1;
  2875. uint64_t significand = (br & significand_mask);
  2876. int exponent = static_cast<int>((br & exponent_mask<double>()) >>
  2877. num_significand_bits<double>());
  2878. if (exponent != 0) { // Check if normal.
  2879. exponent -= exponent_bias<double>() + num_significand_bits<double>();
  2880. significand |=
  2881. (static_cast<uint64_t>(1) << num_significand_bits<double>());
  2882. significand <<= 1;
  2883. } else {
  2884. // Normalize subnormal inputs.
  2885. FMT_ASSERT(significand != 0, "zeros should not appear here");
  2886. int shift = countl_zero(significand);
  2887. FMT_ASSERT(shift >= num_bits<uint64_t>() - num_significand_bits<double>(),
  2888. "");
  2889. shift -= (num_bits<uint64_t>() - num_significand_bits<double>() - 2);
  2890. exponent = (std::numeric_limits<double>::min_exponent -
  2891. num_significand_bits<double>()) -
  2892. shift;
  2893. significand <<= shift;
  2894. }
  2895. // Compute the first several nonzero decimal significand digits.
  2896. // We call the number we get the first segment.
  2897. const int k = info::kappa - dragonbox::floor_log10_pow2(exponent);
  2898. exp = -k;
  2899. const int beta = exponent + dragonbox::floor_log2_pow10(k);
  2900. uint64_t first_segment;
  2901. bool has_more_segments;
  2902. int digits_in_the_first_segment;
  2903. {
  2904. const auto r = dragonbox::umul192_upper128(
  2905. significand << beta, dragonbox::get_cached_power(k));
  2906. first_segment = r.high();
  2907. has_more_segments = r.low() != 0;
  2908. // The first segment can have 18 ~ 19 digits.
  2909. if (first_segment >= 1000000000000000000ULL) {
  2910. digits_in_the_first_segment = 19;
  2911. } else {
  2912. // When it is of 18-digits, we align it to 19-digits by adding a bogus
  2913. // zero at the end.
  2914. digits_in_the_first_segment = 18;
  2915. first_segment *= 10;
  2916. }
  2917. }
  2918. // Compute the actual number of decimal digits to print.
  2919. if (fixed) adjust_precision(precision, exp + digits_in_the_first_segment);
  2920. // Use Dragon4 only when there might be not enough digits in the first
  2921. // segment.
  2922. if (digits_in_the_first_segment > precision) {
  2923. use_dragon = false;
  2924. if (precision <= 0) {
  2925. exp += digits_in_the_first_segment;
  2926. if (precision < 0) {
  2927. // Nothing to do, since all we have are just leading zeros.
  2928. buf.try_resize(0);
  2929. } else {
  2930. // We may need to round-up.
  2931. buf.try_resize(1);
  2932. if ((first_segment | static_cast<uint64_t>(has_more_segments)) >
  2933. 5000000000000000000ULL) {
  2934. buf[0] = '1';
  2935. } else {
  2936. buf[0] = '0';
  2937. }
  2938. }
  2939. } // precision <= 0
  2940. else {
  2941. exp += digits_in_the_first_segment - precision;
  2942. // When precision > 0, we divide the first segment into three
  2943. // subsegments, each with 9, 9, and 0 ~ 1 digits so that each fits
  2944. // in 32-bits which usually allows faster calculation than in
  2945. // 64-bits. Since some compiler (e.g. MSVC) doesn't know how to optimize
  2946. // division-by-constant for large 64-bit divisors, we do it here
  2947. // manually. The magic number 7922816251426433760 below is equal to
  2948. // ceil(2^(64+32) / 10^10).
  2949. const uint32_t first_subsegment = static_cast<uint32_t>(
  2950. dragonbox::umul128_upper64(first_segment, 7922816251426433760ULL) >>
  2951. 32);
  2952. const uint64_t second_third_subsegments =
  2953. first_segment - first_subsegment * 10000000000ULL;
  2954. uint64_t prod;
  2955. uint32_t digits;
  2956. bool should_round_up;
  2957. int number_of_digits_to_print = min_of(precision, 9);
  2958. // Print a 9-digits subsegment, either the first or the second.
  2959. auto print_subsegment = [&](uint32_t subsegment, char* buffer) {
  2960. int number_of_digits_printed = 0;
  2961. // If we want to print an odd number of digits from the subsegment,
  2962. if ((number_of_digits_to_print & 1) != 0) {
  2963. // Convert to 64-bit fixed-point fractional form with 1-digit
  2964. // integer part. The magic number 720575941 is a good enough
  2965. // approximation of 2^(32 + 24) / 10^8; see
  2966. // https://jk-jeon.github.io/posts/2022/12/fixed-precision-formatting/#fixed-length-case
  2967. // for details.
  2968. prod = ((subsegment * static_cast<uint64_t>(720575941)) >> 24) + 1;
  2969. digits = static_cast<uint32_t>(prod >> 32);
  2970. *buffer = static_cast<char>('0' + digits);
  2971. number_of_digits_printed++;
  2972. }
  2973. // If we want to print an even number of digits from the
  2974. // first_subsegment,
  2975. else {
  2976. // Convert to 64-bit fixed-point fractional form with 2-digits
  2977. // integer part. The magic number 450359963 is a good enough
  2978. // approximation of 2^(32 + 20) / 10^7; see
  2979. // https://jk-jeon.github.io/posts/2022/12/fixed-precision-formatting/#fixed-length-case
  2980. // for details.
  2981. prod = ((subsegment * static_cast<uint64_t>(450359963)) >> 20) + 1;
  2982. digits = static_cast<uint32_t>(prod >> 32);
  2983. write2digits(buffer, digits);
  2984. number_of_digits_printed += 2;
  2985. }
  2986. // Print all digit pairs.
  2987. while (number_of_digits_printed < number_of_digits_to_print) {
  2988. prod = static_cast<uint32_t>(prod) * static_cast<uint64_t>(100);
  2989. digits = static_cast<uint32_t>(prod >> 32);
  2990. write2digits(buffer + number_of_digits_printed, digits);
  2991. number_of_digits_printed += 2;
  2992. }
  2993. };
  2994. // Print first subsegment.
  2995. print_subsegment(first_subsegment, buf.data());
  2996. // Perform rounding if the first subsegment is the last subsegment to
  2997. // print.
  2998. if (precision <= 9) {
  2999. // Rounding inside the subsegment.
  3000. // We round-up if:
  3001. // - either the fractional part is strictly larger than 1/2, or
  3002. // - the fractional part is exactly 1/2 and the last digit is odd.
  3003. // We rely on the following observations:
  3004. // - If fractional_part >= threshold, then the fractional part is
  3005. // strictly larger than 1/2.
  3006. // - If the MSB of fractional_part is set, then the fractional part
  3007. // must be at least 1/2.
  3008. // - When the MSB of fractional_part is set, either
  3009. // second_third_subsegments being nonzero or has_more_segments
  3010. // being true means there are further digits not printed, so the
  3011. // fractional part is strictly larger than 1/2.
  3012. if (precision < 9) {
  3013. uint32_t fractional_part = static_cast<uint32_t>(prod);
  3014. should_round_up =
  3015. fractional_part >= fractional_part_rounding_thresholds(
  3016. 8 - number_of_digits_to_print) ||
  3017. ((fractional_part >> 31) &
  3018. ((digits & 1) | (second_third_subsegments != 0) |
  3019. has_more_segments)) != 0;
  3020. }
  3021. // Rounding at the subsegment boundary.
  3022. // In this case, the fractional part is at least 1/2 if and only if
  3023. // second_third_subsegments >= 5000000000ULL, and is strictly larger
  3024. // than 1/2 if we further have either second_third_subsegments >
  3025. // 5000000000ULL or has_more_segments == true.
  3026. else {
  3027. should_round_up = second_third_subsegments > 5000000000ULL ||
  3028. (second_third_subsegments == 5000000000ULL &&
  3029. ((digits & 1) != 0 || has_more_segments));
  3030. }
  3031. }
  3032. // Otherwise, print the second subsegment.
  3033. else {
  3034. // Compilers are not aware of how to leverage the maximum value of
  3035. // second_third_subsegments to find out a better magic number which
  3036. // allows us to eliminate an additional shift. 1844674407370955162 =
  3037. // ceil(2^64/10) < ceil(2^64*(10^9/(10^10 - 1))).
  3038. const uint32_t second_subsegment =
  3039. static_cast<uint32_t>(dragonbox::umul128_upper64(
  3040. second_third_subsegments, 1844674407370955162ULL));
  3041. const uint32_t third_subsegment =
  3042. static_cast<uint32_t>(second_third_subsegments) -
  3043. second_subsegment * 10;
  3044. number_of_digits_to_print = precision - 9;
  3045. print_subsegment(second_subsegment, buf.data() + 9);
  3046. // Rounding inside the subsegment.
  3047. if (precision < 18) {
  3048. // The condition third_subsegment != 0 implies that the segment was
  3049. // of 19 digits, so in this case the third segment should be
  3050. // consisting of a genuine digit from the input.
  3051. uint32_t fractional_part = static_cast<uint32_t>(prod);
  3052. should_round_up =
  3053. fractional_part >= fractional_part_rounding_thresholds(
  3054. 8 - number_of_digits_to_print) ||
  3055. ((fractional_part >> 31) &
  3056. ((digits & 1) | (third_subsegment != 0) |
  3057. has_more_segments)) != 0;
  3058. }
  3059. // Rounding at the subsegment boundary.
  3060. else {
  3061. // In this case, the segment must be of 19 digits, thus
  3062. // the third subsegment should be consisting of a genuine digit from
  3063. // the input.
  3064. should_round_up = third_subsegment > 5 ||
  3065. (third_subsegment == 5 &&
  3066. ((digits & 1) != 0 || has_more_segments));
  3067. }
  3068. }
  3069. // Round-up if necessary.
  3070. if (should_round_up) {
  3071. ++buf[precision - 1];
  3072. for (int i = precision - 1; i > 0 && buf[i] > '9'; --i) {
  3073. buf[i] = '0';
  3074. ++buf[i - 1];
  3075. }
  3076. if (buf[0] > '9') {
  3077. buf[0] = '1';
  3078. if (fixed)
  3079. buf[precision++] = '0';
  3080. else
  3081. ++exp;
  3082. }
  3083. }
  3084. buf.try_resize(to_unsigned(precision));
  3085. }
  3086. } // if (digits_in_the_first_segment > precision)
  3087. else {
  3088. // Adjust the exponent for its use in Dragon4.
  3089. exp += digits_in_the_first_segment - 1;
  3090. }
  3091. }
  3092. if (use_dragon) {
  3093. auto f = basic_fp<uint128_t>();
  3094. bool is_predecessor_closer = binary32 ? f.assign(static_cast<float>(value))
  3095. : f.assign(converted_value);
  3096. if (is_predecessor_closer) dragon_flags |= dragon::predecessor_closer;
  3097. if (fixed) dragon_flags |= dragon::fixed;
  3098. // Limit precision to the maximum possible number of significant digits in
  3099. // an IEEE754 double because we don't need to generate zeros.
  3100. const int max_double_digits = 767;
  3101. if (precision > max_double_digits) precision = max_double_digits;
  3102. format_dragon(f, dragon_flags, precision, buf, exp);
  3103. }
  3104. if (!fixed && !specs.alt()) {
  3105. // Remove trailing zeros.
  3106. auto num_digits = buf.size();
  3107. while (num_digits > 0 && buf[num_digits - 1] == '0') {
  3108. --num_digits;
  3109. ++exp;
  3110. }
  3111. buf.try_resize(num_digits);
  3112. }
  3113. return exp;
  3114. }
  3115. template <typename Char, typename OutputIt, typename T,
  3116. FMT_ENABLE_IF(is_floating_point<T>::value)>
  3117. FMT_CONSTEXPR20 auto write(OutputIt out, T value, format_specs specs,
  3118. locale_ref loc = {}) -> OutputIt {
  3119. if (specs.localized() && write_loc(out, value, specs, loc)) return out;
  3120. // Use signbit because value < 0 is false for NaN.
  3121. sign s = detail::signbit(value) ? sign::minus : specs.sign();
  3122. if (!detail::isfinite(value))
  3123. return write_nonfinite<Char>(out, detail::isnan(value), specs, s);
  3124. if (specs.align() == align::numeric && s != sign::none) {
  3125. *out++ = detail::getsign<Char>(s);
  3126. s = sign::none;
  3127. if (specs.width != 0) --specs.width;
  3128. }
  3129. const int exp_upper = detail::exp_upper<T>();
  3130. int precision = specs.precision;
  3131. if (precision < 0) {
  3132. if (specs.type() != presentation_type::none) {
  3133. precision = 6;
  3134. } else if (is_fast_float<T>::value && !is_constant_evaluated()) {
  3135. // Use Dragonbox for the shortest format.
  3136. auto dec = dragonbox::to_decimal(static_cast<fast_float_t<T>>(value));
  3137. return write_float<Char>(out, dec, specs, s, exp_upper, loc);
  3138. }
  3139. }
  3140. memory_buffer buffer;
  3141. if (specs.type() == presentation_type::hexfloat) {
  3142. if (s != sign::none) buffer.push_back(detail::getsign<char>(s));
  3143. format_hexfloat(convert_float(value), specs, buffer);
  3144. return write_bytes<Char, align::right>(out, {buffer.data(), buffer.size()},
  3145. specs);
  3146. }
  3147. if (specs.type() == presentation_type::exp) {
  3148. if (precision == max_value<int>())
  3149. report_error("number is too big");
  3150. else
  3151. ++precision;
  3152. if (specs.precision != 0) specs.set_alt();
  3153. } else if (specs.type() == presentation_type::fixed) {
  3154. if (specs.precision != 0) specs.set_alt();
  3155. } else if (precision == 0) {
  3156. precision = 1;
  3157. }
  3158. int exp = format_float(convert_float(value), precision, specs,
  3159. std::is_same<T, float>(), buffer);
  3160. specs.precision = precision;
  3161. auto f = big_decimal_fp{buffer.data(), static_cast<int>(buffer.size()), exp};
  3162. return write_float<Char>(out, f, specs, s, exp_upper, loc);
  3163. }
  3164. template <typename Char, typename OutputIt, typename T,
  3165. FMT_ENABLE_IF(is_fast_float<T>::value)>
  3166. FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt {
  3167. if (is_constant_evaluated()) return write<Char>(out, value, format_specs());
  3168. auto s = detail::signbit(value) ? sign::minus : sign::none;
  3169. auto mask = exponent_mask<fast_float_t<T>>();
  3170. if ((bit_cast<decltype(mask)>(value) & mask) == mask)
  3171. return write_nonfinite<Char>(out, std::isnan(value), {}, s);
  3172. auto dec = dragonbox::to_decimal(static_cast<fast_float_t<T>>(value));
  3173. auto significand = dec.significand;
  3174. int significand_size = count_digits(significand);
  3175. int exponent = dec.exponent + significand_size - 1;
  3176. if (use_fixed(exponent, detail::exp_upper<T>())) {
  3177. return write_fixed<Char, fallback_digit_grouping<Char>>(
  3178. out, dec, significand_size, Char('.'), {}, s);
  3179. }
  3180. // Write value in the exponential format.
  3181. const char* prefix = "e+";
  3182. int abs_exponent = exponent;
  3183. if (exponent < 0) {
  3184. abs_exponent = -exponent;
  3185. prefix = "e-";
  3186. }
  3187. auto has_decimal_point = significand_size != 1;
  3188. size_t size = std::is_pointer<OutputIt>::value
  3189. ? 0u
  3190. : to_unsigned((s != sign::none ? 1 : 0) + significand_size +
  3191. (has_decimal_point ? 1 : 0) +
  3192. (abs_exponent >= 100 ? 5 : 4));
  3193. if (auto ptr = to_pointer<Char>(out, size)) {
  3194. if (s != sign::none) *ptr++ = Char('-');
  3195. if (has_decimal_point) {
  3196. auto begin = ptr;
  3197. ptr = format_decimal<Char>(ptr, significand, significand_size + 1);
  3198. *begin = begin[1];
  3199. begin[1] = '.';
  3200. } else {
  3201. *ptr++ = static_cast<Char>('0' + significand);
  3202. }
  3203. if (std::is_same<Char, char>::value) {
  3204. memcpy(ptr, prefix, 2);
  3205. ptr += 2;
  3206. } else {
  3207. *ptr++ = prefix[0];
  3208. *ptr++ = prefix[1];
  3209. }
  3210. if (abs_exponent >= 100) {
  3211. *ptr++ = static_cast<Char>('0' + abs_exponent / 100);
  3212. abs_exponent %= 100;
  3213. }
  3214. write2digits(ptr, static_cast<unsigned>(abs_exponent));
  3215. return select<std::is_pointer<OutputIt>::value>(ptr + 2, out);
  3216. }
  3217. auto it = reserve(out, size);
  3218. if (s != sign::none) *it++ = Char('-');
  3219. // Insert a decimal point after the first digit and add an exponent.
  3220. it = write_significand(it, significand, significand_size, 1,
  3221. has_decimal_point ? Char('.') : Char());
  3222. *it++ = Char('e');
  3223. it = write_exponent<Char>(exponent, it);
  3224. return base_iterator(out, it);
  3225. }
  3226. template <typename Char, typename OutputIt, typename T,
  3227. FMT_ENABLE_IF(is_floating_point<T>::value &&
  3228. !is_fast_float<T>::value)>
  3229. inline auto write(OutputIt out, T value) -> OutputIt {
  3230. return write<Char>(out, value, {});
  3231. }
  3232. template <typename Char, typename OutputIt>
  3233. auto write(OutputIt out, monostate, format_specs = {}, locale_ref = {})
  3234. -> OutputIt {
  3235. FMT_ASSERT(false, "");
  3236. return out;
  3237. }
  3238. template <typename Char, typename OutputIt>
  3239. FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> value)
  3240. -> OutputIt {
  3241. return copy_noinline<Char>(value.begin(), value.end(), out);
  3242. }
  3243. template <typename Char, typename OutputIt, typename T,
  3244. FMT_ENABLE_IF(has_to_string_view<T>::value)>
  3245. constexpr auto write(OutputIt out, const T& value) -> OutputIt {
  3246. return write<Char>(out, to_string_view(value));
  3247. }
  3248. // FMT_ENABLE_IF() condition separated to workaround an MSVC bug.
  3249. template <
  3250. typename Char, typename OutputIt, typename T,
  3251. bool check = std::is_enum<T>::value && !std::is_same<T, Char>::value &&
  3252. mapped_type_constant<T, Char>::value != type::custom_type,
  3253. FMT_ENABLE_IF(check)>
  3254. FMT_CONSTEXPR auto write(OutputIt out, T value) -> OutputIt {
  3255. return write<Char>(out, static_cast<underlying_t<T>>(value));
  3256. }
  3257. template <typename Char, typename OutputIt, typename T,
  3258. FMT_ENABLE_IF(std::is_same<T, bool>::value)>
  3259. FMT_CONSTEXPR auto write(OutputIt out, T value, const format_specs& specs = {},
  3260. locale_ref = {}) -> OutputIt {
  3261. return specs.type() != presentation_type::none &&
  3262. specs.type() != presentation_type::string
  3263. ? write<Char>(out, value ? 1 : 0, specs, {})
  3264. : write_bytes<Char>(out, value ? "true" : "false", specs);
  3265. }
  3266. template <typename Char, typename OutputIt>
  3267. FMT_CONSTEXPR auto write(OutputIt out, Char value) -> OutputIt {
  3268. auto it = reserve(out, 1);
  3269. *it++ = value;
  3270. return base_iterator(out, it);
  3271. }
  3272. template <typename Char, typename OutputIt>
  3273. FMT_CONSTEXPR20 auto write(OutputIt out, const Char* value) -> OutputIt {
  3274. if (value) return write(out, basic_string_view<Char>(value));
  3275. report_error("string pointer is null");
  3276. return out;
  3277. }
  3278. template <typename Char, typename OutputIt, typename T,
  3279. FMT_ENABLE_IF(std::is_same<T, void>::value)>
  3280. auto write(OutputIt out, const T* value, const format_specs& specs = {},
  3281. locale_ref = {}) -> OutputIt {
  3282. return write_ptr<Char>(out, bit_cast<uintptr_t>(value), &specs);
  3283. }
  3284. template <typename Char, typename OutputIt, typename T,
  3285. FMT_ENABLE_IF(mapped_type_constant<T, Char>::value ==
  3286. type::custom_type &&
  3287. !std::is_fundamental<T>::value)>
  3288. FMT_CONSTEXPR auto write(OutputIt out, const T& value) -> OutputIt {
  3289. auto f = formatter<T, Char>();
  3290. auto parse_ctx = parse_context<Char>({});
  3291. f.parse(parse_ctx);
  3292. auto ctx = basic_format_context<OutputIt, Char>(out, {}, {});
  3293. return f.format(value, ctx);
  3294. }
  3295. template <typename T>
  3296. using is_builtin =
  3297. bool_constant<std::is_same<T, int>::value || FMT_BUILTIN_TYPES>;
  3298. // An argument visitor that formats the argument and writes it via the output
  3299. // iterator. It's a class and not a generic lambda for compatibility with C++11.
  3300. template <typename Char> struct default_arg_formatter {
  3301. using context = buffered_context<Char>;
  3302. basic_appender<Char> out;
  3303. void operator()(monostate) { report_error("argument not found"); }
  3304. template <typename T, FMT_ENABLE_IF(is_builtin<T>::value)>
  3305. void operator()(T value) {
  3306. write<Char>(out, value);
  3307. }
  3308. template <typename T, FMT_ENABLE_IF(!is_builtin<T>::value)>
  3309. void operator()(T) {
  3310. FMT_ASSERT(false, "");
  3311. }
  3312. void operator()(typename basic_format_arg<context>::handle h) {
  3313. // Use a null locale since the default format must be unlocalized.
  3314. auto parse_ctx = parse_context<Char>({});
  3315. auto format_ctx = context(out, {}, {});
  3316. h.format(parse_ctx, format_ctx);
  3317. }
  3318. };
  3319. template <typename Char> struct arg_formatter {
  3320. basic_appender<Char> out;
  3321. const format_specs& specs;
  3322. FMT_NO_UNIQUE_ADDRESS locale_ref locale;
  3323. template <typename T, FMT_ENABLE_IF(is_builtin<T>::value)>
  3324. FMT_CONSTEXPR FMT_INLINE void operator()(T value) {
  3325. detail::write<Char>(out, value, specs, locale);
  3326. }
  3327. template <typename T, FMT_ENABLE_IF(!is_builtin<T>::value)>
  3328. void operator()(T) {
  3329. FMT_ASSERT(false, "");
  3330. }
  3331. void operator()(typename basic_format_arg<buffered_context<Char>>::handle) {
  3332. // User-defined types are handled separately because they require access
  3333. // to the parse context.
  3334. }
  3335. };
  3336. struct dynamic_spec_getter {
  3337. template <typename T, FMT_ENABLE_IF(is_integer<T>::value)>
  3338. FMT_CONSTEXPR auto operator()(T value) -> unsigned long long {
  3339. return is_negative(value) ? ~0ull : static_cast<unsigned long long>(value);
  3340. }
  3341. template <typename T, FMT_ENABLE_IF(!is_integer<T>::value)>
  3342. FMT_CONSTEXPR auto operator()(T) -> unsigned long long {
  3343. report_error("width/precision is not integer");
  3344. return 0;
  3345. }
  3346. };
  3347. template <typename Context>
  3348. FMT_CONSTEXPR void handle_dynamic_spec(
  3349. arg_id_kind kind, int& value,
  3350. const arg_ref<typename Context::char_type>& ref, Context& ctx) {
  3351. if (kind == arg_id_kind::none) return;
  3352. auto arg =
  3353. kind == arg_id_kind::index ? ctx.arg(ref.index) : ctx.arg(ref.name);
  3354. if (!arg) report_error("argument not found");
  3355. unsigned long long result = arg.visit(dynamic_spec_getter());
  3356. if (result > to_unsigned(max_value<int>()))
  3357. report_error("width/precision is out of range");
  3358. value = static_cast<int>(result);
  3359. }
  3360. #if FMT_USE_NONTYPE_TEMPLATE_ARGS
  3361. template <typename T, typename Char, size_t N,
  3362. fmt::detail::fixed_string<Char, N> Str>
  3363. struct static_named_arg : view {
  3364. static constexpr auto name = Str.data;
  3365. const T& value;
  3366. static_named_arg(const T& v) : value(v) {}
  3367. };
  3368. template <typename T, typename Char, size_t N,
  3369. fmt::detail::fixed_string<Char, N> Str>
  3370. struct is_named_arg<static_named_arg<T, Char, N, Str>> : std::true_type {};
  3371. template <typename T, typename Char, size_t N,
  3372. fmt::detail::fixed_string<Char, N> Str>
  3373. struct is_static_named_arg<static_named_arg<T, Char, N, Str>> : std::true_type {
  3374. };
  3375. template <typename Char, size_t N, fmt::detail::fixed_string<Char, N> Str>
  3376. struct udl_arg {
  3377. template <typename T> auto operator=(T&& value) const {
  3378. return static_named_arg<T, Char, N, Str>(std::forward<T>(value));
  3379. }
  3380. };
  3381. #else
  3382. template <typename Char> struct udl_arg {
  3383. const Char* str;
  3384. template <typename T> auto operator=(T&& value) const -> named_arg<Char, T> {
  3385. return {str, std::forward<T>(value)};
  3386. }
  3387. };
  3388. #endif // FMT_USE_NONTYPE_TEMPLATE_ARGS
  3389. template <typename Char = char> struct format_handler {
  3390. parse_context<Char> parse_ctx;
  3391. buffered_context<Char> ctx;
  3392. void on_text(const Char* begin, const Char* end) {
  3393. copy_noinline<Char>(begin, end, ctx.out());
  3394. }
  3395. FMT_CONSTEXPR auto on_arg_id() -> int { return parse_ctx.next_arg_id(); }
  3396. FMT_CONSTEXPR auto on_arg_id(int id) -> int {
  3397. parse_ctx.check_arg_id(id);
  3398. return id;
  3399. }
  3400. FMT_CONSTEXPR auto on_arg_id(basic_string_view<Char> id) -> int {
  3401. parse_ctx.check_arg_id(id);
  3402. int arg_id = ctx.arg_id(id);
  3403. if (arg_id < 0) report_error("argument not found");
  3404. return arg_id;
  3405. }
  3406. FMT_INLINE void on_replacement_field(int id, const Char*) {
  3407. ctx.arg(id).visit(default_arg_formatter<Char>{ctx.out()});
  3408. }
  3409. auto on_format_specs(int id, const Char* begin, const Char* end)
  3410. -> const Char* {
  3411. auto arg = ctx.arg(id);
  3412. if (!arg) report_error("argument not found");
  3413. // Not using a visitor for custom types gives better codegen.
  3414. if (arg.format_custom(begin, parse_ctx, ctx)) return parse_ctx.begin();
  3415. auto specs = dynamic_format_specs<Char>();
  3416. begin = parse_format_specs(begin, end, specs, parse_ctx, arg.type());
  3417. if (specs.dynamic()) {
  3418. handle_dynamic_spec(specs.dynamic_width(), specs.width, specs.width_ref,
  3419. ctx);
  3420. handle_dynamic_spec(specs.dynamic_precision(), specs.precision,
  3421. specs.precision_ref, ctx);
  3422. }
  3423. arg.visit(arg_formatter<Char>{ctx.out(), specs, ctx.locale()});
  3424. return begin;
  3425. }
  3426. FMT_NORETURN void on_error(const char* message) { report_error(message); }
  3427. };
  3428. // It is used in format-inl.h and os.cc.
  3429. using format_func = void (*)(detail::buffer<char>&, int, const char*);
  3430. FMT_API void do_report_error(format_func func, int error_code,
  3431. const char* message) noexcept;
  3432. FMT_API void format_error_code(buffer<char>& out, int error_code,
  3433. string_view message) noexcept;
  3434. template <typename T, typename Char, type TYPE>
  3435. template <typename FormatContext>
  3436. FMT_CONSTEXPR auto native_formatter<T, Char, TYPE>::format(
  3437. const T& val, FormatContext& ctx) const -> decltype(ctx.out()) {
  3438. if (!specs_.dynamic())
  3439. return write<Char>(ctx.out(), val, specs_, ctx.locale());
  3440. auto specs = format_specs(specs_);
  3441. handle_dynamic_spec(specs.dynamic_width(), specs.width, specs_.width_ref,
  3442. ctx);
  3443. handle_dynamic_spec(specs.dynamic_precision(), specs.precision,
  3444. specs_.precision_ref, ctx);
  3445. return write<Char>(ctx.out(), val, specs, ctx.locale());
  3446. }
  3447. } // namespace detail
  3448. FMT_BEGIN_EXPORT
  3449. // A generic formatting context with custom output iterator and character
  3450. // (code unit) support. Char is the format string code unit type which can be
  3451. // different from OutputIt::value_type.
  3452. template <typename OutputIt, typename Char> class generic_context {
  3453. private:
  3454. OutputIt out_;
  3455. basic_format_args<generic_context> args_;
  3456. locale_ref loc_;
  3457. public:
  3458. using char_type = Char;
  3459. using iterator = OutputIt;
  3460. enum { builtin_types = FMT_BUILTIN_TYPES };
  3461. constexpr generic_context(OutputIt out,
  3462. basic_format_args<generic_context> args,
  3463. locale_ref loc = {})
  3464. : out_(out), args_(args), loc_(loc) {}
  3465. generic_context(generic_context&&) = default;
  3466. generic_context(const generic_context&) = delete;
  3467. void operator=(const generic_context&) = delete;
  3468. constexpr auto arg(int id) const -> basic_format_arg<generic_context> {
  3469. return args_.get(id);
  3470. }
  3471. auto arg(basic_string_view<Char> name) const
  3472. -> basic_format_arg<generic_context> {
  3473. return args_.get(name);
  3474. }
  3475. constexpr auto arg_id(basic_string_view<Char> name) const -> int {
  3476. return args_.get_id(name);
  3477. }
  3478. constexpr auto out() const -> iterator { return out_; }
  3479. void advance_to(iterator it) {
  3480. if (!detail::is_back_insert_iterator<iterator>()) out_ = it;
  3481. }
  3482. constexpr auto locale() const -> locale_ref { return loc_; }
  3483. };
  3484. class loc_value {
  3485. private:
  3486. basic_format_arg<context> value_;
  3487. public:
  3488. template <typename T, FMT_ENABLE_IF(!detail::is_float128<T>::value)>
  3489. loc_value(T value) : value_(value) {}
  3490. template <typename T, FMT_ENABLE_IF(detail::is_float128<T>::value)>
  3491. loc_value(T) {}
  3492. template <typename Visitor> auto visit(Visitor&& vis) -> decltype(vis(0)) {
  3493. return value_.visit(vis);
  3494. }
  3495. };
  3496. // A locale facet that formats values in UTF-8.
  3497. // It is parameterized on the locale to avoid the heavy <locale> include.
  3498. template <typename Locale> class format_facet : public Locale::facet {
  3499. private:
  3500. std::string separator_;
  3501. std::string grouping_;
  3502. std::string decimal_point_;
  3503. protected:
  3504. virtual auto do_put(appender out, loc_value val,
  3505. const format_specs& specs) const -> bool;
  3506. public:
  3507. static FMT_API typename Locale::id id;
  3508. explicit format_facet(Locale& loc);
  3509. explicit format_facet(string_view sep = "", std::string grouping = "\3",
  3510. std::string decimal_point = ".")
  3511. : separator_(sep.data(), sep.size()),
  3512. grouping_(grouping),
  3513. decimal_point_(decimal_point) {}
  3514. auto put(appender out, loc_value val, const format_specs& specs) const
  3515. -> bool {
  3516. return do_put(out, val, specs);
  3517. }
  3518. };
  3519. #define FMT_FORMAT_AS(Type, Base) \
  3520. template <typename Char> \
  3521. struct formatter<Type, Char> : formatter<Base, Char> { \
  3522. template <typename FormatContext> \
  3523. FMT_CONSTEXPR auto format(Type value, FormatContext& ctx) const \
  3524. -> decltype(ctx.out()) { \
  3525. return formatter<Base, Char>::format(value, ctx); \
  3526. } \
  3527. }
  3528. FMT_FORMAT_AS(signed char, int);
  3529. FMT_FORMAT_AS(unsigned char, unsigned);
  3530. FMT_FORMAT_AS(short, int);
  3531. FMT_FORMAT_AS(unsigned short, unsigned);
  3532. FMT_FORMAT_AS(long, detail::long_type);
  3533. FMT_FORMAT_AS(unsigned long, detail::ulong_type);
  3534. FMT_FORMAT_AS(Char*, const Char*);
  3535. FMT_FORMAT_AS(detail::std_string_view<Char>, basic_string_view<Char>);
  3536. FMT_FORMAT_AS(std::nullptr_t, const void*);
  3537. FMT_FORMAT_AS(void*, const void*);
  3538. template <typename Char, size_t N>
  3539. struct formatter<Char[N], Char> : formatter<basic_string_view<Char>, Char> {};
  3540. template <typename Char, typename Traits, typename Allocator>
  3541. class formatter<std::basic_string<Char, Traits, Allocator>, Char>
  3542. : public formatter<basic_string_view<Char>, Char> {};
  3543. template <int N, typename Char>
  3544. struct formatter<detail::bitint<N>, Char> : formatter<long long, Char> {};
  3545. template <int N, typename Char>
  3546. struct formatter<detail::ubitint<N>, Char>
  3547. : formatter<unsigned long long, Char> {};
  3548. template <typename Char>
  3549. struct formatter<detail::float128, Char>
  3550. : detail::native_formatter<detail::float128, Char,
  3551. detail::type::float_type> {};
  3552. template <typename T, typename Char>
  3553. struct formatter<T, Char, void_t<detail::format_as_result<T>>>
  3554. : formatter<detail::format_as_result<T>, Char> {
  3555. template <typename FormatContext>
  3556. FMT_CONSTEXPR auto format(const T& value, FormatContext& ctx) const
  3557. -> decltype(ctx.out()) {
  3558. auto&& val = format_as(value); // Make an lvalue reference for format.
  3559. return formatter<detail::format_as_result<T>, Char>::format(val, ctx);
  3560. }
  3561. };
  3562. /**
  3563. * Converts `p` to `const void*` for pointer formatting.
  3564. *
  3565. * **Example**:
  3566. *
  3567. * auto s = fmt::format("{}", fmt::ptr(p));
  3568. */
  3569. template <typename T> auto ptr(T p) -> const void* {
  3570. static_assert(std::is_pointer<T>::value, "fmt::ptr used with non-pointer");
  3571. return detail::bit_cast<const void*>(p);
  3572. }
  3573. /**
  3574. * Converts `e` to the underlying type.
  3575. *
  3576. * **Example**:
  3577. *
  3578. * enum class color { red, green, blue };
  3579. * auto s = fmt::format("{}", fmt::underlying(color::red)); // s == "0"
  3580. */
  3581. template <typename Enum>
  3582. constexpr auto underlying(Enum e) noexcept -> underlying_t<Enum> {
  3583. return static_cast<underlying_t<Enum>>(e);
  3584. }
  3585. namespace enums {
  3586. template <typename Enum, FMT_ENABLE_IF(std::is_enum<Enum>::value)>
  3587. constexpr auto format_as(Enum e) noexcept -> underlying_t<Enum> {
  3588. return static_cast<underlying_t<Enum>>(e);
  3589. }
  3590. } // namespace enums
  3591. #ifdef __cpp_lib_byte
  3592. template <typename Char>
  3593. struct formatter<std::byte, Char> : formatter<unsigned, Char> {
  3594. static auto format_as(std::byte b) -> unsigned char {
  3595. return static_cast<unsigned char>(b);
  3596. }
  3597. template <typename Context>
  3598. auto format(std::byte b, Context& ctx) const -> decltype(ctx.out()) {
  3599. return formatter<unsigned, Char>::format(format_as(b), ctx);
  3600. }
  3601. };
  3602. #endif
  3603. struct bytes {
  3604. string_view data;
  3605. inline explicit bytes(string_view s) : data(s) {}
  3606. };
  3607. template <> struct formatter<bytes> {
  3608. private:
  3609. detail::dynamic_format_specs<> specs_;
  3610. public:
  3611. FMT_CONSTEXPR auto parse(parse_context<>& ctx) -> const char* {
  3612. return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx,
  3613. detail::type::string_type);
  3614. }
  3615. template <typename FormatContext>
  3616. auto format(bytes b, FormatContext& ctx) const -> decltype(ctx.out()) {
  3617. auto specs = specs_;
  3618. detail::handle_dynamic_spec(specs.dynamic_width(), specs.width,
  3619. specs.width_ref, ctx);
  3620. detail::handle_dynamic_spec(specs.dynamic_precision(), specs.precision,
  3621. specs.precision_ref, ctx);
  3622. return detail::write_bytes<char>(ctx.out(), b.data, specs);
  3623. }
  3624. };
  3625. // group_digits_view is not derived from view because it copies the argument.
  3626. template <typename T> struct group_digits_view {
  3627. T value;
  3628. };
  3629. /**
  3630. * Returns a view that formats an integer value using ',' as a
  3631. * locale-independent thousands separator.
  3632. *
  3633. * **Example**:
  3634. *
  3635. * fmt::print("{}", fmt::group_digits(12345));
  3636. * // Output: "12,345"
  3637. */
  3638. template <typename T> auto group_digits(T value) -> group_digits_view<T> {
  3639. return {value};
  3640. }
  3641. template <typename T> struct formatter<group_digits_view<T>> : formatter<T> {
  3642. private:
  3643. detail::dynamic_format_specs<> specs_;
  3644. public:
  3645. FMT_CONSTEXPR auto parse(parse_context<>& ctx) -> const char* {
  3646. return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx,
  3647. detail::type::int_type);
  3648. }
  3649. template <typename FormatContext>
  3650. auto format(group_digits_view<T> view, FormatContext& ctx) const
  3651. -> decltype(ctx.out()) {
  3652. auto specs = specs_;
  3653. detail::handle_dynamic_spec(specs.dynamic_width(), specs.width,
  3654. specs.width_ref, ctx);
  3655. detail::handle_dynamic_spec(specs.dynamic_precision(), specs.precision,
  3656. specs.precision_ref, ctx);
  3657. auto arg = detail::make_write_int_arg(view.value, specs.sign());
  3658. return detail::write_int(
  3659. ctx.out(), static_cast<detail::uint64_or_128_t<T>>(arg.abs_value),
  3660. arg.prefix, specs, detail::digit_grouping<char>("\3", ","));
  3661. }
  3662. };
  3663. template <typename T, typename Char> struct nested_view {
  3664. const formatter<T, Char>* fmt;
  3665. const T* value;
  3666. };
  3667. template <typename T, typename Char>
  3668. struct formatter<nested_view<T, Char>, Char> {
  3669. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
  3670. return ctx.begin();
  3671. }
  3672. template <typename FormatContext>
  3673. auto format(nested_view<T, Char> view, FormatContext& ctx) const
  3674. -> decltype(ctx.out()) {
  3675. return view.fmt->format(*view.value, ctx);
  3676. }
  3677. };
  3678. template <typename T, typename Char = char> struct nested_formatter {
  3679. private:
  3680. basic_specs specs_;
  3681. int width_;
  3682. formatter<T, Char> formatter_;
  3683. public:
  3684. constexpr nested_formatter() : width_(0) {}
  3685. FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
  3686. auto it = ctx.begin(), end = ctx.end();
  3687. if (it == end) return it;
  3688. auto specs = format_specs();
  3689. it = detail::parse_align(it, end, specs);
  3690. specs_ = specs;
  3691. Char c = *it;
  3692. auto width_ref = detail::arg_ref<Char>();
  3693. if ((c >= '0' && c <= '9') || c == '{') {
  3694. it = detail::parse_width(it, end, specs, width_ref, ctx);
  3695. width_ = specs.width;
  3696. }
  3697. ctx.advance_to(it);
  3698. return formatter_.parse(ctx);
  3699. }
  3700. template <typename FormatContext, typename F>
  3701. auto write_padded(FormatContext& ctx, F write) const -> decltype(ctx.out()) {
  3702. if (width_ == 0) return write(ctx.out());
  3703. auto buf = basic_memory_buffer<Char>();
  3704. write(basic_appender<Char>(buf));
  3705. auto specs = format_specs();
  3706. specs.width = width_;
  3707. specs.copy_fill_from(specs_);
  3708. specs.set_align(specs_.align());
  3709. return detail::write<Char>(
  3710. ctx.out(), basic_string_view<Char>(buf.data(), buf.size()), specs);
  3711. }
  3712. auto nested(const T& value) const -> nested_view<T, Char> {
  3713. return nested_view<T, Char>{&formatter_, &value};
  3714. }
  3715. };
  3716. inline namespace literals {
  3717. #if FMT_USE_NONTYPE_TEMPLATE_ARGS
  3718. template <detail::fixed_string S> constexpr auto operator""_a() {
  3719. using char_t = remove_cvref_t<decltype(*S.data)>;
  3720. return detail::udl_arg<char_t, sizeof(S.data) / sizeof(char_t), S>();
  3721. }
  3722. #else
  3723. /**
  3724. * User-defined literal equivalent of `fmt::arg`.
  3725. *
  3726. * **Example**:
  3727. *
  3728. * using namespace fmt::literals;
  3729. * fmt::print("The answer is {answer}.", "answer"_a=42);
  3730. */
  3731. constexpr auto operator""_a(const char* s, size_t) -> detail::udl_arg<char> {
  3732. return {s};
  3733. }
  3734. #endif // FMT_USE_NONTYPE_TEMPLATE_ARGS
  3735. } // namespace literals
  3736. /// A fast integer formatter.
  3737. class format_int {
  3738. private:
  3739. // Buffer should be large enough to hold all digits (digits10 + 1),
  3740. // a sign and a null character.
  3741. enum { buffer_size = std::numeric_limits<unsigned long long>::digits10 + 3 };
  3742. mutable char buffer_[buffer_size];
  3743. char* str_;
  3744. template <typename UInt>
  3745. FMT_CONSTEXPR20 auto format_unsigned(UInt value) -> char* {
  3746. auto n = static_cast<detail::uint32_or_64_or_128_t<UInt>>(value);
  3747. return detail::do_format_decimal(buffer_, n, buffer_size - 1);
  3748. }
  3749. template <typename Int>
  3750. FMT_CONSTEXPR20 auto format_signed(Int value) -> char* {
  3751. auto abs_value = static_cast<detail::uint32_or_64_or_128_t<Int>>(value);
  3752. bool negative = value < 0;
  3753. if (negative) abs_value = 0 - abs_value;
  3754. auto begin = format_unsigned(abs_value);
  3755. if (negative) *--begin = '-';
  3756. return begin;
  3757. }
  3758. public:
  3759. FMT_CONSTEXPR20 explicit format_int(int value) : str_(format_signed(value)) {}
  3760. FMT_CONSTEXPR20 explicit format_int(long value)
  3761. : str_(format_signed(value)) {}
  3762. FMT_CONSTEXPR20 explicit format_int(long long value)
  3763. : str_(format_signed(value)) {}
  3764. FMT_CONSTEXPR20 explicit format_int(unsigned value)
  3765. : str_(format_unsigned(value)) {}
  3766. FMT_CONSTEXPR20 explicit format_int(unsigned long value)
  3767. : str_(format_unsigned(value)) {}
  3768. FMT_CONSTEXPR20 explicit format_int(unsigned long long value)
  3769. : str_(format_unsigned(value)) {}
  3770. /// Returns the number of characters written to the output buffer.
  3771. FMT_CONSTEXPR20 auto size() const -> size_t {
  3772. return detail::to_unsigned(buffer_ - str_ + buffer_size - 1);
  3773. }
  3774. /// Returns a pointer to the output buffer content. No terminating null
  3775. /// character is appended.
  3776. FMT_CONSTEXPR20 auto data() const -> const char* { return str_; }
  3777. /// Returns a pointer to the output buffer content with terminating null
  3778. /// character appended.
  3779. FMT_CONSTEXPR20 auto c_str() const -> const char* {
  3780. buffer_[buffer_size - 1] = '\0';
  3781. return str_;
  3782. }
  3783. /// Returns the content of the output buffer as an `std::string`.
  3784. inline auto str() const -> std::string { return {str_, size()}; }
  3785. };
  3786. #if FMT_CLANG_ANALYZER
  3787. # define FMT_STRING_IMPL(s, base) s
  3788. #else
  3789. # define FMT_STRING_IMPL(s, base) \
  3790. [] { \
  3791. /* Use the hidden visibility as a workaround for a GCC bug (#1973). */ \
  3792. /* Use a macro-like name to avoid shadowing warnings. */ \
  3793. struct FMT_VISIBILITY("hidden") FMT_COMPILE_STRING : base { \
  3794. using char_type = fmt::remove_cvref_t<decltype(s[0])>; \
  3795. constexpr explicit operator fmt::basic_string_view<char_type>() \
  3796. const { \
  3797. return fmt::detail::compile_string_to_view<char_type>(s); \
  3798. } \
  3799. }; \
  3800. using FMT_STRING_VIEW = \
  3801. fmt::basic_string_view<typename FMT_COMPILE_STRING::char_type>; \
  3802. fmt::detail::ignore_unused(FMT_STRING_VIEW(FMT_COMPILE_STRING())); \
  3803. return FMT_COMPILE_STRING(); \
  3804. }()
  3805. #endif // FMT_CLANG_ANALYZER
  3806. /**
  3807. * Constructs a legacy compile-time format string from a string literal `s`.
  3808. *
  3809. * **Example**:
  3810. *
  3811. * // A compile-time error because 'd' is an invalid specifier for strings.
  3812. * std::string s = fmt::format(FMT_STRING("{:d}"), "foo");
  3813. */
  3814. #define FMT_STRING(s) FMT_STRING_IMPL(s, fmt::detail::compile_string)
  3815. FMT_API auto vsystem_error(int error_code, string_view fmt, format_args args)
  3816. -> std::system_error;
  3817. /**
  3818. * Constructs `std::system_error` with a message formatted with
  3819. * `fmt::format(fmt, args...)`.
  3820. * `error_code` is a system error code as given by `errno`.
  3821. *
  3822. * **Example**:
  3823. *
  3824. * // This throws std::system_error with the description
  3825. * // cannot open file 'madeup': No such file or directory
  3826. * // or similar (system message may vary).
  3827. * const char* filename = "madeup";
  3828. * FILE* file = fopen(filename, "r");
  3829. * if (!file)
  3830. * throw fmt::system_error(errno, "cannot open file '{}'", filename);
  3831. */
  3832. template <typename... T>
  3833. auto system_error(int error_code, format_string<T...> fmt, T&&... args)
  3834. -> std::system_error {
  3835. return vsystem_error(error_code, fmt.str, vargs<T...>{{args...}});
  3836. }
  3837. /**
  3838. * Formats an error message for an error returned by an operating system or a
  3839. * language runtime, for example a file opening error, and writes it to `out`.
  3840. * The format is the same as the one used by `std::system_error(ec, message)`
  3841. * where `ec` is `std::error_code(error_code, std::generic_category())`.
  3842. * It is implementation-defined but normally looks like:
  3843. *
  3844. * <message>: <system-message>
  3845. *
  3846. * where `<message>` is the passed message and `<system-message>` is the system
  3847. * message corresponding to the error code.
  3848. * `error_code` is a system error code as given by `errno`.
  3849. */
  3850. FMT_API void format_system_error(detail::buffer<char>& out, int error_code,
  3851. const char* message) noexcept;
  3852. // Reports a system error without throwing an exception.
  3853. // Can be used to report errors from destructors.
  3854. FMT_API void report_system_error(int error_code, const char* message) noexcept;
  3855. inline auto vformat(locale_ref loc, string_view fmt, format_args args)
  3856. -> std::string {
  3857. auto buf = memory_buffer();
  3858. detail::vformat_to(buf, fmt, args, loc);
  3859. return {buf.data(), buf.size()};
  3860. }
  3861. template <typename... T>
  3862. FMT_INLINE auto format(locale_ref loc, format_string<T...> fmt, T&&... args)
  3863. -> std::string {
  3864. return vformat(loc, fmt.str, vargs<T...>{{args...}});
  3865. }
  3866. template <typename OutputIt,
  3867. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
  3868. auto vformat_to(OutputIt out, locale_ref loc, string_view fmt, format_args args)
  3869. -> OutputIt {
  3870. auto&& buf = detail::get_buffer<char>(out);
  3871. detail::vformat_to(buf, fmt, args, loc);
  3872. return detail::get_iterator(buf, out);
  3873. }
  3874. template <typename OutputIt, typename... T,
  3875. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
  3876. FMT_INLINE auto format_to(OutputIt out, locale_ref loc, format_string<T...> fmt,
  3877. T&&... args) -> OutputIt {
  3878. return fmt::vformat_to(out, loc, fmt.str, vargs<T...>{{args...}});
  3879. }
  3880. template <typename... T>
  3881. FMT_NODISCARD FMT_INLINE auto formatted_size(locale_ref loc,
  3882. format_string<T...> fmt,
  3883. T&&... args) -> size_t {
  3884. auto buf = detail::counting_buffer<>();
  3885. detail::vformat_to(buf, fmt.str, vargs<T...>{{args...}}, loc);
  3886. return buf.count();
  3887. }
  3888. FMT_API auto vformat(string_view fmt, format_args args) -> std::string;
  3889. /**
  3890. * Formats `args` according to specifications in `fmt` and returns the result
  3891. * as a string.
  3892. *
  3893. * **Example**:
  3894. *
  3895. * #include <fmt/format.h>
  3896. * std::string message = fmt::format("The answer is {}.", 42);
  3897. */
  3898. template <typename... T>
  3899. FMT_NODISCARD FMT_INLINE auto format(format_string<T...> fmt, T&&... args)
  3900. -> std::string {
  3901. return vformat(fmt.str, vargs<T...>{{args...}});
  3902. }
  3903. /**
  3904. * Converts `value` to `std::string` using the default format for type `T`.
  3905. *
  3906. * **Example**:
  3907. *
  3908. * std::string answer = fmt::to_string(42);
  3909. */
  3910. template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
  3911. FMT_NODISCARD FMT_CONSTEXPR_STRING auto to_string(T value) -> std::string {
  3912. // The buffer should be large enough to store the number including the sign
  3913. // or "false" for bool.
  3914. char buffer[max_of(detail::digits10<T>() + 2, 5)];
  3915. return {buffer, detail::write<char>(buffer, value)};
  3916. }
  3917. template <typename T, FMT_ENABLE_IF(detail::use_format_as<T>::value)>
  3918. FMT_NODISCARD FMT_CONSTEXPR_STRING auto to_string(const T& value)
  3919. -> std::string {
  3920. return to_string(format_as(value));
  3921. }
  3922. template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value &&
  3923. !detail::use_format_as<T>::value)>
  3924. FMT_NODISCARD FMT_CONSTEXPR_STRING auto to_string(const T& value)
  3925. -> std::string {
  3926. auto buffer = memory_buffer();
  3927. detail::write<char>(appender(buffer), value);
  3928. return {buffer.data(), buffer.size()};
  3929. }
  3930. FMT_END_EXPORT
  3931. FMT_END_NAMESPACE
  3932. #ifdef FMT_HEADER_ONLY
  3933. # define FMT_FUNC inline
  3934. # include "format-inl.h"
  3935. #endif
  3936. // Restore _LIBCPP_REMOVE_TRANSITIVE_INCLUDES.
  3937. #ifdef FMT_REMOVE_TRANSITIVE_INCLUDES
  3938. # undef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
  3939. #endif
  3940. #endif // FMT_FORMAT_H_
  3941. #else
  3942. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  3943. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)