github.com/Aoi-hosizora/ahlib@v1.5.1-0.20230404072829-241b93cf91c7/xnumber/xnumber_parse.go (about)

     1  package xnumber
     2  
     3  import (
     4  	"strconv"
     5  )
     6  
     7  // =====
     8  // parse
     9  // =====
    10  
    11  // ParseInt parses a string to int using given base.
    12  func ParseInt(s string, base int) (int, error) {
    13  	i, e := strconv.ParseInt(s, base, 0)
    14  	return int(i), e
    15  }
    16  
    17  // ParseInt8 parses a string to int8 using given base.
    18  func ParseInt8(s string, base int) (int8, error) {
    19  	i, e := strconv.ParseInt(s, base, 8)
    20  	return int8(i), e
    21  }
    22  
    23  // ParseInt16 parses a string to int16 using given base.
    24  func ParseInt16(s string, base int) (int16, error) {
    25  	i, e := strconv.ParseInt(s, base, 16)
    26  	return int16(i), e
    27  }
    28  
    29  // ParseInt32 parses a string to int32 using given base.
    30  func ParseInt32(s string, base int) (int32, error) {
    31  	i, e := strconv.ParseInt(s, base, 32)
    32  	return int32(i), e
    33  }
    34  
    35  // ParseInt64 parses a string to int64 using given base.
    36  func ParseInt64(s string, base int) (int64, error) {
    37  	i, e := strconv.ParseInt(s, base, 64)
    38  	return i, e
    39  }
    40  
    41  // ParseUint parses a string to uint using given base.
    42  func ParseUint(s string, base int) (uint, error) {
    43  	u, e := strconv.ParseUint(s, base, 0)
    44  	return uint(u), e
    45  }
    46  
    47  // ParseUint8 parses a string to uint8 using given base.
    48  func ParseUint8(s string, base int) (uint8, error) {
    49  	u, e := strconv.ParseUint(s, base, 8)
    50  	return uint8(u), e
    51  }
    52  
    53  // ParseUint16 parses a string to uint16 using given base.
    54  func ParseUint16(s string, base int) (uint16, error) {
    55  	u, e := strconv.ParseUint(s, base, 16)
    56  	return uint16(u), e
    57  }
    58  
    59  // ParseUint32 parses a string to uint32 using given base.
    60  func ParseUint32(s string, base int) (uint32, error) {
    61  	u, e := strconv.ParseUint(s, base, 32)
    62  	return uint32(u), e
    63  }
    64  
    65  // ParseUint64 parses a string to uint64 using given base.
    66  func ParseUint64(s string, base int) (uint64, error) {
    67  	u, e := strconv.ParseUint(s, base, 64)
    68  	return u, e
    69  }
    70  
    71  // ParseFloat32 parses a string to float32.
    72  func ParseFloat32(s string) (float32, error) {
    73  	f, e := strconv.ParseFloat(s, 32)
    74  	return float32(f), e
    75  }
    76  
    77  // ParseFloat64 parses a string to float64.
    78  func ParseFloat64(s string) (float64, error) {
    79  	f, e := strconv.ParseFloat(s, 64)
    80  	return f, e
    81  }
    82  
    83  // =======
    84  // parseOr
    85  // =======
    86  
    87  // ParseIntOr parses a string to int using given base with a fallback value.
    88  func ParseIntOr(s string, base int, o int) int {
    89  	i, e := ParseInt(s, base)
    90  	if e != nil {
    91  		return o
    92  	}
    93  	return i
    94  }
    95  
    96  // ParseInt8Or parses a string to int8 using given base with a fallback value.
    97  func ParseInt8Or(s string, base int, o int8) int8 {
    98  	i, e := ParseInt8(s, base)
    99  	if e != nil {
   100  		return o
   101  	}
   102  	return i
   103  }
   104  
   105  // ParseInt16Or parses a string to int16 using given base with a fallback value.
   106  func ParseInt16Or(s string, base int, o int16) int16 {
   107  	i, e := ParseInt16(s, base)
   108  	if e != nil {
   109  		return o
   110  	}
   111  	return i
   112  }
   113  
   114  // ParseInt32Or parses a string to int32 using given base with a fallback value.
   115  func ParseInt32Or(s string, base int, o int32) int32 {
   116  	i, e := ParseInt32(s, base)
   117  	if e != nil {
   118  		return o
   119  	}
   120  	return i
   121  }
   122  
   123  // ParseInt64Or parses a string to int64 using given base with a fallback value.
   124  func ParseInt64Or(s string, base int, o int64) int64 {
   125  	i, e := ParseInt64(s, base)
   126  	if e != nil {
   127  		return o
   128  	}
   129  	return i
   130  }
   131  
   132  // ParseUintOr parses a string to uint using given base with a fallback value.
   133  func ParseUintOr(s string, base int, o uint) uint {
   134  	u, e := ParseUint(s, base)
   135  	if e != nil {
   136  		return o
   137  	}
   138  	return u
   139  }
   140  
   141  // ParseUint8Or parses a string to uint8 using given base with a fallback value.
   142  func ParseUint8Or(s string, base int, o uint8) uint8 {
   143  	u, e := ParseUint8(s, base)
   144  	if e != nil {
   145  		return o
   146  	}
   147  	return u
   148  }
   149  
   150  // ParseUint16Or parses a string to uint16 using given base with a fallback value.
   151  func ParseUint16Or(s string, base int, o uint16) uint16 {
   152  	u, e := ParseUint16(s, base)
   153  	if e != nil {
   154  		return o
   155  	}
   156  	return u
   157  }
   158  
   159  // ParseUint32Or parses a string to uint32 using given base with a fallback value.
   160  func ParseUint32Or(s string, base int, o uint32) uint32 {
   161  	u, e := ParseUint32(s, base)
   162  	if e != nil {
   163  		return o
   164  	}
   165  	return u
   166  }
   167  
   168  // ParseUint64Or parses a string to uint64 using given base with a fallback value.
   169  func ParseUint64Or(s string, base int, o uint64) uint64 {
   170  	u, e := ParseUint64(s, base)
   171  	if e != nil {
   172  		return o
   173  	}
   174  	return u
   175  }
   176  
   177  // ParseFloat32Or parses a string to float32 with a fallback value.
   178  func ParseFloat32Or(s string, o float32) float32 {
   179  	f, e := ParseFloat32(s)
   180  	if e != nil {
   181  		return o
   182  	}
   183  	return f
   184  }
   185  
   186  // ParseFloat64Or parses a string to float64 with a fallback value.
   187  func ParseFloat64Or(s string, o float64) float64 {
   188  	f, e := ParseFloat64(s)
   189  	if e != nil {
   190  		return o
   191  	}
   192  	return f
   193  }
   194  
   195  // ====
   196  // atoX
   197  // ====
   198  
   199  // Atoi parses a string to int in base 10.
   200  func Atoi(s string) (int, error) {
   201  	return ParseInt(s, 10)
   202  }
   203  
   204  // Atoi8 parses a string to int8 in base 10.
   205  func Atoi8(s string) (int8, error) {
   206  	return ParseInt8(s, 10)
   207  }
   208  
   209  // Atoi16 parses a string to int8 in base 10.
   210  func Atoi16(s string) (int16, error) {
   211  	return ParseInt16(s, 10)
   212  }
   213  
   214  // Atoi32 parses a string to int32 in base 10.
   215  func Atoi32(s string) (int32, error) {
   216  	return ParseInt32(s, 10)
   217  }
   218  
   219  // Atoi64 parses a string to int64 in base 10.
   220  func Atoi64(s string) (int64, error) {
   221  	return ParseInt64(s, 10)
   222  }
   223  
   224  // Atou parses a string to uint in base 10.
   225  func Atou(s string) (uint, error) {
   226  	return ParseUint(s, 10)
   227  }
   228  
   229  // Atou8 parses a string to uint8 in base 10.
   230  func Atou8(s string) (uint8, error) {
   231  	return ParseUint8(s, 10)
   232  }
   233  
   234  // Atou16 parses a string to uint16 in base 10.
   235  func Atou16(s string) (uint16, error) {
   236  	return ParseUint16(s, 10)
   237  }
   238  
   239  // Atou32 parses a string to uint32 in base 10.
   240  func Atou32(s string) (uint32, error) {
   241  	return ParseUint32(s, 10)
   242  }
   243  
   244  // Atou64 parses a string to uint64 in base 10.
   245  func Atou64(s string) (uint64, error) {
   246  	return ParseUint64(s, 10)
   247  }
   248  
   249  // Atof32 parses a string to float32, is same as ParseFloat32.
   250  func Atof32(s string) (float32, error) {
   251  	return ParseFloat32(s)
   252  }
   253  
   254  // Atof64 parses a string to float32, is same as ParseFloat64.
   255  func Atof64(s string) (float64, error) {
   256  	return ParseFloat64(s)
   257  }
   258  
   259  // ======
   260  // atoXOr
   261  // ======
   262  
   263  // AtoiOr parses a string to int in base 10 with a fallback value.
   264  func AtoiOr(s string, o int) int {
   265  	i, e := Atoi(s)
   266  	if e != nil {
   267  		return o
   268  	}
   269  	return i
   270  }
   271  
   272  // Atoi8Or parses a string to int8 in base 10 with a fallback value.
   273  func Atoi8Or(s string, o int8) int8 {
   274  	i, e := Atoi8(s)
   275  	if e != nil {
   276  		return o
   277  	}
   278  	return i
   279  }
   280  
   281  // Atoi16Or parses a string to int8 in base 10 with a fallback value.
   282  func Atoi16Or(s string, o int16) int16 {
   283  	i, e := Atoi16(s)
   284  	if e != nil {
   285  		return o
   286  	}
   287  	return i
   288  }
   289  
   290  // Atoi32Or parses a string to int32 in base 10 with a fallback value.
   291  func Atoi32Or(s string, o int32) int32 {
   292  	i, e := Atoi32(s)
   293  	if e != nil {
   294  		return o
   295  	}
   296  	return i
   297  }
   298  
   299  // Atoi64Or parses a string to int64 in base 10 with a fallback value.
   300  func Atoi64Or(s string, o int64) int64 {
   301  	i, e := Atoi64(s)
   302  	if e != nil {
   303  		return o
   304  	}
   305  	return i
   306  }
   307  
   308  // AtouOr parses a string to uint in base 10 with a fallback value.
   309  func AtouOr(s string, o uint) uint {
   310  	u, e := Atou(s)
   311  	if e != nil {
   312  		return o
   313  	}
   314  	return u
   315  }
   316  
   317  // Atou8Or parses a string to uint8 in base 10 with a fallback value.
   318  func Atou8Or(s string, o uint8) uint8 {
   319  	u, e := Atou8(s)
   320  	if e != nil {
   321  		return o
   322  	}
   323  	return u
   324  }
   325  
   326  // Atou16Or parses a string to uint16 in base 10 with a fallback value.
   327  func Atou16Or(s string, o uint16) uint16 {
   328  	u, e := Atou16(s)
   329  	if e != nil {
   330  		return o
   331  	}
   332  	return u
   333  }
   334  
   335  // Atou32Or parses a string to uint32 in base 10 with a fallback value.
   336  func Atou32Or(s string, o uint32) uint32 {
   337  	u, e := Atou32(s)
   338  	if e != nil {
   339  		return o
   340  	}
   341  	return u
   342  }
   343  
   344  // Atou64Or parses a string to uint64 in base 10 with a fallback value.
   345  func Atou64Or(s string, o uint64) uint64 {
   346  	u, e := Atou64(s)
   347  	if e != nil {
   348  		return o
   349  	}
   350  	return u
   351  }
   352  
   353  // Atof32Or parses a string to float32 with a fallback value.
   354  func Atof32Or(s string, o float32) float32 {
   355  	f, e := Atof32(s)
   356  	if e != nil {
   357  		return o
   358  	}
   359  	return f
   360  }
   361  
   362  // Atof64Or parses a string to float32 with a fallback value.
   363  func Atof64Or(s string, o float64) float64 {
   364  	f, e := Atof64(s)
   365  	if e != nil {
   366  		return o
   367  	}
   368  	return f
   369  }
   370  
   371  // ======
   372  // format
   373  // ======
   374  
   375  // FormatInt formats an int to string using given base.
   376  func FormatInt(i int, base int) string {
   377  	return strconv.FormatInt(int64(i), base)
   378  }
   379  
   380  // FormatInt8 formats an int8 to string using given base.
   381  func FormatInt8(i int8, base int) string {
   382  	return strconv.FormatInt(int64(i), base)
   383  }
   384  
   385  // FormatInt16 formats an int16 to string using given base.
   386  func FormatInt16(i int16, base int) string {
   387  	return strconv.FormatInt(int64(i), base)
   388  }
   389  
   390  // FormatInt32 formats an int32 to string using given base.
   391  func FormatInt32(i int32, base int) string {
   392  	return strconv.FormatInt(int64(i), base)
   393  }
   394  
   395  // FormatInt64 formats an int64 to string using given base.
   396  func FormatInt64(i int64, base int) string {
   397  	return strconv.FormatInt(i, base)
   398  }
   399  
   400  // FormatUint formats an uint to string using given base.
   401  func FormatUint(u uint, base int) string {
   402  	return strconv.FormatUint(uint64(u), base)
   403  }
   404  
   405  // FormatUint8 formats an uint8 to string using given base.
   406  func FormatUint8(u uint8, base int) string {
   407  	return strconv.FormatUint(uint64(u), base)
   408  }
   409  
   410  // FormatUint16 formats an uint16 to string using given base.
   411  func FormatUint16(u uint16, base int) string {
   412  	return strconv.FormatUint(uint64(u), base)
   413  }
   414  
   415  // FormatUint32 formats an uint32 to string using given base.
   416  func FormatUint32(u uint32, base int) string {
   417  	return strconv.FormatUint(uint64(u), base)
   418  }
   419  
   420  // FormatUint64 formats an uint64 to string using given base.
   421  func FormatUint64(u uint64, base int) string {
   422  	return strconv.FormatUint(u, base)
   423  }
   424  
   425  // FormatFloat32 formats a float32 to string using given format and precision.
   426  func FormatFloat32(f float32, fmt byte, prec int) string {
   427  	return strconv.FormatFloat(float64(f), fmt, prec, 32)
   428  }
   429  
   430  // FormatFloat64 formats a float64 to string using given format and precision.
   431  func FormatFloat64(f float64, fmt byte, prec int) string {
   432  	return strconv.FormatFloat(f, fmt, prec, 64)
   433  }
   434  
   435  // ====
   436  // Xtoa
   437  // ====
   438  
   439  // Itoa formats an int to string in base 10.
   440  func Itoa(i int) string {
   441  	return FormatInt(i, 10)
   442  }
   443  
   444  // I8toa formats an int8 to string in base 10.
   445  func I8toa(i int8) string {
   446  	return FormatInt8(i, 10)
   447  }
   448  
   449  // I16toa formats an int16 to string in base 10.
   450  func I16toa(i int16) string {
   451  	return FormatInt16(i, 10)
   452  }
   453  
   454  // I32toa formats an int32 to string in base 10.
   455  func I32toa(i int32) string {
   456  	return FormatInt32(i, 10)
   457  }
   458  
   459  // I64toa formats an int64 to string in base 10.
   460  func I64toa(i int64) string {
   461  	return FormatInt64(i, 10)
   462  }
   463  
   464  // Utoa formats an uint to string in base 10.
   465  func Utoa(u uint) string {
   466  	return FormatUint(u, 10)
   467  }
   468  
   469  // U8toa formats an uint8 to string in base 10.
   470  func U8toa(u uint8) string {
   471  	return FormatUint8(u, 10)
   472  }
   473  
   474  // U16toa formats an uint16 to string in base 10.
   475  func U16toa(u uint16) string {
   476  	return FormatUint16(u, 10)
   477  }
   478  
   479  // U32toa formats an uint32 to string in base 10.
   480  func U32toa(u uint32) string {
   481  	return FormatUint32(u, 10)
   482  }
   483  
   484  // U64toa formats an uint64 to string in base 10.
   485  func U64toa(u uint64) string {
   486  	return FormatUint64(u, 10)
   487  }
   488  
   489  // F32toa formats a float32 to string using default format.
   490  func F32toa(f float32) string {
   491  	return FormatFloat32(f, 'f', -1)
   492  }
   493  
   494  // F64toa formats a float64 to string using default format.
   495  func F64toa(f float64) string {
   496  	return FormatFloat64(f, 'f', -1)
   497  }