Number.Formatting.cs 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397
  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. goto SkipRounding;
  1379. }
  1380. else
  1381. {
  1382. // This ensures that the PAL code pads out to the correct place even when we use the default precision
  1383. nMaxDigits = number.DigitsCount;
  1384. }
  1385. }
  1386. RoundNumber(ref number, nMaxDigits);
  1387. SkipRounding:
  1388. if (number.IsNegative)
  1389. sb.Append(info.NegativeSign);
  1390. FormatGeneral(ref sb, ref number, nMaxDigits, info, (char)(format - ('G' - 'E')), noRounding);
  1391. break;
  1392. }
  1393. case 'P':
  1394. case 'p':
  1395. {
  1396. if (nMaxDigits < 0)
  1397. nMaxDigits = info.PercentDecimalDigits;
  1398. number.Scale += 2;
  1399. RoundNumber(ref number, number.Scale + nMaxDigits);
  1400. FormatPercent(ref sb, ref number, nMaxDigits, info);
  1401. break;
  1402. }
  1403. default:
  1404. throw new FormatException(SR.Argument_BadFormatSpecifier);
  1405. }
  1406. }
  1407. internal static unsafe void NumberToStringFormat(ref ValueStringBuilder sb, ref NumberBuffer number, ReadOnlySpan<char> format, NumberFormatInfo info)
  1408. {
  1409. number.CheckConsistency();
  1410. int digitCount;
  1411. int decimalPos;
  1412. int firstDigit;
  1413. int lastDigit;
  1414. int digPos;
  1415. bool scientific;
  1416. int thousandPos;
  1417. int thousandCount = 0;
  1418. bool thousandSeps;
  1419. int scaleAdjust;
  1420. int adjust;
  1421. int section;
  1422. int src;
  1423. byte* dig = number.GetDigitsPointer();
  1424. char ch;
  1425. section = FindSection(format, dig[0] == 0 ? 2 : number.IsNegative ? 1 : 0);
  1426. while (true)
  1427. {
  1428. digitCount = 0;
  1429. decimalPos = -1;
  1430. firstDigit = 0x7FFFFFFF;
  1431. lastDigit = 0;
  1432. scientific = false;
  1433. thousandPos = -1;
  1434. thousandSeps = false;
  1435. scaleAdjust = 0;
  1436. src = section;
  1437. fixed (char* pFormat = &MemoryMarshal.GetReference(format))
  1438. {
  1439. while (src < format.Length && (ch = pFormat[src++]) != 0 && ch != ';')
  1440. {
  1441. switch (ch)
  1442. {
  1443. case '#':
  1444. digitCount++;
  1445. break;
  1446. case '0':
  1447. if (firstDigit == 0x7FFFFFFF)
  1448. firstDigit = digitCount;
  1449. digitCount++;
  1450. lastDigit = digitCount;
  1451. break;
  1452. case '.':
  1453. if (decimalPos < 0)
  1454. decimalPos = digitCount;
  1455. break;
  1456. case ',':
  1457. if (digitCount > 0 && decimalPos < 0)
  1458. {
  1459. if (thousandPos >= 0)
  1460. {
  1461. if (thousandPos == digitCount)
  1462. {
  1463. thousandCount++;
  1464. break;
  1465. }
  1466. thousandSeps = true;
  1467. }
  1468. thousandPos = digitCount;
  1469. thousandCount = 1;
  1470. }
  1471. break;
  1472. case '%':
  1473. scaleAdjust += 2;
  1474. break;
  1475. case '\x2030':
  1476. scaleAdjust += 3;
  1477. break;
  1478. case '\'':
  1479. case '"':
  1480. while (src < format.Length && pFormat[src] != 0 && pFormat[src++] != ch)
  1481. ;
  1482. break;
  1483. case '\\':
  1484. if (src < format.Length && pFormat[src] != 0)
  1485. src++;
  1486. break;
  1487. case 'E':
  1488. case 'e':
  1489. if ((src < format.Length && pFormat[src] == '0') ||
  1490. (src + 1 < format.Length && (pFormat[src] == '+' || pFormat[src] == '-') && pFormat[src + 1] == '0'))
  1491. {
  1492. while (++src < format.Length && pFormat[src] == '0')
  1493. ;
  1494. scientific = true;
  1495. }
  1496. break;
  1497. }
  1498. }
  1499. }
  1500. if (decimalPos < 0)
  1501. decimalPos = digitCount;
  1502. if (thousandPos >= 0)
  1503. {
  1504. if (thousandPos == decimalPos)
  1505. scaleAdjust -= thousandCount * 3;
  1506. else
  1507. thousandSeps = true;
  1508. }
  1509. if (dig[0] != 0)
  1510. {
  1511. number.Scale += scaleAdjust;
  1512. int pos = scientific ? digitCount : number.Scale + digitCount - decimalPos;
  1513. RoundNumber(ref number, pos);
  1514. if (dig[0] == 0)
  1515. {
  1516. src = FindSection(format, 2);
  1517. if (src != section)
  1518. {
  1519. section = src;
  1520. continue;
  1521. }
  1522. }
  1523. }
  1524. else
  1525. {
  1526. number.Scale = 0; // Decimals with scale ('0.00') should be rounded.
  1527. }
  1528. break;
  1529. }
  1530. firstDigit = firstDigit < decimalPos ? decimalPos - firstDigit : 0;
  1531. lastDigit = lastDigit > decimalPos ? decimalPos - lastDigit : 0;
  1532. if (scientific)
  1533. {
  1534. digPos = decimalPos;
  1535. adjust = 0;
  1536. }
  1537. else
  1538. {
  1539. digPos = number.Scale > decimalPos ? number.Scale : decimalPos;
  1540. adjust = number.Scale - decimalPos;
  1541. }
  1542. src = section;
  1543. // Adjust can be negative, so we make this an int instead of an unsigned int.
  1544. // Adjust represents the number of characters over the formatting e.g. format string is "0000" and you are trying to
  1545. // format 100000 (6 digits). Means adjust will be 2. On the other hand if you are trying to format 10 adjust will be
  1546. // -2 and we'll need to fixup these digits with 0 padding if we have 0 formatting as in this example.
  1547. Span<int> thousandsSepPos = stackalloc int[4];
  1548. int thousandsSepCtr = -1;
  1549. if (thousandSeps)
  1550. {
  1551. // We need to precompute this outside the number formatting loop
  1552. if (info.NumberGroupSeparator.Length > 0)
  1553. {
  1554. // We need this array to figure out where to insert the thousands separator. We would have to traverse the string
  1555. // backwards. PIC formatting always traverses forwards. These indices are precomputed to tell us where to insert
  1556. // the thousands separator so we can get away with traversing forwards. Note we only have to compute up to digPos.
  1557. // The max is not bound since you can have formatting strings of the form "000,000..", and this
  1558. // should handle that case too.
  1559. int[] groupDigits = info.numberGroupSizes;
  1560. int groupSizeIndex = 0; // Index into the groupDigits array.
  1561. int groupTotalSizeCount = 0;
  1562. int groupSizeLen = groupDigits.Length; // The length of groupDigits array.
  1563. if (groupSizeLen != 0)
  1564. groupTotalSizeCount = groupDigits[groupSizeIndex]; // The current running total of group size.
  1565. int groupSize = groupTotalSizeCount;
  1566. int totalDigits = digPos + ((adjust < 0) ? adjust : 0); // Actual number of digits in o/p
  1567. int numDigits = (firstDigit > totalDigits) ? firstDigit : totalDigits;
  1568. while (numDigits > groupTotalSizeCount)
  1569. {
  1570. if (groupSize == 0)
  1571. break;
  1572. ++thousandsSepCtr;
  1573. if (thousandsSepCtr >= thousandsSepPos.Length)
  1574. {
  1575. var newThousandsSepPos = new int[thousandsSepPos.Length * 2];
  1576. thousandsSepPos.CopyTo(newThousandsSepPos);
  1577. thousandsSepPos = newThousandsSepPos;
  1578. }
  1579. thousandsSepPos[thousandsSepCtr] = groupTotalSizeCount;
  1580. if (groupSizeIndex < groupSizeLen - 1)
  1581. {
  1582. groupSizeIndex++;
  1583. groupSize = groupDigits[groupSizeIndex];
  1584. }
  1585. groupTotalSizeCount += groupSize;
  1586. }
  1587. }
  1588. }
  1589. if (number.IsNegative && (section == 0) && (number.Scale != 0))
  1590. sb.Append(info.NegativeSign);
  1591. bool decimalWritten = false;
  1592. fixed (char* pFormat = &MemoryMarshal.GetReference(format))
  1593. {
  1594. byte* cur = dig;
  1595. while (src < format.Length && (ch = pFormat[src++]) != 0 && ch != ';')
  1596. {
  1597. if (adjust > 0)
  1598. {
  1599. switch (ch)
  1600. {
  1601. case '#':
  1602. case '0':
  1603. case '.':
  1604. while (adjust > 0)
  1605. {
  1606. // digPos will be one greater than thousandsSepPos[thousandsSepCtr] since we are at
  1607. // the character after which the groupSeparator needs to be appended.
  1608. sb.Append(*cur != 0 ? (char)(*cur++) : '0');
  1609. if (thousandSeps && digPos > 1 && thousandsSepCtr >= 0)
  1610. {
  1611. if (digPos == thousandsSepPos[thousandsSepCtr] + 1)
  1612. {
  1613. sb.Append(info.NumberGroupSeparator);
  1614. thousandsSepCtr--;
  1615. }
  1616. }
  1617. digPos--;
  1618. adjust--;
  1619. }
  1620. break;
  1621. }
  1622. }
  1623. switch (ch)
  1624. {
  1625. case '#':
  1626. case '0':
  1627. {
  1628. if (adjust < 0)
  1629. {
  1630. adjust++;
  1631. ch = digPos <= firstDigit ? '0' : '\0';
  1632. }
  1633. else
  1634. {
  1635. ch = *cur != 0 ? (char)(*cur++) : digPos > lastDigit ? '0' : '\0';
  1636. }
  1637. if (ch != 0)
  1638. {
  1639. sb.Append(ch);
  1640. if (thousandSeps && digPos > 1 && thousandsSepCtr >= 0)
  1641. {
  1642. if (digPos == thousandsSepPos[thousandsSepCtr] + 1)
  1643. {
  1644. sb.Append(info.NumberGroupSeparator);
  1645. thousandsSepCtr--;
  1646. }
  1647. }
  1648. }
  1649. digPos--;
  1650. break;
  1651. }
  1652. case '.':
  1653. {
  1654. if (digPos != 0 || decimalWritten)
  1655. {
  1656. // For compatibility, don't echo repeated decimals
  1657. break;
  1658. }
  1659. // If the format has trailing zeros or the format has a decimal and digits remain
  1660. if (lastDigit < 0 || (decimalPos < digitCount && *cur != 0))
  1661. {
  1662. sb.Append(info.NumberDecimalSeparator);
  1663. decimalWritten = true;
  1664. }
  1665. break;
  1666. }
  1667. case '\x2030':
  1668. sb.Append(info.PerMilleSymbol);
  1669. break;
  1670. case '%':
  1671. sb.Append(info.PercentSymbol);
  1672. break;
  1673. case ',':
  1674. break;
  1675. case '\'':
  1676. case '"':
  1677. while (src < format.Length && pFormat[src] != 0 && pFormat[src] != ch)
  1678. sb.Append(pFormat[src++]);
  1679. if (src < format.Length && pFormat[src] != 0)
  1680. src++;
  1681. break;
  1682. case '\\':
  1683. if (src < format.Length && pFormat[src] != 0)
  1684. sb.Append(pFormat[src++]);
  1685. break;
  1686. case 'E':
  1687. case 'e':
  1688. {
  1689. bool positiveSign = false;
  1690. int i = 0;
  1691. if (scientific)
  1692. {
  1693. if (src < format.Length && pFormat[src] == '0')
  1694. {
  1695. // Handles E0, which should format the same as E-0
  1696. i++;
  1697. }
  1698. else if (src + 1 < format.Length && pFormat[src] == '+' && pFormat[src + 1] == '0')
  1699. {
  1700. // Handles E+0
  1701. positiveSign = true;
  1702. }
  1703. else if (src + 1 < format.Length && pFormat[src] == '-' && pFormat[src + 1] == '0')
  1704. {
  1705. // Handles E-0
  1706. // Do nothing, this is just a place holder s.t. we don't break out of the loop.
  1707. }
  1708. else
  1709. {
  1710. sb.Append(ch);
  1711. break;
  1712. }
  1713. while (++src < format.Length && pFormat[src] == '0')
  1714. i++;
  1715. if (i > 10)
  1716. i = 10;
  1717. int exp = dig[0] == 0 ? 0 : number.Scale - decimalPos;
  1718. FormatExponent(ref sb, info, exp, ch, i, positiveSign);
  1719. scientific = false;
  1720. }
  1721. else
  1722. {
  1723. sb.Append(ch); // Copy E or e to output
  1724. if (src < format.Length)
  1725. {
  1726. if (pFormat[src] == '+' || pFormat[src] == '-')
  1727. sb.Append(pFormat[src++]);
  1728. while (src < format.Length && pFormat[src] == '0')
  1729. sb.Append(pFormat[src++]);
  1730. }
  1731. }
  1732. break;
  1733. }
  1734. default:
  1735. sb.Append(ch);
  1736. break;
  1737. }
  1738. }
  1739. }
  1740. if (number.IsNegative && (section == 0) && (number.Scale == 0) && (sb.Length > 0))
  1741. sb.Insert(0, info.NegativeSign);
  1742. }
  1743. private static void FormatCurrency(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info)
  1744. {
  1745. string fmt = number.IsNegative ?
  1746. s_negCurrencyFormats[info.CurrencyNegativePattern] :
  1747. s_posCurrencyFormats[info.CurrencyPositivePattern];
  1748. foreach (char ch in fmt)
  1749. {
  1750. switch (ch)
  1751. {
  1752. case '#':
  1753. FormatFixed(ref sb, ref number, nMaxDigits, info, info.currencyGroupSizes, info.CurrencyDecimalSeparator, info.CurrencyGroupSeparator);
  1754. break;
  1755. case '-':
  1756. sb.Append(info.NegativeSign);
  1757. break;
  1758. case '$':
  1759. sb.Append(info.CurrencySymbol);
  1760. break;
  1761. default:
  1762. sb.Append(ch);
  1763. break;
  1764. }
  1765. }
  1766. }
  1767. private static unsafe void FormatFixed(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info, int[] groupDigits, string sDecimal, string sGroup)
  1768. {
  1769. int digPos = number.Scale;
  1770. byte* dig = number.GetDigitsPointer();
  1771. if (digPos > 0)
  1772. {
  1773. if (groupDigits != null)
  1774. {
  1775. int groupSizeIndex = 0; // Index into the groupDigits array.
  1776. int bufferSize = digPos; // The length of the result buffer string.
  1777. int groupSize = 0; // The current group size.
  1778. // Find out the size of the string buffer for the result.
  1779. if (groupDigits.Length != 0) // You can pass in 0 length arrays
  1780. {
  1781. int groupSizeCount = groupDigits[groupSizeIndex]; // The current total of group size.
  1782. while (digPos > groupSizeCount)
  1783. {
  1784. groupSize = groupDigits[groupSizeIndex];
  1785. if (groupSize == 0)
  1786. break;
  1787. bufferSize += sGroup.Length;
  1788. if (groupSizeIndex < groupDigits.Length - 1)
  1789. groupSizeIndex++;
  1790. groupSizeCount += groupDigits[groupSizeIndex];
  1791. if (groupSizeCount < 0 || bufferSize < 0)
  1792. throw new ArgumentOutOfRangeException(); // If we overflow
  1793. }
  1794. groupSize = groupSizeCount == 0 ? 0 : groupDigits[0]; // If you passed in an array with one entry as 0, groupSizeCount == 0
  1795. }
  1796. groupSizeIndex = 0;
  1797. int digitCount = 0;
  1798. int digLength = number.DigitsCount;
  1799. int digStart = (digPos < digLength) ? digPos : digLength;
  1800. fixed (char* spanPtr = &MemoryMarshal.GetReference(sb.AppendSpan(bufferSize)))
  1801. {
  1802. char* p = spanPtr + bufferSize - 1;
  1803. for (int i = digPos - 1; i >= 0; i--)
  1804. {
  1805. *(p--) = (i < digStart) ? (char)(dig[i]) : '0';
  1806. if (groupSize > 0)
  1807. {
  1808. digitCount++;
  1809. if ((digitCount == groupSize) && (i != 0))
  1810. {
  1811. for (int j = sGroup.Length - 1; j >= 0; j--)
  1812. *(p--) = sGroup[j];
  1813. if (groupSizeIndex < groupDigits.Length - 1)
  1814. {
  1815. groupSizeIndex++;
  1816. groupSize = groupDigits[groupSizeIndex];
  1817. }
  1818. digitCount = 0;
  1819. }
  1820. }
  1821. }
  1822. Debug.Assert(p >= spanPtr - 1, "Underflow");
  1823. dig += digStart;
  1824. }
  1825. }
  1826. else
  1827. {
  1828. do
  1829. {
  1830. sb.Append(*dig != 0 ? (char)(*dig++) : '0');
  1831. }
  1832. while (--digPos > 0);
  1833. }
  1834. }
  1835. else
  1836. {
  1837. sb.Append('0');
  1838. }
  1839. if (nMaxDigits > 0)
  1840. {
  1841. sb.Append(sDecimal);
  1842. if ((digPos < 0) && (nMaxDigits > 0))
  1843. {
  1844. int zeroes = Math.Min(-digPos, nMaxDigits);
  1845. sb.Append('0', zeroes);
  1846. digPos += zeroes;
  1847. nMaxDigits -= zeroes;
  1848. }
  1849. while (nMaxDigits > 0)
  1850. {
  1851. sb.Append((*dig != 0) ? (char)(*dig++) : '0');
  1852. nMaxDigits--;
  1853. }
  1854. }
  1855. }
  1856. private static void FormatNumber(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info)
  1857. {
  1858. string fmt = number.IsNegative ?
  1859. s_negNumberFormats[info.NumberNegativePattern] :
  1860. PosNumberFormat;
  1861. foreach (char ch in fmt)
  1862. {
  1863. switch (ch)
  1864. {
  1865. case '#':
  1866. FormatFixed(ref sb, ref number, nMaxDigits, info, info.numberGroupSizes, info.NumberDecimalSeparator, info.NumberGroupSeparator);
  1867. break;
  1868. case '-':
  1869. sb.Append(info.NegativeSign);
  1870. break;
  1871. default:
  1872. sb.Append(ch);
  1873. break;
  1874. }
  1875. }
  1876. }
  1877. private static unsafe void FormatScientific(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info, char expChar)
  1878. {
  1879. byte* dig = number.GetDigitsPointer();
  1880. sb.Append((*dig != 0) ? (char)(*dig++) : '0');
  1881. if (nMaxDigits != 1) // For E0 we would like to suppress the decimal point
  1882. sb.Append(info.NumberDecimalSeparator);
  1883. while (--nMaxDigits > 0)
  1884. sb.Append((*dig != 0) ? (char)(*dig++) : '0');
  1885. int e = number.Digits[0] == 0 ? 0 : number.Scale - 1;
  1886. FormatExponent(ref sb, info, e, expChar, 3, true);
  1887. }
  1888. private static unsafe void FormatExponent(ref ValueStringBuilder sb, NumberFormatInfo info, int value, char expChar, int minDigits, bool positiveSign)
  1889. {
  1890. sb.Append(expChar);
  1891. if (value < 0)
  1892. {
  1893. sb.Append(info.NegativeSign);
  1894. value = -value;
  1895. }
  1896. else
  1897. {
  1898. if (positiveSign)
  1899. sb.Append(info.PositiveSign);
  1900. }
  1901. char* digits = stackalloc char[MaxUInt32DecDigits];
  1902. char* p = UInt32ToDecChars(digits + MaxUInt32DecDigits, (uint)value, minDigits);
  1903. int i = (int)(digits + MaxUInt32DecDigits - p);
  1904. sb.Append(p, (int)(digits + MaxUInt32DecDigits - p));
  1905. }
  1906. private static unsafe void FormatGeneral(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info, char expChar, bool bSuppressScientific)
  1907. {
  1908. int digPos = number.Scale;
  1909. bool scientific = false;
  1910. if (!bSuppressScientific)
  1911. {
  1912. // Don't switch to scientific notation
  1913. if (digPos > nMaxDigits || digPos < -3)
  1914. {
  1915. digPos = 1;
  1916. scientific = true;
  1917. }
  1918. }
  1919. byte* dig = number.GetDigitsPointer();
  1920. if (digPos > 0)
  1921. {
  1922. do
  1923. {
  1924. sb.Append((*dig != 0) ? (char)(*dig++) : '0');
  1925. } while (--digPos > 0);
  1926. }
  1927. else
  1928. {
  1929. sb.Append('0');
  1930. }
  1931. if (*dig != 0 || digPos < 0)
  1932. {
  1933. sb.Append(info.NumberDecimalSeparator);
  1934. while (digPos < 0)
  1935. {
  1936. sb.Append('0');
  1937. digPos++;
  1938. }
  1939. while (*dig != 0)
  1940. sb.Append((char)(*dig++));
  1941. }
  1942. if (scientific)
  1943. FormatExponent(ref sb, info, number.Scale - 1, expChar, 2, true);
  1944. }
  1945. private static void FormatPercent(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info)
  1946. {
  1947. string fmt = number.IsNegative ?
  1948. s_negPercentFormats[info.PercentNegativePattern] :
  1949. s_posPercentFormats[info.PercentPositivePattern];
  1950. foreach (char ch in fmt)
  1951. {
  1952. switch (ch)
  1953. {
  1954. case '#':
  1955. FormatFixed(ref sb, ref number, nMaxDigits, info, info.percentGroupSizes, info.PercentDecimalSeparator, info.PercentGroupSeparator);
  1956. break;
  1957. case '-':
  1958. sb.Append(info.NegativeSign);
  1959. break;
  1960. case '%':
  1961. sb.Append(info.PercentSymbol);
  1962. break;
  1963. default:
  1964. sb.Append(ch);
  1965. break;
  1966. }
  1967. }
  1968. }
  1969. internal static unsafe void RoundNumber(ref NumberBuffer number, int pos)
  1970. {
  1971. byte* dig = number.GetDigitsPointer();
  1972. int i = 0;
  1973. while (i < pos && dig[i] != 0)
  1974. i++;
  1975. if (i == pos && dig[i] >= '5')
  1976. {
  1977. while (i > 0 && dig[i - 1] == '9')
  1978. i--;
  1979. if (i > 0)
  1980. {
  1981. dig[i - 1]++;
  1982. }
  1983. else
  1984. {
  1985. number.Scale++;
  1986. dig[0] = (byte)('1');
  1987. i = 1;
  1988. }
  1989. }
  1990. else
  1991. {
  1992. while (i > 0 && dig[i - 1] == '0')
  1993. i--;
  1994. }
  1995. if (i == 0)
  1996. {
  1997. number.Scale = 0;
  1998. if (number.Kind == NumberBufferKind.Integer)
  1999. {
  2000. number.IsNegative = false;
  2001. }
  2002. }
  2003. dig[i] = (byte)('\0');
  2004. number.DigitsCount = i;
  2005. number.CheckConsistency();
  2006. }
  2007. private static unsafe int FindSection(ReadOnlySpan<char> format, int section)
  2008. {
  2009. int src;
  2010. char ch;
  2011. if (section == 0)
  2012. return 0;
  2013. fixed (char* pFormat = &MemoryMarshal.GetReference(format))
  2014. {
  2015. src = 0;
  2016. for (; ; )
  2017. {
  2018. if (src >= format.Length)
  2019. {
  2020. return 0;
  2021. }
  2022. switch (ch = pFormat[src++])
  2023. {
  2024. case '\'':
  2025. case '"':
  2026. while (src < format.Length && pFormat[src] != 0 && pFormat[src++] != ch)
  2027. ;
  2028. break;
  2029. case '\\':
  2030. if (src < format.Length && pFormat[src] != 0)
  2031. src++;
  2032. break;
  2033. case ';':
  2034. if (--section != 0)
  2035. break;
  2036. if (src < format.Length && pFormat[src] != 0 && pFormat[src] != ';')
  2037. return src;
  2038. goto case '\0';
  2039. case '\0':
  2040. return 0;
  2041. }
  2042. }
  2043. }
  2044. }
  2045. private static uint Low32(ulong value) => (uint)value;
  2046. private static uint High32(ulong value) => (uint)((value & 0xFFFFFFFF00000000) >> 32);
  2047. private static uint Int64DivMod1E9(ref ulong value)
  2048. {
  2049. uint rem = (uint)(value % 1000000000);
  2050. value /= 1000000000;
  2051. return rem;
  2052. }
  2053. private static unsafe void DoubleToNumber(double value, int precision, ref NumberBuffer number)
  2054. {
  2055. if (!double.IsFinite(value))
  2056. {
  2057. number.Scale = double.IsNaN(value) ? ScaleNAN : ScaleINF;
  2058. number.IsNegative = double.IsNegative(value);
  2059. number.Digits[0] = (byte)('\0');
  2060. }
  2061. else if (value == 0.0)
  2062. {
  2063. number.Scale = 0;
  2064. number.IsNegative = double.IsNegative(value);
  2065. number.Digits[0] = (byte)('\0');
  2066. }
  2067. else
  2068. {
  2069. number.DigitsCount = precision;
  2070. if (!Grisu3.Run(value, precision, ref number))
  2071. {
  2072. Dragon4(value, precision, ref number);
  2073. }
  2074. }
  2075. number.CheckConsistency();
  2076. }
  2077. private static long ExtractFractionAndBiasedExponent(double value, out int exponent)
  2078. {
  2079. var bits = BitConverter.DoubleToInt64Bits(value);
  2080. long fraction = (bits & 0xFFFFFFFFFFFFF);
  2081. exponent = (int)((bits >> 52) & 0x7FF);
  2082. if (exponent != 0)
  2083. {
  2084. // For normalized value, according to https://en.wikipedia.org/wiki/Double-precision_floating-point_format
  2085. // value = 1.fraction * 2^(exp - 1023)
  2086. // = (1 + mantissa / 2^52) * 2^(exp - 1023)
  2087. // = (2^52 + mantissa) * 2^(exp - 1023 - 52)
  2088. //
  2089. // So f = (2^52 + mantissa), e = exp - 1075;
  2090. fraction |= ((long)(1) << 52);
  2091. exponent -= 1075;
  2092. }
  2093. else
  2094. {
  2095. // For denormalized value, according to https://en.wikipedia.org/wiki/Double-precision_floating-point_format
  2096. // value = 0.fraction * 2^(1 - 1023)
  2097. // = (mantissa / 2^52) * 2^(-1022)
  2098. // = mantissa * 2^(-1022 - 52)
  2099. // = mantissa * 2^(-1074)
  2100. // So f = mantissa, e = -1074
  2101. exponent = -1074;
  2102. }
  2103. return fraction;
  2104. }
  2105. }
  2106. }