all repos

clerk @ 3bf8ce6

missing tooling for ledger/hledger
1 files changed, 18 insertions(+), 27 deletions(-)
parser: simplify detectFormat
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2026-06-22 18:00:13 +0300
Authored at: 2026-06-17 15:49:35 +0300
Change ID: yupuluknkwzovxmrqpxwwqornvopzrlz
Parent: 86eedb4
M journal/parser/parser.go
ยทยทยท
        1306
        1306
         }

      
        1307
        1307
         

      
        1308
        1308
         func detectFormat(lit string) ast.QuantityFormat {

      
        1309
        
        -	var separators []int

      
        
        1309
        +	var seps []int

      
        1310
        1310
         	for i, ch := range []byte(lit) {

      
        1311
        1311
         		if ch == '.' || ch == ',' || ch == ' ' || ch == '_' || ch == '\'' {

      
        1312
        
        -			separators = append(separators, i)

      
        
        1312
        +			seps = append(seps, i)

      
        1313
        1313
         		}

      
        1314
        1314
         	}

      
        1315
        1315
         

      
        1316
        
        -	if len(separators) == 0 {

      
        
        1316
        +	if len(seps) == 0 {

      
        1317
        1317
         		return ast.QuantityFormat{Decimal: '.', Thousands: 0, Precision: 0}

      
        1318
        1318
         	}

      
        1319
        1319
         

      
        1320
        
        -	var decimal byte

      
        1321
        
        -	thousands := byte(0)

      
        1322
        
        -	precision := 0

      
        
        1320
        +	last := seps[len(seps)-1]

      
        
        1321
        +	dec := lit[last]

      
        
        1322
        +	var thou byte

      
        
        1323
        +	if len(seps) > 1 {

      
        
        1324
        +		thou = lit[seps[0]]

      
        
        1325
        +	} else if dec == ' ' || dec == '_' || dec == '\'' {

      
        
        1326
        +		// single space/underscore/apostrophe is always thousands

      
        
        1327
        +		thou = dec

      
        
        1328
        +		dec = '.'

      
        
        1329
        +	}

      
        1323
        1330
         

      
        1324
        
        -	if len(separators) == 1 {

      
        1325
        
        -		pos := separators[0]

      
        1326
        
        -		sepChar := lit[pos]

      
        1327
        
        -		if sepChar == ' ' || sepChar == '_' || sepChar == '\'' {

      
        1328
        
        -			thousands = sepChar

      
        1329
        
        -			decimal = '.' // default

      
        1330
        
        -			precision = 0

      
        1331
        
        -		} else {

      
        1332
        
        -			decimal = sepChar

      
        1333
        
        -			precision = len(lit) - pos - 1

      
        1334
        
        -		}

      
        1335
        
        -	} else {

      
        1336
        
        -		last := separators[len(separators)-1]

      
        1337
        
        -		decimal = lit[last]

      
        1338
        
        -		thousands = lit[separators[0]]

      
        1339
        
        -		precision = len(lit) - last - 1

      
        
        1331
        +	// calculate precision when the last separator is a real decimal

      
        
        1332
        +	prec := 0

      
        
        1333
        +	if thou == 0 || len(seps) > 1 {

      
        
        1334
        +		prec = len(lit) - last - 1

      
        1340
        1335
         	}

      
        1341
        1336
         

      
        1342
        
        -	return ast.QuantityFormat{

      
        1343
        
        -		Decimal:   decimal,

      
        1344
        
        -		Thousands: thousands,

      
        1345
        
        -		Precision: precision,

      
        1346
        
        -	}

      
        
        1337
        +	return ast.QuantityFormat{Decimal: dec, Thousands: thou, Precision: prec}

      
        1347
        1338
         }

      
        1348
        1339
         

      
        1349
        1340
         func parseSimpleDate(s string) ast.Date {