Add support for (inefficiently) getting uppercase return values

This commit is contained in:
2024-10-26 23:54:18 -04:00
parent afad2b7ee8
commit 63f3025096
3 changed files with 26 additions and 7 deletions
+7 -4
View File
@@ -2,7 +2,10 @@
This package was adapted from [romannumeral](https://github.com/brandenc40/romannumeral) to fit my specific minimal use-case.
All functionality except for the ability to convert integers to **lowercase** roman numerals has been removed.
All functionality except for the ability to convert integers to roman numerals has been removed.
Since I have less functionality to worry about, I was also able to implement vinculum notation support to extend the range of supported integers to 3,999,999.
I also added the ability to choose between uppercase and lowercase roman numerals (optimized for lowercase due to my personal use-case).
If you need any additional functionality or further documentation, please see the original package.
@@ -28,11 +31,11 @@ import (
rom "github.com/rwinkhart/convertroman"
)
func ExampleFromInt() {
func main() {
roman := FromInt(4)
if roman == "OOB" {
panic("Input integer is out of bounds")
panic("Input integer is out of bounds (greater than 3,999,999)")
}
fmt.Println(roman == "IV") // True
fmt.Println(roman == "iv") // True
}
```
+17 -1
View File
@@ -1,5 +1,7 @@
package convertroman
import "strings"
var (
// lookup arrays used for converting from an int to a roman numeral extremely quickly.
// method from here: https://rosettacode.org/wiki/Roman_numerals/Encode#Go
@@ -14,7 +16,6 @@ var (
// The function will return "OOB" if the input integer is greater than
// or equal to 4,000,000 (the roman numeral vinculum limit).
func FromInt(input int) string {
// check the range of the input integer
if input < 1 {
return "0"
} else if input < 4000 {
@@ -27,6 +28,21 @@ func FromInt(input int) string {
}
}
// FromIntCapital very inefficiently converts an integer value to a CAPITAL roman numeral string.
// Note that this package is optimized for lowercase roman numerals. As such, this function is a slow afterthought.
func FromIntCapital(input int) string {
if input < 4000 {
// if below vinculum range, return the translated result
return strings.ToUpper(FromInt(input))
} else {
// if in vinculum range, remove the ANSI escape codes, capitalize the result, and reapply the ANSI escape codes
noPrefix := strings.TrimPrefix(FromInt(input), "\033[53m")
secondIndex := strings.Index(noPrefix, "\033[0m")
capitalPreANSI := strings.ToUpper(noPrefix[:secondIndex] + noPrefix[secondIndex+4:])
return "\033[53m" + capitalPreANSI[:secondIndex] + "\033[0m" + capitalPreANSI[secondIndex:]
}
}
// separateThousands separates an integer into two parts: the thousands and the remainder.
func separateThousands(num int) (int, int) {
return num / 1000, num % 1000
+2 -2
View File
@@ -53,7 +53,7 @@ func BenchmarkFromInt(b *testing.B) {
func ExampleFromInt() {
roman := FromInt(4)
if roman == "OOB" {
panic("Input integer is out of bounds")
panic("Input integer is out of bounds (greater than 3,999,999)")
}
fmt.Println(roman == "IV") // True
fmt.Println(roman == "iv") // True
}