From ba551e3fadddcfebf8355fb1bfa0352197b9e339f20c5ad38958e7c19806354d Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sun, 6 Oct 2024 11:19:17 +0300 Subject: [PATCH] More convenient namespace --- tyac/test-vector.tcl | 7 ++- tyac/tyac.tcl | 135 ++++++++++++++++++++++--------------------- 2 files changed, 72 insertions(+), 70 deletions(-) diff --git a/tyac/test-vector.tcl b/tyac/test-vector.tcl index 22813c8..bd508dd 100644 --- a/tyac/test-vector.tcl +++ b/tyac/test-vector.tcl @@ -1,4 +1,5 @@ source tyac.tcl +namespace import YAC::* MAP { ints {MAP { @@ -24,7 +25,7 @@ MAP { }} }} floats {LIST { - {Raw [expr 0x11] [binary decode hex "01020304"]} + {RAW [expr 0x11] [binary decode hex "01020304"]} }} nil NIL bool {LIST {TRUE FALSE}} @@ -50,7 +51,7 @@ MAP { {MAP {}} {BLOB 123 ""} {UUID "00000000-0000-0000-0000-000000000000"} - {Raw [expr 0x18] [binary decode hex "0000000000000000"]} + {RAW [expr 0x18] [binary decode hex "0000000000000000"]} }} dates {LIST { {TAI64 1234567890} @@ -61,4 +62,4 @@ MAP { uuid {UUID 0e875e3f-d385-49eb-87b4-be42d641c367} } -puts [binary encode hex [YACBufGet]] +puts [binary encode hex $::YAC::buf] diff --git a/tyac/tyac.tcl b/tyac/tyac.tcl index 52a55cc..7ed9d8b 100644 --- a/tyac/tyac.tcl +++ b/tyac/tyac.tcl @@ -13,30 +13,33 @@ # You should have received a copy of the GNU Lesser General Public # License along with this program. If not, see . -set YACBuf {} +namespace eval YAC { -proc yacAdd {v} { - global YACBuf - lappend YACBuf [binary format c $v] +variable buf {} + +proc add {v} { + variable buf + set buf [string cat $buf $v] } -proc EOC {} { yacAdd [expr 0x00] } -proc NIL {} { yacAdd [expr 0x01] } -proc FALSE {} { yacAdd [expr 0x02] } -proc TRUE {} { yacAdd [expr 0x03] } +proc char {v} { add [binary format c $v] } + +proc EOC {} { char [expr 0x00] } +proc NIL {} { char [expr 0x01] } +proc FALSE {} { char [expr 0x02] } +proc TRUE {} { char [expr 0x03] } proc UUID {v} { set v [binary decode hex [string map {- ""} $v]] if {[string length $v] != 16} { error "bad UUID len" } - yacAdd [expr 0x04] - global YACBuf - lappend YACBuf $v + char [expr 0x04] + add $v } proc toBE {l v} { for {set i 0} {$i < $l} {incr i} { set b [expr {($l - $i - 1) * 8}] - yacAdd [expr {($v & (0xFF << $b)) >> $b}] + char [expr {($v & (0xFF << $b)) >> $b}] } } @@ -48,9 +51,9 @@ proc INT {v} { } if {$v < 32} { if {$neg} { - yacAdd [expr {0x60 | $v}] + char [expr {0x60 | $v}] } { - yacAdd [expr {0x40 | $v}] + char [expr {0x40 | $v}] } return } @@ -62,7 +65,7 @@ proc INT {v} { incr l incr bits 8 } - yacAdd [expr {$b | $l}] + char [expr {$b | $l}] toBE [expr {$l + 1}] $v } @@ -80,22 +83,21 @@ proc _str {atom v} { set lv 61 set ll 1 } - yacAdd [expr {$atom | $lv}] + char [expr {$atom | $lv}] if {$ll > 0} { toBE $ll $vl } - global YACBuf - lappend YACBuf $v + add $v } -proc STR {v} { _str [expr {0x80 | 0x40}] [encoding convertto utf-8 $v]} proc BIN {v} { _str [expr 0x80] $v} +proc STR {v} { _str [expr {0x80 | 0x40}] [encoding convertto utf-8 $v]} proc LIST {v} { - yacAdd [expr 0x08] + char [expr 0x08] foreach i $v { eval $i } EOC } -proc lenFirstSort {a b} { +proc LenFirstSort {a b} { set a [encoding convertto utf-8 $a] set b [encoding convertto utf-8 $b] set al [string length $a] @@ -120,8 +122,8 @@ proc MAP {pairs} { lappend keys $k dict set d $k $v } - set keys [lsort -command lenFirstSort $keys] - yacAdd [expr 0x09] + set keys [lsort -command LenFirstSort $keys] + char [expr 0x09] foreach k $keys { STR $k eval [dict get $d $k] @@ -130,14 +132,13 @@ proc MAP {pairs} { } proc BLOB {chunkLen v} { - yacAdd [expr 0x0B] + char [expr 0x0B] INT $chunkLen set vl [string length $v] set chunks [expr {$vl / $chunkLen}] - global YACBuf for {set i 0} {$i < $chunks} {incr i} { NIL - lappend YACBuf [string range $v \ + add [string range $v \ [expr {$i * $chunkLen}] \ [expr {(($i + 1) * $chunkLen) - 1}]] } @@ -149,39 +150,39 @@ proc BLOB {chunkLen v} { } } -set Leapsecs1972 10 -set Leapsecs [list \ - 1483228800 \ - 1435708800 \ - 1341100800 \ - 1230768000 \ - 1136073600 \ - 915148800 \ - 867715200 \ - 820454400 \ - 773020800 \ - 741484800 \ - 709948800 \ - 662688000 \ - 631152000 \ - 567993600 \ - 489024000 \ - 425865600 \ - 394329600 \ - 362793600 \ - 315532800 \ - 283996800 \ - 252460800 \ - 220924800 \ - 189302400 \ - 157766400 \ - 126230400 \ - 94694400 \ - 78796800] +variable Leapsecs { + 1483228800 + 1435708800 + 1341100800 + 1230768000 + 1136073600 + 915148800 + 867715200 + 820454400 + 773020800 + 741484800 + 709948800 + 662688000 + 631152000 + 567993600 + 489024000 + 425865600 + 394329600 + 362793600 + 315532800 + 283996800 + 252460800 + 220924800 + 189302400 + 157766400 + 126230400 + 94694400 + 78796800 +} proc toTAI64 {v} { - global Leapsecs Leapsecs1972 - set diff $Leapsecs1972 + variable Leapsecs + set diff 10 for {set i 0} {$i < [llength $Leapsecs]} {incr i} { if {$v > [lindex $Leapsecs $i]} { set diff [expr {$diff + [llength $Leapsecs] - $i}] @@ -194,16 +195,16 @@ proc toTAI64 {v} { proc TAI64 {v {n 0} {a 0}} { if {$a != 0} { - yacAdd [expr 0x1A] + char [expr 0x1A] toTAI64 $v toBE 4 $n toBE 4 $a } elseif {$n != 0} { - yacAdd [expr 0x19] + char [expr 0x19] toTAI64 $v toBE 4 $n } else { - yacAdd [expr 0x18] + char [expr 0x18] toTAI64 $v } } @@ -212,13 +213,13 @@ proc UTCFromISO {v} { return [clock scan $v -format {%Y-%m-%d %H:%M:%S} -gmt 1] } -proc Raw {t v} { - yacAdd $t - global YACBuf - lappend YACBuf $v +proc RAW {t v} { + char $t + add $v } -proc YACBufGet {} { - global YACBuf - return [string cat {*}$YACBuf] +namespace export EOC NIL FALSE TRUE UUID INT STR BIN RAW +namespace export TAI64 UTCFromISO +namespace export LIST MAP LenFirstSort BLOB + } -- 2.48.1