clmconv

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2024 License: MIT Imports: 1 Imported by: 9

README

clmconv

GoDoc CI codecov MIT License

A golang package for converting to spreadsheet column alphabet or integer.

Usage

Default converter
i, err := clmconv.Atoi("A") // i = 0
i := clmconv.MustAtoi("a") // i = 0
a := clmconv.Itoa(0) // a = "A"
Custom converter
converter := clmconv.New(clmconv.WithStartFromOne(), clmconv.WithLowercase())
i, err := converter.Atoi("A") // i = 1
i := converter.MustAtoi("a") // i = 1
a := converter.Itoa(1) // a = "a"

Example

Alphabet Integer Integer (WithStartFromOne)
A 0 1
B 1 2
Z 25 26
AA 26 27
ZZ 701 702
ABC 730 731
ABCDE 494264 494265

References

Documentation

Overview

Package clmconv converts spreadsheet column alphabet to integer and vice versa.

Example
package main

import (
	"fmt"

	"github.com/takuoki/clmconv"
)

func main() {

	// Default converter
	i, _ := clmconv.Atoi("A")
	a := clmconv.Itoa(0)
	fmt.Printf("i=%d, a='%s'\n", i, a)

	// Custom converter
	converter := clmconv.New(clmconv.WithStartFromOne(), clmconv.WithLowercase())
	i, _ = converter.Atoi("A")
	a = converter.Itoa(1)
	fmt.Printf("i=%d, a='%s'\n", i, a)

}
Output:

i=0, a='A'
i=1, a='a'

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Atoi

func Atoi(s string) (int, error)

Atoi converts alphabet to number.

Example
package main

import (
	"fmt"

	"github.com/takuoki/clmconv"
)

func main() {
	ai, _ := clmconv.Atoi("A")
	bi, _ := clmconv.Atoi("B")
	_, err := clmconv.Atoi("error!")
	fmt.Printf("ai=%d, bi=%d, err='%v'", ai, bi, err)

}
Output:

ai=0, bi=1, err='must not contain non-alphabetic characters'

func Itoa

func Itoa(i int) string

Itoa converts number to alphabet. If argument is negative, returns empty string.

Example
package main

import (
	"fmt"

	"github.com/takuoki/clmconv"
)

func main() {
	a0 := clmconv.Itoa(0)
	a1 := clmconv.Itoa(1)
	an := clmconv.Itoa(-1)
	fmt.Printf("a0='%s', a1='%s', an='%s'", a0, a1, an)

}
Output:

a0='A', a1='B', an=''

func MustAtoi

func MustAtoi(s string) int

MustAtoi converts alphabet to number. In case of invalid argument, throws panic.

Example
package main

import (
	"fmt"

	"github.com/takuoki/clmconv"
)

func main() {
	ai := clmconv.MustAtoi("A")
	v := func() (v interface{}) {
		defer func() {
			if p := recover(); p != nil {
				v = p
			}
		}()
		clmconv.MustAtoi("panic!")
		return nil
	}()
	fmt.Printf("ai=%d, v='%v'", ai, v)

}
Output:

ai=0, v='must not contain non-alphabetic characters'

Types

type Converter added in v1.1.0

type Converter struct {
	// contains filtered or unexported fields
}

Converter represents a conversion object. If you need a conversion other than the default, you need create a new conversion object.

func New added in v1.1.0

func New(opts ...Option) *Converter

New creates a new Converter.

func (*Converter) Atoi added in v1.1.0

func (c *Converter) Atoi(s string) (int, error)

Atoi converts alphabet to number with converter.

func (*Converter) Itoa added in v1.1.0

func (c *Converter) Itoa(i int) string

Itoa converts number to alphabet with converter. If argument is negative, returns empty string.

func (*Converter) MustAtoi added in v1.1.0

func (c *Converter) MustAtoi(s string) int

MustAtoi converts alphabet to number with converter. In case of invalid argument, throws panic.

type Option added in v1.1.0

type Option interface {
	// contains filtered or unexported methods
}

Option is an option that configures Converter.

func WithLowercase added in v1.1.0

func WithLowercase() Option

WithLowercase is an option to use lowercase letters as the output alphabet. This option works only with `Itoa`.

func WithStartFromOne added in v1.1.0

func WithStartFromOne() Option

WithStartFromOne is an option to start the index from one. This option works for both `Atoi` and `Itoa`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL