util: Handle negative values for NumToAlpha
This shouldn't be used often, but it makes writing some kinds of code easier.
This commit is contained in:
@@ -33,6 +33,9 @@ import (
|
|||||||
// you specify 0, you'll get `a`, 25 gives you `z`, and 26 gives you `aa` and so
|
// you specify 0, you'll get `a`, 25 gives you `z`, and 26 gives you `aa` and so
|
||||||
// on...
|
// on...
|
||||||
func NumToAlpha(idx int) string {
|
func NumToAlpha(idx int) string {
|
||||||
|
if idx < 0 {
|
||||||
|
return "" // don't error or produce junk
|
||||||
|
}
|
||||||
var mod = idx % 26
|
var mod = idx % 26
|
||||||
var div = idx / 26
|
var div = idx / 26
|
||||||
if div > 0 {
|
if div > 0 {
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ func TestNumToAlpha(t *testing.T) {
|
|||||||
{702, "aaa"},
|
{702, "aaa"},
|
||||||
{703, "aab"},
|
{703, "aab"},
|
||||||
{63269, "cool"},
|
{63269, "cool"},
|
||||||
|
{-1, ""},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range numToAlphaTests {
|
for _, test := range numToAlphaTests {
|
||||||
|
|||||||
Reference in New Issue
Block a user