mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
[core-utils] introduce gnc:format
(gnc:format str [binding value]...)
str will contain ${binding} which will be replaced to value.
This commit is contained in:
@@ -32,6 +32,7 @@
|
|||||||
(use-modules (srfi srfi-26))
|
(use-modules (srfi srfi-26))
|
||||||
(use-modules (ice-9 match))
|
(use-modules (ice-9 match))
|
||||||
(use-modules (ice-9 i18n))
|
(use-modules (ice-9 i18n))
|
||||||
|
(use-modules (ice-9 regex))
|
||||||
|
|
||||||
(export N_)
|
(export N_)
|
||||||
(export G_)
|
(export G_)
|
||||||
@@ -41,6 +42,7 @@
|
|||||||
(export gnc:string-locale<?)
|
(export gnc:string-locale<?)
|
||||||
(export gnc:string-locale>?)
|
(export gnc:string-locale>?)
|
||||||
(export gnc:version)
|
(export gnc:version)
|
||||||
|
(export gnc:format)
|
||||||
|
|
||||||
;; loads modules and re-exports all its public interface into the
|
;; loads modules and re-exports all its public interface into the
|
||||||
;; current module
|
;; current module
|
||||||
@@ -107,3 +109,16 @@
|
|||||||
(_ (default-printer))))
|
(_ (default-printer))))
|
||||||
|
|
||||||
(set-exception-printer! 'unbound-variable print-unbound-variable-error)
|
(set-exception-printer! 'unbound-variable print-unbound-variable-error)
|
||||||
|
|
||||||
|
;; format.
|
||||||
|
(define %regex (make-regexp "[$][{]([[:alnum:]]+)[}]"))
|
||||||
|
(define (gnc:format str . bindings)
|
||||||
|
(define hash (make-hash-table))
|
||||||
|
(define (substitute m)
|
||||||
|
(or (hashq-ref hash (string->symbol (match:substring m 1)))
|
||||||
|
(warn "invalid identifier" (match:substring m 0))))
|
||||||
|
(let lp ((bindings bindings))
|
||||||
|
(match bindings
|
||||||
|
(() (regexp-substitute/global #f %regex str 'pre substitute 'post))
|
||||||
|
(((? symbol? k) v . rest) (hashq-set! hash k (format #f "~a" v)) (lp rest))
|
||||||
|
(_ (error "gnc:format syntax error")))))
|
||||||
|
|||||||
@@ -13,8 +13,37 @@
|
|||||||
"foobar"
|
"foobar"
|
||||||
(N_ "foobar")))
|
(N_ "foobar")))
|
||||||
|
|
||||||
|
(define (gnc-format-tests)
|
||||||
|
(test-equal "null"
|
||||||
|
""
|
||||||
|
(gnc:format ""))
|
||||||
|
|
||||||
|
(test-equal "basic"
|
||||||
|
"basic"
|
||||||
|
(gnc:format "basic"))
|
||||||
|
|
||||||
|
(test-equal "basic with unused symbols"
|
||||||
|
"basic"
|
||||||
|
(gnc:format "basic" 'task "testing"))
|
||||||
|
|
||||||
|
(test-equal "one substitution"
|
||||||
|
"basic test"
|
||||||
|
(gnc:format "basic ${job}" 'job "test"))
|
||||||
|
|
||||||
|
(test-equal "two substitutions out of order"
|
||||||
|
"basic test"
|
||||||
|
(gnc:format "${difficulty} ${job}" 'job "test" 'difficulty "basic"))
|
||||||
|
|
||||||
|
(test-equal "trying to reference invalid symbol"
|
||||||
|
"${symbol} does not exist"
|
||||||
|
(gnc:format "${symbol} does not exist" 'existence "none"))
|
||||||
|
|
||||||
|
(test-error "gnc:format syntax error"
|
||||||
|
(gnc:format "${symbol} does not exist" 'existence)))
|
||||||
|
|
||||||
(define (run-test)
|
(define (run-test)
|
||||||
(test-runner-factory gnc:test-runner)
|
(test-runner-factory gnc:test-runner)
|
||||||
(test-begin "test-core-utils")
|
(test-begin "test-core-utils")
|
||||||
(N_-tests)
|
(N_-tests)
|
||||||
|
(gnc-format-tests)
|
||||||
(test-end "test-core-utils"))
|
(test-end "test-core-utils"))
|
||||||
|
|||||||
Reference in New Issue
Block a user