Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattverse committed Feb 22, 2024
1 parent 54e29ff commit fe75550
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 42 deletions.
76 changes: 41 additions & 35 deletions types/coin_regex.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions types/coin_regex.rl
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ func MatchDenom(data []byte) bool {
return false
}

func MatchDecCoin(data []byte) (amountStart, amountEnd, denomEnd int, isValid bool) {
func MatchDecCoin(data []byte) (amountStart, amountEnd, denomStart, denomEnd int, isValid bool) {

%% machine dec_coin;
%% write data;

// Initialize positions and validity flag
amountStart, amountEnd, denomEnd = -1, -1, -1
amountStart, amountEnd, denomStart, denomEnd = -1, -1, -1, -1
isValid = false

// Ragel state variables
Expand All @@ -57,6 +58,9 @@ func MatchDecCoin(data []byte) (amountStart, amountEnd, denomEnd int, isValid bo
action EndAmount {
amountEnd = p;
}
action StartDenom {
denomStart = p-1;
}
action EndDenom {
denomEnd = p-1; // Adjusted to exclude space if present
}
Expand All @@ -68,7 +72,8 @@ func MatchDecCoin(data []byte) (amountStart, amountEnd, denomEnd int, isValid bo

dec_amt = (digit+ >StartAmount ('.' digit+)? %EndAmount) | ('.' >StartAmount digit+ %EndAmount);

denom = [a-zA-Z] (alnum | special){2,127} %EndDenom;

denom = [a-zA-Z] (alnum | special){2,127} >StartDenom %EndDenom;

main := dec_amt (space* denom)?;

Expand All @@ -79,5 +84,5 @@ func MatchDecCoin(data []byte) (amountStart, amountEnd, denomEnd int, isValid bo
isValid = (cs >= %%{ write first_final; }%%);

// Return the captured positions and validity
return amountStart, amountEnd, denomEnd, isValid
return amountStart, amountEnd, denomStart, denomEnd, isValid
}
6 changes: 3 additions & 3 deletions types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,15 +631,15 @@ func (coins DecCoins) Sort() DecCoins {
// invalid. An empty string is considered invalid.
func ParseDecCoin(coinStr string) (coin DecCoin, err error) {
coinStr = strings.TrimSpace(coinStr)
amountStart, amountEnd, denomEnd, isValid := MatchDecCoin([]byte(coinStr))
amountStart, amountEnd, denomStart, denomEnd, isValid := MatchDecCoin([]byte(coinStr))

if !isValid {
return DecCoin{}, fmt.Errorf("invalid decimal coin expression: %s", coinStr)
}

// Extract amount and denomination using the positions returned by MatchDecCoin
amountStr := coinStr[amountStart:amountEnd]
denomStr := coinStr[amountEnd : denomEnd+1]
amountStr := coinStr[amountStart:amountEnd] // Adjusted to include amountEnd
denomStr := coinStr[denomStart : denomEnd+1]

amount, err := math.LegacyNewDecFromStr(amountStr)
if err != nil {
Expand Down

0 comments on commit fe75550

Please sign in to comment.