@@ -21,6 +21,7 @@ import (
2121 "errors"
2222 "fmt"
2323 "io"
24+ "slices"
2425 "strings"
2526
2627 "github.com/cgi-fr/posimap/pkg/posimap/api"
@@ -38,11 +39,12 @@ type Comp3 struct {
3839 intDigits int
3940 decDigits int
4041 signed bool
41- size int // number of bytes needed to store the COMP-3 value
42- length int // number of characters in the string representation (not counting the sign)
42+ size int // number of bytes needed to store the COMP-3 value
43+ length int // number of characters in the string representation (not counting the sign)
44+ sep rune // decimal separator to use in the string representation while decoding comp-3
4345}
4446
45- func NewComp3 (intDigits , decDigits int , signed bool ) * Comp3 {
47+ func NewComp3 (intDigits , decDigits int , signed bool , sep rune ) * Comp3 {
4648 length := intDigits + decDigits
4749 if decDigits > 0 {
4850 length ++
@@ -54,6 +56,7 @@ func NewComp3(intDigits, decDigits int, signed bool) *Comp3 {
5456 signed : signed ,
5557 size : (intDigits + decDigits + 2 ) / 2 , //nolint:mnd
5658 length : length ,
59+ sep : sep ,
5760 }
5861}
5962
@@ -77,7 +80,7 @@ func (c *Comp3) Decode(buffer api.Buffer, offset int) (any, error) {
7780
7881 for byteIndex , byteVal := range bytes {
7982 if byteIndex * 2 == c .intDigits {
80- result .WriteRune ('.' )
83+ result .WriteRune (c . sep )
8184 }
8285
8386 if byteIndex == c .size - 1 {
@@ -98,7 +101,7 @@ func (c *Comp3) Decode(buffer api.Buffer, offset int) (any, error) {
98101 result .WriteRune (convertNibbleToRune ((byteVal & highNibbleMask ) >> nibbleShift ))
99102
100103 if byteIndex * 2 + 1 == c .intDigits {
101- result .WriteRune ('.' )
104+ result .WriteRune (c . sep )
102105 }
103106
104107 result .WriteRune (convertNibbleToRune (byteVal & lowNibbleMask ))
@@ -113,13 +116,14 @@ func (c *Comp3) Encode(buffer api.Buffer, offset int, value any) error {
113116 return err
114117 }
115118
116- if c .decDigits > 0 && str [c .intDigits ] != '.' {
119+ // accept both '.' and ',' as valid decimal separator while encoding ensure robustness
120+ if c .decDigits > 0 && str [c .intDigits ] != '.' && str [c .intDigits ] != ',' {
117121 return fmt .Errorf ("%w: expected decimal separator at position %d, got %q" ,
118122 ErrMisplacedDecimalSep , c .intDigits , str [c .intDigits ])
119123 }
120124
121125 // ensure that there is only one decimal separator
122- if strings .Count (str , "." ) > 1 {
126+ if strings .Count (str , "." )+ strings . Count ( str , "," ) > 1 {
123127 return fmt .Errorf ("%w: too many decimal separators in COMP-3 encoding" , ErrMisplacedDecimalSep )
124128 }
125129
@@ -171,7 +175,7 @@ func (c *Comp3) encode(buffer api.Buffer, offset int, str string, nibbleSign byt
171175 return fmt .Errorf ("%w: too many characters in COMP-3 encoding" , ErrBufferTooShort )
172176 }
173177
174- if char == '.' {
178+ if slices . Contains ([] rune { '.' , ',' }, char ) {
175179 if charIndex - ignoreCount != c .intDigits {
176180 return fmt .Errorf ("%w" , ErrMisplacedDecimalSep )
177181 }
0 commit comments