8000 feat: introduce CellFrom/ToString by justinhwang · Pull Request #95 · uber/h3-go · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: introduce CellFrom/ToString #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions h3.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ func (c Cell) BaseCellNumber() int {
return BaseCellNumber(c)
}

// IndexFromString returns a Cell from a string. Should call c.IsValid() to check
// IndexFromString returns an uint64 from a string. Should call c.IsValid() to check
// if the Cell is valid before using it.
func IndexFromString(s string) uint64 {
if len(s) > 2 && strings.ToLower(s[:2]) == "0x" {
Expand All @@ -751,15 +751,25 @@ func IndexFromString(s string) uint64 {
return c
}

// IndexToString returns a Cell from a string. Should call c.IsValid() to check
// if the Cell is valid before using it.
// IndexToString returns a string from a Cell.
func IndexToString(i uint64) string {
return strconv.FormatUint(i, base16)
}

// CellFromString returns a Cell from a string. Should call c.IsValid() to check
// if the Cell is valid before using it.
func CellFromString(s string) Cell {
return Cell(IndexFromString(s))
}

// CellToString returns a string from a Cell.
func CellToString(c Cell) string {
return IndexToString(uint64(c))
}

// String returns the string representation of the H3Index h.
func (c Cell) String() string {
return IndexToString(uint64(c))
return CellToString(c)
}

// MarshalText implements the encoding.TextMarshaler interface.
Expand Down
6 changes: 6 additions & 0 deletions h3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@ func TestStrings(t *testing.T) {
t.Parallel()
i := IndexFromString("oops")
assertEqual(t, 0, i)

c := CellFromString("oops")
assertEqual(t, 0, c)
})

t.Run("good string round trip", func(t *testing.T) {
Expand All @@ -666,6 +669,9 @@ func TestStrings(t *testing.T) {

//nolint:gosec // IndexFromString returns uint64 and fixing that to detect integer overflows will break package API. Let's skip it for now.
assertEqual(t, validCell, Cell(i))

c := CellFromString(validCell.String())
assertEqual(t, validCell, c)
})

t.Run("no 0x prefix", func(t *testing.T) {
Expand Down
Loading
0