2
0

Number.Formatting.cs 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410
  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. private const int SinglePrecision = 7;
  244. private const int DoublePrecision = 15;
  245. private const int ScaleNAN = unchecked((int)0x80000000);
  246. private const int ScaleINF = 0x7FFFFFFF;
  247. private const int MaxUInt32DecDigits = 10;
  248. private const int CharStackBufferSize = 32;
  249. private const string PosNumberFormat = "#";
  250. private static readonly string[] s_singleDigitStringCache = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
  251. private static readonly string[] s_posCurrencyFormats =
  252. {
  253. "$#", "#$", "$ #", "# $"
  254. };
  255. private static readonly string[] s_negCurrencyFormats =
  256. {
  257. "($#)", "-$#", "$-#", "$#-",
  258. "(#$)", "-#$", "#-$", "#$-",
  259. "-# $", "-$ #", "# $-", "$ #-",
  260. "$ -#", "#- $", "($ #)", "(# $)"
  261. };
  262. private static readonly string[] s_posPercentFormats =
  263. {
  264. "# %", "#%", "%#", "% #"
  265. };
  266. private static readonly string[] s_negPercentFormats =
  267. {
  268. "-# %", "-#%", "-%#",
  269. "%-#", "%#-",
  270. "#-%", "#%-",
  271. "-% #", "# %-", "% #-",
  272. "% -#", "#- %"
  273. };
  274. private static readonly string[] s_negNumberFormats =
  275. {
  276. "(#)", "-#", "- #", "#-", "# -",
  277. };
  278. public static unsafe string FormatDecimal(decimal value, ReadOnlySpan<char> format, NumberFormatInfo info)
  279. {
  280. char fmt = ParseFormatSpecifier(format, out int digits);
  281. byte* pDigits = stackalloc byte[DecimalNumberBufferLength];
  282. NumberBuffer number = new NumberBuffer(NumberBufferKind.Decimal, pDigits, DecimalNumberBufferLength);
  283. DecimalToNumber(ref value, ref number);
  284. char* stackPtr = stackalloc char[CharStackBufferSize];
  285. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  286. if (fmt != 0)
  287. {
  288. NumberToString(ref sb, ref number, fmt, digits, info);
  289. }
  290. else
  291. {
  292. NumberToStringFormat(ref sb, ref number, format, info);
  293. }
  294. return sb.ToString();
  295. }
  296. public static unsafe bool TryFormatDecimal(decimal value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
  297. {
  298. char fmt = ParseFormatSpecifier(format, out int digits);
  299. byte* pDigits = stackalloc byte[DecimalNumberBufferLength];
  300. NumberBuffer number = new NumberBuffer(NumberBufferKind.Decimal, pDigits, DecimalNumberBufferLength);
  301. DecimalToNumber(ref value, ref number);
  302. char* stackPtr = stackalloc char[CharStackBufferSize];
  303. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  304. if (fmt != 0)
  305. {
  306. NumberToString(ref sb, ref number, fmt, digits, info);
  307. }
  308. else
  309. {
  310. NumberToStringFormat(ref sb, ref number, format, info);
  311. }
  312. return sb.TryCopyTo(destination, out charsWritten);
  313. }
  314. internal static unsafe void DecimalToNumber(ref decimal d, ref NumberBuffer number)
  315. {
  316. byte* buffer = number.GetDigitsPointer();
  317. number.DigitsCount = DecimalPrecision;
  318. number.IsNegative = d.IsNegative;
  319. byte* p = buffer + DecimalPrecision;
  320. while ((d.Mid | d.High) != 0)
  321. {
  322. p = UInt32ToDecChars(p, decimal.DecDivMod1E9(ref d), 9);
  323. }
  324. p = UInt32ToDecChars(p, d.Low, 0);
  325. int i = (int)((buffer + DecimalPrecision) - p);
  326. number.DigitsCount = i;
  327. number.Scale = i - d.Scale;
  328. byte* dst = number.GetDigitsPointer();
  329. while (--i >= 0)
  330. {
  331. *dst++ = *p++;
  332. }
  333. *dst = (byte)('\0');
  334. number.CheckConsistency();
  335. }
  336. public static string FormatDouble(double value, string format, NumberFormatInfo info)
  337. {
  338. Span<char> stackBuffer = stackalloc char[CharStackBufferSize];
  339. var sb = new ValueStringBuilder(stackBuffer);
  340. return FormatDouble(ref sb, value, format, info) ?? sb.ToString();
  341. }
  342. public static bool TryFormatDouble(double value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
  343. {
  344. Span<char> stackBuffer = stackalloc char[CharStackBufferSize];
  345. var sb = new ValueStringBuilder(stackBuffer);
  346. string s = FormatDouble(ref sb, value, format, info);
  347. return s != null ?
  348. TryCopyTo(s, destination, out charsWritten) :
  349. sb.TryCopyTo(destination, out charsWritten);
  350. }
  351. /// <summary>Formats the specified value according to the specified format and info.</summary>
  352. /// <returns>
  353. /// Non-null if an existing string can be returned, in which case the builder will be unmodified.
  354. /// Null if no existing string was returned, in which case the formatted output is in the builder.
  355. /// </returns>
  356. private static unsafe string FormatDouble(ref ValueStringBuilder sb, double value, ReadOnlySpan<char> format, NumberFormatInfo info)
  357. {
  358. char fmt = ParseFormatSpecifier(format, out int digits);
  359. int precision = DoublePrecision;
  360. byte* pDigits = stackalloc byte[DoubleNumberBufferLength];
  361. NumberBuffer number = new NumberBuffer(NumberBufferKind.FloatingPoint, pDigits, DoubleNumberBufferLength);
  362. switch (fmt)
  363. {
  364. case 'R':
  365. case 'r':
  366. {
  367. // In order to give numbers that are both friendly to display and round-trippable, we parse the
  368. // number using 15 digits and then determine if it round trips to the same value. If it does, we
  369. // convert that NUMBER to a string, otherwise we reparse using 17 digits and display that.
  370. DoubleToNumber(value, DoublePrecision, ref number);
  371. if (number.Scale == ScaleNAN)
  372. {
  373. return info.NaNSymbol;
  374. }
  375. else if (number.Scale == ScaleINF)
  376. {
  377. return number.IsNegative ? info.NegativeInfinitySymbol : info.PositiveInfinitySymbol;
  378. }
  379. double roundTrip = NumberToDouble(ref number);
  380. if (roundTrip == value)
  381. {
  382. NumberToString(ref sb, ref number, 'G', DoublePrecision, info);
  383. }
  384. else
  385. {
  386. DoubleToNumber(value, 17, ref number);
  387. NumberToString(ref sb, ref number, 'G', 17, info);
  388. }
  389. return null;
  390. }
  391. case 'E':
  392. case 'e':
  393. // Round values less than E14 to 15 digits
  394. if (digits > 14)
  395. {
  396. precision = 17;
  397. }
  398. break;
  399. case 'G':
  400. case 'g':
  401. // Round values less than G15 to 15 digits. G16 and G17 will not be touched.
  402. if (digits > 15)
  403. {
  404. precision = 17;
  405. }
  406. break;
  407. }
  408. DoubleToNumber(value, precision, ref number);
  409. if (number.Scale == ScaleNAN)
  410. {
  411. return info.NaNSymbol;
  412. }
  413. else if (number.Scale == ScaleINF)
  414. {
  415. return number.IsNegative ? info.NegativeInfinitySymbol : info.PositiveInfinitySymbol;
  416. }
  417. if (fmt != 0)
  418. {
  419. NumberToString(ref sb, ref number, fmt, digits, info);
  420. }
  421. else
  422. {
  423. NumberToStringFormat(ref sb, ref number, format, info);
  424. }
  425. return null;
  426. }
  427. public static string FormatSingle(float value, string format, NumberFormatInfo info)
  428. {
  429. Span<char> stackBuffer = stackalloc char[CharStackBufferSize];
  430. var sb = new ValueStringBuilder(stackBuffer);
  431. return FormatSingle(ref sb, value, format, info) ?? sb.ToString();
  432. }
  433. public static bool TryFormatSingle(float value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
  434. {
  435. Span<char> stackBuffer = stackalloc char[CharStackBufferSize];
  436. var sb = new ValueStringBuilder(stackBuffer);
  437. string s = FormatSingle(ref sb, value, format, info);
  438. return s != null ?
  439. TryCopyTo(s, destination, out charsWritten) :
  440. sb.TryCopyTo(destination, out charsWritten);
  441. }
  442. /// <summary>Formats the specified value according to the specified format and info.</summary>
  443. /// <returns>
  444. /// Non-null if an existing string can be returned, in which case the builder will be unmodified.
  445. /// Null if no existing string was returned, in which case the formatted output is in the builder.
  446. /// </returns>
  447. private static unsafe string FormatSingle(ref ValueStringBuilder sb, float value, ReadOnlySpan<char> format, NumberFormatInfo info)
  448. {
  449. char fmt = ParseFormatSpecifier(format, out int digits);
  450. int precision = SinglePrecision;
  451. byte* pDigits = stackalloc byte[SingleNumberBufferLength];
  452. NumberBuffer number = new NumberBuffer(NumberBufferKind.FloatingPoint, pDigits, SingleNumberBufferLength);
  453. switch (fmt)
  454. {
  455. case 'R':
  456. case 'r':
  457. {
  458. // In order to give numbers that are both friendly to display and round-trippable, we parse the
  459. // number using 7 digits and then determine if it round trips to the same value. If it does, we
  460. // convert that NUMBER to a string, otherwise we reparse using 9 digits and display that.
  461. DoubleToNumber(value, SinglePrecision, ref number);
  462. if (number.Scale == ScaleNAN)
  463. {
  464. return info.NaNSymbol;
  465. }
  466. else if (number.Scale == ScaleINF)
  467. {
  468. return number.IsNegative ? info.NegativeInfinitySymbol : info.PositiveInfinitySymbol;
  469. }
  470. float roundTrip = NumberToSingle(ref number);
  471. if (roundTrip == value)
  472. {
  473. NumberToString(ref sb, ref number, 'G', SinglePrecision, info);
  474. }
  475. else
  476. {
  477. DoubleToNumber(value, 9, ref number);
  478. NumberToString(ref sb, ref number, 'G', 9, info);
  479. }
  480. return null;
  481. }
  482. case 'E':
  483. case 'e':
  484. // Round values less than E14 to 15 digits.
  485. if (digits > 6)
  486. {
  487. precision = 9;
  488. }
  489. break;
  490. case 'G':
  491. case 'g':
  492. // Round values less than G15 to 15 digits. G16 and G17 will not be touched.
  493. if (digits > 7)
  494. {
  495. precision = 9;
  496. }
  497. break;
  498. }
  499. DoubleToNumber(value, precision, ref number);
  500. if (number.Scale == ScaleNAN)
  501. {
  502. return info.NaNSymbol;
  503. }
  504. else if (number.Scale == ScaleINF)
  505. {
  506. return number.IsNegative ? info.NegativeInfinitySymbol : info.PositiveInfinitySymbol;
  507. }
  508. if (fmt != 0)
  509. {
  510. NumberToString(ref sb, ref number, fmt, digits, info);
  511. }
  512. else
  513. {
  514. NumberToStringFormat(ref sb, ref number, format, info);
  515. }
  516. return null;
  517. }
  518. private static bool TryCopyTo(string source, Span<char> destination, out int charsWritten)
  519. {
  520. Debug.Assert(source != null);
  521. if (source.AsSpan().TryCopyTo(destination))
  522. {
  523. charsWritten = source.Length;
  524. return true;
  525. }
  526. charsWritten = 0;
  527. return false;
  528. }
  529. public static unsafe string FormatInt32(int value, ReadOnlySpan<char> format, IFormatProvider provider)
  530. {
  531. // Fast path for default format with a non-negative value
  532. if (value >= 0 && format.Length == 0)
  533. {
  534. return UInt32ToDecStr((uint)value, digits: -1);
  535. }
  536. char fmt = ParseFormatSpecifier(format, out int digits);
  537. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  538. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  539. {
  540. return value >= 0 ?
  541. UInt32ToDecStr((uint)value, digits) :
  542. NegativeInt32ToDecStr(value, digits, NumberFormatInfo.GetInstance(provider).NegativeSign);
  543. }
  544. else if (fmtUpper == 'X')
  545. {
  546. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  547. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code produces lowercase.
  548. return Int32ToHexStr(value, (char)(fmt - ('X' - 'A' + 10)), digits);
  549. }
  550. else
  551. {
  552. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  553. byte* pDigits = stackalloc byte[Int32NumberBufferLength];
  554. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, Int32NumberBufferLength);
  555. Int32ToNumber(value, ref number);
  556. char* stackPtr = stackalloc char[CharStackBufferSize];
  557. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  558. if (fmt != 0)
  559. {
  560. NumberToString(ref sb, ref number, fmt, digits, info);
  561. }
  562. else
  563. {
  564. NumberToStringFormat(ref sb, ref number, format, info);
  565. }
  566. return sb.ToString();
  567. }
  568. }
  569. public static unsafe bool TryFormatInt32(int value, ReadOnlySpan<char> format, IFormatProvider provider, Span<char> destination, out int charsWritten)
  570. {
  571. // Fast path for default format with a non-negative value
  572. if (value >= 0 && format.Length == 0)
  573. {
  574. return TryUInt32ToDecStr((uint)value, digits: -1, destination, out charsWritten);
  575. }
  576. char fmt = ParseFormatSpecifier(format, out int digits);
  577. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  578. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  579. {
  580. return value >= 0 ?
  581. TryUInt32ToDecStr((uint)value, digits, destination, out charsWritten) :
  582. TryNegativeInt32ToDecStr(value, digits, NumberFormatInfo.GetInstance(provider).NegativeSign, destination, out charsWritten);
  583. }
  584. else if (fmtUpper == 'X')
  585. {
  586. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  587. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code produces lowercase.
  588. return TryInt32ToHexStr(value, (char)(fmt - ('X' - 'A' + 10)), digits, destination, out charsWritten);
  589. }
  590. else
  591. {
  592. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  593. byte* pDigits = stackalloc byte[Int32NumberBufferLength];
  594. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, Int32NumberBufferLength);
  595. Int32ToNumber(value, ref number);
  596. char* stackPtr = stackalloc char[CharStackBufferSize];
  597. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  598. if (fmt != 0)
  599. {
  600. NumberToString(ref sb, ref number, fmt, digits, info);
  601. }
  602. else
  603. {
  604. NumberToStringFormat(ref sb, ref number, format, info);
  605. }
  606. return sb.TryCopyTo(destination, out charsWritten);
  607. }
  608. }
  609. public static unsafe string FormatUInt32(uint value, ReadOnlySpan<char> format, IFormatProvider provider)
  610. {
  611. // Fast path for default format
  612. if (format.Length == 0)
  613. {
  614. return UInt32ToDecStr(value, digits: -1);
  615. }
  616. char fmt = ParseFormatSpecifier(format, out int digits);
  617. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  618. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  619. {
  620. return UInt32ToDecStr(value, digits);
  621. }
  622. else if (fmtUpper == 'X')
  623. {
  624. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  625. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code produces lowercase.
  626. return Int32ToHexStr((int)value, (char)(fmt - ('X' - 'A' + 10)), digits);
  627. }
  628. else
  629. {
  630. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  631. byte* pDigits = stackalloc byte[UInt32NumberBufferLength];
  632. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, UInt32NumberBufferLength);
  633. UInt32ToNumber(value, ref number);
  634. char* stackPtr = stackalloc char[CharStackBufferSize];
  635. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  636. if (fmt != 0)
  637. {
  638. NumberToString(ref sb, ref number, fmt, digits, info);
  639. }
  640. else
  641. {
  642. NumberToStringFormat(ref sb, ref number, format, info);
  643. }
  644. return sb.ToString();
  645. }
  646. }
  647. public static unsafe bool TryFormatUInt32(uint value, ReadOnlySpan<char> format, IFormatProvider provider, Span<char> destination, out int charsWritten)
  648. {
  649. // Fast path for default format
  650. if (format.Length == 0)
  651. {
  652. return TryUInt32ToDecStr(value, digits: -1, destination, out charsWritten);
  653. }
  654. char fmt = ParseFormatSpecifier(format, out int digits);
  655. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  656. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  657. {
  658. return TryUInt32ToDecStr(value, digits, destination, out charsWritten);
  659. }
  660. else if (fmtUpper == 'X')
  661. {
  662. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  663. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code produces lowercase.
  664. return TryInt32ToHexStr((int)value, (char)(fmt - ('X' - 'A' + 10)), digits, destination, out charsWritten);
  665. }
  666. else
  667. {
  668. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  669. byte* pDigits = stackalloc byte[UInt32NumberBufferLength];
  670. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, UInt32NumberBufferLength);
  671. UInt32ToNumber(value, ref number);
  672. char* stackPtr = stackalloc char[CharStackBufferSize];
  673. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  674. if (fmt != 0)
  675. {
  676. NumberToString(ref sb, ref number, fmt, digits, info);
  677. }
  678. else
  679. {
  680. NumberToStringFormat(ref sb, ref number, format, info);
  681. }
  682. return sb.TryCopyTo(destination, out charsWritten);
  683. }
  684. }
  685. public static unsafe string FormatInt64(long value, ReadOnlySpan<char> format, IFormatProvider provider)
  686. {
  687. // Fast path for default format with a non-negative value
  688. if (value >= 0 && format.Length == 0)
  689. {
  690. return UInt64ToDecStr((ulong)value, digits: -1);
  691. }
  692. char fmt = ParseFormatSpecifier(format, out int digits);
  693. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  694. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  695. {
  696. return value >= 0 ?
  697. UInt64ToDecStr((ulong)value, digits) :
  698. NegativeInt64ToDecStr(value, digits, NumberFormatInfo.GetInstance(provider).NegativeSign);
  699. }
  700. else if (fmtUpper == 'X')
  701. {
  702. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  703. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code
  704. // produces lowercase.
  705. return Int64ToHexStr(value, (char)(fmt - ('X' - 'A' + 10)), digits);
  706. }
  707. else
  708. {
  709. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  710. byte* pDigits = stackalloc byte[Int64NumberBufferLength];
  711. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, Int64NumberBufferLength);
  712. Int64ToNumber(value, ref number);
  713. char* stackPtr = stackalloc char[CharStackBufferSize];
  714. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  715. if (fmt != 0)
  716. {
  717. NumberToString(ref sb, ref number, fmt, digits, info);
  718. }
  719. else
  720. {
  721. NumberToStringFormat(ref sb, ref number, format, info);
  722. }
  723. return sb.ToString();
  724. }
  725. }
  726. public static unsafe bool TryFormatInt64(long value, ReadOnlySpan<char> format, IFormatProvider provider, Span<char> destination, out int charsWritten)
  727. {
  728. // Fast path for default format with a non-negative value
  729. if (value >= 0 && format.Length == 0)
  730. {
  731. return TryUInt64ToDecStr((ulong)value, digits: -1, destination, out charsWritten);
  732. }
  733. char fmt = ParseFormatSpecifier(format, out int digits);
  734. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  735. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  736. {
  737. return value >= 0 ?
  738. TryUInt64ToDecStr((ulong)value, digits, destination, out charsWritten) :
  739. TryNegativeInt64ToDecStr(value, digits, NumberFormatInfo.GetInstance(provider).NegativeSign, destination, out charsWritten);
  740. }
  741. else if (fmtUpper == 'X')
  742. {
  743. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  744. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code
  745. // produces lowercase.
  746. return TryInt64ToHexStr(value, (char)(fmt - ('X' - 'A' + 10)), digits, destination, out charsWritten);
  747. }
  748. else
  749. {
  750. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  751. byte* pDigits = stackalloc byte[Int64NumberBufferLength];
  752. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, Int64NumberBufferLength);
  753. Int64ToNumber(value, ref number);
  754. char* stackPtr = stackalloc char[CharStackBufferSize];
  755. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  756. if (fmt != 0)
  757. {
  758. NumberToString(ref sb, ref number, fmt, digits, info);
  759. }
  760. else
  761. {
  762. NumberToStringFormat(ref sb, ref number, format, info);
  763. }
  764. return sb.TryCopyTo(destination, out charsWritten);
  765. }
  766. }
  767. public static unsafe string FormatUInt64(ulong value, ReadOnlySpan<char> format, IFormatProvider provider)
  768. {
  769. // Fast path for default format
  770. if (format.Length == 0)
  771. {
  772. return UInt64ToDecStr(value, digits: -1);
  773. }
  774. char fmt = ParseFormatSpecifier(format, out int digits);
  775. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  776. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  777. {
  778. return UInt64ToDecStr(value, digits);
  779. }
  780. else if (fmtUpper == 'X')
  781. {
  782. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  783. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code
  784. // produces lowercase.
  785. return Int64ToHexStr((long)value, (char)(fmt - ('X' - 'A' + 10)), digits);
  786. }
  787. else
  788. {
  789. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  790. byte* pDigits = stackalloc byte[UInt64NumberBufferLength];
  791. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, UInt64NumberBufferLength);
  792. UInt64ToNumber(value, ref number);
  793. char* stackPtr = stackalloc char[CharStackBufferSize];
  794. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  795. if (fmt != 0)
  796. {
  797. NumberToString(ref sb, ref number, fmt, digits, info);
  798. }
  799. else
  800. {
  801. NumberToStringFormat(ref sb, ref number, format, info);
  802. }
  803. return sb.ToString();
  804. }
  805. }
  806. public static unsafe bool TryFormatUInt64(ulong value, ReadOnlySpan<char> format, IFormatProvider provider, Span<char> destination, out int charsWritten)
  807. {
  808. // Fast path for default format
  809. if (format.Length == 0)
  810. {
  811. return TryUInt64ToDecStr(value, digits: -1, destination, out charsWritten);
  812. }
  813. char fmt = ParseFormatSpecifier(format, out int digits);
  814. char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
  815. if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
  816. {
  817. return TryUInt64ToDecStr(value, digits, destination, out charsWritten);
  818. }
  819. else if (fmtUpper == 'X')
  820. {
  821. // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
  822. // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code
  823. // produces lowercase.
  824. return TryInt64ToHexStr((long)value, (char)(fmt - ('X' - 'A' + 10)), digits, destination, out charsWritten);
  825. }
  826. else
  827. {
  828. NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
  829. byte* pDigits = stackalloc byte[UInt64NumberBufferLength];
  830. NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, UInt64NumberBufferLength);
  831. UInt64ToNumber(value, ref number);
  832. char* stackPtr = stackalloc char[CharStackBufferSize];
  833. ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
  834. if (fmt != 0)
  835. {
  836. NumberToString(ref sb, ref number, fmt, digits, info);
  837. }
  838. else
  839. {
  840. NumberToStringFormat(ref sb, ref number, format, info);
  841. }
  842. return sb.TryCopyTo(destination, out charsWritten);
  843. }
  844. }
  845. [MethodImpl(MethodImplOptions.AggressiveInlining)] // called from only one location
  846. private static unsafe void Int32ToNumber(int value, ref NumberBuffer number)
  847. {
  848. number.DigitsCount = Int32Precision;
  849. if (value >= 0)
  850. {
  851. number.IsNegative = false;
  852. }
  853. else
  854. {
  855. number.IsNegative = true;
  856. value = -value;
  857. }
  858. byte* buffer = number.GetDigitsPointer();
  859. byte* p = UInt32ToDecChars(buffer + Int32Precision, (uint)value, 0);
  860. int i = (int)(buffer + Int32Precision - p);
  861. number.DigitsCount = i;
  862. number.Scale = i;
  863. byte* dst = number.GetDigitsPointer();
  864. while (--i >= 0)
  865. *dst++ = *p++;
  866. *dst = (byte)('\0');
  867. number.CheckConsistency();
  868. }
  869. private static unsafe string NegativeInt32ToDecStr(int value, int digits, string sNegative)
  870. {
  871. Debug.Assert(value < 0);
  872. if (digits < 1)
  873. digits = 1;
  874. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits((uint)(-value))) + sNegative.Length;
  875. string result = string.FastAllocateString(bufferLength);
  876. fixed (char* buffer = result)
  877. {
  878. char* p = UInt32ToDecChars(buffer + bufferLength, (uint)(-value), digits);
  879. Debug.Assert(p == buffer + sNegative.Length);
  880. for (int i = sNegative.Length - 1; i >= 0; i--)
  881. {
  882. *(--p) = sNegative[i];
  883. }
  884. Debug.Assert(p == buffer);
  885. }
  886. return result;
  887. }
  888. private static unsafe bool TryNegativeInt32ToDecStr(int value, int digits, string sNegative, Span<char> destination, out int charsWritten)
  889. {
  890. Debug.Assert(value < 0);
  891. if (digits < 1)
  892. digits = 1;
  893. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits((uint)(-value))) + sNegative.Length;
  894. if (bufferLength > destination.Length)
  895. {
  896. charsWritten = 0;
  897. return false;
  898. }
  899. charsWritten = bufferLength;
  900. fixed (char* buffer = &MemoryMarshal.GetReference(destination))
  901. {
  902. char* p = UInt32ToDecChars(buffer + bufferLength, (uint)(-value), digits);
  903. Debug.Assert(p == buffer + sNegative.Length);
  904. for (int i = sNegative.Length - 1; i >= 0; i--)
  905. {
  906. *(--p) = sNegative[i];
  907. }
  908. Debug.Assert(p == buffer);
  909. }
  910. return true;
  911. }
  912. private static unsafe string Int32ToHexStr(int value, char hexBase, int digits)
  913. {
  914. if (digits < 1)
  915. digits = 1;
  916. int bufferLength = Math.Max(digits, FormattingHelpers.CountHexDigits((uint)value));
  917. string result = string.FastAllocateString(bufferLength);
  918. fixed (char* buffer = result)
  919. {
  920. char* p = Int32ToHexChars(buffer + bufferLength, (uint)value, hexBase, digits);
  921. Debug.Assert(p == buffer);
  922. }
  923. return result;
  924. }
  925. private static unsafe bool TryInt32ToHexStr(int value, char hexBase, int digits, Span<char> destination, out int charsWritten)
  926. {
  927. if (digits < 1)
  928. digits = 1;
  929. int bufferLength = Math.Max(digits, FormattingHelpers.CountHexDigits((uint)value));
  930. if (bufferLength > destination.Length)
  931. {
  932. charsWritten = 0;
  933. return false;
  934. }
  935. charsWritten = bufferLength;
  936. fixed (char* buffer = &MemoryMarshal.GetReference(destination))
  937. {
  938. char* p = Int32ToHexChars(buffer + bufferLength, (uint)value, hexBase, digits);
  939. Debug.Assert(p == buffer);
  940. }
  941. return true;
  942. }
  943. private static unsafe char* Int32ToHexChars(char* buffer, uint value, int hexBase, int digits)
  944. {
  945. while (--digits >= 0 || value != 0)
  946. {
  947. byte digit = (byte)(value & 0xF);
  948. *(--buffer) = (char)(digit + (digit < 10 ? (byte)'0' : hexBase));
  949. value >>= 4;
  950. }
  951. return buffer;
  952. }
  953. [MethodImpl(MethodImplOptions.AggressiveInlining)] // called from only one location
  954. private static unsafe void UInt32ToNumber(uint value, ref NumberBuffer number)
  955. {
  956. number.DigitsCount = UInt32Precision;
  957. number.IsNegative = false;
  958. byte* buffer = number.GetDigitsPointer();
  959. byte* p = UInt32ToDecChars(buffer + UInt32Precision, value, 0);
  960. int i = (int)(buffer + UInt32Precision - p);
  961. number.DigitsCount = i;
  962. number.Scale = i;
  963. byte* dst = number.GetDigitsPointer();
  964. while (--i >= 0)
  965. *dst++ = *p++;
  966. *dst = (byte)('\0');
  967. number.CheckConsistency();
  968. }
  969. internal static unsafe byte* UInt32ToDecChars(byte* bufferEnd, uint value, int digits)
  970. {
  971. while (--digits >= 0 || value != 0)
  972. {
  973. // TODO https://github.com/dotnet/coreclr/issues/3439
  974. uint newValue = value / 10;
  975. *(--bufferEnd) = (byte)(value - (newValue * 10) + '0');
  976. value = newValue;
  977. }
  978. return bufferEnd;
  979. }
  980. internal static unsafe char* UInt32ToDecChars(char* bufferEnd, uint value, int digits)
  981. {
  982. while (--digits >= 0 || value != 0)
  983. {
  984. // TODO https://github.com/dotnet/coreclr/issues/3439
  985. uint newValue = value / 10;
  986. *(--bufferEnd) = (char)(value - (newValue * 10) + '0');
  987. value = newValue;
  988. }
  989. return bufferEnd;
  990. }
  991. private static unsafe string UInt32ToDecStr(uint value, int digits)
  992. {
  993. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
  994. // For single-digit values that are very common, especially 0 and 1, just return cached strings.
  995. if (bufferLength == 1)
  996. {
  997. return s_singleDigitStringCache[value];
  998. }
  999. string result = string.FastAllocateString(bufferLength);
  1000. fixed (char* buffer = result)
  1001. {
  1002. char* p = buffer + bufferLength;
  1003. if (digits <= 1)
  1004. {
  1005. do
  1006. {
  1007. // TODO https://github.com/dotnet/coreclr/issues/3439
  1008. uint div = value / 10;
  1009. *(--p) = (char)('0' + value - (div * 10));
  1010. value = div;
  1011. }
  1012. while (value != 0);
  1013. }
  1014. else
  1015. {
  1016. p = UInt32ToDecChars(p, value, digits);
  1017. }
  1018. Debug.Assert(p == buffer);
  1019. }
  1020. return result;
  1021. }
  1022. private static unsafe bool TryUInt32ToDecStr(uint value, int digits, Span<char> destination, out int charsWritten)
  1023. {
  1024. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
  1025. if (bufferLength > destination.Length)
  1026. {
  1027. charsWritten = 0;
  1028. return false;
  1029. }
  1030. charsWritten = bufferLength;
  1031. fixed (char* buffer = &MemoryMarshal.GetReference(destination))
  1032. {
  1033. char* p = buffer + bufferLength;
  1034. if (digits <= 1)
  1035. {
  1036. do
  1037. {
  1038. // TODO https://github.com/dotnet/coreclr/issues/3439
  1039. uint div = value / 10;
  1040. *(--p) = (char)('0' + value - (div * 10));
  1041. value = div;
  1042. }
  1043. while (value != 0);
  1044. }
  1045. else
  1046. {
  1047. p = UInt32ToDecChars(p, value, digits);
  1048. }
  1049. Debug.Assert(p == buffer);
  1050. }
  1051. return true;
  1052. }
  1053. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1054. private static unsafe bool TryCopyTo(char* src, int length, Span<char> destination, out int charsWritten)
  1055. {
  1056. if (new ReadOnlySpan<char>(src, length).TryCopyTo(destination))
  1057. {
  1058. charsWritten = length;
  1059. return true;
  1060. }
  1061. else
  1062. {
  1063. charsWritten = 0;
  1064. return false;
  1065. }
  1066. }
  1067. private static unsafe void Int64ToNumber(long input, ref NumberBuffer number)
  1068. {
  1069. ulong value = (ulong)input;
  1070. number.IsNegative = input < 0;
  1071. number.DigitsCount = Int64Precision;
  1072. if (number.IsNegative)
  1073. {
  1074. value = (ulong)(-input);
  1075. }
  1076. byte* buffer = number.GetDigitsPointer();
  1077. byte* p = buffer + Int64Precision;
  1078. while (High32(value) != 0)
  1079. p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
  1080. p = UInt32ToDecChars(p, Low32(value), 0);
  1081. int i = (int)(buffer + Int64Precision - p);
  1082. number.DigitsCount = i;
  1083. number.Scale = i;
  1084. byte* dst = number.GetDigitsPointer();
  1085. while (--i >= 0)
  1086. *dst++ = *p++;
  1087. *dst = (byte)('\0');
  1088. number.CheckConsistency();
  1089. }
  1090. private static unsafe string NegativeInt64ToDecStr(long input, int digits, string sNegative)
  1091. {
  1092. Debug.Assert(input < 0);
  1093. if (digits < 1)
  1094. {
  1095. digits = 1;
  1096. }
  1097. ulong value = (ulong)(-input);
  1098. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value)) + sNegative.Length;
  1099. string result = string.FastAllocateString(bufferLength);
  1100. fixed (char* buffer = result)
  1101. {
  1102. char* p = buffer + bufferLength;
  1103. while (High32(value) != 0)
  1104. {
  1105. p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
  1106. digits -= 9;
  1107. }
  1108. p = UInt32ToDecChars(p, Low32(value), digits);
  1109. Debug.Assert(p == buffer + sNegative.Length);
  1110. for (int i = sNegative.Length - 1; i >= 0; i--)
  1111. {
  1112. *(--p) = sNegative[i];
  1113. }
  1114. Debug.Assert(p == buffer);
  1115. }
  1116. return result;
  1117. }
  1118. private static unsafe bool TryNegativeInt64ToDecStr(long input, int digits, string sNegative, Span<char> destination, out int charsWritten)
  1119. {
  1120. Debug.Assert(input < 0);
  1121. if (digits < 1)
  1122. {
  1123. digits = 1;
  1124. }
  1125. ulong value = (ulong)(-input);
  1126. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits((ulong)(-input))) + sNegative.Length;
  1127. if (bufferLength > destination.Length)
  1128. {
  1129. charsWritten = 0;
  1130. return false;
  1131. }
  1132. charsWritten = bufferLength;
  1133. fixed (char* buffer = &MemoryMarshal.GetReference(destination))
  1134. {
  1135. char* p = buffer + bufferLength;
  1136. while (High32(value) != 0)
  1137. {
  1138. p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
  1139. digits -= 9;
  1140. }
  1141. p = UInt32ToDecChars(p, Low32(value), digits);
  1142. Debug.Assert(p == buffer + sNegative.Length);
  1143. for (int i = sNegative.Length - 1; i >= 0; i--)
  1144. {
  1145. *(--p) = sNegative[i];
  1146. }
  1147. Debug.Assert(p == buffer);
  1148. }
  1149. return true;
  1150. }
  1151. private static unsafe string Int64ToHexStr(long value, char hexBase, int digits)
  1152. {
  1153. int bufferLength = Math.Max(digits, FormattingHelpers.CountHexDigits((ulong)value));
  1154. string result = string.FastAllocateString(bufferLength);
  1155. fixed (char* buffer = result)
  1156. {
  1157. char* p = buffer + bufferLength;
  1158. if (High32((ulong)value) != 0)
  1159. {
  1160. p = Int32ToHexChars(p, Low32((ulong)value), hexBase, 8);
  1161. p = Int32ToHexChars(p, High32((ulong)value), hexBase, digits - 8);
  1162. }
  1163. else
  1164. {
  1165. p = Int32ToHexChars(p, Low32((ulong)value), hexBase, Math.Max(digits, 1));
  1166. }
  1167. Debug.Assert(p == buffer);
  1168. }
  1169. return result;
  1170. }
  1171. private static unsafe bool TryInt64ToHexStr(long value, char hexBase, int digits, Span<char> destination, out int charsWritten)
  1172. {
  1173. int bufferLength = Math.Max(digits, FormattingHelpers.CountHexDigits((ulong)value));
  1174. if (bufferLength > destination.Length)
  1175. {
  1176. charsWritten = 0;
  1177. return false;
  1178. }
  1179. charsWritten = bufferLength;
  1180. fixed (char* buffer = &MemoryMarshal.GetReference(destination))
  1181. {
  1182. char* p = buffer + bufferLength;
  1183. if (High32((ulong)value) != 0)
  1184. {
  1185. p = Int32ToHexChars(p, Low32((ulong)value), hexBase, 8);
  1186. p = Int32ToHexChars(p, High32((ulong)value), hexBase, digits - 8);
  1187. }
  1188. else
  1189. {
  1190. p = Int32ToHexChars(p, Low32((ulong)value), hexBase, Math.Max(digits, 1));
  1191. }
  1192. Debug.Assert(p == buffer);
  1193. }
  1194. return true;
  1195. }
  1196. private static unsafe void UInt64ToNumber(ulong value, ref NumberBuffer number)
  1197. {
  1198. number.DigitsCount = UInt64Precision;
  1199. number.IsNegative = false;
  1200. byte* buffer = number.GetDigitsPointer();
  1201. byte* p = buffer + UInt64Precision;
  1202. while (High32(value) != 0)
  1203. p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
  1204. p = UInt32ToDecChars(p, Low32(value), 0);
  1205. int i = (int)(buffer + UInt64Precision - p);
  1206. number.DigitsCount = i;
  1207. number.Scale = i;
  1208. byte* dst = number.GetDigitsPointer();
  1209. while (--i >= 0)
  1210. *dst++ = *p++;
  1211. *dst = (byte)('\0');
  1212. number.CheckConsistency();
  1213. }
  1214. private static unsafe string UInt64ToDecStr(ulong value, int digits)
  1215. {
  1216. if (digits < 1)
  1217. digits = 1;
  1218. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
  1219. // For single-digit values that are very common, especially 0 and 1, just return cached strings.
  1220. if (bufferLength == 1)
  1221. {
  1222. return s_singleDigitStringCache[value];
  1223. }
  1224. string result = string.FastAllocateString(bufferLength);
  1225. fixed (char* buffer = result)
  1226. {
  1227. char* p = buffer + bufferLength;
  1228. while (High32(value) != 0)
  1229. {
  1230. p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
  1231. digits -= 9;
  1232. }
  1233. p = UInt32ToDecChars(p, Low32(value), digits);
  1234. Debug.Assert(p == buffer);
  1235. }
  1236. return result;
  1237. }
  1238. private static unsafe bool TryUInt64ToDecStr(ulong value, int digits, Span<char> destination, out int charsWritten)
  1239. {
  1240. if (digits < 1)
  1241. digits = 1;
  1242. int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
  1243. if (bufferLength > destination.Length)
  1244. {
  1245. charsWritten = 0;
  1246. return false;
  1247. }
  1248. charsWritten = bufferLength;
  1249. fixed (char* buffer = &MemoryMarshal.GetReference(destination))
  1250. {
  1251. char* p = buffer + bufferLength;
  1252. while (High32(value) != 0)
  1253. {
  1254. p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
  1255. digits -= 9;
  1256. }
  1257. p = UInt32ToDecChars(p, Low32(value), digits);
  1258. Debug.Assert(p == buffer);
  1259. }
  1260. return true;
  1261. }
  1262. internal static unsafe char ParseFormatSpecifier(ReadOnlySpan<char> format, out int digits)
  1263. {
  1264. char c = default;
  1265. if (format.Length > 0)
  1266. {
  1267. // If the format begins with a symbol, see if it's a standard format
  1268. // with or without a specified number of digits.
  1269. c = format[0];
  1270. if ((uint)(c - 'A') <= 'Z' - 'A' ||
  1271. (uint)(c - 'a') <= 'z' - 'a')
  1272. {
  1273. // Fast path for sole symbol, e.g. "D"
  1274. if (format.Length == 1)
  1275. {
  1276. digits = -1;
  1277. return c;
  1278. }
  1279. if (format.Length == 2)
  1280. {
  1281. // Fast path for symbol and single digit, e.g. "X4"
  1282. int d = format[1] - '0';
  1283. if ((uint)d < 10)
  1284. {
  1285. digits = d;
  1286. return c;
  1287. }
  1288. }
  1289. else if (format.Length == 3)
  1290. {
  1291. // Fast path for symbol and double digit, e.g. "F12"
  1292. int d1 = format[1] - '0', d2 = format[2] - '0';
  1293. if ((uint)d1 < 10 && (uint)d2 < 10)
  1294. {
  1295. digits = d1 * 10 + d2;
  1296. return c;
  1297. }
  1298. }
  1299. // Fallback for symbol and any length digits. The digits value must be >= 0 && <= 99,
  1300. // but it can begin with any number of 0s, and thus we may need to check more than two
  1301. // digits. Further, for compat, we need to stop when we hit a null char.
  1302. int n = 0;
  1303. int i = 1;
  1304. while (i < format.Length && (((uint)format[i] - '0') < 10) && n < 10)
  1305. {
  1306. n = (n * 10) + format[i++] - '0';
  1307. }
  1308. // If we're at the end of the digits rather than having stopped because we hit something
  1309. // other than a digit or overflowed, return the standard format info.
  1310. if (i == format.Length || format[i] == '\0')
  1311. {
  1312. digits = n;
  1313. return c;
  1314. }
  1315. }
  1316. }
  1317. // Default empty format to be "G"; custom format is signified with '\0'.
  1318. digits = -1;
  1319. return format.Length == 0 || c == '\0' ? // For compat, treat '\0' as the end of the specifier, even if the specifier extends beyond it.
  1320. 'G' :
  1321. '\0';
  1322. }
  1323. internal static unsafe void NumberToString(ref ValueStringBuilder sb, ref NumberBuffer number, char format, int nMaxDigits, NumberFormatInfo info)
  1324. {
  1325. number.CheckConsistency();
  1326. switch (format)
  1327. {
  1328. case 'C':
  1329. case 'c':
  1330. {
  1331. if (nMaxDigits < 0)
  1332. nMaxDigits = info.CurrencyDecimalDigits;
  1333. RoundNumber(ref number, number.Scale + nMaxDigits); // Don't change this line to use digPos since digCount could have its sign changed.
  1334. FormatCurrency(ref sb, ref number, nMaxDigits, info);
  1335. break;
  1336. }
  1337. case 'F':
  1338. case 'f':
  1339. {
  1340. if (nMaxDigits < 0)
  1341. nMaxDigits = info.NumberDecimalDigits;
  1342. RoundNumber(ref number, number.Scale + nMaxDigits);
  1343. if (number.IsNegative)
  1344. sb.Append(info.NegativeSign);
  1345. FormatFixed(ref sb, ref number, nMaxDigits, info, null, info.NumberDecimalSeparator, null);
  1346. break;
  1347. }
  1348. case 'N':
  1349. case 'n':
  1350. {
  1351. if (nMaxDigits < 0)
  1352. nMaxDigits = info.NumberDecimalDigits; // Since we are using digits in our calculation
  1353. RoundNumber(ref number, number.Scale + nMaxDigits);
  1354. FormatNumber(ref sb, ref number, nMaxDigits, info);
  1355. break;
  1356. }
  1357. case 'E':
  1358. case 'e':
  1359. {
  1360. if (nMaxDigits < 0)
  1361. nMaxDigits = 6;
  1362. nMaxDigits++;
  1363. RoundNumber(ref number, nMaxDigits);
  1364. if (number.IsNegative)
  1365. sb.Append(info.NegativeSign);
  1366. FormatScientific(ref sb, ref number, nMaxDigits, info, format);
  1367. break;
  1368. }
  1369. case 'G':
  1370. case 'g':
  1371. {
  1372. bool noRounding = false;
  1373. if (nMaxDigits < 1)
  1374. {
  1375. if ((number.Kind == NumberBufferKind.Decimal) && (nMaxDigits == -1))
  1376. {
  1377. noRounding = true; // Turn off rounding for ECMA compliance to output trailing 0's after decimal as significant
  1378. if (number.Digits[0] == 0)
  1379. {
  1380. // -0 should be formatted as 0 for decimal. This is normally handled by RoundNumber (which we are skipping)
  1381. goto SkipSign;
  1382. }
  1383. goto SkipRounding;
  1384. }
  1385. else
  1386. {
  1387. // This ensures that the PAL code pads out to the correct place even when we use the default precision
  1388. nMaxDigits = number.DigitsCount;
  1389. }
  1390. }
  1391. RoundNumber(ref number, nMaxDigits);
  1392. SkipRounding:
  1393. if (number.IsNegative)
  1394. sb.Append(info.NegativeSign);
  1395. SkipSign:
  1396. FormatGeneral(ref sb, ref number, nMaxDigits, info, (char)(format - ('G' - 'E')), noRounding);
  1397. break;
  1398. }
  1399. case 'P':
  1400. case 'p':
  1401. {
  1402. if (nMaxDigits < 0)
  1403. nMaxDigits = info.PercentDecimalDigits;
  1404. number.Scale += 2;
  1405. RoundNumber(ref number, number.Scale + nMaxDigits);
  1406. FormatPercent(ref sb, ref number, nMaxDigits, info);
  1407. break;
  1408. }
  1409. default:
  1410. throw new FormatException(SR.Argument_BadFormatSpecifier);
  1411. }
  1412. }
  1413. internal static unsafe void NumberToStringFormat(ref ValueStringBuilder sb, ref NumberBuffer number, ReadOnlySpan<char> format, NumberFormatInfo info)
  1414. {
  1415. number.CheckConsistency();
  1416. int digitCount;
  1417. int decimalPos;
  1418. int firstDigit;
  1419. int lastDigit;
  1420. int digPos;
  1421. bool scientific;
  1422. int thousandPos;
  1423. int thousandCount = 0;
  1424. bool thousandSeps;
  1425. int scaleAdjust;
  1426. int adjust;
  1427. int section;
  1428. int src;
  1429. byte* dig = number.GetDigitsPointer();
  1430. char ch;
  1431. section = FindSection(format, dig[0] == 0 ? 2 : number.IsNegative ? 1 : 0);
  1432. while (true)
  1433. {
  1434. digitCount = 0;
  1435. decimalPos = -1;
  1436. firstDigit = 0x7FFFFFFF;
  1437. lastDigit = 0;
  1438. scientific = false;
  1439. thousandPos = -1;
  1440. thousandSeps = false;
  1441. scaleAdjust = 0;
  1442. src = section;
  1443. fixed (char* pFormat = &MemoryMarshal.GetReference(format))
  1444. {
  1445. while (src < format.Length && (ch = pFormat[src++]) != 0 && ch != ';')
  1446. {
  1447. switch (ch)
  1448. {
  1449. case '#':
  1450. digitCount++;
  1451. break;
  1452. case '0':
  1453. if (firstDigit == 0x7FFFFFFF)
  1454. firstDigit = digitCount;
  1455. digitCount++;
  1456. lastDigit = digitCount;
  1457. break;
  1458. case '.':
  1459. if (decimalPos < 0)
  1460. decimalPos = digitCount;
  1461. break;
  1462. case ',':
  1463. if (digitCount > 0 && decimalPos < 0)
  1464. {
  1465. if (thousandPos >= 0)
  1466. {
  1467. if (thousandPos == digitCount)
  1468. {
  1469. thousandCount++;
  1470. break;
  1471. }
  1472. thousandSeps = true;
  1473. }
  1474. thousandPos = digitCount;
  1475. thousandCount = 1;
  1476. }
  1477. break;
  1478. case '%':
  1479. scaleAdjust += 2;
  1480. break;
  1481. case '\x2030':
  1482. scaleAdjust += 3;
  1483. break;
  1484. case '\'':
  1485. case '"':
  1486. while (src < format.Length && pFormat[src] != 0 && pFormat[src++] != ch)
  1487. ;
  1488. break;
  1489. case '\\':
  1490. if (src < format.Length && pFormat[src] != 0)
  1491. src++;
  1492. break;
  1493. case 'E':
  1494. case 'e':
  1495. if ((src < format.Length && pFormat[src] == '0') ||
  1496. (src + 1 < format.Length && (pFormat[src] == '+' || pFormat[src] == '-') && pFormat[src + 1] == '0'))
  1497. {
  1498. while (++src < format.Length && pFormat[src] == '0')
  1499. ;
  1500. scientific = true;
  1501. }
  1502. break;
  1503. }
  1504. }
  1505. }
  1506. if (decimalPos < 0)
  1507. decimalPos = digitCount;
  1508. if (thousandPos >= 0)
  1509. {
  1510. if (thousandPos == decimalPos)
  1511. scaleAdjust -= thousandCount * 3;
  1512. else
  1513. thousandSeps = true;
  1514. }
  1515. if (dig[0] != 0)
  1516. {
  1517. number.Scale += scaleAdjust;
  1518. int pos = scientific ? digitCount : number.Scale + digitCount - decimalPos;
  1519. RoundNumber(ref number, pos);
  1520. if (dig[0] == 0)
  1521. {
  1522. src = FindSection(format, 2);
  1523. if (src != section)
  1524. {
  1525. section = src;
  1526. continue;
  1527. }
  1528. }
  1529. }
  1530. else
  1531. {
  1532. if (number.Kind != NumberBufferKind.FloatingPoint)
  1533. {
  1534. // The integer types don't have a concept of -0 and decimal always format -0 as 0
  1535. number.IsNegative = false;
  1536. }
  1537. number.Scale = 0; // Decimals with scale ('0.00') should be rounded.
  1538. }
  1539. break;
  1540. }
  1541. firstDigit = firstDigit < decimalPos ? decimalPos - firstDigit : 0;
  1542. lastDigit = lastDigit > decimalPos ? decimalPos - lastDigit : 0;
  1543. if (scientific)
  1544. {
  1545. digPos = decimalPos;
  1546. adjust = 0;
  1547. }
  1548. else
  1549. {
  1550. digPos = number.Scale > decimalPos ? number.Scale : decimalPos;
  1551. adjust = number.Scale - decimalPos;
  1552. }
  1553. src = section;
  1554. // Adjust can be negative, so we make this an int instead of an unsigned int.
  1555. // Adjust represents the number of characters over the formatting e.g. format string is "0000" and you are trying to
  1556. // format 100000 (6 digits). Means adjust will be 2. On the other hand if you are trying to format 10 adjust will be
  1557. // -2 and we'll need to fixup these digits with 0 padding if we have 0 formatting as in this example.
  1558. Span<int> thousandsSepPos = stackalloc int[4];
  1559. int thousandsSepCtr = -1;
  1560. if (thousandSeps)
  1561. {
  1562. // We need to precompute this outside the number formatting loop
  1563. if (info.NumberGroupSeparator.Length > 0)
  1564. {
  1565. // We need this array to figure out where to insert the thousands separator. We would have to traverse the string
  1566. // backwards. PIC formatting always traverses forwards. These indices are precomputed to tell us where to insert
  1567. // the thousands separator so we can get away with traversing forwards. Note we only have to compute up to digPos.
  1568. // The max is not bound since you can have formatting strings of the form "000,000..", and this
  1569. // should handle that case too.
  1570. int[] groupDigits = info.numberGroupSizes;
  1571. int groupSizeIndex = 0; // Index into the groupDigits array.
  1572. int groupTotalSizeCount = 0;
  1573. int groupSizeLen = groupDigits.Length; // The length of groupDigits array.
  1574. if (groupSizeLen != 0)
  1575. groupTotalSizeCount = groupDigits[groupSizeIndex]; // The current running total of group size.
  1576. int groupSize = groupTotalSizeCount;
  1577. int totalDigits = digPos + ((adjust < 0) ? adjust : 0); // Actual number of digits in o/p
  1578. int numDigits = (firstDigit > totalDigits) ? firstDigit : totalDigits;
  1579. while (numDigits > groupTotalSizeCount)
  1580. {
  1581. if (groupSize == 0)
  1582. break;
  1583. ++thousandsSepCtr;
  1584. if (thousandsSepCtr >= thousandsSepPos.Length)
  1585. {
  1586. var newThousandsSepPos = new int[thousandsSepPos.Length * 2];
  1587. thousandsSepPos.CopyTo(newThousandsSepPos);
  1588. thousandsSepPos = newThousandsSepPos;
  1589. }
  1590. thousandsSepPos[thousandsSepCtr] = groupTotalSizeCount;
  1591. if (groupSizeIndex < groupSizeLen - 1)
  1592. {
  1593. groupSizeIndex++;
  1594. groupSize = groupDigits[groupSizeIndex];
  1595. }
  1596. groupTotalSizeCount += groupSize;
  1597. }
  1598. }
  1599. }
  1600. if (number.IsNegative && (section == 0) && (number.Scale != 0))
  1601. sb.Append(info.NegativeSign);
  1602. bool decimalWritten = false;
  1603. fixed (char* pFormat = &MemoryMarshal.GetReference(format))
  1604. {
  1605. byte* cur = dig;
  1606. while (src < format.Length && (ch = pFormat[src++]) != 0 && ch != ';')
  1607. {
  1608. if (adjust > 0)
  1609. {
  1610. switch (ch)
  1611. {
  1612. case '#':
  1613. case '0':
  1614. case '.':
  1615. while (adjust > 0)
  1616. {
  1617. // digPos will be one greater than thousandsSepPos[thousandsSepCtr] since we are at
  1618. // the character after which the groupSeparator needs to be appended.
  1619. sb.Append(*cur != 0 ? (char)(*cur++) : '0');
  1620. if (thousandSeps && digPos > 1 && thousandsSepCtr >= 0)
  1621. {
  1622. if (digPos == thousandsSepPos[thousandsSepCtr] + 1)
  1623. {
  1624. sb.Append(info.NumberGroupSeparator);
  1625. thousandsSepCtr--;
  1626. }
  1627. }
  1628. digPos--;
  1629. adjust--;
  1630. }
  1631. break;
  1632. }
  1633. }
  1634. switch (ch)
  1635. {
  1636. case '#':
  1637. case '0':
  1638. {
  1639. if (adjust < 0)
  1640. {
  1641. adjust++;
  1642. ch = digPos <= firstDigit ? '0' : '\0';
  1643. }
  1644. else
  1645. {
  1646. ch = *cur != 0 ? (char)(*cur++) : digPos > lastDigit ? '0' : '\0';
  1647. }
  1648. if (ch != 0)
  1649. {
  1650. sb.Append(ch);
  1651. if (thousandSeps && digPos > 1 && thousandsSepCtr >= 0)
  1652. {
  1653. if (digPos == thousandsSepPos[thousandsSepCtr] + 1)
  1654. {
  1655. sb.Append(info.NumberGroupSeparator);
  1656. thousandsSepCtr--;
  1657. }
  1658. }
  1659. }
  1660. digPos--;
  1661. break;
  1662. }
  1663. case '.':
  1664. {
  1665. if (digPos != 0 || decimalWritten)
  1666. {
  1667. // For compatibility, don't echo repeated decimals
  1668. break;
  1669. }
  1670. // If the format has trailing zeros or the format has a decimal and digits remain
  1671. if (lastDigit < 0 || (decimalPos < digitCount && *cur != 0))
  1672. {
  1673. sb.Append(info.NumberDecimalSeparator);
  1674. decimalWritten = true;
  1675. }
  1676. break;
  1677. }
  1678. case '\x2030':
  1679. sb.Append(info.PerMilleSymbol);
  1680. break;
  1681. case '%':
  1682. sb.Append(info.PercentSymbol);
  1683. break;
  1684. case ',':
  1685. break;
  1686. case '\'':
  1687. case '"':
  1688. while (src < format.Length && pFormat[src] != 0 && pFormat[src] != ch)
  1689. sb.Append(pFormat[src++]);
  1690. if (src < format.Length && pFormat[src] != 0)
  1691. src++;
  1692. break;
  1693. case '\\':
  1694. if (src < format.Length && pFormat[src] != 0)
  1695. sb.Append(pFormat[src++]);
  1696. break;
  1697. case 'E':
  1698. case 'e':
  1699. {
  1700. bool positiveSign = false;
  1701. int i = 0;
  1702. if (scientific)
  1703. {
  1704. if (src < format.Length && pFormat[src] == '0')
  1705. {
  1706. // Handles E0, which should format the same as E-0
  1707. i++;
  1708. }
  1709. else if (src + 1 < format.Length && pFormat[src] == '+' && pFormat[src + 1] == '0')
  1710. {
  1711. // Handles E+0
  1712. positiveSign = true;
  1713. }
  1714. else if (src + 1 < format.Length && pFormat[src] == '-' && pFormat[src + 1] == '0')
  1715. {
  1716. // Handles E-0
  1717. // Do nothing, this is just a place holder s.t. we don't break out of the loop.
  1718. }
  1719. else
  1720. {
  1721. sb.Append(ch);
  1722. break;
  1723. }
  1724. while (++src < format.Length && pFormat[src] == '0')
  1725. i++;
  1726. if (i > 10)
  1727. i = 10;
  1728. int exp = dig[0] == 0 ? 0 : number.Scale - decimalPos;
  1729. FormatExponent(ref sb, info, exp, ch, i, positiveSign);
  1730. scientific = false;
  1731. }
  1732. else
  1733. {
  1734. sb.Append(ch); // Copy E or e to output
  1735. if (src < format.Length)
  1736. {
  1737. if (pFormat[src] == '+' || pFormat[src] == '-')
  1738. sb.Append(pFormat[src++]);
  1739. while (src < format.Length && pFormat[src] == '0')
  1740. sb.Append(pFormat[src++]);
  1741. }
  1742. }
  1743. break;
  1744. }
  1745. default:
  1746. sb.Append(ch);
  1747. break;
  1748. }
  1749. }
  1750. }
  1751. if (number.IsNegative && (section == 0) && (number.Scale == 0) && (sb.Length > 0))
  1752. sb.Insert(0, info.NegativeSign);
  1753. }
  1754. private static void FormatCurrency(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info)
  1755. {
  1756. string fmt = number.IsNegative ?
  1757. s_negCurrencyFormats[info.CurrencyNegativePattern] :
  1758. s_posCurrencyFormats[info.CurrencyPositivePattern];
  1759. foreach (char ch in fmt)
  1760. {
  1761. switch (ch)
  1762. {
  1763. case '#':
  1764. FormatFixed(ref sb, ref number, nMaxDigits, info, info.currencyGroupSizes, info.CurrencyDecimalSeparator, info.CurrencyGroupSeparator);
  1765. break;
  1766. case '-':
  1767. sb.Append(info.NegativeSign);
  1768. break;
  1769. case '$':
  1770. sb.Append(info.CurrencySymbol);
  1771. break;
  1772. default:
  1773. sb.Append(ch);
  1774. break;
  1775. }
  1776. }
  1777. }
  1778. private static unsafe void FormatFixed(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info, int[] groupDigits, string sDecimal, string sGroup)
  1779. {
  1780. int digPos = number.Scale;
  1781. byte* dig = number.GetDigitsPointer();
  1782. if (digPos > 0)
  1783. {
  1784. if (groupDigits != null)
  1785. {
  1786. int groupSizeIndex = 0; // Index into the groupDigits array.
  1787. int bufferSize = digPos; // The length of the result buffer string.
  1788. int groupSize = 0; // The current group size.
  1789. // Find out the size of the string buffer for the result.
  1790. if (groupDigits.Length != 0) // You can pass in 0 length arrays
  1791. {
  1792. int groupSizeCount = groupDigits[groupSizeIndex]; // The current total of group size.
  1793. while (digPos > groupSizeCount)
  1794. {
  1795. groupSize = groupDigits[groupSizeIndex];
  1796. if (groupSize == 0)
  1797. break;
  1798. bufferSize += sGroup.Length;
  1799. if (groupSizeIndex < groupDigits.Length - 1)
  1800. groupSizeIndex++;
  1801. groupSizeCount += groupDigits[groupSizeIndex];
  1802. if (groupSizeCount < 0 || bufferSize < 0)
  1803. throw new ArgumentOutOfRangeException(); // If we overflow
  1804. }
  1805. groupSize = groupSizeCount == 0 ? 0 : groupDigits[0]; // If you passed in an array with one entry as 0, groupSizeCount == 0
  1806. }
  1807. groupSizeIndex = 0;
  1808. int digitCount = 0;
  1809. int digLength = number.DigitsCount;
  1810. int digStart = (digPos < digLength) ? digPos : digLength;
  1811. fixed (char* spanPtr = &MemoryMarshal.GetReference(sb.AppendSpan(bufferSize)))
  1812. {
  1813. char* p = spanPtr + bufferSize - 1;
  1814. for (int i = digPos - 1; i >= 0; i--)
  1815. {
  1816. *(p--) = (i < digStart) ? (char)(dig[i]) : '0';
  1817. if (groupSize > 0)
  1818. {
  1819. digitCount++;
  1820. if ((digitCount == groupSize) && (i != 0))
  1821. {
  1822. for (int j = sGroup.Length - 1; j >= 0; j--)
  1823. *(p--) = sGroup[j];
  1824. if (groupSizeIndex < groupDigits.Length - 1)
  1825. {
  1826. groupSizeIndex++;
  1827. groupSize = groupDigits[groupSizeIndex];
  1828. }
  1829. digitCount = 0;
  1830. }
  1831. }
  1832. }
  1833. Debug.Assert(p >= spanPtr - 1, "Underflow");
  1834. dig += digStart;
  1835. }
  1836. }
  1837. else
  1838. {
  1839. do
  1840. {
  1841. sb.Append(*dig != 0 ? (char)(*dig++) : '0');
  1842. }
  1843. while (--digPos > 0);
  1844. }
  1845. }
  1846. else
  1847. {
  1848. sb.Append('0');
  1849. }
  1850. if (nMaxDigits > 0)
  1851. {
  1852. sb.Append(sDecimal);
  1853. if ((digPos < 0) && (nMaxDigits > 0))
  1854. {
  1855. int zeroes = Math.Min(-digPos, nMaxDigits);
  1856. sb.Append('0', zeroes);
  1857. digPos += zeroes;
  1858. nMaxDigits -= zeroes;
  1859. }
  1860. while (nMaxDigits > 0)
  1861. {
  1862. sb.Append((*dig != 0) ? (char)(*dig++) : '0');
  1863. nMaxDigits--;
  1864. }
  1865. }
  1866. }
  1867. private static void FormatNumber(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info)
  1868. {
  1869. string fmt = number.IsNegative ?
  1870. s_negNumberFormats[info.NumberNegativePattern] :
  1871. PosNumberFormat;
  1872. foreach (char ch in fmt)
  1873. {
  1874. switch (ch)
  1875. {
  1876. case '#':
  1877. FormatFixed(ref sb, ref number, nMaxDigits, info, info.numberGroupSizes, info.NumberDecimalSeparator, info.NumberGroupSeparator);
  1878. break;
  1879. case '-':
  1880. sb.Append(info.NegativeSign);
  1881. break;
  1882. default:
  1883. sb.Append(ch);
  1884. break;
  1885. }
  1886. }
  1887. }
  1888. private static unsafe void FormatScientific(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info, char expChar)
  1889. {
  1890. byte* dig = number.GetDigitsPointer();
  1891. sb.Append((*dig != 0) ? (char)(*dig++) : '0');
  1892. if (nMaxDigits != 1) // For E0 we would like to suppress the decimal point
  1893. sb.Append(info.NumberDecimalSeparator);
  1894. while (--nMaxDigits > 0)
  1895. sb.Append((*dig != 0) ? (char)(*dig++) : '0');
  1896. int e = number.Digits[0] == 0 ? 0 : number.Scale - 1;
  1897. FormatExponent(ref sb, info, e, expChar, 3, true);
  1898. }
  1899. private static unsafe void FormatExponent(ref ValueStringBuilder sb, NumberFormatInfo info, int value, char expChar, int minDigits, bool positiveSign)
  1900. {
  1901. sb.Append(expChar);
  1902. if (value < 0)
  1903. {
  1904. sb.Append(info.NegativeSign);
  1905. value = -value;
  1906. }
  1907. else
  1908. {
  1909. if (positiveSign)
  1910. sb.Append(info.PositiveSign);
  1911. }
  1912. char* digits = stackalloc char[MaxUInt32DecDigits];
  1913. char* p = UInt32ToDecChars(digits + MaxUInt32DecDigits, (uint)value, minDigits);
  1914. int i = (int)(digits + MaxUInt32DecDigits - p);
  1915. sb.Append(p, (int)(digits + MaxUInt32DecDigits - p));
  1916. }
  1917. private static unsafe void FormatGeneral(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info, char expChar, bool bSuppressScientific)
  1918. {
  1919. int digPos = number.Scale;
  1920. bool scientific = false;
  1921. if (!bSuppressScientific)
  1922. {
  1923. // Don't switch to scientific notation
  1924. if (digPos > nMaxDigits || digPos < -3)
  1925. {
  1926. digPos = 1;
  1927. scientific = true;
  1928. }
  1929. }
  1930. byte* dig = number.GetDigitsPointer();
  1931. if (digPos > 0)
  1932. {
  1933. do
  1934. {
  1935. sb.Append((*dig != 0) ? (char)(*dig++) : '0');
  1936. } while (--digPos > 0);
  1937. }
  1938. else
  1939. {
  1940. sb.Append('0');
  1941. }
  1942. if (*dig != 0 || digPos < 0)
  1943. {
  1944. sb.Append(info.NumberDecimalSeparator);
  1945. while (digPos < 0)
  1946. {
  1947. sb.Append('0');
  1948. digPos++;
  1949. }
  1950. while (*dig != 0)
  1951. sb.Append((char)(*dig++));
  1952. }
  1953. if (scientific)
  1954. FormatExponent(ref sb, info, number.Scale - 1, expChar, 2, true);
  1955. }
  1956. private static void FormatPercent(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info)
  1957. {
  1958. string fmt = number.IsNegative ?
  1959. s_negPercentFormats[info.PercentNegativePattern] :
  1960. s_posPercentFormats[info.PercentPositivePattern];
  1961. foreach (char ch in fmt)
  1962. {
  1963. switch (ch)
  1964. {
  1965. case '#':
  1966. FormatFixed(ref sb, ref number, nMaxDigits, info, info.percentGroupSizes, info.PercentDecimalSeparator, info.PercentGroupSeparator);
  1967. break;
  1968. case '-':
  1969. sb.Append(info.NegativeSign);
  1970. break;
  1971. case '%':
  1972. sb.Append(info.PercentSymbol);
  1973. break;
  1974. default:
  1975. sb.Append(ch);
  1976. break;
  1977. }
  1978. }
  1979. }
  1980. internal static unsafe void RoundNumber(ref NumberBuffer number, int pos)
  1981. {
  1982. byte* dig = number.GetDigitsPointer();
  1983. int i = 0;
  1984. while (i < pos && dig[i] != 0)
  1985. i++;
  1986. if (i == pos && dig[i] >= '5')
  1987. {
  1988. while (i > 0 && dig[i - 1] == '9')
  1989. i--;
  1990. if (i > 0)
  1991. {
  1992. dig[i - 1]++;
  1993. }
  1994. else
  1995. {
  1996. number.Scale++;
  1997. dig[0] = (byte)('1');
  1998. i = 1;
  1999. }
  2000. }
  2001. else
  2002. {
  2003. while (i > 0 && dig[i - 1] == '0')
  2004. i--;
  2005. }
  2006. if (i == 0)
  2007. {
  2008. if (number.Kind != NumberBufferKind.FloatingPoint)
  2009. {
  2010. // The integer types don't have a concept of -0 and decimal always format -0 as 0
  2011. number.IsNegative = false;
  2012. }
  2013. number.Scale = 0; // Decimals with scale ('0.00') should be rounded.
  2014. }
  2015. dig[i] = (byte)('\0');
  2016. number.DigitsCount = i;
  2017. number.CheckConsistency();
  2018. }
  2019. private static unsafe int FindSection(ReadOnlySpan<char> format, int section)
  2020. {
  2021. int src;
  2022. char ch;
  2023. if (section == 0)
  2024. return 0;
  2025. fixed (char* pFormat = &MemoryMarshal.GetReference(format))
  2026. {
  2027. src = 0;
  2028. for (; ; )
  2029. {
  2030. if (src >= format.Length)
  2031. {
  2032. return 0;
  2033. }
  2034. switch (ch = pFormat[src++])
  2035. {
  2036. case '\'':
  2037. case '"':
  2038. while (src < format.Length && pFormat[src] != 0 && pFormat[src++] != ch)
  2039. ;
  2040. break;
  2041. case '\\':
  2042. if (src < format.Length && pFormat[src] != 0)
  2043. src++;
  2044. break;
  2045. case ';':
  2046. if (--section != 0)
  2047. break;
  2048. if (src < format.Length && pFormat[src] != 0 && pFormat[src] != ';')
  2049. return src;
  2050. goto case '\0';
  2051. case '\0':
  2052. return 0;
  2053. }
  2054. }
  2055. }
  2056. }
  2057. private static uint Low32(ulong value) => (uint)value;
  2058. private static uint High32(ulong value) => (uint)((value & 0xFFFFFFFF00000000) >> 32);
  2059. private static uint Int64DivMod1E9(ref ulong value)
  2060. {
  2061. uint rem = (uint)(value % 1000000000);
  2062. value /= 1000000000;
  2063. return rem;
  2064. }
  2065. private static unsafe void DoubleToNumber(double value, int precision, ref NumberBuffer number)
  2066. {
  2067. if (!double.IsFinite(value))
  2068. {
  2069. number.Scale = double.IsNaN(value) ? ScaleNAN : ScaleINF;
  2070. number.IsNegative = double.IsNegative(value);
  2071. number.Digits[0] = (byte)('\0');
  2072. }
  2073. else if (value == 0.0)
  2074. {
  2075. number.Scale = 0;
  2076. number.IsNegative = double.IsNegative(value);
  2077. number.Digits[0] = (byte)('\0');
  2078. }
  2079. else
  2080. {
  2081. number.DigitsCount = precision;
  2082. if (!Grisu3.Run(value, precision, ref number))
  2083. {
  2084. Dragon4(value, precision, ref number);
  2085. }
  2086. }
  2087. number.CheckConsistency();
  2088. }
  2089. private static long ExtractFractionAndBiasedExponent(double value, out int exponent)
  2090. {
  2091. var bits = BitConverter.DoubleToInt64Bits(value);
  2092. long fraction = (bits & 0xFFFFFFFFFFFFF);
  2093. exponent = (int)((bits >> 52) & 0x7FF);
  2094. if (exponent != 0)
  2095. {
  2096. // For normalized value, according to https://en.wikipedia.org/wiki/Double-precision_floating-point_format
  2097. // value = 1.fraction * 2^(exp - 1023)
  2098. // = (1 + mantissa / 2^52) * 2^(exp - 1023)
  2099. // = (2^52 + mantissa) * 2^(exp - 1023 - 52)
  2100. //
  2101. // So f = (2^52 + mantissa), e = exp - 1075;
  2102. fraction |= ((long)(1) << 52);
  2103. exponent -= 1075;
  2104. }
  2105. else
  2106. {
  2107. // For denormalized value, according to https://en.wikipedia.org/wiki/Double-precision_floating-point_format
  2108. // value = 0.fraction * 2^(1 - 1023)
  2109. // = (mantissa / 2^52) * 2^(-1022)
  2110. // = mantissa * 2^(-1022 - 52)
  2111. // = mantissa * 2^(-1074)
  2112. // So f = mantissa, e = -1074
  2113. exponent = -1074;
  2114. }
  2115. return fraction;
  2116. }
  2117. }
  2118. }