| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- using System.Buffers.Text;
- using System.Diagnostics;
- using System.Globalization;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace System
- {
- // The Format methods provided by the numeric classes convert
- // the numeric value to a string using the format string given by the
- // format parameter. If the format parameter is null or
- // an empty string, the number is formatted as if the string "G" (general
- // format) was specified. The info parameter specifies the
- // NumberFormatInfo instance to use when formatting the number. If the
- // info parameter is null or omitted, the numeric formatting information
- // is obtained from the current culture. The NumberFormatInfo supplies
- // such information as the characters to use for decimal and thousand
- // separators, and the spelling and placement of currency symbols in monetary
- // values.
- //
- // Format strings fall into two categories: Standard format strings and
- // user-defined format strings. A format string consisting of a single
- // alphabetic character (A-Z or a-z), optionally followed by a sequence of
- // digits (0-9), is a standard format string. All other format strings are
- // used-defined format strings.
- //
- // A standard format string takes the form Axx, where A is an
- // alphabetic character called the format specifier and xx is a
- // sequence of digits called the precision specifier. The format
- // specifier controls the type of formatting applied to the number and the
- // precision specifier controls the number of significant digits or decimal
- // places of the formatting operation. The following table describes the
- // supported standard formats.
- //
- // C c - Currency format. The number is
- // converted to a string that represents a currency amount. The conversion is
- // controlled by the currency format information of the NumberFormatInfo
- // used to format the number. The precision specifier indicates the desired
- // number of decimal places. If the precision specifier is omitted, the default
- // currency precision given by the NumberFormatInfo is used.
- //
- // D d - Decimal format. This format is
- // supported for integral types only. The number is converted to a string of
- // decimal digits, prefixed by a minus sign if the number is negative. The
- // precision specifier indicates the minimum number of digits desired in the
- // resulting string. If required, the number will be left-padded with zeros to
- // produce the number of digits given by the precision specifier.
- //
- // E e Engineering (scientific) format.
- // The number is converted to a string of the form
- // "-d.ddd...E+ddd" or "-d.ddd...e+ddd", where each
- // 'd' indicates a digit (0-9). The string starts with a minus sign if the
- // number is negative, and one digit always precedes the decimal point. The
- // precision specifier indicates the desired number of digits after the decimal
- // point. If the precision specifier is omitted, a default of 6 digits after
- // the decimal point is used. The format specifier indicates whether to prefix
- // the exponent with an 'E' or an 'e'. The exponent is always consists of a
- // plus or minus sign and three digits.
- //
- // F f Fixed point format. The number is
- // converted to a string of the form "-ddd.ddd....", where each
- // 'd' indicates a digit (0-9). The string starts with a minus sign if the
- // number is negative. The precision specifier indicates the desired number of
- // decimal places. If the precision specifier is omitted, the default numeric
- // precision given by the NumberFormatInfo is used.
- //
- // G g - General format. The number is
- // converted to the shortest possible decimal representation using fixed point
- // or scientific format. The precision specifier determines the number of
- // significant digits in the resulting string. If the precision specifier is
- // omitted, the number of significant digits is determined by the type of the
- // number being converted (10 for int, 19 for long, 7 for
- // float, 15 for double, 19 for Currency, and 29 for
- // Decimal). Trailing zeros after the decimal point are removed, and the
- // resulting string contains a decimal point only if required. The resulting
- // string uses fixed point format if the exponent of the number is less than
- // the number of significant digits and greater than or equal to -4. Otherwise,
- // the resulting string uses scientific format, and the case of the format
- // specifier controls whether the exponent is prefixed with an 'E' or an 'e'.
- //
- // N n Number format. The number is
- // converted to a string of the form "-d,ddd,ddd.ddd....", where
- // each 'd' indicates a digit (0-9). The string starts with a minus sign if the
- // number is negative. Thousand separators are inserted between each group of
- // three digits to the left of the decimal point. The precision specifier
- // indicates the desired number of decimal places. If the precision specifier
- // is omitted, the default numeric precision given by the
- // NumberFormatInfo is used.
- //
- // X x - Hexadecimal format. This format is
- // supported for integral types only. The number is converted to a string of
- // hexadecimal digits. The format specifier indicates whether to use upper or
- // lower case characters for the hexadecimal digits above 9 ('X' for 'ABCDEF',
- // and 'x' for 'abcdef'). The precision specifier indicates the minimum number
- // of digits desired in the resulting string. If required, the number will be
- // left-padded with zeros to produce the number of digits given by the
- // precision specifier.
- //
- // Some examples of standard format strings and their results are shown in the
- // table below. (The examples all assume a default NumberFormatInfo.)
- //
- // Value Format Result
- // 12345.6789 C $12,345.68
- // -12345.6789 C ($12,345.68)
- // 12345 D 12345
- // 12345 D8 00012345
- // 12345.6789 E 1.234568E+004
- // 12345.6789 E10 1.2345678900E+004
- // 12345.6789 e4 1.2346e+004
- // 12345.6789 F 12345.68
- // 12345.6789 F0 12346
- // 12345.6789 F6 12345.678900
- // 12345.6789 G 12345.6789
- // 12345.6789 G7 12345.68
- // 123456789 G7 1.234568E8
- // 12345.6789 N 12,345.68
- // 123456789 N4 123,456,789.0000
- // 0x2c45e x 2c45e
- // 0x2c45e X 2C45E
- // 0x2c45e X8 0002C45E
- //
- // Format strings that do not start with an alphabetic character, or that start
- // with an alphabetic character followed by a non-digit, are called
- // user-defined format strings. The following table describes the formatting
- // characters that are supported in user defined format strings.
- //
- //
- // 0 - Digit placeholder. If the value being
- // formatted has a digit in the position where the '0' appears in the format
- // string, then that digit is copied to the output string. Otherwise, a '0' is
- // stored in that position in the output string. The position of the leftmost
- // '0' before the decimal point and the rightmost '0' after the decimal point
- // determines the range of digits that are always present in the output
- // string.
- //
- // # - Digit placeholder. If the value being
- // formatted has a digit in the position where the '#' appears in the format
- // string, then that digit is copied to the output string. Otherwise, nothing
- // is stored in that position in the output string.
- //
- // . - Decimal point. The first '.' character
- // in the format string determines the location of the decimal separator in the
- // formatted value; any additional '.' characters are ignored. The actual
- // character used as a the decimal separator in the output string is given by
- // the NumberFormatInfo used to format the number.
- //
- // , - Thousand separator and number scaling.
- // The ',' character serves two purposes. First, if the format string contains
- // a ',' character between two digit placeholders (0 or #) and to the left of
- // the decimal point if one is present, then the output will have thousand
- // separators inserted between each group of three digits to the left of the
- // decimal separator. The actual character used as a the decimal separator in
- // the output string is given by the NumberFormatInfo used to format the
- // number. Second, if the format string contains one or more ',' characters
- // immediately to the left of the decimal point, or after the last digit
- // placeholder if there is no decimal point, then the number will be divided by
- // 1000 times the number of ',' characters before it is formatted. For example,
- // the format string '0,,' will represent 100 million as just 100. Use of the
- // ',' character to indicate scaling does not also cause the formatted number
- // to have thousand separators. Thus, to scale a number by 1 million and insert
- // thousand separators you would use the format string '#,##0,,'.
- //
- // % - Percentage placeholder. The presence of
- // a '%' character in the format string causes the number to be multiplied by
- // 100 before it is formatted. The '%' character itself is inserted in the
- // output string where it appears in the format string.
- //
- // E+ E- e+ e- - Scientific notation.
- // If any of the strings 'E+', 'E-', 'e+', or 'e-' are present in the format
- // string and are immediately followed by at least one '0' character, then the
- // number is formatted using scientific notation with an 'E' or 'e' inserted
- // between the number and the exponent. The number of '0' characters following
- // the scientific notation indicator determines the minimum number of digits to
- // output for the exponent. The 'E+' and 'e+' formats indicate that a sign
- // character (plus or minus) should always precede the exponent. The 'E-' and
- // 'e-' formats indicate that a sign character should only precede negative
- // exponents.
- //
- // \ - Literal character. A backslash character
- // causes the next character in the format string to be copied to the output
- // string as-is. The backslash itself isn't copied, so to place a backslash
- // character in the output string, use two backslashes (\\) in the format
- // string.
- //
- // 'ABC' "ABC" - Literal string. Characters
- // enclosed in single or double quotation marks are copied to the output string
- // as-is and do not affect formatting.
- //
- // ; - Section separator. The ';' character is
- // used to separate sections for positive, negative, and zero numbers in the
- // format string.
- //
- // Other - All other characters are copied to
- // the output string in the position they appear.
- //
- // For fixed point formats (formats not containing an 'E+', 'E-', 'e+', or
- // 'e-'), the number is rounded to as many decimal places as there are digit
- // placeholders to the right of the decimal point. If the format string does
- // not contain a decimal point, the number is rounded to the nearest
- // integer. If the number has more digits than there are digit placeholders to
- // the left of the decimal point, the extra digits are copied to the output
- // string immediately before the first digit placeholder.
- //
- // For scientific formats, the number is rounded to as many significant digits
- // as there are digit placeholders in the format string.
- //
- // To allow for different formatting of positive, negative, and zero values, a
- // user-defined format string may contain up to three sections separated by
- // semicolons. The results of having one, two, or three sections in the format
- // string are described in the table below.
- //
- // Sections:
- //
- // One - The format string applies to all values.
- //
- // Two - The first section applies to positive values
- // and zeros, and the second section applies to negative values. If the number
- // to be formatted is negative, but becomes zero after rounding according to
- // the format in the second section, then the resulting zero is formatted
- // according to the first section.
- //
- // Three - The first section applies to positive
- // values, the second section applies to negative values, and the third section
- // applies to zeros. The second section may be left empty (by having no
- // characters between the semicolons), in which case the first section applies
- // to all non-zero values. If the number to be formatted is non-zero, but
- // becomes zero after rounding according to the format in the first or second
- // section, then the resulting zero is formatted according to the third
- // section.
- //
- // For both standard and user-defined formatting operations on values of type
- // float and double, if the value being formatted is a NaN (Not
- // a Number) or a positive or negative infinity, then regardless of the format
- // string, the resulting string is given by the NaNSymbol,
- // PositiveInfinitySymbol, or NegativeInfinitySymbol property of
- // the NumberFormatInfo used to format the number.
- internal static partial class Number
- {
- internal const int DecimalPrecision = 29; // Decimal.DecCalc also uses this value
- // SinglePrecision and DoublePrecision represent the maximum number of digits required
- // to guarantee that any given Single or Double can roundtrip. Some numbers may require
- // less, but none will require more.
- private const int SinglePrecision = 9;
- private const int DoublePrecision = 17;
- private const int DefaultPrecisionExponentialFormat = 6;
- private const int ScaleNAN = unchecked((int)0x80000000);
- private const int ScaleINF = 0x7FFFFFFF;
- private const int MaxUInt32DecDigits = 10;
- private const int CharStackBufferSize = 32;
- private const string PosNumberFormat = "#";
- private static readonly string[] s_singleDigitStringCache = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
- private static readonly string[] s_posCurrencyFormats =
- {
- "$#", "#$", "$ #", "# $"
- };
- private static readonly string[] s_negCurrencyFormats =
- {
- "($#)", "-$#", "$-#", "$#-",
- "(#$)", "-#$", "#-$", "#$-",
- "-# $", "-$ #", "# $-", "$ #-",
- "$ -#", "#- $", "($ #)", "(# $)"
- };
- private static readonly string[] s_posPercentFormats =
- {
- "# %", "#%", "%#", "% #"
- };
- private static readonly string[] s_negPercentFormats =
- {
- "-# %", "-#%", "-%#",
- "%-#", "%#-",
- "#-%", "#%-",
- "-% #", "# %-", "% #-",
- "% -#", "#- %"
- };
- private static readonly string[] s_negNumberFormats =
- {
- "(#)", "-#", "- #", "#-", "# -",
- };
- public static unsafe string FormatDecimal(decimal value, ReadOnlySpan<char> format, NumberFormatInfo info)
- {
- char fmt = ParseFormatSpecifier(format, out int digits);
- byte* pDigits = stackalloc byte[DecimalNumberBufferLength];
- NumberBuffer number = new NumberBuffer(NumberBufferKind.Decimal, pDigits, DecimalNumberBufferLength);
- DecimalToNumber(ref value, ref number);
- char* stackPtr = stackalloc char[CharStackBufferSize];
- ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
- if (fmt != 0)
- {
- NumberToString(ref sb, ref number, fmt, digits, info);
- }
- else
- {
- NumberToStringFormat(ref sb, ref number, format, info);
- }
- return sb.ToString();
- }
- public static unsafe bool TryFormatDecimal(decimal value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
- {
- char fmt = ParseFormatSpecifier(format, out int digits);
- byte* pDigits = stackalloc byte[DecimalNumberBufferLength];
- NumberBuffer number = new NumberBuffer(NumberBufferKind.Decimal, pDigits, DecimalNumberBufferLength);
- DecimalToNumber(ref value, ref number);
- char* stackPtr = stackalloc char[CharStackBufferSize];
- ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
- if (fmt != 0)
- {
- NumberToString(ref sb, ref number, fmt, digits, info);
- }
- else
- {
- NumberToStringFormat(ref sb, ref number, format, info);
- }
- return sb.TryCopyTo(destination, out charsWritten);
- }
- internal static unsafe void DecimalToNumber(ref decimal d, ref NumberBuffer number)
- {
- byte* buffer = number.GetDigitsPointer();
- number.DigitsCount = DecimalPrecision;
- number.IsNegative = d.IsNegative;
- byte* p = buffer + DecimalPrecision;
- while ((d.Mid | d.High) != 0)
- {
- p = UInt32ToDecChars(p, decimal.DecDivMod1E9(ref d), 9);
- }
- p = UInt32ToDecChars(p, d.Low, 0);
- int i = (int)((buffer + DecimalPrecision) - p);
- number.DigitsCount = i;
- number.Scale = i - d.Scale;
- byte* dst = number.GetDigitsPointer();
- while (--i >= 0)
- {
- *dst++ = *p++;
- }
- *dst = (byte)('\0');
- number.CheckConsistency();
- }
- public static string FormatDouble(double value, string format, NumberFormatInfo info)
- {
- Span<char> stackBuffer = stackalloc char[CharStackBufferSize];
- var sb = new ValueStringBuilder(stackBuffer);
- return FormatDouble(ref sb, value, format, info) ?? sb.ToString();
- }
- public static bool TryFormatDouble(double value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
- {
- Span<char> stackBuffer = stackalloc char[CharStackBufferSize];
- var sb = new ValueStringBuilder(stackBuffer);
- string s = FormatDouble(ref sb, value, format, info);
- return s != null ?
- TryCopyTo(s, destination, out charsWritten) :
- sb.TryCopyTo(destination, out charsWritten);
- }
- private static int GetFloatingPointMaxDigitsAndPrecision(char fmt, ref int precision, NumberFormatInfo info, out bool isSignificantDigits)
- {
- if (fmt == 0)
- {
- isSignificantDigits = true;
- return precision;
- }
- int maxDigits = precision;
- switch (fmt)
- {
- case 'C':
- case 'c':
- {
- // The currency format uses the precision specifier to indicate the number of
- // decimal digits to format. This defaults to NumberFormatInfo.CurrencyDecimalDigits.
- if (precision == -1)
- {
- precision = info.CurrencyDecimalDigits;
- }
- isSignificantDigits = false;
- break;
- }
- case 'E':
- case 'e':
- {
- // The exponential format uses the precision specifier to indicate the number of
- // decimal digits to format. This defaults to 6. However, the exponential format
- // also always formats a single integral digit, so we need to increase the precision
- // specifier and treat it as the number of significant digits to account for this.
- if (precision == -1)
- {
- precision = DefaultPrecisionExponentialFormat;
- }
- precision++;
- isSignificantDigits = true;
- break;
- }
- case 'F':
- case 'f':
- case 'N':
- case 'n':
- {
- // The fixed-point and number formats use the precision specifier to indicate the number
- // of decimal digits to format. This defaults to NumberFormatInfo.NumberDecimalDigits.
- if (precision == -1)
- {
- precision = info.NumberDecimalDigits;
- }
- isSignificantDigits = false;
- break;
- }
- case 'G':
- case 'g':
- {
- // The general format uses the precision specifier to indicate the number of significant
- // digits to format. This defaults to the shortest roundtrippable string. Additionally,
- // given that we can't return zero significant digits, we treat 0 as returning the shortest
- // roundtrippable string as well.
- if (precision == 0)
- {
- precision = -1;
- }
- isSignificantDigits = true;
- break;
- }
- case 'P':
- case 'p':
- {
- // The percent format uses the precision specifier to indicate the number of
- // decimal digits to format. This defaults to NumberFormatInfo.PercentDecimalDigits.
- // However, the percent format also always multiplies the number by 100, so we need
- // to increase the precision specifier to ensure we get the appropriate number of digits.
- if (precision == -1)
- {
- precision = info.PercentDecimalDigits;
- }
- precision += 2;
- isSignificantDigits = false;
- break;
- }
- case 'R':
- case 'r':
- {
- // The roundtrip format ignores the precision specifier and always returns the shortest
- // roundtrippable string.
- precision = -1;
- isSignificantDigits = true;
- break;
- }
- default:
- {
- throw new FormatException(SR.Argument_BadFormatSpecifier);
- }
- }
- return maxDigits;
- }
- /// <summary>Formats the specified value according to the specified format and info.</summary>
- /// <returns>
- /// Non-null if an existing string can be returned, in which case the builder will be unmodified.
- /// Null if no existing string was returned, in which case the formatted output is in the builder.
- /// </returns>
- private static unsafe string FormatDouble(ref ValueStringBuilder sb, double value, ReadOnlySpan<char> format, NumberFormatInfo info)
- {
- if (!double.IsFinite(value))
- {
- if (double.IsNaN(value))
- {
- return info.NaNSymbol;
- }
- return double.IsNegative(value) ? info.NegativeInfinitySymbol : info.PositiveInfinitySymbol;
- }
- char fmt = ParseFormatSpecifier(format, out int precision);
- byte* pDigits = stackalloc byte[DoubleNumberBufferLength];
- NumberBuffer number = new NumberBuffer(NumberBufferKind.FloatingPoint, pDigits, DoubleNumberBufferLength);
- number.IsNegative = double.IsNegative(value);
- // We need to track the original precision requested since some formats
- // accept values like 0 and others may require additional fixups.
- int nMaxDigits = GetFloatingPointMaxDigitsAndPrecision(fmt, ref precision, info, out bool isSignificantDigits);
- if ((value != 0.0) && (!isSignificantDigits || !Grisu3.TryRunDouble(value, precision, ref number)))
- {
- Dragon4Double(value, precision, isSignificantDigits, ref number);
- }
- number.CheckConsistency();
- // When the number is known to be roundtrippable (either because we requested it be, or
- // because we know we have enough digits to satisfy roundtrippability), we should validate
- // that the number actually roundtrips back to the original result.
- Debug.Assert(((precision != -1) && (precision < DoublePrecision)) || (BitConverter.DoubleToInt64Bits(value) == BitConverter.DoubleToInt64Bits(NumberToDouble(ref number))));
- if (fmt != 0)
- {
- if (precision == -1)
- {
- Debug.Assert((fmt == 'G') || (fmt == 'g') || (fmt == 'R') || (fmt == 'r'));
- // For the roundtrip and general format specifiers, when returning the shortest roundtrippable
- // string, we need to update the maximum number of digits to be the greater of number.DigitsCount
- // or DoublePrecision. This ensures that we continue returning "pretty" strings for values with
- // less digits. One example this fixes is "-60", which would otherwise be formatted as "-6E+01"
- // since DigitsCount would be 1 and the formatter would almost immediately switch to scientific notation.
- nMaxDigits = Math.Max(number.DigitsCount, DoublePrecision);
- }
- NumberToString(ref sb, ref number, fmt, nMaxDigits, info);
- }
- else
- {
- NumberToStringFormat(ref sb, ref number, format, info);
- }
- return null;
- }
- public static string FormatSingle(float value, string format, NumberFormatInfo info)
- {
- Span<char> stackBuffer = stackalloc char[CharStackBufferSize];
- var sb = new ValueStringBuilder(stackBuffer);
- return FormatSingle(ref sb, value, format, info) ?? sb.ToString();
- }
- public static bool TryFormatSingle(float value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
- {
- Span<char> stackBuffer = stackalloc char[CharStackBufferSize];
- var sb = new ValueStringBuilder(stackBuffer);
- string s = FormatSingle(ref sb, value, format, info);
- return s != null ?
- TryCopyTo(s, destination, out charsWritten) :
- sb.TryCopyTo(destination, out charsWritten);
- }
- /// <summary>Formats the specified value according to the specified format and info.</summary>
- /// <returns>
- /// Non-null if an existing string can be returned, in which case the builder will be unmodified.
- /// Null if no existing string was returned, in which case the formatted output is in the builder.
- /// </returns>
- private static unsafe string FormatSingle(ref ValueStringBuilder sb, float value, ReadOnlySpan<char> format, NumberFormatInfo info)
- {
- if (!float.IsFinite(value))
- {
- if (float.IsNaN(value))
- {
- return info.NaNSymbol;
- }
- return float.IsNegative(value) ? info.NegativeInfinitySymbol : info.PositiveInfinitySymbol;
- }
- char fmt = ParseFormatSpecifier(format, out int precision);
- byte* pDigits = stackalloc byte[SingleNumberBufferLength];
- NumberBuffer number = new NumberBuffer(NumberBufferKind.FloatingPoint, pDigits, SingleNumberBufferLength);
- number.IsNegative = float.IsNegative(value);
- // We need to track the original precision requested since some formats
- // accept values like 0 and others may require additional fixups.
- int nMaxDigits = GetFloatingPointMaxDigitsAndPrecision(fmt, ref precision, info, out bool isSignificantDigits);
- if ((value != 0.0f) && (!isSignificantDigits || !Grisu3.TryRunSingle(value, precision, ref number)))
- {
- Dragon4Single(value, precision, isSignificantDigits, ref number);
- }
- number.CheckConsistency();
- // When the number is known to be roundtrippable (either because we requested it be, or
- // because we know we have enough digits to satisfy roundtrippability), we should validate
- // that the number actually roundtrips back to the original result.
- Debug.Assert(((precision != -1) && (precision < SinglePrecision)) || (BitConverter.SingleToInt32Bits(value) == BitConverter.SingleToInt32Bits(NumberToSingle(ref number))));
- if (fmt != 0)
- {
- if (precision == -1)
- {
- Debug.Assert((fmt == 'G') || (fmt == 'g') || (fmt == 'R') || (fmt == 'r'));
- // For the roundtrip and general format specifiers, when returning the shortest roundtrippable
- // string, we need to update the maximum number of digits to be the greater of number.DigitsCount
- // or SinglePrecision. This ensures that we continue returning "pretty" strings for values with
- // less digits. One example this fixes is "-60", which would otherwise be formatted as "-6E+01"
- // since DigitsCount would be 1 and the formatter would almost immediately switch to scientific notation.
- nMaxDigits = Math.Max(number.DigitsCount, SinglePrecision);
- }
- NumberToString(ref sb, ref number, fmt, nMaxDigits, info);
- }
- else
- {
- NumberToStringFormat(ref sb, ref number, format, info);
- }
- return null;
- }
- private static bool TryCopyTo(string source, Span<char> destination, out int charsWritten)
- {
- Debug.Assert(source != null);
- if (source.AsSpan().TryCopyTo(destination))
- {
- charsWritten = source.Length;
- return true;
- }
- charsWritten = 0;
- return false;
- }
- public static unsafe string FormatInt32(int value, ReadOnlySpan<char> format, IFormatProvider provider)
- {
- // Fast path for default format with a non-negative value
- if (value >= 0 && format.Length == 0)
- {
- return UInt32ToDecStr((uint)value, digits: -1);
- }
- char fmt = ParseFormatSpecifier(format, out int digits);
- char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
- if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
- {
- return value >= 0 ?
- UInt32ToDecStr((uint)value, digits) :
- NegativeInt32ToDecStr(value, digits, NumberFormatInfo.GetInstance(provider).NegativeSign);
- }
- else if (fmtUpper == 'X')
- {
- // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
- // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code produces lowercase.
- return Int32ToHexStr(value, (char)(fmt - ('X' - 'A' + 10)), digits);
- }
- else
- {
- NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
- byte* pDigits = stackalloc byte[Int32NumberBufferLength];
- NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, Int32NumberBufferLength);
- Int32ToNumber(value, ref number);
- char* stackPtr = stackalloc char[CharStackBufferSize];
- ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
- if (fmt != 0)
- {
- NumberToString(ref sb, ref number, fmt, digits, info);
- }
- else
- {
- NumberToStringFormat(ref sb, ref number, format, info);
- }
- return sb.ToString();
- }
- }
- public static unsafe bool TryFormatInt32(int value, ReadOnlySpan<char> format, IFormatProvider provider, Span<char> destination, out int charsWritten)
- {
- // Fast path for default format with a non-negative value
- if (value >= 0 && format.Length == 0)
- {
- return TryUInt32ToDecStr((uint)value, digits: -1, destination, out charsWritten);
- }
- char fmt = ParseFormatSpecifier(format, out int digits);
- char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
- if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
- {
- return value >= 0 ?
- TryUInt32ToDecStr((uint)value, digits, destination, out charsWritten) :
- TryNegativeInt32ToDecStr(value, digits, NumberFormatInfo.GetInstance(provider).NegativeSign, destination, out charsWritten);
- }
- else if (fmtUpper == 'X')
- {
- // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
- // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code produces lowercase.
- return TryInt32ToHexStr(value, (char)(fmt - ('X' - 'A' + 10)), digits, destination, out charsWritten);
- }
- else
- {
- NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
- byte* pDigits = stackalloc byte[Int32NumberBufferLength];
- NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, Int32NumberBufferLength);
- Int32ToNumber(value, ref number);
- char* stackPtr = stackalloc char[CharStackBufferSize];
- ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
- if (fmt != 0)
- {
- NumberToString(ref sb, ref number, fmt, digits, info);
- }
- else
- {
- NumberToStringFormat(ref sb, ref number, format, info);
- }
- return sb.TryCopyTo(destination, out charsWritten);
- }
- }
- public static unsafe string FormatUInt32(uint value, ReadOnlySpan<char> format, IFormatProvider provider)
- {
- // Fast path for default format
- if (format.Length == 0)
- {
- return UInt32ToDecStr(value, digits: -1);
- }
- char fmt = ParseFormatSpecifier(format, out int digits);
- char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
- if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
- {
- return UInt32ToDecStr(value, digits);
- }
- else if (fmtUpper == 'X')
- {
- // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
- // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code produces lowercase.
- return Int32ToHexStr((int)value, (char)(fmt - ('X' - 'A' + 10)), digits);
- }
- else
- {
- NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
- byte* pDigits = stackalloc byte[UInt32NumberBufferLength];
- NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, UInt32NumberBufferLength);
- UInt32ToNumber(value, ref number);
- char* stackPtr = stackalloc char[CharStackBufferSize];
- ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
- if (fmt != 0)
- {
- NumberToString(ref sb, ref number, fmt, digits, info);
- }
- else
- {
- NumberToStringFormat(ref sb, ref number, format, info);
- }
- return sb.ToString();
- }
- }
- public static unsafe bool TryFormatUInt32(uint value, ReadOnlySpan<char> format, IFormatProvider provider, Span<char> destination, out int charsWritten)
- {
- // Fast path for default format
- if (format.Length == 0)
- {
- return TryUInt32ToDecStr(value, digits: -1, destination, out charsWritten);
- }
- char fmt = ParseFormatSpecifier(format, out int digits);
- char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
- if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
- {
- return TryUInt32ToDecStr(value, digits, destination, out charsWritten);
- }
- else if (fmtUpper == 'X')
- {
- // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
- // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code produces lowercase.
- return TryInt32ToHexStr((int)value, (char)(fmt - ('X' - 'A' + 10)), digits, destination, out charsWritten);
- }
- else
- {
- NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
- byte* pDigits = stackalloc byte[UInt32NumberBufferLength];
- NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, UInt32NumberBufferLength);
- UInt32ToNumber(value, ref number);
- char* stackPtr = stackalloc char[CharStackBufferSize];
- ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
- if (fmt != 0)
- {
- NumberToString(ref sb, ref number, fmt, digits, info);
- }
- else
- {
- NumberToStringFormat(ref sb, ref number, format, info);
- }
- return sb.TryCopyTo(destination, out charsWritten);
- }
- }
- public static unsafe string FormatInt64(long value, ReadOnlySpan<char> format, IFormatProvider provider)
- {
- // Fast path for default format with a non-negative value
- if (value >= 0 && format.Length == 0)
- {
- return UInt64ToDecStr((ulong)value, digits: -1);
- }
- char fmt = ParseFormatSpecifier(format, out int digits);
- char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
- if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
- {
- return value >= 0 ?
- UInt64ToDecStr((ulong)value, digits) :
- NegativeInt64ToDecStr(value, digits, NumberFormatInfo.GetInstance(provider).NegativeSign);
- }
- else if (fmtUpper == 'X')
- {
- // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
- // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code
- // produces lowercase.
- return Int64ToHexStr(value, (char)(fmt - ('X' - 'A' + 10)), digits);
- }
- else
- {
- NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
- byte* pDigits = stackalloc byte[Int64NumberBufferLength];
- NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, Int64NumberBufferLength);
- Int64ToNumber(value, ref number);
- char* stackPtr = stackalloc char[CharStackBufferSize];
- ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
- if (fmt != 0)
- {
- NumberToString(ref sb, ref number, fmt, digits, info);
- }
- else
- {
- NumberToStringFormat(ref sb, ref number, format, info);
- }
- return sb.ToString();
- }
- }
- public static unsafe bool TryFormatInt64(long value, ReadOnlySpan<char> format, IFormatProvider provider, Span<char> destination, out int charsWritten)
- {
- // Fast path for default format with a non-negative value
- if (value >= 0 && format.Length == 0)
- {
- return TryUInt64ToDecStr((ulong)value, digits: -1, destination, out charsWritten);
- }
- char fmt = ParseFormatSpecifier(format, out int digits);
- char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
- if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
- {
- return value >= 0 ?
- TryUInt64ToDecStr((ulong)value, digits, destination, out charsWritten) :
- TryNegativeInt64ToDecStr(value, digits, NumberFormatInfo.GetInstance(provider).NegativeSign, destination, out charsWritten);
- }
- else if (fmtUpper == 'X')
- {
- // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
- // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code
- // produces lowercase.
- return TryInt64ToHexStr(value, (char)(fmt - ('X' - 'A' + 10)), digits, destination, out charsWritten);
- }
- else
- {
- NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
- byte* pDigits = stackalloc byte[Int64NumberBufferLength];
- NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, Int64NumberBufferLength);
- Int64ToNumber(value, ref number);
- char* stackPtr = stackalloc char[CharStackBufferSize];
- ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
- if (fmt != 0)
- {
- NumberToString(ref sb, ref number, fmt, digits, info);
- }
- else
- {
- NumberToStringFormat(ref sb, ref number, format, info);
- }
- return sb.TryCopyTo(destination, out charsWritten);
- }
- }
- public static unsafe string FormatUInt64(ulong value, ReadOnlySpan<char> format, IFormatProvider provider)
- {
- // Fast path for default format
- if (format.Length == 0)
- {
- return UInt64ToDecStr(value, digits: -1);
- }
- char fmt = ParseFormatSpecifier(format, out int digits);
- char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
- if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
- {
- return UInt64ToDecStr(value, digits);
- }
- else if (fmtUpper == 'X')
- {
- // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
- // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code
- // produces lowercase.
- return Int64ToHexStr((long)value, (char)(fmt - ('X' - 'A' + 10)), digits);
- }
- else
- {
- NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
- byte* pDigits = stackalloc byte[UInt64NumberBufferLength];
- NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, UInt64NumberBufferLength);
- UInt64ToNumber(value, ref number);
- char* stackPtr = stackalloc char[CharStackBufferSize];
- ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
- if (fmt != 0)
- {
- NumberToString(ref sb, ref number, fmt, digits, info);
- }
- else
- {
- NumberToStringFormat(ref sb, ref number, format, info);
- }
- return sb.ToString();
- }
- }
- public static unsafe bool TryFormatUInt64(ulong value, ReadOnlySpan<char> format, IFormatProvider provider, Span<char> destination, out int charsWritten)
- {
- // Fast path for default format
- if (format.Length == 0)
- {
- return TryUInt64ToDecStr(value, digits: -1, destination, out charsWritten);
- }
- char fmt = ParseFormatSpecifier(format, out int digits);
- char fmtUpper = (char)(fmt & 0xFFDF); // ensure fmt is upper-cased for purposes of comparison
- if ((fmtUpper == 'G' && digits < 1) || fmtUpper == 'D')
- {
- return TryUInt64ToDecStr(value, digits, destination, out charsWritten);
- }
- else if (fmtUpper == 'X')
- {
- // The fmt-(X-A+10) hack has the effect of dictating whether we produce uppercase or lowercase
- // hex numbers for a-f. 'X' as the fmt code produces uppercase. 'x' as the format code
- // produces lowercase.
- return TryInt64ToHexStr((long)value, (char)(fmt - ('X' - 'A' + 10)), digits, destination, out charsWritten);
- }
- else
- {
- NumberFormatInfo info = NumberFormatInfo.GetInstance(provider);
- byte* pDigits = stackalloc byte[UInt64NumberBufferLength];
- NumberBuffer number = new NumberBuffer(NumberBufferKind.Integer, pDigits, UInt64NumberBufferLength);
- UInt64ToNumber(value, ref number);
- char* stackPtr = stackalloc char[CharStackBufferSize];
- ValueStringBuilder sb = new ValueStringBuilder(new Span<char>(stackPtr, CharStackBufferSize));
- if (fmt != 0)
- {
- NumberToString(ref sb, ref number, fmt, digits, info);
- }
- else
- {
- NumberToStringFormat(ref sb, ref number, format, info);
- }
- return sb.TryCopyTo(destination, out charsWritten);
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)] // called from only one location
- private static unsafe void Int32ToNumber(int value, ref NumberBuffer number)
- {
- number.DigitsCount = Int32Precision;
- if (value >= 0)
- {
- number.IsNegative = false;
- }
- else
- {
- number.IsNegative = true;
- value = -value;
- }
- byte* buffer = number.GetDigitsPointer();
- byte* p = UInt32ToDecChars(buffer + Int32Precision, (uint)value, 0);
- int i = (int)(buffer + Int32Precision - p);
- number.DigitsCount = i;
- number.Scale = i;
- byte* dst = number.GetDigitsPointer();
- while (--i >= 0)
- *dst++ = *p++;
- *dst = (byte)('\0');
- number.CheckConsistency();
- }
- private static unsafe string NegativeInt32ToDecStr(int value, int digits, string sNegative)
- {
- Debug.Assert(value < 0);
- if (digits < 1)
- digits = 1;
- int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits((uint)(-value))) + sNegative.Length;
- string result = string.FastAllocateString(bufferLength);
- fixed (char* buffer = result)
- {
- char* p = UInt32ToDecChars(buffer + bufferLength, (uint)(-value), digits);
- Debug.Assert(p == buffer + sNegative.Length);
- for (int i = sNegative.Length - 1; i >= 0; i--)
- {
- *(--p) = sNegative[i];
- }
- Debug.Assert(p == buffer);
- }
- return result;
- }
- private static unsafe bool TryNegativeInt32ToDecStr(int value, int digits, string sNegative, Span<char> destination, out int charsWritten)
- {
- Debug.Assert(value < 0);
- if (digits < 1)
- digits = 1;
- int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits((uint)(-value))) + sNegative.Length;
- if (bufferLength > destination.Length)
- {
- charsWritten = 0;
- return false;
- }
- charsWritten = bufferLength;
- fixed (char* buffer = &MemoryMarshal.GetReference(destination))
- {
- char* p = UInt32ToDecChars(buffer + bufferLength, (uint)(-value), digits);
- Debug.Assert(p == buffer + sNegative.Length);
- for (int i = sNegative.Length - 1; i >= 0; i--)
- {
- *(--p) = sNegative[i];
- }
- Debug.Assert(p == buffer);
- }
- return true;
- }
- private static unsafe string Int32ToHexStr(int value, char hexBase, int digits)
- {
- if (digits < 1)
- digits = 1;
- int bufferLength = Math.Max(digits, FormattingHelpers.CountHexDigits((uint)value));
- string result = string.FastAllocateString(bufferLength);
- fixed (char* buffer = result)
- {
- char* p = Int32ToHexChars(buffer + bufferLength, (uint)value, hexBase, digits);
- Debug.Assert(p == buffer);
- }
- return result;
- }
- private static unsafe bool TryInt32ToHexStr(int value, char hexBase, int digits, Span<char> destination, out int charsWritten)
- {
- if (digits < 1)
- digits = 1;
- int bufferLength = Math.Max(digits, FormattingHelpers.CountHexDigits((uint)value));
- if (bufferLength > destination.Length)
- {
- charsWritten = 0;
- return false;
- }
- charsWritten = bufferLength;
- fixed (char* buffer = &MemoryMarshal.GetReference(destination))
- {
- char* p = Int32ToHexChars(buffer + bufferLength, (uint)value, hexBase, digits);
- Debug.Assert(p == buffer);
- }
- return true;
- }
- private static unsafe char* Int32ToHexChars(char* buffer, uint value, int hexBase, int digits)
- {
- while (--digits >= 0 || value != 0)
- {
- byte digit = (byte)(value & 0xF);
- *(--buffer) = (char)(digit + (digit < 10 ? (byte)'0' : hexBase));
- value >>= 4;
- }
- return buffer;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)] // called from only one location
- private static unsafe void UInt32ToNumber(uint value, ref NumberBuffer number)
- {
- number.DigitsCount = UInt32Precision;
- number.IsNegative = false;
- byte* buffer = number.GetDigitsPointer();
- byte* p = UInt32ToDecChars(buffer + UInt32Precision, value, 0);
- int i = (int)(buffer + UInt32Precision - p);
- number.DigitsCount = i;
- number.Scale = i;
- byte* dst = number.GetDigitsPointer();
- while (--i >= 0)
- *dst++ = *p++;
- *dst = (byte)('\0');
- number.CheckConsistency();
- }
- internal static unsafe byte* UInt32ToDecChars(byte* bufferEnd, uint value, int digits)
- {
- while (--digits >= 0 || value != 0)
- {
- // TODO https://github.com/dotnet/coreclr/issues/3439
- uint newValue = value / 10;
- *(--bufferEnd) = (byte)(value - (newValue * 10) + '0');
- value = newValue;
- }
- return bufferEnd;
- }
- internal static unsafe char* UInt32ToDecChars(char* bufferEnd, uint value, int digits)
- {
- while (--digits >= 0 || value != 0)
- {
- // TODO https://github.com/dotnet/coreclr/issues/3439
- uint newValue = value / 10;
- *(--bufferEnd) = (char)(value - (newValue * 10) + '0');
- value = newValue;
- }
- return bufferEnd;
- }
- private static unsafe string UInt32ToDecStr(uint value, int digits)
- {
- int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
- // For single-digit values that are very common, especially 0 and 1, just return cached strings.
- if (bufferLength == 1)
- {
- return s_singleDigitStringCache[value];
- }
- string result = string.FastAllocateString(bufferLength);
- fixed (char* buffer = result)
- {
- char* p = buffer + bufferLength;
- if (digits <= 1)
- {
- do
- {
- // TODO https://github.com/dotnet/coreclr/issues/3439
- uint div = value / 10;
- *(--p) = (char)('0' + value - (div * 10));
- value = div;
- }
- while (value != 0);
- }
- else
- {
- p = UInt32ToDecChars(p, value, digits);
- }
- Debug.Assert(p == buffer);
- }
- return result;
- }
- private static unsafe bool TryUInt32ToDecStr(uint value, int digits, Span<char> destination, out int charsWritten)
- {
- int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
- if (bufferLength > destination.Length)
- {
- charsWritten = 0;
- return false;
- }
- charsWritten = bufferLength;
- fixed (char* buffer = &MemoryMarshal.GetReference(destination))
- {
- char* p = buffer + bufferLength;
- if (digits <= 1)
- {
- do
- {
- // TODO https://github.com/dotnet/coreclr/issues/3439
- uint div = value / 10;
- *(--p) = (char)('0' + value - (div * 10));
- value = div;
- }
- while (value != 0);
- }
- else
- {
- p = UInt32ToDecChars(p, value, digits);
- }
- Debug.Assert(p == buffer);
- }
- return true;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static unsafe bool TryCopyTo(char* src, int length, Span<char> destination, out int charsWritten)
- {
- if (new ReadOnlySpan<char>(src, length).TryCopyTo(destination))
- {
- charsWritten = length;
- return true;
- }
- else
- {
- charsWritten = 0;
- return false;
- }
- }
- private static unsafe void Int64ToNumber(long input, ref NumberBuffer number)
- {
- ulong value = (ulong)input;
- number.IsNegative = input < 0;
- number.DigitsCount = Int64Precision;
- if (number.IsNegative)
- {
- value = (ulong)(-input);
- }
- byte* buffer = number.GetDigitsPointer();
- byte* p = buffer + Int64Precision;
- while (High32(value) != 0)
- p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
- p = UInt32ToDecChars(p, Low32(value), 0);
- int i = (int)(buffer + Int64Precision - p);
- number.DigitsCount = i;
- number.Scale = i;
- byte* dst = number.GetDigitsPointer();
- while (--i >= 0)
- *dst++ = *p++;
- *dst = (byte)('\0');
- number.CheckConsistency();
- }
- private static unsafe string NegativeInt64ToDecStr(long input, int digits, string sNegative)
- {
- Debug.Assert(input < 0);
- if (digits < 1)
- {
- digits = 1;
- }
- ulong value = (ulong)(-input);
- int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value)) + sNegative.Length;
- string result = string.FastAllocateString(bufferLength);
- fixed (char* buffer = result)
- {
- char* p = buffer + bufferLength;
- while (High32(value) != 0)
- {
- p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
- digits -= 9;
- }
- p = UInt32ToDecChars(p, Low32(value), digits);
- Debug.Assert(p == buffer + sNegative.Length);
- for (int i = sNegative.Length - 1; i >= 0; i--)
- {
- *(--p) = sNegative[i];
- }
- Debug.Assert(p == buffer);
- }
- return result;
- }
- private static unsafe bool TryNegativeInt64ToDecStr(long input, int digits, string sNegative, Span<char> destination, out int charsWritten)
- {
- Debug.Assert(input < 0);
- if (digits < 1)
- {
- digits = 1;
- }
- ulong value = (ulong)(-input);
- int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits((ulong)(-input))) + sNegative.Length;
- if (bufferLength > destination.Length)
- {
- charsWritten = 0;
- return false;
- }
- charsWritten = bufferLength;
- fixed (char* buffer = &MemoryMarshal.GetReference(destination))
- {
- char* p = buffer + bufferLength;
- while (High32(value) != 0)
- {
- p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
- digits -= 9;
- }
- p = UInt32ToDecChars(p, Low32(value), digits);
- Debug.Assert(p == buffer + sNegative.Length);
- for (int i = sNegative.Length - 1; i >= 0; i--)
- {
- *(--p) = sNegative[i];
- }
- Debug.Assert(p == buffer);
- }
- return true;
- }
- private static unsafe string Int64ToHexStr(long value, char hexBase, int digits)
- {
- int bufferLength = Math.Max(digits, FormattingHelpers.CountHexDigits((ulong)value));
- string result = string.FastAllocateString(bufferLength);
- fixed (char* buffer = result)
- {
- char* p = buffer + bufferLength;
- if (High32((ulong)value) != 0)
- {
- p = Int32ToHexChars(p, Low32((ulong)value), hexBase, 8);
- p = Int32ToHexChars(p, High32((ulong)value), hexBase, digits - 8);
- }
- else
- {
- p = Int32ToHexChars(p, Low32((ulong)value), hexBase, Math.Max(digits, 1));
- }
- Debug.Assert(p == buffer);
- }
- return result;
- }
- private static unsafe bool TryInt64ToHexStr(long value, char hexBase, int digits, Span<char> destination, out int charsWritten)
- {
- int bufferLength = Math.Max(digits, FormattingHelpers.CountHexDigits((ulong)value));
- if (bufferLength > destination.Length)
- {
- charsWritten = 0;
- return false;
- }
- charsWritten = bufferLength;
- fixed (char* buffer = &MemoryMarshal.GetReference(destination))
- {
- char* p = buffer + bufferLength;
- if (High32((ulong)value) != 0)
- {
- p = Int32ToHexChars(p, Low32((ulong)value), hexBase, 8);
- p = Int32ToHexChars(p, High32((ulong)value), hexBase, digits - 8);
- }
- else
- {
- p = Int32ToHexChars(p, Low32((ulong)value), hexBase, Math.Max(digits, 1));
- }
- Debug.Assert(p == buffer);
- }
- return true;
- }
- private static unsafe void UInt64ToNumber(ulong value, ref NumberBuffer number)
- {
- number.DigitsCount = UInt64Precision;
- number.IsNegative = false;
- byte* buffer = number.GetDigitsPointer();
- byte* p = buffer + UInt64Precision;
- while (High32(value) != 0)
- p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
- p = UInt32ToDecChars(p, Low32(value), 0);
- int i = (int)(buffer + UInt64Precision - p);
- number.DigitsCount = i;
- number.Scale = i;
- byte* dst = number.GetDigitsPointer();
- while (--i >= 0)
- *dst++ = *p++;
- *dst = (byte)('\0');
- number.CheckConsistency();
- }
- private static unsafe string UInt64ToDecStr(ulong value, int digits)
- {
- if (digits < 1)
- digits = 1;
- int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
- // For single-digit values that are very common, especially 0 and 1, just return cached strings.
- if (bufferLength == 1)
- {
- return s_singleDigitStringCache[value];
- }
- string result = string.FastAllocateString(bufferLength);
- fixed (char* buffer = result)
- {
- char* p = buffer + bufferLength;
- while (High32(value) != 0)
- {
- p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
- digits -= 9;
- }
- p = UInt32ToDecChars(p, Low32(value), digits);
- Debug.Assert(p == buffer);
- }
- return result;
- }
- private static unsafe bool TryUInt64ToDecStr(ulong value, int digits, Span<char> destination, out int charsWritten)
- {
- if (digits < 1)
- digits = 1;
- int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
- if (bufferLength > destination.Length)
- {
- charsWritten = 0;
- return false;
- }
- charsWritten = bufferLength;
- fixed (char* buffer = &MemoryMarshal.GetReference(destination))
- {
- char* p = buffer + bufferLength;
- while (High32(value) != 0)
- {
- p = UInt32ToDecChars(p, Int64DivMod1E9(ref value), 9);
- digits -= 9;
- }
- p = UInt32ToDecChars(p, Low32(value), digits);
- Debug.Assert(p == buffer);
- }
- return true;
- }
- internal static unsafe char ParseFormatSpecifier(ReadOnlySpan<char> format, out int digits)
- {
- char c = default;
- if (format.Length > 0)
- {
- // If the format begins with a symbol, see if it's a standard format
- // with or without a specified number of digits.
- c = format[0];
- if ((uint)(c - 'A') <= 'Z' - 'A' ||
- (uint)(c - 'a') <= 'z' - 'a')
- {
- // Fast path for sole symbol, e.g. "D"
- if (format.Length == 1)
- {
- digits = -1;
- return c;
- }
- if (format.Length == 2)
- {
- // Fast path for symbol and single digit, e.g. "X4"
- int d = format[1] - '0';
- if ((uint)d < 10)
- {
- digits = d;
- return c;
- }
- }
- else if (format.Length == 3)
- {
- // Fast path for symbol and double digit, e.g. "F12"
- int d1 = format[1] - '0', d2 = format[2] - '0';
- if ((uint)d1 < 10 && (uint)d2 < 10)
- {
- digits = d1 * 10 + d2;
- return c;
- }
- }
- // Fallback for symbol and any length digits. The digits value must be >= 0 && <= 99,
- // but it can begin with any number of 0s, and thus we may need to check more than two
- // digits. Further, for compat, we need to stop when we hit a null char.
- int n = 0;
- int i = 1;
- while (i < format.Length && (((uint)format[i] - '0') < 10) && n < 10)
- {
- n = (n * 10) + format[i++] - '0';
- }
- // If we're at the end of the digits rather than having stopped because we hit something
- // other than a digit or overflowed, return the standard format info.
- if (i == format.Length || format[i] == '\0')
- {
- digits = n;
- return c;
- }
- }
- }
- // Default empty format to be "G"; custom format is signified with '\0'.
- digits = -1;
- return format.Length == 0 || c == '\0' ? // For compat, treat '\0' as the end of the specifier, even if the specifier extends beyond it.
- 'G' :
- '\0';
- }
- internal static unsafe void NumberToString(ref ValueStringBuilder sb, ref NumberBuffer number, char format, int nMaxDigits, NumberFormatInfo info)
- {
- number.CheckConsistency();
- switch (format)
- {
- case 'C':
- case 'c':
- {
- if (nMaxDigits < 0)
- nMaxDigits = info.CurrencyDecimalDigits;
- RoundNumber(ref number, number.Scale + nMaxDigits); // Don't change this line to use digPos since digCount could have its sign changed.
- FormatCurrency(ref sb, ref number, nMaxDigits, info);
- break;
- }
- case 'F':
- case 'f':
- {
- if (nMaxDigits < 0)
- nMaxDigits = info.NumberDecimalDigits;
- RoundNumber(ref number, number.Scale + nMaxDigits);
- if (number.IsNegative)
- sb.Append(info.NegativeSign);
- FormatFixed(ref sb, ref number, nMaxDigits, info, null, info.NumberDecimalSeparator, null);
- break;
- }
- case 'N':
- case 'n':
- {
- if (nMaxDigits < 0)
- nMaxDigits = info.NumberDecimalDigits; // Since we are using digits in our calculation
- RoundNumber(ref number, number.Scale + nMaxDigits);
- FormatNumber(ref sb, ref number, nMaxDigits, info);
- break;
- }
- case 'E':
- case 'e':
- {
- if (nMaxDigits < 0)
- nMaxDigits = DefaultPrecisionExponentialFormat;
- nMaxDigits++;
- RoundNumber(ref number, nMaxDigits);
- if (number.IsNegative)
- sb.Append(info.NegativeSign);
- FormatScientific(ref sb, ref number, nMaxDigits, info, format);
- break;
- }
- case 'G':
- case 'g':
- {
- bool noRounding = false;
- if (nMaxDigits < 1)
- {
- if ((number.Kind == NumberBufferKind.Decimal) && (nMaxDigits == -1))
- {
- noRounding = true; // Turn off rounding for ECMA compliance to output trailing 0's after decimal as significant
- if (number.Digits[0] == 0)
- {
- // -0 should be formatted as 0 for decimal. This is normally handled by RoundNumber (which we are skipping)
- goto SkipSign;
- }
- goto SkipRounding;
- }
- else
- {
- // This ensures that the PAL code pads out to the correct place even when we use the default precision
- nMaxDigits = number.DigitsCount;
- }
- }
- RoundNumber(ref number, nMaxDigits);
- SkipRounding:
- if (number.IsNegative)
- sb.Append(info.NegativeSign);
- SkipSign:
- FormatGeneral(ref sb, ref number, nMaxDigits, info, (char)(format - ('G' - 'E')), noRounding);
- break;
- }
- case 'P':
- case 'p':
- {
- if (nMaxDigits < 0)
- nMaxDigits = info.PercentDecimalDigits;
- number.Scale += 2;
- RoundNumber(ref number, number.Scale + nMaxDigits);
- FormatPercent(ref sb, ref number, nMaxDigits, info);
- break;
- }
- case 'R':
- case 'r':
- {
- format = (char)(format - ('R' - 'G'));
- Debug.Assert((format == 'G') || (format == 'g'));
- goto case 'G';
- }
- default:
- throw new FormatException(SR.Argument_BadFormatSpecifier);
- }
- }
- internal static unsafe void NumberToStringFormat(ref ValueStringBuilder sb, ref NumberBuffer number, ReadOnlySpan<char> format, NumberFormatInfo info)
- {
- number.CheckConsistency();
- int digitCount;
- int decimalPos;
- int firstDigit;
- int lastDigit;
- int digPos;
- bool scientific;
- int thousandPos;
- int thousandCount = 0;
- bool thousandSeps;
- int scaleAdjust;
- int adjust;
- int section;
- int src;
- byte* dig = number.GetDigitsPointer();
- char ch;
- section = FindSection(format, dig[0] == 0 ? 2 : number.IsNegative ? 1 : 0);
- while (true)
- {
- digitCount = 0;
- decimalPos = -1;
- firstDigit = 0x7FFFFFFF;
- lastDigit = 0;
- scientific = false;
- thousandPos = -1;
- thousandSeps = false;
- scaleAdjust = 0;
- src = section;
- fixed (char* pFormat = &MemoryMarshal.GetReference(format))
- {
- while (src < format.Length && (ch = pFormat[src++]) != 0 && ch != ';')
- {
- switch (ch)
- {
- case '#':
- digitCount++;
- break;
- case '0':
- if (firstDigit == 0x7FFFFFFF)
- firstDigit = digitCount;
- digitCount++;
- lastDigit = digitCount;
- break;
- case '.':
- if (decimalPos < 0)
- decimalPos = digitCount;
- break;
- case ',':
- if (digitCount > 0 && decimalPos < 0)
- {
- if (thousandPos >= 0)
- {
- if (thousandPos == digitCount)
- {
- thousandCount++;
- break;
- }
- thousandSeps = true;
- }
- thousandPos = digitCount;
- thousandCount = 1;
- }
- break;
- case '%':
- scaleAdjust += 2;
- break;
- case '\x2030':
- scaleAdjust += 3;
- break;
- case '\'':
- case '"':
- while (src < format.Length && pFormat[src] != 0 && pFormat[src++] != ch)
- ;
- break;
- case '\\':
- if (src < format.Length && pFormat[src] != 0)
- src++;
- break;
- case 'E':
- case 'e':
- if ((src < format.Length && pFormat[src] == '0') ||
- (src + 1 < format.Length && (pFormat[src] == '+' || pFormat[src] == '-') && pFormat[src + 1] == '0'))
- {
- while (++src < format.Length && pFormat[src] == '0')
- ;
- scientific = true;
- }
- break;
- }
- }
- }
- if (decimalPos < 0)
- decimalPos = digitCount;
- if (thousandPos >= 0)
- {
- if (thousandPos == decimalPos)
- scaleAdjust -= thousandCount * 3;
- else
- thousandSeps = true;
- }
- if (dig[0] != 0)
- {
- number.Scale += scaleAdjust;
- int pos = scientific ? digitCount : number.Scale + digitCount - decimalPos;
- RoundNumber(ref number, pos);
- if (dig[0] == 0)
- {
- src = FindSection(format, 2);
- if (src != section)
- {
- section = src;
- continue;
- }
- }
- }
- else
- {
- if (number.Kind != NumberBufferKind.FloatingPoint)
- {
- // The integer types don't have a concept of -0 and decimal always format -0 as 0
- number.IsNegative = false;
- }
- number.Scale = 0; // Decimals with scale ('0.00') should be rounded.
- }
- break;
- }
- firstDigit = firstDigit < decimalPos ? decimalPos - firstDigit : 0;
- lastDigit = lastDigit > decimalPos ? decimalPos - lastDigit : 0;
- if (scientific)
- {
- digPos = decimalPos;
- adjust = 0;
- }
- else
- {
- digPos = number.Scale > decimalPos ? number.Scale : decimalPos;
- adjust = number.Scale - decimalPos;
- }
- src = section;
- // Adjust can be negative, so we make this an int instead of an unsigned int.
- // Adjust represents the number of characters over the formatting e.g. format string is "0000" and you are trying to
- // format 100000 (6 digits). Means adjust will be 2. On the other hand if you are trying to format 10 adjust will be
- // -2 and we'll need to fixup these digits with 0 padding if we have 0 formatting as in this example.
- Span<int> thousandsSepPos = stackalloc int[4];
- int thousandsSepCtr = -1;
- if (thousandSeps)
- {
- // We need to precompute this outside the number formatting loop
- if (info.NumberGroupSeparator.Length > 0)
- {
- // We need this array to figure out where to insert the thousands separator. We would have to traverse the string
- // backwards. PIC formatting always traverses forwards. These indices are precomputed to tell us where to insert
- // the thousands separator so we can get away with traversing forwards. Note we only have to compute up to digPos.
- // The max is not bound since you can have formatting strings of the form "000,000..", and this
- // should handle that case too.
- int[] groupDigits = info.numberGroupSizes;
- int groupSizeIndex = 0; // Index into the groupDigits array.
- int groupTotalSizeCount = 0;
- int groupSizeLen = groupDigits.Length; // The length of groupDigits array.
- if (groupSizeLen != 0)
- groupTotalSizeCount = groupDigits[groupSizeIndex]; // The current running total of group size.
- int groupSize = groupTotalSizeCount;
- int totalDigits = digPos + ((adjust < 0) ? adjust : 0); // Actual number of digits in o/p
- int numDigits = (firstDigit > totalDigits) ? firstDigit : totalDigits;
- while (numDigits > groupTotalSizeCount)
- {
- if (groupSize == 0)
- break;
- ++thousandsSepCtr;
- if (thousandsSepCtr >= thousandsSepPos.Length)
- {
- var newThousandsSepPos = new int[thousandsSepPos.Length * 2];
- thousandsSepPos.CopyTo(newThousandsSepPos);
- thousandsSepPos = newThousandsSepPos;
- }
- thousandsSepPos[thousandsSepCtr] = groupTotalSizeCount;
- if (groupSizeIndex < groupSizeLen - 1)
- {
- groupSizeIndex++;
- groupSize = groupDigits[groupSizeIndex];
- }
- groupTotalSizeCount += groupSize;
- }
- }
- }
- if (number.IsNegative && (section == 0) && (number.Scale != 0))
- sb.Append(info.NegativeSign);
- bool decimalWritten = false;
- fixed (char* pFormat = &MemoryMarshal.GetReference(format))
- {
- byte* cur = dig;
- while (src < format.Length && (ch = pFormat[src++]) != 0 && ch != ';')
- {
- if (adjust > 0)
- {
- switch (ch)
- {
- case '#':
- case '0':
- case '.':
- while (adjust > 0)
- {
- // digPos will be one greater than thousandsSepPos[thousandsSepCtr] since we are at
- // the character after which the groupSeparator needs to be appended.
- sb.Append(*cur != 0 ? (char)(*cur++) : '0');
- if (thousandSeps && digPos > 1 && thousandsSepCtr >= 0)
- {
- if (digPos == thousandsSepPos[thousandsSepCtr] + 1)
- {
- sb.Append(info.NumberGroupSeparator);
- thousandsSepCtr--;
- }
- }
- digPos--;
- adjust--;
- }
- break;
- }
- }
- switch (ch)
- {
- case '#':
- case '0':
- {
- if (adjust < 0)
- {
- adjust++;
- ch = digPos <= firstDigit ? '0' : '\0';
- }
- else
- {
- ch = *cur != 0 ? (char)(*cur++) : digPos > lastDigit ? '0' : '\0';
- }
- if (ch != 0)
- {
- sb.Append(ch);
- if (thousandSeps && digPos > 1 && thousandsSepCtr >= 0)
- {
- if (digPos == thousandsSepPos[thousandsSepCtr] + 1)
- {
- sb.Append(info.NumberGroupSeparator);
- thousandsSepCtr--;
- }
- }
- }
- digPos--;
- break;
- }
- case '.':
- {
- if (digPos != 0 || decimalWritten)
- {
- // For compatibility, don't echo repeated decimals
- break;
- }
- // If the format has trailing zeros or the format has a decimal and digits remain
- if (lastDigit < 0 || (decimalPos < digitCount && *cur != 0))
- {
- sb.Append(info.NumberDecimalSeparator);
- decimalWritten = true;
- }
- break;
- }
- case '\x2030':
- sb.Append(info.PerMilleSymbol);
- break;
- case '%':
- sb.Append(info.PercentSymbol);
- break;
- case ',':
- break;
- case '\'':
- case '"':
- while (src < format.Length && pFormat[src] != 0 && pFormat[src] != ch)
- sb.Append(pFormat[src++]);
- if (src < format.Length && pFormat[src] != 0)
- src++;
- break;
- case '\\':
- if (src < format.Length && pFormat[src] != 0)
- sb.Append(pFormat[src++]);
- break;
- case 'E':
- case 'e':
- {
- bool positiveSign = false;
- int i = 0;
- if (scientific)
- {
- if (src < format.Length && pFormat[src] == '0')
- {
- // Handles E0, which should format the same as E-0
- i++;
- }
- else if (src + 1 < format.Length && pFormat[src] == '+' && pFormat[src + 1] == '0')
- {
- // Handles E+0
- positiveSign = true;
- }
- else if (src + 1 < format.Length && pFormat[src] == '-' && pFormat[src + 1] == '0')
- {
- // Handles E-0
- // Do nothing, this is just a place holder s.t. we don't break out of the loop.
- }
- else
- {
- sb.Append(ch);
- break;
- }
- while (++src < format.Length && pFormat[src] == '0')
- i++;
- if (i > 10)
- i = 10;
- int exp = dig[0] == 0 ? 0 : number.Scale - decimalPos;
- FormatExponent(ref sb, info, exp, ch, i, positiveSign);
- scientific = false;
- }
- else
- {
- sb.Append(ch); // Copy E or e to output
- if (src < format.Length)
- {
- if (pFormat[src] == '+' || pFormat[src] == '-')
- sb.Append(pFormat[src++]);
- while (src < format.Length && pFormat[src] == '0')
- sb.Append(pFormat[src++]);
- }
- }
- break;
- }
- default:
- sb.Append(ch);
- break;
- }
- }
- }
- if (number.IsNegative && (section == 0) && (number.Scale == 0) && (sb.Length > 0))
- sb.Insert(0, info.NegativeSign);
- }
- private static void FormatCurrency(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info)
- {
- string fmt = number.IsNegative ?
- s_negCurrencyFormats[info.CurrencyNegativePattern] :
- s_posCurrencyFormats[info.CurrencyPositivePattern];
- foreach (char ch in fmt)
- {
- switch (ch)
- {
- case '#':
- FormatFixed(ref sb, ref number, nMaxDigits, info, info.currencyGroupSizes, info.CurrencyDecimalSeparator, info.CurrencyGroupSeparator);
- break;
- case '-':
- sb.Append(info.NegativeSign);
- break;
- case '$':
- sb.Append(info.CurrencySymbol);
- break;
- default:
- sb.Append(ch);
- break;
- }
- }
- }
- private static unsafe void FormatFixed(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info, int[] groupDigits, string sDecimal, string sGroup)
- {
- int digPos = number.Scale;
- byte* dig = number.GetDigitsPointer();
- if (digPos > 0)
- {
- if (groupDigits != null)
- {
- int groupSizeIndex = 0; // Index into the groupDigits array.
- int bufferSize = digPos; // The length of the result buffer string.
- int groupSize = 0; // The current group size.
- // Find out the size of the string buffer for the result.
- if (groupDigits.Length != 0) // You can pass in 0 length arrays
- {
- int groupSizeCount = groupDigits[groupSizeIndex]; // The current total of group size.
- while (digPos > groupSizeCount)
- {
- groupSize = groupDigits[groupSizeIndex];
- if (groupSize == 0)
- break;
- bufferSize += sGroup.Length;
- if (groupSizeIndex < groupDigits.Length - 1)
- groupSizeIndex++;
- groupSizeCount += groupDigits[groupSizeIndex];
- if (groupSizeCount < 0 || bufferSize < 0)
- throw new ArgumentOutOfRangeException(); // If we overflow
- }
- groupSize = groupSizeCount == 0 ? 0 : groupDigits[0]; // If you passed in an array with one entry as 0, groupSizeCount == 0
- }
- groupSizeIndex = 0;
- int digitCount = 0;
- int digLength = number.DigitsCount;
- int digStart = (digPos < digLength) ? digPos : digLength;
- fixed (char* spanPtr = &MemoryMarshal.GetReference(sb.AppendSpan(bufferSize)))
- {
- char* p = spanPtr + bufferSize - 1;
- for (int i = digPos - 1; i >= 0; i--)
- {
- *(p--) = (i < digStart) ? (char)(dig[i]) : '0';
- if (groupSize > 0)
- {
- digitCount++;
- if ((digitCount == groupSize) && (i != 0))
- {
- for (int j = sGroup.Length - 1; j >= 0; j--)
- *(p--) = sGroup[j];
- if (groupSizeIndex < groupDigits.Length - 1)
- {
- groupSizeIndex++;
- groupSize = groupDigits[groupSizeIndex];
- }
- digitCount = 0;
- }
- }
- }
- Debug.Assert(p >= spanPtr - 1, "Underflow");
- dig += digStart;
- }
- }
- else
- {
- do
- {
- sb.Append(*dig != 0 ? (char)(*dig++) : '0');
- }
- while (--digPos > 0);
- }
- }
- else
- {
- sb.Append('0');
- }
- if (nMaxDigits > 0)
- {
- sb.Append(sDecimal);
- if ((digPos < 0) && (nMaxDigits > 0))
- {
- int zeroes = Math.Min(-digPos, nMaxDigits);
- sb.Append('0', zeroes);
- digPos += zeroes;
- nMaxDigits -= zeroes;
- }
- while (nMaxDigits > 0)
- {
- sb.Append((*dig != 0) ? (char)(*dig++) : '0');
- nMaxDigits--;
- }
- }
- }
- private static void FormatNumber(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info)
- {
- string fmt = number.IsNegative ?
- s_negNumberFormats[info.NumberNegativePattern] :
- PosNumberFormat;
- foreach (char ch in fmt)
- {
- switch (ch)
- {
- case '#':
- FormatFixed(ref sb, ref number, nMaxDigits, info, info.numberGroupSizes, info.NumberDecimalSeparator, info.NumberGroupSeparator);
- break;
- case '-':
- sb.Append(info.NegativeSign);
- break;
- default:
- sb.Append(ch);
- break;
- }
- }
- }
- private static unsafe void FormatScientific(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info, char expChar)
- {
- byte* dig = number.GetDigitsPointer();
- sb.Append((*dig != 0) ? (char)(*dig++) : '0');
- if (nMaxDigits != 1) // For E0 we would like to suppress the decimal point
- sb.Append(info.NumberDecimalSeparator);
- while (--nMaxDigits > 0)
- sb.Append((*dig != 0) ? (char)(*dig++) : '0');
- int e = number.Digits[0] == 0 ? 0 : number.Scale - 1;
- FormatExponent(ref sb, info, e, expChar, 3, true);
- }
- private static unsafe void FormatExponent(ref ValueStringBuilder sb, NumberFormatInfo info, int value, char expChar, int minDigits, bool positiveSign)
- {
- sb.Append(expChar);
- if (value < 0)
- {
- sb.Append(info.NegativeSign);
- value = -value;
- }
- else
- {
- if (positiveSign)
- sb.Append(info.PositiveSign);
- }
- char* digits = stackalloc char[MaxUInt32DecDigits];
- char* p = UInt32ToDecChars(digits + MaxUInt32DecDigits, (uint)value, minDigits);
- int i = (int)(digits + MaxUInt32DecDigits - p);
- sb.Append(p, (int)(digits + MaxUInt32DecDigits - p));
- }
- private static unsafe void FormatGeneral(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info, char expChar, bool bSuppressScientific)
- {
- int digPos = number.Scale;
- bool scientific = false;
- if (!bSuppressScientific)
- {
- // Don't switch to scientific notation
- if (digPos > nMaxDigits || digPos < -3)
- {
- digPos = 1;
- scientific = true;
- }
- }
- byte* dig = number.GetDigitsPointer();
- if (digPos > 0)
- {
- do
- {
- sb.Append((*dig != 0) ? (char)(*dig++) : '0');
- } while (--digPos > 0);
- }
- else
- {
- sb.Append('0');
- }
- if (*dig != 0 || digPos < 0)
- {
- sb.Append(info.NumberDecimalSeparator);
- while (digPos < 0)
- {
- sb.Append('0');
- digPos++;
- }
- while (*dig != 0)
- sb.Append((char)(*dig++));
- }
- if (scientific)
- FormatExponent(ref sb, info, number.Scale - 1, expChar, 2, true);
- }
- private static void FormatPercent(ref ValueStringBuilder sb, ref NumberBuffer number, int nMaxDigits, NumberFormatInfo info)
- {
- string fmt = number.IsNegative ?
- s_negPercentFormats[info.PercentNegativePattern] :
- s_posPercentFormats[info.PercentPositivePattern];
- foreach (char ch in fmt)
- {
- switch (ch)
- {
- case '#':
- FormatFixed(ref sb, ref number, nMaxDigits, info, info.percentGroupSizes, info.PercentDecimalSeparator, info.PercentGroupSeparator);
- break;
- case '-':
- sb.Append(info.NegativeSign);
- break;
- case '%':
- sb.Append(info.PercentSymbol);
- break;
- default:
- sb.Append(ch);
- break;
- }
- }
- }
- internal static unsafe void RoundNumber(ref NumberBuffer number, int pos)
- {
- byte* dig = number.GetDigitsPointer();
- int i = 0;
- while (i < pos && dig[i] != 0)
- i++;
- if (i == pos && dig[i] >= '5')
- {
- while (i > 0 && dig[i - 1] == '9')
- i--;
- if (i > 0)
- {
- dig[i - 1]++;
- }
- else
- {
- number.Scale++;
- dig[0] = (byte)('1');
- i = 1;
- }
- }
- else
- {
- while (i > 0 && dig[i - 1] == '0')
- i--;
- }
- if (i == 0)
- {
- if (number.Kind != NumberBufferKind.FloatingPoint)
- {
- // The integer types don't have a concept of -0 and decimal always format -0 as 0
- number.IsNegative = false;
- }
- number.Scale = 0; // Decimals with scale ('0.00') should be rounded.
- }
- dig[i] = (byte)('\0');
- number.DigitsCount = i;
- number.CheckConsistency();
- }
- private static unsafe int FindSection(ReadOnlySpan<char> format, int section)
- {
- int src;
- char ch;
- if (section == 0)
- return 0;
- fixed (char* pFormat = &MemoryMarshal.GetReference(format))
- {
- src = 0;
- for (; ; )
- {
- if (src >= format.Length)
- {
- return 0;
- }
- switch (ch = pFormat[src++])
- {
- case '\'':
- case '"':
- while (src < format.Length && pFormat[src] != 0 && pFormat[src++] != ch)
- ;
- break;
- case '\\':
- if (src < format.Length && pFormat[src] != 0)
- src++;
- break;
- case ';':
- if (--section != 0)
- break;
- if (src < format.Length && pFormat[src] != 0 && pFormat[src] != ';')
- return src;
- goto case '\0';
- case '\0':
- return 0;
- }
- }
- }
- }
- private static uint Low32(ulong value) => (uint)value;
- private static uint High32(ulong value) => (uint)((value & 0xFFFFFFFF00000000) >> 32);
- private static uint Int64DivMod1E9(ref ulong value)
- {
- uint rem = (uint)(value % 1000000000);
- value /= 1000000000;
- return rem;
- }
- private static ulong ExtractFractionAndBiasedExponent(double value, out int exponent)
- {
- ulong bits = (ulong)(BitConverter.DoubleToInt64Bits(value));
- ulong fraction = (bits & 0xFFFFFFFFFFFFF);
- exponent = ((int)(bits >> 52) & 0x7FF);
- if (exponent != 0)
- {
- // For normalized value, according to https://en.wikipedia.org/wiki/Double-precision_floating-point_format
- // value = 1.fraction * 2^(exp - 1023)
- // = (1 + mantissa / 2^52) * 2^(exp - 1023)
- // = (2^52 + mantissa) * 2^(exp - 1023 - 52)
- //
- // So f = (2^52 + mantissa), e = exp - 1075;
- fraction |= (1UL << 52);
- exponent -= 1075;
- }
- else
- {
- // For denormalized value, according to https://en.wikipedia.org/wiki/Double-precision_floating-point_format
- // value = 0.fraction * 2^(1 - 1023)
- // = (mantissa / 2^52) * 2^(-1022)
- // = mantissa * 2^(-1022 - 52)
- // = mantissa * 2^(-1074)
- // So f = mantissa, e = -1074
- exponent = -1074;
- }
- return fraction;
- }
- private static uint ExtractFractionAndBiasedExponent(float value, out int exponent)
- {
- uint bits = (uint)(BitConverter.SingleToInt32Bits(value));
- uint fraction = (bits & 0x7FFFFF);
- exponent = ((int)(bits >> 23) & 0xFF);
- if (exponent != 0)
- {
- // For normalized value, according to https://en.wikipedia.org/wiki/Single-precision_floating-point_format
- // value = 1.fraction * 2^(exp - 127)
- // = (1 + mantissa / 2^23) * 2^(exp - 127)
- // = (2^23 + mantissa) * 2^(exp - 127 - 23)
- //
- // So f = (2^23 + mantissa), e = exp - 150;
- fraction |= (1U << 23);
- exponent -= 150;
- }
- else
- {
- // For denormalized value, according to https://en.wikipedia.org/wiki/Single-precision_floating-point_format
- // value = 0.fraction * 2^(1 - 127)
- // = (mantissa / 2^23) * 2^(-126)
- // = mantissa * 2^(-126 - 23)
- // = mantissa * 2^(-149)
- // So f = mantissa, e = -149
- exponent = -149;
- }
- return fraction;
- }
- }
- }
|