oooooo close
parent
4daf580b1b
commit
49f688f40e
|
|
@ -2,10 +2,10 @@ package ledger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -129,8 +129,12 @@ func readTransaction(r io.Reader) (io.Reader, transaction, error) {
|
||||||
newR, recipient, err := readTransactionRecipient(r)
|
newR, recipient, err := readTransactionRecipient(r)
|
||||||
r = newR
|
r = newR
|
||||||
if !recipient.empty() {
|
if !recipient.empty() {
|
||||||
|
if recipient.currency == "" {
|
||||||
|
result.payee = recipient.name
|
||||||
|
} else {
|
||||||
result.recipients = append(result.recipients, recipient)
|
result.recipients = append(result.recipients, recipient)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if err != nil || recipient.empty() {
|
if err != nil || recipient.empty() {
|
||||||
return r, result, err
|
return r, result, err
|
||||||
}
|
}
|
||||||
|
|
@ -226,6 +230,7 @@ func readTransactionRecipient(r io.Reader) (io.Reader, transactionRecipient, err
|
||||||
if isSpaceByte(b) && b != '\n' {
|
if isSpaceByte(b) && b != '\n' {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
r = io.MultiReader(bytes.NewReader([]byte{b}), r)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -259,5 +264,52 @@ func isNumericByte(b byte) bool {
|
||||||
|
|
||||||
// from "[^\n]*\n?.*" read "[^\n]*\n?"
|
// from "[^\n]*\n?.*" read "[^\n]*\n?"
|
||||||
func readTransactionValueCurrency(r io.Reader) (io.Reader, float64, string, error) {
|
func readTransactionValueCurrency(r io.Reader) (io.Reader, float64, string, error) {
|
||||||
return nil, 0, "", errors.New("not impl read a value currency")
|
restOfLine := make([]byte, 0, 16)
|
||||||
|
for {
|
||||||
|
b, err := readOne(r)
|
||||||
|
if b == '\n' || err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return r, 0, "", err
|
||||||
|
}
|
||||||
|
restOfLine = append(restOfLine, b)
|
||||||
|
}
|
||||||
|
restOfLine = bytes.TrimSpace(restOfLine)
|
||||||
|
if len(restOfLine) == 0 {
|
||||||
|
return r, 0, "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
firstNonNumericIdx := -1
|
||||||
|
firstNumericIdx := -1
|
||||||
|
for i := range restOfLine {
|
||||||
|
if !isNumericByte(restOfLine[i]) && firstNonNumericIdx == -1 {
|
||||||
|
firstNonNumericIdx = i
|
||||||
|
}
|
||||||
|
if isNumericByte(restOfLine[i]) && firstNumericIdx == -1 {
|
||||||
|
firstNumericIdx = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if firstNonNumericIdx == -1 || firstNumericIdx == -1 {
|
||||||
|
return r, 0, "", fmt.Errorf("needed numeric and non-numeric bytes in %q", restOfLine)
|
||||||
|
}
|
||||||
|
|
||||||
|
var valueS, currency string
|
||||||
|
if firstNonNumericIdx < firstNumericIdx {
|
||||||
|
currency = string(restOfLine[firstNonNumericIdx:firstNumericIdx])
|
||||||
|
valueS = string(restOfLine[firstNumericIdx:])
|
||||||
|
} else {
|
||||||
|
valueS = string(restOfLine[firstNumericIdx:firstNonNumericIdx])
|
||||||
|
currency = string(restOfLine[firstNonNumericIdx:])
|
||||||
|
}
|
||||||
|
valueS = strings.TrimSpace(valueS)
|
||||||
|
currency = strings.TrimSpace(currency)
|
||||||
|
|
||||||
|
value, err := strconv.ParseFloat(valueS, 64)
|
||||||
|
if err != nil {
|
||||||
|
return r, 0, "", fmt.Errorf("value %q is not a float: %w", valueS, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r, value, currency, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue