1- namespace eval ::cluster {}
2- namespace eval ::cluster::shortid { variable i 0; variable n 1 }
1+ namespace eval ::shortid { variable i 0; variable n 1 }
32
4- proc ::cluster::shortid::shuffle { list {max {}}} {
3+ proc ::shortid::rand {min max} {
4+ expr { int(rand() * ($max - $min + 1) + $min )}
5+ }
6+
7+ proc ::shortid::shuffle { list {max {}}} {
58 set l1 {}; set l2 {}; set l3 {}; set l4 {}
69 foreach le $list [set list {}] {
710 if {rand()<.5} {
@@ -14,39 +17,39 @@ proc ::cluster::shortid::shuffle { list {max {}}} {
1417 return [concat $l1 $l2 $l3 $l4 ]
1518}
1619
17- proc ::cluster:: shortid::encode { str {type base64} } {
20+ proc ::shortid::encode { str {type base64} } {
1821 return [string map { {=} {} } [binary encode $type $str ]]
1922}
2023
21- proc ::cluster:: shortid::shuffle_string { str {max {}} } {
22- join [shuffle [split $str {}] $max ] {}
24+ proc ::shortid::shuffle_string { str {max {}} } {
25+ join [shuffle [split $str {}] $max ] {}
2326}
2427
25- proc ::cluster:: shortid::generate { {max_length 8} } {
28+ proc ::shortid::generate { {max_length 8} } {
2629
2730 set i [incr [namespace current]::i]
28-
31+
2932 lassign [shuffle [list 1 2 3 4]] 1 2 3 4
30-
33+
3134 set clicks [clock clicks]
3235 set cmds [string map { {=} {} } [binary encode base64 [join [shuffle [info commands] 20] {}]]]
3336
3437 set cl [expr { [string length $cmds ] - 5 }]
35- set c1 [::cluster ::rand $1 $cl ]; set c2 [::cluster ::rand $3 $cl ]
36-
38+ set c1 [::shortid ::rand $1 $cl ]; set c2 [::shortid ::rand $3 $cl ]
39+
3740 set uuid [string range [shuffle_string [format {%s%s%s} \
3841 [ incr i ] \
3942 [ string range $cmds $c1 [expr { $c1 + 8 }] ] \
4043 [ string range $cmds $c2 [expr { $c2 + 8 }] ]
41- ] ] $1 [expr { $1 + [expr { round ( $max_length / 2 ) }] }]]
42-
44+ ] ] $1 [expr { $1 + [expr {round( $max_length / 2 )}] }]]
45+
4346 switch -- $1 {
4447 1 { set op1 [incr [namespace current]::n]${i}$::tcl_platform(os)$::tcl_platform(osVersion) }
4548 2 { set op1 $::tcl_platform(machine)${i}$::tcl_platform(user) [incr [namespace current]::n] }
4649 3 { set op1 [string range $cmds 5 30][expr { int( rand() * [info cmdcount] + 1 ) }] }
4750 4 { set op1 $cmds }
4851 }
49-
52+
5053 switch -- $2 {
5154 1 { set op2 $::tcl_platform(os) [incr [namespace current]::n][pid ] }
5255 2 { set op2 [incr [namespace current]::n]$::tcl_platform(machine)$::tcl_platform(user)${i} }
@@ -55,21 +58,21 @@ proc ::cluster::shortid::generate { {max_length 8} } {
5558 }
5659
5760 lassign [shuffle [list 1 2 3 4]] 1 2 3 4
58-
61+
5962 binary scan [ format {%s%s} \
6063 [string range $op1 $1 [expr { $1 + 8 }]] [string range [encode $op2 uuencode] $2 [expr { $2 + 8 }]] \
6164 ] H* h1
62-
65+
6366 binary scan [ format {%s%s} \
6467 [string range $op2 $3 [expr { $3 + 8 }]] [string range [encode $op1 uuencode] $4 [expr { $4 + 8 }]] \
6568 ] h* h2
66-
67- append uuid [string range [ shuffle_string [ format {%s%s%s%s} \
69+
70+ append uuid [string range [shuffle_string [ format {%s%s%s%s} \
6871 $h1 $i [string toupper $h2 ] [string range $clicks 9 14] \
6972 ] ] 0 [ expr { $max_length - [string length $uuid ] }]]
7073
7174 return [string range $uuid 0 [expr { $max_length - 1 }]]
72-
75+
7376}
7477
75- proc ::cluster:: shortid { {max_length 8} } { tailcall ::cluster ::shortid::generate $max_length }
78+ proc ::shortid { {max_length 8} } { tailcall ::shortid::generate $max_length }
0 commit comments