Number.Formatting.cs 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Buffers.Text;
  5. using System.Diagnostics;
  6. using System.Globalization;
  7. using System.Runtime.CompilerServices;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. namespace System
  11. {
  12. // The Format methods provided by the numeric classes convert
  13. // the numeric value to a string using the format string given by the
  14. // format parameter. If the format parameter is null or
  15. // an empty string, the number is formatted as if the string "G" (general
  16. // format) was specified. The info parameter specifies the
  17. // NumberFormatInfo instance to use when formatting the number. If the
  18. // info parameter is null or omitted, the numeric formatting information
  19. // is obtained from the current culture. The NumberFormatInfo supplies
  20. // such information as the characters to use for decimal and thousand
  21. // separators, and the spelling and placement of currency symbols in monetary
  22. // values.
  23. //
  24. // Format strings fall into two categories: Standard format strings and
  25. // user-defined format strings. A format string consisting of a single
  26. // alphabetic character (A-Z or a-z), optionally followed by a sequence of
  27. // digits (0-9), is a standard format string. All other format strings are
  28. // used-defined format strings.
  29. //
  30. // A standard format string takes the form Axx, where A is an
  31. // alphabetic character called the format specifier and xx is a
  32. // sequence of digits called the precision specifier. The format
  33. // specifier controls the type of formatting applied to the number and the
  34. // precision specifier controls the number of significant digits or decimal
  35. // places of the formatting operation. The following table describes the
  36. // supported standard formats.
  37. //
  38. // C c - Currency format. The number is
  39. // converted to a string that represents a currency amount. The conversion is
  40. // controlled by the currency format information of the NumberFormatInfo
  41. // used to format the number. The precision specifier indicates the desired
  42. // number of decimal places. If the precision specifier is omitted, the default
  43. // currency precision given by the NumberFormatInfo is used.
  44. //
  45. // D d - Decimal format. This format is
  46. // supported for integral types only. The number is converted to a string of
  47. // decimal digits, prefixed by a minus sign if the number is negative. The
  48. // precision specifier indicates the minimum number of digits desired in the
  49. // resulting string. If required, the number will be left-padded with zeros to
  50. // produce the number of digits given by the precision specifier.
  51. //
  52. // E e Engineering (scientific) format.
  53. // The number is converted to a string of the form
  54. // "-d.ddd...E+ddd" or "-d.ddd...e+ddd", where each
  55. // 'd' indicates a digit (0-9). The string starts with a minus sign if the
  56. // number is negative, and one digit always precedes the decimal point. The
  57. // precision specifier indicates the desired number of digits after the decimal
  58. // point. If the precision specifier is omitted, a default of 6 digits after
  59. // the decimal point is used. The format specifier indicates whether to prefix
  60. // the exponent with an 'E' or an 'e'. The exponent is always consists of a
  61. // plus or minus sign and three digits.
  62. //
  63. // F f Fixed point format. The number is
  64. // converted to a string of the form "-ddd.ddd....", where each
  65. // 'd' indicates a digit (0-9). The string starts with a minus sign if the
  66. // number is negative. The precision specifier indicates the desired number of
  67. // decimal places. If the precision specifier is omitted, the default numeric
  68. // precision given by the NumberFormatInfo is used.
  69. //
  70. // G g - General format. The number is
  71. // converted to the shortest possible decimal representation using fixed point
  72. // or scientific format. The precision specifier determines the number of
  73. // significant digits in the resulting string. If the precision specifier is
  74. // omitted, the number of significant digits is determined by the type of the
  75. // number being converted (10 for int, 19 for long, 7 for
  76. // float, 15 for double, 19 for Currency, and 29 for
  77. // Decimal). Trailing zeros after the decimal point are removed, and the
  78. // resulting string contains a decimal point only if required. The resulting
  79. // string uses fixed point format if the exponent of the number is less than
  80. // the number of significant digits and greater than or equal to -4. Otherwise,
  81. // the resulting string uses scientific format, and the case of the format
  82. // specifier controls whether the exponent is prefixed with an 'E' or an 'e'.
  83. //
  84. // N n Number format. The number is
  85. // converted to a string of the form "-d,ddd,ddd.ddd....", where
  86. // each 'd' indicates a digit (0-9). The string starts with a minus sign if the
  87. // number is negative. Thousand separators are inserted between each group of
  88. // three digits to the left of the decimal point. The precision specifier
  89. // indicates the desired number of decimal places. If the precision specifier
  90. // is omitted, the default numeric precision given by the
  91. // NumberFormatInfo is used.
  92. //
  93. // X x - Hexadecimal format. This format is
  94. // supported for integral types only. The number is converted to a string of
  95. // hexadecimal digits. The format specifier indicates whether to use upper or
  96. // lower case characters for the hexadecimal digits above 9 ('X' for 'ABCDEF',
  97. // and 'x' for 'abcdef'). The precision specifier indicates the minimum number
  98. // of digits desired in the resulting string. If required, the number will be
  99. // left-padded with zeros to produce the number of digits given by the
  100. // precision specifier.
  101. //
  102. // Some examples of standard format strings and their results are shown in the
  103. // table below. (The examples all assume a default NumberFormatInfo.)
  104. //
  105. // Value Format Result
  106. // 12345.6789 C $12,345.68
  107. // -12345.6789 C ($12,345.68)
  108. // 12345 D 12345
  109. // 12345 D8 00012345
  110. // 12345.6789 E 1.234568E+004
  111. // 12345.6789 E10 1.2345678900E+004
  112. // 12345.6789 e4 1.2346e+004
  113. // 12345.6789 F 12345.68
  114. // 12345.6789 F0 12346
  115. // 12345.6789 F6 12345.678900
  116. // 12345.6789 G 12345.6789
  117. // 12345.6789 G7 12345.68
  118. // 123456789 G7 1.234568E8
  119. // 12345.6789 N 12,345.68
  120. // 123456789 N4 123,456,789.0000
  121. // 0x2c45e x 2c45e
  122. // 0x2c45e X 2C45E
  123. // 0x2c45e X8 0002C45E
  124. //
  125. // Format strings that do not start with an alphabetic character, or that start
  126. // with an alphabetic character followed by a non-digit, are called
  127. // user-defined format strings. The following table describes the formatting
  128. // characters that are supported in user defined format strings.
  129. //
  130. //
  131. // 0 - Digit placeholder. If the value being
  132. // formatted has a digit in the position where the '0' appears in the format
  133. // string, then that digit is copied to the output string. Otherwise, a '0' is
  134. // stored in that position in the output string. The position of the leftmost
  135. // '0' before the decimal point and the rightmost '0' after the decimal point
  136. // determines the range of digits that are always present in the output
  137. // string.
  138. //
  139. // # - Digit placeholder. If the value being
  140. // formatted has a digit in the position where the '#' appears in the format
  141. // string, then that digit is copied to the output string. Otherwise, nothing
  142. // is stored in that position in the output string.
  143. //
  144. // . - Decimal point. The first '.' character
  145. // in the format string determines the location of the decimal separator in the
  146. // formatted value; any additional '.' characters are ignored. The actual
  147. // character used as a the decimal separator in the output string is given by
  148. // the NumberFormatInfo used to format the number.
  149. //
  150. // , - Thousand separator and number scaling.
  151. // The ',' character serves two purposes. First, if the format string contains
  152. // a ',' character between two digit placeholders (0 or #) and to the left of
  153. // the decimal point if one is present, then the output will have thousand
  154. // separators inserted between each group of three digits to the left of the
  155. // decimal separator. The actual character used as a the decimal separator in
  156. // the output string is given by the NumberFormatInfo used to format the
  157. // number. Second, if the format string contains one or more ',' characters
  158. // immediately to the left of the decimal point, or after the last digit
  159. // placeholder if there is no decimal point, then the number will be divided by
  160. // 1000 times the number of ',' characters before it is formatted. For example,
  161. // the format string '0,,' will represent 100 million as just 100. Use of the
  162. // ',' character to indicate scaling does not also cause the formatted number
  163. // to have thousand separators. Thus, to scale a number by 1 million and insert
  164. // thousand separators you would use the format string '#,##0,,'.
  165. //
  166. // % - Percentage placeholder. The presence of
  167. // a '%' character in the format string causes the number to be multiplied by
  168. // 100 before it is formatted. The '%' character itself is inserted in the
  169. // output string where it appears in the format string.
  170. //
  171. // E+ E- e+ e- - Scientific notation.
  172. // If any of the strings 'E+', 'E-', 'e+', or 'e-' are present in the format
  173. // string and are immediately followed by at least one '0' character, then the
  174. // number is formatted using scientific notation with an 'E' or 'e' inserted
  175. // between the number and the exponent. The number of '0' characters following
  176. // the scientific notation indicator determines the minimum number of digits to
  177. // output for the exponent. The 'E+' and 'e+' formats indicate that a sign
  178. // character (plus or minus) should always precede the exponent. The 'E-' and
  179. // 'e-' formats indicate that a sign character should only precede negative
  180. // exponents.
  181. //
  182. // \ - Literal character. A backslash character
  183. // causes the next character in the format string to be copied to the output
  184. // string as-is. The backslash itself isn't copied, so to place a backslash
  185. // character in the output string, use two backslashes (\\) in the format
  186. // string.
  187. //
  188. // 'ABC' "ABC" - Literal string. Characters
  189. // enclosed in single or double quotation marks are copied to the output string
  190. // as-is and do not affect formatting.
  191. //
  192. // ; - Section separator. The ';' character is
  193. // used to separate sections for positive, negative, and zero numbers in the
  194. // format string.
  195. //
  196. // Other - All other characters are copied to
  197. // the output string in the position they appear.
  198. //
  199. // For fixed point formats (formats not containing an 'E+', 'E-', 'e+', or
  200. // 'e-'), the number is rounded to as many decimal places as there are digit
  201. // placeholders to the right of the decimal point. If the format string does
  202. // not contain a decimal point, the number is rounded to the nearest
  203. // integer. If the number has more digits than there are digit placeholders to
  204. // the left of the decimal point, the extra digits are copied to the output
  205. // string immediately before the first digit placeholder.
  206. //
  207. // For scientific formats, the number is rounded to as many significant digits
  208. // as there are digit placeholders in the format string.
  209. //
  210. // To allow for different formatting of positive, negative, and zero values, a
  211. // user-defined format string may contain up to three sections separated by
  212. // semicolons. The results of having one, two, or three sections in the format
  213. // string are described in the table below.
  214. //
  215. // Sections:
  216. //
  217. // One - The format string applies to all values.
  218. //
  219. // Two - The first section applies to positive values
  220. // and zeros, and the second section applies to negative values. If the number
  221. // to be formatted is negative, but becomes zero after rounding according to
  222. // the format in the second section, then the resulting zero is formatted
  223. // according to the first section.
  224. //
  225. // Three - The first section applies to positive
  226. // values, the second section applies to negative values, and the third section
  227. // applies to zeros. The second section may be left empty (by having no
  228. // characters between the semicolons), in which case the first section applies
  229. // to all non-zero values. If the number to be formatted is non-zero, but
  230. // becomes zero after rounding according to the format in the first or second
  231. // section, then the resulting zero is formatted according to the third
  232. // section.
  233. //
  234. // For both standard and user-defined formatting operations on values of type
  235. // float and double, if the value being formatted is a NaN (Not
  236. // a Number) or a positive or negative infinity, then regardless of the format
  237. // string, the resulting string is given by the NaNSymbol,
  238. // PositiveInfinitySymbol, or NegativeInfinitySymbol property of
  239. // the NumberFormatInfo used to format the number.
  240. internal static partial class Number
  241. {
  242. internal const int DecimalPrecision = 29; // Decimal.DecCalc also uses this value
  243. // SinglePrecision and DoublePrecision represent the maximum number of digits required
  244. // to guarantee that any given Single or Double can roundtrip. Some numbers may require
  245. // less, but none will require more.
  246. private const int SinglePrecision = 9;
  247. private const int DoublePrecision = 17;
  248. // SinglePrecisionCustomFormat and DoublePrecisionCustomFormat are used to ensure that
  249. // custom format strings return the same string as in previous releases when the format
  250. // would return x digits or less (where x is the value of the corresponding constant).
  251. // In order to support more digits, we would need to update ParseFormatSpecifier to pre-parse
  252. // the format and determine exactly how many digits are being requested and whether they
  253. // represent "significant digits" or "digits after the decimal point".
  254. private const int SinglePrecisionCustomFormat = 7;
  255. private const int DoublePrecisionCustomFormat = 15;
  256. private const int DefaultPrecisionExponentialFormat = 6;
  257. private const int MaxUInt32DecDigits = 10;
  258. private const int CharStackBufferSize = 32;
  259. private const string PosNumberFormat = "#";
  260. private static readonly string[] s_singleDigitStringCache = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
  261. private static readonly string[] s_posCurrencyFormats =
  262. {
  263. "$#", "#$", "$ #", "# $"
  264. };
  265. private static readonly string[] s_negCurrencyFormats =
  266. {
  267. "($#)", "-$#", "$-#", "$#-",
  268. "(#$)", "-#$", "#-$", "#$-",
  269. "-# $", "-$ #", "# $-", "$ #-",
  270. "$ -#", "#- $", "($ #)", "(# $)"
  271. };
  272. private static readonly string[] s_posPercentFormats =
  273. {
  274. "# %", "#%", "%#", "% #"
  275. };
  276. private static readonly string[] s_negPercentFormats =
  277. {
  278. "-# %", "-#%", "-%#",
  279. "%-#", "%#-",
  280. "#-%", "#%-",
  281. "-% #", "# %-", "% #-",
  282. "% -#", "#- %"
  283. };
  284. private static readonly string[] s_negNumberFormats =
  285. {
  286. "(#)", "-#", "- #", "#-", "# -",
  287. };
  288. public static unsafe string FormatDecimal(decimal value, ReadOnlySpan<char> format, NumberFormatInfo info)
  289. {
  290. char fmt = ParseFormatSpecifier(format, out int digits);
  291. byte* pDigits = stackalloc byte[DecimalNumberBufferLength];
  292. NumberBuffer number = new NumberBuffer(NumberBufferKind.Decimal, pDigits, DecimalNumberBufferLength);
  293. DecimalToNumber(ref value, ref number);
  294. char* stackPtr = stackalloc char[CharStackBufferSize];
  295. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  296. if (fmt != 0)
  297. {
  298. NumberToString(ref sb, ref number, fmt, digits, info);
  299. }
  300. else
  301. {
  302. NumberToStringFormat(ref sb, ref number, format, info);
  303. }
  304. return sb.ToString();
  305. }
  306. public static unsafe bool TryFormatDecimal(decimal value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
  307. {
  308. char fmt = ParseFormatSpecifier(format, out int digits);
  309. byte* pDigits = stackalloc byte[DecimalNumberBufferLength];
  310. NumberBuffer number = new NumberBuffer(NumberBufferKind.Decimal, pDigits, DecimalNumberBufferLength);
  311. DecimalToNumber(ref value, ref number);
  312. char* stackPtr = stackalloc char[CharStackBufferSize];
  313. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  314. if (fmt != 0)
  315. {
  316. NumberToString(ref sb, ref number, fmt, digits, info);
  317. }
  318. else
  319. {
  320. NumberToStringFormat(ref sb, ref number, format, info);
  321. }
  322. return sb.TryCopyTo(destination, out charsWritten);
  323. }
  324. internal static unsafe void DecimalToNumber(ref decimal d, ref NumberBuffer number)
  325. {
  326. byte* buffer = number.GetDigitsPointer();
  327. number.DigitsCount = DecimalPrecision;
  328. number.IsNegative = d.IsNegative;
  329. byte* p = buffer + DecimalPrecision;
  330. while ((d.Mid | d.High) != 0)
  331. {
  332. p = UInt32ToDecChars(p, decimal.DecDivMod1E9(ref d), 9);
  333. }
  334. p = UInt32ToDecChars(p, d.Low, 0);
  335. int i = (int)((buffer + DecimalPrecision) - p);
  336. number.DigitsCount = i;
  337. number.Scale = i - d.Scale;
  338. byte* dst = number.GetDigitsPointer();
  339. while (--i >= 0)
  340. {
  341. *dst++ = *p++;
  342. }
  343. *dst = (byte)('\0');
  344. number.CheckConsistency();
  345. }
  346. public static string FormatDouble(double value, string? format, NumberFormatInfo info)
  347. {
  348. Span<char> stackBuffer = stackalloc char[CharStackBufferSize];
  349. var sb = new ValueStringBuilder(stackBuffer);
  350. return FormatDouble(ref sb, value, format, info) ?? sb.ToString();
  351. }
  352. public static bool TryFormatDouble(double value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
  353. {
  354. Span<char> stackBuffer = stackalloc char[CharStackBufferSize];
  355. var sb = new ValueStringBuilder(stackBuffer);
  356. string? s = FormatDouble(ref sb, value, format, info);
  357. return s != null ?
  358. TryCopyTo(s, destination, out charsWritten) :
  359. sb.TryCopyTo(destination, out charsWritten);
  360. }
  361. private static int GetFloatingPointMaxDigitsAndPrecision(char fmt, ref int precision, NumberFormatInfo info, out bool isSignificantDigits)
  362. {
  363. if (fmt == 0)
  364. {
  365. isSignificantDigits = true;
  366. return precision;
  367. }
  368. int maxDigits = precision;
  369. switch (fmt)
  370. {
  371. case 'C':
  372. case 'c':
  373. {
  374. // The currency format uses the precision specifier to indicate the number of
  375. // decimal digits to format. This defaults to NumberFormatInfo.CurrencyDecimalDigits.
  376. if (precision == -1)
  377. {
  378. precision = info.CurrencyDecimalDigits;
  379. }
  380. isSignificantDigits = false;
  381. break;
  382. }
  383. case 'E':
  384. case 'e':
  385. {
  386. // The exponential format uses the precision specifier to indicate the number of
  387. // decimal digits to format. This defaults to 6. However, the exponential format
  388. // also always formats a single integral digit, so we need to increase the precision
  389. // specifier and treat it as the number of significant digits to account for this.
  390. if (precision == -1)
  391. {
  392. precision = DefaultPrecisionExponentialFormat;
  393. }
  394. precision++;
  395. isSignificantDigits = true;
  396. break;
  397. }
  398. case 'F':
  399. case 'f':
  400. case 'N':
  401. case 'n':
  402. {
  403. // The fixed-point and number formats use the precision specifier to indicate the number
  404. // of decimal digits to format. This defaults to NumberFormatInfo.NumberDecimalDigits.
  405. if (precision == -1)
  406. {
  407. precision = info.NumberDecimalDigits;
  408. }
  409. isSignificantDigits = false;
  410. break;
  411. }
  412. case 'G':
  413. case 'g':
  414. {
  415. // The general format uses the precision specifier to indicate the number of significant
  416. // digits to format. This defaults to the shortest roundtrippable string. Additionally,
  417. // given that we can't return zero significant digits, we treat 0 as returning the shortest
  418. // roundtrippable string as well.
  419. if (precision == 0)
  420. {
  421. precision = -1;
  422. }
  423. isSignificantDigits = true;
  424. break;
  425. }
  426. case 'P':
  427. case 'p':
  428. {
  429. // The percent format uses the precision specifier to indicate the number of
  430. // decimal digits to format. This defaults to NumberFormatInfo.PercentDecimalDigits.
  431. // However, the percent format also always multiplies the number by 100, so we need
  432. // to increase the precision specifier to ensure we get the appropriate number of digits.
  433. if (precision == -1)
  434. {
  435. precision = info.PercentDecimalDigits;
  436. }
  437. precision += 2;
  438. isSignificantDigits = false;
  439. break;
  440. }
  441. case 'R':
  442. case 'r':
  443. {
  444. // The roundtrip format ignores the precision specifier and always returns the shortest
  445. // roundtrippable string.
  446. precision = -1;
  447. isSignificantDigits = true;
  448. break;
  449. }
  450. default:
  451. {
  452. throw new FormatException(SR.Argument_BadFormatSpecifier);
  453. }
  454. }
  455. return maxDigits;
  456. }
  457. /// <summary>Formats the specified value according to the specified format and info.</summary>
  458. /// <returns>
  459. /// Non-null if an existing string can be returned, in which case the builder will be unmodified.
  460. /// Null if no existing string was returned, in which case the formatted output is in the builder.
  461. /// </returns>
  462. private static unsafe string? FormatDouble(ref ValueStringBuilder sb, double value, ReadOnlySpan<char> format, NumberFormatInfo info)
  463. {
  464. if (!double.IsFinite(value))
  465. {
  466. if (double.IsNaN(value))
  467. {
  468. return info.NaNSymbol;
  469. }
  470. return double.IsNegative(value) ? info.NegativeInfinitySymbol : info.PositiveInfinitySymbol;
  471. }
  472. char fmt = ParseFormatSpecifier(format, out int precision);
  473. byte* pDigits = stackalloc byte[DoubleNumberBufferLength];
  474. if (fmt == '\0')
  475. {
  476. // For back-compat we currently specially treat the precision for custom
  477. // format specifiers. The constant has more details as to why.
  478. precision = DoublePrecisionCustomFormat;
  479. }
  480. NumberBuffer number = new NumberBuffer(NumberBufferKind.FloatingPoint, pDigits, DoubleNumberBufferLength);
  481. number.IsNegative = double.IsNegative(value);
  482. // We need to track the original precision requested since some formats
  483. // accept values like 0 and others may require additional fixups.
  484. int nMaxDigits = GetFloatingPointMaxDigitsAndPrecision(fmt, ref precision, info, out bool isSignificantDigits);
  485. if ((value != 0.0) && (!isSignificantDigits || !Grisu3.TryRunDouble(value, precision, ref number)))
  486. {
  487. Dragon4Double(value, precision, isSignificantDigits, ref number);
  488. }
  489. number.CheckConsistency();
  490. // When the number is known to be roundtrippable (either because we requested it be, or
  491. // because we know we have enough digits to satisfy roundtrippability), we should validate
  492. // that the number actually roundtrips back to the original result.
  493. Debug.Assert(((precision != -1) && (precision < DoublePrecision)) || (BitConverter.DoubleToInt64Bits(value) == BitConverter.DoubleToInt64Bits(NumberToDouble(ref number))));
  494. if (fmt != 0)
  495. {
  496. if (precision == -1)
  497. {
  498. Debug.Assert((fmt == 'G') || (fmt == 'g') || (fmt == 'R') || (fmt == 'r'));
  499. // For the roundtrip and general format specifiers, when returning the shortest roundtrippable
  500. // string, we need to update the maximum number of digits to be the greater of number.DigitsCount
  501. // or DoublePrecision. This ensures that we continue returning "pretty" strings for values with
  502. // less digits. One example this fixes is "-60", which would otherwise be formatted as "-6E+01"
  503. // since DigitsCount would be 1 and the formatter would almost immediately switch to scientific notation.
  504. nMaxDigits = Math.Max(number.DigitsCount, DoublePrecision);
  505. }
  506. NumberToString(ref sb, ref number, fmt, nMaxDigits, info);
  507. }
  508. else
  509. {
  510. Debug.Assert(precision == DoublePrecisionCustomFormat);
  511. NumberToStringFormat(ref sb, ref number, format, info);
  512. }
  513. return null;
  514. }
  515. public static string FormatSingle(float value, string? format, NumberFormatInfo info)
  516. {
  517. Span<char> stackBuffer = stackalloc char[CharStackBufferSize];
  518. var sb = new ValueStringBuilder(stackBuffer);
  519. return FormatSingle(ref sb, value, format, info) ?? sb.ToString();
  520. }
  521. public static bool TryFormatSingle(float value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
  522. {
  523. Span<char> stackBuffer = stackalloc char[CharStackBufferSize];
  524. var sb = new ValueStringBuilder(stackBuffer);
  525. string? s = FormatSingle(ref sb, value, format, info);
  526. return s != null ?
  527. TryCopyTo(s, destination, out charsWritten) :
  528. sb.TryCopyTo(destination, out charsWritten);
  529. }
  530. /// <summary>Formats the specified value according to the specified format and info.</summary>
  531. /// <returns>
  532. /// Non-null if an existing string can be returned, in which case the builder will be unmodified.
  533. /// Null if no existing string was returned, in which case the formatted output is in the builder.
  534. /// </returns>
  535. private static unsafe string? FormatSingle(ref ValueStringBuilder sb, float value, ReadOnlySpan<char> format, NumberFormatInfo info)
  536. {
  537. if (!float.IsFinite(value))
  538. {
  539. if (float.IsNaN(value))
  540. {
  541. return info.NaNSymbol;
  542. }
  543. return float.IsNegative(value) ? info.NegativeInfinitySymbol : info.PositiveInfinitySymbol;
  544. }
  545. char fmt = ParseFormatSpecifier(format, out int precision);
  546. byte* pDigits = stackalloc byte[SingleNumberBufferLength];
  547. if (fmt == '\0')
  548. {
  549. // For back-compat we currently specially treat the precision for custom
  550. // format specifiers. The constant has more details as to why.
  551. precision = SinglePrecisionCustomFormat;
  552. }
  553. NumberBuffer number = new NumberBuffer(NumberBufferKind.FloatingPoint, pDigits, SingleNumberBufferLength);
  554. number.IsNegative = float.IsNegative(value);
  555. // We need to track the original precision requested since some formats
  556. // accept values like 0 and others may require additional fixups.
  557. int nMaxDigits = GetFloatingPointMaxDigitsAndPrecision(fmt, ref precision, info, out bool isSignificantDigits);
  558. if ((value != 0.0f) && (!isSignificantDigits || !Grisu3.TryRunSingle(value, precision, ref number)))
  559. {
  560. Dragon4Single(value, precision, isSignificantDigits, ref number);
  561. }
  562. number.CheckConsistency();
  563. // When the number is known to be roundtrippable (either because we requested it be, or
  564. // because we know we have enough digits to satisfy roundtrippability), we should validate
  565. // that the number actually roundtrips back to the original result.
  566. Debug.Assert(((precision != -1) && (precision < SinglePrecision)) || (BitConverter.SingleToInt32Bits(value) == BitConverter.SingleToInt32Bits(NumberToSingle(ref number))));
  567. if (fmt != 0)
  568. {
  569. if (precision == -1)
  570. {
  571. Debug.Assert((fmt == 'G') || (fmt == 'g') || (fmt == 'R') || (fmt == 'r'));
  572. // For the roundtrip and general format specifiers, when returning the shortest roundtrippable
  573. // string, we need to update the maximum number of digits to be the greater of number.DigitsCount
  574. // or SinglePrecision. This ensures that we continue returning "pretty" strings for values with
  575. // less digits. One example this fixes is "-60", which would otherwise be formatted as "-6E+01"
  576. // since DigitsCount would be 1 and the formatter would almost immediately switch to scientific notation.
  577. nMaxDigits = Math.Max(number.DigitsCount, SinglePrecision);
  578. }
  579. NumberToString(ref sb, ref number, fmt, nMaxDigits, info);
  580. }
  581. else
  582. {
  583. Debug.Assert(precision == SinglePrecisionCustomFormat);
  584. NumberToStringFormat(ref sb, ref number, format, info);
  585. }
  586. return null;
  587. }
  588. private static bool TryCopyTo(string source, Span<char> destination, out int charsWritten)
  589. {
  590. Debug.Assert(source != null);
  591. if (source.AsSpan().TryCopyTo(destination))
  592. {
  593. charsWritten = source.Length;
  594. return true;
  595. }
  596. charsWritten = 0;
  597. return false;
  598. }
  599. public static unsafe string FormatInt32(int value, ReadOnlySpan<char> format, IFormatProvider? provider)
  600. {
  601. // Fast path for default format with a non-negative value
  602. if (value >= 0 && format.Length == 0)
  603. {
  604. return UInt32ToDecStr((uint)value, digits: -1);
  605. }
  606. char fmt = ParseFormatSpecifier(format, out int digits);
  607. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  608. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  609. {
  610. return value >= 0 ?
  611. UInt32ToDecStr((uint)value, digits) :
  612. NegativeInt32ToDecStr(value, digits, NumberFormatInfo.GetInstance(provider).NegativeSign);
  613. }
  614. else if (fmtUpper == 'X')
  615. {
  616. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  617. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code produces lowercase.
  618. return Int32ToHexStr(value, (char)(fmt - ('X' - 'A' + 10)), digits);
  619. }
  620. else
  621. {
  622. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  623. byte* pDigits = stackalloc byte[Int32NumberBufferLength];
  624. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, Int32NumberBufferLength);
  625. Int32ToNumber(value, ref number);
  626. char* stackPtr = stackalloc char[CharStackBufferSize];
  627. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  628. if (fmt != 0)
  629. {
  630. NumberToString(ref sb, ref number, fmt, digits, info);
  631. }
  632. else
  633. {
  634. NumberToStringFormat(ref sb, ref number, format, info);
  635. }
  636. return sb.ToString();
  637. }
  638. }
  639. public static unsafe bool TryFormatInt32(int value, ReadOnlySpan<char> format, IFormatProvider? provider, Span<char> destination, out int charsWritten)
  640. {
  641. // Fast path for default format with a non-negative value
  642. if (value >= 0 && format.Length == 0)
  643. {
  644. return TryUInt32ToDecStr((uint)value, digits: -1, destination, out charsWritten);
  645. }
  646. char fmt = ParseFormatSpecifier(format, out int digits);
  647. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  648. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  649. {
  650. return value >= 0 ?
  651. TryUInt32ToDecStr((uint)value, digits, destination, out charsWritten) :
  652. TryNegativeInt32ToDecStr(value, digits, NumberFormatInfo.GetInstance(provider).NegativeSign, destination, out charsWritten);
  653. }
  654. else if (fmtUpper == 'X')
  655. {
  656. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  657. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code produces lowercase.
  658. return TryInt32ToHexStr(value, (char)(fmt - ('X' - 'A' + 10)), digits, destination, out charsWritten);
  659. }
  660. else
  661. {
  662. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  663. byte* pDigits = stackalloc byte[Int32NumberBufferLength];
  664. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, Int32NumberBufferLength);
  665. Int32ToNumber(value, ref number);
  666. char* stackPtr = stackalloc char[CharStackBufferSize];
  667. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  668. if (fmt != 0)
  669. {
  670. NumberToString(ref sb, ref number, fmt, digits, info);
  671. }
  672. else
  673. {
  674. NumberToStringFormat(ref sb, ref number, format, info);
  675. }
  676. return sb.TryCopyTo(destination, out charsWritten);
  677. }
  678. }
  679. public static unsafe string FormatUInt32(uint value, ReadOnlySpan<char> format, IFormatProvider? provider)
  680. {
  681. // Fast path for default format
  682. if (format.Length == 0)
  683. {
  684. return UInt32ToDecStr(value, digits: -1);
  685. }
  686. char fmt = ParseFormatSpecifier(format, out int digits);
  687. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  688. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  689. {
  690. return UInt32ToDecStr(value, digits);
  691. }
  692. else if (fmtUpper == 'X')
  693. {
  694. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  695. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code produces lowercase.
  696. return Int32ToHexStr((int)value, (char)(fmt - ('X' - 'A' + 10)), digits);
  697. }
  698. else
  699. {
  700. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  701. byte* pDigits = stackalloc byte[UInt32NumberBufferLength];
  702. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, UInt32NumberBufferLength);
  703. UInt32ToNumber(value, ref number);
  704. char* stackPtr = stackalloc char[CharStackBufferSize];
  705. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  706. if (fmt != 0)
  707. {
  708. NumberToString(ref sb, ref number, fmt, digits, info);
  709. }
  710. else
  711. {
  712. NumberToStringFormat(ref sb, ref number, format, info);
  713. }
  714. return sb.ToString();
  715. }
  716. }
  717. public static unsafe bool TryFormatUInt32(uint value, ReadOnlySpan<char> format, IFormatProvider? provider, Span<char> destination, out int charsWritten)
  718. {
  719. // Fast path for default format
  720. if (format.Length == 0)
  721. {
  722. return TryUInt32ToDecStr(value, digits: -1, destination, out charsWritten);
  723. }
  724. char fmt = ParseFormatSpecifier(format, out int digits);
  725. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  726. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  727. {
  728. return TryUInt32ToDecStr(value, digits, destination, out charsWritten);
  729. }
  730. else if (fmtUpper == 'X')
  731. {
  732. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  733. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code produces lowercase.
  734. return TryInt32ToHexStr((int)value, (char)(fmt - ('X' - 'A' + 10)), digits, destination, out charsWritten);
  735. }
  736. else
  737. {
  738. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  739. byte* pDigits = stackalloc byte[UInt32NumberBufferLength];
  740. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, UInt32NumberBufferLength);
  741. UInt32ToNumber(value, ref number);
  742. char* stackPtr = stackalloc char[CharStackBufferSize];
  743. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  744. if (fmt != 0)
  745. {
  746. NumberToString(ref sb, ref number, fmt, digits, info);
  747. }
  748. else
  749. {
  750. NumberToStringFormat(ref sb, ref number, format, info);
  751. }
  752. return sb.TryCopyTo(destination, out charsWritten);
  753. }
  754. }
  755. public static unsafe string FormatInt64(long value, ReadOnlySpan<char> format, IFormatProvider? provider)
  756. {
  757. // Fast path for default format with a non-negative value
  758. if (value >= 0 && format.Length == 0)
  759. {
  760. return UInt64ToDecStr((ulong)value, digits: -1);
  761. }
  762. char fmt = ParseFormatSpecifier(format, out int digits);
  763. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  764. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  765. {
  766. return value >= 0 ?
  767. UInt64ToDecStr((ulong)value, digits) :
  768. NegativeInt64ToDecStr(value, digits, NumberFormatInfo.GetInstance(provider).NegativeSign);
  769. }
  770. else if (fmtUpper == 'X')
  771. {
  772. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  773. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code
  774. // produces lowercase.
  775. return Int64ToHexStr(value, (char)(fmt - ('X' - 'A' + 10)), digits);
  776. }
  777. else
  778. {
  779. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  780. byte* pDigits = stackalloc byte[Int64NumberBufferLength];
  781. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, Int64NumberBufferLength);
  782. Int64ToNumber(value, ref number);
  783. char* stackPtr = stackalloc char[CharStackBufferSize];
  784. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  785. if (fmt != 0)
  786. {
  787. NumberToString(ref sb, ref number, fmt, digits, info);
  788. }
  789. else
  790. {
  791. NumberToStringFormat(ref sb, ref number, format, info);
  792. }
  793. return sb.ToString();
  794. }
  795. }
  796. public static unsafe bool TryFormatInt64(long value, ReadOnlySpan<char> format, IFormatProvider? provider, Span<char> destination, out int charsWritten)
  797. {
  798. // Fast path for default format with a non-negative value
  799. if (value >= 0 && format.Length == 0)
  800. {
  801. return TryUInt64ToDecStr((ulong)value, digits: -1, destination, out charsWritten);
  802. }
  803. char fmt = ParseFormatSpecifier(format, out int digits);
  804. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  805. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  806. {
  807. return value >= 0 ?
  808. TryUInt64ToDecStr((ulong)value, digits, destination, out charsWritten) :
  809. TryNegativeInt64ToDecStr(value, digits, NumberFormatInfo.GetInstance(provider).NegativeSign, destination, out charsWritten);
  810. }
  811. else if (fmtUpper == 'X')
  812. {
  813. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  814. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code
  815. // produces lowercase.
  816. return TryInt64ToHexStr(value, (char)(fmt - ('X' - 'A' + 10)), digits, destination, out charsWritten);
  817. }
  818. else
  819. {
  820. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  821. byte* pDigits = stackalloc byte[Int64NumberBufferLength];
  822. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, Int64NumberBufferLength);
  823. Int64ToNumber(value, ref number);
  824. char* stackPtr = stackalloc char[CharStackBufferSize];
  825. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  826. if (fmt != 0)
  827. {
  828. NumberToString(ref sb, ref number, fmt, digits, info);
  829. }
  830. else
  831. {
  832. NumberToStringFormat(ref sb, ref number, format, info);
  833. }
  834. return sb.TryCopyTo(destination, out charsWritten);
  835. }
  836. }
  837. public static unsafe string FormatUInt64(ulong value, ReadOnlySpan<char> format, IFormatProvider? provider)
  838. {
  839. // Fast path for default format
  840. if (format.Length == 0)
  841. {
  842. return UInt64ToDecStr(value, digits: -1);
  843. }
  844. char fmt = ParseFormatSpecifier(format, out int digits);
  845. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  846. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  847. {
  848. return UInt64ToDecStr(value, digits);
  849. }
  850. else if (fmtUpper == 'X')
  851. {
  852. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  853. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code
  854. // produces lowercase.
  855. return Int64ToHexStr((long)value, (char)(fmt - ('X' - 'A' + 10)), digits);
  856. }
  857. else
  858. {
  859. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  860. byte* pDigits = stackalloc byte[UInt64NumberBufferLength];
  861. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, UInt64NumberBufferLength);
  862. UInt64ToNumber(value, ref number);
  863. char* stackPtr = stackalloc char[CharStackBufferSize];
  864. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  865. if (fmt != 0)
  866. {
  867. NumberToString(ref sb, ref number, fmt, digits, info);
  868. }
  869. else
  870. {
  871. NumberToStringFormat(ref sb, ref number, format, info);
  872. }
  873. return sb.ToString();
  874. }
  875. }
  876. public static unsafe bool TryFormatUInt64(ulong value, ReadOnlySpan<char> format, IFormatProvider? provider, Span<char> destination, out int charsWritten)
  877. {
  878. // Fast path for default format
  879. if (format.Length == 0)
  880. {
  881. return TryUInt64ToDecStr(value, digits: -1, destination, out charsWritten);
  882. }
  883. char fmt = ParseFormatSpecifier(format, out int digits);
  884. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  885. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  886. {
  887. return TryUInt64ToDecStr(value, digits, destination, out charsWritten);
  888. }
  889. else if (fmtUpper == 'X')
  890. {
  891. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  892. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code
  893. // produces lowercase.
  894. return TryInt64ToHexStr((long)value, (char)(fmt - ('X' - 'A' + 10)), digits, destination, out charsWritten);
  895. }
  896. else
  897. {
  898. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  899. byte* pDigits = stackalloc byte[UInt64NumberBufferLength];
  900. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, UInt64NumberBufferLength);
  901. UInt64ToNumber(value, ref number);
  902. char* stackPtr = stackalloc char[CharStackBufferSize];
  903. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  904. if (fmt != 0)
  905. {
  906. NumberToString(ref sb, ref number, fmt, digits, info);
  907. }
  908. else
  909. {
  910. NumberToStringFormat(ref sb, ref number, format, info);
  911. }
  912. return sb.TryCopyTo(destination, out charsWritten);
  913. }
  914. }
  915. [MethodImpl(MethodImplOptions.AggressiveInlining)] // called from only one location
  916. private static unsafe void Int32ToNumber(int value, ref NumberBuffer number)
  917. {
  918. number.DigitsCount = Int32Precision;
  919. if (value >= 0)
  920. {
  921. number.IsNegative = false;
  922. }
  923. else
  924. {
  925. number.IsNegative = true;
  926. value = -value;
  927. }
  928. byte* buffer = number.GetDigitsPointer();
  929. byte* p = UInt32ToDecChars(buffer + Int32Precision, (uint)value, 0);
  930. int i = (int)(buffer + Int32Precision - p);
  931. number.DigitsCount = i;
  932. number.Scale = i;
  933. byte* dst = number.GetDigitsPointer();
  934. while (--i >= 0)
  935. *dst++ = *p++;
  936. *dst = (byte)('\0');
  937. number.CheckConsistency();
  938. }
  939. private static unsafe string NegativeInt32ToDecStr(int value, int digits, string sNegative)
  940. {
  941. Debug.Assert(value < 0);
  942. if (digits < 1)
  943. digits = 1;
  944. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits((uint)(-value))) + sNegative.Length;
  945. string result = string.FastAllocateString(bufferLength);
  946. fixed (char* buffer = result)
  947. {
  948. char* p = UInt32ToDecChars(buffer + bufferLength, (uint)(-value), digits);
  949. Debug.Assert(p == buffer + sNegative.Length);
  950. for (int i = sNegative.Length - 1; i >= 0; i--)
  951. {
  952. *(--p) = sNegative[i];
  953. }
  954. Debug.Assert(p == buffer);
  955. }
  956. return result;
  957. }
  958. private static unsafe bool TryNegativeInt32ToDecStr(int value, int digits, string sNegative, Span<char> destination, out int charsWritten)
  959. {
  960. Debug.Assert(value < 0);
  961. if (digits < 1)
  962. digits = 1;
  963. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits((uint)(-value))) + sNegative.Length;
  964. if (bufferLength > destination.Length)
  965. {
  966. charsWritten = 0;
  967. return false;
  968. }
  969. charsWritten = bufferLength;
  970. fixed (char* buffer = &MemoryMarshal.GetReference(destination))
  971. {
  972. char* p = UInt32ToDecChars(buffer + bufferLength, (uint)(-value), digits);
  973. Debug.Assert(p == buffer + sNegative.Length);
  974. for (int i = sNegative.Length - 1; i >= 0; i--)
  975. {
  976. *(--p) = sNegative[i];
  977. }
  978. Debug.Assert(p == buffer);
  979. }
  980. return true;
  981. }
  982. private static unsafe string Int32ToHexStr(int value, char hexBase, int digits)
  983. {
  984. if (digits < 1)
  985. digits = 1;
  986. int bufferLength = Math.Max(digits, FormattingHelpers.CountHexDigits((uint)value));
  987. string result = string.FastAllocateString(bufferLength);
  988. fixed (char* buffer = result)
  989. {
  990. char* p = Int32ToHexChars(buffer + bufferLength, (uint)value, hexBase, digits);
  991. Debug.Assert(p == buffer);
  992. }
  993. return result;
  994. }
  995. private static unsafe bool TryInt32ToHexStr(int value, char hexBase, int digits, Span<char> destination, out int charsWritten)
  996. {
  997. if (digits < 1)
  998. digits = 1;
  999. int bufferLength = Math.Max(digits, FormattingHelpers.CountHexDigits((uint)value));
  1000. if (bufferLength > destination.Length)
  1001. {
  1002. charsWritten = 0;
  1003. return false;
  1004. }
  1005. charsWritten = bufferLength;
  1006. fixed (char* buffer = &MemoryMarshal.GetReference(destination))
  1007. {
  1008. char* p = Int32ToHexChars(buffer + bufferLength, (uint)value, hexBase, digits);
  1009. Debug.Assert(p == buffer);
  1010. }
  1011. return true;
  1012. }
  1013. private static unsafe char* Int32ToHexChars(char* buffer, uint value, int hexBase, int digits)
  1014. {
  1015. while (--digits >= 0 || value != 0)
  1016. {
  1017. byte digit = (byte)(value & 0xF);
  1018. *(--buffer) = (char)(digit + (digit < 10 ? (byte)'0' : hexBase));
  1019. value >>= 4;
  1020. }
  1021. return buffer;
  1022. }
  1023. [MethodImpl(MethodImplOptions.AggressiveInlining)] // called from only one location
  1024. private static unsafe void UInt32ToNumber(uint value, ref NumberBuffer number)
  1025. {
  1026. number.DigitsCount = UInt32Precision;
  1027. number.IsNegative = false;
  1028. byte* buffer = number.GetDigitsPointer();
  1029. byte* p = UInt32ToDecChars(buffer + UInt32Precision, value, 0);
  1030. int i = (int)(buffer + UInt32Precision - p);
  1031. number.DigitsCount = i;
  1032. number.Scale = i;
  1033. byte* dst = number.GetDigitsPointer();
  1034. while (--i >= 0)
  1035. *dst++ = *p++;
  1036. *dst = (byte)('\0');
  1037. number.CheckConsistency();
  1038. }
  1039. internal static unsafe byte* UInt32ToDecChars(byte* bufferEnd, uint value, int digits)
  1040. {
  1041. while (--digits >= 0 || value != 0)
  1042. {
  1043. value = Math.DivRem(value, 10, out uint remainder);
  1044. *(--bufferEnd) = (byte)(remainder + '0');
  1045. }
  1046. return bufferEnd;
  1047. }
  1048. internal static unsafe char* UInt32ToDecChars(char* bufferEnd, uint value, int digits)
  1049. {
  1050. while (--digits >= 0 || value != 0)
  1051. {
  1052. value = Math.DivRem(value, 10, out uint remainder);
  1053. *(--bufferEnd) = (char)(remainder + '0');
  1054. }
  1055. return bufferEnd;
  1056. }
  1057. internal static unsafe string UInt32ToDecStr(uint value, int digits)
  1058. {
  1059. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
  1060. // For single-digit values that are very common, especially 0 and 1, just return cached strings.
  1061. if (bufferLength == 1)
  1062. {
  1063. return s_singleDigitStringCache[value];
  1064. }
  1065. string result = string.FastAllocateString(bufferLength);
  1066. fixed (char* buffer = result)
  1067. {
  1068. char* p = buffer + bufferLength;
  1069. if (digits <= 1)
  1070. {
  1071. do
  1072. {
  1073. value = Math.DivRem(value, 10, out uint remainder);
  1074. *(--p) = (char)(remainder + '0');
  1075. }
  1076. while (value != 0);
  1077. }
  1078. else
  1079. {
  1080. p = UInt32ToDecChars(p, value, digits);
  1081. }
  1082. Debug.Assert(p == buffer);
  1083. }
  1084. return result;
  1085. }
  1086. private static unsafe bool TryUInt32ToDecStr(uint value, int digits, Span<char> destination, out int charsWritten)
  1087. {
  1088. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
  1089. if (bufferLength > destination.Length)
  1090. {
  1091. charsWritten = 0;
  1092. return false;
  1093. }
  1094. charsWritten = bufferLength;
  1095. fixed (char* buffer = &MemoryMarshal.GetReference(destination))
  1096. {
  1097. char* p = buffer + bufferLength;
  1098. if (digits <= 1)
  1099. {
  1100. do
  1101. {
  1102. value = Math.DivRem(value, 10, out uint remainder);
  1103. *(--p) = (char)(remainder + '0');
  1104. }
  1105. while (value != 0);
  1106. }
  1107. else
  1108. {
  1109. p = UInt32ToDecChars(p, value, digits);
  1110. }
  1111. Debug.Assert(p == buffer);
  1112. }
  1113. return true;
  1114. }
  1115. private static unsafe void Int64ToNumber(long input, ref NumberBuffer number)
  1116. {
  1117. ulong value = (ulong)input;
  1118. number.IsNegative = input < 0;
  1119. number.DigitsCount = Int64Precision;
  1120. if (number.IsNegative)
  1121. {
  1122. value = (ulong)(-input);
  1123. }
  1124. byte* buffer = number.GetDigitsPointer();
  1125. byte* p = buffer + Int64Precision;
  1126. while (High32(value) != 0)
  1127. p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
  1128. p = UInt32ToDecChars(p, Low32(value), 0);
  1129. int i = (int)(buffer + Int64Precision - p);
  1130. number.DigitsCount = i;
  1131. number.Scale = i;
  1132. byte* dst = number.GetDigitsPointer();
  1133. while (--i >= 0)
  1134. *dst++ = *p++;
  1135. *dst = (byte)('\0');
  1136. number.CheckConsistency();
  1137. }
  1138. private static unsafe string NegativeInt64ToDecStr(long input, int digits, string sNegative)
  1139. {
  1140. Debug.Assert(input < 0);
  1141. if (digits < 1)
  1142. {
  1143. digits = 1;
  1144. }
  1145. ulong value = (ulong)(-input);
  1146. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value)) + sNegative.Length;
  1147. string result = string.FastAllocateString(bufferLength);
  1148. fixed (char* buffer = result)
  1149. {
  1150. char* p = buffer + bufferLength;
  1151. while (High32(value) != 0)
  1152. {
  1153. p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
  1154. digits -= 9;
  1155. }
  1156. p = UInt32ToDecChars(p, Low32(value), digits);
  1157. Debug.Assert(p == buffer + sNegative.Length);
  1158. for (int i = sNegative.Length - 1; i >= 0; i--)
  1159. {
  1160. *(--p) = sNegative[i];
  1161. }
  1162. Debug.Assert(p == buffer);
  1163. }
  1164. return result;
  1165. }
  1166. private static unsafe bool TryNegativeInt64ToDecStr(long input, int digits, string sNegative, Span<char> destination, out int charsWritten)
  1167. {
  1168. Debug.Assert(input < 0);
  1169. if (digits < 1)
  1170. {
  1171. digits = 1;
  1172. }
  1173. ulong value = (ulong)(-input);
  1174. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits((ulong)(-input))) + sNegative.Length;
  1175. if (bufferLength > destination.Length)
  1176. {
  1177. charsWritten = 0;
  1178. return false;
  1179. }
  1180. charsWritten = bufferLength;
  1181. fixed (char* buffer = &MemoryMarshal.GetReference(destination))
  1182. {
  1183. char* p = buffer + bufferLength;
  1184. while (High32(value) != 0)
  1185. {
  1186. p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
  1187. digits -= 9;
  1188. }
  1189. p = UInt32ToDecChars(p, Low32(value), digits);
  1190. Debug.Assert(p == buffer + sNegative.Length);
  1191. for (int i = sNegative.Length - 1; i >= 0; i--)
  1192. {
  1193. *(--p) = sNegative[i];
  1194. }
  1195. Debug.Assert(p == buffer);
  1196. }
  1197. return true;
  1198. }
  1199. private static unsafe string Int64ToHexStr(long value, char hexBase, int digits)
  1200. {
  1201. int bufferLength = Math.Max(digits, FormattingHelpers.CountHexDigits((ulong)value));
  1202. string result = string.FastAllocateString(bufferLength);
  1203. fixed (char* buffer = result)
  1204. {
  1205. char* p = buffer + bufferLength;
  1206. if (High32((ulong)value) != 0)
  1207. {
  1208. p = Int32ToHexChars(p, Low32((ulong)value), hexBase, 8);
  1209. p = Int32ToHexChars(p, High32((ulong)value), hexBase, digits - 8);
  1210. }
  1211. else
  1212. {
  1213. p = Int32ToHexChars(p, Low32((ulong)value), hexBase, Math.Max(digits, 1));
  1214. }
  1215. Debug.Assert(p == buffer);
  1216. }
  1217. return result;
  1218. }
  1219. private static unsafe bool TryInt64ToHexStr(long value, char hexBase, int digits, Span<char> destination, out int charsWritten)
  1220. {
  1221. int bufferLength = Math.Max(digits, FormattingHelpers.CountHexDigits((ulong)value));
  1222. if (bufferLength > destination.Length)
  1223. {
  1224. charsWritten = 0;
  1225. return false;
  1226. }
  1227. charsWritten = bufferLength;
  1228. fixed (char* buffer = &MemoryMarshal.GetReference(destination))
  1229. {
  1230. char* p = buffer + bufferLength;
  1231. if (High32((ulong)value) != 0)
  1232. {
  1233. p = Int32ToHexChars(p, Low32((ulong)value), hexBase, 8);
  1234. p = Int32ToHexChars(p, High32((ulong)value), hexBase, digits - 8);
  1235. }
  1236. else
  1237. {
  1238. p = Int32ToHexChars(p, Low32((ulong)value), hexBase, Math.Max(digits, 1));
  1239. }
  1240. Debug.Assert(p == buffer);
  1241. }
  1242. return true;
  1243. }
  1244. private static unsafe void UInt64ToNumber(ulong value, ref NumberBuffer number)
  1245. {
  1246. number.DigitsCount = UInt64Precision;
  1247. number.IsNegative = false;
  1248. byte* buffer = number.GetDigitsPointer();
  1249. byte* p = buffer + UInt64Precision;
  1250. while (High32(value) != 0)
  1251. p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
  1252. p = UInt32ToDecChars(p, Low32(value), 0);
  1253. int i = (int)(buffer + UInt64Precision - p);
  1254. number.DigitsCount = i;
  1255. number.Scale = i;
  1256. byte* dst = number.GetDigitsPointer();
  1257. while (--i >= 0)
  1258. *dst++ = *p++;
  1259. *dst = (byte)('\0');
  1260. number.CheckConsistency();
  1261. }
  1262. internal static unsafe string UInt64ToDecStr(ulong value, int digits)
  1263. {
  1264. if (digits < 1)
  1265. digits = 1;
  1266. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
  1267. // For single-digit values that are very common, especially 0 and 1, just return cached strings.
  1268. if (bufferLength == 1)
  1269. {
  1270. return s_singleDigitStringCache[value];
  1271. }
  1272. string result = string.FastAllocateString(bufferLength);
  1273. fixed (char* buffer = result)
  1274. {
  1275. char* p = buffer + bufferLength;
  1276. while (High32(value) != 0)
  1277. {
  1278. p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
  1279. digits -= 9;
  1280. }
  1281. p = UInt32ToDecChars(p, Low32(value), digits);
  1282. Debug.Assert(p == buffer);
  1283. }
  1284. return result;
  1285. }
  1286. private static unsafe bool TryUInt64ToDecStr(ulong value, int digits, Span<char> destination, out int charsWritten)
  1287. {
  1288. if (digits < 1)
  1289. digits = 1;
  1290. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
  1291. if (bufferLength > destination.Length)
  1292. {
  1293. charsWritten = 0;
  1294. return false;
  1295. }
  1296. charsWritten = bufferLength;
  1297. fixed (char* buffer = &MemoryMarshal.GetReference(destination))
  1298. {
  1299. char* p = buffer + bufferLength;
  1300. while (High32(value) != 0)
  1301. {
  1302. p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
  1303. digits -= 9;
  1304. }
  1305. p = UInt32ToDecChars(p, Low32(value), digits);
  1306. Debug.Assert(p == buffer);
  1307. }
  1308. return true;
  1309. }
  1310. internal static unsafe char ParseFormatSpecifier(ReadOnlySpan<char> format, out int digits)
  1311. {
  1312. char c = default;
  1313. if (format.Length > 0)
  1314. {
  1315. // If the format begins with a symbol, see if it's a standard format
  1316. // with or without a specified number of digits.
  1317. c = format[0];
  1318. if ((uint)(c - 'A') <= 'Z' - 'A' ||
  1319. (uint)(c - 'a') <= 'z' - 'a')
  1320. {
  1321. // Fast path for sole symbol, e.g. "D"
  1322. if (format.Length == 1)
  1323. {
  1324. digits = -1;
  1325. return c;
  1326. }
  1327. if (format.Length == 2)
  1328. {
  1329. // Fast path for symbol and single digit, e.g. "X4"
  1330. int d = format[1] - '0';
  1331. if ((uint)d < 10)
  1332. {
  1333. digits = d;
  1334. return c;
  1335. }
  1336. }
  1337. else if (format.Length == 3)
  1338. {
  1339. // Fast path for symbol and double digit, e.g. "F12"
  1340. int d1 = format[1] - '0', d2 = format[2] - '0';
  1341. if ((uint)d1 < 10 && (uint)d2 < 10)
  1342. {
  1343. digits = d1 * 10 + d2;
  1344. return c;
  1345. }
  1346. }
  1347. // Fallback for symbol and any length digits. The digits value must be >= 0 && <= 99,
  1348. // but it can begin with any number of 0s, and thus we may need to check more than two
  1349. // digits. Further, for compat, we need to stop when we hit a null char.
  1350. int n = 0;
  1351. int i = 1;
  1352. while (i < format.Length && (((uint)format[i] - '0') < 10) && n < 10)
  1353. {
  1354. n = (n * 10) + format[i++] - '0';
  1355. }
  1356. // If we're at the end of the digits rather than having stopped because we hit something
  1357. // other than a digit or overflowed, return the standard format info.
  1358. if (i == format.Length || format[i] == '\0')
  1359. {
  1360. digits = n;
  1361. return c;
  1362. }
  1363. }
  1364. }
  1365. // Default empty format to be "G"; custom format is signified with '\0'.
  1366. digits = -1;
  1367. return format.Length == 0 || c == '\0' ? // For compat, treat '\0' as the end of the specifier, even if the specifier extends beyond it.
  1368. 'G' :
  1369. '\0';
  1370. }
  1371. internal static unsafe void NumberToString(ref ValueStringBuilder sb, ref NumberBuffer number, char format, int nMaxDigits, NumberFormatInfo info)
  1372. {
  1373. number.CheckConsistency();
  1374. bool isCorrectlyRounded = (number.Kind == NumberBufferKind.FloatingPoint);
  1375. switch (format)
  1376. {
  1377. case 'C':
  1378. case 'c':
  1379. {
  1380. if (nMaxDigits < 0)
  1381. nMaxDigits = info.CurrencyDecimalDigits;
  1382. RoundNumber(ref number, number.Scale + nMaxDigits, isCorrectlyRounded); // Don't change this line to use digPos since digCount could have its sign changed.
  1383. FormatCurrency(ref sb, ref number, nMaxDigits, info);
  1384. break;
  1385. }
  1386. case 'F':
  1387. case 'f':
  1388. {
  1389. if (nMaxDigits < 0)
  1390. nMaxDigits = info.NumberDecimalDigits;
  1391. RoundNumber(ref number, number.Scale + nMaxDigits, isCorrectlyRounded);
  1392. if (number.IsNegative)
  1393. sb.Append(info.NegativeSign);
  1394. FormatFixed(ref sb, ref number, nMaxDigits, null, info.NumberDecimalSeparator, null);
  1395. break;
  1396. }
  1397. case 'N':
  1398. case 'n':
  1399. {
  1400. if (nMaxDigits < 0)
  1401. nMaxDigits = info.NumberDecimalDigits; // Since we are using digits in our calculation
  1402. RoundNumber(ref number, number.Scale + nMaxDigits, isCorrectlyRounded);
  1403. FormatNumber(ref sb, ref number, nMaxDigits, info);
  1404. break;
  1405. }
  1406. case 'E':
  1407. case 'e':
  1408. {
  1409. if (nMaxDigits < 0)
  1410. nMaxDigits = DefaultPrecisionExponentialFormat;
  1411. nMaxDigits++;
  1412. RoundNumber(ref number, nMaxDigits, isCorrectlyRounded);
  1413. if (number.IsNegative)
  1414. sb.Append(info.NegativeSign);
  1415. FormatScientific(ref sb, ref number, nMaxDigits, info, format);
  1416. break;
  1417. }
  1418. case 'G':
  1419. case 'g':
  1420. {
  1421. bool noRounding = false;
  1422. if (nMaxDigits < 1)
  1423. {
  1424. if ((number.Kind == NumberBufferKind.Decimal) && (nMaxDigits == -1))
  1425. {
  1426. noRounding = true; // Turn off rounding for ECMA compliance to output trailing 0's after decimal as significant
  1427. if (number.Digits[0] == 0)
  1428. {
  1429. // -0 should be formatted as 0 for decimal. This is normally handled by RoundNumber (which we are skipping)
  1430. goto SkipSign;
  1431. }
  1432. goto SkipRounding;
  1433. }
  1434. else
  1435. {
  1436. // This ensures that the PAL code pads out to the correct place even when we use the default precision
  1437. nMaxDigits = number.DigitsCount;
  1438. }
  1439. }
  1440. RoundNumber(ref number, nMaxDigits, isCorrectlyRounded);
  1441. SkipRounding:
  1442. if (number.IsNegative)
  1443. sb.Append(info.NegativeSign);
  1444. SkipSign:
  1445. FormatGeneral(ref sb, ref number, nMaxDigits, info, (char)(format - ('G' - 'E')), noRounding);
  1446. break;
  1447. }
  1448. case 'P':
  1449. case 'p':
  1450. {
  1451. if (nMaxDigits < 0)
  1452. nMaxDigits = info.PercentDecimalDigits;
  1453. number.Scale += 2;
  1454. RoundNumber(ref number, number.Scale + nMaxDigits, isCorrectlyRounded);
  1455. FormatPercent(ref sb, ref number, nMaxDigits, info);
  1456. break;
  1457. }
  1458. case 'R':
  1459. case 'r':
  1460. {
  1461. if (number.Kind != NumberBufferKind.FloatingPoint)
  1462. {
  1463. goto default;
  1464. }
  1465. format = (char)(format - ('R' - 'G'));
  1466. Debug.Assert((format == 'G') || (format == 'g'));
  1467. goto case 'G';
  1468. }
  1469. default:
  1470. throw new FormatException(SR.Argument_BadFormatSpecifier);
  1471. }
  1472. }
  1473. internal static unsafe void NumberToStringFormat(ref ValueStringBuilder sb, ref NumberBuffer number, ReadOnlySpan<char> format, NumberFormatInfo info)
  1474. {
  1475. number.CheckConsistency();
  1476. int digitCount;
  1477. int decimalPos;
  1478. int firstDigit;
  1479. int lastDigit;
  1480. int digPos;
  1481. bool scientific;
  1482. int thousandPos;
  1483. int thousandCount = 0;
  1484. bool thousandSeps;
  1485. int scaleAdjust;
  1486. int adjust;
  1487. int section;
  1488. int src;
  1489. byte* dig = number.GetDigitsPointer();
  1490. char ch;
  1491. section = FindSection(format, dig[0] == 0 ? 2 : number.IsNegative ? 1 : 0);
  1492. while (true)
  1493. {
  1494. digitCount = 0;
  1495. decimalPos = -1;
  1496. firstDigit = 0x7FFFFFFF;
  1497. lastDigit = 0;
  1498. scientific = false;
  1499. thousandPos = -1;
  1500. thousandSeps = false;
  1501. scaleAdjust = 0;
  1502. src = section;
  1503. fixed (char* pFormat = &MemoryMarshal.GetReference(format))
  1504. {
  1505. while (src < format.Length && (ch = pFormat[src++]) != 0 && ch != ';')
  1506. {
  1507. switch (ch)
  1508. {
  1509. case '#':
  1510. digitCount++;
  1511. break;
  1512. case '0':
  1513. if (firstDigit == 0x7FFFFFFF)
  1514. firstDigit = digitCount;
  1515. digitCount++;
  1516. lastDigit = digitCount;
  1517. break;
  1518. case '.':
  1519. if (decimalPos < 0)
  1520. decimalPos = digitCount;
  1521. break;
  1522. case ',':
  1523. if (digitCount > 0 && decimalPos < 0)
  1524. {
  1525. if (thousandPos >= 0)
  1526. {
  1527. if (thousandPos == digitCount)
  1528. {
  1529. thousandCount++;
  1530. break;
  1531. }
  1532. thousandSeps = true;
  1533. }
  1534. thousandPos = digitCount;
  1535. thousandCount = 1;
  1536. }
  1537. break;
  1538. case '%':
  1539. scaleAdjust += 2;
  1540. break;
  1541. case '\x2030':
  1542. scaleAdjust += 3;
  1543. break;
  1544. case '\'':
  1545. case '"':
  1546. while (src < format.Length && pFormat[src] != 0 && pFormat[src++] != ch)
  1547. ;
  1548. break;
  1549. case '\\':
  1550. if (src < format.Length && pFormat[src] != 0)
  1551. src++;
  1552. break;
  1553. case 'E':
  1554. case 'e':
  1555. if ((src < format.Length && pFormat[src] == '0') ||
  1556. (src + 1 < format.Length && (pFormat[src] == '+' || pFormat[src] == '-') && pFormat[src + 1] == '0'))
  1557. {
  1558. while (++src < format.Length && pFormat[src] == '0')
  1559. ;
  1560. scientific = true;
  1561. }
  1562. break;
  1563. }
  1564. }
  1565. }
  1566. if (decimalPos < 0)
  1567. decimalPos = digitCount;
  1568. if (thousandPos >= 0)
  1569. {
  1570. if (thousandPos == decimalPos)
  1571. scaleAdjust -= thousandCount * 3;
  1572. else
  1573. thousandSeps = true;
  1574. }
  1575. if (dig[0] != 0)
  1576. {
  1577. number.Scale += scaleAdjust;
  1578. int pos = scientific ? digitCount : number.Scale + digitCount - decimalPos;
  1579. RoundNumber(ref number, pos, isCorrectlyRounded: false);
  1580. if (dig[0] == 0)
  1581. {
  1582. src = FindSection(format, 2);
  1583. if (src != section)
  1584. {
  1585. section = src;
  1586. continue;
  1587. }
  1588. }
  1589. }
  1590. else
  1591. {
  1592. if (number.Kind != NumberBufferKind.FloatingPoint)
  1593. {
  1594. // The integer types don't have a concept of -0 and decimal always format -0 as 0
  1595. number.IsNegative = false;
  1596. }
  1597. number.Scale = 0; // Decimals with scale ('0.00') should be rounded.
  1598. }
  1599. break;
  1600. }
  1601. firstDigit = firstDigit < decimalPos ? decimalPos - firstDigit : 0;
  1602. lastDigit = lastDigit > decimalPos ? decimalPos - lastDigit : 0;
  1603. if (scientific)
  1604. {
  1605. digPos = decimalPos;
  1606. adjust = 0;
  1607. }
  1608. else
  1609. {
  1610. digPos = number.Scale > decimalPos ? number.Scale : decimalPos;
  1611. adjust = number.Scale - decimalPos;
  1612. }
  1613. src = section;
  1614. // Adjust can be negative, so we make this an int instead of an unsigned int.
  1615. // Adjust represents the number of characters over the formatting e.g. format string is "0000" and you are trying to
  1616. // format 100000 (6 digits). Means adjust will be 2. On the other hand if you are trying to format 10 adjust will be
  1617. // -2 and we'll need to fixup these digits with 0 padding if we have 0 formatting as in this example.
  1618. Span<int> thousandsSepPos = stackalloc int[4];
  1619. int thousandsSepCtr = -1;
  1620. if (thousandSeps)
  1621. {
  1622. // We need to precompute this outside the number formatting loop
  1623. if (info.NumberGroupSeparator.Length > 0)
  1624. {
  1625. // We need this array to figure out where to insert the thousands separator. We would have to traverse the string
  1626. // backwards. PIC formatting always traverses forwards. These indices are precomputed to tell us where to insert
  1627. // the thousands separator so we can get away with traversing forwards. Note we only have to compute up to digPos.
  1628. // The max is not bound since you can have formatting strings of the form "000,000..", and this
  1629. // should handle that case too.
  1630. int[] groupDigits = info._numberGroupSizes;
  1631. int groupSizeIndex = 0; // Index into the groupDigits array.
  1632. int groupTotalSizeCount = 0;
  1633. int groupSizeLen = groupDigits.Length; // The length of groupDigits array.
  1634. if (groupSizeLen != 0)
  1635. groupTotalSizeCount = groupDigits[groupSizeIndex]; // The current running total of group size.
  1636. int groupSize = groupTotalSizeCount;
  1637. int totalDigits = digPos + ((adjust < 0) ? adjust : 0); // Actual number of digits in o/p
  1638. int numDigits = (firstDigit > totalDigits) ? firstDigit : totalDigits;
  1639. while (numDigits > groupTotalSizeCount)
  1640. {
  1641. if (groupSize == 0)
  1642. break;
  1643. ++thousandsSepCtr;
  1644. if (thousandsSepCtr >= thousandsSepPos.Length)
  1645. {
  1646. var newThousandsSepPos = new int[thousandsSepPos.Length * 2];
  1647. thousandsSepPos.CopyTo(newThousandsSepPos);
  1648. thousandsSepPos = newThousandsSepPos;
  1649. }
  1650. thousandsSepPos[thousandsSepCtr] = groupTotalSizeCount;
  1651. if (groupSizeIndex < groupSizeLen - 1)
  1652. {
  1653. groupSizeIndex++;
  1654. groupSize = groupDigits[groupSizeIndex];
  1655. }
  1656. groupTotalSizeCount += groupSize;
  1657. }
  1658. }
  1659. }
  1660. if (number.IsNegative && (section == 0) && (number.Scale != 0))
  1661. sb.Append(info.NegativeSign);
  1662. bool decimalWritten = false;
  1663. fixed (char* pFormat = &MemoryMarshal.GetReference(format))
  1664. {
  1665. byte* cur = dig;
  1666. while (src < format.Length && (ch = pFormat[src++]) != 0 && ch != ';')
  1667. {
  1668. if (adjust > 0)
  1669. {
  1670. switch (ch)
  1671. {
  1672. case '#':
  1673. case '0':
  1674. case '.':
  1675. while (adjust > 0)
  1676. {
  1677. // digPos will be one greater than thousandsSepPos[thousandsSepCtr] since we are at
  1678. // the character after which the groupSeparator needs to be appended.
  1679. sb.Append(*cur != 0 ? (char)(*cur++) : '0');
  1680. if (thousandSeps && digPos > 1 && thousandsSepCtr >= 0)
  1681. {
  1682. if (digPos == thousandsSepPos[thousandsSepCtr] + 1)
  1683. {
  1684. sb.Append(info.NumberGroupSeparator);
  1685. thousandsSepCtr--;
  1686. }
  1687. }
  1688. digPos--;
  1689. adjust--;
  1690. }
  1691. break;
  1692. }
  1693. }
  1694. switch (ch)
  1695. {
  1696. case '#':
  1697. case '0':
  1698. {
  1699. if (adjust < 0)
  1700. {
  1701. adjust++;
  1702. ch = digPos <= firstDigit ? '0' : '\0';
  1703. }
  1704. else
  1705. {
  1706. ch = *cur != 0 ? (char)(*cur++) : digPos > lastDigit ? '0' : '\0';
  1707. }
  1708. if (ch != 0)
  1709. {
  1710. sb.Append(ch);
  1711. if (thousandSeps && digPos > 1 && thousandsSepCtr >= 0)
  1712. {
  1713. if (digPos == thousandsSepPos[thousandsSepCtr] + 1)
  1714. {
  1715. sb.Append(info.NumberGroupSeparator);
  1716. thousandsSepCtr--;
  1717. }
  1718. }
  1719. }
  1720. digPos--;
  1721. break;
  1722. }
  1723. case '.':
  1724. {
  1725. if (digPos != 0 || decimalWritten)
  1726. {
  1727. // For compatibility, don't echo repeated decimals
  1728. break;
  1729. }
  1730. // If the format has trailing zeros or the format has a decimal and digits remain
  1731. if (lastDigit < 0 || (decimalPos < digitCount && *cur != 0))
  1732. {
  1733. sb.Append(info.NumberDecimalSeparator);
  1734. decimalWritten = true;
  1735. }
  1736. break;
  1737. }
  1738. case '\x2030':
  1739. sb.Append(info.PerMilleSymbol);
  1740. break;
  1741. case '%':
  1742. sb.Append(info.PercentSymbol);
  1743. break;
  1744. case ',':
  1745. break;
  1746. case '\'':
  1747. case '"':
  1748. while (src < format.Length && pFormat[src] != 0 && pFormat[src] != ch)
  1749. sb.Append(pFormat[src++]);
  1750. if (src < format.Length && pFormat[src] != 0)
  1751. src++;
  1752. break;
  1753. case '\\':
  1754. if (src < format.Length && pFormat[src] != 0)
  1755. sb.Append(pFormat[src++]);
  1756. break;
  1757. case 'E':
  1758. case 'e':
  1759. {
  1760. bool positiveSign = false;
  1761. int i = 0;
  1762. if (scientific)
  1763. {
  1764. if (src < format.Length && pFormat[src] == '0')
  1765. {
  1766. // Handles E0, which should format the same as E-0
  1767. i++;
  1768. }
  1769. else if (src + 1 < format.Length && pFormat[src] == '+' && pFormat[src + 1] == '0')
  1770. {
  1771. // Handles E+0
  1772. positiveSign = true;
  1773. }
  1774. else if (src + 1 < format.Length && pFormat[src] == '-' && pFormat[src + 1] == '0')
  1775. {
  1776. // Handles E-0
  1777. // Do nothing, this is just a place holder s.t. we don't break out of the loop.
  1778. }
  1779. else
  1780. {
  1781. sb.Append(ch);
  1782. break;
  1783. }
  1784. while (++src < format.Length && pFormat[src] == '0')
  1785. i++;
  1786. if (i > 10)
  1787. i = 10;
  1788. int exp = dig[0] == 0 ? 0 : number.Scale - decimalPos;
  1789. FormatExponent(ref sb, info, exp, ch, i, positiveSign);
  1790. scientific = false;
  1791. }
  1792. else
  1793. {
  1794. sb.Append(ch); // Copy E or e to output
  1795. if (src < format.Length)
  1796. {
  1797. if (pFormat[src] == '+' || pFormat[src] == '-')
  1798. sb.Append(pFormat[src++]);
  1799. while (src < format.Length && pFormat[src] == '0')
  1800. sb.Append(pFormat[src++]);
  1801. }
  1802. }
  1803. break;
  1804. }
  1805. default:
  1806. sb.Append(ch);
  1807. break;
  1808. }
  1809. }
  1810. }
  1811. if (number.IsNegative && (section == 0) && (number.Scale == 0) && (sb.Length > 0))
  1812. sb.Insert(0, info.NegativeSign);
  1813. }
  1814. private static void FormatCurrency(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info)
  1815. {
  1816. string fmt = number.IsNegative ?
  1817. s_negCurrencyFormats[info.CurrencyNegativePattern] :
  1818. s_posCurrencyFormats[info.CurrencyPositivePattern];
  1819. foreach (char ch in fmt)
  1820. {
  1821. switch (ch)
  1822. {
  1823. case '#':
  1824. FormatFixed(ref sb, ref number, nMaxDigits, info._currencyGroupSizes, info.CurrencyDecimalSeparator, info.CurrencyGroupSeparator);
  1825. break;
  1826. case '-':
  1827. sb.Append(info.NegativeSign);
  1828. break;
  1829. case '$':
  1830. sb.Append(info.CurrencySymbol);
  1831. break;
  1832. default:
  1833. sb.Append(ch);
  1834. break;
  1835. }
  1836. }
  1837. }
  1838. private static unsafe void FormatFixed(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, int[]? groupDigits, string? sDecimal, string? sGroup)
  1839. {
  1840. int digPos = number.Scale;
  1841. byte* dig = number.GetDigitsPointer();
  1842. if (digPos > 0)
  1843. {
  1844. if (groupDigits != null)
  1845. {
  1846. Debug.Assert(sGroup != null, "Must be nulll when groupDigits != null");
  1847. int groupSizeIndex = 0; // Index into the groupDigits array.
  1848. int bufferSize = digPos; // The length of the result buffer string.
  1849. int groupSize = 0; // The current group size.
  1850. // Find out the size of the string buffer for the result.
  1851. if (groupDigits.Length != 0) // You can pass in 0 length arrays
  1852. {
  1853. int groupSizeCount = groupDigits[groupSizeIndex]; // The current total of group size.
  1854. while (digPos > groupSizeCount)
  1855. {
  1856. groupSize = groupDigits[groupSizeIndex];
  1857. if (groupSize == 0)
  1858. break;
  1859. bufferSize += sGroup.Length;
  1860. if (groupSizeIndex < groupDigits.Length - 1)
  1861. groupSizeIndex++;
  1862. groupSizeCount += groupDigits[groupSizeIndex];
  1863. if (groupSizeCount < 0 || bufferSize < 0)
  1864. throw new ArgumentOutOfRangeException(); // If we overflow
  1865. }
  1866. groupSize = groupSizeCount == 0 ? 0 : groupDigits[0]; // If you passed in an array with one entry as 0, groupSizeCount == 0
  1867. }
  1868. groupSizeIndex = 0;
  1869. int digitCount = 0;
  1870. int digLength = number.DigitsCount;
  1871. int digStart = (digPos < digLength) ? digPos : digLength;
  1872. fixed (char* spanPtr = &MemoryMarshal.GetReference(sb.AppendSpan(bufferSize)))
  1873. {
  1874. char* p = spanPtr + bufferSize - 1;
  1875. for (int i = digPos - 1; i >= 0; i--)
  1876. {
  1877. *(p--) = (i < digStart) ? (char)(dig[i]) : '0';
  1878. if (groupSize > 0)
  1879. {
  1880. digitCount++;
  1881. if ((digitCount == groupSize) && (i != 0))
  1882. {
  1883. for (int j = sGroup.Length - 1; j >= 0; j--)
  1884. *(p--) = sGroup[j];
  1885. if (groupSizeIndex < groupDigits.Length - 1)
  1886. {
  1887. groupSizeIndex++;
  1888. groupSize = groupDigits[groupSizeIndex];
  1889. }
  1890. digitCount = 0;
  1891. }
  1892. }
  1893. }
  1894. Debug.Assert(p >= spanPtr - 1, "Underflow");
  1895. dig += digStart;
  1896. }
  1897. }
  1898. else
  1899. {
  1900. do
  1901. {
  1902. sb.Append(*dig != 0 ? (char)(*dig++) : '0');
  1903. }
  1904. while (--digPos > 0);
  1905. }
  1906. }
  1907. else
  1908. {
  1909. sb.Append('0');
  1910. }
  1911. if (nMaxDigits > 0)
  1912. {
  1913. Debug.Assert(sDecimal != null);
  1914. sb.Append(sDecimal);
  1915. if ((digPos < 0) && (nMaxDigits > 0))
  1916. {
  1917. int zeroes = Math.Min(-digPos, nMaxDigits);
  1918. sb.Append('0', zeroes);
  1919. digPos += zeroes;
  1920. nMaxDigits -= zeroes;
  1921. }
  1922. while (nMaxDigits > 0)
  1923. {
  1924. sb.Append((*dig != 0) ? (char)(*dig++) : '0');
  1925. nMaxDigits--;
  1926. }
  1927. }
  1928. }
  1929. private static void FormatNumber(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info)
  1930. {
  1931. string fmt = number.IsNegative ?
  1932. s_negNumberFormats[info.NumberNegativePattern] :
  1933. PosNumberFormat;
  1934. foreach (char ch in fmt)
  1935. {
  1936. switch (ch)
  1937. {
  1938. case '#':
  1939. FormatFixed(ref sb, ref number, nMaxDigits, info._numberGroupSizes, info.NumberDecimalSeparator, info.NumberGroupSeparator);
  1940. break;
  1941. case '-':
  1942. sb.Append(info.NegativeSign);
  1943. break;
  1944. default:
  1945. sb.Append(ch);
  1946. break;
  1947. }
  1948. }
  1949. }
  1950. private static unsafe void FormatScientific(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info, char expChar)
  1951. {
  1952. byte* dig = number.GetDigitsPointer();
  1953. sb.Append((*dig != 0) ? (char)(*dig++) : '0');
  1954. if (nMaxDigits != 1) // For E0 we would like to suppress the decimal point
  1955. sb.Append(info.NumberDecimalSeparator);
  1956. while (--nMaxDigits > 0)
  1957. sb.Append((*dig != 0) ? (char)(*dig++) : '0');
  1958. int e = number.Digits[0] == 0 ? 0 : number.Scale - 1;
  1959. FormatExponent(ref sb, info, e, expChar, 3, true);
  1960. }
  1961. private static unsafe void FormatExponent(ref ValueStringBuilder sb, NumberFormatInfo info, int value, char expChar, int minDigits, bool positiveSign)
  1962. {
  1963. sb.Append(expChar);
  1964. if (value < 0)
  1965. {
  1966. sb.Append(info.NegativeSign);
  1967. value = -value;
  1968. }
  1969. else
  1970. {
  1971. if (positiveSign)
  1972. sb.Append(info.PositiveSign);
  1973. }
  1974. char* digits = stackalloc char[MaxUInt32DecDigits];
  1975. char* p = UInt32ToDecChars(digits + MaxUInt32DecDigits, (uint)value, minDigits);
  1976. sb.Append(p, (int)(digits + MaxUInt32DecDigits - p));
  1977. }
  1978. private static unsafe void FormatGeneral(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info, char expChar, bool bSuppressScientific)
  1979. {
  1980. int digPos = number.Scale;
  1981. bool scientific = false;
  1982. if (!bSuppressScientific)
  1983. {
  1984. // Don't switch to scientific notation
  1985. if (digPos > nMaxDigits || digPos < -3)
  1986. {
  1987. digPos = 1;
  1988. scientific = true;
  1989. }
  1990. }
  1991. byte* dig = number.GetDigitsPointer();
  1992. if (digPos > 0)
  1993. {
  1994. do
  1995. {
  1996. sb.Append((*dig != 0) ? (char)(*dig++) : '0');
  1997. } while (--digPos > 0);
  1998. }
  1999. else
  2000. {
  2001. sb.Append('0');
  2002. }
  2003. if (*dig != 0 || digPos < 0)
  2004. {
  2005. sb.Append(info.NumberDecimalSeparator);
  2006. while (digPos < 0)
  2007. {
  2008. sb.Append('0');
  2009. digPos++;
  2010. }
  2011. while (*dig != 0)
  2012. sb.Append((char)(*dig++));
  2013. }
  2014. if (scientific)
  2015. FormatExponent(ref sb, info, number.Scale - 1, expChar, 2, true);
  2016. }
  2017. private static void FormatPercent(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info)
  2018. {
  2019. string fmt = number.IsNegative ?
  2020. s_negPercentFormats[info.PercentNegativePattern] :
  2021. s_posPercentFormats[info.PercentPositivePattern];
  2022. foreach (char ch in fmt)
  2023. {
  2024. switch (ch)
  2025. {
  2026. case '#':
  2027. FormatFixed(ref sb, ref number, nMaxDigits, info._percentGroupSizes, info.PercentDecimalSeparator, info.PercentGroupSeparator);
  2028. break;
  2029. case '-':
  2030. sb.Append(info.NegativeSign);
  2031. break;
  2032. case '%':
  2033. sb.Append(info.PercentSymbol);
  2034. break;
  2035. default:
  2036. sb.Append(ch);
  2037. break;
  2038. }
  2039. }
  2040. }
  2041. internal static unsafe void RoundNumber(ref NumberBuffer number, int pos, bool isCorrectlyRounded)
  2042. {
  2043. byte* dig = number.GetDigitsPointer();
  2044. int i = 0;
  2045. while (i < pos && dig[i] != '\0')
  2046. i++;
  2047. if ((i == pos) && ShouldRoundUp(dig, i, number.Kind, isCorrectlyRounded))
  2048. {
  2049. while (i > 0 && dig[i - 1] == '9')
  2050. i--;
  2051. if (i > 0)
  2052. {
  2053. dig[i - 1]++;
  2054. }
  2055. else
  2056. {
  2057. number.Scale++;
  2058. dig[0] = (byte)('1');
  2059. i = 1;
  2060. }
  2061. }
  2062. else
  2063. {
  2064. while (i > 0 && dig[i - 1] == '0')
  2065. i--;
  2066. }
  2067. if (i == 0)
  2068. {
  2069. if (number.Kind != NumberBufferKind.FloatingPoint)
  2070. {
  2071. // The integer types don't have a concept of -0 and decimal always format -0 as 0
  2072. number.IsNegative = false;
  2073. }
  2074. number.Scale = 0; // Decimals with scale ('0.00') should be rounded.
  2075. }
  2076. dig[i] = (byte)('\0');
  2077. number.DigitsCount = i;
  2078. number.CheckConsistency();
  2079. static bool ShouldRoundUp(byte* dig, int i, NumberBufferKind numberKind, bool isCorrectlyRounded)
  2080. {
  2081. // We only want to round up if the digit is greater than or equal to 5 and we are
  2082. // not rounding a floating-point number. If we are rounding a floating-point number
  2083. // we have one of two cases.
  2084. //
  2085. // In the case of a standard numeric-format specifier, the exact and correctly rounded
  2086. // string will have been produced. In this scenario, pos will have pointed to the
  2087. // terminating null for the buffer and so this will return false.
  2088. //
  2089. // However, in the case of a custom numeric-format specifier, we currently fall back
  2090. // to generating Single/DoublePrecisionCustomFormat digits and then rely on this
  2091. // function to round correctly instead. This can unfortunately lead to double-rounding
  2092. // bugs but is the best we have right now due to back-compat concerns.
  2093. byte digit = dig[i];
  2094. if ((digit == '\0') || isCorrectlyRounded)
  2095. {
  2096. // Fast path for the common case with no rounding
  2097. return false;
  2098. }
  2099. // Values greater than or equal to 5 should round up, otherwise we round down. The IEEE
  2100. // 754 spec actually dictates that ties (exactly 5) should round to the nearest even number
  2101. // but that can have undesired behavior for custom numeric format strings. This probably
  2102. // needs further thought for .NET 5 so that we can be spec compliant and so that users
  2103. // can get the desired rounding behavior for their needs.
  2104. return digit >= '5';
  2105. }
  2106. }
  2107. private static unsafe int FindSection(ReadOnlySpan<char> format, int section)
  2108. {
  2109. int src;
  2110. char ch;
  2111. if (section == 0)
  2112. return 0;
  2113. fixed (char* pFormat = &MemoryMarshal.GetReference(format))
  2114. {
  2115. src = 0;
  2116. while (true)
  2117. {
  2118. if (src >= format.Length)
  2119. {
  2120. return 0;
  2121. }
  2122. switch (ch = pFormat[src++])
  2123. {
  2124. case '\'':
  2125. case '"':
  2126. while (src < format.Length && pFormat[src] != 0 && pFormat[src++] != ch) ;
  2127. break;
  2128. case '\\':
  2129. if (src < format.Length && pFormat[src] != 0)
  2130. src++;
  2131. break;
  2132. case ';':
  2133. if (--section != 0)
  2134. break;
  2135. if (src < format.Length && pFormat[src] != 0 && pFormat[src] != ';')
  2136. return src;
  2137. goto case '\0';
  2138. case '\0':
  2139. return 0;
  2140. }
  2141. }
  2142. }
  2143. }
  2144. private static uint Low32(ulong value) => (uint)value;
  2145. private static uint High32(ulong value) => (uint)((value & 0xFFFFFFFF00000000) >> 32);
  2146. private static uint Int64DivMod1E9(ref ulong value)
  2147. {
  2148. uint rem = (uint)(value % 1000000000);
  2149. value /= 1000000000;
  2150. return rem;
  2151. }
  2152. private static ulong ExtractFractionAndBiasedExponent(double value, out int exponent)
  2153. {
  2154. ulong bits = (ulong)(BitConverter.DoubleToInt64Bits(value));
  2155. ulong fraction = (bits & 0xFFFFFFFFFFFFF);
  2156. exponent = ((int)(bits >> 52) & 0x7FF);
  2157. if (exponent != 0)
  2158. {
  2159. // For normalized value, according to https://en.wikipedia.org/wiki/Double-precision_floating-point_format
  2160. // value = 1.fraction * 2^(exp - 1023)
  2161. // = (1 + mantissa / 2^52) * 2^(exp - 1023)
  2162. // = (2^52 + mantissa) * 2^(exp - 1023 - 52)
  2163. //
  2164. // So f = (2^52 + mantissa), e = exp - 1075;
  2165. fraction |= (1UL << 52);
  2166. exponent -= 1075;
  2167. }
  2168. else
  2169. {
  2170. // For denormalized value, according to https://en.wikipedia.org/wiki/Double-precision_floating-point_format
  2171. // value = 0.fraction * 2^(1 - 1023)
  2172. // = (mantissa / 2^52) * 2^(-1022)
  2173. // = mantissa * 2^(-1022 - 52)
  2174. // = mantissa * 2^(-1074)
  2175. // So f = mantissa, e = -1074
  2176. exponent = -1074;
  2177. }
  2178. return fraction;
  2179. }
  2180. private static uint ExtractFractionAndBiasedExponent(float value, out int exponent)
  2181. {
  2182. uint bits = (uint)(BitConverter.SingleToInt32Bits(value));
  2183. uint fraction = (bits & 0x7FFFFF);
  2184. exponent = ((int)(bits >> 23) & 0xFF);
  2185. if (exponent != 0)
  2186. {
  2187. // For normalized value, according to https://en.wikipedia.org/wiki/Single-precision_floating-point_format
  2188. // value = 1.fraction * 2^(exp - 127)
  2189. // = (1 + mantissa / 2^23) * 2^(exp - 127)
  2190. // = (2^23 + mantissa) * 2^(exp - 127 - 23)
  2191. //
  2192. // So f = (2^23 + mantissa), e = exp - 150;
  2193. fraction |= (1U << 23);
  2194. exponent -= 150;
  2195. }
  2196. else
  2197. {
  2198. // For denormalized value, according to https://en.wikipedia.org/wiki/Single-precision_floating-point_format
  2199. // value = 0.fraction * 2^(1 - 127)
  2200. // = (mantissa / 2^23) * 2^(-126)
  2201. // = mantissa * 2^(-126 - 23)
  2202. // = mantissa * 2^(-149)
  2203. // So f = mantissa, e = -149
  2204. exponent = -149;
  2205. }
  2206. return fraction;
  2207. }
  2208. }
  2209. }