59 lines
38 KiB
Go
Raw Normal View History

//
2020-08-23 14:15:53 +00:00
// Copyright 2020 FoxyUtils ehf. All rights reserved.
//
// This is a commercial product and requires a license to operate.
// A trial license can be obtained at https://unidoc.io
//
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
//
// Use of this source code is governed by the UniDoc End User License Agreement
// terms that can be accessed at https://unidoc.io/eula/
2020-08-23 14:15:53 +00:00
// Package format provides support for parsing and evaluating
// spreadsheetml/Excel number formats.
//
// Internally spreadsheets store numbers and dates values as a text
// representation of a floating point number (e.g. 1.2345). This number is then
// displayed in Excel or another spreadsheet viewer differently depending on the
// number fornat of the cell style applied to the cell.
//
// As an example, the same value of 1.2345 can be displayed as:
// - "1" with format "0"
// - "1.2" with format "0.0"
// - "1.23" with format "0.00"
// - "1.235" with format "0.000"
// - "123%" with format "0%"
// - "1 23/100" with fornat "0 0/100"
// - "1.23E+00" with format "0.00E+00"
// - "29:37:41s" with format `[h]:mm:ss"s"`
package format ;import (_dfb "bytes";_fdef "fmt";_eegg "github.com/unidoc/unioffice";_bbfb "io";_daae "math";_gff "strconv";_decb "strings";_fg "time";);
// Format is a parsed number format.
2020-08-23 14:15:53 +00:00
type Format struct{Whole []Token ;Fractional []Token ;Exponent []Token ;IsExponential bool ;_ggd bool ;_bafe bool ;_dde bool ;_gac bool ;_gad bool ;_cbc bool ;_ebdg int64 ;_fae int ;};type Lexer struct{_gag Format ;_cgf []Format ;};
// Number is used to format a number with a format string. If the format
// string is empty, then General number formatting is used which attempts to mimic
// Excel's general formatting.
2020-08-23 14:15:53 +00:00
func Number (v float64 ,f string )string {if f ==""||f =="\u0047e\u006e\u0065\u0072\u0061\u006c"||f =="\u0040"{return NumberGeneric (v );};_gacb :=Parse (f );if len (_gacb )==1{return _fcfc (v ,_gacb [0],false );}else if len (_gacb )> 1&&v < 0{return _fcfc (v ,_gacb [1],true );}else if len (_gacb )> 2&&v ==0{return _fcfc (v ,_gacb [2],false );};return _fcfc (v ,_gacb [0],false );};func _fda (_bfc ,_ged float64 ,_dgg Format )[]byte {if len (_dgg .Fractional )==0{return nil ;};_ebg :=_gff .AppendFloat (nil ,_bfc ,'f',-1,64);if len (_ebg )> 2{_ebg =_ebg [2:];}else {_ebg =nil ;};_gffa :=make ([]byte ,0,len (_ebg ));_gffa =append (_gffa ,'.');_dbf :=0;_ddea :for _aba :=0;_aba < len (_dgg .Fractional );_aba ++{_dfd :=_aba ;_dcd :=_dgg .Fractional [_aba ];switch _dcd .Type {case FmtTypeDigit :if _dfd < len (_ebg ){_gffa =append (_gffa ,_ebg [_dfd ]);_dbf ++;}else {_gffa =append (_gffa ,'0');};case FmtTypeDigitOpt :if _dfd >=0{_gffa =append (_gffa ,_ebg [_dfd ]);_dbf ++;}else {break _ddea ;};case FmtTypeLiteral :_gffa =append (_gffa ,_dcd .Literal );default:_eegg .Log ("\u0075\u006e\u0073\u0075\u0070\u0070o\u0072\u0074\u0065\u0064\u0020\u0074\u0079\u0070\u0065\u0020\u0069\u006e\u0020f\u0072\u0061\u0063\u0074\u0069\u006f\u006ea\u006c\u0020\u0025\u0076",_dcd );};};return _gffa ;};func _cdbf (_cbd []byte )[]byte {for _cded :=len (_cbd )-1;_cded > 0;_cded --{if _cbd [_cded ]=='9'+1{_cbd [_cded ]='0';if _cbd [_cded -1]=='.'{_cded --;};_cbd [_cded -1]++;};};if _cbd [0]=='9'+1{_cbd [0]='0';copy (_cbd [1:],_cbd [0:]);_cbd [0]='1';};return _cbd ;};
// Token is a format token in the Excel format string.
type Token struct{Type FmtType ;Literal byte ;DateTime string ;};
2017-09-20 15:01:05 -04:00
// Value formats a value as a number or string depending on if it appears to be
// a number or string.
2020-08-23 14:15:53 +00:00
func Value (v string ,f string )string {if IsNumber (v ){_ffcb ,_ :=_gff .ParseFloat (v ,64);return Number (_ffcb ,f );};return String (v ,f );};const _bgc int =-1;func _ef (_gdb ,_gbbf float64 ,_cdb Format )[]byte {if len (_cdb .Whole )==0{return nil ;};_cfd :=_fg .Date (1899,12,30,0,0,0,0,_fg .UTC );_fdb :=_cfd .Add (_fg .Duration (_gbbf *float64 (24*_fg .Hour )));_fdb =_bbg (_fdb );_fage :=_gff .AppendFloat (nil ,_gdb ,'f',-1,64);_adc :=make ([]byte ,0,len (_fage ));_gcfe :=0;_gg :=1;_fded :for _cee :=len (_cdb .Whole )-1;_cee >=0;_cee --{_cfa :=len (_fage )-1-_gcfe ;_aaf :=_cdb .Whole [_cee ];switch _aaf .Type {case FmtTypeDigit :if _cfa >=0{_adc =append (_adc ,_fage [_cfa ]);_gcfe ++;_gg =_cee ;}else {_adc =append (_adc ,'0');};case FmtTypeDigitOpt :if _cfa >=0{_adc =append (_adc ,_fage [_cfa ]);_gcfe ++;_gg =_cee ;}else {for _egc :=_cee ;_egc >=0;_egc --{_egbd :=_cdb .Whole [_egc ];if _egbd .Type ==FmtTypeLiteral {_adc =append (_adc ,_egbd .Literal );};};break _fded ;};case FmtTypeDollar :for _bfde :=_gcfe ;_bfde < len (_fage );_bfde ++{_adc =append (_adc ,_fage [len (_fage )-1-_bfde ]);_gcfe ++;};_adc =append (_adc ,'$');case FmtTypeComma :if !_cdb ._gac {_adc =append (_adc ,',');};case FmtTypeLiteral :_adc =append (_adc ,_aaf .Literal );case FmtTypeDate :_adc =append (_adc ,_aafb (_aacg (_fdb ,_aaf .DateTime ))...);case FmtTypeTime :_adc =append (_adc ,_aafb (_aea (_fdb ,_gbbf ,_aaf .DateTime ))...);default:_eegg .Log ("\u0075\u006e\u0073\u0075p\u0070\u006f\u0072\u0074\u0065\u0064\u0020\u0074\u0079\u0070e\u0020i\u006e\u0020\u0077\u0068\u006f\u006c\u0065 \u0025\u0076",_aaf );};};_gcad :=_aafb (_adc );if _gcfe < len (_fage )&&(_gcfe !=0||_cdb ._cbc ){_ede :=len (_fage )-_gcfe ;_bgg :=make ([]byte ,len (_gcad )+_ede );copy (_bgg ,_gcad [0:_gg ]);copy (_bgg [_gg :],_fage [0:]);copy (_bgg [_gg +_ede :],_gcad [_gg :]);_gcad =_bgg ;};if _cdb ._gac {_gae :=_dfb .Buffer {};_afe :=0;for _ge :=len (_gcad )-1;_ge >=0;_ge --{if !(_gcad [_ge ]>='0'&&_gcad [_ge ]<='9'){_afe ++;}else {break ;};};for _dfcb :=0;_dfcb < len (_gcad );_dfcb ++{_beae :=(len (_gcad )-_dfcb -_afe );if _beae %3==0&&_beae !=0&&_dfcb !=0{_gae .WriteByte (',');};_gae .WriteByte (_gcad [_dfcb ]);};_gcad =_gae .Bytes ();};return _gcad ;};const _bdg ="\u0046\u006d\u0074\u0054\u0079\u0070\u0065\u004c\u0069\u0074\u0065\u0072a\u006c\u0046\u006d\u0074\u0054\u0079\u0070\u0065\u0044\u0069\u0067\u0069\u0074\u0046\u006d\u0074\u0054y\u0070\u0065\u0044i\u0067\u0069\u0074\u004f\u0070\u0074\u0046\u006d\u0074\u0054\u0079\u0070\u0065\u0043o\u006d\u006d\u0061\u0046\u006d\u0074\u0054\u0079\u0070\u0065\u0044\u0065\u0063\u0069\u006da\u006c\u0046\u006d\u0074\u0054\u0079\u0070\u0065Pe\u0072\u0063e\u006e\u0074\u0046\u006d\u0074\u0054\u0079\u0070e\u0044\u006f\u006c\u006c\u0061\u0072\u0046\u006d\u0074Ty\u0070\u0065\u0044i\u0067\u0069\u0074\u004f\u0070\u0074\u0054\u0068\u006f\u0075\u0073\u0061n\u0064\u0073\u0046\u006d\u0074\u0054\u0079\u0070\u0065\u0055n\u0064\u0065\u0072\u0073c\u006f\u0072\u0065\u0046\u006d\u0074T\u0079\u0070\u0065\u0044\u0061\u0074\u0065\u0046\u006d\u0074\u0054y\u0070e\u0054\u0069\u006d\u0065\u0046\u006d\u0074\u0054\u0079\u0070\u0065\u0046\u0072\u0061\u0063t\u0069\u006f\u006e\u0046\u006dt\u0054\u0079\u0070\u0065\u0054e\u0078\u0074";func Parse (s string )[]Format {_acdg :=Lexer {};_acdg .Lex (_decb .NewReader (s ));_acdg ._cgf =append (_acdg ._cgf ,_acdg ._gag );return _acdg ._cgf ;};var _baf =[...]uint8 {0,14,26,41,53,67,81,94,118,135,146,157,172,183};
// NumberGeneric formats the number with the generic format which attemps to
// mimic Excel's general formatting.
2020-08-23 14:15:53 +00:00
func NumberGeneric (v float64 )string {if _daae .Abs (v )>=_abec ||_daae .Abs (v )<=_aggf &&v !=0{return _egb (v );};_ccea :=make ([]byte ,0,15);_ccea =_gff .AppendFloat (_ccea ,v ,'f',-1,64);if len (_ccea )> 11{_bfa :=_ccea [11]-'0';if _bfa >=5&&_bfa <=9{_ccea [10]++;_ccea =_ccea [0:11];_ccea =_cdbf (_ccea );};_ccea =_ccea [0:11];}else if len (_ccea )==11{if _ccea [len (_ccea )-1]=='9'{_ccea [len (_ccea )-1]++;_ccea =_cdbf (_ccea );};};_ccea =_cegf (_ccea );return string (_ccea );};func _egb (_bbf float64 )string {_fcda :=_gff .FormatFloat (_bbf ,'E',-1,64);_cad :=_gff .FormatFloat (_bbf ,'E',5,64);if len (_fcda )< len (_cad ){return _gff .FormatFloat (_bbf ,'E',2,64);};return _cad ;};
2020-08-23 14:15:53 +00:00
// FmtType is the type of a format token.
//go:generate stringer -type=FmtType
type FmtType byte ;func _aafb (_gge []byte )[]byte {for _fbdf :=0;_fbdf < len (_gge )/2;_fbdf ++{_cacb :=len (_gge )-1-_fbdf ;_gge [_fbdf ],_gge [_cacb ]=_gge [_cacb ],_gge [_fbdf ];};return _gge ;};func _aacg (_dee _fg .Time ,_bcd string )[]byte {_af :=[]byte {};_gef :=0;for _acdb :=0;_acdb < len (_bcd );_acdb ++{var _fcff string ;if _bcd [_acdb ]=='/'{_fcff =string (_bcd [_gef :_acdb ]);_gef =_acdb +1;}else if _acdb ==len (_bcd )-1{_fcff =string (_bcd [_gef :_acdb +1]);}else {continue ;};switch _fcff {case "\u0079\u0079":_af =_dee .AppendFormat (_af ,"\u0030\u0036");case "\u0079\u0079\u0079\u0079":_af =_dee .AppendFormat (_af ,"\u0032\u0030\u0030\u0036");case "\u006d":_af =_dee .AppendFormat (_af ,"\u0031");case "\u006d\u006d":_af =_dee .AppendFormat (_af ,"\u0030\u0031");case "\u006d\u006d\u006d":_af =_dee .AppendFormat (_af ,"\u004a\u0061\u006e");case "\u006d\u006d\u006d\u006d":_af =_dee .AppendFormat (_af ,"\u004aa\u006e\u0075\u0061\u0072\u0079");case "\u006d\u006d\u006dm\u006d":switch _dee .Month (){case _fg .January ,_fg .July ,_fg .June :_af =append (_af ,'J');case _fg .February :_af =append (_af ,'M');case _fg .March ,_fg .May :_af =append (_af ,'M');case _fg .April ,_fg .August :_af =append (_af ,'A');case _fg .September :_af =append (_af ,'S');case _fg .October :_af =append (_af ,'O');case _fg .November :_af =append (_af ,'N');case _fg .December :_af =append (_af ,'D');};case "\u0064":_af =_dee .AppendFormat (_af ,"\u0032");case "\u0064\u0064":_af =_dee .AppendFormat (_af ,"\u0030\u0032");case "\u0064\u0064\u0064":_af =_dee .AppendFormat (_af ,"\u004d\u006f\u006e");case "\u0064\u0064\u0064\u0064":_af =_dee .AppendFormat (_af ,"\u004d\u006f\u006e\u0064\u0061\u0079");default:_eegg .Log ("\u0075\u006e\u0073\u0075\u0070\u0070\u006f\u0072\u0074\u0065\u0064 \u0064\u0061\u0074\u0065\u0020\u0066\u006f\u0072\u006d\u0061t\u0020\u0025\u0073",_fcff );};if _bcd [_acdb ]=='/'{_af =append (_af ,'/');};};return _af ;};const _fbcb int =34;func (_faeb FmtType )String ()string {if _faeb >=FmtType (len (_baf )-1){return _fdef .Sprintf ("F\u006d\u0074\u0054\u0079\u0070\u0065\u0028\u0025\u0064\u0029",_faeb );};return _bdg [_baf [_faeb ]:_baf [_faeb +1]];};const _aac int =34;
2020-08-23 14:15:53 +00:00
// AddToken adds a format token to the format.
func (_gfg *Format )AddToken (t FmtType ,l []byte ){if _gfg ._gad {_gfg ._gad =false ;return ;};switch t {case FmtTypeDecimal :_gfg ._cbc =true ;case FmtTypeUnderscore :_gfg ._gad =true ;case FmtTypeText :_gfg .Whole =append (_gfg .Whole ,Token {Type :t });case FmtTypeDate ,FmtTypeTime :_gfg .Whole =append (_gfg .Whole ,Token {Type :t ,DateTime :string (l )});case FmtTypePercent :_gfg ._bafe =true ;t =FmtTypeLiteral ;l =[]byte {'%'};fallthrough;case FmtTypeDigitOpt :fallthrough;case FmtTypeLiteral ,FmtTypeDigit ,FmtTypeDollar ,FmtTypeComma :if l ==nil {l =[]byte {0};};for _ ,_dgd :=range l {if _gfg .IsExponential {_gfg .Exponent =append (_gfg .Exponent ,Token {Type :t ,Literal :_dgd });}else if !_gfg ._cbc {_gfg .Whole =append (_gfg .Whole ,Token {Type :t ,Literal :_dgd });}else {_gfg .Fractional =append (_gfg .Fractional ,Token {Type :t ,Literal :_dgd });};};case FmtTypeDigitOptThousands :_gfg ._gac =true ;case FmtTypeFraction :_cea :=_decb .Split (string (l ),"\u002f");if len (_cea )==2{_gfg ._ggd =true ;_gfg ._ebdg ,_ =_gff .ParseInt (_cea [1],10,64);for _ ,_age :=range _cea [1]{if _age =='?'||_age =='0'{_gfg ._fae ++;};};};default:_eegg .Log ("\u0075\u006e\u0073u\u0070\u0070\u006f\u0072t\u0065\u0064\u0020\u0070\u0068\u0020\u0074y\u0070\u0065\u0020\u0069\u006e\u0020\u0070\u0061\u0072\u0073\u0065\u0020\u0025\u0076",t );};};func _bbg (_fbdg _fg .Time )_fg .Time {_fbdg =_fbdg .UTC ();return _fg .Date (_fbdg .Year (),_fbdg .Month (),_fbdg .Day (),_fbdg .Hour (),_fbdg .Minute (),_fbdg .Second (),_fbdg .Nanosecond (),_fg .Local );};const _adb int =34;const _gfgd int =0;const (FmtTypeLiteral FmtType =iota ;FmtTypeDigit ;FmtTypeDigitOpt ;FmtTypeComma ;FmtTypeDecimal ;FmtTypePercent ;FmtTypeDollar ;FmtTypeDigitOptThousands ;FmtTypeUnderscore ;FmtTypeDate ;FmtTypeTime ;FmtTypeFraction ;FmtTypeText ;);func _aea (_agbb _fg .Time ,_gf float64 ,_bgcd string )[]byte {_cdg :=[]byte {};_beed :=0;for _feg :=0;_feg < len (_bgcd );_feg ++{var _ege string ;if _bgcd [_feg ]==':'{_ege =string (_bgcd [_beed :_feg ]);_beed =_feg +1;}else if _feg ==len (_bgcd )-1{_ege =string (_bgcd [_beed :_feg +1]);}else {continue ;};switch _ege {case "\u0064":_cdg =_agbb .AppendFormat (_cdg ,"\u0032");case "\u0068":_cdg =_agbb .AppendFormat (_cdg ,"\u0033");case "\u0068\u0068":_cdg =_agbb .AppendFormat (_cdg ,"\u0031\u0035");case "\u006d":_cdg =_agbb .AppendFormat (_cdg ,"\u0034");case "\u006d\u006d":_cdg =_agbb .AppendFormat (_cdg ,"\u0030\u0034");case "\u0073":_cdg =_agbb .Round (_fg .Second ).AppendFormat (_cdg ,"\u0035");case "\u0073\u002e\u0030":_cdg =_agbb .Round (_fg .Second /10).AppendFormat (_cdg ,"\u0035\u002e\u0030");case "\u0073\u002e\u0030\u0030":_cdg =_agbb .Round (_fg .Second /100).AppendFormat (_cdg ,"\u0035\u002e\u0030\u0030");case "\u0073\u002e\u00300\u0030":_cdg =_agbb .Round (_fg .Second /1000).AppendFormat (_cdg ,"\u0035\u002e\u00300\u0030");case "\u0073\u0073":_cdg =_agbb .Round (_fg .Second ).AppendFormat (_cdg ,"\u0030\u0035");case "\u0073\u0073\u002e\u0030":_cdg =_agbb .Round (_fg .Second /10).AppendFormat (_cdg ,"\u0030\u0035\u002e\u0030");case "\u0073\u0073\u002e0\u0030":_cdg =_agbb .Round (_fg .Second /100).AppendFormat (_cdg ,"\u0030\u0035\u002e0\u0030");case "\u0073\u0073\u002e\u0030\u0030\u0030":_cdg =_agbb .Round (_fg .Second /1000).AppendFormat (_cdg ,"\u0030\u0035\u002e\u0030\u0030\u0030");case "\u0041\u004d\u002fP\u004d":_cdg =_agbb .AppendFormat (_cdg ,"\u0050\u004d");case "\u005b\u0068\u005d":_cdg =_gff .AppendInt (_cdg ,int64 (_gf *24),10);case "\u005b\u006d\u005d":_cdg =_gff .AppendInt (_cdg ,int64 (_gf *24*60),10);case "\u005b\u0073\u005d":_cdg =_gff .AppendInt (_cdg ,int64 (_gf *24*60*60),10);case "":default:_eegg .Log ("\u0075\u006e\u0073\u0075\u0070\u0070\u006f\u0072\u0074\u0065\u0064 \u0074\u0069\u006d\u0065\u0020\u0066\u006f\u0072\u006d\u0061t\u0020\u0025\u0073",_ege );};if _bgcd [_feg ]==':'{_cdg =append (_cdg ,':');};};return _cdg ;};const _agg int =0;const _bg int =0;func IsNumber (data string )(_cge bool ){_adec ,_fdc ,_ffg :=0,0,len (data );_dbg :=len (data );_dfe ,_df ,_abdeb :=0,0,0;_ =_df ;_ =_abdeb ;_ =
2020-08-23 14:15:53 +00:00
// String returns the string formatted according to the type. In format strings
// this is the fourth item, where '@' is used as a placeholder for text.
func String (v string ,f string )string {_cg :=Parse (f );var _gda Format ;if len (_cg )==1{_gda =_cg [0];}else if len (_cg )==4{_gda =_cg [3];};_ebd :=false ;for _ ,_bcfb :=range _gda .Whole {if _bcfb .Type ==FmtTypeText {_ebd =true ;};};if !_ebd {return v ;};_dggf :=_dfb .Buffer {};for _ ,_cf :=range _gda .Whole {switch _cf .Type {case FmtTypeLiteral :_dggf .WriteByte (_cf .Literal );case FmtTypeText :_dggf .WriteString (v );};};return _dggf .String ();};const _aggf =1e-10;func _gbd (_g int64 ,_bf Format )[]byte {if !_bf .IsExponential ||len (_bf .Exponent )==0{return nil ;};_bbe :=_gff .AppendInt (nil ,_badd (_g ),10);_gaa :=make ([]byte ,0,len (_bbe )+2);_gaa =append (_gaa ,'E');if _g >=0{_gaa =append (_gaa ,'+');}else {_gaa =append (_gaa ,'-');_g *=-1;};_abe :=0;_efa :for _gbg :=len (_bf .Exponent )-1;_gbg >=0;_gbg --{_gace :=len (_bbe )-1-_abe ;_aeee :=_bf .Exponent [_gbg ];switch _aeee .Type {case FmtTypeDigit :if _gace >=0{_gaa =append (_gaa ,_bbe [_gace ]);_abe ++;}else {_gaa =append (_gaa ,'0');};case FmtTypeDigitOpt :if _gace >=0{_gaa =append (_gaa ,_bbe [_gace ]);_abe ++;}else {for _bea :=_gbg ;_bea >=0;_bea --{_ed :=_bf .Exponent [_bea ];if _ed .Type ==FmtTypeLiteral {_gaa =append (_gaa ,_ed .Literal );};};break _efa ;};case FmtTypeLiteral :_gaa =append (_gaa ,_aeee .Literal );default:_eegg .Log ("\u0075\u006e\u0073\u0075\u0070\u0070\u006f\u0072\u0074\u0065\u0064 \u0074\u0079\u0070\u0065\u0020\u0069\u006e\u0020\u0065\u0078p\u0020\u0025\u0076",_aeee );};};if _abe < len (_bbe ){_gaa =append (_gaa ,_bbe [len (_bbe )-_abe -1:_abe -1]...);};_aafb (_gaa [2:]);return _gaa ;};func _badd (_befg int64 )int64 {if _befg < 0{return -_befg ;};return _befg ;};const _bbb int =-1;